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