#include #include #include #include #include #include #include #include static char *Kinds[] = { "override", "content", "room", "sender", "underride", NULL }; static HashMap * MakeRule(char * id, int def, int enabled, Array * actions, Array * conditions) { HashMap *rule = HashMapCreate(); HashMapSet(rule, "rule_id", JsonValueString(id)); HashMapSet(rule, "default", JsonValueBoolean(def)); HashMapSet(rule, "enabled", JsonValueBoolean(enabled)); HashMapSet(rule, "actions", JsonValueArray(actions)); if (conditions) { HashMapSet(rule, "conditions", JsonValueArray(conditions)); } return rule; } static Array * ActionList(char * action) { Array *actions = ArrayCreate(); if (action) { ArrayAdd(actions, JsonValueString(action)); } return actions; } static Array * HighlightActions(void) { Array *actions = ActionList("notify"); HashMap *tweak = HashMapCreate(); HashMapSet(tweak, "set_tweak", JsonValueString("highlight")); ArrayAdd(actions, JsonValueObject(tweak)); return actions; } static HashMap * MatchCondition(char * key, char * pattern) { HashMap *condition = HashMapCreate(); HashMapSet(condition, "kind", JsonValueString("event_match")); HashMapSet(condition, "key", JsonValueString(key)); HashMapSet(condition, "pattern", JsonValueString(pattern)); return condition; } static HashMap * PropertyCondition(char * key, char * value) { HashMap *condition = HashMapCreate(); HashMapSet(condition, "kind", JsonValueString("event_property_is")); HashMapSet(condition, "key", JsonValueString(key)); HashMapSet(condition, "value", JsonValueString(value)); return condition; } static HashMap * DisplayNameCondition(void) { HashMap *condition = HashMapCreate(); HashMapSet(condition, "kind", JsonValueString("contains_display_name")); return condition; } static HashMap * DefaultRules(char * localpart) { HashMap *rules = HashMapCreate(); Array *override = ArrayCreate(); Array *content = ArrayCreate(); Array *underride = ArrayCreate(); Array *conditions; HashMap *rule; ArrayAdd(override, JsonValueObject(MakeRule(".m.rule.master", 1, 0, ArrayCreate(), NULL))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.room.message"))); ArrayAdd(conditions, JsonValueObject(MatchCondition("content.msgtype", "m.notice"))); ArrayAdd(override, JsonValueObject(MakeRule(".m.rule.suppress_notices", 1, 1, ArrayCreate(), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.room.member"))); ArrayAdd(conditions, JsonValueObject(PropertyCondition("content.membership", "invite"))); ArrayAdd(override, JsonValueObject(MakeRule(".m.rule.invite_for_me", 1, 1, ActionList("notify"), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.room.member"))); ArrayAdd(override, JsonValueObject(MakeRule(".m.rule.member_event", 1, 1, ArrayCreate(), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(DisplayNameCondition())); ArrayAdd(override, JsonValueObject(MakeRule(".m.rule.contains_display_name", 1, 1, HighlightActions(), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("content.body", "@room"))); ArrayAdd(override, JsonValueObject(MakeRule(".m.rule.roomnotif", 1, 1, HighlightActions(), conditions))); rule = MakeRule(".m.rule.contains_user_name", 1, 1, HighlightActions(), NULL); HashMapSet(rule, "pattern", JsonValueString(localpart)); ArrayAdd(content, JsonValueObject(rule)); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.call.invite"))); ArrayAdd(underride, JsonValueObject(MakeRule(".m.rule.call", 1, 1, ActionList("notify"), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.room.encrypted"))); ArrayAdd(underride, JsonValueObject(MakeRule(".m.rule.encrypted_room_one_to_one", 1, 1, ActionList("notify"), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.room.message"))); ArrayAdd(underride, JsonValueObject(MakeRule(".m.rule.room_one_to_one", 1, 1, ActionList("notify"), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.room.message"))); ArrayAdd(underride, JsonValueObject(MakeRule(".m.rule.message", 1, 1, ActionList("notify"), conditions))); conditions = ArrayCreate(); ArrayAdd(conditions, JsonValueObject(MatchCondition("type", "m.room.encrypted"))); ArrayAdd(underride, JsonValueObject(MakeRule(".m.rule.encrypted", 1, 1, ActionList("notify"), conditions))); HashMapSet(rules, "override", JsonValueArray(override)); HashMapSet(rules, "content", JsonValueArray(content)); HashMapSet(rules, "room", JsonValueArray(ArrayCreate())); HashMapSet(rules, "sender", JsonValueArray(ArrayCreate())); HashMapSet(rules, "underride", JsonValueArray(underride)); return rules; } static void CopyKinds(HashMap * dest, HashMap * src) { int i; for (i = 0; Kinds[i]; i++) { JsonValue *value = HashMapGet(src, Kinds[i]); HashMapSet(dest, Kinds[i], value ? JsonValueDuplicate(value) : JsonValueArray(ArrayCreate())); } } HashMap * PushRulesLoad(Db * db, char * localpart, char * serverName, int create) { DbRef *ref; HashMap *rules = NULL; (void) serverName; ref = DbLock(db, 2, "pushrules", localpart); if (!ref && create) { ref = DbCreate(db, 2, "pushrules", localpart); if (ref) { HashMap *defaults = DefaultRules(localpart); CopyKinds(DbJson(ref), defaults); JsonFree(defaults); } } if (ref) { rules = JsonDuplicate(DbJson(ref)); DbUnlock(db, ref); } return rules; } void PushRulesStore(Db * db, char * localpart, HashMap * rules) { DbRef *ref = DbLock(db, 2, "pushrules", localpart); if (!ref) { ref = DbCreate(db, 2, "pushrules", localpart); } if (ref) { CopyKinds(DbJson(ref), rules); DbUnlock(db, ref); } } static JsonValue * LookupDotted(HashMap * obj, char * key) { char *copy; char *part; JsonValue *value = NULL; if (!obj || !key) { return NULL; } copy = StrDuplicate(key); if (!copy) { return NULL; } part = copy; while (part && *part) { char *next = strchr(part, '.'); if (next) { *next++ = '\0'; } value = HashMapGet(obj, part); if (!value) { break; } if (next) { obj = JsonValueAsObject(value); if (!obj) { value = NULL; break; } value = NULL; } part = next; } Free(copy); return value; } static int GlobMatch(const char * pattern, const char * text) { while (*pattern) { if (*pattern == '*') { while (*pattern == '*') { pattern++; } if (!*pattern) { return 1; } while (*text) { if (GlobMatch(pattern, text)) { return 1; } text++; } return GlobMatch(pattern, text); } if (!*text) { return 0; } if (*pattern != '?' && *pattern != *text) { return 0; } pattern++; text++; } return *text == '\0'; } static int StringEqual(JsonValue * value, char * expected) { char *actual; if (!value || !expected) { return 0; } actual = JsonValueAsString(value); return actual ? StrEquals(actual, expected) : 0; } static int ConditionMatches(HashMap * rule, HashMap * event, char * displayName) { Array *conditions = JsonValueAsArray(HashMapGet(rule, "conditions")); size_t i; if (!conditions) { return 1; } for (i = 0; i < ArraySize(conditions); i++) { HashMap *condition = JsonValueAsObject(ArrayGet(conditions, i)); char *kind = condition ? JsonValueAsString(HashMapGet(condition, "kind")) : NULL; if (!kind) { return 0; } if (StrEquals(kind, "event_match")) { char *key = JsonValueAsString(HashMapGet(condition, "key")); char *pattern = JsonValueAsString(HashMapGet(condition, "pattern")); JsonValue *value = LookupDotted(event, key); char *actual = value ? JsonValueAsString(value) : NULL; if (!actual || !pattern || !GlobMatch(pattern, actual)) { return 0; } } else if (StrEquals(kind, "event_property_is")) { char *key = JsonValueAsString(HashMapGet(condition, "key")); JsonValue *expected = HashMapGet(condition, "value"); JsonValue *value = LookupDotted(event, key); if (!value || !expected) { return 0; } if (!StringEqual(value, JsonValueAsString(expected))) { return 0; } } else if (StrEquals(kind, "contains_display_name")) { JsonValue *value = LookupDotted(event, "content.body"); char *body = value ? JsonValueAsString(value) : NULL; if (!displayName || !*displayName || !body || !strstr(body, displayName)) { return 0; } } else { return 0; } } return 1; } static JsonValue * MatchRules(HashMap * rules, HashMap * event, char * displayName) { int k; if (!rules) { return NULL; } for (k = 0; Kinds[k]; k++) { Array *list = JsonValueAsArray(HashMapGet(rules, Kinds[k])); size_t i; for (i = 0; list && i < ArraySize(list); i++) { HashMap *rule = JsonValueAsObject(ArrayGet(list, i)); JsonValue *enabled = rule ? HashMapGet(rule, "enabled") : NULL; int on = enabled ? JsonValueAsBoolean(enabled) : 1; if (!rule || !on) { continue; } if (StrEquals(Kinds[k], "content")) { char *pattern = JsonValueAsString(HashMapGet(rule, "pattern")); JsonValue *value = LookupDotted(event, "content.body"); char *body = value ? JsonValueAsString(value) : NULL; if (!pattern || !body || !GlobMatch(pattern, body)) { continue; } } else if (!ConditionMatches(rule, event, displayName)) { continue; } return HashMapGet(rule, "actions"); } } return NULL; } static int WantsNotify(JsonValue * actions) { Array *list = JsonValueAsArray(actions); size_t i; if (!list) { return 0; } for (i = 0; i < ArraySize(list); i++) { char *action = JsonValueAsString(ArrayGet(list, i)); if (action && StrEquals(action, "notify")) { return 1; } } return 0; } static void AppendNotification(Db * db, char * localpart, char * roomId, HashMap * event, JsonValue * actions) { DbRef *ref = DbLock(db, 2, "notifications", localpart); HashMap *store; Array *list; JsonValue *next; uint64_t seq; HashMap *record; HashMap *copy; if (!ref) { ref = DbCreate(db, 2, "notifications", localpart); } if (!ref) { return; } store = DbJson(ref); list = JsonValueAsArray(HashMapGet(store, "notifications")); next = HashMapGet(store, "next"); if (!list) { list = ArrayCreate(); HashMapSet(store, "notifications", JsonValueArray(list)); } seq = next ? JsonValueAsInteger(next) : 0; HashMapSet(store, "next", JsonValueInteger(seq + 1)); copy = JsonDuplicate(event); HashMapDelete(copy, "_seq"); record = HashMapCreate(); HashMapSet(record, "actions", JsonValueDuplicate(actions)); HashMapSet(record, "event", JsonValueObject(copy)); HashMapSet(record, "profile_tag", JsonValueString("")); HashMapSet(record, "read", JsonValueBoolean(false)); HashMapSet(record, "room_id", JsonValueString(roomId)); HashMapSet(record, "ts", JsonValueDuplicate(HashMapGet(event, "origin_server_ts"))); HashMapSet(record, "idx", JsonValueInteger(seq)); ArrayAdd(list, JsonValueObject(record)); while (ArraySize(list) > 200) { JsonValue *old = ArrayDelete(list, 0); JsonValueFree(old); } DbUnlock(db, ref); } void PushNotifyEvent(Db * db, char * roomId, HashMap * state, HashMap * event) { HashMap *members; char *sender; char *userId; void *value; size_t i = 0; if (!db || !roomId || !state || !event) { return; } members = JsonValueAsObject(HashMapGet(state, "m.room.member")); sender = JsonValueAsString(HashMapGet(event, "sender")); if (!members) { return; } while (HashMapIterateReentrant(members, &userId, &value, &i)) { HashMap *member = JsonValueAsObject(value); char *membership; char *displayName; char *colon; char *local; size_t length; DbRef *userRef; HashMap *rules; JsonValue *actions; if (!userId || !*userId || userId[0] != '@') { continue; } if (sender && StrEquals(userId, sender)) { continue; } membership = member ? JsonValueAsString(HashMapGet(member, "membership")) : NULL; if (!membership || (!StrEquals(membership, "join") && !StrEquals(membership, "invite"))) { continue; } colon = strchr(userId, ':'); if (!colon || colon == userId + 1) { continue; } length = (size_t) (colon - userId) - 1; local = Malloc(length + 1); if (!local) { continue; } memcpy(local, userId + 1, length); local[length] = '\0'; userRef = DbLock(db, 2, "users", local); if (!userRef) { Free(local); continue; } DbUnlock(db, userRef); rules = PushRulesLoad(db, local, NULL, 1); displayName = member ? JsonValueAsString(HashMapGet(member, "displayname")) : NULL; actions = MatchRules(rules, event, displayName); if (WantsNotify(actions)) { AppendNotification(db, local, roomId, event, actions); } JsonFree(rules); Free(local); } }