Debian Codegraph @

modules/operserv/rmatch.c

raw
1 /*
2 * Copyright (c) 2006 Robin Burchell <[email protected]>
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * Regex usersearch feature.
6 *
7 */
8
9 /*
10 * @match regex!here.+
11 * Matches `nick!user@host realname here' for each client against a given regex, and dumps matches.
12 */
13 #include "atheme.h"
14 #include <limits.h>
15
16 DECLARE_MODULE_V1
17 (
18 "operserv/rmatch", false, _modinit, _moddeinit,
19 PACKAGE_STRING,
20 VENDOR_STRING
21 );
22
23 static void os_cmd_rmatch(sourceinfo_t *si, int parc, char *parv[]);
24
25 command_t os_rmatch = { "RMATCH", N_("Scans the network for users based on a specific regex pattern."), PRIV_USER_AUSPEX, 1, os_cmd_rmatch, { .path = "oservice/rmatch" } };
26
27 void _modinit(module_t *m)
28 {
29 service_named_bind_command("operserv", &os_rmatch);
30 }
31
32 void _moddeinit(module_unload_intent_t intent)
33 {
34 service_named_unbind_command("operserv", &os_rmatch);
35 }
36
37 #define MAXMATCHES_DEF 1000
38
39 static void os_cmd_rmatch(sourceinfo_t *si, int parc, char *parv[])
40 {
41 atheme_regex_t *regex;
42 char usermask[512];
43 unsigned int matches = 0, maxmatches;
44 mowgli_patricia_iteration_state_t state;
45 user_t *u;
46 char *args = parv[0];
47 char *pattern;
48 int flags = 0;
49
50 if (args == NULL)
51 {
52 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "RMATCH");
53 command_fail(si, fault_needmoreparams, _("Syntax: RMATCH /<regex>/[i] [FORCE]"));
54 return;
55 }
56
57 pattern = regex_extract(args, &args, &flags);
58 if (pattern == NULL)
59 {
60 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "RMATCH");
61 command_fail(si, fault_badparams, _("Syntax: RMATCH /<regex>/[i] [FORCE]"));
62 return;
63 }
64
65 while (*args == ' ')
66 args++;
67
68 if (!strcasecmp(args, "FORCE"))
69 maxmatches = UINT_MAX;
70 else if (*args == '\0')
71 maxmatches = MAXMATCHES_DEF;
72 else
73 {
74 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "RMATCH");
75 command_fail(si, fault_badparams, _("Syntax: RMATCH /<regex>/[i] [FORCE]"));
76 return;
77 }
78
79 regex = regex_create(pattern, flags);
80
81 if (regex == NULL)
82 {
83 command_fail(si, fault_badparams, _("The provided regex \2%s\2 is invalid."), pattern);
84 return;
85 }
86
87 MOWGLI_PATRICIA_FOREACH(u, &state, userlist)
88 {
89 sprintf(usermask, "%s!%s@%s %s", u->nick, u->user, u->host, u->gecos);
90
91 if (regex_match(regex, usermask))
92 {
93 matches++;
94 if (matches <= maxmatches)
95 command_success_nodata(si, _("\2Match:\2 %s!%s@%s %s"), u->nick, u->user, u->host, u->gecos);
96 else if (matches == maxmatches + 1)
97 {
98 command_success_nodata(si, _("Too many matches, not displaying any more"));
99 command_success_nodata(si, _("Add the FORCE keyword to see them all"));
100 }
101 }
102 }
103
104 regex_destroy(regex);
105 command_success_nodata(si, _("\2%d\2 matches for %s"), matches, pattern);
106 logcommand(si, CMDLOG_ADMIN, "RMATCH: \2%s\2 (\2%d\2 matches)", pattern, matches);
107 }
108
109 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
110 * vim:ts=8
111 * vim:sw=8
112 * vim:noexpandtab
113 */