skunkybot-d/source/app.d
2025-01-08 17:24:48 +03:00

86 lines
2.6 KiB
D

import std.stdio: writeln;
import std.net.curl;
import asdf: deserialize, serializeToJson;
import util;
void main() {
init("syt_c29mdHBpZ2VvbmVz_QqNnkNjuuAffMKfGfUEO_0SqrAx", "hot-chilli.im");
sync();
}
HTTP http;
string homeserver;
void init(string token, string hs) {
http = HTTP();
http.addRequestHeader("Authorization", "Bearer " ~ token);
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 core.memory;
import object: destroy;
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);
}
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:",
`<img data-mx-emoticon height="32" src="mxc://4d2.org/XvWYAuhASYRHtYvtspsrWvtU" alt=":orehussmile:" title=":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, `<img src="`~url[15..$-2]~`">`), room);
break;
default: break;
}
} catch (Exception e) {
writeln(e.msg);
send(MSG(e.msg), room);
}
}
}
}
}
}
string intToStr(T)(T num) {
char[] buf;
for(short i; num > 0; ++i) {
buf = (num % 10 + '0')~buf;
num /= 10;
} return cast(string)buf;
}