Debian Codegraph @

modules/nickserv/list.c

raw
1 /*
2 * Copyright (c) 2005-2007 Robin Burchell, et al.
3 * Copyright (c) 2010 William Pitcock <[email protected]>.
4 * Rights to this code are as documented in doc/LICENSE.
5 *
6 * This file contains code for the NickServ LIST function.
7 * Based on Alex Lambert's LISTEMAIL.
8 */
9
10 #include "atheme.h"
11 #include "list_common.h"
12
13 DECLARE_MODULE_V1
14 (
15 "nickserv/list", false, _modinit, _moddeinit,
16 PACKAGE_STRING,
17 VENDOR_STRING
18 );
19
20 static void ns_cmd_list(sourceinfo_t *si, int parc, char *parv[]);
21 static mowgli_patricia_t *list_params;
22
23 command_t ns_list = { "LIST", N_("Lists nicknames registered matching a given pattern."), PRIV_USER_AUSPEX, 10, ns_cmd_list, { .path = "nickserv/list" } };
24
25 void list_register(const char *param_name, list_param_t *param);
26 void list_unregister(const char *param_name);
27
28 static bool email_match(const mynick_t *mn, const void *arg)
29 {
30 myuser_t *mu = mn->owner;
31 const char *cmpr = (const char*)arg;
32
33 return !match(cmpr, mu->email);
34 }
35
36 static bool lastlogin_match(const mynick_t *mn, const void *arg)
37 {
38 myuser_t *mu = mn->owner;
39 const time_t lastlogin = *(time_t *)arg;
40
41 return (CURRTIME - mu->lastlogin) > lastlogin;
42 }
43
44 static bool pattern_match(const mynick_t *mn, const void *arg)
45 {
46 const char *pattern = (const char*)arg;
47
48 char pat[512], *nickpattern = NULL, *hostpattern = NULL, *p;
49 metadata_t *md;
50
51 bool hostmatch;
52
53 myuser_t *mu = mn->owner;
54
55 if (pattern != NULL)
56 {
57 mowgli_strlcpy(pat, pattern, sizeof pat);
58 p = strrchr(pat, ' ');
59 if (p == NULL)
60 p = strrchr(pat, '!');
61 if (p != NULL)
62 {
63 *p++ = '\0';
64 nickpattern = pat;
65 hostpattern = p;
66 }
67 else if (strchr(pat, '@'))
68 hostpattern = pat;
69 else
70 nickpattern = pat;
71 if (nickpattern && !strcmp(nickpattern, "*"))
72 nickpattern = NULL;
73 }
74
75 if (nickpattern && match(nickpattern, mn->nick))
76 return false;
77
78 if (hostpattern)
79 {
80 hostmatch = false;
81 md = metadata_find(mu, "private:host:actual");
82 if (md != NULL && !match(hostpattern, md->value))
83 hostmatch = true;
84 md = metadata_find(mu, "private:host:vhost");
85 if (md != NULL && !match(hostpattern, md->value))
86 hostmatch = true;
87 if (!hostmatch)
88 return false;
89 }
90
91 return true;
92 }
93
94 static bool registered_match(const mynick_t *mn, const void *arg)
95 {
96 myuser_t *mu = mn->owner;
97 const time_t age = *(time_t *)arg;
98
99 return (CURRTIME - mu->registered) > age;
100 }
101
102 static bool has_waitauth(const mynick_t *mn, const void *arg) {
103 myuser_t *mu = mn->owner;
104
105 return ( mu->flags & MU_WAITAUTH ) == MU_WAITAUTH;
106 }
107
108 void _modinit(module_t *m)
109 {
110 list_params = mowgli_patricia_create(strcasecanon);
111 service_named_bind_command("nickserv", &ns_list);
112
113 /* list email */
114 static list_param_t email;
115 email.opttype = OPT_STRING;
116 email.is_match = email_match;
117
118 static list_param_t lastlogin;
119 lastlogin.opttype = OPT_AGE;
120 lastlogin.is_match = lastlogin_match;
121
122 static list_param_t pattern;
123 pattern.opttype = OPT_STRING;
124 pattern.is_match = pattern_match;
125
126 static list_param_t registered;
127 registered.opttype = OPT_AGE;
128 registered.is_match = registered_match;
129
130 list_register("email", &email);
131 list_register("lastlogin", &lastlogin);
132 list_register("mail", &email);
133
134 list_register("pattern", &pattern);
135 list_register("registered", &registered);
136
137 static list_param_t waitauth;
138 waitauth.opttype = OPT_BOOL;
139 waitauth.is_match = has_waitauth;
140
141 list_register("waitauth", &waitauth);
142 }
143
144 void _moddeinit(module_unload_intent_t intent)
145 {
146 service_named_unbind_command("nickserv", &ns_list);
147
148 list_unregister("email");
149 list_unregister("lastlogin");
150 list_unregister("mail");
151
152 list_unregister("pattern");
153 list_unregister("registered");
154
155 list_unregister("waitauth");
156 }
157
158 void list_register(const char *param_name, list_param_t *param) {
159 mowgli_patricia_add(list_params, param_name, param);
160 }
161
162 void list_unregister(const char *param_name) {
163 mowgli_patricia_delete(list_params, param_name);
164 }
165
166
167 static time_t parse_age(char *s)
168 {
169 time_t duration;
170
171 duration = (atol(s) * 60);
172 while (isdigit((unsigned char)*s))
173 s++;
174
175 if (*s == 'h' || *s == 'H')
176 duration *= 60;
177 else if (*s == 'd' || *s == 'D')
178 duration *= 1440;
179 else if (*s == 'w' || *s == 'W')
180 duration *= 10080;
181 else if (*s == '\0')
182 ;
183 else
184 duration = 0;
185
186 return duration;
187 }
188
189 static void build_criteriastr(char *buf, int parc, char *parv[])
190 {
191 int i;
192
193 return_if_fail(buf != NULL);
194
195 *buf = 0;
196 for (i = 0; i < parc; i++)
197 {
198 mowgli_strlcat(buf, parv[i], BUFSIZE);
199 mowgli_strlcat(buf, " ", BUFSIZE);
200 }
201 }
202
203 static void list_one(sourceinfo_t *si, myuser_t *mu, mynick_t *mn)
204 {
205 char buf[BUFSIZE];
206
207 if (mn != NULL)
208 mu = mn->owner;
209
210 *buf = '\0';
211 if (metadata_find(mu, "private:freeze:freezer")) {
212 if (*buf)
213 mowgli_strlcat(buf, " ", BUFSIZE);
214
215 mowgli_strlcat(buf, "\2[frozen]\2", BUFSIZE);
216 }
217 if (metadata_find(mu, "private:restrict:setter")) {
218 if (*buf)
219 mowgli_strlcat(buf, " ", BUFSIZE);
220
221 mowgli_strlcat(buf, "\2[restricted]\2", BUFSIZE);
222 }
223 if (mu->flags & MU_HOLD) {
224 if (*buf)
225 mowgli_strlcat(buf, " ", BUFSIZE);
226
227 mowgli_strlcat(buf, "\2[held]\2", BUFSIZE);
228 }
229 if (mu->flags & MU_WAITAUTH) {
230 if (*buf)
231 mowgli_strlcat(buf, " ", BUFSIZE);
232
233 mowgli_strlcat(buf, "\2[unverified]\2", BUFSIZE);
234 }
235
236 if (mn == NULL || !irccasecmp(mn->nick, entity(mu)->name))
237 command_success_nodata(si, "- %s (%s) %s", entity(mu)->name, mu->email, buf);
238 else
239 command_success_nodata(si, "- %s (%s) (%s) %s", mn->nick, mu->email, entity(mu)->name, buf);
240 }
241
242 static void ns_cmd_list(sourceinfo_t *si, int parc, char *parv[])
243 {
244 char criteriastr[BUFSIZE];
245
246 mowgli_patricia_iteration_state_t state;
247 myentity_iteration_state_t mestate;
248 mynick_t *mn;
249
250 int matches = 0;
251
252 int i;
253 bool error = false;
254 bool found;
255
256 MOWGLI_PATRICIA_FOREACH(mn, &state, nicklist)
257 {
258 found = true;
259
260 for (i = 0; i < parc; i++)
261 {
262 list_param_t *param = mowgli_patricia_retrieve(list_params, parv[i]);
263
264 if (param == NULL) {
265 command_fail(si, fault_badparams, _("\2%s\2 is not a recognized LIST criterion"), parv[i]);
266 error = true;
267 break;
268 }
269
270 if (param->opttype == OPT_BOOL) {
271 bool arg = true;
272
273 if (!param->is_match(mn, &arg)) {
274 found = false;
275 break;
276 }
277 } else if (param->opttype == OPT_INT) {
278 if (i + 1 < parc) {
279 int arg = atoi(parv[++i]);
280
281 if (!param->is_match (mn, &arg)) {
282 found = false;
283 break;
284 }
285 } else {
286 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, parv[i]);
287 error = true;
288 break;
289 }
290 } else if (param->opttype == OPT_STRING) {
291 if (i + 1 < parc) {
292 if (!param->is_match (mn, parv[++i])) {
293 found = false;
294 break;
295 }
296 } else {
297 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, parv[i]);
298 error = true;
299 break;
300 }
301 }
302 else if (param->opttype == OPT_AGE) {
303 if (i + 1 < parc)
304 {
305 time_t age = parse_age(parv[++i]);
306
307 if (!param->is_match (mn, &age)) {
308 found = false;
309 break;
310 }
311 } else {
312 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, parv[i]);
313 error = true;
314 break;
315 }
316 }
317 } //for (i = 0; i < parc; i++)
318
319 if (error) {
320 break;
321 }
322
323 if (found) {
324 list_one(si, NULL, mn);
325 matches++;
326 }
327 } //MOWGLI_PATRICIA_FOREACH(mn, &state, nicklist)
328
329 build_criteriastr(criteriastr, parc, parv);
330
331 logcommand(si, CMDLOG_ADMIN, "LIST: \2%s\2 (\2%d\2 matches)", criteriastr, matches);
332 if (matches == 0)
333 command_success_nodata(si, _("No nicknames matched criteria \2%s\2"), criteriastr);
334 else
335 command_success_nodata(si, ngettext(N_("\2%d\2 match for criteria \2%s\2"), N_("\2%d\2 matches for criteria \2%s\2"), matches), matches, criteriastr);
336 }
337
338 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
339 * vim:ts=8
340 * vim:sw=8
341 * vim:noexpandtab
342 */