ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/operserv/compare.C
Revision: 1.6
Committed: Sun Sep 16 18:54:43 2007 UTC (19 years ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +10 -5 lines
Log Message:
#defines to enum

File Contents

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