mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 15:15:10 +00:00
[MOD/WIP] Use actual router
This commit is contained in:
parent
307fe6a341
commit
0ee79cc586
7 changed files with 183 additions and 87 deletions
75
src/Commands/BanUser.c
Normal file
75
src/Commands/BanUser.c
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#include <Routes.h>
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
|
||||
#include <Matrix.h>
|
||||
#include <AS.h>
|
||||
|
||||
CommandHead(CmdBanUser, cmd, argp)
|
||||
{
|
||||
ParseeCmdArg *args = argp;
|
||||
ParseeData *data = args->data;
|
||||
HashMap *event = args->event;
|
||||
char *user = HashMapGet(cmd->arguments, "user");
|
||||
char *room = HashMapGet(cmd->arguments, "room");
|
||||
char *msg;
|
||||
char *msgtype = GrabString(event, 2, "content", "msgtype");
|
||||
char *body = GrabString(event, 2, "content", "body");
|
||||
char *id = GrabString(event, 1, "room_id");
|
||||
char *ev_id = GrabString(event, 1, "event_id");
|
||||
char *sender = GrabString(event, 1, "sender");
|
||||
char *profile = StrConcat(4,
|
||||
"@", data->config->sender_localpart,
|
||||
":", data->config->homeserver_host
|
||||
);
|
||||
|
||||
if (!user || !room)
|
||||
{
|
||||
Free(profile);
|
||||
return;
|
||||
}
|
||||
ASBan(data->config, room, user);
|
||||
|
||||
msg = StrConcat(3, "Banning '", user, "'...");
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice(msg)
|
||||
));
|
||||
Free(msg);
|
||||
Free(profile);
|
||||
}
|
||||
CommandHead(CmdNoFlyList, cmd, argp)
|
||||
{
|
||||
ParseeCmdArg *args = argp;
|
||||
ParseeData *data = args->data;
|
||||
HashMap *event = args->event;
|
||||
char *user = HashMapGet(cmd->arguments, "user");
|
||||
char *msg;
|
||||
char *msgtype = GrabString(event, 2, "content", "msgtype");
|
||||
char *body = GrabString(event, 2, "content", "body");
|
||||
char *id = GrabString(event, 1, "room_id");
|
||||
char *ev_id = GrabString(event, 1, "event_id");
|
||||
char *sender = GrabString(event, 1, "sender");
|
||||
char *profile = StrConcat(4,
|
||||
"@", data->config->sender_localpart,
|
||||
":", data->config->homeserver_host
|
||||
);
|
||||
|
||||
if (!user)
|
||||
{
|
||||
Free(profile);
|
||||
return;
|
||||
}
|
||||
|
||||
msg = StrConcat(3, "Banning '", user, "'...");
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice(msg)
|
||||
));
|
||||
Free(msg);
|
||||
ParseeGlobalBan(data, user);
|
||||
Free(profile);
|
||||
}
|
||||
49
src/Commands/ListBans.c
Normal file
49
src/Commands/ListBans.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include <Routes.h>
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
|
||||
#include <Matrix.h>
|
||||
#include <AS.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
CommandHead(CmdListBans, cmd, argp)
|
||||
{
|
||||
ParseeCmdArg *args = argp;
|
||||
ParseeData *data = args->data;
|
||||
HashMap *json, *event = args->event;
|
||||
DbRef *listed;
|
||||
char *str = NULL, *tmp = NULL, *banned;
|
||||
char *profile = StrConcat(4,
|
||||
"@", data->config->sender_localpart,
|
||||
":", data->config->homeserver_host
|
||||
);
|
||||
char *id = GrabString(event, 1, "room_id");
|
||||
JsonValue *val;
|
||||
|
||||
listed = DbLock(data->db, 1, "global_bans");
|
||||
json = DbJson(listed);
|
||||
|
||||
while (HashMapIterate(json, &banned, (void **) &val))
|
||||
{
|
||||
tmp = str;
|
||||
str = StrConcat(4, str, "- ", banned, "\n");
|
||||
Free(tmp);
|
||||
}
|
||||
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice("No-fly listed users:")
|
||||
));
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice(str)
|
||||
));
|
||||
|
||||
Free(str);
|
||||
DbUnlock(data->db, listed);
|
||||
Free(profile);
|
||||
}
|
||||
28
src/Commands/SetPL.c
Normal file
28
src/Commands/SetPL.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <Routes.h>
|
||||
|
||||
#include <AS.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
CommandHead(CmdSetPL, cmd, argp)
|
||||
{
|
||||
ParseeCmdArg *args = argp;
|
||||
ParseeData *data = args->data;
|
||||
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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
map = ASGetPL(data->config, room);
|
||||
JsonValueFree(JsonSet(
|
||||
map, JsonValueInteger(pl),
|
||||
2, "users", user
|
||||
));
|
||||
ASSetPL(data->config, room, map);
|
||||
}
|
||||
|
|
@ -10,7 +10,6 @@
|
|||
#include <Matrix.h>
|
||||
#include <AS.h>
|
||||
|
||||
#define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__))
|
||||
static void
|
||||
ParseeMemberHandler(ParseeData *data, HashMap *event)
|
||||
{
|
||||
|
|
@ -106,95 +105,15 @@ ParseeBotHandler(ParseeData *data, HashMap *event)
|
|||
return;
|
||||
}
|
||||
body++;
|
||||
#define BAN_USER "ban-user"
|
||||
#define BAN_LIST "ban-list"
|
||||
#define LS_USERS "ls-flying"
|
||||
|
||||
cmd = CommandParse(body);
|
||||
if (cmd && StrEquals(cmd->command, BAN_USER))
|
||||
{
|
||||
char *user = HashMapGet(cmd->arguments, "user");
|
||||
char *room = HashMapGet(cmd->arguments, "room");
|
||||
char *msg;
|
||||
ParseeCmdArg arg;
|
||||
arg.data = data;
|
||||
arg.event = event;
|
||||
|
||||
if (!user || !room)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
ASBan(data->config, room, user);
|
||||
|
||||
msg = StrConcat(3, "Banning '", user, "'...");
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice(msg)
|
||||
));
|
||||
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");
|
||||
HashMap *json = DbJson(listed);
|
||||
char *str = NULL, *tmp = NULL, *banned;
|
||||
JsonValue *val;
|
||||
|
||||
while (HashMapIterate(json, &banned, (void **) &val))
|
||||
{
|
||||
tmp = str;
|
||||
str = StrConcat(4, str, "- ", banned, "\n");
|
||||
Free(tmp);
|
||||
}
|
||||
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice("No-fly listed users:")
|
||||
));
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice(str)
|
||||
));
|
||||
|
||||
Free(str);
|
||||
DbUnlock(data->db, listed);
|
||||
}
|
||||
else if (cmd && StrEquals(cmd->command, BAN_LIST))
|
||||
{
|
||||
char *user = HashMapGet(cmd->arguments, "user");
|
||||
char *msg;
|
||||
|
||||
if (!user)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
msg = StrConcat(3, "Banning '", user, "'...");
|
||||
Free(ASSend(
|
||||
data->config, id, profile,
|
||||
"m.room.message",
|
||||
MatrixCreateNotice(msg)
|
||||
));
|
||||
Free(msg);
|
||||
ParseeGlobalBan(data, user);
|
||||
Log(LOG_INFO, "Routing...");
|
||||
RouteCommand(data->handler, cmd, &arg);
|
||||
}
|
||||
end:
|
||||
Free(profile);
|
||||
|
|
|
|||
|
|
@ -36,6 +36,9 @@ ParseeInitData(XMPPComponent *comp)
|
|||
while (0);
|
||||
ROUTES
|
||||
#undef X_ROUTE
|
||||
#define X_COMMAND(path, name) CommandAddCommand(data->handler, path, name);
|
||||
COMMANDS
|
||||
#undef X_COMMAND
|
||||
return data;
|
||||
}
|
||||
void
|
||||
|
|
|
|||
|
|
@ -51,6 +51,12 @@ typedef struct ParseeData {
|
|||
Db *db;
|
||||
} ParseeData;
|
||||
|
||||
#define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabInteger(obj, ...) JsonValueAsInteger(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabBoolean(obj, ...) JsonValueAsBoolean(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabObject(obj, ...) JsonValueAsObject(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabArray(obj, ...) JsonValueAsArray(JsonGet(obj, __VA_ARGS__))
|
||||
|
||||
#define SECONDS * 1000
|
||||
#define MINUTES * 60 SECONDS
|
||||
#define HOURS * 60 MINUTES
|
||||
|
|
|
|||
|
|
@ -8,6 +8,22 @@ typedef struct ParseeHttpArg {
|
|||
Stream *stream;
|
||||
} ParseeHttpArg;
|
||||
|
||||
typedef struct ParseeCmdArg {
|
||||
ParseeData *data;
|
||||
HashMap *event;
|
||||
} ParseeCmdArg;
|
||||
|
||||
/* A list of all commands. */
|
||||
#define COMMANDS X_COMMAND("ban-user", CmdBanUser) \
|
||||
X_COMMAND("ban-list", CmdNoFlyList) \
|
||||
X_COMMAND("list-bans", CmdListBans) \
|
||||
X_COMMAND("set-pl", CmdSetPL) \
|
||||
|
||||
#define X_COMMAND(path, name) extern void name(Command *, void *);
|
||||
COMMANDS
|
||||
#undef X_COMMAND
|
||||
#define CommandHead(name, cmd, argp) void \
|
||||
name(Command * cmd, void *argp)
|
||||
/* A list of all routes. */
|
||||
#define ROUTES X_ROUTE("/", RouteRoot) \
|
||||
X_ROUTE("/_matrix/app/v1/transactions/(.*)", RouteTxns) \
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue