| 1 |
/* |
| 2 |
* Copyright (c) 2005 William Pitcock |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Restrictions for nicknames. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
#include "list_common.h" |
| 11 |
#include "list.h" |
| 12 |
|
| 13 |
DECLARE_MODULE_V1 |
| 14 |
( |
| 15 |
"nickserv/restrict", false, _modinit, _moddeinit, |
| 16 |
PACKAGE_STRING, |
| 17 |
VENDOR_STRING |
| 18 |
); |
| 19 |
|
| 20 |
static void ns_cmd_restrict(sourceinfo_t *si, int parc, char *parv[]); |
| 21 |
|
| 22 |
command_t ns_restrict = { "RESTRICT", N_("Restrict a user from using certain commands."), PRIV_MARK, 3, ns_cmd_restrict, { .path = "nickserv/restrict" } }; |
| 23 |
|
| 24 |
static bool is_restricted(const mynick_t *mn, const void *arg) { |
| 25 |
myuser_t *mu = mn->owner; |
| 26 |
|
| 27 |
return !!metadata_find(mu, "private:restrict:setter"); |
| 28 |
} |
| 29 |
|
| 30 |
static bool restricted_match(const mynick_t *mn, const void *arg) { |
| 31 |
const char *restrictedpattern = (const char*)arg; |
| 32 |
metadata_t *mdrestricted; |
| 33 |
|
| 34 |
myuser_t *mu = mn->owner; |
| 35 |
|
| 36 |
mdrestricted = metadata_find(mu, "private:restrict:reason"); |
| 37 |
|
| 38 |
if (mdrestricted != NULL && !match(restrictedpattern, mdrestricted->value)) |
| 39 |
return true; |
| 40 |
|
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
static void info_hook(hook_user_req_t *hdata) |
| 45 |
{ |
| 46 |
metadata_t *md; |
| 47 |
|
| 48 |
if (has_priv(hdata->si, PRIV_USER_AUSPEX) && (md = metadata_find(hdata->mu, "private:restrict:setter"))) |
| 49 |
{ |
| 50 |
const char *setter = md->value; |
| 51 |
const char *reason; |
| 52 |
time_t ts; |
| 53 |
struct tm tm; |
| 54 |
char strfbuf[BUFSIZE]; |
| 55 |
|
| 56 |
md = metadata_find(hdata->mu, "private:restrict:reason"); |
| 57 |
reason = md != NULL ? md->value : "unknown"; |
| 58 |
|
| 59 |
md = metadata_find(hdata->mu, "private:restrict:timestamp"); |
| 60 |
ts = md != NULL ? atoi(md->value) : 0; |
| 61 |
|
| 62 |
tm = *localtime(&ts); |
| 63 |
strftime(strfbuf, sizeof strfbuf, TIME_FORMAT, &tm); |
| 64 |
|
| 65 |
command_success_nodata(hdata->si, _("%s was \2RESTRICTED\2 by %s on %s (%s)"), entity(hdata->mu)->name, setter, strfbuf, reason); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
void _modinit(module_t *m) |
| 70 |
{ |
| 71 |
service_named_bind_command("nickserv", &ns_restrict); |
| 72 |
|
| 73 |
hook_add_event("user_info"); |
| 74 |
hook_add_user_info(info_hook); |
| 75 |
|
| 76 |
use_nslist_main_symbols(m); |
| 77 |
|
| 78 |
static list_param_t restricted; |
| 79 |
restricted.opttype = OPT_BOOL; |
| 80 |
restricted.is_match = is_restricted; |
| 81 |
|
| 82 |
static list_param_t restrict_match; |
| 83 |
restrict_match.opttype = OPT_STRING; |
| 84 |
restrict_match.is_match = restricted_match; |
| 85 |
|
| 86 |
list_register("restricted", &restricted); |
| 87 |
list_register("restricted-reason", &restrict_match); |
| 88 |
} |
| 89 |
|
| 90 |
void _moddeinit(module_unload_intent_t intent) |
| 91 |
{ |
| 92 |
service_named_unbind_command("nickserv", &ns_restrict); |
| 93 |
|
| 94 |
hook_del_user_info(info_hook); |
| 95 |
|
| 96 |
list_unregister("restricted"); |
| 97 |
list_unregister("restricted-reason"); |
| 98 |
} |
| 99 |
|
| 100 |
static void ns_cmd_restrict(sourceinfo_t *si, int parc, char *parv[]) |
| 101 |
{ |
| 102 |
char *target = parv[0]; |
| 103 |
char *action = parv[1]; |
| 104 |
char *info = parv[2]; |
| 105 |
myuser_t *mu; |
| 106 |
|
| 107 |
if (!target || !action) |
| 108 |
{ |
| 109 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "RESTRICT"); |
| 110 |
command_fail(si, fault_needmoreparams, _("Usage: RESTRICT <target> <ON|OFF> [note]")); |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
if (!(mu = myuser_find_ext(target))) |
| 115 |
{ |
| 116 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), target); |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
if (!strcasecmp(action, "ON")) |
| 121 |
{ |
| 122 |
if (!info) |
| 123 |
{ |
| 124 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "RESTRICT"); |
| 125 |
command_fail(si, fault_needmoreparams, _("Usage: RESTRICT <target> ON <note>")); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if (metadata_find(mu, "private:restrict:setter")) |
| 130 |
{ |
| 131 |
command_fail(si, fault_badparams, _("\2%s\2 is already restricted."), entity(mu)->name); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
metadata_add(mu, "private:restrict:setter", get_oper_name(si)); |
| 136 |
metadata_add(mu, "private:restrict:reason", info); |
| 137 |
metadata_add(mu, "private:restrict:timestamp", number_to_string(time(NULL))); |
| 138 |
|
| 139 |
wallops("%s restricted the account \2%s\2.", get_oper_name(si), entity(mu)->name); |
| 140 |
logcommand(si, CMDLOG_ADMIN, "RESTRICT:ON: \2%s\2 (reason: \2%s\2)", entity(mu)->name, info); |
| 141 |
command_success_nodata(si, _("\2%s\2 is now restricted."), entity(mu)->name); |
| 142 |
} |
| 143 |
else if (!strcasecmp(action, "OFF")) |
| 144 |
{ |
| 145 |
if (!metadata_find(mu, "private:restrict:setter")) |
| 146 |
{ |
| 147 |
command_fail(si, fault_badparams, _("\2%s\2 is not restricted."), entity(mu)->name); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
metadata_delete(mu, "private:restrict:setter"); |
| 152 |
metadata_delete(mu, "private:restrict:reason"); |
| 153 |
metadata_delete(mu, "private:restrict:timestamp"); |
| 154 |
|
| 155 |
wallops("%s unrestricted the account \2%s\2.", get_oper_name(si), entity(mu)->name); |
| 156 |
logcommand(si, CMDLOG_ADMIN, "RESTRICT:OFF: \2%s\2", entity(mu)->name); |
| 157 |
command_success_nodata(si, _("\2%s\2 is now unrestricted."), entity(mu)->name); |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "RESTRICT"); |
| 162 |
command_fail(si, fault_needmoreparams, _("Usage: RESTRICT <target> <ON|OFF> [note]")); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 167 |
* vim:ts=8 |
| 168 |
* vim:sw=8 |
| 169 |
* vim:noexpandtab |
| 170 |
*/ |