74 lines
2.1 KiB
D
74 lines
2.1 KiB
D
import std.stdio: writeln;
|
|
import std.net.curl;
|
|
import asdf: deserialize, serializeToJson;
|
|
|
|
import util;
|
|
|
|
void main() {
|
|
init("", "hot-chilli.im");
|
|
sync;
|
|
}
|
|
|
|
HTTP http;
|
|
string homeserver;
|
|
void init(string token, string hs) {
|
|
http = HTTP();
|
|
http.addRequestHeader("Authorization", "Bearer " ~ token);
|
|
http.tcpNoDelay = true;
|
|
homeserver = "https://" ~ hs;
|
|
get(homeserver~"/_matrix/federation/v1/version");
|
|
}
|
|
|
|
void send(T)(T content, string roomid, string type = "m.room.message") {
|
|
import std.random: uniform;
|
|
put(homeserver~"/_matrix/client/v3/rooms/" ~ roomid ~ "/send/" ~ type ~
|
|
"/skunky-" ~ intToStr(uniform(1111_1111, 9999_9999)), content.serializeToJson, http);
|
|
}
|
|
|
|
// void join(string roomid) {
|
|
// post(homeserver~"/_matrix/client/v3/rooms/"~roomid~"/join", http);
|
|
// }
|
|
|
|
void sync() {
|
|
import commands;
|
|
|
|
string str = homeserver ~ "/_matrix/client/v3/sync?set_presence=online";
|
|
Sync content = get(str, http).deserialize!Sync; str ~= "&since=";
|
|
for (;;) {
|
|
string bthUrl = str ~ content.next_batch;
|
|
content = get(bthUrl, http).deserialize!Sync;
|
|
|
|
foreach (room, _; content.rooms.invite) {
|
|
writeln("Joining to room: ", room);
|
|
post(homeserver~"/_matrix/client/v3/rooms/"~room~"/join", null, http);
|
|
send(MSG("Привет, я СканкиБот"), room);
|
|
}
|
|
foreach (room, roomContent; content.rooms.join) {
|
|
foreach(event; roomContent.timeline.events) {
|
|
if (event.type == "m.room.message") {
|
|
try {
|
|
auto evt = deserialize!MSG(event.content.data);
|
|
foreach (member; __traits(allMembers, commands)) {
|
|
alias mmbr = __traits(getMember, commands, member);
|
|
if (evt.body[1..$] == member) {
|
|
static foreach (attr; __traits(getAttributes, mmbr)) {
|
|
static if (is(attr == Command)) {
|
|
static if (is(mmbr == function))
|
|
send(mmbr(), room);
|
|
else static if (__traits(isStaticArray, mmbr))
|
|
send(MSG(mmbr[0], mmbr[1]), room);
|
|
else
|
|
send(MSG(mmbr), room);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
writeln(e.msg);
|
|
send(MSG(e.msg), room);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |