ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/operserv/compare.C
Revision: 1.5
Committed: Thu Aug 30 19:56:24 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +8 -8 lines
Log Message:
- put faultcodes into their own namespace
- removed old files
- limited header garbage in atheme.h
- macros to inline bools for connection_t::is_*
- put some connection_t functions into the connection_t class

File Contents

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