]> jfr.im git - irc/evilnet/x3.git/blame - src/opserv.c
This needs more testing *hint hint* rubin :P I diff'ed srvx 1.3.1 to 1.3 then picked...
[irc/evilnet/x3.git] / src / opserv.c
CommitLineData
d76ed9a9 1/* opserv.c - IRC Operator assistance service
2 * Copyright 2000-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 5 *
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21#include "conf.h"
22#include "gline.h"
23#include "global.h"
24#include "nickserv.h"
25#include "modcmd.h"
26#include "opserv.h"
27#include "timeq.h"
28#include "saxdb.h"
d914d1cb 29#include "shun.h"
d76ed9a9 30
31#ifdef HAVE_SYS_TIMES_H
32#include <sys/times.h>
33#endif
34#ifdef HAVE_NETINET_IN_H
35#include <netinet/in.h>
36#endif
37#ifdef HAVE_ARPA_INET_H
38#include <arpa/inet.h>
39#endif
27eaa617 40#ifdef HAVE_REGEX_H
41#include <regex.h>
42#endif
d76ed9a9 43
44#define OPSERV_CONF_NAME "services/opserv"
45
46#define KEY_ALERT_CHANNEL "alert_channel"
47#define KEY_ALERT_CHANNEL_MODES "alert_channel_modes"
48#define KEY_DEBUG_CHANNEL "debug_channel"
49#define KEY_DEBUG_CHANNEL_MODES "debug_channel_modes"
50#define KEY_UNTRUSTED_MAX "untrusted_max"
51#define KEY_PURGE_LOCK_DELAY "purge_lock_delay"
52#define KEY_JOIN_FLOOD_MODERATE "join_flood_moderate"
53#define KEY_JOIN_FLOOD_MODERATE_THRESH "join_flood_moderate_threshold"
54#define KEY_NICK "nick"
55#define KEY_JOIN_POLICER "join_policer"
56#define KEY_NEW_USER_POLICER "new_user_policer"
57#define KEY_REASON "reason"
58#define KEY_RESERVES "reserves"
59#define KEY_IDENT "username" /* for compatibility with 1.0 DBs */
60#define KEY_HOSTNAME "hostname"
61#define KEY_DESC "description"
62#define KEY_BAD_WORDS "bad"
63#define KEY_EXEMPT_CHANNELS "exempt"
64#define KEY_SECRET_WORDS "secret"
65#define KEY_TRUSTED_HOSTS "trusted"
66#define KEY_OWNER "owner"
67#define KEY_GAGS "gags"
68#define KEY_ALERTS "alerts"
69#define KEY_REACTION "reaction"
70#define KEY_DISCRIM "discrim"
71#define KEY_WARN "chanwarn"
72#define KEY_MAX "max"
73#define KEY_TIME "time"
74#define KEY_MAX_CLIENTS "max_clients"
75#define KEY_LIMIT "limit"
76#define KEY_EXPIRES "expires"
77#define KEY_STAFF_AUTH_CHANNEL "staff_auth_channel"
78#define KEY_STAFF_AUTH_CHANNEL_MODES "staff_auth_channel_modes"
79#define KEY_CLONE_GLINE_DURATION "clone_gline_duration"
80#define KEY_BLOCK_GLINE_DURATION "block_gline_duration"
d914d1cb 81#define KEY_BLOCK_SHUN_DURATION "block_shun_duration"
d76ed9a9 82#define KEY_ISSUER "issuer"
83#define KEY_ISSUED "issued"
5a1daaab 84#define KEY_ADMIN_LEVEL "admin_level"
d76ed9a9 85
258d1427 86#define IDENT_FORMAT "%s [%s@%s/%s]"
2f61d1d7 87#define IDENT_DATA(user) user->nick, user->ident, user->hostname, irc_ntoa(&user->ip)
258d1427 88#define MAX_CHANNELS_WHOIS 50
d76ed9a9 89#define OSMSG_PART_REASON "%s has no reason."
90#define OSMSG_KICK_REQUESTED "Kick requested by %s."
91#define OSMSG_KILL_REQUESTED "Kill requested by %s."
92#define OSMSG_GAG_REQUESTED "Gag requested by %s."
93
94static const struct message_entry msgtab[] = {
de9510bc 95 { "OSMSG_BAR", "----------------------------------------" },
d76ed9a9 96 { "OSMSG_USER_ACCESS_IS", "$b%s$b (account $b%s$b) has %d access." },
97 { "OSMSG_LEVEL_TOO_LOW", "You lack sufficient access to use this command." },
98 { "OSMSG_NEED_CHANNEL", "You must specify a channel for $b%s$b." },
99 { "OSMSG_INVALID_IRCMASK", "$b%s$b is an invalid IRC hostmask." },
100 { "OSMSG_ADDED_BAN", "I have banned $b%s$b from $b%s$b." },
d914d1cb 101 { "OSMSG_SHUN_ISSUED", "Shun issued for $b%s$b." },
102 { "OSMSG_SHUN_REMOVED", "Shun removed for $b%s$b." },
103 { "OSMSG_SHUN_FORCE_REMOVED", "Unknown/expired Shun removed for $b%s$b." },
104 { "OSMSG_SHUN_ONE_REFRESHED", "All Shuns resent to $b%s$b." },
105 { "OSMSG_SHUN_REFRESHED", "All Shuns refreshed." },
d76ed9a9 106 { "OSMSG_GLINE_ISSUED", "G-line issued for $b%s$b." },
107 { "OSMSG_GLINE_REMOVED", "G-line removed for $b%s$b." },
108 { "OSMSG_GLINE_FORCE_REMOVED", "Unknown/expired G-line removed for $b%s$b." },
109 { "OSMSG_GLINES_ONE_REFRESHED", "All G-lines resent to $b%s$b." },
110 { "OSMSG_GLINES_REFRESHED", "All G-lines refreshed." },
111 { "OSMSG_CLEARBANS_DONE", "Cleared all bans from channel $b%s$b." },
112 { "OSMSG_CLEARMODES_DONE", "Cleared all modes from channel $b%s$b." },
113 { "OSMSG_NO_CHANNEL_MODES", "Channel $b%s$b had no modes to clear." },
114 { "OSMSG_DEOP_DONE", "Deopped the requested lusers." },
115 { "OSMSG_DEOPALL_DONE", "Deopped everyone on $b%s$b." },
55342ce8 116 { "OSMSG_DEHOP_DONE", "Dehalfopped the requested lusers." },
117 { "OSMSG_DEHOPALL_DONE", "Dehalfopped everyone on $b%s$b." },
d76ed9a9 118 { "OSMSG_NO_DEBUG_CHANNEL", "No debug channel has been configured." },
119 { "OSMSG_INVITE_DONE", "Invited $b%s$b to $b%s$b." },
120 { "OSMSG_ALREADY_THERE", "You are already in $b%s$b." },
121 { "OSMSG_JOIN_DONE", "I have joined $b%s$b." },
122 { "OSMSG_ALREADY_JOINED", "I am already in $b%s$b." },
123 { "OSMSG_NOT_ON_CHANNEL", "$b%s$b does not seem to be on $b%s$b." },
124 { "OSMSG_KICKALL_DONE", "I have cleared out %s." },
125 { "OSMSG_LEAVING", "Leaving $b%s$b." },
126 { "OSMSG_MODE_SET", "I have set the modes for $b%s$b." },
127 { "OSMSG_OP_DONE", "Opped the requested lusers." },
128 { "OSMSG_OPALL_DONE", "Opped everyone on $b%s$b." },
55342ce8 129 { "OSMSG_HOP_DONE", "Halfopped the requested lusers." },
130 { "OSMSG_HOPALL_DONE", "Halfopped everyone on $b%s$b." },
d76ed9a9 131 { "OSMSG_WHOIS_IDENT", "%s (%s@%s) from %d.%d.%d.%d" },
132 { "OSMSG_WHOIS_NICK", "Nick : %s" },
133 { "OSMSG_WHOIS_HOST", "Host : %s@%s" },
134 { "OSMSG_WHOIS_FAKEHOST", "Fakehost: %s" },
135 { "OSMSG_WHOIS_IP", "Real IP : %s" },
136 { "OSMSG_WHOIS_MODES", "Modes : +%s " },
137 { "OSMSG_WHOIS_INFO", "Info : %s" },
138 { "OSMSG_WHOIS_NUMERIC", "Numnick : %s" },
139 { "OSMSG_WHOIS_SERVER", "Server : %s" },
140 { "OSMSG_WHOIS_NICK_AGE", "Nick Age: %s" },
141 { "OSMSG_WHOIS_ACCOUNT", "Account : %s" },
142 { "OSMSG_WHOIS_CHANNELS", "Channels: %s" },
143 { "OSMSG_WHOIS_HIDECHANS", "Channel list omitted for your sanity." },
144 { "OSMSG_UNBAN_DONE", "Ban(s) removed from channel %s." },
145 { "OSMSG_CHANNEL_VOICED", "All users on %s voiced." },
146 { "OSMSG_CHANNEL_DEVOICED", "All voiced users on %s de-voiced." },
147 { "OSMSG_BAD_MODIFIER", "Unknown bad-word modifier $b%s$b." },
148 { "OSMSG_BAD_REDUNDANT", "$b%s$b is already covered by a bad word ($b%s$b)." },
149 { "OSMSG_BAD_GROWING", "Replacing bad word $b%s$b with shorter bad word $b%s$b." },
150 { "OSMSG_BAD_NUKING", " .. and removing redundant bad word $b%s$b." },
151 { "OSMSG_ADDED_BAD", "Added $b%s$b to the bad-word list." },
152 { "OSMSG_REMOVED_BAD", "Removed $b%s$b from the bad-word list." },
153 { "OSMSG_NOT_BAD_WORD", "$b%s$b is not a bad word." },
154 { "OSMSG_ADDED_EXEMPTION", "Added $b%s$b to the bad-word exemption list." },
155 { "OSMSG_ADDED_EXEMPTIONS", "Added %d exception(s) to the bad word list." },
156 { "OSMSG_REMOVED_EXEMPTION", "Removed $b%s$b from the exemption list." },
157 { "OSMSG_NOT_EXEMPT", "$b%s$b is not on the exempt list." },
158 { "OSMSG_ALREADY_TRUSTED", "Host $b%s$b is already trusted (use $bdeltrust$b and then $baddtrust$b to adjust)." },
159 { "OSMSG_NOT_TRUSTED", "Host $b%s$b is not trusted." },
160 { "OSMSG_BAD_IP", "$b%s$b is not a valid IP address" },
161 { "OSMSG_BAD_NUMBER", "$b%s$b is not a number" },
162 { "OSMSG_ADDED_TRUSTED", "Added trusted hosts to the trusted-hosts list." },
163 { "OSMSG_UPDATED_TRUSTED", "Updated trusted host $b%s$b." },
164 { "OSMSG_REMOVED_TRUSTED", "Removed trusted hosts from the trusted-hosts list." },
165 { "OSMSG_CLONE_EXISTS", "Nick $b%s$b is already in use." },
166 { "OSMSG_NOT_A_HOSTMASK", "The hostmask must be in user@host form." },
167 { "OSMSG_BADWORD_LIST", "Bad words: %s" },
168 { "OSMSG_EXEMPTED_LIST", "Exempted channels: %s" },
169 { "OSMSG_GLINE_COUNT", "There are %d glines active on the network." },
d914d1cb 170 { "OSMSG_SHUN_COUNT", "There are %d shuns active on the network." },
d76ed9a9 171 { "OSMSG_LINKS_SERVER", "%s%s (%u clients; %s)" },
172 { "OSMSG_MAX_CLIENTS", "Max clients: %d at %s" },
173 { "OSMSG_NETWORK_INFO", "Total users: %d (%d invisible, %d opers)" },
174 { "OSMSG_RESERVED_LIST", "List of reserved nicks:" },
de9510bc 175 { "OSMSG_TRUSTED_LIST", "$bTrusted Hosts$b" },
176 { "OSMSG_TRUSTED_LIST_HEADER", "IP Address Limit By Time" },
177 { "OSMSG_HOST_IS_TRUSTED", "%-15s %-5s %-10s set %s ago, expires %s" },
178 { "OSMSG_HOST_IS_TRUSTED_DESC", " Reason: %s" },
179 { "OSMSG_TRUSTED_LIST_BAR", "----------------------------------------" },
180 { "OSMSG_TRUSTED_LIST_END", "----------End of Trusted Hosts----------" },
d76ed9a9 181 { "OSMSG_HOST_NOT_TRUSTED", "%s does not have a special trust." },
182 { "OSMSG_UPTIME_STATS", "Uptime: %s (%u lines processed, CPU time %.2fu/%.2fs)" },
183 { "OSMSG_LINE_DUMPED", "Raw line sent." },
184 { "OSMSG_RAW_PARSE_ERROR", "Error parsing raw line (not dumping to uplink)." },
185 { "OSMSG_COLLIDED_NICK", "Now temporarily holding nick $b%s$b." },
186 { "OSMSG_RESERVED_NICK", "Now reserving nick $b%s$b." },
187 { "OSMSG_NICK_UNRESERVED", "Nick $b%s$b is no longer reserved." },
188 { "OSMSG_NOT_RESERVED", "Nick $b%s$b is not reserved." },
189 { "OSMSG_ILLEGAL_REASON", "This channel is illegal." },
190 { "OSMSG_ILLEGAL_KILL_REASON", "Joined an illegal modeless channel - do not repeat." },
191 { "OSMSG_ILLEGAL_CHANNEL", "$b%s$b is an ILLEGAL channel. Do not re-join it." },
192 { "OSMSG_FLOOD_MODERATE", "This channel has been temporarily moderated due to a possible join flood attack detected in this channel; network staff have been notified and will investigate." },
193 { "OSMSG_CLONE_WARNING", "WARNING: You have connected the maximum permitted number of clients from one IP address (clones). If you connect any more, your host will be temporarily banned from the network." },
194 { "OSMSG_CLONE_ADDED", "Added clone $b%s$b." },
195 { "OSMSG_CLONE_FAILED", "Unable to add user $b%s$b." },
196 { "OSMSG_NOT_A_CLONE", "Har har. $b%s$b isn't a clone." },
197 { "OSMSG_CLONE_REMOVED", "Removed clone $b%s$b." },
198 { "OSMSG_CLONE_JOINED", "$b%s$b has joined $b%s$b." },
199 { "OSMSG_CLONE_PARTED", "$b%s$b has left $b%s$b." },
200 { "OSMSG_OPS_GIVEN", "I have given ops in $b%s$b to $b%s$b." },
55342ce8 201 { "OSMSG_HOPS_GIVEN", "I have given halfops in $b%s$b to $b%s$b." },
d76ed9a9 202 { "OSMSG_CLONE_SAID", "$b%s$b has spoken to $b%s$b." },
203 { "OSMSG_UNKNOWN_SUBCOMMAND", "$b%s$b is not a valid subcommand of $b%s$b." },
204 { "OSMSG_UNKNOWN_OPTION", "$b%s$b has not been set." },
205 { "OSMSG_OPTION_IS", "$b%s$b is set to $b%s$b." },
206 { "OSMSG_OPTION_ROOT", "The following keys can be queried:" },
207 { "OSMSG_OPTION_LIST", "$b%s$b contains the following values:" },
208 { "OSMSG_OPTION_KEYS", "$b%s$b contains the following keys:" },
209 { "OSMSG_OPTION_LIST_EMPTY", "Empty list." },
210 { "OSMSG_SET_NOT_SET", "$b%s$b does not exist, and cannot be set." },
211 { "OSMSG_SET_BAD_TYPE", "$b%s$b is not a string, and cannot be set." },
212 { "OSMSG_SET_SUCCESS", "$b%s$b has been set to $b%s$b." },
213 { "OSMSG_SETTIME_SUCCESS", "Set time for servers named like $b%s$b." },
214 { "OSMSG_BAD_ACTION", "Unrecognized trace action $b%s$b." },
215 { "OSMSG_USER_SEARCH_RESULTS", "The following users were found:" },
de9510bc 216 { "OSMSG_USER_SEARCH_HEADER", "Nick User@Host (Account)" },
217 { "OSMSG_USER_SEARCH_BAR", "-------------------------------------------" },
8e11460f 218 { "OSMSG_USER_SEARCH_COUNT", "There were %4u matches" },
219 { "OSMSG_USER_SEARCH_COUNT_BAR", "------------ Found %4u matches -----------" },
d76ed9a9 220 { "OSMSG_CHANNEL_SEARCH_RESULTS", "The following channels were found:" },
221 { "OSMSG_GLINE_SEARCH_RESULTS", "The following glines were found:" },
d914d1cb 222 { "OSMSG_SHUN_SEARCH_RESULTS", "The following shun were found:" },
d76ed9a9 223 { "OSMSG_LOG_SEARCH_RESULTS", "The following log entries were found:" },
224 { "OSMSG_GSYNC_RUNNING", "Synchronizing glines from %s." },
d914d1cb 225 { "OSMSG_SSYNC_RUNNING", "Synchronizing shuns from %s." },
d76ed9a9 226 { "OSMSG_GTRACE_FORMAT", "%s (issued %s by %s, expires %s): %s" },
d914d1cb 227 { "OSMSG_STRACE_FORMAT", "%s (issued %s by %s, expires %s): %s" },
d76ed9a9 228 { "OSMSG_GAG_APPLIED", "Gagged $b%s$b, affecting %d users." },
229 { "OSMSG_GAG_ADDED", "Gagged $b%s$b." },
230 { "OSMSG_REDUNDANT_GAG", "Gag $b%s$b is redundant." },
231 { "OSMSG_GAG_NOT_FOUND", "Could not find gag $b%s$b." },
232 { "OSMSG_NO_GAGS", "No gags have been set." },
233 { "OSMSG_UNGAG_APPLIED", "Ungagged $b%s$b, affecting %d users." },
234 { "OSMSG_UNGAG_ADDED", "Ungagged $b%s$b." },
235 { "OSMSG_TIMEQ_INFO", "%u events in timeq; next in %lu seconds." },
236 { "OSMSG_ALERT_EXISTS", "An alert named $b%s$b already exists." },
237 { "OSMSG_UNKNOWN_REACTION", "Unknown alert reaction $b%s$b." },
238 { "OSMSG_ADDED_ALERT", "Added alert named $b%s$b." },
239 { "OSMSG_REMOVED_ALERT", "Removed alert named $b%s$b." },
240 { "OSMSG_NO_SUCH_ALERT", "No alert named $b%s$b could be found." },
de9510bc 241 { "OSMSG_ALERTS_LIST", "$bCurrent $O alerts$b" },
242 { "OSMSG_ALERTS_BAR", "----------------------------------------------" },
243 { "OSMSG_ALERTS_HEADER", "Name Action (by Oper)" },
244 { "OSMSG_ALERTS_DESC", " Criteria: %s" },
245 { "OSMSG_ALERT_IS", "$b%-20s$b %-6s (by %s)" },
246 { "OSMSG_ALERT_END", "----------------End of Alerts-----------------" },
d76ed9a9 247 { "OSMSG_REHASH_COMPLETE", "Completed rehash of configuration database." },
248 { "OSMSG_REHASH_FAILED", "Rehash of configuration database failed, previous configuration is intact." },
249 { "OSMSG_REOPEN_COMPLETE", "Closed and reopened all log files." },
250 { "OSMSG_RECONNECTING", "Reconnecting to my uplink." },
251 { "OSMSG_NUMERIC_COLLIDE", "Numeric %d (%s) is already in use." },
252 { "OSMSG_NAME_COLLIDE", "That name is already in use." },
253 { "OSMSG_SRV_CREATE_FAILED", "Server creation failed -- check log files." },
254 { "OSMSG_SERVER_JUPED", "Added new jupe server %s." },
255 { "OSMSG_SERVER_NOT_JUPE", "That server is not a juped server." },
256 { "OSMSG_SERVER_UNJUPED", "Server jupe removed." },
c52666c6 257 /*
d76ed9a9 258 { "OSMSG_WARN_ADDED", "Added channel activity warning for $b%s$b (%s)" },
259 { "OSMSG_WARN_EXISTS", "Channel activity warning for $b%s$b already exists." },
260 { "OSMSG_WARN_DELETED", "Removed channel activity warning for $b%s$b" },
261 { "OSMSG_WARN_NOEXIST", "Channel activity warning for $b%s$b does not exist." },
262 { "OSMSG_WARN_LISTSTART", "Channel activity warnings:" },
263 { "OSMSG_WARN_LISTENTRY", "%s (%s)" },
264 { "OSMSG_WARN_LISTEND", "End of activity warning list." },
c52666c6 265 */
d76ed9a9 266 { "OSMSG_UPLINK_CONNECTING", "Establishing connection with %s (%s:%d)." },
267 { "OSMSG_CURRENT_UPLINK", "$b%s$b is already the current uplink." },
268 { "OSMSG_INVALID_UPLINK", "$b%s$b is not a valid uplink name." },
269 { "OSMSG_UPLINK_DISABLED", "$b%s$b is a disabled or unavailable uplink." },
270 { "OSMSG_UPLINK_START", "Uplink $b%s$b:" },
271 { "OSMSG_UPLINK_ADDRESS", "Address: %s:%d" },
272 { "OSMSG_STUPID_GLINE", "Gline %s? Now $bthat$b would be smooth." },
d914d1cb 273 { "OSMSG_STUPID_SHUN", "Shun %s? Now $bthat$b would be smooth." },
d76ed9a9 274 { "OSMSG_ACCOUNTMASK_AUTHED", "Invalid criteria: it is impossible to match an account mask but not be authed" },
275 { "OSMSG_CHANINFO_HEADER", "%s Information" },
276 { "OSMSG_CHANINFO_TIMESTAMP", "Created on: %a %b %d %H:%M:%S %Y (%s)" },
277 { "OSMSG_CHANINFO_MODES", "Modes: %s" },
278 { "OSMSG_CHANINFO_MODES_BADWORD", "Modes: %s; bad-word channel" },
279 { "OSMSG_CHANINFO_TOPIC", "Topic (set by %%s, %a %b %d %H:%M:%S %Y): %%s" },
280 { "OSMSG_CHANINFO_TOPIC_UNKNOWN", "Topic: (none / not gathered)" },
281 { "OSMSG_CHANINFO_BAN_COUNT", "Bans (%d):" },
282 { "OSMSG_CHANINFO_BAN", "%%s by %%s (%a %b %d %H:%M:%S %Y)" },
2aef5f4b 283 { "OSMSG_CHANINFO_EXEMPT_COUNT", "Exempts (%d):" },
284 { "OSMSG_CHANINFO_EXEMPT", "%%s by %%s (%a %b %d %H:%M:%S %Y)" },
d76ed9a9 285 { "OSMSG_CHANINFO_MANY_USERS", "%d users (\"/msg $S %s %s users\" for the list)" },
286 { "OSMSG_CHANINFO_USER_COUNT", "Users (%d):" },
287 { "OSMSG_CSEARCH_CHANNEL_INFO", "%s [%d users] %s %s" },
27eaa617 288 { "OSMSG_INVALID_REGEX", "Invalid regex: %s: %s (%d)" },
d76ed9a9 289 { NULL, NULL }
290};
291
b1bf690d 292#define OPSERV_SYNTAX() svccmd_send_help_brief(user, opserv, cmd)
d76ed9a9 293
294typedef int (*discrim_search_func)(struct userNode *match, void *extra);
295
296struct userNode *opserv;
258d1427 297static struct service *opserv_service;
d76ed9a9 298
de9510bc 299/*static dict_t opserv_chan_warn; */ /* data is char* */
d76ed9a9 300static dict_t opserv_reserved_nick_dict; /* data is struct userNode* */
301static struct string_list *opserv_bad_words;
302static dict_t opserv_exempt_channels; /* data is not used */
303static dict_t opserv_trusted_hosts; /* data is struct trusted_host* */
304static dict_t opserv_hostinfo_dict; /* data is struct opserv_hostinfo* */
305static dict_t opserv_user_alerts; /* data is struct opserv_user_alert* */
306static dict_t opserv_nick_based_alerts; /* data is struct opserv_user_alert* */
307static dict_t opserv_channel_alerts; /* data is struct opserv_user_alert* */
308static struct module *opserv_module;
309static struct log_type *OS_LOG;
310static unsigned int new_user_flood;
311static char *level_strings[1001];
312
313static struct {
314 struct chanNode *debug_channel;
315 struct chanNode *alert_channel;
316 struct chanNode *staff_auth_channel;
317 struct policer_params *join_policer_params;
318 struct policer new_user_policer;
319 unsigned long untrusted_max;
320 unsigned long clone_gline_duration;
321 unsigned long block_gline_duration;
d914d1cb 322 unsigned long block_shun_duration;
d76ed9a9 323 unsigned long purge_lock_delay;
324 unsigned long join_flood_moderate;
325 unsigned long join_flood_moderate_threshold;
5a1daaab 326 unsigned long admin_level;
d76ed9a9 327} opserv_conf;
328
329struct trusted_host {
330 char *ipaddr;
331 char *issuer;
332 char *reason;
333 unsigned long limit;
334 time_t issued;
335 time_t expires;
336};
337
338struct gag_entry {
339 char *mask;
340 char *owner;
341 char *reason;
342 time_t expires;
343 struct gag_entry *next;
344};
345
346static struct gag_entry *gagList;
347
348struct opserv_hostinfo {
349 struct userList clients;
350 struct trusted_host *trusted;
351};
352
353static void
354opserv_free_hostinfo(void *data)
355{
356 struct opserv_hostinfo *ohi = data;
357 userList_clean(&ohi->clients);
358 free(ohi);
359}
360
361typedef struct opservDiscrim {
362 struct chanNode *channel;
2f61d1d7 363 char *mask_nick, *mask_ident, *mask_host, *mask_info, *server, *reason, *accountmask;
364 irc_in_addr_t ip_mask;
365 unsigned long limit;
366 time_t min_ts, max_ts;
27eaa617 367 regex_t regex_nick, regex_ident, regex_host, regex_info;
368 unsigned int has_regex_nick : 1, has_regex_ident : 1, has_regex_host : 1, has_regex_info : 1;
d76ed9a9 369 unsigned int min_level, max_level, domain_depth, duration, min_clones, min_channels, max_channels;
2f61d1d7 370 unsigned char ip_mask_bits;
d76ed9a9 371 unsigned int match_opers : 1, option_log : 1;
372 unsigned int chan_req_modes : 2, chan_no_modes : 2;
373 int authed : 2, info_space : 2;
63665495 374 unsigned int intra_scmp : 2, intra_dcmp : 2;
27eaa617 375 unsigned int use_regex : 1;
d76ed9a9 376} *discrim_t;
377
378struct discrim_and_source {
379 discrim_t discrim;
380 struct userNode *source;
258d1427 381 struct userNode *destination;
d76ed9a9 382 dict_t dict;
383 unsigned int disp_limit;
384};
385
258d1427 386static discrim_t opserv_discrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[], int allow_channel);
d76ed9a9 387static unsigned int opserv_discrim_search(discrim_t discrim, discrim_search_func dsf, void *data);
388static int gag_helper_func(struct userNode *match, void *extra);
389static int ungag_helper_func(struct userNode *match, void *extra);
390
391typedef enum {
392 REACT_NOTICE,
393 REACT_KILL,
9a75756e 394 REACT_SILENT,
d914d1cb 395 REACT_GLINE,
396 REACT_SHUN
d76ed9a9 397} opserv_alert_reaction;
398
399struct opserv_user_alert {
400 char *owner;
401 char *text_discrim, *split_discrim;
402 discrim_t discrim;
403 opserv_alert_reaction reaction;
404};
405
406/* funny type to make it acceptible to dict_set_free_data, far below */
407static void
408opserv_free_user_alert(void *data)
409{
410 struct opserv_user_alert *alert = data;
411 if (alert->discrim->channel)
412 UnlockChannel(alert->discrim->channel);
413 free(alert->owner);
414 free(alert->text_discrim);
415 free(alert->split_discrim);
27eaa617 416 if(alert->discrim->has_regex_nick)
417 regfree(&alert->discrim->regex_nick);
418 if(alert->discrim->has_regex_ident)
419 regfree(&alert->discrim->regex_ident);
420 if(alert->discrim->has_regex_host)
421 regfree(&alert->discrim->regex_host);
422 if(alert->discrim->has_regex_info)
423 regfree(&alert->discrim->regex_info);
d76ed9a9 424 free(alert->discrim->reason);
425 free(alert->discrim);
426 free(alert);
427}
428
429#define opserv_debug(format...) do { if (opserv_conf.debug_channel) send_channel_notice(opserv_conf.debug_channel , opserv , ## format); } while (0)
430#define opserv_alert(format...) do { if (opserv_conf.alert_channel) send_channel_notice(opserv_conf.alert_channel , opserv , ## format); } while (0)
431
432/* A lot of these commands are very similar to what ChanServ can do,
433 * but OpServ can do them even on channels that aren't registered.
434 */
435
436static MODCMD_FUNC(cmd_access)
437{
438 struct handle_info *hi;
439 const char *target;
440 unsigned int res;
441
442 target = (argc > 1) ? (const char*)argv[1] : user->nick;
443 if (!irccasecmp(target, "*")) {
444 nickserv_show_oper_accounts(user, cmd);
445 return 1;
446 }
447 if (!(hi = modcmd_get_handle_info(user, target)))
448 return 0;
258d1427 449 res = (argc > 2) ? oper_try_set_access(user, opserv_service->bot, hi, strtoul(argv[2], NULL, 0)) : 0;
d76ed9a9 450 reply("OSMSG_USER_ACCESS_IS", target, hi->handle, hi->opserv_level);
451 return res;
452}
453
454static MODCMD_FUNC(cmd_ban)
455{
456 struct mod_chanmode change;
457 struct userNode *victim;
458
459 mod_chanmode_init(&change);
460 change.argc = 1;
461 change.args[0].mode = MODE_BAN;
462 if (is_ircmask(argv[1]))
a32da4c7 463 change.args[0].u.hostmask = strdup(argv[1]);
d76ed9a9 464 else if ((victim = GetUserH(argv[1])))
a32da4c7 465 change.args[0].u.hostmask = generate_hostmask(victim, 0);
d76ed9a9 466 else {
258d1427 467 reply("OSMSG_INVALID_IRCMASK", argv[1]);
468 return 0;
d76ed9a9 469 }
470 modcmd_chanmode_announce(&change);
a32da4c7 471 reply("OSMSG_ADDED_BAN", change.args[0].u.hostmask, channel->name);
472 free((char*)change.args[0].u.hostmask);
d76ed9a9 473 return 1;
474}
475
476static MODCMD_FUNC(cmd_chaninfo)
477{
478 char buffer[MAXLEN];
479 const char *fmt;
480 struct banNode *ban;
2aef5f4b 481 struct exemptNode *exempt;
d76ed9a9 482 struct modeNode *moden;
483 unsigned int n;
484
485 reply("OSMSG_CHANINFO_HEADER", channel->name);
486 fmt = user_find_message(user, "OSMSG_CHANINFO_TIMESTAMP");
487 strftime(buffer, sizeof(buffer), fmt, gmtime(&channel->timestamp));
488 send_message_type(4, user, cmd->parent->bot, "%s", buffer);
489 irc_make_chanmode(channel, buffer);
490 if (channel->bad_channel)
491 reply("OSMSG_CHANINFO_MODES_BADWORD", buffer);
492 else
493 reply("OSMSG_CHANINFO_MODES", buffer);
494 if (channel->topic_time) {
495 fmt = user_find_message(user, "OSMSG_CHANINFO_TOPIC");
496 strftime(buffer, sizeof(buffer), fmt, gmtime(&channel->topic_time));
497 send_message_type(4, user, cmd->parent->bot, buffer, channel->topic_nick, channel->topic);
498 } else {
258d1427 499 irc_fetchtopic(cmd->parent->bot, channel->name);
500 reply("OSMSG_CHANINFO_TOPIC_UNKNOWN");
d76ed9a9 501 }
502 if (channel->banlist.used) {
258d1427 503 reply("OSMSG_CHANINFO_BAN_COUNT", channel->banlist.used);
d76ed9a9 504 fmt = user_find_message(user, "OSMSG_CHANINFO_BAN");
258d1427 505 for (n = 0; n < channel->banlist.used; n++) {
506 ban = channel->banlist.list[n];
507 strftime(buffer, sizeof(buffer), fmt, localtime(&ban->set));
508 send_message_type(4, user, cmd->parent->bot, buffer, ban->ban, ban->who);
509 }
d76ed9a9 510 }
2aef5f4b 511 if (channel->exemptlist.used) {
512 reply("OSMSG_CHANINFO_EXEMPT_COUNT", channel->exemptlist.used);
513 fmt = user_find_message(user, "OSMSG_CHANINFO_EXEMPT");
514 for (n = 0; n < channel->exemptlist.used; n++) {
515 exempt = channel->exemptlist.list[n];
516 strftime(buffer, sizeof(buffer), fmt, localtime(&exempt->set));
517 send_message_type(4, user, cmd->parent->bot, buffer, exempt->exempt, exempt->who);
518 }
519 }
d76ed9a9 520 if ((argc < 2) && (channel->members.used >= 50)) {
521 /* early out unless they ask for users */
522 reply("OSMSG_CHANINFO_MANY_USERS", channel->members.used, argv[0], channel->name);
523 return 1;
524 }
525 reply("OSMSG_CHANINFO_USER_COUNT", channel->members.used);
526 for (n=0; n<channel->members.used; n++) {
258d1427 527 moden = channel->members.list[n];
2f61d1d7 528 if (moden->modes & MODE_CHANOP) {
529 if (moden->oplevel >= 0)
530 send_message_type(4, user, cmd->parent->bot, " @%s:%d (%s@%s)", moden->user->nick, moden->oplevel, moden->user->ident, moden->user->hostname);
531 else
532 send_message_type(4, user, cmd->parent->bot, " @%s (%s@%s)", moden->user->nick, moden->user->ident, moden->user->hostname);
533 }
d76ed9a9 534 }
55342ce8 535 for (n=0; n<channel->members.used; n++) {
536 moden = channel->members.list[n];
537 if ((moden->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE)) == MODE_HALFOP)
538 send_message_type(4, user, cmd->parent->bot, " %s%s (%s@%s)", "%", moden->user->nick, moden->user->ident, moden->user->hostname);
539 }
d76ed9a9 540 for (n=0; n<channel->members.used; n++) {
258d1427 541 moden = channel->members.list[n];
542 if ((moden->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE)) == MODE_VOICE)
d76ed9a9 543 send_message_type(4, user, cmd->parent->bot, " +%s (%s@%s)", moden->user->nick, moden->user->ident, moden->user->hostname);
544 }
545 for (n=0; n<channel->members.used; n++) {
258d1427 546 moden = channel->members.list[n];
547 if ((moden->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE)) == 0)
d76ed9a9 548 send_message_type(4, user, cmd->parent->bot, " %s (%s@%s)", moden->user->nick, moden->user->ident, moden->user->hostname);
549 }
550 return 1;
551}
552
de9510bc 553/* This command has been replaced by 'alert notice channel #foo' */
554/*
2f61d1d7 555static MODCMD_FUNC(cmd_warn)
d76ed9a9 556{
557 char *reason, *message;
558
559 if (!IsChannelName(argv[1])) {
258d1427 560 reply("OSMSG_NEED_CHANNEL", argv[0]);
561 return 0;
d76ed9a9 562 }
563 reason = dict_find(opserv_chan_warn, argv[1], NULL);
564 if (reason) {
565 reply("OSMSG_WARN_EXISTS", argv[1]);
566 return 0;
567 }
568 if (argv[2])
569 reason = strdup(unsplit_string(argv+2, argc-2, NULL));
570 else
571 reason = strdup("No reason");
572 dict_insert(opserv_chan_warn, strdup(argv[1]), reason);
573 reply("OSMSG_WARN_ADDED", argv[1], reason);
574 if (dict_find(channels, argv[1], NULL)) {
575 message = alloca(strlen(reason) + strlen(argv[1]) + 55);
576 sprintf(message, "Channel activity warning for channel %s: %s", argv[1], reason);
577 global_message(MESSAGE_RECIPIENT_OPERS, message);
578 }
579 return 1;
580}
581
582static MODCMD_FUNC(cmd_unwarn)
583{
584 if ((argc < 2) || !IsChannelName(argv[1])) {
585 reply("OSMSG_NEED_CHANNEL", argv[0]);
258d1427 586 return 0;
d76ed9a9 587 }
588 if (!dict_remove(opserv_chan_warn, argv[1])) {
589 reply("OSMSG_WARN_NOEXIST", argv[1]);
590 return 0;
591 }
592 reply("OSMSG_WARN_DELETED", argv[1]);
593 return 1;
594}
de9510bc 595*/
d76ed9a9 596
597static MODCMD_FUNC(cmd_clearbans)
598{
599 struct mod_chanmode *change;
600 unsigned int ii;
601
602 change = mod_chanmode_alloc(channel->banlist.used);
603 for (ii=0; ii<channel->banlist.used; ii++) {
604 change->args[ii].mode = MODE_REMOVE | MODE_BAN;
ec1a68c8 605 change->args[ii].u.hostmask = strdup(channel->banlist.list[ii]->ban);
d76ed9a9 606 }
607 modcmd_chanmode_announce(change);
ec1a68c8 608 for (ii=0; ii<change->argc; ++ii)
609 free((char*)change->args[ii].u.hostmask);
d76ed9a9 610 mod_chanmode_free(change);
611 reply("OSMSG_CLEARBANS_DONE", channel->name);
612 return 1;
613}
614
615static MODCMD_FUNC(cmd_clearmodes)
616{
617 struct mod_chanmode change;
618
619 if (!channel->modes) {
258d1427 620 reply("OSMSG_NO_CHANNEL_MODES", channel->name);
d76ed9a9 621 return 0;
622 }
623 mod_chanmode_init(&change);
624 change.modes_clear = channel->modes;
625 modcmd_chanmode_announce(&change);
626 reply("OSMSG_CLEARMODES_DONE", channel->name);
627 return 1;
628}
629
630static MODCMD_FUNC(cmd_deop)
631{
632 struct mod_chanmode *change;
633 unsigned int arg, count;
634
635 change = mod_chanmode_alloc(argc-1);
636 for (arg = 1, count = 0; arg < argc; ++arg) {
637 struct userNode *victim = GetUserH(argv[arg]);
638 struct modeNode *mn;
258d1427 639 if (!victim || IsService(victim)
d76ed9a9 640 || !(mn = GetUserMode(channel, victim))
641 || !(mn->modes & MODE_CHANOP))
642 continue;
643 change->args[count].mode = MODE_REMOVE | MODE_CHANOP;
a32da4c7 644 change->args[count++].u.member = mn;
d76ed9a9 645 }
646 if (count) {
647 change->argc = count;
648 modcmd_chanmode_announce(change);
649 }
650 mod_chanmode_free(change);
651 reply("OSMSG_DEOP_DONE");
652 return 1;
653}
654
55342ce8 655static MODCMD_FUNC(cmd_dehop)
656{
657 struct mod_chanmode *change;
658 unsigned int arg, count;
659
660 change = mod_chanmode_alloc(argc-1);
661 for (arg = 1, count = 0; arg < argc; ++arg) {
662 struct userNode *victim = GetUserH(argv[arg]);
663 struct modeNode *mn;
664 if (!victim || IsService(victim)
665 || !(mn = GetUserMode(channel, victim))
666 || !(mn->modes & MODE_HALFOP))
667 continue;
668 change->args[count].mode = MODE_REMOVE | MODE_HALFOP;
11408ce4 669 change->args[count++].u.member = mn;
55342ce8 670 }
671 if (count) {
672 change->argc = count;
673 modcmd_chanmode_announce(change);
674 }
675 mod_chanmode_free(change);
676 reply("OSMSG_DEHOP_DONE");
677 return 1;
678}
679
d76ed9a9 680static MODCMD_FUNC(cmd_deopall)
681{
682 struct mod_chanmode *change;
683 unsigned int ii, count;
684
685 change = mod_chanmode_alloc(channel->members.used);
686 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 687 struct modeNode *mn = channel->members.list[ii];
688 if (IsService(mn->user) || !(mn->modes & MODE_CHANOP))
d76ed9a9 689 continue;
690 change->args[count].mode = MODE_REMOVE | MODE_CHANOP;
a32da4c7 691 change->args[count++].u.member = mn;
d76ed9a9 692 }
693 if (count) {
694 change->argc = count;
695 modcmd_chanmode_announce(change);
696 }
697 mod_chanmode_free(change);
698 reply("OSMSG_DEOPALL_DONE", channel->name);
699 return 1;
700}
701
55342ce8 702static MODCMD_FUNC(cmd_dehopall)
703{
704 struct mod_chanmode *change;
705 unsigned int ii, count;
706
707 change = mod_chanmode_alloc(channel->members.used);
708 for (ii = count = 0; ii < channel->members.used; ++ii) {
709 struct modeNode *mn = channel->members.list[ii];
710 if (IsService(mn->user) || !(mn->modes & MODE_HALFOP))
711 continue;
712 change->args[count].mode = MODE_REMOVE | MODE_HALFOP;
11408ce4 713 change->args[count++].u.member = mn;
55342ce8 714 }
715 if (count) {
716 change->argc = count;
717 modcmd_chanmode_announce(change);
718 }
719 mod_chanmode_free(change);
720 reply("OSMSG_DEHOPALL_DONE", channel->name);
721 return 1;
722}
723
d76ed9a9 724static MODCMD_FUNC(cmd_rehash)
725{
726 extern char *services_config;
727
728 if (conf_read(services_config))
258d1427 729 reply("OSMSG_REHASH_COMPLETE");
d76ed9a9 730 else
258d1427 731 reply("OSMSG_REHASH_FAILED");
d76ed9a9 732 return 1;
733}
734
735static MODCMD_FUNC(cmd_reopen)
736{
737 log_reopen();
738 reply("OSMSG_REOPEN_COMPLETE");
739 return 1;
740}
741
742static MODCMD_FUNC(cmd_reconnect)
743{
744 reply("OSMSG_RECONNECTING");
745 irc_squit(self, "Reconnecting.", NULL);
746 return 1;
747}
748
749static MODCMD_FUNC(cmd_jupe)
750{
751 extern int force_n2k;
752 struct server *newsrv;
753 unsigned int num;
754 char numeric[COMBO_NUMERIC_LEN+1], srvdesc[SERVERDESCRIPTMAX+1];
755
756 num = atoi(argv[2]);
757 if ((num < 64) && !force_n2k) {
758 inttobase64(numeric, num, 1);
759 inttobase64(numeric+1, 64*64-1, 2);
760 } else {
761 inttobase64(numeric, num, 2);
762 inttobase64(numeric+2, 64*64*64-1, 3);
763 }
764#ifdef WITH_PROTOCOL_P10
765 if (GetServerN(numeric)) {
766 reply("OSMSG_NUMERIC_COLLIDE", num, numeric);
767 return 0;
768 }
769#endif
770 if (GetServerH(argv[1])) {
771 reply("OSMSG_NAME_COLLIDE");
772 return 0;
773 }
774 snprintf(srvdesc, sizeof(srvdesc), "JUPE %s", unsplit_string(argv+3, argc-3, NULL));
775 newsrv = AddServer(self, argv[1], 1, now, now, numeric, srvdesc);
776 if (!newsrv) {
777 reply("OSMSG_SRV_CREATE_FAILED");
778 return 0;
779 }
780 irc_server(newsrv);
781 reply("OSMSG_SERVER_JUPED", argv[1]);
782 return 1;
783}
784
785static MODCMD_FUNC(cmd_unjupe)
786{
787 struct server *srv;
788 char *reason;
789
790 srv = GetServerH(argv[1]);
791 if (!srv) {
792 reply("MSG_SERVER_UNKNOWN", argv[1]);
793 return 0;
794 }
795 if (strncmp(srv->description, "JUPE", 4)) {
796 reply("OSMSG_SERVER_NOT_JUPE");
797 return 0;
798 }
799 reason = (argc > 2) ? unsplit_string(argv+2, argc-2, NULL) : "Unjuping server";
800 DelServer(srv, 1, reason);
801 reply("OSMSG_SERVER_UNJUPED");
802 return 1;
803}
804
805static MODCMD_FUNC(cmd_jump)
806{
807 extern struct cManagerNode cManager;
808 void uplink_select(char *name);
809 struct uplinkNode *uplink_find(char *name);
810 struct uplinkNode *uplink;
811 char *target;
812
813 target = unsplit_string(argv+1, argc-1, NULL);
814
815 if (!strcmp(cManager.uplink->name, target)) {
258d1427 816 reply("OSMSG_CURRENT_UPLINK", cManager.uplink->name);
817 return 0;
d76ed9a9 818 }
819
820 uplink = uplink_find(target);
821 if (!uplink) {
258d1427 822 reply("OSMSG_INVALID_UPLINK", target);
823 return 0;
d76ed9a9 824 }
825 if (uplink->flags & UPLINK_UNAVAILABLE) {
826 reply("OSMSG_UPLINK_DISABLED", uplink->name);
827 return 0;
828 }
829
830 reply("OSMSG_UPLINK_CONNECTING", uplink->name, uplink->host, uplink->port);
831 uplink_select(target);
832 irc_squit(self, "Reconnecting.", NULL);
833 return 1;
834}
835
836static MODCMD_FUNC(cmd_die)
837{
838 char *reason, *text;
839
840 text = unsplit_string(argv+1, argc-1, NULL);
841 reason = alloca(strlen(text) + strlen(user->nick) + 20);
842 sprintf(reason, "Disconnected by %s [%s]", user->nick, text);
843 irc_squit(self, reason, text);
844 quit_services = 1;
845 return 1;
846}
847
848static MODCMD_FUNC(cmd_restart)
849{
850 extern int services_argc;
851 extern char **services_argv;
852 char **restart_argv, *reason, *text;
853
854 text = unsplit_string(argv+1, argc-1, NULL);
855 reason = alloca(strlen(text) + strlen(user->nick) + 17);
856 sprintf(reason, "Restarted by %s [%s]", user->nick, text);
857 irc_squit(self, reason, text);
858
859 /* Append a NULL to the end of argv[]. */
860 restart_argv = (char **)alloca((services_argc + 1) * sizeof(char *));
861 memcpy(restart_argv, services_argv, services_argc * sizeof(char *));
862 restart_argv[services_argc] = NULL;
863
864 call_exit_funcs();
865
866 /* Don't blink. */
867 execv(services_argv[0], restart_argv);
868
869 /* If we're still here, that means something went wrong. Reconnect. */
870 return 1;
871}
872
873static struct gline *
9a75756e 874opserv_block(struct userNode *target, char *src_handle, char *reason, unsigned long duration, int silent)
d76ed9a9 875{
2f61d1d7 876 char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
877 irc_ntop(mask + 2, sizeof(mask) - 2, &target->ip);
878 if (!reason)
879 snprintf(reason = alloca(MAXLEN), MAXLEN,
880 "G-line requested by %s.", src_handle);
881 if (!duration)
882 duration = opserv_conf.block_gline_duration;
9a75756e 883 return gline_add(src_handle, mask, duration, reason, now, 1, silent ? 1 : 0);
d76ed9a9 884}
885
886static MODCMD_FUNC(cmd_block)
887{
888 struct userNode *target;
889 struct gline *gline;
890 char *reason;
891
892 target = GetUserH(argv[1]);
893 if (!target) {
258d1427 894 reply("MSG_NICK_UNKNOWN", argv[1]);
895 return 0;
d76ed9a9 896 }
897 if (IsService(target)) {
258d1427 898 reply("MSG_SERVICE_IMMUNE", target->nick);
899 return 0;
d76ed9a9 900 }
901 reason = (argc > 2) ? unsplit_string(argv+2, argc-2, NULL) : NULL;
9a75756e 902 gline = opserv_block(target, user->handle_info->handle, reason, 0, 0);
d76ed9a9 903 reply("OSMSG_GLINE_ISSUED", gline->target);
904 return 1;
905}
906
907static MODCMD_FUNC(cmd_gline)
908{
909 unsigned long duration;
910 char *reason;
911 struct gline *gline;
912
913 reason = unsplit_string(argv+3, argc-3, NULL);
914 if (!is_gline(argv[1]) && !IsChannelName(argv[1]) && (argv[1][0] != '&')) {
258d1427 915 reply("MSG_INVALID_GLINE", argv[1]);
916 return 0;
d76ed9a9 917 }
918 if (!argv[1][strspn(argv[1], "#&*?@.")] && (strlen(argv[1]) < 10)) {
919 reply("OSMSG_STUPID_GLINE", argv[1]);
920 return 0;
921 }
922 duration = ParseInterval(argv[2]);
923 if (!duration) {
924 reply("MSG_INVALID_DURATION", argv[2]);
925 return 0;
926 }
9a75756e 927 gline = gline_add(user->handle_info->handle, argv[1], duration, reason, now, 1, 0);
d76ed9a9 928 reply("OSMSG_GLINE_ISSUED", gline->target);
929 return 1;
930}
931
932static MODCMD_FUNC(cmd_ungline)
933{
934 if (gline_remove(argv[1], 1))
935 reply("OSMSG_GLINE_REMOVED", argv[1]);
936 else
937 reply("OSMSG_GLINE_FORCE_REMOVED", argv[1]);
938 return 1;
939}
940
941static MODCMD_FUNC(cmd_refreshg)
942{
943 if (argc > 1) {
944 unsigned int count;
945 dict_iterator_t it;
946 struct server *srv;
947
948 for (it=dict_first(servers), count=0; it; it=iter_next(it)) {
949 srv = iter_data(it);
950 if ((srv == self) || !match_ircglob(srv->name, argv[1]))
951 continue;
952 gline_refresh_server(srv);
953 reply("OSMSG_GLINES_ONE_REFRESHED", srv->name);
954 count++;
955 }
956 if (!count) {
957 reply("MSG_SERVER_UNKNOWN", argv[1]);
958 return 0;
959 }
960 } else {
961 gline_refresh_all();
962 reply("OSMSG_GLINES_REFRESHED");
963 }
964 return 1;
965}
966
d914d1cb 967static struct shun *
968opserv_shun(struct userNode *target, char *src_handle, char *reason, unsigned long duration)
969{
970 char *mask;
971 mask = alloca(MAXLEN);
972 snprintf(mask, MAXLEN, "*@%s", target->hostname);
973 if (!reason) {
974 reason = alloca(MAXLEN);
975 snprintf(reason, MAXLEN, "Shun requested by %s.", src_handle);
976 }
977 if (!duration) duration = opserv_conf.block_shun_duration;
978 return shun_add(src_handle, mask, duration, reason, now, 1);
979}
980
981static MODCMD_FUNC(cmd_sblock)
982{
983 struct userNode *target;
984 struct shun *shun;
985 char *reason;
986
987 target = GetUserH(argv[1]);
988 if (!target) {
989 reply("MSG_NICK_UNKNOWN", argv[1]);
990 return 0;
991 }
992 if (IsService(target)) {
993 reply("MSG_SERVICE_IMMUNE", target->nick);
994 return 0;
995 }
996 reason = (argc > 2) ? unsplit_string(argv+2, argc-2, NULL) : NULL;
997 shun = opserv_shun(target, user->handle_info->handle, reason, 0);
998 reply("OSMSG_SHUN_ISSUED", shun->target);
999 return 1;
1000}
1001
1002static MODCMD_FUNC(cmd_shun)
1003{
1004 unsigned long duration;
1005 char *reason;
1006 struct shun *shun;
1007
1008 reason = unsplit_string(argv+3, argc-3, NULL);
1009 if (!is_shun(argv[1]) && !IsChannelName(argv[1]) && (argv[1][0] != '&')) {
1010 reply("MSG_INVALID_SHUN", argv[1]);
1011 return 0;
1012 }
1013 if (!argv[1][strspn(argv[1], "#&*?@.")] && (strlen(argv[1]) < 10)) {
1014 reply("OSMSG_STUPID_SHUN", argv[1]);
1015 return 0;
1016 }
1017 duration = ParseInterval(argv[2]);
1018 if (!duration) {
1019 reply("MSG_INVALID_DURATION", argv[2]);
1020 return 0;
1021 }
1022 shun = shun_add(user->handle_info->handle, argv[1], duration, reason, now, 1);
1023 reply("OSMSG_SHUN_ISSUED", shun->target);
1024 return 1;
1025}
1026
1027static MODCMD_FUNC(cmd_unshun)
1028{
1029 if (shun_remove(argv[1], 1))
1030 reply("OSMSG_SHUN_REMOVED", argv[1]);
1031 else
1032 reply("OSMSG_SHUN_FORCE_REMOVED", argv[1]);
1033 return 1;
1034}
1035
1036static MODCMD_FUNC(cmd_refreshs)
1037{
1038 if (argc > 1) {
1039 unsigned int count;
1040 dict_iterator_t it;
1041 struct server *srv;
1042
1043 for (it=dict_first(servers), count=0; it; it=iter_next(it)) {
1044 srv = iter_data(it);
1045 if ((srv == self) || !match_ircglob(srv->name, argv[1]))
1046 continue;
1047 shun_refresh_server(srv);
1048 reply("OSMSG_SHUNS_ONE_REFRESHED", srv->name);
1049 count++;
1050 }
1051 if (!count) {
1052 reply("MSG_SERVER_UNKNOWN", argv[1]);
1053 return 0;
1054 }
1055 } else {
1056 shun_refresh_all();
1057 reply("OSMSG_SHUNS_REFRESHED");
1058 }
1059 return 1;
1060}
1061
d76ed9a9 1062static void
1063opserv_ison(struct userNode *tell, struct userNode *target, const char *message)
1064{
1065 struct modeNode *mn;
1066 unsigned int count, here_len, n, maxlen;
1067 char buff[MAXLEN];
1068
1069 maxlen = tell->handle_info ? tell->handle_info->screen_width : 0;
1070 if (!maxlen)
1071 maxlen = MAX_LINE_SIZE;
1072 for (n=count=0; n<target->channels.used; n++) {
258d1427 1073 mn = target->channels.list[n];
1074 here_len = strlen(mn->channel->name);
1075 if ((count + here_len + 4) > maxlen) {
1076 buff[count] = 0;
d76ed9a9 1077 send_message(tell, opserv, message, buff);
258d1427 1078 count = 0;
1079 }
1080 if (mn->modes & MODE_CHANOP)
d76ed9a9 1081 buff[count++] = '@';
55342ce8 1082 if (mn->modes & MODE_HALFOP)
1083 buff[count++] = '%';
258d1427 1084 if (mn->modes & MODE_VOICE)
d76ed9a9 1085 buff[count++] = '+';
258d1427 1086 memcpy(buff+count, mn->channel->name, here_len);
1087 count += here_len;
1088 buff[count++] = ' ';
d76ed9a9 1089 }
1090 if (count) {
258d1427 1091 buff[count] = 0;
1092 send_message(tell, opserv, message, buff);
d76ed9a9 1093 }
1094}
1095
1096static MODCMD_FUNC(cmd_inviteme)
1097{
1098 struct userNode *target;
1099
1100 if (argc < 2) {
258d1427 1101 target = user;
d76ed9a9 1102 } else {
258d1427 1103 target = GetUserH(argv[1]);
1104 if (!target) {
1105 reply("MSG_NICK_UNKNOWN", argv[1]);
1106 return 0;
1107 }
d76ed9a9 1108 }
1109 if (opserv_conf.debug_channel == NULL) {
258d1427 1110 reply("OSMSG_NO_DEBUG_CHANNEL");
1111 return 0;
d76ed9a9 1112 }
1113 if (GetUserMode(opserv_conf.debug_channel, user)) {
a32da4c7 1114 reply("OSMSG_ALREADY_THERE", opserv_conf.debug_channel->name);
d76ed9a9 1115 return 0;
1116 }
1117 irc_invite(cmd->parent->bot, target, opserv_conf.debug_channel);
1118 if (target != user)
258d1427 1119 reply("OSMSG_INVITE_DONE", target->nick, opserv_conf.debug_channel->name);
d76ed9a9 1120 return 1;
1121}
1122
1123static MODCMD_FUNC(cmd_invite)
1124{
1125 if (GetUserMode(channel, user)) {
1126 reply("OSMSG_ALREADY_THERE", channel->name);
1127 return 0;
1128 }
1129 irc_invite(cmd->parent->bot, user, channel);
1130 return 1;
1131}
1132
1133static MODCMD_FUNC(cmd_join)
1134{
1135 struct userNode *bot = cmd->parent->bot;
1136
1137 if (!IsChannelName(argv[1])) {
1138 reply("MSG_NOT_CHANNEL_NAME");
1139 return 0;
1140 } else if (!(channel = GetChannel(argv[1]))) {
2aef5f4b 1141 channel = AddChannel(argv[1], now, NULL, NULL, NULL);
d76ed9a9 1142 AddChannelUser(bot, channel)->modes |= MODE_CHANOP;
1143 } else if (GetUserMode(channel, bot)) {
1144 reply("OSMSG_ALREADY_JOINED", channel->name);
1145 return 0;
1146 } else {
1147 struct mod_chanmode change;
1148 mod_chanmode_init(&change);
1149 change.argc = 1;
1150 change.args[0].mode = MODE_CHANOP;
a32da4c7 1151 change.args[0].u.member = AddChannelUser(bot, channel);
d76ed9a9 1152 modcmd_chanmode_announce(&change);
1153 }
1154 irc_fetchtopic(bot, channel->name);
1155 reply("OSMSG_JOIN_DONE", channel->name);
1156 return 1;
1157}
1158
1159static MODCMD_FUNC(cmd_kick)
1160{
1161 struct userNode *target;
1162 char *reason;
1163
1164 if (argc < 3) {
258d1427 1165 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1166 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1167 } else {
258d1427 1168 reason = unsplit_string(argv+2, argc-2, NULL);
d76ed9a9 1169 }
1170 target = GetUserH(argv[1]);
1171 if (!target) {
258d1427 1172 reply("MSG_NICK_UNKNOWN", argv[1]);
1173 return 0;
d76ed9a9 1174 }
1175 if (!GetUserMode(channel, target)) {
258d1427 1176 reply("OSMSG_NOT_ON_CHANNEL", target->nick, channel->name);
1177 return 0;
d76ed9a9 1178 }
1179 KickChannelUser(target, channel, cmd->parent->bot, reason);
1180 return 1;
1181}
1182
1183static MODCMD_FUNC(cmd_kickall)
1184{
1185 unsigned int limit, n, inchan;
1186 struct modeNode *mn;
1187 char *reason;
1188 struct userNode *bot = cmd->parent->bot;
1189
1190 /* ircu doesn't let servers KICK users, so if OpServ's not in the
1191 * channel, we have to join it in temporarily. */
1192 if (!(inchan = GetUserMode(channel, bot) ? 1 : 0)) {
1193 struct mod_chanmode change;
1194 mod_chanmode_init(&change);
1195 change.args[0].mode = MODE_CHANOP;
a32da4c7 1196 change.args[0].u.member = AddChannelUser(bot, channel);
d76ed9a9 1197 modcmd_chanmode_announce(&change);
1198 }
1199 if (argc < 2) {
258d1427 1200 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1201 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1202 } else {
258d1427 1203 reason = unsplit_string(argv+1, argc-1, NULL);
d76ed9a9 1204 }
1205 limit = user->handle_info->opserv_level;
1206 for (n=channel->members.used; n>0;) {
258d1427 1207 mn = channel->members.list[--n];
1208 if (IsService(mn->user)
1209 || (mn->user->handle_info
1210 && (mn->user->handle_info->opserv_level >= limit))) {
1211 continue;
1212 }
1213 KickChannelUser(mn->user, channel, bot, reason);
d76ed9a9 1214 }
1215 if (!inchan)
1216 DelChannelUser(bot, channel, "My work here is done", 0);
1217 reply("OSMSG_KICKALL_DONE", channel->name);
1218 return 1;
1219}
1220
1221static MODCMD_FUNC(cmd_kickban)
1222{
1223 struct mod_chanmode change;
1224 struct userNode *target;
1225 char *reason;
1226 char *mask;
1227
1228 if (argc == 2) {
258d1427 1229 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1230 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1231 } else {
258d1427 1232 reason = unsplit_string(argv+2, argc-2, NULL);
d76ed9a9 1233 }
1234 target = GetUserH(argv[1]);
1235 if (!target) {
258d1427 1236 reply("MSG_NICK_UNKNOWN", argv[1]);
1237 return 0;
d76ed9a9 1238 }
1239 if (!GetUserMode(channel, target)) {
258d1427 1240 reply("OSMSG_NOT_ON_CHANNEL", target->nick, channel->name);
1241 return 0;
d76ed9a9 1242 }
1243 mod_chanmode_init(&change);
1244 change.argc = 1;
1245 change.args[0].mode = MODE_BAN;
a32da4c7 1246 change.args[0].u.hostmask = mask = generate_hostmask(target, 0);
d76ed9a9 1247 modcmd_chanmode_announce(&change);
1248 KickChannelUser(target, channel, cmd->parent->bot, reason);
1249 free(mask);
1250 return 1;
1251}
1252
1253static MODCMD_FUNC(cmd_kickbanall)
1254{
1255 struct modeNode *mn;
1256 struct userNode *bot = cmd->parent->bot;
1257 struct mod_chanmode *change;
1258 char *reason;
1259 unsigned int limit, n, inchan;
1260
1261 /* ircu doesn't let servers KICK users, so if OpServ's not in the
1262 * channel, we have to join it in temporarily. */
1263 if (!(inchan = GetUserMode(channel, bot) ? 1 : 0)) {
1264 change = mod_chanmode_alloc(2);
1265 change->args[0].mode = MODE_CHANOP;
a32da4c7 1266 change->args[0].u.member = AddChannelUser(bot, channel);
d76ed9a9 1267 change->args[1].mode = MODE_BAN;
a32da4c7 1268 change->args[1].u.hostmask = "*!*@*";
d76ed9a9 1269 } else {
1270 change = mod_chanmode_alloc(1);
1271 change->args[0].mode = MODE_BAN;
a32da4c7 1272 change->args[0].u.hostmask = "*!*@*";
d76ed9a9 1273 }
1274 modcmd_chanmode_announce(change);
a32da4c7 1275 mod_chanmode_free(change);
d76ed9a9 1276 if (argc < 2) {
258d1427 1277 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1278 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1279 } else {
258d1427 1280 reason = unsplit_string(argv+1, argc-1, NULL);
d76ed9a9 1281 }
1282 /* now kick them */
1283 limit = user->handle_info->opserv_level;
1284 for (n=channel->members.used; n>0; ) {
258d1427 1285 mn = channel->members.list[--n];
1286 if (IsService(mn->user)
1287 || (mn->user->handle_info
1288 && (mn->user->handle_info->opserv_level >= limit))) {
1289 continue;
1290 }
1291 KickChannelUser(mn->user, channel, bot, reason);
d76ed9a9 1292 }
1293 if (!inchan)
1294 DelChannelUser(bot, channel, "My work here is done", 0);
1295 reply("OSMSG_KICKALL_DONE", channel->name);
1296 return 1;
1297}
1298
1299static MODCMD_FUNC(cmd_part)
1300{
1301 char *reason;
1302
1303 if (!IsChannelName(argv[1])) {
1304 reply("MSG_NOT_CHANNEL_NAME");
1305 return 0;
1306 }
1307 if ((channel = GetChannel(argv[1]))) {
1308 if (!GetUserMode(channel, cmd->parent->bot)) {
1309 reply("OSMSG_NOT_ON_CHANNEL", cmd->parent->bot->nick, channel->name);
1310 return 0;
1311 }
1312 reason = (argc < 3) ? "Leaving." : unsplit_string(argv+2, argc-2, NULL);
1313 reply("OSMSG_LEAVING", channel->name);
1314 DelChannelUser(cmd->parent->bot, channel, reason, 0);
1315 }
1316 return 1;
1317}
1318
1319static MODCMD_FUNC(cmd_mode)
1320{
1321 if (!modcmd_chanmode(argv+1, argc-1, MCP_ALLOW_OVB|MCP_KEY_FREE|MC_ANNOUNCE)) {
1322 reply("MSG_INVALID_MODES", unsplit_string(argv+1, argc-1, NULL));
1323 return 0;
1324 }
1325 reply("OSMSG_MODE_SET", channel->name);
1326 return 1;
1327}
1328
1329static MODCMD_FUNC(cmd_op)
1330{
1331 struct mod_chanmode *change;
1332 unsigned int arg, count;
1333
1334 change = mod_chanmode_alloc(argc-1);
1335 for (arg = 1, count = 0; arg < argc; ++arg) {
1336 struct userNode *victim;
1337 struct modeNode *mn;
1338 if (!(victim = GetUserH(argv[arg])))
1339 continue;
1340 if (!(mn = GetUserMode(channel, victim)))
1341 continue;
1342 if (mn->modes & MODE_CHANOP)
1343 continue;
1344 change->args[count].mode = MODE_CHANOP;
a32da4c7 1345 change->args[count++].u.member = mn;
d76ed9a9 1346 }
1347 if (count) {
1348 change->argc = count;
1349 modcmd_chanmode_announce(change);
1350 }
1351 mod_chanmode_free(change);
1352 reply("OSMSG_OP_DONE");
1353 return 1;
1354}
1355
55342ce8 1356static MODCMD_FUNC(cmd_hop)
1357{
1358 struct mod_chanmode *change;
1359 unsigned int arg, count;
1360
1361 change = mod_chanmode_alloc(argc-1);
1362 for (arg = 1, count = 0; arg < argc; ++arg) {
1363 struct userNode *victim;
1364 struct modeNode *mn;
1365 if (!(victim = GetUserH(argv[arg])))
1366 continue;
1367 if (!(mn = GetUserMode(channel, victim)))
1368 continue;
1369 if (mn->modes & MODE_HALFOP)
1370 continue;
1371 change->args[count].mode = MODE_HALFOP;
11408ce4 1372 change->args[count++].u.member = mn;
55342ce8 1373 }
1374 if (count) {
1375 change->argc = count;
1376 modcmd_chanmode_announce(change);
1377 }
1378 mod_chanmode_free(change);
1379 reply("OSMSG_HOP_DONE");
1380 return 1;
1381}
1382
d76ed9a9 1383static MODCMD_FUNC(cmd_opall)
1384{
1385 struct mod_chanmode *change;
1386 unsigned int ii, count;
1387
1388 change = mod_chanmode_alloc(channel->members.used);
1389 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 1390 struct modeNode *mn = channel->members.list[ii];
1391 if (mn->modes & MODE_CHANOP)
d76ed9a9 1392 continue;
1393 change->args[count].mode = MODE_CHANOP;
a32da4c7 1394 change->args[count++].u.member = mn;
d76ed9a9 1395 }
1396 if (count) {
1397 change->argc = count;
258d1427 1398 modcmd_chanmode_announce(change);
d76ed9a9 1399 }
1400 mod_chanmode_free(change);
1401 reply("OSMSG_OPALL_DONE", channel->name);
1402 return 1;
1403}
1404
55342ce8 1405static MODCMD_FUNC(cmd_hopall)
1406{
1407 struct mod_chanmode *change;
1408 unsigned int ii, count;
1409
1410 change = mod_chanmode_alloc(channel->members.used);
1411 for (ii = count = 0; ii < channel->members.used; ++ii) {
1412 struct modeNode *mn = channel->members.list[ii];
1413 if (mn->modes & MODE_HALFOP)
1414 continue;
1415 change->args[count].mode = MODE_HALFOP;
11408ce4 1416 change->args[count++].u.member = mn;
55342ce8 1417 }
1418 if (count) {
1419 change->argc = count;
1420 modcmd_chanmode_announce(change);
1421 }
1422 mod_chanmode_free(change);
1423 reply("OSMSG_HOPALL_DONE", channel->name);
1424 return 1;
1425}
1426
d76ed9a9 1427static MODCMD_FUNC(cmd_whois)
1428{
1429 struct userNode *target;
1430 char buffer[128];
1431 int bpos, herelen;
1432
1433#ifdef WITH_PROTOCOL_P10
1434 if (argv[1][0] == '*')
1435 target = GetUserN(argv[1]+1);
1436 else
d76ed9a9 1437#endif
1117fc5a 1438 target = GetUserH(argv[1]);
d76ed9a9 1439 if (!target) {
1440 reply("MSG_NICK_UNKNOWN", argv[1]);
1441 return 0;
1442 }
1443 reply("OSMSG_WHOIS_NICK", target->nick);
1444 reply("OSMSG_WHOIS_HOST", target->ident, target->hostname);
1445 if (IsFakeHost(target))
1446 reply("OSMSG_WHOIS_FAKEHOST", target->fakehost);
2f61d1d7 1447 reply("OSMSG_WHOIS_IP", irc_ntoa(&target->ip));
d76ed9a9 1448 if (target->modes) {
258d1427 1449 bpos = 0;
d76ed9a9 1450#define buffer_cat(str) (herelen = strlen(str), memcpy(buffer+bpos, str, herelen), bpos += herelen)
258d1427 1451 if (IsInvisible(target)) buffer[bpos++] = 'i';
1452 if (IsWallOp(target)) buffer[bpos++] = 'w';
1453 if (IsOper(target)) buffer[bpos++] = 'o';
1454 if (IsGlobal(target)) buffer[bpos++] = 'g';
1455 if (IsServNotice(target)) buffer[bpos++] = 's';
1456
1457 // sethost - reed/apples
1458 // if (IsHelperIrcu(target)) buffer[bpos++] = 'h';
1459 if (IsSetHost(target)) buffer[bpos++] = 'h';
1460
1461 if (IsService(target)) buffer[bpos++] = 'k';
1462 if (IsDeaf(target)) buffer[bpos++] = 'd';
b2cf3d66 1463 if (target->handle_info) buffer[bpos++] = 'r';
d76ed9a9 1464 if (IsHiddenHost(target)) buffer[bpos++] = 'x';
1465 if (IsGagged(target)) buffer_cat(" (gagged)");
258d1427 1466 if (IsRegistering(target)) buffer_cat(" (registered account)");
1467 buffer[bpos] = 0;
1468 if (bpos > 0)
d76ed9a9 1469 reply("OSMSG_WHOIS_MODES", buffer);
1470 }
1471 reply("OSMSG_WHOIS_INFO", target->info);
1472#ifdef WITH_PROTOCOL_P10
1473 reply("OSMSG_WHOIS_NUMERIC", target->numeric);
1474#endif
1475 reply("OSMSG_WHOIS_SERVER", target->uplink->name);
1476 reply("OSMSG_WHOIS_ACCOUNT", (target->handle_info ? target->handle_info->handle : "Not authenticated"));
1477 intervalString(buffer, now - target->timestamp, user->handle_info);
1478 reply("OSMSG_WHOIS_NICK_AGE", buffer);
1479 if (target->channels.used <= MAX_CHANNELS_WHOIS)
258d1427 1480 opserv_ison(user, target, "OSMSG_WHOIS_CHANNELS");
d76ed9a9 1481 else
258d1427 1482 reply("OSMSG_WHOIS_HIDECHANS");
d76ed9a9 1483 return 1;
1484}
1485
1486static MODCMD_FUNC(cmd_unban)
1487{
1488 struct mod_chanmode change;
1489 mod_chanmode_init(&change);
1490 change.argc = 1;
1491 change.args[0].mode = MODE_REMOVE | MODE_BAN;
a32da4c7 1492 change.args[0].u.hostmask = argv[1];
d76ed9a9 1493 modcmd_chanmode_announce(&change);
1494 reply("OSMSG_UNBAN_DONE", channel->name);
1495 return 1;
1496}
1497
1498static MODCMD_FUNC(cmd_voiceall)
1499{
1500 struct mod_chanmode *change;
1501 unsigned int ii, count;
1502
1503 change = mod_chanmode_alloc(channel->members.used);
1504 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 1505 struct modeNode *mn = channel->members.list[ii];
1506 if (mn->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE))
d76ed9a9 1507 continue;
1508 change->args[count].mode = MODE_VOICE;
a32da4c7 1509 change->args[count++].u.member = mn;
d76ed9a9 1510 }
1511 if (count) {
1512 change->argc = count;
258d1427 1513 modcmd_chanmode_announce(change);
d76ed9a9 1514 }
1515 mod_chanmode_free(change);
1516 reply("OSMSG_CHANNEL_VOICED", channel->name);
1517 return 1;
1518}
1519
1520static MODCMD_FUNC(cmd_devoiceall)
1521{
1522 struct mod_chanmode *change;
1523 unsigned int ii, count;
1524
1525 change = mod_chanmode_alloc(channel->members.used);
1526 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 1527 struct modeNode *mn = channel->members.list[ii];
1528 if (!(mn->modes & MODE_VOICE))
d76ed9a9 1529 continue;
1530 change->args[count].mode = MODE_REMOVE | MODE_VOICE;
a32da4c7 1531 change->args[count++].u.member = mn;
d76ed9a9 1532 }
1533 if (count) {
1534 change->argc = count;
258d1427 1535 modcmd_chanmode_announce(change);
d76ed9a9 1536 }
1537 mod_chanmode_free(change);
1538 reply("OSMSG_CHANNEL_DEVOICED", channel->name);
1539 return 1;
1540}
1541
1542static MODCMD_FUNC(cmd_stats_bad) {
1543 dict_iterator_t it;
1544 unsigned int ii, end, here_len;
1545 char buffer[400];
1546
1547 /* Show the bad word list.. */
de9510bc 1548 /* TODO: convert nonprinting chars like bold to $b etc in a usable way */
d76ed9a9 1549 for (ii=end=0; ii<opserv_bad_words->used; ii++) {
1550 here_len = strlen(opserv_bad_words->list[ii]);
de9510bc 1551 /* If the line is full output it & start again */
d76ed9a9 1552 if ((end + here_len + 2) > sizeof(buffer)) {
1553 buffer[end] = 0;
1554 reply("OSMSG_BADWORD_LIST", buffer);
1555 end = 0;
1556 }
1557 memcpy(buffer+end, opserv_bad_words->list[ii], here_len);
1558 end += here_len;
1559 buffer[end++] = ' ';
1560 }
1561 buffer[end] = 0;
1562 reply("OSMSG_BADWORD_LIST", buffer);
1563
1564 /* Show the exemption list.. */
1565 for (it=dict_first(opserv_exempt_channels), end=0; it; it=iter_next(it)) {
1566 here_len = strlen(iter_key(it));
1567 if ((end + here_len + 2) > sizeof(buffer)) {
1568 buffer[end] = 0;
1569 reply("OSMSG_EXEMPTED_LIST", buffer);
1570 end = 0;
1571 }
1572 memcpy(buffer+end, iter_key(it), here_len);
1573 end += here_len;
1574 buffer[end++] = ' ';
1575 }
1576 buffer[end] = 0;
1577 reply("OSMSG_EXEMPTED_LIST", buffer);
1578 return 1;
1579}
1580
1581static MODCMD_FUNC(cmd_stats_glines) {
1582 reply("OSMSG_GLINE_COUNT", gline_count());
1583 return 1;
1584}
1585
d914d1cb 1586static MODCMD_FUNC(cmd_stats_shuns) {
1587 reply("OSMSG_SHUN_COUNT", shun_count());
1588 return 1;
1589}
1590
d76ed9a9 1591static void
1592trace_links(struct userNode *bot, struct userNode *user, struct server *server, unsigned int depth) {
1593 unsigned int nn, pos;
1594 char buffer[400];
1595
1596 for (nn=1; nn<=depth; nn<<=1) ;
1597 for (pos=0, nn>>=1; nn>1; ) {
1598 nn >>= 1;
1599 buffer[pos++] = (depth & nn) ? ((nn == 1) ? '`' : ' ') : '|';
1600 buffer[pos++] = (nn == 1) ? '-': ' ';
1601 }
1602 buffer[pos] = 0;
1603 send_message(user, bot, "OSMSG_LINKS_SERVER", buffer, server->name, server->clients, server->description);
1604 if (!server->children.used)
1605 return;
1606 for (nn=0; nn<server->children.used-1; nn++) {
1607 trace_links(bot, user, server->children.list[nn], depth<<1);
1608 }
1609 trace_links(bot, user, server->children.list[nn], (depth<<1)|1);
1610}
1611
1612static MODCMD_FUNC(cmd_stats_links) {
1613 trace_links(cmd->parent->bot, user, self, 1);
1614 return 1;
1615}
1616
1617
1618static MODCMD_FUNC(cmd_stats_max) {
1619 reply("OSMSG_MAX_CLIENTS", max_clients, asctime(localtime(&max_clients_time)));
1620 return 1;
1621}
1622
1623static MODCMD_FUNC(cmd_stats_network) {
1624 struct helpfile_table tbl;
1625 unsigned int nn, tot_clients;
1626 dict_iterator_t it;
1627
1628 tot_clients = dict_size(clients);
1629 reply("OSMSG_NETWORK_INFO", tot_clients, invis_clients, curr_opers.used);
1630 tbl.length = dict_size(servers)+1;
1631 tbl.width = 3;
1632 tbl.flags = TABLE_NO_FREE;
1633 tbl.contents = calloc(tbl.length, sizeof(*tbl.contents));
1634 tbl.contents[0] = calloc(tbl.width, sizeof(**tbl.contents));
1635 tbl.contents[0][0] = "Server Name";
1636 tbl.contents[0][1] = "Clients";
1637 tbl.contents[0][2] = "Load";
1638 for (it=dict_first(servers), nn=1; it; it=iter_next(it)) {
1639 struct server *server = iter_data(it);
1640 char *buffer = malloc(32);
1641 tbl.contents[nn] = calloc(tbl.width, sizeof(**tbl.contents));
1642 tbl.contents[nn][0] = server->name;
1643 tbl.contents[nn][1] = buffer;
1644 sprintf(buffer, "%u", server->clients);
1645 tbl.contents[nn][2] = buffer + 16;
1646 sprintf(buffer+16, "%3.3g%%", ((double)server->clients/tot_clients)*100);
1647 nn++;
1648 }
1649 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
1650 for (nn=1; nn<tbl.length; nn++) {
1651 free((char*)tbl.contents[nn][1]);
1652 free(tbl.contents[nn]);
1653 }
1654 free(tbl.contents[0]);
1655 free(tbl.contents);
1656 return 1;
1657}
1658
1659static MODCMD_FUNC(cmd_stats_network2) {
1660 struct helpfile_table tbl;
1661 unsigned int nn;
1662 dict_iterator_t it;
1663
1664 tbl.length = dict_size(servers)+1;
1665 tbl.width = 3;
1666 tbl.flags = TABLE_NO_FREE;
1667 tbl.contents = calloc(tbl.length, sizeof(*tbl.contents));
1668 tbl.contents[0] = calloc(tbl.width, sizeof(**tbl.contents));
1669 tbl.contents[0][0] = "Server Name";
1670 tbl.contents[0][1] = "Numeric";
1671 tbl.contents[0][2] = "Link Time";
1672 for (it=dict_first(servers), nn=1; it; it=iter_next(it)) {
1673 struct server *server = iter_data(it);
1674 char *buffer = malloc(64);
1675 int ofs;
1676
1677 tbl.contents[nn] = calloc(tbl.width, sizeof(**tbl.contents));
1678 tbl.contents[nn][0] = server->name;
1679#ifdef WITH_PROTOCOL_P10
1680 sprintf(buffer, "%s (%ld)", server->numeric, base64toint(server->numeric, strlen(server->numeric)));
1681#else
1682 buffer[0] = 0;
1683#endif
1684 tbl.contents[nn][1] = buffer;
1685 ofs = strlen(buffer) + 1;
1686 intervalString(buffer + ofs, now - server->link, user->handle_info);
1687 if (server->self_burst)
1688 strcat(buffer + ofs, " Bursting");
1689 tbl.contents[nn][2] = buffer + ofs;
1690 nn++;
1691 }
1692 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
1693 for (nn=1; nn<tbl.length; nn++) {
1694 free((char*)tbl.contents[nn][1]);
1695 free(tbl.contents[nn]);
1696 }
1697 free(tbl.contents[0]);
1698 free(tbl.contents);
1699 return 1;
1700}
1701
1702static MODCMD_FUNC(cmd_stats_reserved) {
1703 dict_iterator_t it;
1704
1705 reply("OSMSG_RESERVED_LIST");
1706 for (it = dict_first(opserv_reserved_nick_dict); it; it = iter_next(it))
1707 send_message_type(4, user, cmd->parent->bot, "%s", iter_key(it));
1708 return 1;
1709}
1710
1711static MODCMD_FUNC(cmd_stats_trusted) {
1712 dict_iterator_t it;
1713 struct trusted_host *th;
1714 char length[INTERVALLEN], issued[INTERVALLEN], limit[32];
1715
de9510bc 1716 reply("OSMSG_TRUSTED_LIST");
1717 reply("OSMSG_TRUSTED_LIST_BAR");
1718 reply("OSMSG_TRUSTED_LIST_HEADER");
1719 reply("OSMSG_TRUSTED_LIST_BAR");
d76ed9a9 1720 if (argc > 1) {
1721 th = dict_find(opserv_trusted_hosts, argv[1], NULL);
1722 if (th) {
1723 if (th->issued)
1724 intervalString(issued, now - th->issued, user->handle_info);
1725 if (th->expires)
1726 intervalString(length, th->expires - now, user->handle_info);
1727 if (th->limit)
de9510bc 1728 sprintf(limit, "%lu", th->limit);
d76ed9a9 1729 reply("OSMSG_HOST_IS_TRUSTED",
1730 th->ipaddr,
de9510bc 1731 (th->limit ? limit : "none"),
d76ed9a9 1732 (th->issuer ? th->issuer : "<unknown>"),
de9510bc 1733 (th->issued ? issued : "some time"),
1734 (th->expires ? length : "never"));
1735 reply("OSMSG_HOST_IS_TRUSTED_DESC", (th->reason ? th->reason : "<unknown>"));
d76ed9a9 1736 } else {
1737 reply("OSMSG_HOST_NOT_TRUSTED", argv[1]);
1738 }
1739 } else {
d76ed9a9 1740 for (it = dict_first(opserv_trusted_hosts); it; it = iter_next(it)) {
1741 th = iter_data(it);
1742 if (th->issued)
1743 intervalString(issued, now - th->issued, user->handle_info);
1744 if (th->expires)
1745 intervalString(length, th->expires - now, user->handle_info);
1746 if (th->limit)
de9510bc 1747 sprintf(limit, "%lu", th->limit);
d76ed9a9 1748 reply("OSMSG_HOST_IS_TRUSTED", iter_key(it),
de9510bc 1749 (th->limit ? limit : "none"),
d76ed9a9 1750 (th->issuer ? th->issuer : "<unknown>"),
de9510bc 1751 (th->issued ? issued : "some time"),
1752 (th->expires ? length : "never"));
1753 reply("OSMSG_HOST_IS_TRUSTED_DESC", (th->reason ? th->reason : "<unknown>"));
d76ed9a9 1754 }
1755 }
de9510bc 1756 reply("OSMSG_TRUSTED_LIST_END");
d76ed9a9 1757 return 1;
1758}
1759
1760static MODCMD_FUNC(cmd_stats_uplink) {
1761 extern struct cManagerNode cManager;
1762 struct uplinkNode *uplink;
1763
1764 uplink = cManager.uplink;
1765 reply("OSMSG_UPLINK_START", uplink->name);
1766 reply("OSMSG_UPLINK_ADDRESS", uplink->host, uplink->port);
1767 return 1;
1768}
1769
1770static MODCMD_FUNC(cmd_stats_uptime) {
1771 char uptime[INTERVALLEN];
1772 struct tms buf;
1773 extern time_t boot_time;
1774 extern int lines_processed;
1775 static long clocks_per_sec;
1776
1777 if (!clocks_per_sec) {
1778#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
1779 clocks_per_sec = sysconf(_SC_CLK_TCK);
1780 if (clocks_per_sec <= 0)
1781#endif
1782 {
1783 log_module(OS_LOG, LOG_ERROR, "Unable to query sysconf(_SC_CLK_TCK), output of 'stats uptime' will be wrong");
1784 clocks_per_sec = CLOCKS_PER_SEC;
1785 }
1786 }
1787 intervalString(uptime, time(NULL)-boot_time, user->handle_info);
1788 times(&buf);
1789 reply("OSMSG_UPTIME_STATS",
1790 uptime, lines_processed,
1791 buf.tms_utime/(double)clocks_per_sec,
1792 buf.tms_stime/(double)clocks_per_sec);
1793 return 1;
1794}
1795
1796static MODCMD_FUNC(cmd_stats_alerts) {
1797 dict_iterator_t it;
1798 struct opserv_user_alert *alert;
1799 const char *reaction;
1800
1801 reply("OSMSG_ALERTS_LIST");
de9510bc 1802 reply("OSMSG_ALERTS_BAR");
1803 reply("OSMSG_ALERTS_HEADER");
1804 reply("OSMSG_ALERTS_BAR");
d76ed9a9 1805 for (it = dict_first(opserv_user_alerts); it; it = iter_next(it)) {
1806 alert = iter_data(it);
1807 switch (alert->reaction) {
1808 case REACT_NOTICE: reaction = "notice"; break;
1809 case REACT_KILL: reaction = "kill"; break;
9a75756e 1810 case REACT_SILENT: reaction = "silent"; break;
d76ed9a9 1811 case REACT_GLINE: reaction = "gline"; break;
d914d1cb 1812 case REACT_SHUN: reaction = "shun"; break;
d76ed9a9 1813 default: reaction = "<unknown>"; break;
1814 }
de9510bc 1815 reply("OSMSG_ALERT_IS", iter_key(it), reaction, alert->owner);
1816 reply("OSMSG_ALERTS_DESC", alert->text_discrim);
d76ed9a9 1817 }
de9510bc 1818 reply("OSMSG_ALERT_END");
d76ed9a9 1819 return 1;
1820}
1821
1822static MODCMD_FUNC(cmd_stats_gags) {
1823 struct gag_entry *gag;
1824 struct helpfile_table table;
1825 unsigned int nn;
1826
1827 if (!gagList) {
258d1427 1828 reply("OSMSG_NO_GAGS");
d76ed9a9 1829 return 1;
1830 }
1831 for (nn=0, gag=gagList; gag; nn++, gag=gag->next) ;
1832 table.length = nn+1;
1833 table.width = 4;
1834 table.flags = TABLE_NO_FREE;
1835 table.contents = calloc(table.length, sizeof(char**));
1836 table.contents[0] = calloc(table.width, sizeof(char*));
1837 table.contents[0][0] = "Mask";
1838 table.contents[0][1] = "Owner";
1839 table.contents[0][2] = "Expires";
1840 table.contents[0][3] = "Reason";
1841 for (nn=1, gag=gagList; gag; nn++, gag=gag->next) {
1842 char expstr[INTERVALLEN];
1843 if (gag->expires)
1844 intervalString(expstr, gag->expires - now, user->handle_info);
1845 else
1846 strcpy(expstr, "Never");
1847 table.contents[nn] = calloc(table.width, sizeof(char*));
1848 table.contents[nn][0] = gag->mask;
1849 table.contents[nn][1] = gag->owner;
1850 table.contents[nn][2] = strdup(expstr);
1851 table.contents[nn][3] = gag->reason;
1852 }
1853 table_send(cmd->parent->bot, user->nick, 0, NULL, table);
1854 for (nn=1; nn<table.length; nn++) {
1855 free((char*)table.contents[nn][2]);
1856 free(table.contents[nn]);
1857 }
1858 free(table.contents[0]);
1859 free(table.contents);
1860 return 1;
1861}
1862
1863static MODCMD_FUNC(cmd_stats_timeq) {
1864 reply("OSMSG_TIMEQ_INFO", timeq_size(), timeq_next()-now);
1865 return 1;
1866}
1867
de9510bc 1868/*
d76ed9a9 1869static MODCMD_FUNC(cmd_stats_warn) {
1870 dict_iterator_t it;
1871
1872 reply("OSMSG_WARN_LISTSTART");
1873 for (it=dict_first(opserv_chan_warn); it; it=iter_next(it))
1874 reply("OSMSG_WARN_LISTENTRY", iter_key(it), (char*)iter_data(it));
1875 reply("OSMSG_WARN_LISTEND");
1876 return 1;
1877}
de9510bc 1878*/
d76ed9a9 1879
f14e4f83 1880#if defined(WITH_MALLOC_X3)
ec1a68c8 1881static MODCMD_FUNC(cmd_stats_memory) {
1882 extern unsigned long alloc_count, alloc_size;
0d16e639 1883 send_message_type(MSG_TYPE_NOXLATE, user, cmd->parent->bot,
1884 "%u allocations totalling %u bytes.",
1885 alloc_count, alloc_size);
1886 return 1;
1887}
1888#elif defined(WITH_MALLOC_SLAB)
1889static MODCMD_FUNC(cmd_stats_memory) {
1890 extern unsigned long slab_alloc_count, slab_count, slab_alloc_size;
1891 extern unsigned long big_alloc_count, big_alloc_size;
1892 send_message_type(MSG_TYPE_NOXLATE, user, cmd->parent->bot,
1893 "%u allocations in %u slabs totalling %u bytes.",
1894 slab_alloc_count, slab_count, slab_alloc_size);
de9510bc 1895/* send_message_type(MSG_TYPE_NOXLATE, user, cmd->parent->bot,
0d16e639 1896 "%u big allocations totalling %u bytes.",
de9510bc 1897 */
ec1a68c8 1898 return 1;
1899}
1900#endif
1901
d76ed9a9 1902static MODCMD_FUNC(cmd_dump)
1903{
b336c8db 1904 char linedup[MAXLEN], original[MAXLEN];
d76ed9a9 1905
b336c8db 1906 unsplit_string(argv+1, argc-1, original);
d76ed9a9 1907 safestrncpy(linedup, original, sizeof(linedup));
1908 /* assume it's only valid IRC if we can parse it */
1909 if (parse_line(linedup, 1)) {
258d1427 1910 irc_raw(original);
1911 reply("OSMSG_LINE_DUMPED");
d76ed9a9 1912 } else
258d1427 1913 reply("OSMSG_RAW_PARSE_ERROR");
d76ed9a9 1914 return 1;
1915}
1916
1917static MODCMD_FUNC(cmd_raw)
1918{
b336c8db 1919 char linedup[MAXLEN], original[MAXLEN];
d76ed9a9 1920
b336c8db 1921 unsplit_string(argv+1, argc-1, original);
d76ed9a9 1922 safestrncpy(linedup, original, sizeof(linedup));
1923 /* Try to parse the line before sending it; if it's too wrong,
1924 * maybe it will core us instead of our uplink. */
1925 parse_line(linedup, 1);
1926 irc_raw(original);
1927 reply("OSMSG_LINE_DUMPED");
1928 return 1;
1929}
1930
1931static struct userNode *
1932opserv_add_reserve(struct svccmd *cmd, struct userNode *user, const char *nick, const char *ident, const char *host, const char *desc)
1933{
1934 struct userNode *resv = GetUserH(nick);
1935 if (resv) {
258d1427 1936 if (IsService(resv)) {
1937 reply("MSG_SERVICE_IMMUNE", resv->nick);
1938 return NULL;
1939 }
1940 if (resv->handle_info
1941 && resv->handle_info->opserv_level > user->handle_info->opserv_level) {
1942 reply("OSMSG_LEVEL_TOO_LOW");
1943 return NULL;
1944 }
d76ed9a9 1945 }
1946 if ((resv = AddClone(nick, ident, host, desc))) {
1947 dict_insert(opserv_reserved_nick_dict, resv->nick, resv);
1948 }
1949 return resv;
1950}
1951
1952static MODCMD_FUNC(cmd_collide)
1953{
1954 struct userNode *resv;
1955
1956 resv = opserv_add_reserve(cmd, user, argv[1], argv[2], argv[3], unsplit_string(argv+4, argc-4, NULL));
1957 if (resv) {
258d1427 1958 reply("OSMSG_COLLIDED_NICK", resv->nick);
1959 return 1;
d76ed9a9 1960 } else {
1961 reply("OSMSG_CLONE_FAILED", argv[1]);
258d1427 1962 return 0;
d76ed9a9 1963 }
1964}
1965
1966static MODCMD_FUNC(cmd_reserve)
1967{
1968 struct userNode *resv;
1969
1970 resv = opserv_add_reserve(cmd, user, argv[1], argv[2], argv[3], unsplit_string(argv+4, argc-4, NULL));
1971 if (resv) {
258d1427 1972 resv->modes |= FLAGS_PERSISTENT;
1973 reply("OSMSG_RESERVED_NICK", resv->nick);
1974 return 1;
d76ed9a9 1975 } else {
1976 reply("OSMSG_CLONE_FAILED", argv[1]);
258d1427 1977 return 0;
d76ed9a9 1978 }
1979}
1980
1981static int
1982free_reserve(char *nick)
1983{
1984 struct userNode *resv;
1985 unsigned int rlen;
1986 char *reason;
1987
1988 resv = dict_find(opserv_reserved_nick_dict, nick, NULL);
1989 if (!resv)
1990 return 0;
1991
1992 rlen = strlen(resv->nick)+strlen(OSMSG_PART_REASON);
1993 reason = alloca(rlen);
1994 snprintf(reason, rlen, OSMSG_PART_REASON, resv->nick);
1995 DelUser(resv, NULL, 1, reason);
1996 dict_remove(opserv_reserved_nick_dict, nick);
1997 return 1;
1998}
1999
2000static MODCMD_FUNC(cmd_unreserve)
2001{
2002 if (free_reserve(argv[1]))
258d1427 2003 reply("OSMSG_NICK_UNRESERVED", argv[1]);
d76ed9a9 2004 else
258d1427 2005 reply("OSMSG_NOT_RESERVED", argv[1]);
d76ed9a9 2006 return 1;
2007}
2008
2009static void
2010opserv_part_channel(void *data)
2011{
2012 DelChannelUser(opserv, data, "Leaving.", 0);
2013}
2014
2015static int alert_check_user(const char *key, void *data, void *extra);
2016
2017static int
2018opserv_new_user_check(struct userNode *user)
2019{
2020 struct opserv_hostinfo *ohi;
2021 struct gag_entry *gag;
2f61d1d7 2022 char addr[IRC_NTOP_MAX_SIZE];
d76ed9a9 2023
2024 /* Check to see if we should ignore them entirely. */
2025 if (IsLocal(user) || IsService(user))
2026 return 0;
2027
2028 /* Check for alerts, and stop if we find one that kills them. */
2029 if (dict_foreach(opserv_user_alerts, alert_check_user, user))
2030 return 1;
2031
2032 /* Gag them if appropriate. */
2033 for (gag = gagList; gag; gag = gag->next) {
2f61d1d7 2034 if (user_matches_glob(user, gag->mask, MATCH_USENICK)) {
d76ed9a9 2035 gag_helper_func(user, NULL);
2036 break;
2037 }
2038 }
2039
2040 /* Add to host info struct */
2f61d1d7 2041 irc_ntop(addr, sizeof(addr), &user->ip);
2042 if (!(ohi = dict_find(opserv_hostinfo_dict, addr, NULL))) {
d76ed9a9 2043 ohi = calloc(1, sizeof(*ohi));
2f61d1d7 2044 dict_insert(opserv_hostinfo_dict, strdup(addr), ohi);
d76ed9a9 2045 userList_init(&ohi->clients);
2046 }
2047 userList_append(&ohi->clients, user);
2048
2049 /* Only warn of new user floods outside of bursts. */
2050 if (!user->uplink->burst) {
2051 if (!policer_conforms(&opserv_conf.new_user_policer, now, 10)) {
2052 if (!new_user_flood) {
2053 new_user_flood = 1;
2054 opserv_alert("Warning: Possible new-user flood.");
2055 }
2056 } else {
2057 new_user_flood = 0;
2058 }
2059 }
2060
2061 /* Only warn or G-line if there's an untrusted max and their IP is sane. */
2f61d1d7 2062 if (opserv_conf.untrusted_max
2063 && irc_in_addr_is_valid(user->ip)
2064 && !irc_in_addr_is_loopback(user->ip)) {
2065 struct trusted_host *th = dict_find(opserv_trusted_hosts, addr, NULL);
d76ed9a9 2066 unsigned int limit = th ? th->limit : opserv_conf.untrusted_max;
2067 if (!limit) {
2068 /* 0 means unlimited hosts */
2069 } else if (ohi->clients.used == limit) {
2070 unsigned int nn;
2071 for (nn=0; nn<ohi->clients.used; nn++)
2072 send_message(ohi->clients.list[nn], opserv, "OSMSG_CLONE_WARNING");
2073 } else if (ohi->clients.used > limit) {
2f61d1d7 2074 char target[IRC_NTOP_MAX_SIZE + 3] = { '*', '@', '\0' };
2075 strcpy(target + 2, addr);
9a75756e 2076 gline_add(opserv->nick, target, opserv_conf.clone_gline_duration, "Excessive connections from a single host.", now, 1, 1);
d76ed9a9 2077 }
2078 }
2079
2080 return 0;
2081}
2082
2083static void
2084opserv_user_cleanup(struct userNode *user, UNUSED_ARG(struct userNode *killer), UNUSED_ARG(const char *why))
2085{
2086 struct opserv_hostinfo *ohi;
2f61d1d7 2087 char addr[IRC_NTOP_MAX_SIZE];
d76ed9a9 2088
2089 if (IsLocal(user)) {
2090 /* Try to remove it from the reserved nick dict without
2091 * calling free_reserve, because that would call DelUser(),
2092 * and we'd loop back to here. */
2093 dict_remove(opserv_reserved_nick_dict, user->nick);
2094 return;
2095 }
2f61d1d7 2096 irc_ntop(addr, sizeof(addr), &user->ip);
2097 if ((ohi = dict_find(opserv_hostinfo_dict, addr, NULL))) {
d76ed9a9 2098 userList_remove(&ohi->clients, user);
2f61d1d7 2099 if (ohi->clients.used == 0)
2100 dict_remove(opserv_hostinfo_dict, addr);
d76ed9a9 2101 }
2102}
2103
2104int
2105opserv_bad_channel(const char *name)
2106{
2107 unsigned int found;
697f4c9a 2108 int present;
d76ed9a9 2109
697f4c9a 2110 dict_find(opserv_exempt_channels, name, &present);
2111 if (present)
d76ed9a9 2112 return 0;
2113
2114 if (gline_find(name))
2115 return 1;
2116
2117 for (found=0; found<opserv_bad_words->used; ++found)
2118 if (irccasestr(name, opserv_bad_words->list[found]))
2119 return 1;
2120
2121 return 0;
2122}
2123
2124static void
2125opserv_shutdown_channel(struct chanNode *channel, const char *reason)
2126{
2127 struct mod_chanmode *change;
2128 unsigned int nn;
2129
2130 change = mod_chanmode_alloc(2);
2131 change->modes_set = MODE_SECRET | MODE_INVITEONLY;
2132 change->args[0].mode = MODE_CHANOP;
a32da4c7 2133 change->args[0].u.member = AddChannelUser(opserv, channel);
d76ed9a9 2134 change->args[1].mode = MODE_BAN;
a32da4c7 2135 change->args[1].u.hostmask = "*!*@*";
d76ed9a9 2136 mod_chanmode_announce(opserv, channel, change);
2137 mod_chanmode_free(change);
2138 for (nn=channel->members.used; nn>0; ) {
2139 struct modeNode *mNode = channel->members.list[--nn];
2140 if (IsService(mNode->user))
2141 continue;
2142 KickChannelUser(mNode->user, channel, opserv, user_find_message(mNode->user, reason));
2143 }
2144 timeq_add(now + opserv_conf.purge_lock_delay, opserv_part_channel, channel);
2145}
2146
2147static void
2148opserv_channel_check(struct chanNode *newchan)
2149{
c52666c6 2150 /*char *warning; */
d76ed9a9 2151
2152 if (!newchan->join_policer.params) {
2153 newchan->join_policer.last_req = now;
2154 newchan->join_policer.params = opserv_conf.join_policer_params;
2155 }
c52666c6 2156 /*
d76ed9a9 2157 if ((warning = dict_find(opserv_chan_warn, newchan->name, NULL))) {
2158 char message[MAXLEN];
2159 snprintf(message, sizeof(message), "Channel activity warning for channel %s: %s", newchan->name, warning);
2160 global_message(MESSAGE_RECIPIENT_OPERS, message);
2161 }
c52666c6 2162 */
d76ed9a9 2163
c52666c6 2164 /* Wait until the join check to shut channels down. */
d76ed9a9 2165 newchan->bad_channel = opserv_bad_channel(newchan->name);
2166}
2167
2168static void
2169opserv_channel_delete(struct chanNode *chan)
2170{
2171 timeq_del(0, opserv_part_channel, chan, TIMEQ_IGNORE_WHEN);
2172}
2173
2174static int
2175opserv_join_check(struct modeNode *mNode)
2176{
2177 struct userNode *user = mNode->user;
2178 struct chanNode *channel = mNode->channel;
2179 const char *msg;
2180
2181 if (IsService(user))
2182 return 0;
2183
2184 dict_foreach(opserv_channel_alerts, alert_check_user, user);
2185
2186 if (channel->bad_channel) {
2187 opserv_debug("Found $b%s$b in bad-word channel $b%s$b; removing the user.", user->nick, channel->name);
2188 if (channel->name[0] != '#')
2189 DelUser(user, opserv, 1, "OSMSG_ILLEGAL_KILL_REASON");
2190 else if (!GetUserMode(channel, opserv))
2191 opserv_shutdown_channel(channel, "OSMSG_ILLEGAL_REASON");
2192 else {
2193 send_message(user, opserv, "OSMSG_ILLEGAL_CHANNEL", channel->name);
2194 msg = user_find_message(user, "OSMSG_ILLEGAL_REASON");
2195 KickChannelUser(user, channel, opserv, msg);
2196 }
2197 return 1;
2198 }
2199
2200 if (user->uplink->burst)
2201 return 0;
2202 if (policer_conforms(&channel->join_policer, now, 1.0)) {
2203 channel->join_flooded = 0;
2204 return 0;
2205 }
2206 if (!channel->join_flooded) {
2207 /* Don't moderate the channel unless it is activated and
2208 the number of users in the channel is over the threshold. */
2209 struct mod_chanmode change;
2210 mod_chanmode_init(&change);
2211 channel->join_flooded = 1;
2212 if (opserv_conf.join_flood_moderate && (channel->members.used > opserv_conf.join_flood_moderate_threshold)) {
2213 if (!GetUserMode(channel, opserv)) {
2214 /* If we aren't in the channel, join it. */
2215 change.args[0].mode = MODE_CHANOP;
a32da4c7 2216 change.args[0].u.member = AddChannelUser(opserv, channel);
d76ed9a9 2217 change.argc++;
2218 }
2219 if (!(channel->modes & MODE_MODERATED))
2220 change.modes_set |= MODE_MODERATED;
2221 if (change.modes_set || change.argc)
2222 mod_chanmode_announce(opserv, channel, &change);
2223 send_target_message(0, channel->name, opserv, "OSMSG_FLOOD_MODERATE");
2224 opserv_alert("Warning: Possible join flood in %s (currently %d users; channel moderated).", channel->name, channel->members.used);
2225 } else {
2226 opserv_alert("Warning: Possible join flood in %s (currently %d users).", channel->name, channel->members.used);
2227 }
2228 }
2229 log_module(OS_LOG, LOG_INFO, "Join to %s during flood: "IDENT_FORMAT, channel->name, IDENT_DATA(user));
2230 return 0;
2231}
2232
2233static int
2234opserv_add_bad_word(struct svccmd *cmd, struct userNode *user, const char *new_bad) {
2235 unsigned int bad_idx;
2236
2237 for (bad_idx = 0; bad_idx < opserv_bad_words->used; ++bad_idx) {
2238 char *orig_bad = opserv_bad_words->list[bad_idx];
2239 if (irccasestr(new_bad, orig_bad)) {
2240 if (user)
2241 reply("OSMSG_BAD_REDUNDANT", new_bad, orig_bad);
2242 return 0;
2243 } else if (irccasestr(orig_bad, new_bad)) {
2244 if (user)
2245 reply("OSMSG_BAD_GROWING", orig_bad, new_bad);
2246 free(orig_bad);
2247 opserv_bad_words->list[bad_idx] = strdup(new_bad);
2248 for (bad_idx++; bad_idx < opserv_bad_words->used; bad_idx++) {
2249 orig_bad = opserv_bad_words->list[bad_idx];
2250 if (!irccasestr(orig_bad, new_bad))
2251 continue;
2252 if (user)
2253 reply("OSMSG_BAD_NUKING", orig_bad);
2254 string_list_delete(opserv_bad_words, bad_idx);
2255 bad_idx--;
2256 free(orig_bad);
2257 }
2258 return 1;
2259 }
2260 }
2261 string_list_append(opserv_bad_words, strdup(new_bad));
2262 if (user)
2263 reply("OSMSG_ADDED_BAD", new_bad);
2264 return 1;
2265}
2266
2267static MODCMD_FUNC(cmd_addbad)
2268{
2269 unsigned int arg, count;
2270 dict_iterator_t it;
2271 int bad_found, exempt_found;
2272
2273 /* Create the bad word if it doesn't exist. */
2274 bad_found = !opserv_add_bad_word(cmd, user, argv[1]);
2275
2276 /* Look for exception modifiers. */
2277 for (arg=2; arg<argc; arg++) {
2278 if (!irccasecmp(argv[arg], "except")) {
2279 reply("MSG_DEPRECATED_COMMAND", "addbad ... except", "addexempt");
2280 if (++arg > argc) {
2281 reply("MSG_MISSING_PARAMS", "except");
2282 break;
2283 }
2284 for (count = 0; (arg < argc) && IsChannelName(argv[arg]); arg++) {
2285 dict_find(opserv_exempt_channels, argv[arg], &exempt_found);
2286 if (!exempt_found) {
2287 dict_insert(opserv_exempt_channels, strdup(argv[arg]), NULL);
2288 count++;
2289 }
2290 }
2291 reply("OSMSG_ADDED_EXEMPTIONS", count);
2292 } else {
2293 reply("MSG_DEPRECATED_COMMAND", "addbad (with modifiers)", "addbad");
2294 reply("OSMSG_BAD_MODIFIER", argv[arg]);
2295 }
2296 }
2297
2298 /* Scan for existing channels that match the new bad word. */
2299 if (!bad_found) {
2300 for (it = dict_first(channels); it; it = iter_next(it)) {
2301 struct chanNode *channel = iter_data(it);
2302
2303 if (!opserv_bad_channel(channel->name))
2304 continue;
2305 channel->bad_channel = 1;
2306 if (channel->name[0] == '#')
2307 opserv_shutdown_channel(channel, "OSMSG_ILLEGAL_REASON");
2308 else {
2309 unsigned int nn;
2310 for (nn=0; nn<channel->members.used; nn++) {
2311 struct userNode *user = channel->members.list[nn]->user;
2312 DelUser(user, cmd->parent->bot, 1, "OSMSG_ILLEGAL_KILL_REASON");
2313 }
2314 }
2315 }
2316 }
2317
2318 return 1;
2319}
2320
2321static MODCMD_FUNC(cmd_delbad)
2322{
2323 dict_iterator_t it;
2324 unsigned int nn;
2325
2326 for (nn=0; nn<opserv_bad_words->used; nn++) {
2327 if (!irccasecmp(opserv_bad_words->list[nn], argv[1])) {
2328 string_list_delete(opserv_bad_words, nn);
2329 for (it = dict_first(channels); it; it = iter_next(it)) {
2330 channel = iter_data(it);
2331 if (irccasestr(channel->name, argv[1])
2332 && !opserv_bad_channel(channel->name)) {
2333 DelChannelUser(cmd->parent->bot, channel, "Channel name no longer contains a bad word.", 1);
2334 timeq_del(0, opserv_part_channel, channel, TIMEQ_IGNORE_WHEN);
2335 channel->bad_channel = 0;
2336 }
2337 }
2338 reply("OSMSG_REMOVED_BAD", argv[1]);
2339 return 1;
2340 }
2341 }
2342 reply("OSMSG_NOT_BAD_WORD", argv[1]);
2343 return 0;
2344}
2345
2346static MODCMD_FUNC(cmd_addexempt)
2347{
2348 const char *chanName;
2349
2350 if ((argc > 1) && IsChannelName(argv[1])) {
2351 chanName = argv[1];
2352 } else {
2353 reply("MSG_NOT_CHANNEL_NAME");
2354 OPSERV_SYNTAX();
2355 return 0;
2356 }
2357 dict_insert(opserv_exempt_channels, strdup(chanName), NULL);
2358 channel = GetChannel(chanName);
2359 if (channel) {
2360 if (channel->bad_channel) {
2361 DelChannelUser(cmd->parent->bot, channel, "Channel is now exempt from bad-word checking.", 1);
2362 timeq_del(0, opserv_part_channel, channel, TIMEQ_IGNORE_WHEN);
2363 }
2364 channel->bad_channel = 0;
2365 }
2366 reply("OSMSG_ADDED_EXEMPTION", chanName);
2367 return 1;
2368}
2369
2370static MODCMD_FUNC(cmd_delexempt)
2371{
2372 const char *chanName;
2373
2374 if ((argc > 1) && IsChannelName(argv[1])) {
2375 chanName = argv[1];
2376 } else {
2377 reply("MSG_NOT_CHANNEL_NAME");
2378 OPSERV_SYNTAX();
2379 return 0;
2380 }
2381 if (!dict_remove(opserv_exempt_channels, chanName)) {
2382 reply("OSMSG_NOT_EXEMPT", chanName);
2383 return 0;
2384 }
2385 reply("OSMSG_REMOVED_EXEMPTION", chanName);
2386 return 1;
2387}
2388
2389static void
2390opserv_expire_trusted_host(void *data)
2391{
2392 struct trusted_host *th = data;
2393 dict_remove(opserv_trusted_hosts, th->ipaddr);
2394}
2395
2396static void
2397opserv_add_trusted_host(const char *ipaddr, unsigned int limit, const char *issuer, time_t issued, time_t expires, const char *reason)
2398{
2399 struct trusted_host *th;
2400 th = calloc(1, sizeof(*th));
2401 if (!th)
2402 return;
2403 th->ipaddr = strdup(ipaddr);
2404 th->reason = reason ? strdup(reason) : NULL;
2405 th->issuer = issuer ? strdup(issuer) : NULL;
2406 th->issued = issued;
2407 th->limit = limit;
2408 th->expires = expires;
2409 dict_insert(opserv_trusted_hosts, th->ipaddr, th);
2410 if (th->expires)
2411 timeq_add(th->expires, opserv_expire_trusted_host, th);
2412}
2413
2414static void
2415free_trusted_host(void *data)
2416{
2417 struct trusted_host *th = data;
2418 free(th->ipaddr);
2419 free(th->reason);
2420 free(th->issuer);
2421 free(th);
2422}
2423
2424static MODCMD_FUNC(cmd_addtrust)
2425{
2426 unsigned long interval;
2427 char *reason, *tmp;
2f61d1d7 2428 irc_in_addr_t tmpaddr;
d76ed9a9 2429 unsigned int count;
2430
2431 if (dict_find(opserv_trusted_hosts, argv[1], NULL)) {
2432 reply("OSMSG_ALREADY_TRUSTED", argv[1]);
2433 return 0;
2434 }
2435
2f61d1d7 2436 if (!irc_pton(&tmpaddr, NULL, argv[1])) {
d76ed9a9 2437 reply("OSMSG_BAD_IP", argv[1]);
2438 return 0;
2439 }
2440
2441 count = strtoul(argv[2], &tmp, 10);
2442 if (*tmp != '\0') {
2443 reply("OSMSG_BAD_NUMBER", argv[2]);
2444 return 0;
2445 }
2446
2447 interval = ParseInterval(argv[3]);
2448 if (!interval && strcmp(argv[3], "0")) {
2449 reply("MSG_INVALID_DURATION", argv[3]);
2450 return 0;
2451 }
2452
2453 reason = unsplit_string(argv+4, argc-4, NULL);
2454 opserv_add_trusted_host(argv[1], count, user->handle_info->handle, now, interval ? (now + interval) : 0, reason);
2455 reply("OSMSG_ADDED_TRUSTED");
2456 return 1;
2457}
2458
2459static MODCMD_FUNC(cmd_edittrust)
2460{
2461 unsigned long interval;
2462 struct trusted_host *th;
2463 char *reason, *tmp;
2464 unsigned int count;
2465
2466 th = dict_find(opserv_trusted_hosts, argv[1], NULL);
2467 if (!th) {
2468 reply("OSMSG_NOT_TRUSTED", argv[1]);
2469 return 0;
2470 }
2471 count = strtoul(argv[2], &tmp, 10);
2472 if (!count || *tmp) {
2473 reply("OSMSG_BAD_NUMBER", argv[2]);
2474 return 0;
2475 }
2476 interval = ParseInterval(argv[3]);
2477 if (!interval && strcmp(argv[3], "0")) {
2478 reply("MSG_INVALID_DURATION", argv[3]);
2479 return 0;
2480 }
2481 reason = unsplit_string(argv+4, argc-4, NULL);
2482 if (th->expires)
2483 timeq_del(th->expires, opserv_expire_trusted_host, th, 0);
2484
2485 free(th->reason);
2486 th->reason = strdup(reason);
2487 free(th->issuer);
2488 th->issuer = strdup(user->handle_info->handle);
2489 th->issued = now;
2490 th->limit = count;
2491 if (interval) {
2492 th->expires = now + interval;
2493 timeq_add(th->expires, opserv_expire_trusted_host, th);
2494 } else
2495 th->expires = 0;
2496 reply("OSMSG_UPDATED_TRUSTED", th->ipaddr);
2497 return 1;
2498}
2499
2500static MODCMD_FUNC(cmd_deltrust)
2501{
2502 unsigned int n;
2503
2504 for (n=1; n<argc; n++) {
2505 struct trusted_host *th = dict_find(opserv_trusted_hosts, argv[n], NULL);
2506 if (!th)
2507 continue;
2508 if (th->expires)
2509 timeq_del(th->expires, opserv_expire_trusted_host, th, 0);
2510 dict_remove(opserv_trusted_hosts, argv[n]);
2511 }
2512 reply("OSMSG_REMOVED_TRUSTED");
2513 return 1;
2514}
2515
2516/* This doesn't use dict_t because it's a little simpler to open-code the
2517 * comparisons (and simpler arg-passing for the ADD subcommand).
2518 */
2519static MODCMD_FUNC(cmd_clone)
2520{
2521 int i;
2522 struct userNode *clone;
2523
2524 clone = GetUserH(argv[2]);
2525 if (!irccasecmp(argv[1], "ADD")) {
2526 char *userinfo;
2527 char ident[USERLEN+1];
2528
258d1427 2529 if (argc < 5) {
2530 reply("MSG_MISSING_PARAMS", argv[1]);
2531 OPSERV_SYNTAX();
2532 return 0;
2533 }
2534 if (clone) {
2535 reply("OSMSG_CLONE_EXISTS", argv[2]);
2536 return 0;
2537 }
2538 userinfo = unsplit_string(argv+4, argc-4, NULL);
2539 for (i=0; argv[3][i] && (i<USERLEN); i++) {
2540 if (argv[3][i] == '@') {
2541 ident[i++] = 0;
2542 break;
2543 } else {
d76ed9a9 2544 ident[i] = argv[3][i];
2545 }
258d1427 2546 }
2547 if (!argv[3][i] || (i==USERLEN)) {
2548 reply("OSMSG_NOT_A_HOSTMASK");
2549 return 0;
2550 }
2551 if (!(clone = AddClone(argv[2], ident, argv[3]+i, userinfo))) {
d76ed9a9 2552 reply("OSMSG_CLONE_FAILED", argv[2]);
2553 return 0;
2554 }
2555 reply("OSMSG_CLONE_ADDED", clone->nick);
258d1427 2556 return 1;
d76ed9a9 2557 }
2558 if (!clone) {
258d1427 2559 reply("MSG_NICK_UNKNOWN", argv[2]);
2560 return 0;
d76ed9a9 2561 }
2562 if (clone->uplink != self || IsService(clone)) {
258d1427 2563 reply("OSMSG_NOT_A_CLONE", clone->nick);
2564 return 0;
d76ed9a9 2565 }
2566 if (!irccasecmp(argv[1], "REMOVE")) {
258d1427 2567 const char *reason;
2568 if (argc > 3) {
2569 reason = unsplit_string(argv+3, argc-3, NULL);
2570 } else {
2571 char *tmp;
2572 tmp = alloca(strlen(clone->nick) + strlen(OSMSG_PART_REASON));
2573 sprintf(tmp, OSMSG_PART_REASON, clone->nick);
2574 reason = tmp;
2575 }
2576 DelUser(clone, NULL, 1, reason);
2577 reply("OSMSG_CLONE_REMOVED", argv[2]);
2578 return 1;
d76ed9a9 2579 }
2580 if (argc < 4) {
258d1427 2581 reply("MSG_MISSING_PARAMS", argv[1]);
2582 OPSERV_SYNTAX();
2583 return 0;
d76ed9a9 2584 }
2585 channel = GetChannel(argv[3]);
2586 if (!irccasecmp(argv[1], "JOIN")) {
258d1427 2587 if (!channel
2588 && !(channel = AddChannel(argv[3], now, NULL, NULL, NULL))) {
2589 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
2590 return 0;
2591 }
2592 AddChannelUser(clone, channel);
2593 reply("OSMSG_CLONE_JOINED", clone->nick, channel->name);
2594 return 1;
d76ed9a9 2595 }
2596 if (!irccasecmp(argv[1], "PART")) {
258d1427 2597 if (!channel) {
2598 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
2599 return 0;
2600 }
2601 if (!GetUserMode(channel, clone)) {
2602 reply("OSMSG_NOT_ON_CHANNEL", clone->nick, channel->name);
2603 return 0;
2604 }
2605 reply("OSMSG_CLONE_PARTED", clone->nick, channel->name);
2606 DelChannelUser(clone, channel, "Leaving.", 0);
2607 return 1;
d76ed9a9 2608 }
2609 if (!irccasecmp(argv[1], "OP")) {
2610 struct mod_chanmode change;
258d1427 2611 if (!channel) {
2612 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
2613 return 0;
2614 }
d76ed9a9 2615 mod_chanmode_init(&change);
2616 change.argc = 1;
2617 change.args[0].mode = MODE_CHANOP;
a32da4c7 2618 change.args[0].u.member = GetUserMode(channel, clone);
2619 if (!change.args[0].u.member) {
d76ed9a9 2620 reply("OSMSG_NOT_ON_CHANNEL", clone->nick, channel->name);
2621 return 0;
258d1427 2622 }
d76ed9a9 2623 modcmd_chanmode_announce(&change);
258d1427 2624 reply("OSMSG_OPS_GIVEN", channel->name, clone->nick);
2625 return 1;
d76ed9a9 2626 }
55342ce8 2627 if (!irccasecmp(argv[1], "HOP")) {
2628 struct mod_chanmode change;
2629 if (!channel) {
2630 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
2631 return 0;
2632 }
2633 mod_chanmode_init(&change);
2634 change.argc = 1;
2635 change.args[0].mode = MODE_HALFOP;
11408ce4 2636 change.args[0].u.member = GetUserMode(channel, clone);
2637 if (!change.args[0].u.member) {
55342ce8 2638 reply("OSMSG_NOT_ON_CHANNEL", clone->nick, channel->name);
2639 return 0;
2640 }
2641 modcmd_chanmode_announce(&change);
2642 reply("OSMSG_HOPS_GIVEN", channel->name, clone->nick);
2643 return 1;
2644 }
d76ed9a9 2645 if (argc < 5) {
258d1427 2646 reply("MSG_MISSING_PARAMS", argv[1]);
2647 OPSERV_SYNTAX();
2648 return 0;
d76ed9a9 2649 }
2650 if (!irccasecmp(argv[1], "SAY")) {
258d1427 2651 char *text = unsplit_string(argv+4, argc-4, NULL);
2652 irc_privmsg(clone, argv[3], text);
2653 reply("OSMSG_CLONE_SAID", clone->nick, argv[3]);
2654 return 1;
d76ed9a9 2655 }
2656 reply("OSMSG_UNKNOWN_SUBCOMMAND", argv[1], argv[0]);
2657 return 0;
2658}
2659
2660static struct helpfile_expansion
2661opserv_help_expand(const char *variable)
2662{
2663 extern struct userNode *message_source;
2664 struct helpfile_expansion exp;
2665 struct service *service;
2666 struct svccmd *cmd;
2667 dict_iterator_t it;
2668 int row;
2669 unsigned int level;
2670
2671 if (!(service = service_find(message_source->nick))) {
2672 exp.type = HF_STRING;
2673 exp.value.str = NULL;
2674 } else if (!irccasecmp(variable, "index")) {
2675 exp.type = HF_TABLE;
2676 exp.value.table.length = 1;
2677 exp.value.table.width = 2;
2678 exp.value.table.flags = TABLE_REPEAT_HEADERS | TABLE_REPEAT_ROWS;
2679 exp.value.table.contents = calloc(dict_size(service->commands)+1, sizeof(char**));
2680 exp.value.table.contents[0] = calloc(exp.value.table.width, sizeof(char*));
2681 exp.value.table.contents[0][0] = "Command";
2682 exp.value.table.contents[0][1] = "Level";
2683 for (it=dict_first(service->commands); it; it=iter_next(it)) {
2684 cmd = iter_data(it);
2685 row = exp.value.table.length++;
2686 exp.value.table.contents[row] = calloc(exp.value.table.width, sizeof(char*));
2687 exp.value.table.contents[row][0] = iter_key(it);
2688 level = cmd->min_opserv_level;
2689 if (!level_strings[level]) {
2690 level_strings[level] = malloc(16);
2691 snprintf(level_strings[level], 16, "%3d", level);
2692 }
2693 exp.value.table.contents[row][1] = level_strings[level];
2694 }
2695 } else if (!strncasecmp(variable, "level", 5)) {
2696 cmd = dict_find(service->commands, variable+6, NULL);
2697 exp.type = HF_STRING;
2698 if (cmd) {
2699 level = cmd->min_opserv_level;
2700 exp.value.str = malloc(16);
2701 snprintf(exp.value.str, 16, "%3d", level);
2702 } else {
2703 exp.value.str = NULL;
2704 }
2705 } else {
2706 exp.type = HF_STRING;
2707 exp.value.str = NULL;
2708 }
2709 return exp;
2710}
2711
2712struct modcmd *
2713opserv_define_func(const char *name, modcmd_func_t *func, int min_level, int reqchan, int min_argc)
2714{
2715 char buf[16], *flags = NULL;
2716 unsigned int iflags = 0;
2717 sprintf(buf, "%d", min_level);
2718 switch (reqchan) {
2719 case 1: flags = "+acceptchan"; break;
2720 case 3: flags = "+acceptpluschan"; /* fall through */
2721 case 2: iflags = MODCMD_REQUIRE_CHANNEL; break;
2722 }
2723 if (flags) {
2724 return modcmd_register(opserv_module, name, func, min_argc, iflags, "level", buf, "flags", flags, "flags", "+oper", NULL);
2725 } else {
2726 return modcmd_register(opserv_module, name, func, min_argc, iflags, "level", buf, "flags", "+oper", NULL);
2727 }
2728}
2729
2730int add_reserved(const char *key, void *data, void *extra)
2731{
2732 struct record_data *rd = data;
2733 const char *ident, *hostname, *desc;
2734 struct userNode *reserve;
2735 ident = database_get_data(rd->d.object, KEY_IDENT, RECDB_QSTRING);
2736 if (!ident) {
258d1427 2737 log_module(OS_LOG, LOG_ERROR, "Missing ident for reserve of %s", key);
2738 return 0;
d76ed9a9 2739 }
2740 hostname = database_get_data(rd->d.object, KEY_HOSTNAME, RECDB_QSTRING);
2741 if (!hostname) {
258d1427 2742 log_module(OS_LOG, LOG_ERROR, "Missing hostname for reserve of %s", key);
2743 return 0;
d76ed9a9 2744 }
2745 desc = database_get_data(rd->d.object, KEY_DESC, RECDB_QSTRING);
2746 if (!desc) {
258d1427 2747 log_module(OS_LOG, LOG_ERROR, "Missing description for reserve of %s", key);
2748 return 0;
d76ed9a9 2749 }
2750 if ((reserve = AddClone(key, ident, hostname, desc))) {
2751 reserve->modes |= FLAGS_PERSISTENT;
2752 dict_insert(extra, reserve->nick, reserve);
2753 }
2754 return 0;
2755}
2756
2757static unsigned int
2758foreach_matching_user(const char *hostmask, discrim_search_func func, void *extra)
2759{
2760 discrim_t discrim;
2761 char *dupmask;
2762 unsigned int matched;
2763
2764 if (!self->uplink) return 0;
2765 discrim = calloc(1, sizeof(*discrim));
2766 discrim->limit = dict_size(clients);
2767 discrim->max_level = ~0;
2768 discrim->max_ts = now;
2769 discrim->max_channels = INT_MAX;
2770 discrim->authed = -1;
2771 discrim->info_space = -1;
63665495 2772 discrim->intra_scmp = 0;
2773 discrim->intra_dcmp = 0;
27eaa617 2774 discrim->use_regex = 0;
d76ed9a9 2775 dupmask = strdup(hostmask);
2776 if (split_ircmask(dupmask, &discrim->mask_nick, &discrim->mask_ident, &discrim->mask_host)) {
2f61d1d7 2777 if (!irc_pton(&discrim->ip_mask, &discrim->ip_mask_bits, discrim->mask_host))
2778 discrim->ip_mask_bits = 0;
d76ed9a9 2779 matched = opserv_discrim_search(discrim, func, extra);
2780 } else {
258d1427 2781 log_module(OS_LOG, LOG_ERROR, "Couldn't split IRC mask for gag %s!", hostmask);
d76ed9a9 2782 matched = 0;
2783 }
2784 free(discrim);
2785 free(dupmask);
2786 return matched;
2787}
2788
2789static unsigned int
2790gag_free(struct gag_entry *gag)
2791{
2792 unsigned int ungagged;
2793
2794 /* Remove from gag list */
2795 if (gagList == gag) {
2796 gagList = gag->next;
2797 } else {
2798 struct gag_entry *prev;
2799 for (prev = gagList; prev->next != gag; prev = prev->next) ;
2800 prev->next = gag->next;
2801 }
2802
2803 ungagged = foreach_matching_user(gag->mask, ungag_helper_func, NULL);
2804
2805 /* Deallocate storage */
2806 free(gag->reason);
2807 free(gag->owner);
2808 free(gag->mask);
2809 free(gag);
2810
2811 return ungagged;
2812}
2813
2814static void
2815gag_expire(void *data)
2816{
2817 gag_free(data);
2818}
2819
2820unsigned int
2821gag_create(const char *mask, const char *owner, const char *reason, time_t expires)
2822{
2823 struct gag_entry *gag;
2824
2825 /* Create gag and put it into linked list */
2826 gag = calloc(1, sizeof(*gag));
2827 gag->mask = strdup(mask);
2828 gag->owner = strdup(owner ? owner : "<unknown>");
2829 gag->reason = strdup(reason ? reason : "<unknown>");
2830 gag->expires = expires;
2831 if (gag->expires)
2832 timeq_add(gag->expires, gag_expire, gag);
2833 gag->next = gagList;
2834 gagList = gag;
2835
2836 /* If we're linked, see if who the gag applies to */
2837 return foreach_matching_user(mask, gag_helper_func, gag);
2838}
2839
2840static int
2841add_gag_helper(const char *key, void *data, UNUSED_ARG(void *extra))
2842{
2843 struct record_data *rd = data;
2844 char *owner, *reason, *expstr;
2845 time_t expires;
2846
2847 owner = database_get_data(rd->d.object, KEY_OWNER, RECDB_QSTRING);
2848 reason = database_get_data(rd->d.object, KEY_REASON, RECDB_QSTRING);
2849 expstr = database_get_data(rd->d.object, KEY_EXPIRES, RECDB_QSTRING);
2850 expires = expstr ? strtoul(expstr, NULL, 0) : 0;
2851 gag_create(key, owner, reason, expires);
2852
2853 return 0;
2854}
2855
2856static struct opserv_user_alert *
2857opserv_add_user_alert(struct userNode *req, const char *name, opserv_alert_reaction reaction, const char *text_discrim)
2858{
2859 unsigned int wordc;
2860 char *wordv[MAXNUMPARAMS], *discrim_copy;
2861 struct opserv_user_alert *alert;
2862 char *name_dup;
2863
2864 if (dict_find(opserv_user_alerts, name, NULL)) {
258d1427 2865 send_message(req, opserv, "OSMSG_ALERT_EXISTS", name);
2866 return NULL;
d76ed9a9 2867 }
2868 alert = malloc(sizeof(*alert));
2869 alert->owner = strdup(req->handle_info ? req->handle_info->handle : req->nick);
2870 alert->text_discrim = strdup(text_discrim);
2871 discrim_copy = strdup(text_discrim); /* save a copy of the discrim */
2872 wordc = split_line(discrim_copy, false, ArrayLength(wordv), wordv);
258d1427 2873 alert->discrim = opserv_discrim_create(req, opserv, wordc, wordv, 0);
d76ed9a9 2874 if (!alert->discrim) {
2875 free(alert->text_discrim);
2876 free(discrim_copy);
2877 free(alert);
2878 return NULL;
2879 }
2880 alert->split_discrim = discrim_copy;
2881 name_dup = strdup(name);
2882 if (!alert->discrim->reason)
2883 alert->discrim->reason = strdup(name);
2884 alert->reaction = reaction;
2885 dict_insert(opserv_user_alerts, name_dup, alert);
697f4c9a 2886 /* Stick the alert into the appropriate additional alert dict(s).
2887 * For channel alerts, we only use channels and min_channels;
2888 * max_channels would have to be checked on /part, which we do not
2889 * yet do, and which seems of questionable value.
2890 */
2891 if (alert->discrim->channel || alert->discrim->min_channels)
d76ed9a9 2892 dict_insert(opserv_channel_alerts, name_dup, alert);
697f4c9a 2893 if (alert->discrim->mask_nick)
d76ed9a9 2894 dict_insert(opserv_nick_based_alerts, name_dup, alert);
2895 return alert;
2896}
2897
de9510bc 2898/*
d76ed9a9 2899static int
2900add_chan_warn(const char *key, void *data, UNUSED_ARG(void *extra))
2901{
2902 struct record_data *rd = data;
2903 char *reason = GET_RECORD_QSTRING(rd);
2904
de9510bc 2905 * i hope this can't happen *
d76ed9a9 2906 if (!reason)
2907 reason = "No Reason";
2908
2909 dict_insert(opserv_chan_warn, strdup(key), strdup(reason));
2910 return 0;
2911}
de9510bc 2912*/
d76ed9a9 2913
2914static int
2915add_user_alert(const char *key, void *data, UNUSED_ARG(void *extra))
2916{
2917 dict_t alert_dict;
2918 const char *discrim, *react, *owner;
2919 opserv_alert_reaction reaction;
2920 struct opserv_user_alert *alert;
2921
2922 if (!(alert_dict = GET_RECORD_OBJECT((struct record_data *)data))) {
2923 log_module(OS_LOG, LOG_ERROR, "Bad type (not a record) for alert %s.", key);
2924 return 1;
2925 }
2926 discrim = database_get_data(alert_dict, KEY_DISCRIM, RECDB_QSTRING);
2927 react = database_get_data(alert_dict, KEY_REACTION, RECDB_QSTRING);
2928 if (!react || !irccasecmp(react, "notice"))
2929 reaction = REACT_NOTICE;
2930 else if (!irccasecmp(react, "kill"))
2931 reaction = REACT_KILL;
9a75756e 2932 else if (!irccasecmp(react, "silent"))
2933 reaction = REACT_SILENT;
d76ed9a9 2934 else if (!irccasecmp(react, "gline"))
2935 reaction = REACT_GLINE;
d914d1cb 2936 else if (!irccasecmp(react, "shun"))
2937 reaction = REACT_SHUN;
d76ed9a9 2938 else {
2939 log_module(OS_LOG, LOG_ERROR, "Invalid reaction %s for alert %s.", react, key);
2940 return 0;
2941 }
2942 alert = opserv_add_user_alert(opserv, key, reaction, discrim);
2943 if (!alert) {
2944 log_module(OS_LOG, LOG_ERROR, "Unable to create alert %s from database.", key);
2945 return 0;
2946 }
2947 owner = database_get_data(alert_dict, KEY_OWNER, RECDB_QSTRING);
2948 free(alert->owner);
2949 alert->owner = strdup(owner ? owner : "<unknown>");
2950 return 0;
2951}
2952
2953static int
2954trusted_host_read(const char *host, void *data, UNUSED_ARG(void *extra))
2955{
2956 struct record_data *rd = data;
2957 const char *limit, *str, *reason, *issuer;
2958 time_t issued, expires;
2959
2960 if (rd->type == RECDB_QSTRING) {
2961 /* old style host by itself */
2962 limit = GET_RECORD_QSTRING(rd);
2963 issued = 0;
2964 issuer = NULL;
2965 expires = 0;
2966 reason = NULL;
2967 } else if (rd->type == RECDB_OBJECT) {
2968 dict_t obj = GET_RECORD_OBJECT(rd);
2969 /* new style structure */
2970 limit = database_get_data(obj, KEY_LIMIT, RECDB_QSTRING);
2971 str = database_get_data(obj, KEY_EXPIRES, RECDB_QSTRING);
2972 expires = str ? ParseInterval(str) : 0;
2973 reason = database_get_data(obj, KEY_REASON, RECDB_QSTRING);
2974 issuer = database_get_data(obj, KEY_ISSUER, RECDB_QSTRING);
2975 str = database_get_data(obj, KEY_ISSUED, RECDB_QSTRING);
2976 issued = str ? ParseInterval(str) : 0;
2977 } else
2978 return 0;
2979
2980 if (expires && (expires < now))
2981 return 0;
2982 opserv_add_trusted_host(host, (limit ? strtoul(limit, NULL, 0) : 0), issuer, issued, expires, reason);
2983 return 0;
2984}
2985
2986static int
2987opserv_saxdb_read(struct dict *conf_db)
2988{
2989 dict_t object;
2990 struct record_data *rd;
2991 dict_iterator_t it;
2992 unsigned int nn;
2993
2994 if ((object = database_get_data(conf_db, KEY_RESERVES, RECDB_OBJECT)))
2995 dict_foreach(object, add_reserved, opserv_reserved_nick_dict);
2996 if ((rd = database_get_path(conf_db, KEY_BAD_WORDS))) {
2997 switch (rd->type) {
2998 case RECDB_STRING_LIST:
2999 /* Add words one by one just in case there are overlaps from an old DB. */
3000 for (nn=0; nn<rd->d.slist->used; ++nn)
3001 opserv_add_bad_word(NULL, NULL, rd->d.slist->list[nn]);
3002 break;
3003 case RECDB_OBJECT:
3004 for (it=dict_first(rd->d.object); it; it=iter_next(it)) {
3005 opserv_add_bad_word(NULL, NULL, iter_key(it));
3006 rd = iter_data(it);
3007 if (rd->type == RECDB_STRING_LIST)
3008 for (nn=0; nn<rd->d.slist->used; nn++)
3009 dict_insert(opserv_exempt_channels, strdup(rd->d.slist->list[nn]), NULL);
3010 }
3011 break;
3012 default:
3013 /* do nothing */;
3014 }
3015 }
3016 if ((rd = database_get_path(conf_db, KEY_EXEMPT_CHANNELS))
3017 && (rd->type == RECDB_STRING_LIST)) {
3018 for (nn=0; nn<rd->d.slist->used; ++nn)
3019 dict_insert(opserv_exempt_channels, strdup(rd->d.slist->list[nn]), NULL);
3020 }
3021 if ((object = database_get_data(conf_db, KEY_MAX_CLIENTS, RECDB_OBJECT))) {
3022 char *str;
3023 if ((str = database_get_data(object, KEY_MAX, RECDB_QSTRING)))
3024 max_clients = atoi(str);
3025 if ((str = database_get_data(object, KEY_TIME, RECDB_QSTRING)))
3026 max_clients_time = atoi(str);
3027 }
3028 if ((object = database_get_data(conf_db, KEY_TRUSTED_HOSTS, RECDB_OBJECT)))
3029 dict_foreach(object, trusted_host_read, opserv_trusted_hosts);
3030 if ((object = database_get_data(conf_db, KEY_GAGS, RECDB_OBJECT)))
3031 dict_foreach(object, add_gag_helper, NULL);
3032 if ((object = database_get_data(conf_db, KEY_ALERTS, RECDB_OBJECT)))
3033 dict_foreach(object, add_user_alert, NULL);
de9510bc 3034/*
d76ed9a9 3035 if ((object = database_get_data(conf_db, KEY_WARN, RECDB_OBJECT)))
3036 dict_foreach(object, add_chan_warn, NULL);
de9510bc 3037*/
d76ed9a9 3038 return 0;
3039}
3040
3041static int
3042opserv_saxdb_write(struct saxdb_context *ctx)
3043{
3044 struct string_list *slist;
3045 dict_iterator_t it;
3046
3047 /* reserved nicks */
3048 if (dict_size(opserv_reserved_nick_dict)) {
3049 saxdb_start_record(ctx, KEY_RESERVES, 1);
3050 for (it = dict_first(opserv_reserved_nick_dict); it; it = iter_next(it)) {
3051 struct userNode *user = iter_data(it);
3052 if (!IsPersistent(user)) continue;
3053 saxdb_start_record(ctx, iter_key(it), 0);
3054 saxdb_write_string(ctx, KEY_IDENT, user->ident);
3055 saxdb_write_string(ctx, KEY_HOSTNAME, user->hostname);
3056 saxdb_write_string(ctx, KEY_DESC, user->info);
3057 saxdb_end_record(ctx);
3058 }
3059 saxdb_end_record(ctx);
3060 }
3061 /* bad word set */
3062 if (opserv_bad_words->used) {
3063 saxdb_write_string_list(ctx, KEY_BAD_WORDS, opserv_bad_words);
3064 }
3065 /* insert exempt channel names */
3066 if (dict_size(opserv_exempt_channels)) {
3067 slist = alloc_string_list(dict_size(opserv_exempt_channels));
3068 for (it=dict_first(opserv_exempt_channels); it; it=iter_next(it)) {
3069 string_list_append(slist, strdup(iter_key(it)));
3070 }
3071 saxdb_write_string_list(ctx, KEY_EXEMPT_CHANNELS, slist);
3072 free_string_list(slist);
3073 }
3074 /* trusted hosts takes a little more work */
3075 if (dict_size(opserv_trusted_hosts)) {
3076 saxdb_start_record(ctx, KEY_TRUSTED_HOSTS, 1);
3077 for (it = dict_first(opserv_trusted_hosts); it; it = iter_next(it)) {
3078 struct trusted_host *th = iter_data(it);
3079 saxdb_start_record(ctx, iter_key(it), 0);
3080 if (th->limit) saxdb_write_int(ctx, KEY_LIMIT, th->limit);
3081 if (th->expires) saxdb_write_int(ctx, KEY_EXPIRES, th->expires);
3082 if (th->issued) saxdb_write_int(ctx, KEY_ISSUED, th->issued);
3083 if (th->issuer) saxdb_write_string(ctx, KEY_ISSUER, th->issuer);
3084 if (th->reason) saxdb_write_string(ctx, KEY_REASON, th->reason);
3085 saxdb_end_record(ctx);
3086 }
3087 saxdb_end_record(ctx);
3088 }
3089 /* gags */
3090 if (gagList) {
3091 struct gag_entry *gag;
3092 saxdb_start_record(ctx, KEY_GAGS, 1);
3093 for (gag = gagList; gag; gag = gag->next) {
3094 saxdb_start_record(ctx, gag->mask, 0);
3095 saxdb_write_string(ctx, KEY_OWNER, gag->owner);
3096 saxdb_write_string(ctx, KEY_REASON, gag->reason);
3097 if (gag->expires) saxdb_write_int(ctx, KEY_EXPIRES, gag->expires);
3098 saxdb_end_record(ctx);
3099 }
3100 saxdb_end_record(ctx);
3101 }
3102 /* channel warnings */
de9510bc 3103 /*
d76ed9a9 3104 if (dict_size(opserv_chan_warn)) {
3105 saxdb_start_record(ctx, KEY_WARN, 0);
3106 for (it = dict_first(opserv_chan_warn); it; it = iter_next(it)) {
3107 saxdb_write_string(ctx, iter_key(it), iter_data(it));
3108 }
3109 saxdb_end_record(ctx);
3110 }
de9510bc 3111 */
d76ed9a9 3112 /* alerts */
3113 if (dict_size(opserv_user_alerts)) {
3114 saxdb_start_record(ctx, KEY_ALERTS, 1);
3115 for (it = dict_first(opserv_user_alerts); it; it = iter_next(it)) {
3116 struct opserv_user_alert *alert = iter_data(it);
3117 const char *reaction;
3118 saxdb_start_record(ctx, iter_key(it), 0);
3119 saxdb_write_string(ctx, KEY_DISCRIM, alert->text_discrim);
3120 saxdb_write_string(ctx, KEY_OWNER, alert->owner);
3121 switch (alert->reaction) {
3122 case REACT_NOTICE: reaction = "notice"; break;
3123 case REACT_KILL: reaction = "kill"; break;
9a75756e 3124 case REACT_SILENT: reaction = "silent"; break;
d76ed9a9 3125 case REACT_GLINE: reaction = "gline"; break;
d914d1cb 3126 case REACT_SHUN: reaction = "shun"; break;
d76ed9a9 3127 default:
3128 reaction = NULL;
3129 log_module(OS_LOG, LOG_ERROR, "Invalid reaction type %d for alert %s (while writing database).", alert->reaction, iter_key(it));
3130 break;
3131 }
3132 if (reaction) saxdb_write_string(ctx, KEY_REACTION, reaction);
3133 saxdb_end_record(ctx);
3134 }
3135 saxdb_end_record(ctx);
3136 }
3137 /* max clients */
3138 saxdb_start_record(ctx, KEY_MAX_CLIENTS, 0);
3139 saxdb_write_int(ctx, KEY_MAX, max_clients);
3140 saxdb_write_int(ctx, KEY_TIME, max_clients_time);
3141 saxdb_end_record(ctx);
3142 return 0;
3143}
3144
3145static int
3146query_keys_helper(const char *key, UNUSED_ARG(void *data), void *extra)
3147{
3148 send_message_type(4, extra, opserv, "$b%s$b", key);
3149 return 0;
3150}
3151
3152static MODCMD_FUNC(cmd_query)
3153{
3154 struct record_data *rd;
3155 unsigned int i;
3156 char *nodename;
3157
3158 if (argc < 2) {
258d1427 3159 reply("OSMSG_OPTION_ROOT");
3160 conf_enum_root(query_keys_helper, user);
3161 return 1;
d76ed9a9 3162 }
3163
3164 nodename = unsplit_string(argv+1, argc-1, NULL);
3165 if (!(rd = conf_get_node(nodename))) {
258d1427 3166 reply("OSMSG_UNKNOWN_OPTION", nodename);
3167 return 0;
d76ed9a9 3168 }
3169
3170 if (rd->type == RECDB_QSTRING)
258d1427 3171 reply("OSMSG_OPTION_IS", nodename, rd->d.qstring);
d76ed9a9 3172 else if (rd->type == RECDB_STRING_LIST) {
258d1427 3173 reply("OSMSG_OPTION_LIST", nodename);
3174 if (rd->d.slist->used)
3175 for (i=0; i<rd->d.slist->used; i++)
3176 send_message_type(4, user, cmd->parent->bot, "$b%s$b", rd->d.slist->list[i]);
3177 else
3178 reply("OSMSG_OPTION_LIST_EMPTY");
d76ed9a9 3179 } else if (rd->type == RECDB_OBJECT) {
258d1427 3180 reply("OSMSG_OPTION_KEYS", nodename);
3181 dict_foreach(rd->d.object, query_keys_helper, user);
d76ed9a9 3182 }
3183
3184 return 1;
3185}
3186
3187static MODCMD_FUNC(cmd_set)
3188{
3189 struct record_data *rd;
3190
3191 /* I originally wanted to be able to fully manipulate the config
3192 db with this, but i wussed out. feel free to fix this - you'll
3193 need to handle quoted strings which have been split, and likely
3194 invent a syntax for it. -Zoot */
3195
3196 if (!(rd = conf_get_node(argv[1]))) {
258d1427 3197 reply("OSMSG_SET_NOT_SET", argv[1]);
3198 return 0;
d76ed9a9 3199 }
3200
3201 if (rd->type != RECDB_QSTRING) {
258d1427 3202 reply("OSMSG_SET_BAD_TYPE", argv[1]);
3203 return 0;
d76ed9a9 3204 }
3205
3206 free(rd->d.qstring);
3207 rd->d.qstring = strdup(argv[2]);
3208 conf_call_reload_funcs();
3209 reply("OSMSG_SET_SUCCESS", argv[1], argv[2]);
3210 return 1;
3211}
3212
3213static MODCMD_FUNC(cmd_settime)
3214{
3215 const char *srv_name_mask = "*";
3216 time_t new_time = now;
3217
3218 if (argc > 1)
3219 srv_name_mask = argv[1];
3220 if (argc > 2)
3221 new_time = time(NULL);
3222 irc_settime(srv_name_mask, new_time);
3223 reply("OSMSG_SETTIME_SUCCESS", srv_name_mask);
3224 return 1;
3225}
3226
3227static discrim_t
258d1427 3228opserv_discrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[], int allow_channel)
d76ed9a9 3229{
3230 unsigned int i, j;
3231 discrim_t discrim;
3232
3233 discrim = calloc(1, sizeof(*discrim));
3234 discrim->limit = 250;
3235 discrim->max_level = ~0;
3236 discrim->max_ts = INT_MAX;
3237 discrim->domain_depth = 2;
3238 discrim->max_channels = INT_MAX;
3239 discrim->authed = -1;
3240 discrim->info_space = -1;
63665495 3241 discrim->intra_dcmp = 0;
3242 discrim->intra_scmp = 0;
d76ed9a9 3243
3244 for (i=0; i<argc; i++) {
3245 if (irccasecmp(argv[i], "log") == 0) {
3246 discrim->option_log = 1;
3247 continue;
3248 }
3249 /* Assume all other criteria require arguments. */
3250 if (i == argc - 1) {
258d1427 3251 send_message(user, bot, "MSG_MISSING_PARAMS", argv[i]);
d76ed9a9 3252 goto fail;
3253 }
63665495 3254 if (argv[i+1][0] == '&') {
3255 /* Looking for intra-userNode matches */
3256 char *tmp = &(argv[i+1][1]);
3257 if (strcasecmp(tmp, argv[i]) != 0) { /* Don't allow "nick &nick" etc */
3258 if (!strcasecmp(tmp, "nick"))
3259 discrim->intra_dcmp = 1;
3260 else if (!strcasecmp(tmp, "ident"))
3261 discrim->intra_dcmp = 2;
3262 else if (!strcasecmp(tmp, "info"))
3263 discrim->intra_dcmp = 3;
3264 }
3265 }
258d1427 3266 if (irccasecmp(argv[i], "mask") == 0) {
3267 if (!is_ircmask(argv[++i])) {
3268 send_message(user, bot, "OSMSG_INVALID_IRCMASK", argv[i]);
3269 goto fail;
3270 }
3271 if (!split_ircmask(argv[i],
d76ed9a9 3272 &discrim->mask_nick,
3273 &discrim->mask_ident,
3274 &discrim->mask_host)) {
258d1427 3275 send_message(user, bot, "OSMSG_INVALID_IRCMASK", argv[i]);
3276 goto fail;
3277 }
3278 } else if (irccasecmp(argv[i], "nick") == 0) {
63665495 3279 i++;
3280 if (discrim->intra_dcmp > 0)
3281 discrim->intra_scmp = 1;
3282 else
3283 discrim->mask_nick = argv[i];
258d1427 3284 } else if (irccasecmp(argv[i], "ident") == 0) {
63665495 3285 i++;
3286 if (discrim->intra_dcmp > 0)
3287 discrim->intra_scmp = 2;
3288 else
3289 discrim->mask_ident = argv[i];
258d1427 3290 } else if (irccasecmp(argv[i], "host") == 0) {
3291 discrim->mask_host = argv[++i];
3292 } else if (irccasecmp(argv[i], "info") == 0) {
63665495 3293 i++;
3294 if (discrim->intra_dcmp > 0)
3295 discrim->intra_scmp = 3;
3296 else
3297 discrim->mask_info = argv[i];
258d1427 3298 } else if (irccasecmp(argv[i], "server") == 0) {
3299 discrim->server = argv[++i];
3300 } else if (irccasecmp(argv[i], "ip") == 0) {
2f61d1d7 3301 j = irc_pton(&discrim->ip_mask, &discrim->ip_mask_bits, argv[++i]);
3302 if (!j) {
3303 send_message(user, opserv, "OSMSG_BAD_IP", argv[i]);
3304 goto fail;
3305 }
d76ed9a9 3306 } else if (irccasecmp(argv[i], "account") == 0) {
3307 if (discrim->authed == 0) {
258d1427 3308 send_message(user, bot, "OSMSG_ACCOUNTMASK_AUTHED");
d76ed9a9 3309 goto fail;
3310 }
3311 discrim->accountmask = argv[++i];
3312 discrim->authed = 1;
3313 } else if (irccasecmp(argv[i], "authed") == 0) {
3314 i++; /* true_string and false_string are macros! */
3315 if (true_string(argv[i])) {
3316 discrim->authed = 1;
3317 } else if (false_string(argv[i])) {
3318 if (discrim->accountmask) {
258d1427 3319 send_message(user, bot, "OSMSG_ACCOUNTMASK_AUTHED");
d76ed9a9 3320 goto fail;
3321 }
3322 discrim->authed = 0;
3323 } else {
258d1427 3324 send_message(user, bot, "MSG_INVALID_BINARY", argv[i]);
d76ed9a9 3325 goto fail;
3326 }
3327 } else if (irccasecmp(argv[i], "info_space") == 0) {
3328 /* XXX: A hack because you can't check explicitly for a space through
3329 * any other means */
3330 i++;
3331 if (true_string(argv[i])) {
3332 discrim->info_space = 1;
3333 } else if (false_string(argv[i])) {
3334 discrim->info_space = 0;
3335 } else {
258d1427 3336 send_message(user, bot, "MSG_INVALID_BINARY", argv[i]);
d76ed9a9 3337 goto fail;
3338 }
27eaa617 3339 } else if (irccasecmp(argv[i], "regex") == 0) {
3340 i++;
3341 if (true_string(argv[i])) {
3342 discrim->use_regex = 1;
3343 } else if (false_string(argv[i])) {
3344 discrim->use_regex = 0;
3345 } else {
3346 send_message(user, opserv, "MSG_INVALID_BINARY", argv[i]);
3347 goto fail;
3348 }
d76ed9a9 3349 } else if (irccasecmp(argv[i], "duration") == 0) {
3350 discrim->duration = ParseInterval(argv[++i]);
258d1427 3351 } else if (irccasecmp(argv[i], "channel") == 0) {
d76ed9a9 3352 for (j=0, i++; ; j++) {
3353 switch (argv[i][j]) {
3354 case '#':
3355 goto find_channel;
3356 case '-':
55342ce8 3357 discrim->chan_no_modes |= MODE_CHANOP | MODE_HALFOP | MODE_VOICE;
d76ed9a9 3358 break;
3359 case '+':
3360 discrim->chan_req_modes |= MODE_VOICE;
3361 discrim->chan_no_modes |= MODE_CHANOP;
55342ce8 3362 discrim->chan_no_modes |= MODE_HALFOP;
3363 break;
3364 case '%':
3365 discrim->chan_req_modes |= MODE_HALFOP;
3366 discrim->chan_no_modes |= MODE_CHANOP;
3367 discrim->chan_no_modes |= MODE_VOICE;
d76ed9a9 3368 break;
3369 case '@':
3370 discrim->chan_req_modes |= MODE_CHANOP;
3371 break;
3372 case '\0':
258d1427 3373 send_message(user, bot, "MSG_NOT_CHANNEL_NAME");
d76ed9a9 3374 goto fail;
3375 }
3376 }
3377 find_channel:
3378 discrim->chan_no_modes &= ~discrim->chan_req_modes;
258d1427 3379 if (!(discrim->channel = GetChannel(argv[i]+j))) {
d76ed9a9 3380 /* secretly "allow_channel" now means "if a channel name is
3381 * specified, require that it currently exist" */
3382 if (allow_channel) {
258d1427 3383 send_message(user, bot, "MSG_CHANNEL_UNKNOWN", argv[i]);
d76ed9a9 3384 goto fail;
3385 } else {
2aef5f4b 3386 discrim->channel = AddChannel(argv[i]+j, now, NULL, NULL, NULL);
d76ed9a9 3387 }
258d1427 3388 }
d76ed9a9 3389 LockChannel(discrim->channel);
3390 } else if (irccasecmp(argv[i], "numchannels") == 0) {
3391 discrim->min_channels = discrim->max_channels = strtoul(argv[++i], NULL, 10);
258d1427 3392 } else if (irccasecmp(argv[i], "limit") == 0) {
3393 discrim->limit = strtoul(argv[++i], NULL, 10);
d76ed9a9 3394 } else if (irccasecmp(argv[i], "reason") == 0) {
3395 discrim->reason = strdup(unsplit_string(argv+i+1, argc-i-1, NULL));
3396 i = argc;
3397 } else if (irccasecmp(argv[i], "last") == 0) {
3398 discrim->min_ts = now - ParseInterval(argv[++i]);
3399 } else if ((irccasecmp(argv[i], "linked") == 0)
3400 || (irccasecmp(argv[i], "nickage") == 0)) {
3401 const char *cmp = argv[++i];
3402 if (cmp[0] == '<') {
3403 if (cmp[1] == '=') {
3404 discrim->min_ts = now - ParseInterval(cmp+2);
3405 } else {
3406 discrim->min_ts = now - (ParseInterval(cmp+1) - 1);
3407 }
3408 } else if (cmp[0] == '>') {
3409 if (cmp[1] == '=') {
3410 discrim->max_ts = now - ParseInterval(cmp+2);
3411 } else {
3412 discrim->max_ts = now - (ParseInterval(cmp+1) - 1);
3413 }
3414 } else {
3415 discrim->min_ts = now - ParseInterval(cmp+2);
3416 }
3417 } else if (irccasecmp(argv[i], "access") == 0) {
3418 const char *cmp = argv[++i];
3419 if (cmp[0] == '<') {
3420 if (discrim->min_level == 0) discrim->min_level = 1;
3421 if (cmp[1] == '=') {
3422 discrim->max_level = strtoul(cmp+2, NULL, 0);
3423 } else {
3424 discrim->max_level = strtoul(cmp+1, NULL, 0) - 1;
3425 }
3426 } else if (cmp[0] == '=') {
3427 discrim->min_level = discrim->max_level = strtoul(cmp+1, NULL, 0);
3428 } else if (cmp[0] == '>') {
3429 if (cmp[1] == '=') {
3430 discrim->min_level = strtoul(cmp+2, NULL, 0);
3431 } else {
3432 discrim->min_level = strtoul(cmp+1, NULL, 0) + 1;
3433 }
3434 } else {
3435 discrim->min_level = strtoul(cmp+2, NULL, 0);
3436 }
3437 } else if ((irccasecmp(argv[i], "abuse") == 0)
3438 && (irccasecmp(argv[++i], "opers") == 0)) {
3439 discrim->match_opers = 1;
3440 } else if (irccasecmp(argv[i], "depth") == 0) {
3441 discrim->domain_depth = strtoul(argv[++i], NULL, 0);
3442 } else if (irccasecmp(argv[i], "clones") == 0) {
3443 discrim->min_clones = strtoul(argv[++i], NULL, 0);
3444 } else {
258d1427 3445 send_message(user, bot, "MSG_INVALID_CRITERIA", argv[i]);
d76ed9a9 3446 goto fail;
3447 }
3448 }
3449
3450 if (discrim->mask_nick && !strcmp(discrim->mask_nick, "*")) {
258d1427 3451 discrim->mask_nick = 0;
d76ed9a9 3452 }
3453 if (discrim->mask_ident && !strcmp(discrim->mask_ident, "*")) {
3454 discrim->mask_ident = 0;
3455 }
3456 if (discrim->mask_info && !strcmp(discrim->mask_info, "*")) {
258d1427 3457 discrim->mask_info = 0;
d76ed9a9 3458 }
3459 if (discrim->mask_host && !discrim->mask_host[strspn(discrim->mask_host, "*.")]) {
3460 discrim->mask_host = 0;
3461 }
27eaa617 3462
3463 if(discrim->use_regex)
3464 {
3465 if(discrim->mask_nick)
3466 {
3467 int err = regcomp(&discrim->regex_nick, discrim->mask_nick, REG_EXTENDED|REG_ICASE|REG_NOSUB);
3468 discrim->has_regex_nick = !err;
3469 if(err)
3470 {
3471 char buff[256];
3472 buff[regerror(err, &discrim->regex_nick, buff, sizeof(buff))] = 0;
3473
3474 send_message(user, opserv, "OSMSG_INVALID_REGEX", discrim->mask_nick, buff, err);
3475 goto regfail;
3476 }
3477 }
3478
3479 if(discrim->mask_ident)
3480 {
3481 int err = regcomp(&discrim->regex_ident, discrim->mask_ident, REG_EXTENDED|REG_ICASE|REG_NOSUB);
3482 discrim->has_regex_ident = !err;
3483 if(err)
3484 {
3485 char buff[256];
3486 buff[regerror(err, &discrim->regex_ident, buff, sizeof(buff))] = 0;
3487
3488 send_message(user, opserv, "OSMSG_INVALID_REGEX", discrim->mask_ident, buff, err);
3489 goto regfail;
3490 }
3491 }
3492
3493 if(discrim->mask_host)
3494 {
3495 int err = regcomp(&discrim->regex_host, discrim->mask_host, REG_EXTENDED|REG_ICASE|REG_NOSUB);
3496 discrim->has_regex_host = !err;
3497 if(err)
3498 {
3499 char buff[256];
3500 buff[regerror(err, &discrim->regex_host, buff, sizeof(buff))] = 0;
3501
3502 send_message(user, opserv, "OSMSG_INVALID_REGEX", discrim->mask_host, buff, err);
3503 goto regfail;
3504 }
3505 }
3506
3507 if(discrim->mask_info)
3508 {
3509 int err = regcomp(&discrim->regex_info, discrim->mask_info, REG_EXTENDED|REG_ICASE|REG_NOSUB);
3510 discrim->has_regex_info = !err;
3511 if(err)
3512 {
3513 char buff[256];
3514 buff[regerror(err, &discrim->regex_info, buff, sizeof(buff))] = 0;
3515
3516 send_message(user, opserv, "OSMSG_INVALID_REGEX", discrim->mask_info, buff, err);
3517 goto regfail;
3518 }
3519 }
3520 }
3521
d76ed9a9 3522 return discrim;
27eaa617 3523
d76ed9a9 3524 fail:
3525 free(discrim);
3526 return NULL;
27eaa617 3527
3528 regfail:
3529 if(discrim->has_regex_nick)
3530 regfree(&discrim->regex_nick);
3531 if(discrim->has_regex_ident)
3532 regfree(&discrim->regex_ident);
3533 if(discrim->has_regex_host)
3534 regfree(&discrim->regex_host);
3535 if(discrim->has_regex_info)
3536 regfree(&discrim->regex_info);
3537
3538 free(discrim);
3539 return NULL;
d76ed9a9 3540}
3541
3542static int
3543discrim_match(discrim_t discrim, struct userNode *user)
3544{
3545 unsigned int access;
63665495 3546 char *scmp=NULL, *dcmp=NULL;
d76ed9a9 3547
3548 if ((user->timestamp < discrim->min_ts)
3549 || (user->timestamp > discrim->max_ts)
3550 || (user->channels.used < discrim->min_channels)
3551 || (user->channels.used > discrim->max_channels)
3552 || (discrim->authed == 0 && user->handle_info)
3553 || (discrim->authed == 1 && !user->handle_info)
3554 || (discrim->info_space == 0 && user->info[0] == ' ')
3555 || (discrim->info_space == 1 && user->info[0] != ' ')
d76ed9a9 3556 || (discrim->server && !match_ircglob(user->uplink->name, discrim->server))
3557 || (discrim->accountmask && (!user->handle_info || !match_ircglob(user->handle_info->handle, discrim->accountmask)))
2f61d1d7 3558 || (discrim->ip_mask_bits && !irc_check_mask(&user->ip, &discrim->ip_mask, discrim->ip_mask_bits))
3559 )
3560 return 0;
3561
3562 if (discrim->channel && !GetUserMode(discrim->channel, user))
d76ed9a9 3563 return 0;
27eaa617 3564
3565 if(discrim->use_regex)
3566 {
3567 if((discrim->has_regex_nick && regexec(&discrim->regex_nick, user->nick, 0, 0, 0))
3568 || (discrim->has_regex_ident && regexec(&discrim->regex_ident, user->ident, 0, 0, 0))
3569 || (discrim->has_regex_host && regexec(&discrim->regex_host, user->hostname, 0, 0, 0))
3570 || (discrim->has_regex_info && regexec(&discrim->regex_info, user->info, 0, 0, 0))) {
3571 return 0;
3572 }
3573 }
3574 else
3575 {
3576 if ((discrim->mask_nick && !match_ircglob(user->nick, discrim->mask_nick))
3577 || (discrim->mask_ident && !match_ircglob(user->ident, discrim->mask_ident))
3578 || (discrim->mask_host && !match_ircglob(user->hostname, discrim->mask_host))
3579 || (discrim->mask_info && !match_ircglob(user->info, discrim->mask_info))) {
3580 return 0;
3581 }
3582 }
3583
63665495 3584 if ((discrim->intra_scmp > 0 && discrim->intra_dcmp > 0)) {
3585 switch(discrim->intra_scmp) {
3586 case 1: scmp=user->nick; break;
3587 case 2: scmp=user->ident; break;
3588 case 3:
3589 scmp=user->info;
3590 if (discrim->info_space == 1) scmp++;
3591 break;
3592 }
3593 switch(discrim->intra_dcmp) {
3594 case 1: dcmp=user->nick; break;
3595 case 2: dcmp=user->ident; break;
3596 case 3: /* When checking INFO, and info_space is enabled
3597 * ignore the first character in a search
3598 * XXX: Should we ignore ALL leading whitespace?
3599 * Also, what about ignoring ~ in ident?
3600 */
3601 dcmp=user->info;
3602 if (discrim->info_space == 1) dcmp++;
3603 break;
3604 }
3605 if (irccasecmp(scmp,dcmp))
3606 return 0;
3607 }
3608
d76ed9a9 3609 access = user->handle_info ? user->handle_info->opserv_level : 0;
3610 if ((access < discrim->min_level)
3611 || (access > discrim->max_level)) {
3612 return 0;
3613 }
d76ed9a9 3614 if (discrim->min_clones > 1) {
2f61d1d7 3615 struct opserv_hostinfo *ohi = dict_find(opserv_hostinfo_dict, irc_ntoa(&user->ip), NULL);
3616 if (!ohi || (ohi->clients.used < discrim->min_clones))
3617 return 0;
d76ed9a9 3618 }
3619 return 1;
3620}
3621
3622static unsigned int
3623opserv_discrim_search(discrim_t discrim, discrim_search_func dsf, void *data)
3624{
3625 unsigned int nn, count;
3626 struct userList matched;
3627
3628 userList_init(&matched);
3629 /* Try most optimized search methods first */
3630 if (discrim->channel) {
3631 for (nn=0;
3632 (nn < discrim->channel->members.used)
3633 && (matched.used < discrim->limit);
3634 nn++) {
3635 struct modeNode *mn = discrim->channel->members.list[nn];
3636 if (((mn->modes & discrim->chan_req_modes) != discrim->chan_req_modes)
3637 || ((mn->modes & discrim->chan_no_modes) != 0)) {
3638 continue;
3639 }
3640 if (discrim_match(discrim, mn->user)) {
3641 userList_append(&matched, mn->user);
3642 }
3643 }
2f61d1d7 3644 } else if (discrim->ip_mask_bits == 128) {
3645 struct opserv_hostinfo *ohi = dict_find(opserv_hostinfo_dict, irc_ntoa(&discrim->ip_mask), NULL);
d76ed9a9 3646 if (!ohi) {
3647 userList_clean(&matched);
3648 return 0;
3649 }
3650 for (nn=0; (nn<ohi->clients.used) && (matched.used < discrim->limit); nn++) {
3651 if (discrim_match(discrim, ohi->clients.list[nn])) {
3652 userList_append(&matched, ohi->clients.list[nn]);
3653 }
3654 }
3655 } else {
3656 dict_iterator_t it;
3657 for (it=dict_first(clients); it && (matched.used < discrim->limit); it=iter_next(it)) {
3658 if (discrim_match(discrim, iter_data(it))) {
3659 userList_append(&matched, iter_data(it));
3660 }
3661 }
3662 }
3663
3664 if (!matched.used) {
3665 userList_clean(&matched);
3666 return 0;
3667 }
3668
3669 if (discrim->option_log) {
3670 log_module(OS_LOG, LOG_INFO, "Logging matches for search:");
3671 }
3672 for (nn=0; nn<matched.used; nn++) {
3673 struct userNode *user = matched.list[nn];
3674 if (discrim->option_log) {
3675 log_module(OS_LOG, LOG_INFO, " %s!%s@%s", user->nick, user->ident, user->hostname);
3676 }
3677 if (dsf(user, data)) {
258d1427 3678 /* If a search function returns true, it ran into a
3679 problem. Stop going through the list. */
3680 break;
3681 }
d76ed9a9 3682 }
3683 if (discrim->option_log) {
3684 log_module(OS_LOG, LOG_INFO, "End of matching users.");
3685 }
3686 count = matched.used;
3687 userList_clean(&matched);
3688 return count;
3689}
3690
3691static int
3692trace_print_func(struct userNode *match, void *extra)
3693{
3694 struct discrim_and_source *das = extra;
3695 if (match->handle_info) {
258d1427 3696 send_message_type(4, das->source, das->destination, "%-15s\002 \002%10s\002@\002%s (%s)", match->nick, match->ident, match->hostname, match->handle_info->handle);
d76ed9a9 3697 } else {
258d1427 3698 send_message_type(4, das->source, das->destination, "%-15s\002 \002%10s\002@\002%s", match->nick, match->ident, match->hostname);
d76ed9a9 3699 }
3700 return 0;
3701}
3702
3703static int
3704trace_count_func(UNUSED_ARG(struct userNode *match), UNUSED_ARG(void *extra))
3705{
3706 return 0;
3707}
3708
3709static int
3710is_oper_victim(struct userNode *user, struct userNode *target, int match_opers)
3711{
3712 return !(IsService(target)
3713 || (!match_opers && IsOper(target))
3714 || (target->handle_info
3715 && target->handle_info->opserv_level > user->handle_info->opserv_level));
3716}
3717
3718static int
3719trace_gline_func(struct userNode *match, void *extra)
3720{
3721 struct discrim_and_source *das = extra;
3722
3723 if (is_oper_victim(das->source, match, das->discrim->match_opers)) {
9a75756e 3724 opserv_block(match, das->source->handle_info->handle, das->discrim->reason, das->discrim->duration, 0);
d76ed9a9 3725 }
3726
3727 return 0;
3728}
3729
d914d1cb 3730static int
3731trace_shun_func(struct userNode *match, void *extra)
3732{
3733 struct discrim_and_source *das = extra;
3734
3735 if (is_oper_victim(das->source, match, das->discrim->match_opers)) {
3736 opserv_shun(match, das->source->handle_info->handle, das->discrim->reason, das->discrim->duration);
3737 }
3738
3739 return 0;
3740}
3741
d76ed9a9 3742static int
3743trace_kill_func(struct userNode *match, void *extra)
3744{
3745 struct discrim_and_source *das = extra;
3746
3747 if (is_oper_victim(das->source, match, das->discrim->match_opers)) {
258d1427 3748 char *reason;
d76ed9a9 3749 if (das->discrim->reason) {
3750 reason = das->discrim->reason;
3751 } else {
3752 reason = alloca(strlen(OSMSG_KILL_REQUESTED)+strlen(das->source->nick)+1);
3753 sprintf(reason, OSMSG_KILL_REQUESTED, das->source->nick);
3754 }
3755 DelUser(match, opserv, 1, reason);
3756 }
3757
3758 return 0;
3759}
3760
3761static int
3762is_gagged(char *mask)
3763{
3764 struct gag_entry *gag;
3765
3766 for (gag = gagList; gag; gag = gag->next) {
3767 if (match_ircglobs(gag->mask, mask)) return 1;
3768 }
3769 return 0;
3770}
3771
3772static int
3773trace_gag_func(struct userNode *match, void *extra)
3774{
3775 struct discrim_and_source *das = extra;
3776
3777 if (is_oper_victim(das->source, match, das->discrim->match_opers)) {
3778 char *reason, *mask;
3779 int masksize;
3780 if (das->discrim->reason) {
3781 reason = das->discrim->reason;
3782 } else {
3783 reason = alloca(strlen(OSMSG_GAG_REQUESTED)+strlen(das->source->nick)+1);
3784 sprintf(reason, OSMSG_GAG_REQUESTED, das->source->nick);
3785 }
258d1427 3786 masksize = 5+strlen(match->hostname);
3787 mask = alloca(masksize);
d76ed9a9 3788 snprintf(mask, masksize, "*!*@%s", match->hostname);
258d1427 3789 if (!is_gagged(mask)) {
d76ed9a9 3790 gag_create(mask, das->source->handle_info->handle, reason,
3791 das->discrim->duration ? (now + das->discrim->duration) : 0);
3792 }
3793 }
3794
3795 return 0;
3796}
3797
3798static int
3799trace_domains_func(struct userNode *match, void *extra)
3800{
3801 struct discrim_and_source *das = extra;
2f61d1d7 3802 irc_in_addr_t ip;
d76ed9a9 3803 unsigned long *count;
3804 unsigned int depth;
3805 char *hostname;
2f61d1d7 3806 char ipmask[IRC_NTOP_MASK_MAX_SIZE];
d76ed9a9 3807
2f61d1d7 3808 if (irc_pton(&ip, NULL, match->hostname)) {
3809 if (irc_in_addr_is_ipv4(ip)) {
3810 unsigned long matchip = ntohl(ip.in6_32[3]);
3811 /* raw IP address.. use up to first three octets of IP */
3812 switch (das->discrim->domain_depth) {
3813 default:
3814 snprintf(ipmask, sizeof(ipmask), "%lu.%lu.%lu.*", (matchip>>24)&255, (matchip>>16)&255, (matchip>>8)&255);
3815 break;
3816 case 2:
3817 snprintf(ipmask, sizeof(ipmask), "%lu.%lu.*", (matchip>>24)&255, (matchip>>16)&255);
3818 break;
3819 case 1:
3820 snprintf(ipmask, sizeof(ipmask), "%lu.*", (matchip>>24)&255);
3821 break;
3822 }
3823 } else if (irc_in_addr_is_ipv6(ip)) {
3824 switch (das->discrim->domain_depth) {
3825 case 1: depth = 16; goto ipv6_pfx;
3826 case 2: depth = 24; goto ipv6_pfx;
3827 case 3: depth = 32; goto ipv6_pfx;
3828 default: depth = das->discrim->domain_depth;
3829 ipv6_pfx:
3830 irc_ntop_mask(ipmask, sizeof(ipmask), &ip, depth);
3831 }
3832 } else safestrncpy(ipmask, match->hostname, sizeof(ipmask));
3833 ipmask[sizeof(ipmask) - 1] = '\0';
d76ed9a9 3834 hostname = ipmask;
3835 } else {
3836 hostname = match->hostname + strlen(match->hostname);
2f61d1d7 3837 for (depth=das->discrim->domain_depth;
d76ed9a9 3838 depth && (hostname > match->hostname);
3839 depth--) {
3840 hostname--;
3841 while ((hostname > match->hostname) && (*hostname != '.')) hostname--;
3842 }
3843 if (*hostname == '.') hostname++; /* advance past last dot we saw */
3844 }
3845 if (!(count = dict_find(das->dict, hostname, NULL))) {
3846 count = calloc(1, sizeof(*count));
3847 dict_insert(das->dict, strdup(hostname), count);
3848 }
3849 (*count)++;
3850 return 0;
3851}
3852
3853static int
3854opserv_show_hostinfo(const char *key, void *data, void *extra)
3855{
3856 unsigned long *count = data;
3857 struct discrim_and_source *das = extra;
3858
258d1427 3859 send_message_type(4, das->source, das->destination, "%s %lu", key, *count);
d76ed9a9 3860 return !--das->disp_limit;
3861}
3862
3863static MODCMD_FUNC(cmd_trace)
3864{
3865 struct discrim_and_source das;
3866 discrim_search_func action;
3867 unsigned int matches;
3868 struct svccmd *subcmd;
3869 char buf[MAXLEN];
3870
3871 sprintf(buf, "trace %s", argv[1]);
258d1427 3872 if (!(subcmd = dict_find(opserv_service->commands, buf, NULL))) {
3873 reply("OSMSG_BAD_ACTION", argv[1]);
d76ed9a9 3874 return 0;
3875 }
258d1427 3876 if (!svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY))
d76ed9a9 3877 return 0;
3878 if (!irccasecmp(argv[1], "print"))
3879 action = trace_print_func;
3880 else if (!irccasecmp(argv[1], "count"))
3881 action = trace_count_func;
3882 else if (!irccasecmp(argv[1], "domains"))
3883 action = trace_domains_func;
3884 else if (!irccasecmp(argv[1], "gline"))
3885 action = trace_gline_func;
d914d1cb 3886 else if (!irccasecmp(argv[1], "shun"))
3887 action = trace_shun_func;
d76ed9a9 3888 else if (!irccasecmp(argv[1], "kill"))
3889 action = trace_kill_func;
3890 else if (!irccasecmp(argv[1], "gag"))
3891 action = trace_gag_func;
3892 else {
258d1427 3893 reply("OSMSG_BAD_ACTION", argv[1]);
3894 return 0;
d76ed9a9 3895 }
3896
3897 if (user->handle_info->opserv_level < subcmd->min_opserv_level) {
3898 reply("OSMSG_LEVEL_TOO_LOW");
3899 return 0;
3900 }
3901
3902 das.dict = NULL;
3903 das.source = user;
258d1427 3904 das.destination = cmd->parent->bot;
3905 das.discrim = opserv_discrim_create(user, cmd->parent->bot, argc-2, argv+2, 1);
d76ed9a9 3906 if (!das.discrim)
3907 return 0;
3908
3909 if (action == trace_print_func)
de9510bc 3910 {
8e11460f 3911 reply("OSMSG_USER_SEARCH_RESULTS");
de9510bc 3912 reply("OSMSG_USER_SEARCH_BAR");
3913 reply("OSMSG_USER_SEARCH_HEADER");
3914 reply("OSMSG_USER_SEARCH_BAR");
3915 }
d76ed9a9 3916 else if (action == trace_count_func)
258d1427 3917 das.discrim->limit = INT_MAX;
d76ed9a9 3918 else if ((action == trace_gline_func) && !das.discrim->duration)
3919 das.discrim->duration = opserv_conf.block_gline_duration;
d914d1cb 3920 else if ((action == trace_shun_func) && !das.discrim->duration)
3921 das.discrim->duration = opserv_conf.block_shun_duration;
d76ed9a9 3922 else if (action == trace_domains_func) {
3923 das.dict = dict_new();
3924 dict_set_free_data(das.dict, free);
3925 dict_set_free_keys(das.dict, free);
3926 das.disp_limit = das.discrim->limit;
3927 das.discrim->limit = INT_MAX;
3928 }
3929 matches = opserv_discrim_search(das.discrim, action, &das);
3930
3931 if (action == trace_domains_func)
3932 dict_foreach(das.dict, opserv_show_hostinfo, &das);
3933
3934 if (matches)
8e11460f 3935 {
3936 if(action == trace_print_func)
258d1427 3937 reply("OSMSG_USER_SEARCH_COUNT_BAR", matches);
8e11460f 3938 else
3939 reply("OSMSG_USER_SEARCH_COUNT", matches);
3940 }
d76ed9a9 3941 else
258d1427 3942 reply("MSG_NO_MATCHES");
d76ed9a9 3943
3944 if (das.discrim->channel)
3945 UnlockChannel(das.discrim->channel);
3946 free(das.discrim->reason);
27eaa617 3947
3948 if(das.discrim->has_regex_nick)
3949 regfree(&das.discrim->regex_nick);
3950 if(das.discrim->has_regex_ident)
3951 regfree(&das.discrim->regex_ident);
3952 if(das.discrim->has_regex_host)
3953 regfree(&das.discrim->regex_host);
3954 if(das.discrim->has_regex_info)
3955 regfree(&das.discrim->regex_info);
3956
d76ed9a9 3957 free(das.discrim);
3958 dict_delete(das.dict);
3959 return 1;
3960}
3961
258d1427 3962typedef void (*cdiscrim_search_func)(struct chanNode *match, void *data, struct userNode *bot);
d76ed9a9 3963
3964typedef struct channel_discrim {
3965 char *name, *topic;
3966
3967 unsigned int min_users, max_users;
3968 time_t min_ts, max_ts;
3969 unsigned int limit;
3970} *cdiscrim_t;
3971
258d1427 3972static cdiscrim_t opserv_cdiscrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[]);
3973static unsigned int opserv_cdiscrim_search(cdiscrim_t discrim, cdiscrim_search_func dsf, void *data, struct userNode *bot);
d76ed9a9 3974
3975static time_t
3976smart_parse_time(const char *str) {
3977 /* If an interval-style string is given, treat as time before now.
3978 * If it's all digits, treat directly as a Unix timestamp. */
3979 return str[strspn(str, "0123456789")] ? (time_t)(now - ParseInterval(str)) : (time_t)atoi(str);
3980}
3981
3982static cdiscrim_t
258d1427 3983opserv_cdiscrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[])
d76ed9a9 3984{
3985 cdiscrim_t discrim;
3986 unsigned int i;
3987
3988 discrim = calloc(1, sizeof(*discrim));
3989 discrim->limit = 25;
3990
3991 for (i = 0; i < argc; i++) {
258d1427 3992 /* Assume all criteria require arguments. */
3993 if (i == (argc - 1)) {
3994 send_message(user, bot, "MSG_MISSING_PARAMS", argv[i]);
3995 return NULL;
3996 }
3997
3998 if (!irccasecmp(argv[i], "name"))
3999 discrim->name = argv[++i];
4000 else if (!irccasecmp(argv[i], "topic"))
4001 discrim->topic = argv[++i];
4002 else if (!irccasecmp(argv[i], "users")) {
4003 const char *cmp = argv[++i];
d76ed9a9 4004 if (cmp[0] == '<') {
4005 if (cmp[1] == '=')
4006 discrim->max_users = strtoul(cmp+2, NULL, 0);
4007 else
4008 discrim->max_users = strtoul(cmp+1, NULL, 0) - 1;
4009 } else if (cmp[0] == '=') {
4010 discrim->min_users = discrim->max_users = strtoul(cmp+1, NULL, 0);
4011 } else if (cmp[0] == '>') {
4012 if (cmp[1] == '=')
4013 discrim->min_users = strtoul(cmp+2, NULL, 0);
4014 else
4015 discrim->min_users = strtoul(cmp+1, NULL, 0) + 1;
4016 } else {
4017 discrim->min_users = strtoul(cmp+2, NULL, 0);
4018 }
258d1427 4019 } else if (!irccasecmp(argv[i], "timestamp")) {
4020 const char *cmp = argv[++i];
d76ed9a9 4021 if (cmp[0] == '<') {
4022 if (cmp[1] == '=')
4023 discrim->max_ts = smart_parse_time(cmp+2);
4024 else
4025 discrim->max_ts = smart_parse_time(cmp+1)-1;
4026 } else if (cmp[0] == '=') {
4027 discrim->min_ts = discrim->max_ts = smart_parse_time(cmp+1);
4028 } else if (cmp[0] == '>') {
4029 if (cmp[1] == '=')
4030 discrim->min_ts = smart_parse_time(cmp+2);
4031 else
4032 discrim->min_ts = smart_parse_time(cmp+1)+1;
4033 } else {
4034 discrim->min_ts = smart_parse_time(cmp);
4035 }
258d1427 4036 } else if (!irccasecmp(argv[i], "limit")) {
4037 discrim->limit = strtoul(argv[++i], NULL, 10);
4038 } else {
4039 send_message(user, bot, "MSG_INVALID_CRITERIA", argv[i]);
4040 goto fail;
4041 }
d76ed9a9 4042 }
4043
4044 if (discrim->name && !strcmp(discrim->name, "*"))
258d1427 4045 discrim->name = 0;
d76ed9a9 4046 if (discrim->topic && !strcmp(discrim->topic, "*"))
258d1427 4047 discrim->topic = 0;
d76ed9a9 4048
4049 return discrim;
4050 fail:
4051 free(discrim);
4052 return NULL;
4053}
4054
4055static int
4056cdiscrim_match(cdiscrim_t discrim, struct chanNode *chan)
4057{
4058 if ((discrim->name && !match_ircglob(chan->name, discrim->name)) ||
4059 (discrim->topic && !match_ircglob(chan->topic, discrim->topic)) ||
4060 (discrim->min_users && chan->members.used < discrim->min_users) ||
4061 (discrim->max_users && chan->members.used > discrim->max_users) ||
4062 (discrim->min_ts && chan->timestamp < discrim->min_ts) ||
4063 (discrim->max_ts && chan->timestamp > discrim->max_ts)) {
258d1427 4064 return 0;
d76ed9a9 4065 }
4066 return 1;
4067}
4068
258d1427 4069static unsigned int opserv_cdiscrim_search(cdiscrim_t discrim, cdiscrim_search_func dsf, void *data, struct userNode *bot)
d76ed9a9 4070{
4071 unsigned int count = 0;
4072 dict_iterator_t it, next;
4073
4074 for (it = dict_first(channels); it && count < discrim->limit ; it = next) {
258d1427 4075 struct chanNode *chan = iter_data(it);
d76ed9a9 4076
258d1427 4077 /* Hold on to the next channel in case we decide to
4078 add actions that destructively modify the channel. */
4079 next = iter_next(it);
4080 if ((chan->members.used > 0) && cdiscrim_match(discrim, chan)) {
4081 dsf(chan, data, bot);
4082 count++;
4083 }
d76ed9a9 4084 }
4085
4086 return count;
4087}
4088
258d1427 4089void channel_count(UNUSED_ARG(struct chanNode *channel), UNUSED_ARG(void *data), UNUSED_ARG(struct userNode *bot))
d76ed9a9 4090{
4091}
4092
258d1427 4093void channel_print(struct chanNode *channel, void *data, struct userNode *bot)
d76ed9a9 4094{
4095 char modes[MAXLEN];
4096 irc_make_chanmode(channel, modes);
258d1427 4097 send_message(data, bot, "OSMSG_CSEARCH_CHANNEL_INFO", channel->name, channel->members.used, modes, channel->topic);
d76ed9a9 4098}
4099
4100static MODCMD_FUNC(cmd_csearch)
4101{
4102 cdiscrim_t discrim;
4103 unsigned int matches;
4104 cdiscrim_search_func action;
4105 struct svccmd *subcmd;
4106 char buf[MAXLEN];
4107
4108 if (!irccasecmp(argv[1], "count"))
258d1427 4109 action = channel_count;
d76ed9a9 4110 else if (!irccasecmp(argv[1], "print"))
258d1427 4111 action = channel_print;
d76ed9a9 4112 else {
258d1427 4113 reply("OSMSG_BAD_ACTION", argv[1]);
4114 return 0;
d76ed9a9 4115 }
4116
4117 sprintf(buf, "%s %s", argv[0], argv[0]);
258d1427 4118 if ((subcmd = dict_find(opserv_service->commands, buf, NULL))
4119 && !svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)) {
d76ed9a9 4120 return 0;
4121 }
4122
258d1427 4123 discrim = opserv_cdiscrim_create(user, cmd->parent->bot, argc - 2, argv + 2);
d76ed9a9 4124 if (!discrim)
258d1427 4125 return 0;
d76ed9a9 4126
4127 if (action == channel_print)
258d1427 4128 reply("OSMSG_CHANNEL_SEARCH_RESULTS");
d76ed9a9 4129 else if (action == channel_count)
258d1427 4130 discrim->limit = INT_MAX;
d76ed9a9 4131
258d1427 4132 matches = opserv_cdiscrim_search(discrim, action, user, cmd->parent->bot);
d76ed9a9 4133
4134 if (matches)
258d1427 4135 reply("MSG_MATCH_COUNT", matches);
d76ed9a9 4136 else
258d1427 4137 reply("MSG_NO_MATCHES");
d76ed9a9 4138
4139 free(discrim);
4140 return 1;
4141}
4142
4143static MODCMD_FUNC(cmd_gsync)
4144{
4145 struct server *src;
4146 if (argc > 1) {
4147 src = GetServerH(argv[1]);
4148 if (!src) {
4149 reply("MSG_SERVER_UNKNOWN", argv[1]);
4150 return 0;
4151 }
4152 } else {
4153 src = self->uplink;
4154 }
4155 irc_stats(cmd->parent->bot, src, 'G');
4156 reply("OSMSG_GSYNC_RUNNING", src->name);
4157 return 1;
4158}
4159
d914d1cb 4160static MODCMD_FUNC(cmd_ssync)
4161{
4162 struct server *src;
4163 if (argc > 1) {
4164 src = GetServerH(argv[1]);
4165 if (!src) {
4166 reply("MSG_SERVER_UNKNOWN", argv[1]);
4167 return 0;
4168 }
4169 } else {
4170 src = self->uplink;
4171 }
4172 irc_stats(cmd->parent->bot, src, 'S');
4173 reply("OSMSG_SSYNC_RUNNING", src->name);
4174 return 1;
4175}
4176
d76ed9a9 4177struct gline_extra {
4178 struct userNode *user;
4179 struct string_list *glines;
258d1427 4180 struct userNode *bot;
d76ed9a9 4181};
4182
4183static void
4184gtrace_print_func(struct gline *gline, void *extra)
4185{
4186 struct gline_extra *xtra = extra;
4187 char *when_text, set_text[20];
4188 strftime(set_text, sizeof(set_text), "%Y-%m-%d", localtime(&gline->issued));
4189 when_text = asctime(localtime(&gline->expires));
4190 when_text[strlen(when_text)-1] = 0; /* strip lame \n */
258d1427 4191 send_message(xtra->user, xtra->bot, "OSMSG_GTRACE_FORMAT", gline->target, set_text, gline->issuer, when_text, gline->reason);
d76ed9a9 4192}
4193
4194static void
4195gtrace_count_func(UNUSED_ARG(struct gline *gline), UNUSED_ARG(void *extra))
4196{
4197}
4198
4199static void
4200gtrace_ungline_func(struct gline *gline, void *extra)
4201{
4202 struct gline_extra *xtra = extra;
4203 string_list_append(xtra->glines, strdup(gline->target));
4204}
4205
4206static MODCMD_FUNC(cmd_gtrace)
4207{
4208 struct gline_discrim *discrim;
4209 gline_search_func action;
4210 unsigned int matches, nn;
4211 struct gline_extra extra;
4212 struct svccmd *subcmd;
4213 char buf[MAXLEN];
4214
4215 if (!irccasecmp(argv[1], "print"))
4216 action = gtrace_print_func;
4217 else if (!irccasecmp(argv[1], "count"))
4218 action = gtrace_count_func;
4219 else if (!irccasecmp(argv[1], "ungline"))
4220 action = gtrace_ungline_func;
4221 else {
4222 reply("OSMSG_BAD_ACTION", argv[1]);
4223 return 0;
4224 }
4225 sprintf(buf, "%s %s", argv[0], argv[0]);
258d1427 4226 if ((subcmd = dict_find(opserv_service->commands, buf, NULL))
4227 && !svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)) {
d76ed9a9 4228 return 0;
4229 }
4230
4231 discrim = gline_discrim_create(user, cmd->parent->bot, argc-2, argv+2);
4232 if (!discrim)
4233 return 0;
4234
4235 if (action == gtrace_print_func)
4236 reply("OSMSG_GLINE_SEARCH_RESULTS");
4237 else if (action == gtrace_count_func)
4238 discrim->limit = INT_MAX;
4239
4240 extra.user = user;
4241 extra.glines = alloc_string_list(4);
258d1427 4242 extra.bot = cmd->parent->bot;
d76ed9a9 4243 matches = gline_discrim_search(discrim, action, &extra);
4244
4245 if (action == gtrace_ungline_func)
4246 for (nn=0; nn<extra.glines->used; nn++)
4247 gline_remove(extra.glines->list[nn], 1);
4248 free_string_list(extra.glines);
4249
4250 if (matches)
4251 reply("MSG_MATCH_COUNT", matches);
4252 else
4253 reply("MSG_NO_MATCHES");
4254 free(discrim->alt_target_mask);
4255 free(discrim);
4256 return 1;
4257}
4258
d914d1cb 4259struct shun_extra {
4260 struct userNode *user;
4261 struct string_list *shuns;
4262 struct userNode *bot;
4263};
4264
4265static void
4266strace_print_func(struct shun *shun, void *extra)
4267{
4268 struct shun_extra *xtra = extra;
4269 char *when_text, set_text[20];
4270 strftime(set_text, sizeof(set_text), "%Y-%m-%d", localtime(&shun->issued));
4271 when_text = asctime(localtime(&shun->expires));
4272 when_text[strlen(when_text)-1] = 0; /* strip lame \n */
4273 send_message(xtra->user, xtra->bot, "OSMSG_STRACE_FORMAT", shun->target, set_text, shun->issuer, when_text, shun->reason);
4274}
4275
4276static void
4277strace_count_func(UNUSED_ARG(struct shun *shun), UNUSED_ARG(void *extra))
4278{
4279}
4280
4281static void
4282strace_unshun_func(struct shun *shun, void *extra)
4283{
4284 struct shun_extra *xtra = extra;
4285 string_list_append(xtra->shuns, strdup(shun->target));
4286}
4287
4288static MODCMD_FUNC(cmd_strace)
4289{
4290 struct shun_discrim *discrim;
4291 shun_search_func action;
4292 unsigned int matches, nn;
4293 struct shun_extra extra;
4294 struct svccmd *subcmd;
4295 char buf[MAXLEN];
4296
4297 if (!irccasecmp(argv[1], "print"))
4298 action = strace_print_func;
4299 else if (!irccasecmp(argv[1], "count"))
4300 action = strace_count_func;
4301 else if (!irccasecmp(argv[1], "unshun"))
4302 action = strace_unshun_func;
4303 else {
4304 reply("OSMSG_BAD_ACTION", argv[1]);
4305 return 0;
4306 }
4307 sprintf(buf, "%s %s", argv[0], argv[0]);
4308 if ((subcmd = dict_find(opserv_service->commands, buf, NULL))
4309 && !svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)) {
4310 return 0;
4311 }
4312
4313 discrim = shun_discrim_create(user, cmd->parent->bot, argc-2, argv+2);
4314 if (!discrim)
4315 return 0;
4316
4317 if (action == strace_print_func)
4318 reply("OSMSG_SHUN_SEARCH_RESULTS");
4319 else if (action == strace_count_func)
4320 discrim->limit = INT_MAX;
4321
4322 extra.user = user;
4323 extra.shuns = alloc_string_list(4);
4324 extra.bot = cmd->parent->bot;
4325 matches = shun_discrim_search(discrim, action, &extra);
4326
4327 if (action == strace_unshun_func)
4328 for (nn=0; nn<extra.shuns->used; nn++)
4329 shun_remove(extra.shuns->list[nn], 1);
4330 free_string_list(extra.shuns);
4331
4332 if (matches)
4333 reply("MSG_MATCH_COUNT", matches);
4334 else
4335 reply("MSG_NO_MATCHES");
4336 free(discrim->alt_target_mask);
4337 free(discrim);
4338 return 1;
4339}
4340
d76ed9a9 4341static int
4342alert_check_user(const char *key, void *data, void *extra)
4343{
4344 struct opserv_user_alert *alert = data;
4345 struct userNode *user = extra;
4346
4347 if (!discrim_match(alert->discrim, user))
4348 return 0;
4349
4350 if ((alert->reaction != REACT_NOTICE)
4351 && IsOper(user)
4352 && !alert->discrim->match_opers) {
4353 return 0;
4354 }
4355
4356 /* The user matches the alert criteria, so trigger the reaction. */
4357 if (alert->discrim->option_log)
4358 log_module(OS_LOG, LOG_INFO, "Alert %s triggered by user %s!%s@%s (%s).", key, user->nick, user->ident, user->hostname, alert->discrim->reason);
4359
4360 /* Return 1 to halt alert matching, such as when killing the user
4361 that triggered the alert. */
4362 switch (alert->reaction) {
4363 case REACT_KILL:
4364 DelUser(user, opserv, 1, alert->discrim->reason);
4365 return 1;
9a75756e 4366 case REACT_SILENT:
4367 opserv_block(user, alert->owner, alert->discrim->reason, alert->discrim->duration, 1);
4368 return 1;
d76ed9a9 4369 case REACT_GLINE:
9a75756e 4370 opserv_block(user, alert->owner, alert->discrim->reason, alert->discrim->duration, 0);
d76ed9a9 4371 return 1;
d914d1cb 4372 case REACT_SHUN:
4373 opserv_shun(user, alert->owner, alert->discrim->reason, alert->discrim->duration);
4374 return 1;
d76ed9a9 4375 default:
4376 log_module(OS_LOG, LOG_ERROR, "Invalid reaction type %d for alert %s.", alert->reaction, key);
4377 /* fall through to REACT_NOTICE case */
4378 case REACT_NOTICE:
4379 opserv_alert("Alert $b%s$b triggered by user $b%s$b!%s@%s (%s).", key, user->nick, user->ident, user->hostname, alert->discrim->reason);
4380 break;
4381 }
4382 return 0;
4383}
4384
4385static void
4386opserv_alert_check_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
4387{
4388 struct gag_entry *gag;
4389 dict_foreach(opserv_nick_based_alerts, alert_check_user, user);
4390 /* Gag them if appropriate (and only if). */
4391 user->modes &= ~FLAGS_GAGGED;
4392 for (gag = gagList; gag; gag = gag->next) {
2f61d1d7 4393 if (user_matches_glob(user, gag->mask, MATCH_USENICK)) {
d76ed9a9 4394 gag_helper_func(user, NULL);
4395 break;
4396 }
4397 }
4398}
4399
4400static void
4401opserv_staff_alert(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle))
4402{
4403 const char *type;
4404
4405 if (!opserv_conf.staff_auth_channel
4406 || user->uplink->burst
4407 || !user->handle_info)
4408 return;
4409 else if (user->handle_info->opserv_level)
4410 type = "OPER";
4411 else if (IsNetworkHelper(user))
4412 type = "NETWORK HELPER";
4413 else if (IsSupportHelper(user))
4414 type = "SUPPORT HELPER";
4415 else
4416 return;
4417
2f61d1d7 4418 if (irc_in_addr_is_valid(user->ip))
d76ed9a9 4419 send_channel_notice(opserv_conf.staff_auth_channel, opserv, IDENT_FORMAT" authed to %s account %s", IDENT_DATA(user), type, user->handle_info->handle);
4420 else
4421 send_channel_notice(opserv_conf.staff_auth_channel, opserv, "%s [%s@%s] authed to %s account %s", user->nick, user->ident, user->hostname, type, user->handle_info->handle);
4422}
4423
4424static MODCMD_FUNC(cmd_log)
4425{
4426 struct logSearch *discrim;
4427 unsigned int matches;
4428 struct logReport report;
4429
4430 discrim = log_discrim_create(cmd->parent->bot, user, argc, argv);
4431 if (!discrim)
4432 return 0;
4433
4434 reply("OSMSG_LOG_SEARCH_RESULTS");
4435 report.reporter = opserv;
4436 report.user = user;
4437 matches = log_entry_search(discrim, log_report_entry, &report);
4438
4439 if (matches)
258d1427 4440 reply("MSG_MATCH_COUNT", matches);
d76ed9a9 4441 else
258d1427 4442 reply("MSG_NO_MATCHES");
d76ed9a9 4443
4444 free(discrim);
4445 return 1;
4446}
4447
4448static int
4449gag_helper_func(struct userNode *match, UNUSED_ARG(void *extra))
4450{
4451 if (IsOper(match) || IsLocal(match))
4452 return 0;
4453 match->modes |= FLAGS_GAGGED;
4454 return 0;
4455}
4456
4457static MODCMD_FUNC(cmd_gag)
4458{
4459 struct gag_entry *gag;
4460 unsigned int gagged;
4461 unsigned long duration;
4462 char *reason;
4463
4464 reason = unsplit_string(argv + 3, argc - 3, NULL);
4465
4466 if (!is_ircmask(argv[1])) {
258d1427 4467 reply("OSMSG_INVALID_IRCMASK", argv[1]);
d76ed9a9 4468 return 0;
4469 }
4470
4471 for (gag = gagList; gag; gag = gag->next)
258d1427 4472 if (match_ircglobs(gag->mask, argv[1]))
d76ed9a9 4473 break;
4474
4475 if (gag) {
258d1427 4476 reply("OSMSG_REDUNDANT_GAG", argv[1]);
4477 return 0;
d76ed9a9 4478 }
4479
4480 duration = ParseInterval(argv[2]);
4481 gagged = gag_create(argv[1], user->handle_info->handle, reason, (duration?now+duration:0));
4482
4483 if (gagged)
258d1427 4484 reply("OSMSG_GAG_APPLIED", argv[1], gagged);
d76ed9a9 4485 else
258d1427 4486 reply("OSMSG_GAG_ADDED", argv[1]);
d76ed9a9 4487 return 1;
4488}
4489
4490static int
4491ungag_helper_func(struct userNode *match, UNUSED_ARG(void *extra))
4492{
4493 match->modes &= ~FLAGS_GAGGED;
4494 return 0;
4495}
4496
4497static MODCMD_FUNC(cmd_ungag)
4498{
4499 struct gag_entry *gag;
4500 unsigned int ungagged;
4501
4502 for (gag = gagList; gag; gag = gag->next)
258d1427 4503 if (!strcmp(gag->mask, argv[1]))
d76ed9a9 4504 break;
4505
4506 if (!gag) {
258d1427 4507 reply("OSMSG_GAG_NOT_FOUND", argv[1]);
4508 return 0;
d76ed9a9 4509 }
4510
4511 timeq_del(gag->expires, gag_expire, gag, 0);
4512 ungagged = gag_free(gag);
4513
4514 if (ungagged)
258d1427 4515 reply("OSMSG_UNGAG_APPLIED", argv[1], ungagged);
d76ed9a9 4516 else
258d1427 4517 reply("OSMSG_UNGAG_ADDED", argv[1]);
d76ed9a9 4518 return 1;
4519}
4520
4521static MODCMD_FUNC(cmd_addalert)
4522{
4523 opserv_alert_reaction reaction;
4524 struct svccmd *subcmd;
4525 const char *name;
4526 char buf[MAXLEN];
4527
4528 name = argv[1];
4529 sprintf(buf, "addalert %s", argv[2]);
258d1427 4530 if (!(subcmd = dict_find(opserv_service->commands, buf, NULL))) {
4531 reply("OSMSG_UNKNOWN_REACTION", argv[2]);
4532 return 0;
d76ed9a9 4533 }
4534 if (!irccasecmp(argv[2], "notice"))
4535 reaction = REACT_NOTICE;
4536 else if (!irccasecmp(argv[2], "kill"))
4537 reaction = REACT_KILL;
9a75756e 4538 else if (!irccasecmp(argv[2], "silent"))
4539 reaction = REACT_SILENT;
d76ed9a9 4540 else if (!irccasecmp(argv[2], "gline"))
4541 reaction = REACT_GLINE;
d914d1cb 4542 else if (!irccasecmp(argv[2], "shun"))
4543 reaction = REACT_SHUN;
d76ed9a9 4544 else {
258d1427 4545 reply("OSMSG_UNKNOWN_REACTION", argv[2]);
4546 return 0;
d76ed9a9 4547 }
258d1427 4548 if (!svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)
d76ed9a9 4549 || !opserv_add_user_alert(user, name, reaction, unsplit_string(argv + 3, argc - 3, NULL)))
4550 return 0;
4551 reply("OSMSG_ADDED_ALERT", name);
4552 return 1;
4553}
4554
4555static MODCMD_FUNC(cmd_delalert)
4556{
4557 unsigned int i;
4558 for (i=1; i<argc; i++) {
4559 dict_remove(opserv_nick_based_alerts, argv[i]);
4560 dict_remove(opserv_channel_alerts, argv[i]);
258d1427 4561 if (dict_remove(opserv_user_alerts, argv[i]))
4562 reply("OSMSG_REMOVED_ALERT", argv[i]);
d76ed9a9 4563 else
258d1427 4564 reply("OSMSG_NO_SUCH_ALERT", argv[i]);
d76ed9a9 4565 }
4566 return 1;
4567}
4568
4569static void
4570opserv_conf_read(void)
4571{
4572 struct record_data *rd;
4573 dict_t conf_node, child;
4574 const char *str, *str2;
4575 struct policer_params *pp;
4576 dict_iterator_t it;
4577
4578 rd = conf_get_node(OPSERV_CONF_NAME);
4579 if (!rd || rd->type != RECDB_OBJECT) {
258d1427 4580 log_module(OS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", OPSERV_CONF_NAME);
4581 return;
d76ed9a9 4582 }
4583 conf_node = rd->d.object;
4584 str = database_get_data(conf_node, KEY_DEBUG_CHANNEL, RECDB_QSTRING);
4585 if (opserv && str) {
4586 str2 = database_get_data(conf_node, KEY_DEBUG_CHANNEL_MODES, RECDB_QSTRING);
4587 if (!str2)
4588 str2 = "+tinms";
258d1427 4589 opserv_conf.debug_channel = AddChannel(str, now, str2, NULL, NULL);
d76ed9a9 4590 AddChannelUser(opserv, opserv_conf.debug_channel)->modes |= MODE_CHANOP;
4591 } else {
258d1427 4592 opserv_conf.debug_channel = NULL;
d76ed9a9 4593 }
4594 str = database_get_data(conf_node, KEY_ALERT_CHANNEL, RECDB_QSTRING);
4595 if (opserv && str) {
4596 str2 = database_get_data(conf_node, KEY_ALERT_CHANNEL_MODES, RECDB_QSTRING);
4597 if (!str2)
4598 str2 = "+tns";
258d1427 4599 opserv_conf.alert_channel = AddChannel(str, now, str2, NULL, NULL);
d76ed9a9 4600 AddChannelUser(opserv, opserv_conf.alert_channel)->modes |= MODE_CHANOP;
4601 } else {
258d1427 4602 opserv_conf.alert_channel = NULL;
d76ed9a9 4603 }
4604 str = database_get_data(conf_node, KEY_STAFF_AUTH_CHANNEL, RECDB_QSTRING);
4605 if (opserv && str) {
4606 str2 = database_get_data(conf_node, KEY_STAFF_AUTH_CHANNEL_MODES, RECDB_QSTRING);
4607 if (!str2)
4608 str2 = "+timns";
2aef5f4b 4609 opserv_conf.staff_auth_channel = AddChannel(str, now, str2, NULL, NULL);
d76ed9a9 4610 AddChannelUser(opserv, opserv_conf.staff_auth_channel)->modes |= MODE_CHANOP;
4611 } else {
4612 opserv_conf.staff_auth_channel = NULL;
4613 }
5a1daaab 4614
4615 str = database_get_data(conf_node, KEY_ADMIN_LEVEL, RECDB_QSTRING);
4616 opserv_conf.admin_level = str ? strtoul(str, NULL, 0): 800;
4617
d76ed9a9 4618 str = database_get_data(conf_node, KEY_UNTRUSTED_MAX, RECDB_QSTRING);
4619 opserv_conf.untrusted_max = str ? strtoul(str, NULL, 0) : 5;
4620 str = database_get_data(conf_node, KEY_PURGE_LOCK_DELAY, RECDB_QSTRING);
4621 opserv_conf.purge_lock_delay = str ? strtoul(str, NULL, 0) : 60;
4622 str = database_get_data(conf_node, KEY_JOIN_FLOOD_MODERATE, RECDB_QSTRING);
4623 opserv_conf.join_flood_moderate = str ? strtoul(str, NULL, 0) : 1;
4624 str = database_get_data(conf_node, KEY_JOIN_FLOOD_MODERATE_THRESH, RECDB_QSTRING);
4625 opserv_conf.join_flood_moderate_threshold = str ? strtoul(str, NULL, 0) : 50;
4626 str = database_get_data(conf_node, KEY_NICK, RECDB_QSTRING);
4627 if (opserv && str)
4628 NickChange(opserv, str, 0);
d914d1cb 4629
d76ed9a9 4630 str = database_get_data(conf_node, KEY_CLONE_GLINE_DURATION, RECDB_QSTRING);
4631 opserv_conf.clone_gline_duration = str ? ParseInterval(str) : 3600;
4632 str = database_get_data(conf_node, KEY_BLOCK_GLINE_DURATION, RECDB_QSTRING);
4633 opserv_conf.block_gline_duration = str ? ParseInterval(str) : 3600;
4634
d914d1cb 4635 str = database_get_data(conf_node, KEY_BLOCK_SHUN_DURATION, RECDB_QSTRING);
4636 opserv_conf.block_shun_duration = str ? ParseInterval(str) : 3600;
4637
d76ed9a9 4638 if (!opserv_conf.join_policer_params)
4639 opserv_conf.join_policer_params = policer_params_new();
4640 policer_params_set(opserv_conf.join_policer_params, "size", "20");
4641 policer_params_set(opserv_conf.join_policer_params, "drain-rate", "1");
4642 if ((child = database_get_data(conf_node, KEY_JOIN_POLICER, RECDB_OBJECT)))
258d1427 4643 dict_foreach(child, set_policer_param, opserv_conf.join_policer_params);
d76ed9a9 4644
4645 for (it = dict_first(channels); it; it = iter_next(it)) {
4646 struct chanNode *cNode = iter_data(it);
4647 cNode->join_policer.params = opserv_conf.join_policer_params;
4648 }
4649
4650 if (opserv_conf.new_user_policer.params)
4651 pp = opserv_conf.new_user_policer.params;
4652 else
4653 pp = opserv_conf.new_user_policer.params = policer_params_new();
4654 policer_params_set(pp, "size", "200");
4655 policer_params_set(pp, "drain-rate", "3");
4656 if ((child = database_get_data(conf_node, KEY_NEW_USER_POLICER, RECDB_OBJECT)))
258d1427 4657 dict_foreach(child, set_policer_param, pp);
d76ed9a9 4658}
4659
5a1daaab 4660/* lame way to export opserv_conf value to nickserv.c ... */
4661unsigned int
4662opserv_conf_admin_level()
4663{
4664 return(opserv_conf.admin_level);
4665}
4666
d76ed9a9 4667static void
4668opserv_db_init(void) {
4669 /* set up opserv_trusted_hosts dict */
4670 dict_delete(opserv_trusted_hosts);
4671 opserv_trusted_hosts = dict_new();
4672 dict_set_free_data(opserv_trusted_hosts, free_trusted_host);
4673 /* set up opserv_chan_warn dict */
de9510bc 4674
4675/* alert trace notice channel #x replaces warnings
d76ed9a9 4676 dict_delete(opserv_chan_warn);
4677 opserv_chan_warn = dict_new();
4678 dict_set_free_keys(opserv_chan_warn, free);
4679 dict_set_free_data(opserv_chan_warn, free);
de9510bc 4680*/
d76ed9a9 4681 /* set up opserv_user_alerts */
4682 dict_delete(opserv_channel_alerts);
4683 opserv_channel_alerts = dict_new();
4684 dict_delete(opserv_nick_based_alerts);
4685 opserv_nick_based_alerts = dict_new();
4686 dict_delete(opserv_user_alerts);
4687 opserv_user_alerts = dict_new();
4688 dict_set_free_keys(opserv_user_alerts, free);
4689 dict_set_free_data(opserv_user_alerts, opserv_free_user_alert);
4690 /* set up opserv_bad_words */
4691 free_string_list(opserv_bad_words);
4692 opserv_bad_words = alloc_string_list(4);
4693 /* and opserv_exempt_channels */
4694 dict_delete(opserv_exempt_channels);
4695 opserv_exempt_channels = dict_new();
4696 dict_set_free_keys(opserv_exempt_channels, free);
4697}
4698
4699static void
4700opserv_db_cleanup(void)
4701{
4702 unsigned int nn;
4703
de9510bc 4704/* dict_delete(opserv_chan_warn); */
d76ed9a9 4705 dict_delete(opserv_reserved_nick_dict);
4706 free_string_list(opserv_bad_words);
4707 dict_delete(opserv_exempt_channels);
4708 dict_delete(opserv_trusted_hosts);
4709 unreg_del_user_func(opserv_user_cleanup);
4710 dict_delete(opserv_hostinfo_dict);
4711 dict_delete(opserv_nick_based_alerts);
4712 dict_delete(opserv_channel_alerts);
4713 dict_delete(opserv_user_alerts);
4714 for (nn=0; nn<ArrayLength(level_strings); ++nn)
4715 free(level_strings[nn]);
4716 while (gagList)
4717 gag_free(gagList);
4718 policer_params_delete(opserv_conf.join_policer_params);
4719 policer_params_delete(opserv_conf.new_user_policer.params);
4720}
4721
4722void
4723init_opserv(const char *nick)
4724{
4725 OS_LOG = log_register_type("OpServ", "file:opserv.log");
a32da4c7 4726 if (nick) {
4727 const char *modes = conf_get_data("services/opserv/modes", RECDB_QSTRING);
4728 opserv = AddService(nick, modes ? modes : NULL, "Oper Services", NULL);
4729 }
d76ed9a9 4730 conf_register_reload(opserv_conf_read);
4731
4732 memset(level_strings, 0, sizeof(level_strings));
4733 opserv_module = module_register("OpServ", OS_LOG, "opserv.help", opserv_help_expand);
4734 opserv_define_func("ACCESS", cmd_access, 0, 0, 0);
4735 opserv_define_func("ADDALERT", cmd_addalert, 800, 0, 4);
4736 opserv_define_func("ADDALERT NOTICE", NULL, 0, 0, 0);
9a75756e 4737 opserv_define_func("ADDALERT SILENT", NULL, 900, 0, 0);
d76ed9a9 4738 opserv_define_func("ADDALERT GLINE", NULL, 900, 0, 0);
d914d1cb 4739 opserv_define_func("ADDALERT SHUN", NULL, 900, 0, 0);
d76ed9a9 4740 opserv_define_func("ADDALERT KILL", NULL, 900, 0, 0);
4741 opserv_define_func("ADDBAD", cmd_addbad, 800, 0, 2);
4742 opserv_define_func("ADDEXEMPT", cmd_addexempt, 800, 0, 2);
4743 opserv_define_func("ADDTRUST", cmd_addtrust, 800, 0, 5);
4744 opserv_define_func("BAN", cmd_ban, 100, 2, 2);
4745 opserv_define_func("BLOCK", cmd_block, 100, 0, 2);
4746 opserv_define_func("CHANINFO", cmd_chaninfo, 0, 3, 0);
4747 opserv_define_func("CLEARBANS", cmd_clearbans, 300, 2, 0);
4748 opserv_define_func("CLEARMODES", cmd_clearmodes, 400, 2, 0);
4749 opserv_define_func("CLONE", cmd_clone, 999, 0, 3);
4750 opserv_define_func("COLLIDE", cmd_collide, 800, 0, 5);
4751 opserv_define_func("CSEARCH", cmd_csearch, 100, 0, 3);
4752 opserv_define_func("CSEARCH COUNT", cmd_csearch, 0, 0, 0);
4753 opserv_define_func("CSEARCH PRINT", cmd_csearch, 0, 0, 0);
4754 opserv_define_func("DELALERT", cmd_delalert, 800, 0, 2);
4755 opserv_define_func("DELBAD", cmd_delbad, 800, 0, 2);
4756 opserv_define_func("DELEXEMPT", cmd_delexempt, 800, 0, 2);
4757 opserv_define_func("DELTRUST", cmd_deltrust, 800, 0, 2);
4758 opserv_define_func("DEOP", cmd_deop, 100, 2, 2);
4759 opserv_define_func("DEOPALL", cmd_deopall, 400, 2, 0);
55342ce8 4760 opserv_define_func("DEHOP", cmd_dehop, 100, 2, 2);
4761 opserv_define_func("DEHOPALL", cmd_dehopall, 400, 2, 0);
d76ed9a9 4762 opserv_define_func("DEVOICEALL", cmd_devoiceall, 300, 2, 0);
4763 opserv_define_func("DIE", cmd_die, 900, 0, 2);
4764 opserv_define_func("DUMP", cmd_dump, 999, 0, 2);
4765 opserv_define_func("EDITTRUST", cmd_edittrust, 800, 0, 5);
4766 opserv_define_func("GAG", cmd_gag, 600, 0, 4);
4767 opserv_define_func("GLINE", cmd_gline, 600, 0, 4);
4768 opserv_define_func("GSYNC", cmd_gsync, 600, 0, 0);
4769 opserv_define_func("GTRACE", cmd_gtrace, 100, 0, 3);
4770 opserv_define_func("GTRACE COUNT", NULL, 0, 0, 0);
4771 opserv_define_func("GTRACE PRINT", NULL, 0, 0, 0);
d914d1cb 4772 opserv_define_func("SBLOCK", cmd_sblock, 100, 0, 2);
4773 opserv_define_func("SHUN", cmd_shun, 600, 0, 4);
4774 opserv_define_func("SSYNC", cmd_ssync, 600, 0, 0);
4775 opserv_define_func("STRACE", cmd_strace, 100, 0, 3);
4776 opserv_define_func("STRACE COUNT", NULL, 0, 0, 0);
4777 opserv_define_func("STRACE PRINT", NULL, 0, 0, 0);
d76ed9a9 4778 opserv_define_func("INVITE", cmd_invite, 100, 2, 0);
4779 opserv_define_func("INVITEME", cmd_inviteme, 100, 0, 0);
4780 opserv_define_func("JOIN", cmd_join, 601, 0, 2);
4781 opserv_define_func("JUMP", cmd_jump, 900, 0, 2);
4782 opserv_define_func("JUPE", cmd_jupe, 900, 0, 4);
4783 opserv_define_func("KICK", cmd_kick, 100, 2, 2);
4784 opserv_define_func("KICKALL", cmd_kickall, 400, 2, 0);
4785 opserv_define_func("KICKBAN", cmd_kickban, 100, 2, 2);
4786 opserv_define_func("KICKBANALL", cmd_kickbanall, 450, 2, 0);
4787 opserv_define_func("LOG", cmd_log, 900, 0, 2);
4788 opserv_define_func("MODE", cmd_mode, 100, 2, 2);
4789 opserv_define_func("OP", cmd_op, 100, 2, 2);
4790 opserv_define_func("OPALL", cmd_opall, 400, 2, 0);
55342ce8 4791 opserv_define_func("HOP", cmd_hop, 100, 2, 2);
4792 opserv_define_func("HOPALL", cmd_hopall, 400, 2, 0);
d76ed9a9 4793 opserv_define_func("PART", cmd_part, 601, 0, 2);
4794 opserv_define_func("QUERY", cmd_query, 0, 0, 0);
4795 opserv_define_func("RAW", cmd_raw, 999, 0, 2);
4796 opserv_define_func("RECONNECT", cmd_reconnect, 900, 0, 0);
4797 opserv_define_func("REFRESHG", cmd_refreshg, 600, 0, 0);
d914d1cb 4798 opserv_define_func("REFRESHS", cmd_refreshs, 600, 0, 0);
d76ed9a9 4799 opserv_define_func("REHASH", cmd_rehash, 900, 0, 0);
4800 opserv_define_func("REOPEN", cmd_reopen, 900, 0, 0);
4801 opserv_define_func("RESERVE", cmd_reserve, 800, 0, 5);
4802 opserv_define_func("RESTART", cmd_restart, 900, 0, 2);
4803 opserv_define_func("SET", cmd_set, 900, 0, 3);
4804 opserv_define_func("SETTIME", cmd_settime, 901, 0, 0);
4805 opserv_define_func("STATS ALERTS", cmd_stats_alerts, 0, 0, 0);
4806 opserv_define_func("STATS BAD", cmd_stats_bad, 0, 0, 0);
4807 opserv_define_func("STATS GAGS", cmd_stats_gags, 0, 0, 0);
4808 opserv_define_func("STATS GLINES", cmd_stats_glines, 0, 0, 0);
d914d1cb 4809 opserv_define_func("STATS SHUNS", cmd_stats_shuns, 0, 0, 0);
d76ed9a9 4810 opserv_define_func("STATS LINKS", cmd_stats_links, 0, 0, 0);
4811 opserv_define_func("STATS MAX", cmd_stats_max, 0, 0, 0);
4812 opserv_define_func("STATS NETWORK", cmd_stats_network, 0, 0, 0);
4813 opserv_define_func("STATS NETWORK2", cmd_stats_network2, 0, 0, 0);
4814 opserv_define_func("STATS RESERVED", cmd_stats_reserved, 0, 0, 0);
4815 opserv_define_func("STATS TIMEQ", cmd_stats_timeq, 0, 0, 0);
4816 opserv_define_func("STATS TRUSTED", cmd_stats_trusted, 0, 0, 0);
4817 opserv_define_func("STATS UPLINK", cmd_stats_uplink, 0, 0, 0);
4818 opserv_define_func("STATS UPTIME", cmd_stats_uptime, 0, 0, 0);
de9510bc 4819/* opserv_define_func("STATS WARN", cmd_stats_warn, 0, 0, 0); */
f14e4f83 4820#if defined(WITH_MALLOC_X3) || defined(WITH_MALLOC_SLAB)
ec1a68c8 4821 opserv_define_func("STATS MEMORY", cmd_stats_memory, 0, 0, 0);
4822#endif
d76ed9a9 4823 opserv_define_func("TRACE", cmd_trace, 100, 0, 3);
4824 opserv_define_func("TRACE PRINT", NULL, 0, 0, 0);
4825 opserv_define_func("TRACE COUNT", NULL, 0, 0, 0);
4826 opserv_define_func("TRACE DOMAINS", NULL, 0, 0, 0);
4827 opserv_define_func("TRACE GLINE", NULL, 600, 0, 0);
d914d1cb 4828 opserv_define_func("TRACE SHUN", NULL, 600, 0, 0);
d76ed9a9 4829 opserv_define_func("TRACE GAG", NULL, 600, 0, 0);
4830 opserv_define_func("TRACE KILL", NULL, 600, 0, 0);
4831 opserv_define_func("UNBAN", cmd_unban, 100, 2, 2);
4832 opserv_define_func("UNGAG", cmd_ungag, 600, 0, 2);
4833 opserv_define_func("UNGLINE", cmd_ungline, 600, 0, 2);
4834 modcmd_register(opserv_module, "GTRACE UNGLINE", NULL, 0, 0, "template", "ungline", NULL);
d914d1cb 4835 opserv_define_func("UNSHUN", cmd_unshun, 600, 0, 2);
4836 modcmd_register(opserv_module, "GTRACE UNSHUN", NULL, 0, 0, "template", "unshun", NULL);
d76ed9a9 4837 opserv_define_func("UNJUPE", cmd_unjupe, 900, 0, 2);
4838 opserv_define_func("UNRESERVE", cmd_unreserve, 800, 0, 2);
de9510bc 4839/* opserv_define_func("UNWARN", cmd_unwarn, 800, 0, 0); */
d76ed9a9 4840 opserv_define_func("VOICEALL", cmd_voiceall, 300, 2, 0);
de9510bc 4841/* opserv_define_func("WARN", cmd_warn, 800, 0, 2); */
d76ed9a9 4842 opserv_define_func("WHOIS", cmd_whois, 0, 0, 2);
4843
4844 opserv_reserved_nick_dict = dict_new();
4845 opserv_hostinfo_dict = dict_new();
4846 dict_set_free_keys(opserv_hostinfo_dict, free);
4847 dict_set_free_data(opserv_hostinfo_dict, opserv_free_hostinfo);
4848
4849 reg_new_user_func(opserv_new_user_check);
4850 reg_nick_change_func(opserv_alert_check_nick);
4851 reg_del_user_func(opserv_user_cleanup);
c52666c6 4852 reg_new_channel_func(opserv_channel_check);
d76ed9a9 4853 reg_del_channel_func(opserv_channel_delete);
4854 reg_join_func(opserv_join_check);
4855 reg_auth_func(opserv_staff_alert);
4856
4857 opserv_db_init();
4858 saxdb_register("OpServ", opserv_saxdb_read, opserv_saxdb_write);
4859 if (nick)
258d1427 4860 {
4861 opserv_service = service_register(opserv);
4862 opserv_service->trigger = '?';
4863 }
d76ed9a9 4864
4865 reg_exit_func(opserv_db_cleanup);
4866 message_register_table(msgtab);
4867}