| 1 |
/* |
| 2 |
* Copyright (c) 2013 Brandon Turner |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Restrict registration to certain email addresses. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "atheme-compat.h" |
| 9 |
|
| 10 |
struct goodmail_ { |
| 11 |
char *mail; |
| 12 |
time_t mail_ts; |
| 13 |
char *creator; |
| 14 |
char *reason; |
| 15 |
}; |
| 16 |
|
| 17 |
typedef struct goodmail_ goodmail_t; |
| 18 |
|
| 19 |
static mowgli_list_t ns_maillist = { NULL, NULL, 0 }; |
| 20 |
|
| 21 |
static void |
| 22 |
write_gedb(database_handle_t *db) |
| 23 |
{ |
| 24 |
mowgli_node_t *n; |
| 25 |
|
| 26 |
MOWGLI_ITER_FOREACH(n, ns_maillist.head) |
| 27 |
{ |
| 28 |
goodmail_t *l = n->data; |
| 29 |
|
| 30 |
db_start_row(db, "GE"); |
| 31 |
db_write_word(db, l->mail); |
| 32 |
db_write_time(db, l->mail_ts); |
| 33 |
db_write_word(db, l->creator); |
| 34 |
db_write_str(db, l->reason); |
| 35 |
db_commit_row(db); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static void |
| 40 |
db_h_ge(database_handle_t *db, const char *type) |
| 41 |
{ |
| 42 |
const char *mail = db_sread_word(db); |
| 43 |
time_t mail_ts = db_sread_time(db); |
| 44 |
const char *creator = db_sread_word(db); |
| 45 |
const char *reason = db_sread_str(db); |
| 46 |
|
| 47 |
goodmail_t *l = smalloc(sizeof(goodmail_t)); |
| 48 |
l->mail = sstrdup(mail); |
| 49 |
l->mail_ts = mail_ts; |
| 50 |
l->creator = sstrdup(creator); |
| 51 |
l->reason = sstrdup(reason); |
| 52 |
mowgli_node_add(l, mowgli_node_create(), &ns_maillist); |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
check_registration(hook_user_register_check_t *hdata) |
| 57 |
{ |
| 58 |
mowgli_node_t *n; |
| 59 |
goodmail_t *l; |
| 60 |
|
| 61 |
if (hdata->approved) |
| 62 |
return; |
| 63 |
|
| 64 |
MOWGLI_ITER_FOREACH(n, ns_maillist.head) |
| 65 |
{ |
| 66 |
l = n->data; |
| 67 |
|
| 68 |
if (!match(l->mail, hdata->email)) |
| 69 |
{ |
| 70 |
return; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
command_fail(hdata->si, fault_noprivs, "Sorry, we do not accept registrations with email addresses from that domain. Use another address."); |
| 75 |
hdata->approved = 1; |
| 76 |
slog(LG_INFO, "REGISTER:GOODMAIL: %s to \2%s\2 by \2%s\2", |
| 77 |
hdata->account, hdata->email, |
| 78 |
hdata->si->su != NULL ? hdata->si->su->nick : get_source_name(hdata->si)); |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
ns_cmd_goodmail(sourceinfo_t *si, int parc, char *parv[]) |
| 83 |
{ |
| 84 |
char *action = parv[0]; |
| 85 |
char *email = parv[1]; |
| 86 |
char *reason = parv[2]; |
| 87 |
mowgli_node_t *n, *tn; |
| 88 |
goodmail_t *l; |
| 89 |
|
| 90 |
if (!action) |
| 91 |
{ |
| 92 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "goodmail"); |
| 93 |
command_fail(si, fault_needmoreparams, _("Syntax: goodmail ADD|DEL|LIST [parameters]")); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
if (!strcasecmp("ADD", action)) |
| 98 |
{ |
| 99 |
if (!email || !reason) |
| 100 |
{ |
| 101 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "goodmail"); |
| 102 |
command_fail(si, fault_needmoreparams, _("Syntax: goodmail ADD <email> <reason>")); |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
if (si->smu == NULL) |
| 107 |
{ |
| 108 |
command_fail(si, fault_noprivs, STR_NOT_LOGGED_IN); |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
/* search for it */ |
| 113 |
MOWGLI_ITER_FOREACH(n, ns_maillist.head) |
| 114 |
{ |
| 115 |
l = n->data; |
| 116 |
|
| 117 |
if (!irccasecmp(l->mail, email)) |
| 118 |
{ |
| 119 |
command_success_nodata(si, _("Email \2%s\2 has already been whitelisted."), email); |
| 120 |
return; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
l = smalloc(sizeof(goodmail_t)); |
| 125 |
l->mail = sstrdup(email); |
| 126 |
l->mail_ts = CURRTIME;; |
| 127 |
l->creator = sstrdup(get_source_name(si)); |
| 128 |
l->reason = sstrdup(reason); |
| 129 |
|
| 130 |
logcommand(si, CMDLOG_ADMIN, "goodmail:ADD: \2%s\2 (Reason: \2%s\2)", email, reason); |
| 131 |
|
| 132 |
n = mowgli_node_create(); |
| 133 |
mowgli_node_add(l, n, &ns_maillist); |
| 134 |
|
| 135 |
command_success_nodata(si, _("You have whitelisted email address \2%s\2."), email); |
| 136 |
return; |
| 137 |
} |
| 138 |
else if (!strcasecmp("DEL", action)) |
| 139 |
{ |
| 140 |
if (!email) |
| 141 |
{ |
| 142 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "goodmail"); |
| 143 |
command_fail(si, fault_needmoreparams, _("Syntax: goodmail DEL <email>")); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, ns_maillist.head) |
| 148 |
{ |
| 149 |
l = n->data; |
| 150 |
|
| 151 |
if (!irccasecmp(l->mail, email)) |
| 152 |
{ |
| 153 |
logcommand(si, CMDLOG_ADMIN, "goodmail:DEL: \2%s\2", l->mail); |
| 154 |
|
| 155 |
mowgli_node_delete(n, &ns_maillist); |
| 156 |
|
| 157 |
sfree(l->mail); |
| 158 |
sfree(l->creator); |
| 159 |
sfree(l->reason); |
| 160 |
sfree(l); |
| 161 |
|
| 162 |
return; |
| 163 |
} |
| 164 |
} |
| 165 |
command_success_nodata(si, _("Email pattern \2%s\2 not found in goodmail database."), email); |
| 166 |
return; |
| 167 |
} |
| 168 |
else if (!strcasecmp("LIST", action)) |
| 169 |
{ |
| 170 |
char buf[BUFSIZE]; |
| 171 |
struct tm tm; |
| 172 |
|
| 173 |
MOWGLI_ITER_FOREACH(n, ns_maillist.head) |
| 174 |
{ |
| 175 |
l = n->data; |
| 176 |
|
| 177 |
tm = *localtime(&l->mail_ts); |
| 178 |
strftime(buf, BUFSIZE, TIME_FORMAT, &tm); |
| 179 |
command_success_nodata(si, "Email: \2%s\2, Reason: \2%s\2 (%s - %s)", |
| 180 |
l->mail, l->reason, l->creator, buf); |
| 181 |
} |
| 182 |
command_success_nodata(si, "End of list."); |
| 183 |
logcommand(si, CMDLOG_GET, "goodmail:LIST"); |
| 184 |
return; |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "goodmail"); |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
static command_t ns_goodmail = { |
| 194 |
.name = "GOODMAIL", |
| 195 |
.desc = N_("Restrict registration to certain email addresses."), |
| 196 |
.access = PRIV_USER_ADMIN, |
| 197 |
.maxparc = 3, |
| 198 |
.cmd = &ns_cmd_goodmail, |
| 199 |
.help = { .path = "contrib/ns_goodmail" }, |
| 200 |
}; |
| 201 |
|
| 202 |
static void |
| 203 |
mod_init(module_t *const restrict m) |
| 204 |
{ |
| 205 |
if (!module_find_published("backend/opensex")) |
| 206 |
{ |
| 207 |
slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name); |
| 208 |
m->mflags |= MODFLAG_FAIL; |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
hook_add_event("user_can_register"); |
| 213 |
hook_add_user_can_register(check_registration); |
| 214 |
hook_add_db_write(write_gedb); |
| 215 |
|
| 216 |
db_register_type_handler("GE", db_h_ge); |
| 217 |
|
| 218 |
service_named_bind_command("nickserv", &ns_goodmail); |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
mod_deinit(const module_unload_intent_t intent) |
| 223 |
{ |
| 224 |
hook_del_user_can_register(check_registration); |
| 225 |
hook_del_db_write(write_gedb); |
| 226 |
|
| 227 |
db_unregister_type_handler("GE"); |
| 228 |
|
| 229 |
service_named_unbind_command("nickserv", &ns_goodmail); |
| 230 |
} |
| 231 |
|
| 232 |
SIMPLE_DECLARE_MODULE_V1("contrib/ns_goodmail", MODULE_UNLOAD_CAPABILITY_OK) |