diff options
| author | 2023-02-07 07:37:23 +0100 | |
|---|---|---|
| committer | 2023-02-07 07:37:23 +0100 | |
| commit | 2db57c9df96f4bf7c9dd996f19c104da3cae6263 (patch) | |
| tree | ccdaf03c90686442cf32a44a60b4b10394b9d918 /activitypub.c | |
| parent | process_queue() now posts the q_item to the thread pool. (diff) | |
| download | snac2-2db57c9df96f4bf7c9dd996f19c104da3cae6263.tar.gz snac2-2db57c9df96f4bf7c9dd996f19c104da3cae6263.tar.xz snac2-2db57c9df96f4bf7c9dd996f19c104da3cae6263.zip | |
Added internals for Telegram notifications.
Diffstat (limited to 'activitypub.c')
| -rw-r--r-- | activitypub.c | 87 |
1 files changed, 58 insertions, 29 deletions
diff --git a/activitypub.c b/activitypub.c index 651c18c..5c98389 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -771,13 +771,8 @@ xs_dict *msg_note(snac *snac, xs_str *content, xs_val *rcpts, xs_str *in_reply_t | |||
| 771 | void notify(snac *snac, char *type, char *utype, char *actor, char *msg) | 771 | void notify(snac *snac, char *type, char *utype, char *actor, char *msg) |
| 772 | /* notifies the user of relevant events */ | 772 | /* notifies the user of relevant events */ |
| 773 | { | 773 | { |
| 774 | char *email = xs_dict_get(snac->config, "email"); | ||
| 775 | char *object = NULL; | 774 | char *object = NULL; |
| 776 | 775 | ||
| 777 | /* no email address? done */ | ||
| 778 | if (xs_is_null(email) || *email == '\0') | ||
| 779 | return; | ||
| 780 | |||
| 781 | if (strcmp(type, "Create") == 0) { | 776 | if (strcmp(type, "Create") == 0) { |
| 782 | /* only notify of notes specifically for us */ | 777 | /* only notify of notes specifically for us */ |
| 783 | xs *rcpts = recipient_list(snac, msg, 0); | 778 | xs *rcpts = recipient_list(snac, msg, 0); |
| @@ -804,21 +799,8 @@ void notify(snac *snac, char *type, char *utype, char *actor, char *msg) | |||
| 804 | } | 799 | } |
| 805 | } | 800 | } |
| 806 | 801 | ||
| 807 | snac_debug(snac, 1, xs_fmt("notify(%s, %s, %s)", type, utype, actor)); | 802 | /* prepare message body */ |
| 808 | 803 | xs *body = xs_str_new(NULL); | |
| 809 | /* prepare message */ | ||
| 810 | |||
| 811 | xs *subject = xs_fmt("snac notify for @%s@%s", | ||
| 812 | xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host")); | ||
| 813 | xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host")); | ||
| 814 | xs *header = xs_fmt( | ||
| 815 | "From: %s\n" | ||
| 816 | "To: %s\n" | ||
| 817 | "Subject: %s\n" | ||
| 818 | "\n", | ||
| 819 | from, email, subject); | ||
| 820 | |||
| 821 | xs *body = xs_str_new(header); | ||
| 822 | 804 | ||
| 823 | if (strcmp(utype, "(null)") != 0) { | 805 | if (strcmp(utype, "(null)") != 0) { |
| 824 | xs *s1 = xs_fmt("Type : %s + %s\n", type, utype); | 806 | xs *s1 = xs_fmt("Type : %s + %s\n", type, utype); |
| @@ -839,7 +821,35 @@ void notify(snac *snac, char *type, char *utype, char *actor, char *msg) | |||
| 839 | body = xs_str_cat(body, s1); | 821 | body = xs_str_cat(body, s1); |
| 840 | } | 822 | } |
| 841 | 823 | ||
| 842 | enqueue_email(body, 0); | 824 | /* email */ |
| 825 | |||
| 826 | char *email = xs_dict_get(snac->config, "email"); | ||
| 827 | |||
| 828 | if (!xs_is_null(email) && *email != '\0') { | ||
| 829 | snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor)); | ||
| 830 | |||
| 831 | xs *subject = xs_fmt("snac notify for @%s@%s", | ||
| 832 | xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host")); | ||
| 833 | xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host")); | ||
| 834 | xs *header = xs_fmt( | ||
| 835 | "From: %s\n" | ||
| 836 | "To: %s\n" | ||
| 837 | "Subject: %s\n" | ||
| 838 | "\n", | ||
| 839 | from, email, subject); | ||
| 840 | |||
| 841 | xs *email_body = xs_fmt("%s%s", header, body); | ||
| 842 | |||
| 843 | enqueue_email(email_body, 0); | ||
| 844 | } | ||
| 845 | |||
| 846 | /* telegram */ | ||
| 847 | |||
| 848 | char *bot = xs_dict_get(snac->config, "telegram_bot"); | ||
| 849 | char *chat_id = xs_dict_get(snac->config, "telegram_chat_id"); | ||
| 850 | |||
| 851 | if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id) | ||
| 852 | enqueue_telegram(body, bot, chat_id); | ||
| 843 | } | 853 | } |
| 844 | 854 | ||
| 845 | 855 | ||
| @@ -1178,17 +1188,40 @@ void process_queue_item(xs_dict *q_item) | |||
| 1178 | retries++; | 1188 | retries++; |
| 1179 | 1189 | ||
| 1180 | if (retries > queue_retry_max) | 1190 | if (retries > queue_retry_max) |
| 1181 | srv_log(xs_fmt("process_queue email giving up (errno: %d)", errno)); | 1191 | srv_log(xs_fmt("email giving up (errno: %d)", errno)); |
| 1182 | else { | 1192 | else { |
| 1183 | /* requeue */ | 1193 | /* requeue */ |
| 1184 | srv_log(xs_fmt( | 1194 | srv_log(xs_fmt( |
| 1185 | "process_queue email requeue #%d (errno: %d)", retries, errno)); | 1195 | "email requeue #%d (errno: %d)", retries, errno)); |
| 1186 | 1196 | ||
| 1187 | enqueue_email(msg, retries); | 1197 | enqueue_email(msg, retries); |
| 1188 | } | 1198 | } |
| 1189 | } | 1199 | } |
| 1190 | } | 1200 | } |
| 1191 | else | 1201 | else |
| 1202 | if (strcmp(type, "telegram") == 0) { | ||
| 1203 | /* send this via telegram */ | ||
| 1204 | char *bot = xs_dict_get(q_item, "bot"); | ||
| 1205 | char *msg = xs_dict_get(q_item, "message"); | ||
| 1206 | xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id")); | ||
| 1207 | int status = 0; | ||
| 1208 | |||
| 1209 | /* chat_id must start with a - */ | ||
| 1210 | if (!xs_startswith(chat_id, "-")) | ||
| 1211 | chat_id = xs_str_wrap_i("-", chat_id, NULL); | ||
| 1212 | |||
| 1213 | xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot); | ||
| 1214 | xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg); | ||
| 1215 | |||
| 1216 | xs *headers = xs_dict_new(); | ||
| 1217 | headers = xs_dict_append(headers, "content-type", "application/json"); | ||
| 1218 | |||
| 1219 | xs *rsp = xs_http_request("POST", url, headers, | ||
| 1220 | body, strlen(body), &status, NULL, NULL, 0); | ||
| 1221 | |||
| 1222 | srv_debug(0, xs_fmt("telegram post %d", status)); | ||
| 1223 | } | ||
| 1224 | else | ||
| 1192 | if (strcmp(type, "purge") == 0) { | 1225 | if (strcmp(type, "purge") == 0) { |
| 1193 | srv_log(xs_dup("purge start")); | 1226 | srv_log(xs_dup("purge start")); |
| 1194 | 1227 | ||
| @@ -1210,12 +1243,8 @@ void process_queue(void) | |||
| 1210 | while (xs_list_iter(&p, &fn)) { | 1243 | while (xs_list_iter(&p, &fn)) { |
| 1211 | xs *q_item = dequeue(fn); | 1244 | xs *q_item = dequeue(fn); |
| 1212 | 1245 | ||
| 1213 | if (q_item == NULL) { | 1246 | if (q_item != NULL) |
| 1214 | srv_log(xs_fmt("process_queue q_item error")); | 1247 | job_post(q_item); |
| 1215 | continue; | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | job_post(q_item); | ||
| 1219 | } | 1248 | } |
| 1220 | } | 1249 | } |
| 1221 | 1250 | ||