Parsee/src/Commands/Plumb.c

77 lines
1.8 KiB
C

#include <Routes.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Matrix.h>
#include <Bot.h>
#include <AS.h>
#include <stdlib.h>
CommandHead(CmdPlumb, cmd, argp)
{
ParseeCmdArg *args = argp;
ParseeData *data = args->data;
HashMap *event = args->event;
char *muc = NULL, *room = NULL, *chat_id = NULL;
char *room_id = NULL;
MUCInfo info = { .exists = false };
BotInitialise();
BotRequired(muc);
BotRequired(room);
/* Check MUC viability */
if (ParseeManageBan(args->data, muc, NULL) ||
!ParseeIsMUCWhitelisted(args->data, muc))
{
ReplySprintf("MUC '%s' is not allowed on this bridge.", muc);
goto end;
}
if (!XMPPQueryMUC(args->data->jabber, muc, &info))
{
ReplySprintf("MUC '%s' does not exist.", muc);
goto end;
}
if ((chat_id = ParseeGetFromMUCID(args->data, muc)))
{
ReplySprintf("MUC '%s' is already mapped to %s.", muc, chat_id);
Free(chat_id);
chat_id = NULL;
goto end;
}
/* Check room viability */
room_id = ASJoin(args->data->config, room, NULL);
if (!room_id)
{
ReplySprintf("Room '%s' does not exist.", room);
goto end;
}
if ((chat_id = ParseeGetFromRoomID(args->data, room_id)))
{
Free(chat_id);
chat_id = NULL;
ReplySprintf("Room '%s' is already mapped.", room);
goto end;
}
chat_id = ParseePushMUC(args->data, room_id, muc);
if (chat_id)
{
char *rev = StrConcat(2, muc, "/parsee");
XMPPJoinMUC(args->data->jabber, "parsee", rev, NULL, -1, false);
Free(rev);
}
ReplySprintf("Plumbed '%s'", muc);
end:
BotDestroy();
Free(chat_id);
Free(room_id);
XMPPFreeMUCInfo(info);
}