diff --git a/src/AS.c b/src/AS.c index 5cf82bb..0ac5bd6 100644 --- a/src/AS.c +++ b/src/AS.c @@ -422,6 +422,50 @@ ASGetName(const ParseeConfig *c, char *room, char *user) } return ret; } +HashMap * +ASGetPL(const ParseeConfig *c, char *room) +{ + char *path; + HttpClientContext *ctx; + HashMap *reply; + if (!c || !room) + { + return NULL; + } + room = HttpUrlEncode(room); + path = StrConcat(4, + "/_matrix/client/v3/rooms/", room, + "/state/m.room.power_levels/", "" + ); + ctx = ParseeCreateRequest(c, HTTP_GET, path); + Free(room); + ASAuthenticateRequest(c, ctx); + HttpRequestSendHeaders(ctx); + HttpRequestSend(ctx); + + reply = JsonDecode(HttpClientStream(ctx)); + + HttpClientContextFree(ctx); + Free(path); + + return reply; +} +void +ASSetPL(const ParseeConfig *conf, char *id, HashMap *m) +{ + char *user; + if (!conf || !id || !m) + { + return; + } + user = StrConcat(4, + "@",conf->sender_localpart, + ":",conf->homeserver_host + ); + ASSetState(conf, id, "m.room.power_levels", "", user, m); + Free(user); +} + char * ASUpload(const ParseeConfig *c, Stream *from, unsigned int size) { diff --git a/src/MatrixEventHandler.c b/src/MatrixEventHandler.c index d42a8b5..0f0fd56 100644 --- a/src/MatrixEventHandler.c +++ b/src/MatrixEventHandler.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -112,7 +113,7 @@ ParseeBotHandler(ParseeData *data, HashMap *event) if (cmd && StrEquals(cmd->command, BAN_USER)) { char *user = HashMapGet(cmd->arguments, "user"); - char *room = HashMapGet(cmd->arguments, "user"); + char *room = HashMapGet(cmd->arguments, "room"); char *msg; if (!user || !room) @@ -129,6 +130,25 @@ ParseeBotHandler(ParseeData *data, HashMap *event) )); Free(msg); } + else if (cmd && StrEquals(cmd->command, "set-pl")) + { + char *user = HashMapGet(cmd->arguments, "user"); + char *room = HashMapGet(cmd->arguments, "room"); + char *pl_str = HashMapGet(cmd->arguments, "pl"); + long pl = strtol(pl_str, NULL, 10); + HashMap *map; + + if (!user || !pl_str) + { + goto end; + } + map = ASGetPL(data->config, room); + JsonValueFree(JsonSet( + map, JsonValueInteger(pl), + 2, "users", user + )); + ASSetPL(data->config, room, map); + } else if (cmd && StrEquals(cmd->command, LS_USERS)) { DbRef *listed = DbLock(data->db, 1, "global_bans"); diff --git a/src/include/AS.h b/src/include/AS.h index 49d9757..a33ce86 100644 --- a/src/include/AS.h +++ b/src/include/AS.h @@ -39,6 +39,10 @@ extern char * ASSend(const ParseeConfig *, char *, char *, char *, HashMap *); /* Sets a state event with a specific type and body */ extern void ASSetState(const ParseeConfig *conf, char *id, char *type, char *key, char *mask, HashMap *event); +/* Gets/Sets the content for a PL event */ +extern HashMap *ASGetPL(const ParseeConfig *conf, char *id); +extern void ASSetPL(const ParseeConfig *conf, char *id, HashMap *m); + /* Creates a room, with a masquerade user as its creator. This function * returns it's ID if it exists. */ extern char * ASCreateRoom(const ParseeConfig *c, char *by, char *alias);