| 1 |
pippijn |
1.1 |
/* |
| 2 |
|
|
* Copyright © 2006 Robin Burchell <surreal.w00t@gmail.com> |
| 3 |
|
|
* Rights to this code are documented in doc/LICENCE. |
| 4 |
|
|
* |
| 5 |
|
|
* This file contains functionality implementing OperServ COMPARE. |
| 6 |
|
|
* |
| 7 |
pippijn |
1.2 |
* $Id: compare.C,v 1.1 2007-07-19 08:24:55 pippijn Exp $ |
| 8 |
pippijn |
1.1 |
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include "atheme.h" |
| 11 |
|
|
|
| 12 |
pippijn |
1.2 |
static char const rcsid[] = "$Id$"; |
| 13 |
pippijn |
1.1 |
|
| 14 |
|
|
struct os_compare_listeners : callback::has_listeners |
| 15 |
|
|
{ |
| 16 |
|
|
}; |
| 17 |
|
|
|
| 18 |
|
|
os_compare_listeners listeners; |
| 19 |
|
|
|
| 20 |
|
|
DECLARE_MODULE_V1 ("operserv/compare", false, _modinit, _moddeinit, rcsid, "Robin Burchell <surreal.w00t@gmail.com>"); |
| 21 |
|
|
|
| 22 |
|
|
static void os_cmd_compare (sourceinfo_t *si, int parc, char *parv[]); |
| 23 |
|
|
|
| 24 |
|
|
command_t os_compare = { "COMPARE", N_("Compares two users or channels."), PRIV_CHAN_AUSPEX, 2, |
| 25 |
|
|
os_cmd_compare |
| 26 |
|
|
}; |
| 27 |
|
|
|
| 28 |
|
|
cmdvec *os_cmdtree; |
| 29 |
|
|
helpvec *os_helptree; |
| 30 |
|
|
|
| 31 |
|
|
void |
| 32 |
|
|
_modinit (module_t *m) |
| 33 |
|
|
{ |
| 34 |
|
|
MODULE_USE_SYMBOL_T (os_cmdtree, cmdvec, "operserv/main", "os_cmdtree"); |
| 35 |
|
|
MODULE_USE_SYMBOL_T (os_helptree, helpvec, "operserv/main", "os_helptree"); |
| 36 |
|
|
|
| 37 |
|
|
os_cmdtree << os_compare; |
| 38 |
|
|
help_addentry (os_helptree, "COMPARE", "help/oservice/compare", NULL); |
| 39 |
|
|
} |
| 40 |
|
|
|
| 41 |
|
|
void |
| 42 |
|
|
_moddeinit (void) |
| 43 |
|
|
{ |
| 44 |
|
|
os_cmdtree >> os_compare; |
| 45 |
|
|
help_delentry (os_helptree, "COMPARE"); |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
|
|
static void |
| 49 |
|
|
os_cmd_compare (sourceinfo_t *si, int parc, char *parv[]) |
| 50 |
|
|
{ |
| 51 |
|
|
char *object1 = parv[0]; |
| 52 |
|
|
char *object2 = parv[1]; |
| 53 |
|
|
channel_t *c1, *c2; |
| 54 |
|
|
user_t *u1, *u2; |
| 55 |
|
|
node_t *n1, *n2; |
| 56 |
|
|
chanuser_t *cu1, *cu2; |
| 57 |
|
|
int matches = 0; |
| 58 |
|
|
|
| 59 |
|
|
int temp = 0; |
| 60 |
|
|
char buf[512]; |
| 61 |
|
|
char tmpbuf[100]; |
| 62 |
|
|
|
| 63 |
|
|
bzero (buf, 512); |
| 64 |
|
|
bzero (tmpbuf, 100); |
| 65 |
|
|
|
| 66 |
|
|
if (!object1 || !object2) |
| 67 |
|
|
{ |
| 68 |
|
|
command_fail (si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "COMPARE"); |
| 69 |
|
|
command_fail (si, fault_needmoreparams, _("Syntax: COMPARE <nick|#channel> <nick|#channel>")); |
| 70 |
|
|
return; |
| 71 |
|
|
} |
| 72 |
|
|
|
| 73 |
|
|
if (*object1 == '#') |
| 74 |
|
|
{ |
| 75 |
|
|
if (*object2 == '#') |
| 76 |
|
|
{ |
| 77 |
|
|
/* compare on two channels */ |
| 78 |
|
|
c1 = channel_find (object1); |
| 79 |
|
|
c2 = channel_find (object2); |
| 80 |
|
|
|
| 81 |
|
|
if (!c1 || !c2) |
| 82 |
|
|
{ |
| 83 |
|
|
command_fail (si, fault_nosuch_target, _("Both channels must exist for @compare")); |
| 84 |
|
|
return; |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
|
|
command_success_nodata (si, _("Common users in \2%s\2 and \2%s\2"), object1, object2); |
| 88 |
|
|
|
| 89 |
|
|
/* iterate over the users in channel 1 */ |
| 90 |
|
|
LIST_FOREACH (n1, c1->members.head) |
| 91 |
|
|
{ |
| 92 |
|
|
cu1 = static_cast < chanuser_t *>(n1->data); |
| 93 |
|
|
|
| 94 |
|
|
/* now iterate over each of channel 2's members, and compare them to the current user from ch1 */ |
| 95 |
|
|
LIST_FOREACH (n2, c2->members.head) |
| 96 |
|
|
{ |
| 97 |
|
|
cu2 = static_cast < chanuser_t *>(n2->data); |
| 98 |
|
|
|
| 99 |
|
|
/* match! */ |
| 100 |
|
|
if (cu1->user == cu2->user) |
| 101 |
|
|
{ |
| 102 |
|
|
/* common user! */ |
| 103 |
|
|
snprintf (tmpbuf, 99, "%s, ", cu1->user->nick); |
| 104 |
|
|
strcat ((char *) buf, tmpbuf); |
| 105 |
|
|
bzero (tmpbuf, 100); |
| 106 |
|
|
|
| 107 |
|
|
/* if too many, output to user */ |
| 108 |
|
|
if (temp >= 5 || strlen (buf) > 300) |
| 109 |
|
|
{ |
| 110 |
|
|
command_success_nodata (si, "%s", buf); |
| 111 |
|
|
bzero (buf, 512); |
| 112 |
|
|
temp = 0; |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
temp++; |
| 116 |
|
|
matches++; |
| 117 |
|
|
} |
| 118 |
|
|
} |
| 119 |
|
|
} |
| 120 |
|
|
} |
| 121 |
|
|
else |
| 122 |
|
|
{ |
| 123 |
|
|
/* bad syntax */ |
| 124 |
|
|
command_fail (si, fault_badparams, _("Bad syntax for @compare. Use @compare on two channels, or two users.")); |
| 125 |
|
|
return; |
| 126 |
|
|
} |
| 127 |
|
|
} |
| 128 |
|
|
else |
| 129 |
|
|
{ |
| 130 |
|
|
if (*object2 == '#') |
| 131 |
|
|
{ |
| 132 |
|
|
/* bad syntax */ |
| 133 |
|
|
command_fail (si, fault_badparams, _("Bad syntax for @compare. Use @compare on two channels, or two users.")); |
| 134 |
|
|
return; |
| 135 |
|
|
} |
| 136 |
|
|
else |
| 137 |
|
|
{ |
| 138 |
|
|
/* compare on two users */ |
| 139 |
|
|
u1 = user_find_named (object1); |
| 140 |
|
|
u2 = user_find_named (object2); |
| 141 |
|
|
|
| 142 |
|
|
if (!u1 || !u2) |
| 143 |
|
|
{ |
| 144 |
|
|
command_fail (si, fault_nosuch_target, _("Both users must exist for @compare")); |
| 145 |
|
|
return; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
|
command_success_nodata (si, _("Common channels for \2%s\2 and \2%s\2"), object1, object2); |
| 149 |
|
|
|
| 150 |
|
|
/* iterate over the channels of user 1 */ |
| 151 |
|
|
LIST_FOREACH (n1, u1->channels.head) |
| 152 |
|
|
{ |
| 153 |
|
|
cu1 = static_cast < chanuser_t *>(n1->data); |
| 154 |
|
|
|
| 155 |
|
|
/* now iterate over each of user 2's channels to the current channel from user 1 */ |
| 156 |
|
|
LIST_FOREACH (n2, u2->channels.head) |
| 157 |
|
|
{ |
| 158 |
|
|
cu2 = static_cast < chanuser_t *>(n2->data); |
| 159 |
|
|
|
| 160 |
|
|
/* match! */ |
| 161 |
|
|
if (cu1->chan == cu2->chan) |
| 162 |
|
|
{ |
| 163 |
|
|
/* common channel! */ |
| 164 |
|
|
snprintf (tmpbuf, 99, "%s, ", cu1->chan->name); |
| 165 |
|
|
strcat ((char *) buf, tmpbuf); |
| 166 |
|
|
bzero (tmpbuf, 100); |
| 167 |
|
|
|
| 168 |
|
|
/* if too many, output to user */ |
| 169 |
|
|
if (temp >= 5 || strlen (buf) > 300) |
| 170 |
|
|
{ |
| 171 |
|
|
command_success_nodata (si, "%s", buf); |
| 172 |
|
|
bzero (buf, 512); |
| 173 |
|
|
temp = 0; |
| 174 |
|
|
} |
| 175 |
|
|
|
| 176 |
|
|
temp++; |
| 177 |
|
|
matches++; |
| 178 |
|
|
} |
| 179 |
|
|
} |
| 180 |
|
|
} |
| 181 |
|
|
} |
| 182 |
|
|
} |
| 183 |
|
|
|
| 184 |
|
|
if (buf[0] != 0) |
| 185 |
|
|
command_success_nodata (si, "%s", buf); |
| 186 |
|
|
|
| 187 |
|
|
command_success_nodata (si, _("\2%d\2 matches comparing %s and %s"), matches, object1, object2); |
| 188 |
|
|
logcommand (si, CMDLOG_ADMIN, "COMPARE %s to %s (%d matches)", object1, object2, matches); |
| 189 |
|
|
snoop ("COMPARE: \2%s\2 to \2%s\2 by \2%s\2", object1, object2, get_oper_name (si)); |
| 190 |
|
|
} |
| 191 |
|
|
|
| 192 |
|
|
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 193 |
|
|
* vim:ts=8 |
| 194 |
|
|
* vim:sw=8 |
| 195 |
|
|
* vim:noexpandtab |
| 196 |
|
|
*/ |