#include #include #include #include #include #include #include #include #include static void ContactsWrite(HashMap *); static char * ContactsDecode(const char *in) { size_t len = strlen(in); char *out = Malloc(len + 1); size_t i = 0, j = 0; while (i < len) { if (in[i] == '=' && i + 2 < len) { char a = in[i + 1], b = in[i + 2], c = 0; if (a >= 'A' && a <= 'F') a = (char) (a - 'A' + 'a'); if (b >= 'A' && b <= 'F') b = (char) (b - 'A' + 'a'); if (a == '4' && b == '0') c = '@'; else if (a == '2' && b == 'e') c = '.'; else if (a == '3' && b == 'a') c = ':'; else if (a == '2' && b == 'b') c = '+'; else if (a == '5' && b == 'f') c = '_'; if (c) { out[j++] = c; i += 3; continue; } } out[j++] = in[i++]; } out[j] = '\0'; return out; } static int ContactsIsPhone(const char *s) { size_t n = 0; if (*s == '+') s++; while (*s) { if (*s < '0' || *s > '9') return 0; s++; n++; } return n >= 6; } static const char * ContactsStripPrefix(const char *s, int *phoneOk) { static const char *phonePrefixes[] = { "whatsapp_", "wa_", "phone_", "sms_", NULL }; static const char *otherPrefixes[] = { "telegram_", "signal_", "discord_", "irc_", "mail_", "email_", NULL }; size_t i; *phoneOk = 1; for (i = 0; otherPrefixes[i]; i++) { size_t n = strlen(otherPrefixes[i]); if (!strncmp(s, otherPrefixes[i], n)) { *phoneOk = 0; return s + n; } } for (i = 0; phonePrefixes[i]; i++) { size_t n = strlen(phonePrefixes[i]); if (!strncmp(s, phonePrefixes[i], n)) { return s + n; } } return s; } static void ContactsSet(HashMap *map, const char *key, JsonValue *value) { JsonValue *old = HashMapSet(map, key, value); if (old) { JsonValueFree(old); } } static void ContactsRecord(HashMap *contacts, const char *userId, const char *roomId, HashMap *member) { const char *colon; const char *display = NULL; const char *raw; const char *id; const char *kind; char *local; char *decoded; int phoneOk; HashMap *existing; HashMap *record; if (!userId || userId[0] != '@') { return; } colon = strchr(userId, ':'); if (!colon || colon == userId + 1) { return; } local = StrSubstr(userId, 1, (size_t) (colon - userId)); if (!local) { return; } decoded = ContactsDecode(local); Free(local); if (!decoded) { return; } raw = ContactsStripPrefix(decoded, &phoneOk); if (phoneOk && ContactsIsPhone(raw)) { kind = "phone"; id = raw; } else if (strchr(raw, '@') && strchr(raw, '.')) { kind = "email"; id = raw; } else { kind = "username"; id = raw; } if (member) { display = JsonValueAsString(HashMapGet(member, "displayname")); } existing = JsonValueAsObject(HashMapGet(contacts, userId)); if (!display || !display[0]) { display = raw; } record = existing ? existing : HashMapCreate(); ContactsSet(record, "name", JsonValueString(display)); ContactsSet(record, "id", JsonValueString(id)); ContactsSet(record, "kind", JsonValueString(kind)); if (kind[0] == 'p') { ContactsSet(record, "number", JsonValueString(id)); } else if (kind[0] == 'e') { ContactsSet(record, "email", JsonValueString(id)); } else { ContactsSet(record, "username", JsonValueString(id)); } ContactsSet(record, "room", JsonValueString(roomId)); ContactsSet(record, "updated", JsonValueInteger((long) time(NULL))); if (!existing) { HashMapSet(contacts, userId, JsonValueObject(record)); } Free(decoded); } void ContactsScanRoom(Db *db, const char *roomId, HashMap *state) { HashMap *members; HashMap *contacts; DbRef *ref; char *key; void *val; if (!db || !roomId || !state) { return; } members = JsonValueAsObject(HashMapGet(state, "m.room.member")); if (!members) { return; } ref = DbLock(db, 1, "contacts"); if (!ref) { ref = DbCreate(db, 1, "contacts"); } if (!ref) { return; } contacts = DbJson(ref); if (!contacts) { DbUnlock(db, ref); return; } while (HashMapIterate(members, &key, &val)) { HashMap *member = JsonValueAsObject((JsonValue *) val); const char *m = member ? JsonValueAsString(HashMapGet(member, "membership")) : NULL; if (m && (StrEquals(m, "leave") || StrEquals(m, "ban"))) { continue; } ContactsRecord(contacts, key, roomId, member); } ContactsWrite(contacts); DbUnlock(db, ref); } void ContactsScanAll(Db *db) { DbRef *iref; Array *ids; size_t i, n; if (!db) { return; } iref = DbLock(db, 2, "rooms", "index"); if (!iref) { return; } ids = JsonValueAsArray(HashMapGet(DbJson(iref), "rooms")); n = ids ? ArraySize(ids) : 0; for (i = 0; i < n; i++) { char *rid = JsonValueAsString(ArrayGet(ids, i)); Room *room = rid ? RoomLock(db, rid) : NULL; if (room) { ContactsScanRoom(db, rid, RoomStateGet(room)); RoomUnlock(room); } } DbUnlock(db, iref); } static void ContactsWrite(HashMap *contacts) { FILE *f = fopen("contacts.tsv", "w"); char *key; void *val; if (!f) { return; } fprintf(f, "name\tkind\tid\troom\n"); while (HashMapIterate(contacts, &key, &val)) { HashMap *r = JsonValueAsObject((JsonValue *) val); const char *name = r ? JsonValueAsString(HashMapGet(r, "name")) : NULL; const char *kind = r ? JsonValueAsString(HashMapGet(r, "kind")) : NULL; const char *id = r ? JsonValueAsString(HashMapGet(r, "id")) : NULL; const char *room = r ? JsonValueAsString(HashMapGet(r, "room")) : NULL; fprintf(f, "%s\t%s\t%s\t%s\n", name ? name : "", kind ? kind : "", id ? id : "", room ? room : ""); } fclose(f); }