| 1 |
/* |
| 2 |
* Copyright (c) 2005-2006 William Pitcock, et al. |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Sets usercloak metadata on register. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "atheme-compat.h" |
| 9 |
|
| 10 |
/* allow us-ascii letters, digits and the following characters */ |
| 11 |
#define VALID_SPECIALS "-" |
| 12 |
|
| 13 |
static int counter = 0; |
| 14 |
|
| 15 |
static void |
| 16 |
user_add_host(myuser_t *mu) |
| 17 |
{ |
| 18 |
int maxlen1, i; |
| 19 |
char newhost[COMPAT_HOSTLEN + 1]; |
| 20 |
const char *p; |
| 21 |
bool invalidchar = false; |
| 22 |
|
| 23 |
maxlen1 = COMPAT_HOSTLEN - 1 - strlen(me.hidehostsuffix); |
| 24 |
if (maxlen1 < 9) |
| 25 |
return; |
| 26 |
p = entity(mu)->name; |
| 27 |
i = 0; |
| 28 |
while (i < maxlen1 && *p != '\0') |
| 29 |
{ |
| 30 |
if (isalnum((unsigned char)*p) || strchr(VALID_SPECIALS, *p)) |
| 31 |
newhost[i++] = *p; |
| 32 |
else |
| 33 |
invalidchar = true; |
| 34 |
p++; |
| 35 |
} |
| 36 |
if (invalidchar || *p != '\0') |
| 37 |
{ |
| 38 |
if (i > maxlen1 - 6) |
| 39 |
i = maxlen1 - 6; |
| 40 |
snprintf(newhost + i, sizeof newhost - i, "-%05d", counter); |
| 41 |
counter++; |
| 42 |
if (counter >= 100000) |
| 43 |
counter = 0; |
| 44 |
if (nicksvs.me != NULL) |
| 45 |
{ |
| 46 |
myuser_notice(nicksvs.nick, mu, "Your account name cannot be used in a vhost directly. To ensure uniqueness, a number was added."); |
| 47 |
myuser_notice(nicksvs.nick, mu, "To avoid this, register an account name containing only letters, digits and %s.", VALID_SPECIALS); |
| 48 |
if (!nicksvs.no_nick_ownership && command_find(nicksvs.me->commands, "GROUP")) |
| 49 |
myuser_notice(nicksvs.nick, mu, "If you drop %s you can group it to your new account.", entity(mu)->name); |
| 50 |
} |
| 51 |
} |
| 52 |
else |
| 53 |
newhost[i] = '\0'; |
| 54 |
mowgli_strlcat(newhost, ".", sizeof newhost); |
| 55 |
mowgli_strlcat(newhost, me.hidehostsuffix, sizeof newhost); |
| 56 |
|
| 57 |
metadata_add(mu, "private:usercloak", newhost); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
handle_verify_register(hook_user_req_t *req) |
| 62 |
{ |
| 63 |
myuser_t *mu = req->mu; |
| 64 |
mowgli_node_t *n; |
| 65 |
user_t *u; |
| 66 |
|
| 67 |
if (me.hidehostsuffix == NULL) |
| 68 |
return; |
| 69 |
|
| 70 |
user_add_host(mu); |
| 71 |
|
| 72 |
MOWGLI_ITER_FOREACH(n, mu->logins.head) |
| 73 |
{ |
| 74 |
u = n->data; |
| 75 |
hook_call_user_identify(u); /* XXX */ |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
hook_user_identify(user_t *u) |
| 81 |
{ |
| 82 |
/* if they have an existing cloak, don't do anything */ |
| 83 |
if ((metadata_find(u->myuser, "private:usercloak")) || (me.hidehostsuffix == NULL)) |
| 84 |
return; |
| 85 |
|
| 86 |
/* they do not, add one. */ |
| 87 |
user_add_host(u->myuser); |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
mod_init(module_t *const restrict m) |
| 92 |
{ |
| 93 |
hook_add_event("user_verify_register"); |
| 94 |
hook_add_user_verify_register(handle_verify_register); |
| 95 |
hook_add_event("user_identify"); |
| 96 |
hook_add_user_identify(hook_user_identify); |
| 97 |
counter = (CURRTIME << 8) % 100000; |
| 98 |
if (counter < 0) |
| 99 |
counter += 100000; |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
mod_deinit(const module_unload_intent_t intent) |
| 104 |
{ |
| 105 |
hook_del_user_verify_register(handle_verify_register); |
| 106 |
hook_del_user_identify(hook_user_identify); |
| 107 |
} |
| 108 |
|
| 109 |
SIMPLE_DECLARE_MODULE_V1("contrib/gen_vhostonreg", MODULE_UNLOAD_CAPABILITY_OK) |