diff --git a/source/app.d b/source/app.d index 46d52a6..60b3cd2 100644 --- a/source/app.d +++ b/source/app.d @@ -5,8 +5,8 @@ import asdf: deserialize, serializeToJson; import util; void main() { - init("syt_c29mdHBpZ2VvbmVz_QqNnkNjuuAffMKfGfUEO_0SqrAx", "hot-chilli.im"); - sync(); + init("", "hot-chilli.im"); + sync; } HTTP http; @@ -14,6 +14,7 @@ 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"); } @@ -29,43 +30,38 @@ void send(T)(T content, string roomid, string type = "m.room.message") { // } void sync() { - import core.memory; - import object: destroy; + 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; - writeln(content.next_batch); - destroy(content); - GC.free(&content); 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("Yasno << Ponyatno"), room); + 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); - writeln(room, ": ", evt.body); - switch (evt.body) { - case "!huy": - send(MSG(":orehussmile:", - `:orehussmile:`), room); - break; - case "!версия": send(MSG("Compiler version: "~intToStr(__VERSION__)), room); break; - case "скунс": send(MSG("еблан"), room); break; - case "!avatar": - string url = cast(string)get(homeserver~"/_matrix/client/v3/profile/"~event.sender~"/avatar_url"); - if (url == "{}") {send(MSG("Avatar is empty"), room); break;} - send(MSG(event.sender, ``), room); - break; - default: break; + 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); @@ -75,12 +71,4 @@ void sync() { } } } -} - -string intToStr(T)(T num) { - char[] buf; - for(short i; num > 0; ++i) { - buf = (num % 10 + '0')~buf; - num /= 10; - } return cast(string)buf; } \ No newline at end of file diff --git a/source/commands.d b/source/commands.d new file mode 100644 index 0000000..9b0537d --- /dev/null +++ b/source/commands.d @@ -0,0 +1,29 @@ +module commands; +import util; +enum Command; // UDA + +// auto avatar(string[] arguments) @Command { +// string url = cast(string)get(homeserver~"/_matrix/client/v3/profile/"~event.sender~"/avatar_url"); +// if (url == "{}") +// return MSG("User has no avatar"); +// return MSG(event.sender, ``); +// } + +@Command string[2] huy = [":orehussmile:", + `:orehussmile:`]; +@Command string ver = "SkunkyBot Pre-Alpha 0.1 :: https://git.bloat.cat/skunky/skunkybot-d"; +static @Command string compver = "Compiler version: "~intToStr(__VERSION__); + +// switch (evt.body) { +// case "!huy": +// send(MSG(":orehussmile:", +// ), room); +// break; +// case "!версия": send(), room); break; +// case "скунс": send(MSG("еблан"), room); break; +// case "!avatar": +// +// break; +// default: break; +// } \ No newline at end of file diff --git a/source/util.d b/source/util.d index 88c248d..497ce86 100644 --- a/source/util.d +++ b/source/util.d @@ -1,8 +1,16 @@ module util; import asdf; +string intToStr(T)(T num) { + char[] buf; + for(short i; num > 0; ++i) { + buf = (num % 10 + '0')~buf; + num /= 10; + } return cast(string)buf; +} + struct JsonObject { - import std.conv: to; + import mir.conv: to; string data; SerdeException deserializeFromAsdf(Asdf data) {