summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2022-10-21 09:41:29 +0200
committerGravatar default2022-10-21 09:41:29 +0200
commita52ed9be78893d1d6192bcfd76901781772fa309 (patch)
tree34d8623b2c493792a8565e36f2e74a851e45d142
parentUpdated TODO. (diff)
downloadsnac2-a52ed9be78893d1d6192bcfd76901781772fa309.tar.gz
snac2-a52ed9be78893d1d6192bcfd76901781772fa309.tar.xz
snac2-a52ed9be78893d1d6192bcfd76901781772fa309.zip
New function notify().
-rw-r--r--activitypub.c84
1 files changed, 80 insertions, 4 deletions
diff --git a/activitypub.c b/activitypub.c
index 992dbff..6aaabc9 100644
--- a/activitypub.c
+++ b/activitypub.c
@@ -626,6 +626,73 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to, char
626} 626}
627 627
628 628
629
630
631void notify(snac *snac, char *type, char *utype, char *actor, char *msg)
632/* notifies the user of relevant events */
633{
634 char *email = xs_dict_get(snac->config, "email");
635
636 /* no email address? done */
637 if (xs_is_null(email) || *email == '\0')
638 return;
639
640 if (strcmp(type, "Create") == 0) {
641 /* only notify of notes specifically for us */
642 char *rcpts = recipient_list(snac, msg, 0);
643
644 if (xs_list_in(rcpts, snac->actor) == -1)
645 return;
646 }
647
648 if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
649 return;
650
651 snac_debug(snac, 1, xs_fmt("notify(%s, %s, %s)", type, utype, actor));
652
653 /* now write */
654 FILE *f;
655
656 if ((f = popen("/usr/sbin/sendmail -t", "w")) == NULL) {
657 snac_log(snac, xs_fmt("cannot pipe to sendmail (errno: %d)", errno));
658 return;
659 }
660
661 xs *subject = xs_fmt("snac notify for @%s@%s",
662 xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
663 xs *from = xs_fmt("snac-daemon@%s (snac daemon)", xs_dict_get(srv_config, "host"));
664
665 fprintf(f, "From: %s\n", from);
666 fprintf(f, "To: %s\n", email);
667 fprintf(f, "Subject: %s\n", subject);
668 fprintf(f, "\n");
669
670 fprintf(f, "Type : %s", type);
671
672 if (strcmp(utype, "(null)") != 0)
673 fprintf(f, " + %s", utype);
674 fprintf(f, "\n");
675
676 fprintf(f, "Actor : %s\n", actor);
677
678 if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
679 /* there is a related object: add it */
680 char *object = xs_dict_get(msg, "object");
681
682 if (!xs_is_null(object)) {
683 if (xs_type(object) == XSTYPE_DICT)
684 object = xs_dict_get(object, "id");
685
686 if (!xs_is_null(object))
687 fprintf(f, "Object: %s\n", object);
688 }
689 }
690
691 if (fclose(f) == EOF)
692 snac_log(snac, xs_fmt("fclose error in pipe to sendmail (errno: %d)", errno));
693}
694
695
629/** queues **/ 696/** queues **/
630 697
631int process_message(snac *snac, char *msg, char *req) 698int process_message(snac *snac, char *msg, char *req)
@@ -636,6 +703,7 @@ int process_message(snac *snac, char *msg, char *req)
636 char *type = xs_dict_get(msg, "type"); 703 char *type = xs_dict_get(msg, "type");
637 xs *actor_o = NULL; 704 xs *actor_o = NULL;
638 int a_status; 705 int a_status;
706 int do_notify = 0;
639 707
640 char *object, *utype; 708 char *object, *utype;
641 709
@@ -687,12 +755,15 @@ int process_message(snac *snac, char *msg, char *req)
687 follower_add(snac, actor, f_msg); 755 follower_add(snac, actor, f_msg);
688 756
689 snac_log(snac, xs_fmt("New follower %s", actor)); 757 snac_log(snac, xs_fmt("New follower %s", actor));
758 do_notify = 1;
690 } 759 }
691 else 760 else
692 if (strcmp(type, "Undo") == 0) { 761 if (strcmp(type, "Undo") == 0) {
693 if (strcmp(utype, "Follow") == 0) { 762 if (strcmp(utype, "Follow") == 0) {
694 if (valid_status(follower_del(snac, actor))) 763 if (valid_status(follower_del(snac, actor))) {
695 snac_log(snac, xs_fmt("no longer following us %s", actor)); 764 snac_log(snac, xs_fmt("no longer following us %s", actor));
765 do_notify = 1;
766 }
696 else 767 else
697 snac_log(snac, xs_fmt("error deleting follower %s", actor)); 768 snac_log(snac, xs_fmt("error deleting follower %s", actor));
698 } 769 }
@@ -710,8 +781,10 @@ int process_message(snac *snac, char *msg, char *req)
710 781
711 timeline_request(snac, in_reply_to, NULL); 782 timeline_request(snac, in_reply_to, NULL);
712 783
713 if (timeline_add(snac, id, object, in_reply_to, NULL)) 784 if (timeline_add(snac, id, object, in_reply_to, NULL)) {
714 snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id)); 785 snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
786 do_notify = 1;
787 }
715 } 788 }
716 } 789 }
717 else 790 else
@@ -737,6 +810,7 @@ int process_message(snac *snac, char *msg, char *req)
737 810
738 timeline_admire(snac, object, actor, 1); 811 timeline_admire(snac, object, actor, 1);
739 snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object)); 812 snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
813 do_notify = 1;
740 } 814 }
741 else 815 else
742 if (strcmp(type, "Announce") == 0) { 816 if (strcmp(type, "Announce") == 0) {
@@ -757,6 +831,7 @@ int process_message(snac *snac, char *msg, char *req)
757 if (valid_status(actor_request(snac, who, &who_o))) { 831 if (valid_status(actor_request(snac, who, &who_o))) {
758 timeline_admire(snac, object, actor, 0); 832 timeline_admire(snac, object, actor, 0);
759 snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object)); 833 snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
834 do_notify = 1;
760 } 835 }
761 else 836 else
762 snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who)); 837 snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
@@ -789,6 +864,9 @@ int process_message(snac *snac, char *msg, char *req)
789 else 864 else
790 snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type)); 865 snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
791 866
867 if (do_notify)
868 notify(snac, type, utype, actor, msg);
869
792 return 1; 870 return 1;
793} 871}
794 872
@@ -869,8 +947,6 @@ void post(snac *snac, char *msg)
869 enqueue_output(snac, msg, v, 0); 947 enqueue_output(snac, msg, v, 0);
870 } 948 }
871} 949}
872
873
874/** HTTP handlers */ 950/** HTTP handlers */
875 951
876int activitypub_get_handler(d_char *req, char *q_path, 952int activitypub_get_handler(d_char *req, char *q_path,