1 |
/** |
2 |
* rmatch.C: Regex usersearch feature. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in COPYING. |
6 |
* |
7 |
* |
8 |
* Portions of this file were derived from sources bearing the following license: |
9 |
* Copyright © 2006 Robin Burchell <surreal.w00t@gmail.com> |
10 |
* Rights to this code are as documented in doc/pod/license.pod. |
11 |
* |
12 |
* $Id: rmatch.C,v 1.7 2007-09-16 18:54:44 pippijn Exp $ |
13 |
*/ |
14 |
|
15 |
/* |
16 |
* @match regex!here.+ |
17 |
* Matches `nick!user@host realname here' for each client against a given regex, and dumps matches. |
18 |
*/ |
19 |
|
20 |
#include <boost/foreach.hpp> |
21 |
|
22 |
#include "atheme.h" |
23 |
#include <ermyth/module.h> |
24 |
|
25 |
static char const rcsid[] = "$Id: rmatch.C,v 1.7 2007-09-16 18:54:44 pippijn Exp $"; |
26 |
|
27 |
REGISTER_MODULE ("operserv/rmatch", false, "The Ermyth Team <http://ermyth.xinutec.org>"); |
28 |
|
29 |
E cmdvec os_cmdtree; |
30 |
E helpvec os_helptree; |
31 |
|
32 |
static void os_cmd_rmatch (sourceinfo_t *si, int parc, char *parv[]); |
33 |
|
34 |
command_t const os_rmatch = { "RMATCH", N_("Scans the network for users based on a specific regex pattern."), PRIV_USER_AUSPEX, 1, os_cmd_rmatch }; |
35 |
|
36 |
bool |
37 |
_modinit (module *m) |
38 |
{ |
39 |
os_cmdtree << os_rmatch; |
40 |
help_addentry (os_helptree, "RMATCH", "help/operserv/rmatch", NULL); |
41 |
|
42 |
return true; |
43 |
} |
44 |
|
45 |
void |
46 |
_moddeinit (void) |
47 |
{ |
48 |
os_cmdtree >> os_rmatch; |
49 |
help_delentry (os_helptree, "RMATCH"); |
50 |
} |
51 |
|
52 |
static void |
53 |
os_cmd_rmatch (sourceinfo_t *si, int parc, char *parv[]) |
54 |
{ |
55 |
regex_t *regex; |
56 |
char usermask[512]; |
57 |
unsigned int matches = 0; |
58 |
user_t *u; |
59 |
char *args = parv[0]; |
60 |
char *pattern; |
61 |
int flags = 0; |
62 |
|
63 |
if (args == NULL) |
64 |
{ |
65 |
command_fail (si, fault::needmoreparams, STR_INSUFFICIENT_PARAMS, "RMATCH"); |
66 |
command_fail (si, fault::needmoreparams, _("Syntax: RMATCH /<regex>/[i]")); |
67 |
return; |
68 |
} |
69 |
|
70 |
pattern = regex_extract (args, &args, &flags); |
71 |
if (pattern == NULL) |
72 |
{ |
73 |
command_fail (si, fault::badparams, STR_INVALID_PARAMS, "RMATCH"); |
74 |
command_fail (si, fault::badparams, _("Syntax: RMATCH /<regex>/[i]")); |
75 |
return; |
76 |
} |
77 |
|
78 |
regex = regex_create (pattern, flags); |
79 |
|
80 |
if (regex == NULL) |
81 |
{ |
82 |
command_fail (si, fault::badparams, _("The provided regex \2%s\2 is invalid."), pattern); |
83 |
return; |
84 |
} |
85 |
|
86 |
foreach (user_t::pair_type &up, user_t::map) |
87 |
{ |
88 |
u = up.second; |
89 |
sprintf (usermask, "%s!%s@%s %s", u->nick, u->user, u->host, u->gecos); |
90 |
|
91 |
if (regex_match (regex, usermask) == true) |
92 |
{ |
93 |
/* match */ |
94 |
command_success_nodata (si, _("\2Match:\2 %s!%s@%s %s"), u->nick, u->user, u->host, u->gecos); |
95 |
matches++; |
96 |
} |
97 |
} |
98 |
|
99 |
regex_destroy (regex); |
100 |
command_success_nodata (si, _("\2%d\2 matches for %s"), matches, pattern); |
101 |
logcommand (si, CMDLOG_ADMIN, "RMATCH %s (%d matches)", pattern, matches); |
102 |
snoop ("RMATCH: \2%s\2 by \2%s\2", pattern, get_oper_name (si)); |
103 |
} |