diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2007-09-06 13:05:56 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2007-09-06 13:05:56 +0000 |
commit | 747e41693d7f2a808b3314cf82b3af38f98daf2d (patch) | |
tree | a023fea0ac96b2abdc2c98141d90a841544dbbf9 /src/mail.c | |
parent | afd4995c30c273a02f81e80a972688936907853a (diff) | |
download | wydawca-747e41693d7f2a808b3314cf82b3af38f98daf2d.tar.gz wydawca-747e41693d7f2a808b3314cf82b3af38f98daf2d.tar.bz2 |
Redo mail notifications
git-svn-id: file:///svnroot/wydawca/trunk@316 6bb4bd81-ecc2-4fd4-a2d4-9571d19c0d33
Diffstat (limited to 'src/mail.c')
-rw-r--r-- | src/mail.c | 172 |
1 files changed, 122 insertions, 50 deletions
@@ -22,7 +22,7 @@ mu_mailer_t mailer; mu_address_t admin_address; mu_address_t from_address; unsigned long mail_admin_mask; -char *admin_stat_message_template; +struct message_template *admin_stat_message_template; unsigned long owner_notification_flags; char *user_message_template[MAX_EVENT]; @@ -98,6 +98,70 @@ mail_send_message (mu_address_t rcpt, const char *text) } void +mail_finish () +{ + if (mailer_opened) + mu_mailer_close (mailer); +} + + +struct message_template +{ + struct message_template *next; + char *name; + char *text; + /* int mime; for future use */ +}; + +static struct message_template *message_list; + +void +register_message_template (char *name, char *text) +{ + struct message_template *newp = xmalloc (sizeof *newp); + newp->name = name; + newp->text = text; + newp->next = message_list; + message_list = newp; +} + +struct message_template * +find_message_template (char *name) +{ + struct message_template *p; + + for (p = message_list; p; p = p->next) + if (strcmp (p->name, name) == 0) + break; + return p; +} + + +struct notification +{ + struct notification *next; + enum notification_event ev; + enum notification_target tgt; + struct message_template *msg; +}; + +static struct notification *notification_list; + +void +register_notification (enum notification_event ev, + enum notification_target tgt, + struct message_template *msg) +{ + struct notification *newp = xmalloc (sizeof *newp); + newp->ev = ev; + newp->tgt = tgt; + newp->msg = msg; + newp->next = notification_list; + notification_list = newp; +} + + +void mail_stats () { struct kw_expansion exp[MAX_STAT + 1]; @@ -129,7 +193,7 @@ mail_stats () make_stat_expansion (exp + 1); - text = expand_param (admin_stat_message_template, + text = expand_param (admin_stat_message_template->text, exp, sizeof (exp) / sizeof (exp[0]), NULL); mail_send_message (admin_address, text); @@ -138,53 +202,26 @@ mail_stats () free_kwexp (exp, sizeof (exp) / sizeof (exp[0])); } -void -mail_finish () +mu_address_t +get_recipient (struct access_method *method, struct file_triplet *trp, + char **errp) { - if (mailer_opened) - mu_mailer_close (mailer); -} - -void -notify_owner (struct file_triplet *trp, enum notification_event ev) -{ - int rc; - struct access_method *method; - char *text; - mu_address_t rcpt = NULL; unsigned nrows, ncols, i; struct kw_expansion kwexp[4]; - - switch (ev) - { - case ev_success: - case ev_bad_directive_signature: - case ev_bad_detached_signature: - method = trp->dpair->user_data_method; - break; - - case ev_bad_ownership: - method = trp->dpair->project_owner_method; - break; - - default: - abort (); - } + mu_address_t rcpt = NULL; + char *text; + int rc; if (method->type == method_none) { - logmsg (LOG_NOTICE, - "NOT notifying project admins: project-owner not configured in " - "[%s, %s]", trp->dpair->source_dir, trp->dpair->dest_dir); - return; + *errp = "access method is not configured"; + return NULL; } if (method_init (method)) { - logmsg (LOG_ERR, - "failed to initialize project-owner method: " - "NOT notifying project admins"); - return; + *errp = "failed to initialize access method"; + return NULL; } make_default_kwexp (kwexp, trp->user, trp->project); @@ -195,9 +232,8 @@ notify_owner (struct file_triplet *trp, enum notification_event ev) free (text); if (rc) { - logmsg (LOG_ERR, "cannot obtain owner emails for %s", - trp->project); - return; + *errp = "cannot obtain recipient emails"; + return NULL; } nrows = method_num_rows (method); @@ -205,9 +241,8 @@ notify_owner (struct file_triplet *trp, enum notification_event ev) if (nrows == 0) { - logmsg (LOG_ERR, "cannot obtain owner emails for %s", - trp->project); - return; + *errp = "cannot obtain recipient emails"; + return NULL; } for (i = 0; i < nrows; i++) @@ -226,6 +261,40 @@ notify_owner (struct file_triplet *trp, enum notification_event ev) mu_address_destroy (&addr); } + return rcpt; +} + +void +do_notify (struct file_triplet *trp, enum notification_event ev, + struct notification *ntf) +{ + int rc; + mu_address_t rcpt = NULL; + char *errp; + + switch (ntf->tgt) + { + case notify_admin: + rcpt = admin_address; + break; + + case notify_user: + rcpt = get_recipient (trp->dpair->user_data_method, trp, &errp); + break; + + case notify_owner: + rcpt = get_recipient (trp->dpair->project_owner_method, trp, &errp); + } + + if (!rcpt) + { + logmsg (LOG_ERR, "not notifying %s (project %s) about %s: %s", + notification_target_str (ntf->tgt), + trp->project, + notification_event_str (ev), errp); + return; + } + if (debug_level) { size_t size; @@ -233,14 +302,14 @@ notify_owner (struct file_triplet *trp, enum notification_event ev) mu_address_to_string (rcpt, NULL, 0, &size); buf = xmalloc (size + 1); mu_address_to_string (rcpt, buf, size + 1, NULL); - logmsg (LOG_DEBUG, "Notifying admins of %s: %s", trp->project, - buf); + logmsg (LOG_DEBUG, "notifying %s (project %s) about %s", + buf, trp->project, notification_event_str (ev)); free (buf); } if (!dry_run_mode) { - text = triplet_expand_param (user_message_template[ev], trp); + char *text = triplet_expand_param (ntf->msg->text, trp); mail_send_message (rcpt, text); free (text); } @@ -251,8 +320,11 @@ notify_owner (struct file_triplet *trp, enum notification_event ev) void notify (struct file_triplet *trp, enum notification_event ev) { + struct notification *p; + fill_project_name (trp); - if (owner_notification_flags & STAT_MASK (ev)) - notify_owner (trp, ev); + for (p = notification_list; p; p = p->next) + if (p->ev == ev) + do_notify (trp, ev, p); /* FIXME */ } |