AmendHub

Download:

jcs

/

subtext

/

amendments

/

563

mail: Add mail_to_sysop()

Errors can be mailed to the sysop instead of just getting lost in
the console log.

jcs made amendment 563 about 1 year ago
--- mail.c Thu Nov 16 13:08:22 2023 +++ mail.c Thu Nov 23 13:23:26 2023 @@ -540,6 +540,8 @@ mail_list(struct session *s, size_t nmail_ids, unsigne if (msg.ftn_msgid.point != 0) snprintf(from + size, sizeof(from) - size, ".%u", msg.ftn_msgid.point); + } else if (msg.sender_user_id == 0) { + strlcpy(from, "(System)", sizeof(from)); } else { user = user_username(msg.sender_user_id); strlcpy(from, user ? user->username : "(unknown)", @@ -608,6 +610,8 @@ mail_read(struct session *s, unsigned long id, short i if (msg.ftn_msgid.point) snprintf(from + size, sizeof(from) - size, ".%u", msg.ftn_msgid.point); + } else if (msg.sender_user_id == 0) { + strlcpy(from, "(System)", sizeof(from)); } else { user = user_username(msg.sender_user_id); if (user) @@ -659,6 +663,12 @@ mail_read(struct session *s, unsigned long id, short i switch (c) { case 'r': + if (msg.sender_user_id == 0) { + session_printf(s, "System messages cannot be replied " + "to.\r\n"); + break; + } + reply_subject = xmalloc(strlen(msg.subject) + 5); if (reply_subject == NULL) break; @@ -854,7 +864,7 @@ mail_toss_ftn_message(struct fidopkt_message *ftnmsg) struct user *to = NULL; struct fidopkt_address our_address; size_t n, nmail_ids, size; - unsigned long *mail_ids = NULL; + unsigned long *mail_ids = NULL, sysop_id; struct mail_message msg; short ret = 1; char *data; @@ -886,12 +896,13 @@ mail_toss_ftn_message(struct fidopkt_message *ftnmsg) to = user_find_by_username(ftnmsg->to); if (to == NULL) { - to = user_find_by_username(user_first_sysop_username()); - if (to == NULL) { + sysop_id = user_first_sysop_id(); + if (!sysop_id || !(to = user_find(sysop_id))) { logger_printf("[mail] Can't find sysop username?"); ret = 0; goto done; } + logger_printf("[mail] No local user \"%s\", bouncing to sysop %s", ftnmsg->to, to->username); } @@ -959,6 +970,38 @@ done: if (to != NULL) xfree(&to); return ret; +} + +void +mail_to_sysop(char *subject, char *body) +{ + struct mail_message msg; + struct username_cache *ucache; + unsigned long sysop_id; + + sysop_id = user_first_sysop_id(); + if (!sysop_id) { + logger_printf("[mail] Tried mailing sysop, none found"); + return; + } + + memset(&msg, 0, sizeof(msg)); + msg.recipient_user_id = sysop_id; + msg.time = Time; + msg.subject = subject; + msg.subject_size = strlen(subject) + 1; + msg.body = body; + msg.body_size = strlen(body) + 1; + + if (mail_save(&msg) != 0) { + logger_printf("[mail] Failed saving new system mail to sysop"); + return; + } + + ucache = user_username(sysop_id); + + logger_printf("[mail] Sent system mail to %s", ucache ? + ucache->username : "(no username)"); } void --- mail.h Wed Mar 15 15:01:12 2023 +++ mail.h Thu Nov 23 11:35:10 2023 @@ -55,6 +55,7 @@ size_t mail_find_ids_for_user(struct user *user, size_ short mail_toss_ftn_message(struct fidopkt_message *fidomsg); size_t mail_encode_as_fidopkt(unsigned long id, struct fidopkt_address *orig_node, char **pkt_buf); +void mail_to_sysop(char *subject, char *body); void mail_prune(short days); #endif