/** * rmatch.C: Regex usersearch feature. * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Copyright © 2006 Robin Burchell * Rights to this code are as documented in doc/pod/license.pod. * * $Id: rmatch.C,v 1.8 2007/09/22 14:27:28 pippijn Exp $ */ /* * @match regex!here.+ * Matches `nick!user@host realname here' for each client against a given regex, and dumps matches. */ #include #include "atheme.h" #include static char const rcsid[] = "$Id: rmatch.C,v 1.8 2007/09/22 14:27:28 pippijn Exp $"; REGISTER_MODULE ("operserv/rmatch", false, "The Ermyth Team "); E cmdvec os_cmdtree; E helpvec os_helptree; static void os_cmd_rmatch (sourceinfo_t *si, int parc, char *parv[]); 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 }; bool _modinit (module *m) { os_cmdtree << os_rmatch; help_addentry (os_helptree, "RMATCH", "help/operserv/rmatch", NULL); return true; } void _moddeinit (void) { os_cmdtree >> os_rmatch; help_delentry (os_helptree, "RMATCH"); } static void os_cmd_rmatch (sourceinfo_t *si, int parc, char *parv[]) { regex_t *regex; char usermask[512]; unsigned int matches = 0; user_t *u; char *args = parv[0]; char *pattern; int flags = 0; if (args == NULL) { command_fail (si, fault::needmoreparams, STR_INSUFFICIENT_PARAMS, "RMATCH"); command_fail (si, fault::needmoreparams, _("Syntax: RMATCH //[i]")); return; } pattern = regex_extract (args, &args, &flags); if (pattern == NULL) { command_fail (si, fault::badparams, STR_INVALID_PARAMS, "RMATCH"); command_fail (si, fault::badparams, _("Syntax: RMATCH //[i]")); return; } regex = regex_create (pattern, flags); if (regex == NULL) { command_fail (si, fault::badparams, _("The provided regex \2%s\2 is invalid."), pattern); return; } foreach (user_t::pair_type &up, user_t::map) { u = up.second; sprintf (usermask, "%s!%s@%s %s", u->nick, u->user, u->host, u->gecos); if (regex_match (regex, usermask) == true) { /* match */ command_success_nodata (si, _("\2Match:\2 %s!%s@%s %s"), u->nick, u->user, u->host, u->gecos); matches++; } } regex_destroy (regex); command_success_nodata (si, _("\2%d\2 matches for %s"), matches, pattern); logcommand (si, CMDLOG_ADMIN, "RMATCH %s (%d matches)", pattern, matches); snoop ("RMATCH: \2%s\2 by \2%s\2", pattern, get_oper_name (si)); }