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