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