ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/modules/chanserv/ban.C
Revision: 1.8
Committed: Sat Sep 22 14:27:27 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +3 -3 lines
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

# Content
1 /**
2 * ban.C: This file contains code for the ChanServ BAN/UNBAN function.
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 © 2005 William Pitcock, et al.
10 * Rights to this code are as documented in doc/pod/license.pod.
11 *
12 * $Id: ban.C,v 1.7 2007-09-16 18:54:42 pippijn Exp $
13 */
14
15 #include "atheme.h"
16 #include <ermyth/module.h>
17 #include <account/mychan.h>
18 #include <account/chanacs.h>
19
20 static char const rcsid[] = "$Id: ban.C,v 1.7 2007-09-16 18:54:42 pippijn Exp $";
21
22 REGISTER_MODULE ("chanserv/ban", false, "The Ermyth Team <http://ermyth.xinutec.org>");
23
24 static void cs_cmd_ban (sourceinfo_t *si, int parc, char *parv[]);
25 static void cs_cmd_unban (sourceinfo_t *si, int parc, char *parv[]);
26
27 command_t const cs_ban = { "BAN", N_("Sets a ban on a channel."), AC_NONE, 2, cs_cmd_ban };
28 command_t const cs_unban = { "UNBAN", N_("Removes a ban on a channel."), AC_NONE, 2, cs_cmd_unban };
29
30 E cmdvec cs_cmdtree;
31 E helpvec cs_helptree;
32
33 bool
34 _modinit (module *m)
35 {
36 cs_cmdtree << cs_ban;
37 cs_cmdtree << cs_unban;
38
39 help_addentry (cs_helptree, "BAN", "help/chanserv/ban", NULL);
40 help_addentry (cs_helptree, "UNBAN", "help/chanserv/unban", NULL);
41
42 return true;
43 }
44
45 void
46 _moddeinit ()
47 {
48 cs_cmdtree >> cs_ban;
49 cs_cmdtree >> cs_unban;
50
51 help_delentry (cs_helptree, "BAN");
52 help_delentry (cs_helptree, "UNBAN");
53 }
54
55 static void
56 cs_cmd_ban (sourceinfo_t *si, int parc, char *parv[])
57 {
58 char *channel = parv[0];
59 char *target = parv[1];
60 channel_t *c = channel_find (channel);
61 mychan_t *mc = mychan_t::find (channel);
62 user_t *tu;
63
64 if (!channel || !target)
65 {
66 command_fail (si, fault::needmoreparams, STR_INSUFFICIENT_PARAMS, "BAN");
67 command_fail (si, fault::needmoreparams, _("Syntax: BAN <#channel> <nickname|hostmask>"));
68 return;
69 }
70
71 if (!mc)
72 {
73 command_fail (si, fault::nosuch_target, _("\2%s\2 is not registered."), channel);
74 return;
75 }
76
77 if (!c)
78 {
79 command_fail (si, fault::nosuch_target, _("\2%s\2 is currently empty."), channel);
80 return;
81 }
82
83 if (!si->smu)
84 {
85 command_fail (si, fault::noprivs, _("You are not logged in."));
86 return;
87 }
88
89 if (!chanacs_source_has_flag (mc, si, CA_REMOVE))
90 {
91 command_fail (si, fault::noprivs, _("You are not authorized to perform this operation."));
92 return;
93 }
94
95 if (mc->find_metadata ("private:close:closer"))
96 {
97 command_fail (si, fault::noprivs, _("\2%s\2 is closed."), channel);
98 return;
99 }
100
101 if (validhostmask (target))
102 {
103 modestack_mode_param (chansvs.nick, c, MTYPE_ADD, 'b', target);
104 chanban_add (c, target, 'b');
105 logcommand (si, CMDLOG_DO, "%s BAN %s", mc->name, target);
106 if (!chanuser_find (mc->chan, si->su))
107 command_success_nodata (si, _("Banned \2%s\2 on \2%s\2."), target, channel);
108 return;
109 }
110 else if ((tu = user_find_named (target)))
111 {
112 char hostbuf[BUFSIZE];
113
114 hostbuf[0] = '\0';
115
116 strlcat (hostbuf, "*!*@", BUFSIZE);
117 strlcat (hostbuf, tu->vhost, BUFSIZE);
118
119 modestack_mode_param (chansvs.nick, c, MTYPE_ADD, 'b', hostbuf);
120 chanban_add (c, hostbuf, 'b');
121 logcommand (si, CMDLOG_DO, "%s BAN %s (for user %s!%s@%s)", mc->name, hostbuf, tu->nick, tu->user, tu->vhost);
122 if (!chanuser_find (mc->chan, si->su))
123 command_success_nodata (si, _("Banned \2%s\2 on \2%s\2."), target, channel);
124 return;
125 }
126 else
127 {
128 command_fail (si, fault::badparams, _("Invalid nickname/hostmask provided: \2%s\2"), target);
129 command_fail (si, fault::badparams, _("Syntax: BAN <#channel> <nickname|hostmask>"));
130 return;
131 }
132 }
133
134 static void
135 cs_cmd_unban (sourceinfo_t *si, int parc, char *parv[])
136 {
137 char *channel = parv[0];
138 char *target = parv[1];
139 channel_t *c = channel_find (channel);
140 mychan_t *mc = mychan_t::find (channel);
141 user_t *tu;
142 chanban_t *cb;
143
144 if (!channel)
145 {
146 command_fail (si, fault::needmoreparams, STR_INSUFFICIENT_PARAMS, "UNBAN");
147 command_fail (si, fault::needmoreparams, _("Syntax: UNBAN <#channel> <nickname|hostmask>"));
148 return;
149 }
150
151 if (!target)
152 {
153 if (si->su == NULL)
154 {
155 command_fail (si, fault::needmoreparams, STR_INSUFFICIENT_PARAMS, "UNBAN");
156 command_fail (si, fault::needmoreparams, _("Syntax: UNBAN <#channel> <nickname|hostmask>"));
157 return;
158 }
159 target = si->su->nick;
160 }
161
162 if (!mc)
163 {
164 command_fail (si, fault::nosuch_target, _("\2%s\2 is not registered."), channel);
165 return;
166 }
167
168 if (!c)
169 {
170 command_fail (si, fault::nosuch_target, _("\2%s\2 is currently empty."), channel);
171 return;
172 }
173
174 if (!si->smu)
175 {
176 command_fail (si, fault::noprivs, _("You are not logged in."));
177 return;
178 }
179
180 if (!chanacs_source_has_flag (mc, si, CA_REMOVE))
181 {
182 command_fail (si, fault::noprivs, _("You are not authorized to perform this operation."));
183 return;
184 }
185
186 if ((tu = user_find_named (target)))
187 {
188 node_t *n, *tn;
189 char hostbuf2[BUFSIZE];
190 int count = 0;
191
192 snprintf (hostbuf2, BUFSIZE, "%s!%s@%s", tu->nick, tu->user, tu->vhost);
193 for (n = phandler->next_matching_ban (c, tu, 'b', c->bans.head); n != NULL; n = phandler->next_matching_ban (c, tu, 'b', tn))
194 {
195 tn = n->next;
196 cb = static_cast<chanban_t *> (n->data);
197
198 logcommand (si, CMDLOG_DO, "%s UNBAN %s (for user %s)", mc->name, cb->mask, hostbuf2);
199 modestack_mode_param (chansvs.nick, c, MTYPE_DEL, cb->type, cb->mask);
200 chanban_delete (cb);
201 count++;
202 }
203 if (count > 0)
204 command_success_nodata (si, _("Unbanned \2%s\2 on \2%s\2 (%d ban%s removed)."), target, channel, count, (count != 1 ? "s" : ""));
205 else
206 command_success_nodata (si, _("No bans found matching \2%s\2 on \2%s\2."), target, channel);
207 return;
208 }
209 else if ((cb = chanban_find (c, target, 'b')) != NULL || validhostmask (target))
210 {
211 if (cb)
212 {
213 modestack_mode_param (chansvs.nick, c, MTYPE_DEL, 'b', target);
214 chanban_delete (cb);
215 logcommand (si, CMDLOG_DO, "%s UNBAN %s", mc->name, target);
216 if (!chanuser_find (mc->chan, si->su))
217 command_success_nodata (si, _("Unbanned \2%s\2 on \2%s\2."), target, channel);
218 }
219 else
220 command_fail (si, fault::nosuch_key, _("No such ban \2%s\2 on \2%s\2."), target, channel);
221
222 return;
223 }
224 else
225 {
226 command_fail (si, fault::badparams, _("Invalid nickname/hostmask provided: \2%s\2"), target);
227 command_fail (si, fault::badparams, _("Syntax: UNBAN <#channel> [nickname|hostmask]"));
228 return;
229 }
230 }