diff --git a/src/Config.c b/src/Config.c index 5c87c85..c19b5a8 100644 --- a/src/Config.c +++ b/src/Config.c @@ -230,3 +230,24 @@ ConfigLogLevelToSyslog(ConfigLogLevel level) } return LOG_INFO; } + + +char * +ConfigGetServerName(Db * db) +{ + char *name; + + Config config; + + ConfigLock(db, &config); + if (!config.ok) + { + return NULL; + } + + name = StrDuplicate(config.serverName); + + ConfigUnlock(&config); + + return name; +} diff --git a/src/Room.c b/src/Room.c index 65a7703..335eefe 100644 --- a/src/Room.c +++ b/src/Room.c @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -2215,25 +2216,6 @@ end: return ret; } -static char * -GetServerName(Db * db) -{ - char *name; - - Config config; - - ConfigLock(db, &config); - if (!config.ok) - { - return NULL; - } - - name = StrDuplicate(config.serverName); - - ConfigUnlock(&config); - - return name; -} bool RoomJoin(Room *room, User *user) { @@ -2250,7 +2232,7 @@ RoomJoin(Room *room, User *user) return false; } - server = GetServerName(room->db); + server = ConfigGetServerName(room->db); if (!server) { return false; diff --git a/src/Routes/RouteFetchEvent.c b/src/Routes/RouteFetchEvent.c index 62f015b..1f49ed8 100644 --- a/src/Routes/RouteFetchEvent.c +++ b/src/Routes/RouteFetchEvent.c @@ -30,32 +30,14 @@ #include #include -#include -#include #include +#include +#include +#include + #include -static char * -GetServerName(Db * db) -{ - char *name; - - Config config; - - ConfigLock(db, &config); - if (!config.ok) - { - return NULL; - } - - name = StrDuplicate(config.serverName); - - ConfigUnlock(&config); - - return name; -} - ROUTE_IMPL(RouteFetchEvent, path, argp) { RouteArgs *args = argp; @@ -90,7 +72,7 @@ ROUTE_IMPL(RouteFetchEvent, path, argp) goto finish; } - serverName = GetServerName(db); + serverName = ConfigGetServerName(db); if (!serverName) { HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); diff --git a/src/Routes/RouteFilter.c b/src/Routes/RouteFilter.c index 55bfc32..89380fe 100644 --- a/src/Routes/RouteFilter.c +++ b/src/Routes/RouteFilter.c @@ -30,31 +30,13 @@ #include #include -#include #include +#include +#include + #include -static char * -GetServerName(Db * db) -{ - char *name; - - Config config; - - ConfigLock(db, &config); - if (!config.ok) - { - return NULL; - } - - name = StrDuplicate(config.serverName); - - ConfigUnlock(&config); - - return name; -} - ROUTE_IMPL(RouteFilter, path, argp) { RouteArgs *args = argp; @@ -80,7 +62,7 @@ ROUTE_IMPL(RouteFilter, path, argp) return MatrixErrorCreate(M_UNKNOWN, NULL); } - serverName = GetServerName(db); + serverName = ConfigGetServerName(db); if (!serverName) { HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); diff --git a/src/Routes/RouteJoinRoom.c b/src/Routes/RouteJoinRoom.c index 1069035..20c9d50 100644 --- a/src/Routes/RouteJoinRoom.c +++ b/src/Routes/RouteJoinRoom.c @@ -30,32 +30,14 @@ #include #include -#include -#include #include +#include +#include +#include + #include -static char * -GetServerName(Db * db) -{ - char *name; - - Config config; - - ConfigLock(db, &config); - if (!config.ok) - { - return NULL; - } - - name = StrDuplicate(config.serverName); - - ConfigUnlock(&config); - - return name; -} - ROUTE_IMPL(RouteJoinRoom, path, argp) { RouteArgs *args = argp; @@ -97,7 +79,7 @@ ROUTE_IMPL(RouteJoinRoom, path, argp) goto finish; } - serverName = GetServerName(db); + serverName = ConfigGetServerName(db); if (!serverName) { HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); diff --git a/src/Routes/RouteJoinRoomAlias.c b/src/Routes/RouteJoinRoomAlias.c index a3e23a3..1cd1e15 100644 --- a/src/Routes/RouteJoinRoomAlias.c +++ b/src/Routes/RouteJoinRoomAlias.c @@ -30,32 +30,14 @@ #include #include -#include -#include #include +#include +#include +#include + #include -static char * -GetServerName(Db * db) -{ - char *name; - - Config config; - - ConfigLock(db, &config); - if (!config.ok) - { - return NULL; - } - - name = StrDuplicate(config.serverName); - - ConfigUnlock(&config); - - return name; -} - ROUTE_IMPL(RouteJoinRoomAlias, path, argp) { RouteArgs *args = argp; @@ -112,7 +94,7 @@ ROUTE_IMPL(RouteJoinRoomAlias, path, argp) goto finish; } - serverName = GetServerName(db); + serverName = ConfigGetServerName(db); if (!serverName) { HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); diff --git a/src/Routes/RouteMessages.c b/src/Routes/RouteMessages.c index fa89694..8ba3f9b 100644 --- a/src/Routes/RouteMessages.c +++ b/src/Routes/RouteMessages.c @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -38,26 +39,6 @@ #include -static char * -GetServerName(Db * db) -{ - char *name; - - Config config; - - ConfigLock(db, &config); - if (!config.ok) - { - return NULL; - } - - name = StrDuplicate(config.serverName); - - ConfigUnlock(&config); - - return name; -} - ROUTE_IMPL(RouteMessages, path, argp) { RouteArgs *args = argp; @@ -102,7 +83,7 @@ ROUTE_IMPL(RouteMessages, path, argp) goto finish; } - serverName = GetServerName(db); + serverName = ConfigGetServerName(db); if (!serverName) { HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); diff --git a/src/Routes/RouteSendEvent.c b/src/Routes/RouteSendEvent.c index a4ad505..784f228 100644 --- a/src/Routes/RouteSendEvent.c +++ b/src/Routes/RouteSendEvent.c @@ -30,32 +30,14 @@ #include #include -#include -#include #include +#include +#include +#include + #include -static char * -GetServerName(Db * db) -{ - char *name; - - Config config; - - ConfigLock(db, &config); - if (!config.ok) - { - return NULL; - } - - name = StrDuplicate(config.serverName); - - ConfigUnlock(&config); - - return name; -} - ROUTE_IMPL(RouteSendEvent, path, argp) { RouteArgs *args = argp; @@ -93,7 +75,7 @@ ROUTE_IMPL(RouteSendEvent, path, argp) goto finish; } - serverName = GetServerName(db); + serverName = ConfigGetServerName(db); if (!serverName) { HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); @@ -209,7 +191,7 @@ ROUTE_IMPL(RouteSendState, path, argp) goto finish; } - serverName = GetServerName(db); + serverName = ConfigGetServerName(db); if (!serverName) { HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); diff --git a/src/Routes/RouteUserProfile.c b/src/Routes/RouteUserProfile.c index a4fc870..376114b 100644 --- a/src/Routes/RouteUserProfile.c +++ b/src/Routes/RouteUserProfile.c @@ -28,10 +28,52 @@ #include #include -#include #include #include +#include +#include +#include + +static void +SendMembership(Db *db, User *user) +{ + char *displayname = UserGetProfile(user, "displayname"); + char *avatar_url = UserGetProfile(user, "avatar_url"); + Array *joins = UserListJoins(user); + size_t i; + + char *server_name = ConfigGetServerName(db); + + CommonID *user_id = UserIdParse(UserGetName(user), server_name); + char *sender = ParserRecomposeCommonID(*user_id); + Free(server_name); + + for (i = 0; i < ArraySize(joins); i++) + { + char *room_id = ArrayGet(joins, i); + Room *room = RoomLock(db, room_id); + HashMap *content = HashMapCreate(); + HashMap *membership = RoomEventCreate( + sender, "m.room.member", sender, + content + ); + + HashMapSet(content, "membership", JsonValueString("join")); + HashMapSet(content, "displayname", JsonValueString(displayname)); + HashMapSet(content, "avatar_url", JsonValueString(avatar_url)); + + JsonFree(RoomEventSend(room, membership)); + JsonFree(membership); + + RoomUnlock(room); + } + + UserFreeList(joins); + UserIdFree(user_id); + Free(sender); +} + ROUTE_IMPL(RouteUserProfile, path, argp) { RouteArgs *args = argp; @@ -142,6 +184,8 @@ ROUTE_IMPL(RouteUserProfile, path, argp) response = MatrixErrorCreate(M_UNKNOWN_TOKEN, NULL); goto finish; } + + /* TODO: This may change in the future. */ entry = ArrayGet(path, 1); if (StrEquals(entry, "displayname") || StrEquals(entry, "avatar_url")) @@ -153,6 +197,7 @@ ROUTE_IMPL(RouteUserProfile, path, argp) /* TODO: Make UserSetProfile notify other parties of * the change */ UserSetProfile(user, entry, value); + SendMembership(db, user); response = HashMapCreate(); goto finish; } diff --git a/src/User.c b/src/User.c index 8e12773..3209ba8 100644 --- a/src/User.c +++ b/src/User.c @@ -83,7 +83,7 @@ UserValidate(char *localpart, char *domain) bool UserHistoricalValidate(char *localpart, char *domain) { - size_t maxLen = 255 - strlen(domain) - 1; + size_t maxLen = 255 - (domain ? strlen(domain) : 0) - 1; size_t i = 0; while (localpart[i]) @@ -949,7 +949,7 @@ UserIdParse(char *id, char *defaultServer) userId->local = StrDuplicate(id); ParseServerPart(defaultServer, &userId->server); } - + userId->sigil = '@'; server = ParserRecomposeServerPart(userId->server); if (!UserHistoricalValidate(userId->local, server)) { diff --git a/src/include/Config.h b/src/include/Config.h index 6d925b7..530ac79 100644 --- a/src/include/Config.h +++ b/src/include/Config.h @@ -98,6 +98,16 @@ extern void ConfigLock(Db *, Config *); */ extern int ConfigUnlock(Config *); +/** + * Returns the configuration's server name from a database. + * This is an utility function that masks behind + * .Fn ConfigLock , + * and the value returned lives on the heap, and must be freed + * with + * .Fn Free . + */ +extern char * ConfigGetServerName(Db *); + /** * Converts a ConfigLogLevel into a valid syslog level. */