From 0337f0404b04012117633b4795508b06ab91ca87 Mon Sep 17 00:00:00 2001 From: Michael Poole Date: Wed, 7 Mar 2007 19:52:57 -0500 Subject: [PATCH] Split sendmail into common and sendmail-specific portions. This is in preparation for an SMTP alternative to the current code. --- configure.in | 14 ++- src/Makefile.am | 6 +- src/mail-common.c | 163 ++++++++++++++++++++++++++++ src/{sendmail.c => mail-sendmail.c} | 144 +----------------------- src/{sendmail.h => mail.h} | 12 +- src/{sendmail.help => mail.help} | 0 src/main-win32.c | 4 +- src/main.c | 4 +- src/nickserv.c | 12 +- 9 files changed, 202 insertions(+), 157 deletions(-) create mode 100644 src/mail-common.c rename src/{sendmail.c => mail-sendmail.c} (61%) rename src/{sendmail.h => mail.h} (73%) rename src/{sendmail.help => mail.help} (100%) diff --git a/configure.in b/configure.in index a821044..57deaae 100644 --- a/configure.in +++ b/configure.in @@ -239,7 +239,6 @@ if test "x$withval" = "xp10" ; then AC_MSG_RESULT(P10) AC_DEFINE(WITH_PROTOCOL_P10, 1, [Define if using the P10 dialect of IRC]) MODULE_OBJS="$MODULE_OBJS proto-p10.\$(OBJEXT)" - PROTO_FILES=proto-p10.c elif test "x$withval" = "xbahamut" ; then AC_MSG_RESULT(Bahamut) AC_DEFINE(WITH_PROTOCOL_BAHAMUT, 1, [Define if using the Bahamut dialect of IRC]) @@ -248,6 +247,19 @@ else AC_MSG_ERROR([Unknown IRC dialect $withval]) fi +AC_MSG_CHECKING(how to send mail) +AC_ARG_WITH(mail, +[ --with-mail=name How to send mail; one of: + sendmail (the default)], +[], +[withval="sendmail"]) +if test "x$withval" = "xsendmail" ; then + AC_MSG_RESULT(sendmail) + MODULE_OBJS="$MODULE_OBJS mail-sendmail.\$(OBJEXT)" +else + AC_MSG_ERROR([Unknown mail method $withval]) +fi + AC_MSG_CHECKING([I/O multiplexing backends]) IOMUXES="" diff --git a/src/Makefile.am b/src/Makefile.am index 4ce9b7f..34456de 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ noinst_DATA = \ nickserv.help \ opserv.help \ saxdb.help \ - sendmail.help \ + mail.help \ mod-helpserv.help \ mod-memoserv.help \ mod-sockcheck.help @@ -48,6 +48,8 @@ EXTRA_srvx_SOURCES = \ ioset-epoll.c \ ioset-select.c \ ioset-win32.c \ + mail-common.c \ + mail-sendmail.c \ main-common.c \ main-win32.c \ proto-bahamut.c \ @@ -75,6 +77,7 @@ srvx_SOURCES = \ helpfile.c helpfile.h \ ioset.c ioset.h ioset-impl.h \ log.c log.h \ + mail.h \ main.c common.h \ md5.c md5.h \ modcmd.c modcmd.h \ @@ -85,7 +88,6 @@ srvx_SOURCES = \ proto.h \ recdb.c recdb.h \ saxdb.c saxdb.h \ - sendmail.c sendmail.h \ timeq.c timeq.h \ tools.c diff --git a/src/mail-common.c b/src/mail-common.c new file mode 100644 index 0000000..84a9cf4 --- /dev/null +++ b/src/mail-common.c @@ -0,0 +1,163 @@ +/* mail-common.c - mail sending utilities + * Copyright 2002-2004, 2007 srvx Development Team + * + * This file is part of srvx. + * + * srvx is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with srvx; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include "conf.h" +#include "modcmd.h" +#include "nickserv.h" +#include "saxdb.h" + +#ifdef HAVE_SYS_WAIT_H +#include +#endif + +#define KEY_PROHIBITED "prohibited" + +static const struct message_entry msgtab[] = { + { "MAILMSG_EMAIL_ALREADY_BANNED", "%s is already banned (%s)." }, + { "MAILMSG_EMAIL_BANNED", "Email to %s has been forbidden." }, + { "MAILMSG_EMAIL_NOT_BANNED", "Email to %s was not forbidden." }, + { "MAILMSG_EMAIL_UNBANNED", "Email to %s is now allowed." }, + { "MAILMSG_PROHIBITED_EMAIL", "%s: %s" }, + { "MAILMSG_NO_PROHIBITED_EMAIL", "All email addresses are accepted." }, + { NULL, NULL } +}; + +static dict_t prohibited_addrs, prohibited_masks; +struct module *mail_module; + +const char * +mail_prohibited_address(const char *addr) +{ + dict_iterator_t it; + const char *data; + + if (prohibited_addrs && (data = dict_find(prohibited_addrs, addr, NULL))) + return data; + if (prohibited_masks) + for (it = dict_first(prohibited_masks); it; it = iter_next(it)) + if (match_ircglob(addr, iter_key(it))) + return iter_data(it); + return NULL; +} + +static int +mail_ban_address(struct userNode *user, struct userNode *bot, const char *addr, const char *reason) { + dict_t target; + const char *str; + + target = strpbrk(addr, "*?") ? prohibited_masks : prohibited_addrs; + if ((str = dict_find(target, addr, NULL))) { + if (user) + send_message(user, bot, "MAILMSG_EMAIL_ALREADY_BANNED", addr, str); + return 0; + } + dict_insert(target, strdup(addr), strdup(reason)); + if (user) send_message(user, bot, "MAILMSG_EMAIL_BANNED", addr); + return 1; +} + +static MODCMD_FUNC(cmd_banemail) { + char *reason = unsplit_string(argv+2, argc-2, NULL); + return mail_ban_address(user, cmd->parent->bot, argv[1], reason); +} + +static MODCMD_FUNC(cmd_unbanemail) { + dict_t target; + const char *addr; + + addr = argv[1]; + target = strpbrk(addr, "*?") ? prohibited_masks : prohibited_addrs; + if (dict_remove(target, addr)) + reply("MAILMSG_EMAIL_UNBANNED", addr); + else + reply("MAILMSG_EMAIL_NOT_BANNED", addr); + return 1; +} + +static MODCMD_FUNC(cmd_stats_email) { + dict_iterator_t it; + int found = 0; + + for (it=dict_first(prohibited_addrs); it; it=iter_next(it)) { + reply("MAILMSG_PROHIBITED_EMAIL", iter_key(it), (const char*)iter_data(it)); + found = 1; + } + for (it=dict_first(prohibited_masks); it; it=iter_next(it)) { + reply("MAILMSG_PROHIBITED_EMAIL", iter_key(it), (const char*)iter_data(it)); + found = 1; + } + if (!found) + reply("MAILMSG_NO_PROHIBITED_EMAIL"); + return 0; +} + +static int +mail_saxdb_read(struct dict *db) { + struct dict *subdb; + struct record_data *rd; + dict_iterator_t it; + + if ((subdb = database_get_data(db, KEY_PROHIBITED, RECDB_OBJECT))) { + for (it = dict_first(subdb); it; it = iter_next(it)) { + rd = iter_data(it); + if (rd->type == RECDB_QSTRING) + mail_ban_address(NULL, NULL, iter_key(it), rd->d.qstring); + } + } + return 0; +} + +static int +mail_saxdb_write(struct saxdb_context *ctx) { + dict_iterator_t it; + + saxdb_start_record(ctx, KEY_PROHIBITED, 0); + for (it = dict_first(prohibited_masks); it; it = iter_next(it)) + saxdb_write_string(ctx, iter_key(it), iter_data(it)); + for (it = dict_first(prohibited_addrs); it; it = iter_next(it)) + saxdb_write_string(ctx, iter_key(it), iter_data(it)); + saxdb_end_record(ctx); + return 0; +} + +static void +mail_common_cleanup(void) +{ + dict_delete(prohibited_addrs); + dict_delete(prohibited_masks); +} + +static void +mail_common_init(void) +{ + prohibited_addrs = dict_new(); + dict_set_free_keys(prohibited_addrs, free); + dict_set_free_data(prohibited_addrs, free); + prohibited_masks = dict_new(); + dict_set_free_keys(prohibited_masks, free); + dict_set_free_data(prohibited_masks, free); + reg_exit_func(mail_common_cleanup); + saxdb_register("sendmail", mail_saxdb_read, mail_saxdb_write); + mail_module = module_register("sendmail", MAIN_LOG, "mail.help", NULL); + modcmd_register(mail_module, "banemail", cmd_banemail, 3, 0, "level", "601", NULL); + modcmd_register(mail_module, "stats email", cmd_stats_email, 0, 0, "flags", "+oper", NULL); + modcmd_register(mail_module, "unbanemail", cmd_unbanemail, 2, 0, "level", "601", NULL); + message_register_table(msgtab); +} diff --git a/src/sendmail.c b/src/mail-sendmail.c similarity index 61% rename from src/sendmail.c rename to src/mail-sendmail.c index 63bb548..d6530b4 100644 --- a/src/sendmail.c +++ b/src/mail-sendmail.c @@ -1,5 +1,5 @@ -/* sendmail.c - mail sending utilities - * Copyright 2002-2004 srvx Development Team +/* mail-common.c - mail sending utilities + * Copyright 2002-2004, 2007 srvx Development Team * * This file is part of srvx. * @@ -18,44 +18,11 @@ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#include "conf.h" -#include "modcmd.h" -#include "nickserv.h" -#include "saxdb.h" - #ifdef HAVE_SYS_WAIT_H #include #endif -#define KEY_PROHIBITED "prohibited" - -static const struct message_entry msgtab[] = { - { "MAILMSG_EMAIL_ALREADY_BANNED", "%s is already banned (%s)." }, - { "MAILMSG_EMAIL_BANNED", "Email to %s has been forbidden." }, - { "MAILMSG_EMAIL_NOT_BANNED", "Email to %s was not forbidden." }, - { "MAILMSG_EMAIL_UNBANNED", "Email to %s is now allowed." }, - { "MAILMSG_PROHIBITED_EMAIL", "%s: %s" }, - { "MAILMSG_NO_PROHIBITED_EMAIL", "All email addresses are accepted." }, - { NULL, NULL } -}; - -static dict_t prohibited_addrs, prohibited_masks; -struct module *sendmail_module; - -const char * -sendmail_prohibited_address(const char *addr) -{ - dict_iterator_t it; - const char *data; - - if (prohibited_addrs && (data = dict_find(prohibited_addrs, addr, NULL))) - return data; - if (prohibited_masks) - for (it = dict_first(prohibited_masks); it; it = iter_next(it)) - if (match_ircglob(addr, iter_key(it))) - return iter_data(it); - return NULL; -} +#include "mail-common.c" /* This function sends the given "paragraph" as flowed text, as * defined in RFC 2646. It lets us only worry about line wrapping @@ -99,7 +66,7 @@ send_flowed_text(FILE *where, const char *para) } void -sendmail(struct userNode *from, struct handle_info *to, const char *subject, const char *body, int first_time) +mail_send(struct userNode *from, struct handle_info *to, const char *subject, const char *body, int first_time) { pid_t child; int infds[2], outfds[2]; @@ -234,107 +201,8 @@ sendmail(struct userNode *from, struct handle_info *to, const char *subject, con } } -static int -sendmail_ban_address(struct userNode *user, struct userNode *bot, const char *addr, const char *reason) { - dict_t target; - const char *str; - - target = strpbrk(addr, "*?") ? prohibited_masks : prohibited_addrs; - if ((str = dict_find(target, addr, NULL))) { - if (user) - send_message(user, bot, "MAILMSG_EMAIL_ALREADY_BANNED", addr, str); - return 0; - } - dict_insert(target, strdup(addr), strdup(reason)); - if (user) send_message(user, bot, "MAILMSG_EMAIL_BANNED", addr); - return 1; -} - -static MODCMD_FUNC(cmd_banemail) { - char *reason = unsplit_string(argv+2, argc-2, NULL); - return sendmail_ban_address(user, cmd->parent->bot, argv[1], reason); -} - -static MODCMD_FUNC(cmd_unbanemail) { - dict_t target; - const char *addr; - - addr = argv[1]; - target = strpbrk(addr, "*?") ? prohibited_masks : prohibited_addrs; - if (dict_remove(target, addr)) - reply("MAILMSG_EMAIL_UNBANNED", addr); - else - reply("MAILMSG_EMAIL_NOT_BANNED", addr); - return 1; -} - -static MODCMD_FUNC(cmd_stats_email) { - dict_iterator_t it; - int found = 0; - - for (it=dict_first(prohibited_addrs); it; it=iter_next(it)) { - reply("MAILMSG_PROHIBITED_EMAIL", iter_key(it), (const char*)iter_data(it)); - found = 1; - } - for (it=dict_first(prohibited_masks); it; it=iter_next(it)) { - reply("MAILMSG_PROHIBITED_EMAIL", iter_key(it), (const char*)iter_data(it)); - found = 1; - } - if (!found) - reply("MAILMSG_NO_PROHIBITED_EMAIL"); - return 0; -} - -static int -sendmail_saxdb_read(struct dict *db) { - struct dict *subdb; - struct record_data *rd; - dict_iterator_t it; - - if ((subdb = database_get_data(db, KEY_PROHIBITED, RECDB_OBJECT))) { - for (it = dict_first(subdb); it; it = iter_next(it)) { - rd = iter_data(it); - if (rd->type == RECDB_QSTRING) - sendmail_ban_address(NULL, NULL, iter_key(it), rd->d.qstring); - } - } - return 0; -} - -static int -sendmail_saxdb_write(struct saxdb_context *ctx) { - dict_iterator_t it; - - saxdb_start_record(ctx, KEY_PROHIBITED, 0); - for (it = dict_first(prohibited_masks); it; it = iter_next(it)) - saxdb_write_string(ctx, iter_key(it), iter_data(it)); - for (it = dict_first(prohibited_addrs); it; it = iter_next(it)) - saxdb_write_string(ctx, iter_key(it), iter_data(it)); - saxdb_end_record(ctx); - return 0; -} - -static void -sendmail_cleanup(void) -{ - dict_delete(prohibited_addrs); - dict_delete(prohibited_masks); -} - void -sendmail_init(void) +mail_init(void) { - prohibited_addrs = dict_new(); - dict_set_free_keys(prohibited_addrs, free); - dict_set_free_data(prohibited_addrs, free); - prohibited_masks = dict_new(); - dict_set_free_keys(prohibited_masks, free); - dict_set_free_data(prohibited_masks, free); - reg_exit_func(sendmail_cleanup); - saxdb_register("sendmail", sendmail_saxdb_read, sendmail_saxdb_write); - sendmail_module = module_register("sendmail", MAIN_LOG, "sendmail.help", NULL); - modcmd_register(sendmail_module, "banemail", cmd_banemail, 3, 0, "level", "601", NULL); - modcmd_register(sendmail_module, "stats email", cmd_stats_email, 0, 0, "flags", "+oper", NULL); - modcmd_register(sendmail_module, "unbanemail", cmd_unbanemail, 2, 0, "level", "601", NULL); - message_register_table(msgtab); + mail_common_init(); } diff --git a/src/sendmail.h b/src/mail.h similarity index 73% rename from src/sendmail.h rename to src/mail.h index 2a11fc1..208020c 100644 --- a/src/sendmail.h +++ b/src/mail.h @@ -1,4 +1,4 @@ -/* sendmail.h - mail sending utilities +/* mail.h - mail sending utilities * Copyright 2002 srvx Development Team * * This file is part of srvx. @@ -18,11 +18,11 @@ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#if !defined(SENDMAIL_H) -#define SENDMAIL_H +#if !defined(MAIL_H) +#define MAIL_H -void sendmail_init(void); -void sendmail(struct userNode *from, struct handle_info *to, const char *subject, const char *body, int first_time); -const char *sendmail_prohibited_address(const char *addr); +void mail_init(void); +void mail_send(struct userNode *from, struct handle_info *to, const char *subject, const char *body, int first_time); +const char *mail_prohibited_address(const char *addr); #endif diff --git a/src/sendmail.help b/src/mail.help similarity index 100% rename from src/sendmail.help rename to src/mail.help diff --git a/src/main-win32.c b/src/main-win32.c index ddf8380..03b0036 100644 --- a/src/main-win32.c +++ b/src/main-win32.c @@ -4,7 +4,7 @@ #include "ioset.h" #include "modcmd.h" #include "saxdb.h" -#include "sendmail.h" +#include "mail.h" #include "timeq.h" #include "chanserv.h" @@ -41,7 +41,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int modcmd_init(); saxdb_init(); gline_init(); - sendmail_init(); + mail_init(); helpfile_init(); conf_globals(); conf_rlimits(); diff --git a/src/main.c b/src/main.c index 577f25b..bffe2be 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ #include "ioset.h" #include "modcmd.h" #include "saxdb.h" -#include "sendmail.h" +#include "mail.h" #include "timeq.h" #include "chanserv.h" @@ -283,7 +283,7 @@ int main(int argc, char *argv[]) modcmd_init(); saxdb_init(); gline_init(); - sendmail_init(); + mail_init(); helpfile_init(); conf_globals(); /* initializes the core services */ conf_rlimits(); diff --git a/src/nickserv.c b/src/nickserv.c index 39428a0..bf22dd6 100644 --- a/src/nickserv.c +++ b/src/nickserv.c @@ -24,7 +24,7 @@ #include "modcmd.h" #include "opserv.h" /* for gag_create(), opserv_bad_channel() */ #include "saxdb.h" -#include "sendmail.h" +#include "mail.h" #include "timeq.h" #ifdef HAVE_REGEX_H @@ -1044,7 +1044,7 @@ nickserv_make_cookie(struct userNode *user, struct handle_info *hi, enum cookie_ snprintf(subject, sizeof(subject), fmt, netname); fmt = handle_find_message(hi, "NSEMAIL_EMAIL_CHANGE_BODY_NEW"); snprintf(body, sizeof(body), fmt, netname, cookie->cookie+COOKIELEN/2, nickserv->nick, self->name, hi->handle, COOKIELEN/2); - sendmail(nickserv, hi, subject, body, 1); + mail_send(nickserv, hi, subject, body, 1); fmt = handle_find_message(hi, "NSEMAIL_EMAIL_CHANGE_BODY_OLD"); snprintf(body, sizeof(body), fmt, netname, cookie->cookie, nickserv->nick, self->name, hi->handle, COOKIELEN/2, hi->email_addr); } else { @@ -1053,7 +1053,7 @@ nickserv_make_cookie(struct userNode *user, struct handle_info *hi, enum cookie_ snprintf(subject, sizeof(subject), fmt, netname); fmt = handle_find_message(hi, "NSEMAIL_EMAIL_VERIFY_BODY"); snprintf(body, sizeof(body), fmt, netname, cookie->cookie, nickserv->nick, self->name, hi->handle); - sendmail(nickserv, hi, subject, body, 1); + mail_send(nickserv, hi, subject, body, 1); subject[0] = 0; } hi->email_addr = misc; @@ -1070,7 +1070,7 @@ nickserv_make_cookie(struct userNode *user, struct handle_info *hi, enum cookie_ break; } if (subject[0]) - sendmail(nickserv, hi, subject, body, first_time); + mail_send(nickserv, hi, subject, body, first_time); nickserv_bake_cookie(cookie); } @@ -1164,7 +1164,7 @@ static NICKSERV_FUNC(cmd_register) } /* .. and that we are allowed to send to it. */ - if ((str = sendmail_prohibited_address(email_addr))) { + if ((str = mail_prohibited_address(email_addr))) { reply("NSMSG_EMAIL_PROHIBITED", email_addr, str); return 0; } @@ -2328,7 +2328,7 @@ static OPTION_FUNC(opt_email) send_message(user, nickserv, "NSMSG_BAD_EMAIL_ADDR"); return 0; } - if ((str = sendmail_prohibited_address(argv[1]))) { + if ((str = mail_prohibited_address(argv[1]))) { send_message(user, nickserv, "NSMSG_EMAIL_PROHIBITED", argv[1], str); return 0; } -- 2.20.1