]> jfr.im git - irc/evilnet/x3.git/blame - src/opserv.c
Fix for crash bug during burst where X3 sends a B (BURST) message with a channel...
[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 *
d0f04f71 6 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 7 * it under the terms of the GNU General Public License as published by
348683aa 8 * the Free Software Foundation; either version 3 of the License, or
d76ed9a9 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
8de34abd 21#include "config.h"
5aa400d2 22#include "chanserv.h"
d76ed9a9 23#include "conf.h"
47956fc5 24#include "common.h"
d76ed9a9 25#include "gline.h"
26#include "global.h"
27#include "nickserv.h"
28#include "modcmd.h"
ec311f39 29#include "modules.h"
89d871d8 30#include "proto.h"
d76ed9a9 31#include "opserv.h"
32#include "timeq.h"
33#include "saxdb.h"
d914d1cb 34#include "shun.h"
d76ed9a9 35
35305a49 36#include <tre/regex.h>
5b1166fd 37
d76ed9a9 38#ifdef HAVE_SYS_TIMES_H
39#include <sys/times.h>
40#endif
41#ifdef HAVE_NETINET_IN_H
42#include <netinet/in.h>
43#endif
44#ifdef HAVE_ARPA_INET_H
45#include <arpa/inet.h>
46#endif
5b1166fd 47
d76ed9a9 48#define OPSERV_CONF_NAME "services/opserv"
49
50#define KEY_ALERT_CHANNEL "alert_channel"
51#define KEY_ALERT_CHANNEL_MODES "alert_channel_modes"
52#define KEY_DEBUG_CHANNEL "debug_channel"
53#define KEY_DEBUG_CHANNEL_MODES "debug_channel_modes"
54#define KEY_UNTRUSTED_MAX "untrusted_max"
55#define KEY_PURGE_LOCK_DELAY "purge_lock_delay"
56#define KEY_JOIN_FLOOD_MODERATE "join_flood_moderate"
57#define KEY_JOIN_FLOOD_MODERATE_THRESH "join_flood_moderate_threshold"
58#define KEY_NICK "nick"
59#define KEY_JOIN_POLICER "join_policer"
60#define KEY_NEW_USER_POLICER "new_user_policer"
7637f48f 61#define KEY_AUTOJOIN_CHANNELS "autojoin_channels"
d76ed9a9 62#define KEY_REASON "reason"
63#define KEY_RESERVES "reserves"
64#define KEY_IDENT "username" /* for compatibility with 1.0 DBs */
65#define KEY_HOSTNAME "hostname"
66#define KEY_DESC "description"
67#define KEY_BAD_WORDS "bad"
68#define KEY_EXEMPT_CHANNELS "exempt"
69#define KEY_SECRET_WORDS "secret"
70#define KEY_TRUSTED_HOSTS "trusted"
71#define KEY_OWNER "owner"
72#define KEY_GAGS "gags"
73#define KEY_ALERTS "alerts"
74#define KEY_REACTION "reaction"
75#define KEY_DISCRIM "discrim"
76#define KEY_WARN "chanwarn"
77#define KEY_MAX "max"
78#define KEY_TIME "time"
083fb96b 79#define KEY_LAST "last"
a947ef71 80#define KEY_EXPIRE "expire"
d76ed9a9 81#define KEY_MAX_CLIENTS "max_clients"
82#define KEY_LIMIT "limit"
83#define KEY_EXPIRES "expires"
84#define KEY_STAFF_AUTH_CHANNEL "staff_auth_channel"
85#define KEY_STAFF_AUTH_CHANNEL_MODES "staff_auth_channel_modes"
86#define KEY_CLONE_GLINE_DURATION "clone_gline_duration"
87#define KEY_BLOCK_GLINE_DURATION "block_gline_duration"
d914d1cb 88#define KEY_BLOCK_SHUN_DURATION "block_shun_duration"
d76ed9a9 89#define KEY_ISSUER "issuer"
90#define KEY_ISSUED "issued"
5a1daaab 91#define KEY_ADMIN_LEVEL "admin_level"
1c5f6697 92#define KEY_SILENT_LEVEL "silent_level"
47956fc5 93#define KEY_UPLINK "uplink"
94#define KEY_SECOND "secondaryuplink"
95#define KEY_PORT "port"
96#define KEY_KARMA "karma"
97#define KEY_OFFLINE "offline"
98#define KEY_ROUTINGPLAN "routingplan"
99#define KEY_ROUTINGPLAN_OPTIONS "routingplan_options"
08895577 100#define KEY_DEFCON1 "DefCon1"
101#define KEY_DEFCON2 "DefCon2"
102#define KEY_DEFCON3 "DefCon3"
103#define KEY_DEFCON4 "DefCon4"
104#define KEY_DEFCON_LEVEL "DefConLevel"
105#define KEY_DEFCON_CHANMODES "DefConChanModes"
106#define KEY_DEFCON_SESSION_LIMIT "DefConSessionLimit"
107#define KEY_DEFCON_TIMEOUT "DefConTimeOut"
08895577 108#define KEY_DEFCON_GLOBAL "GlobalOnDefcon"
109#define KEY_DEFCON_GLOBAL_MORE "GlobalOnDefconMore"
110#define KEY_DEFCON_MESSAGE "DefconMessage"
111#define KEY_DEFCON_OFF_MESSAGE "DefConOffMessage"
112#define KEY_DEFCON_GLINE_DURATION "DefConGlineExpire"
113#define KEY_DEFCON_GLINE_REASON "DefConGlineReason"
47956fc5 114
115/* Routing karma values: */
116/* What value we start out with when new servers are added: */
117#define KARMA_DEFAULT 10
118 /* max, min */
119#define KARMA_MAX 10
120#define KARMA_MIN -10
121/* ping out, reduce karma by this much: */
122#define KARMA_PINGOUT -8
123/* read err, reduce karma by this much: */
124#define KARMA_READERROR -5
125/* every 24 hours everyone gets this much added (so we eventually re-try bad servers) */
126#define KARMA_ENTROPE 1
127/* every 24 hours servers linked for 24 hours get an additional ammount: */
128#define KARMA_RELIABLE 1
129/* How often to run entrope and reliable checks */
130#define KARMA_TIMER 86400 /* 1 day */
131
132#define ROUTING_CONNECT_TIMEOUT 30 /* 30 seconds */
d76ed9a9 133
258d1427 134#define IDENT_FORMAT "%s [%s@%s/%s]"
2f61d1d7 135#define IDENT_DATA(user) user->nick, user->ident, user->hostname, irc_ntoa(&user->ip)
258d1427 136#define MAX_CHANNELS_WHOIS 50
d76ed9a9 137#define OSMSG_PART_REASON "%s has no reason."
138#define OSMSG_KICK_REQUESTED "Kick requested by %s."
139#define OSMSG_KILL_REQUESTED "Kill requested by %s."
140#define OSMSG_GAG_REQUESTED "Gag requested by %s."
141
142static const struct message_entry msgtab[] = {
de9510bc 143 { "OSMSG_BAR", "----------------------------------------" },
d76ed9a9 144 { "OSMSG_USER_ACCESS_IS", "$b%s$b (account $b%s$b) has %d access." },
145 { "OSMSG_LEVEL_TOO_LOW", "You lack sufficient access to use this command." },
146 { "OSMSG_NEED_CHANNEL", "You must specify a channel for $b%s$b." },
147 { "OSMSG_INVALID_IRCMASK", "$b%s$b is an invalid IRC hostmask." },
148 { "OSMSG_ADDED_BAN", "I have banned $b%s$b from $b%s$b." },
1136f709 149 { "OSMSG_BLOCK_TRUSTED", "$b%s$b is on a trusted ip. If you really want to G-line him, use the GLINE command." },
150 { "OSMSG_BLOCK_OPER" , "G-lining $b%s$b (*@%s) would also hit the IRC operator $b%s$b." },
151 { "OSMSG_SBLOCK_OPER" , "Shuning $b%s$b (*@%s) would also hit the IRC operator $b%s$b." },
152 { "OSMSG_NO_GLINE_CMD", "The GLINE command is not bound so you can only block with the default duration." },
153 { "OSMSG_NO_SHUN_CMD", "The SHUN command is not bound so you can only block with the default duration." },
d914d1cb 154 { "OSMSG_SHUN_ISSUED", "Shun issued for $b%s$b." },
155 { "OSMSG_SHUN_REMOVED", "Shun removed for $b%s$b." },
156 { "OSMSG_SHUN_FORCE_REMOVED", "Unknown/expired Shun removed for $b%s$b." },
157 { "OSMSG_SHUN_ONE_REFRESHED", "All Shuns resent to $b%s$b." },
158 { "OSMSG_SHUN_REFRESHED", "All Shuns refreshed." },
d76ed9a9 159 { "OSMSG_GLINE_ISSUED", "G-line issued for $b%s$b." },
160 { "OSMSG_GLINE_REMOVED", "G-line removed for $b%s$b." },
161 { "OSMSG_GLINE_FORCE_REMOVED", "Unknown/expired G-line removed for $b%s$b." },
162 { "OSMSG_GLINES_ONE_REFRESHED", "All G-lines resent to $b%s$b." },
163 { "OSMSG_GLINES_REFRESHED", "All G-lines refreshed." },
164 { "OSMSG_CLEARBANS_DONE", "Cleared all bans from channel $b%s$b." },
165 { "OSMSG_CLEARMODES_DONE", "Cleared all modes from channel $b%s$b." },
166 { "OSMSG_NO_CHANNEL_MODES", "Channel $b%s$b had no modes to clear." },
167 { "OSMSG_DEOP_DONE", "Deopped the requested lusers." },
168 { "OSMSG_DEOPALL_DONE", "Deopped everyone on $b%s$b." },
55342ce8 169 { "OSMSG_DEHOP_DONE", "Dehalfopped the requested lusers." },
170 { "OSMSG_DEHOPALL_DONE", "Dehalfopped everyone on $b%s$b." },
d76ed9a9 171 { "OSMSG_NO_DEBUG_CHANNEL", "No debug channel has been configured." },
172 { "OSMSG_INVITE_DONE", "Invited $b%s$b to $b%s$b." },
173 { "OSMSG_ALREADY_THERE", "You are already in $b%s$b." },
57e3ccf6 174 { "OSMSG_USER_ALREADY_THERE", "%s is already in $b%s$b." },
39c1a4ef 175 { "OSMSG_NOT_THERE", "You not in $b%s$b." },
d76ed9a9 176 { "OSMSG_JOIN_DONE", "I have joined $b%s$b." },
d82cf2f0 177 { "OSMSG_MARK_SET", "Set the MARK." },
c408f18a 178 { "OSMSG_SVSJOIN_SENT", "Sent the SVSJOIN." },
39c1a4ef 179 { "OSMSG_SVSPART_SENT", "Sent the SVSPART." },
d76ed9a9 180 { "OSMSG_ALREADY_JOINED", "I am already in $b%s$b." },
181 { "OSMSG_NOT_ON_CHANNEL", "$b%s$b does not seem to be on $b%s$b." },
182 { "OSMSG_KICKALL_DONE", "I have cleared out %s." },
183 { "OSMSG_LEAVING", "Leaving $b%s$b." },
d82cf2f0 184 { "OSMSG_MARK_INVALID", "Sorry, marks must contain only letters, numbers, and dashes ('-')." },
d76ed9a9 185 { "OSMSG_MODE_SET", "I have set the modes for $b%s$b." },
186 { "OSMSG_OP_DONE", "Opped the requested lusers." },
187 { "OSMSG_OPALL_DONE", "Opped everyone on $b%s$b." },
55342ce8 188 { "OSMSG_HOP_DONE", "Halfopped the requested lusers." },
189 { "OSMSG_HOPALL_DONE", "Halfopped everyone on $b%s$b." },
964a842d 190 { "OMSG_BAD_SVSNICK", "$b%s$b is an invalid nickname." },
191
37ef8ee3 192 { "OSMSG_WHOIS_IDENT", "%s (%s@%s) from %d.%d.%d.%d" },
21f6caee 193 { "OSMSG_WHOIS_NICK", "Nick : %s" },
194 { "OSMSG_WHOIS_HOST", "Host : %s@%s" },
195 { "OSMSG_WHOIS_FAKEHOST", "Fakehost : %s" },
196 { "OSMSG_WHOIS_CRYPT_HOST", "Crypt Host : %s" },
197 { "OSMSG_WHOIS_CRYPT_IP", "Crypt IP : %s" },
198 { "OSMSG_WHOIS_IP", "Real IP : %s" },
199 { "OSMSG_WHOIS_COUNTRY", "Country : %s" },
200 { "OSMSG_WHOIS_COUNTRY_CODE","Country Code : %s" },
201 { "OSMSG_WHOIS_CITY", "City : %s" },
202 { "OSMSG_WHOIS_REGION", "Region/State : %s" },
203 { "OSMSG_WHOIS_POSTAL_CODE","Postal Code : %s" },
204 { "OSMSG_WHOIS_LATITUDE", "Latitude : %f" },
205 { "OSMSG_WHOIS_LONGITUDE", "Longitude : %f" },
1ad7ac15 206 { "OSMSG_WHOIS_MAP", "Map : %s" },
21f6caee 207 { "OSMSG_WHOIS_DMA_CODE", "DMA Code : %d" },
208 { "OSMSG_WHOIS_AREA_CODE", "Area Code : %d" },
209 { "OSMSG_WHOIS_MODES", "Modes : +%s " },
210 { "OSMSG_WHOIS_INFO", "Info : %s" },
211 { "OSMSG_WHOIS_NUMERIC", "Numnick : %s" },
212 { "OSMSG_WHOIS_SERVER", "Server : %s" },
213 { "OSMSG_WHOIS_NICK_AGE", "Nick Age : %s" },
214 { "OSMSG_WHOIS_ACCOUNT", "Account : %s" },
89d871d8 215 { "OSMSG_WHOIS_PRIVS", "IRCd Privs : %s" },
21f6caee 216 { "OSMSG_WHOIS_CHANNELS", "Channels : %s" },
37ef8ee3 217 { "OSMSG_WHOIS_HIDECHANS", "Channel list omitted for your sanity." },
0e08a8e0 218 { "OSMSG_WHOIS_VERSION", "Version : %s" },
ba4b4d51 219 { "OSMSG_WHOIS_SSLFP", "SSL f/print : %s" },
2c00fbc2 220 { "OSMSG_WHOIS_MARK", "Mark : %s" },
da5b7dfc 221 { "OSMSG_WHOIS_NO_NOTICE", "No_notices : %s" },
d76ed9a9 222 { "OSMSG_UNBAN_DONE", "Ban(s) removed from channel %s." },
223 { "OSMSG_CHANNEL_VOICED", "All users on %s voiced." },
224 { "OSMSG_CHANNEL_DEVOICED", "All voiced users on %s de-voiced." },
225 { "OSMSG_BAD_MODIFIER", "Unknown bad-word modifier $b%s$b." },
226 { "OSMSG_BAD_REDUNDANT", "$b%s$b is already covered by a bad word ($b%s$b)." },
227 { "OSMSG_BAD_GROWING", "Replacing bad word $b%s$b with shorter bad word $b%s$b." },
228 { "OSMSG_BAD_NUKING", " .. and removing redundant bad word $b%s$b." },
229 { "OSMSG_ADDED_BAD", "Added $b%s$b to the bad-word list." },
230 { "OSMSG_REMOVED_BAD", "Removed $b%s$b from the bad-word list." },
231 { "OSMSG_NOT_BAD_WORD", "$b%s$b is not a bad word." },
232 { "OSMSG_ADDED_EXEMPTION", "Added $b%s$b to the bad-word exemption list." },
233 { "OSMSG_ADDED_EXEMPTIONS", "Added %d exception(s) to the bad word list." },
234 { "OSMSG_REMOVED_EXEMPTION", "Removed $b%s$b from the exemption list." },
235 { "OSMSG_NOT_EXEMPT", "$b%s$b is not on the exempt list." },
236 { "OSMSG_ALREADY_TRUSTED", "Host $b%s$b is already trusted (use $bdeltrust$b and then $baddtrust$b to adjust)." },
237 { "OSMSG_NOT_TRUSTED", "Host $b%s$b is not trusted." },
238 { "OSMSG_BAD_IP", "$b%s$b is not a valid IP address" },
239 { "OSMSG_BAD_NUMBER", "$b%s$b is not a number" },
240 { "OSMSG_ADDED_TRUSTED", "Added trusted hosts to the trusted-hosts list." },
241 { "OSMSG_UPDATED_TRUSTED", "Updated trusted host $b%s$b." },
242 { "OSMSG_REMOVED_TRUSTED", "Removed trusted hosts from the trusted-hosts list." },
243 { "OSMSG_CLONE_EXISTS", "Nick $b%s$b is already in use." },
244 { "OSMSG_NOT_A_HOSTMASK", "The hostmask must be in user@host form." },
245 { "OSMSG_BADWORD_LIST", "Bad words: %s" },
246 { "OSMSG_EXEMPTED_LIST", "Exempted channels: %s" },
247 { "OSMSG_GLINE_COUNT", "There are %d glines active on the network." },
d914d1cb 248 { "OSMSG_SHUN_COUNT", "There are %d shuns active on the network." },
1136f709 249 { "OSMSG_NO_GLINE", "$b%s$b is not a known G-line." },
250 { "OSMSG_NO_SHUN", "$b%s$b is not a known Shun" },
d76ed9a9 251 { "OSMSG_LINKS_SERVER", "%s%s (%u clients; %s)" },
252 { "OSMSG_MAX_CLIENTS", "Max clients: %d at %s" },
253 { "OSMSG_NETWORK_INFO", "Total users: %d (%d invisible, %d opers)" },
254 { "OSMSG_RESERVED_LIST", "List of reserved nicks:" },
de9510bc 255 { "OSMSG_TRUSTED_LIST", "$bTrusted Hosts$b" },
256 { "OSMSG_TRUSTED_LIST_HEADER", "IP Address Limit By Time" },
257 { "OSMSG_HOST_IS_TRUSTED", "%-15s %-5s %-10s set %s ago, expires %s" },
258 { "OSMSG_HOST_IS_TRUSTED_DESC", " Reason: %s" },
259 { "OSMSG_TRUSTED_LIST_BAR", "----------------------------------------" },
260 { "OSMSG_TRUSTED_LIST_END", "----------End of Trusted Hosts----------" },
d76ed9a9 261 { "OSMSG_HOST_NOT_TRUSTED", "%s does not have a special trust." },
262 { "OSMSG_UPTIME_STATS", "Uptime: %s (%u lines processed, CPU time %.2fu/%.2fs)" },
263 { "OSMSG_LINE_DUMPED", "Raw line sent." },
264 { "OSMSG_RAW_PARSE_ERROR", "Error parsing raw line (not dumping to uplink)." },
265 { "OSMSG_COLLIDED_NICK", "Now temporarily holding nick $b%s$b." },
266 { "OSMSG_RESERVED_NICK", "Now reserving nick $b%s$b." },
267 { "OSMSG_NICK_UNRESERVED", "Nick $b%s$b is no longer reserved." },
268 { "OSMSG_NOT_RESERVED", "Nick $b%s$b is not reserved." },
269 { "OSMSG_ILLEGAL_REASON", "This channel is illegal." },
270 { "OSMSG_ILLEGAL_KILL_REASON", "Joined an illegal modeless channel - do not repeat." },
271 { "OSMSG_ILLEGAL_CHANNEL", "$b%s$b is an ILLEGAL channel. Do not re-join it." },
272 { "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." },
273 { "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." },
274 { "OSMSG_CLONE_ADDED", "Added clone $b%s$b." },
275 { "OSMSG_CLONE_FAILED", "Unable to add user $b%s$b." },
276 { "OSMSG_NOT_A_CLONE", "Har har. $b%s$b isn't a clone." },
277 { "OSMSG_CLONE_REMOVED", "Removed clone $b%s$b." },
278 { "OSMSG_CLONE_JOINED", "$b%s$b has joined $b%s$b." },
279 { "OSMSG_CLONE_PARTED", "$b%s$b has left $b%s$b." },
280 { "OSMSG_OPS_GIVEN", "I have given ops in $b%s$b to $b%s$b." },
55342ce8 281 { "OSMSG_HOPS_GIVEN", "I have given halfops in $b%s$b to $b%s$b." },
d76ed9a9 282 { "OSMSG_CLONE_SAID", "$b%s$b has spoken to $b%s$b." },
283 { "OSMSG_UNKNOWN_SUBCOMMAND", "$b%s$b is not a valid subcommand of $b%s$b." },
284 { "OSMSG_UNKNOWN_OPTION", "$b%s$b has not been set." },
285 { "OSMSG_OPTION_IS", "$b%s$b is set to $b%s$b." },
286 { "OSMSG_OPTION_ROOT", "The following keys can be queried:" },
287 { "OSMSG_OPTION_LIST", "$b%s$b contains the following values:" },
288 { "OSMSG_OPTION_KEYS", "$b%s$b contains the following keys:" },
289 { "OSMSG_OPTION_LIST_EMPTY", "Empty list." },
290 { "OSMSG_SET_NOT_SET", "$b%s$b does not exist, and cannot be set." },
291 { "OSMSG_SET_BAD_TYPE", "$b%s$b is not a string, and cannot be set." },
292 { "OSMSG_SET_SUCCESS", "$b%s$b has been set to $b%s$b." },
293 { "OSMSG_SETTIME_SUCCESS", "Set time for servers named like $b%s$b." },
294 { "OSMSG_BAD_ACTION", "Unrecognized trace action $b%s$b." },
295 { "OSMSG_USER_SEARCH_RESULTS", "The following users were found:" },
de9510bc 296 { "OSMSG_USER_SEARCH_HEADER", "Nick User@Host (Account)" },
297 { "OSMSG_USER_SEARCH_BAR", "-------------------------------------------" },
8e11460f 298 { "OSMSG_USER_SEARCH_COUNT", "There were %4u matches" },
299 { "OSMSG_USER_SEARCH_COUNT_BAR", "------------ Found %4u matches -----------" },
d82cf2f0 300 { "OSMSG_MARK_NO_MARK", "MARK action requires mark criteria (what do you want to mark them as?)" },
a62ba70c 301 { "OSMSG_SVSJOIN_NO_TARGET", "SVSJOIN action requires chantarget criteria (where should they join?)" },
39c1a4ef 302 { "OSMSG_SVSPART_NO_TARGET", "SVSPART action requires chantarget criteria (where should they join?)" },
d76ed9a9 303 { "OSMSG_CHANNEL_SEARCH_RESULTS", "The following channels were found:" },
304 { "OSMSG_GLINE_SEARCH_RESULTS", "The following glines were found:" },
d914d1cb 305 { "OSMSG_SHUN_SEARCH_RESULTS", "The following shun were found:" },
d76ed9a9 306 { "OSMSG_LOG_SEARCH_RESULTS", "The following log entries were found:" },
307 { "OSMSG_GSYNC_RUNNING", "Synchronizing glines from %s." },
d914d1cb 308 { "OSMSG_SSYNC_RUNNING", "Synchronizing shuns from %s." },
995043b4 309 { "OSMSG_GTRACE_FORMAT", "%s (issued %s by %s, expires %s): %s" },
310 { "OSMSG_STRACE_FORMAT", "%s (issued %s by %s, expires %s): %s" },
d76ed9a9 311 { "OSMSG_GAG_APPLIED", "Gagged $b%s$b, affecting %d users." },
312 { "OSMSG_GAG_ADDED", "Gagged $b%s$b." },
313 { "OSMSG_REDUNDANT_GAG", "Gag $b%s$b is redundant." },
314 { "OSMSG_GAG_NOT_FOUND", "Could not find gag $b%s$b." },
315 { "OSMSG_NO_GAGS", "No gags have been set." },
316 { "OSMSG_UNGAG_APPLIED", "Ungagged $b%s$b, affecting %d users." },
317 { "OSMSG_UNGAG_ADDED", "Ungagged $b%s$b." },
318 { "OSMSG_TIMEQ_INFO", "%u events in timeq; next in %lu seconds." },
319 { "OSMSG_ALERT_EXISTS", "An alert named $b%s$b already exists." },
320 { "OSMSG_UNKNOWN_REACTION", "Unknown alert reaction $b%s$b." },
321 { "OSMSG_ADDED_ALERT", "Added alert named $b%s$b." },
a62ba70c 322 { "OSMSG_ALERT_ADD_FAILED", "Unable to add alert. Check syntax, required parts, and access" },
d76ed9a9 323 { "OSMSG_REMOVED_ALERT", "Removed alert named $b%s$b." },
324 { "OSMSG_NO_SUCH_ALERT", "No alert named $b%s$b could be found." },
eaf715f1 325 { "OSMSG_ALERTS_LIST", "$bCurrent $O alerts matching '$b%s$b'$b" },
de9510bc 326 { "OSMSG_ALERTS_BAR", "----------------------------------------------" },
327 { "OSMSG_ALERTS_HEADER", "Name Action (by Oper)" },
083fb96b 328 { "OSMSG_ALERTS_DESC", " $uCriteria$u: %s" },
329 { "OSMSG_ALERTS_LAST", " $uTriggered$u: %s" },
de9510bc 330 { "OSMSG_ALERT_IS", "$b%-20s$b %-6s (by %s)" },
a947ef71 331 { "OSMSG_ALERT_EXPIRE", " $uExpires:$u: %s" },
de9510bc 332 { "OSMSG_ALERT_END", "----------------End of Alerts-----------------" },
47956fc5 333 /* routing messages */
5c6bff84 334 { "OSMSG_ROUTINGPLAN", "$bRouting Plan(s)$b" },
335 { "OSMSG_ROUTINGPLAN_LIST_HEAD", "$bRouting Plans$b" },
47956fc5 336 { "OSMSG_ROUTINGPLAN_BAR", "----------------------------------------------" },
5c6bff84 337 { "OSMSG_ROUTINGPLAN_END", "------------End of Routing Plan(s)------------" },
47956fc5 338 { "OSMSG_ROUTINGPLAN_OPTION", "%s is set to %s" },
339 { "OSMSG_ROUTINGPLAN_ACTIVE", "Auto routing is active, using plan '%s'." },
340 { "OSMSG_ROUTING_ACTIVATION_ERROR", "There was an error activating the routing plan. Check for loops, and make sure the map includes my own uplink." },
341 { "OSMSG_ROUTINGPLAN_OPTION_NOT_FOUND", "There is no routing plan option '%s'." },
342 { "OSMSG_ROUTINGPLAN_OPTION_NOT_SET", "Option '%s' is not currently set." },
343 { "OSMSG_ROUTINGPLAN_NAME", "$b%s:$b" },
5c6bff84 344 { "OSMSG_ROUTINGPLAN_LIST", "$b%s$b" },
47956fc5 345 { "OSMSG_ROUTINGPLAN_SERVER"," %s:%d <-- %s[%d/%s] (%s)" },
346 { "OSMSG_ADDPLAN_SUCCESS", "Added new routing plan '%s'." },
ce9266cf 347 { "OSMSG_ADDPLAN_FAILED", "Could not add new plan '%s' (does it already exist?)." },
47956fc5 348 { "OSMSG_INVALID_PLAN", "That routing plan name is not valid." },
349 { "OSMSG_PLAN_DELETED", "The routing plan was sucessfully deleted." },
350 { "OSMSG_PLAN_NOT_FOUND", "There is no routing plan called '%s'." },
351 { "OSMSG_PLAN_SERVER_ADDED", "Added %s to the routing plan." },
352 { "OSMSG_PLAN_SERVER_DELETED", "The server has been deleted." },
353 { "OSMSG_PLAN_SERVER_NOT_FOUND", "The server '%s' was not found in that routing plan." },
354 { "OSMSG_ROUTING_DISABLED", "Routing is now disabled." },
355 { "OSMSG_DOWNLINKS_FORMAT_A", "%s%s-$b%s$b [%s]" },
356 { "OSMSG_DOWNLINKS_FORMAT_B", "$b%s$b (me)" },
357 { "OSMSG_ROUTELIST_EMPTY", "No servers in route list" },
358 { "OSMSG_ROUTELIST_AS_PLANNED", "Routing plan: Servers as they SHOULD be linked" },
359 { "OSMSG_MAP_CENTERED", "map %s centered, Maxdepth:%d" },
360 { "OSMSG_NO_SERVERS_MISSING", "No servers are missing." },
361 { "OSMSG_CONNECTING_MISSING", "Attempted to connect %d missing servers." },
362 { "OSMSG_CONNECT", "->connect %s %d %s" },
363 { "OSMSG_SQUIT", "->squit %s" },
364 { "OSMSG_COULDNT_FIND_SERVER", "Couldnt find %s, so using %s to link %s" },
365 { "OSMSG_INSPECTING_SERVER", "Inspecting server [%s]" },
366 { "OSMSG_REROUTING_ACC_MAP", "Rerouting network according to loaded map.." },
964abe6b 367 { "OSMSG_REROUTING_NOTCONFIGURED", "You have not configured routing. See $/msg $O help routing$b." },
47956fc5 368 { "OSMSG_CONNECTING_MISSING_ONLY", "Connecting missing servers only.." },
369 { "OSMSG_NO_ROUTING_NECESSARY", "No rerouting appears necessary." },
370 { "OSMSG_TESTING_REROUTE", "Testing Reroute(): Commands not sent to socket.." },
371 { "OSMSG_INVALID_DIRECTIVE", "Reroute(): Invalid directive %s", },
372 { "OSMSG_UPLINKS_MISSING", "%d servers' uplinks were missing, and were not connected." },
373 { "OSMSG_REROUTE_COMPLETE", "Reroute complete: Moved %d, connected %d, total %d changes." },
374 /* end of routing */
d76ed9a9 375 { "OSMSG_REHASH_COMPLETE", "Completed rehash of configuration database." },
376 { "OSMSG_REHASH_FAILED", "Rehash of configuration database failed, previous configuration is intact." },
377 { "OSMSG_REOPEN_COMPLETE", "Closed and reopened all log files." },
378 { "OSMSG_RECONNECTING", "Reconnecting to my uplink." },
379 { "OSMSG_NUMERIC_COLLIDE", "Numeric %d (%s) is already in use." },
380 { "OSMSG_NAME_COLLIDE", "That name is already in use." },
381 { "OSMSG_SRV_CREATE_FAILED", "Server creation failed -- check log files." },
382 { "OSMSG_SERVER_JUPED", "Added new jupe server %s." },
df5f6070 383 { "OSMSG_INVALID_NUMERIC", "Invalid numeric" },
384 { "OSMSG_INVALID_SERVERNAME", "Server name must contain a '.'." },
d76ed9a9 385 { "OSMSG_SERVER_NOT_JUPE", "That server is not a juped server." },
386 { "OSMSG_SERVER_UNJUPED", "Server jupe removed." },
c52666c6 387 /*
d76ed9a9 388 { "OSMSG_WARN_ADDED", "Added channel activity warning for $b%s$b (%s)" },
389 { "OSMSG_WARN_EXISTS", "Channel activity warning for $b%s$b already exists." },
390 { "OSMSG_WARN_DELETED", "Removed channel activity warning for $b%s$b" },
391 { "OSMSG_WARN_NOEXIST", "Channel activity warning for $b%s$b does not exist." },
392 { "OSMSG_WARN_LISTSTART", "Channel activity warnings:" },
393 { "OSMSG_WARN_LISTENTRY", "%s (%s)" },
394 { "OSMSG_WARN_LISTEND", "End of activity warning list." },
c52666c6 395 */
d76ed9a9 396 { "OSMSG_UPLINK_CONNECTING", "Establishing connection with %s (%s:%d)." },
397 { "OSMSG_CURRENT_UPLINK", "$b%s$b is already the current uplink." },
398 { "OSMSG_INVALID_UPLINK", "$b%s$b is not a valid uplink name." },
399 { "OSMSG_UPLINK_DISABLED", "$b%s$b is a disabled or unavailable uplink." },
400 { "OSMSG_UPLINK_START", "Uplink $b%s$b:" },
401 { "OSMSG_UPLINK_ADDRESS", "Address: %s:%d" },
402 { "OSMSG_STUPID_GLINE", "Gline %s? Now $bthat$b would be smooth." },
d914d1cb 403 { "OSMSG_STUPID_SHUN", "Shun %s? Now $bthat$b would be smooth." },
d76ed9a9 404 { "OSMSG_ACCOUNTMASK_AUTHED", "Invalid criteria: it is impossible to match an account mask but not be authed" },
405 { "OSMSG_CHANINFO_HEADER", "%s Information" },
406 { "OSMSG_CHANINFO_TIMESTAMP", "Created on: %a %b %d %H:%M:%S %Y (%s)" },
407 { "OSMSG_CHANINFO_MODES", "Modes: %s" },
408 { "OSMSG_CHANINFO_MODES_BADWORD", "Modes: %s; bad-word channel" },
409 { "OSMSG_CHANINFO_TOPIC", "Topic (set by %%s, %a %b %d %H:%M:%S %Y): %%s" },
410 { "OSMSG_CHANINFO_TOPIC_UNKNOWN", "Topic: (none / not gathered)" },
411 { "OSMSG_CHANINFO_BAN_COUNT", "Bans (%d):" },
412 { "OSMSG_CHANINFO_BAN", "%%s by %%s (%a %b %d %H:%M:%S %Y)" },
2aef5f4b 413 { "OSMSG_CHANINFO_EXEMPT_COUNT", "Exempts (%d):" },
414 { "OSMSG_CHANINFO_EXEMPT", "%%s by %%s (%a %b %d %H:%M:%S %Y)" },
d76ed9a9 415 { "OSMSG_CHANINFO_MANY_USERS", "%d users (\"/msg $S %s %s users\" for the list)" },
416 { "OSMSG_CHANINFO_USER_COUNT", "Users (%d):" },
417 { "OSMSG_CSEARCH_CHANNEL_INFO", "%s [%d users] %s %s" },
1136f709 418 { "OSMSG_TRACE_MAX_CHANNELS", "You may not use the 'channel' criterion more than %d times." },
27eaa617 419 { "OSMSG_INVALID_REGEX", "Invalid regex: %s: %s (%d)" },
ec311f39 420 { "OSMSG_TRACK_DISABLED", "Tracking is not currently compiled into X3" },
7637f48f 421 { "OSMSG_MAXUSERS_RESET", "Max clients has been reset to $b%d$b" },
1136f709 422 { "OSMSG_FORCEKICK_LOCAL", "You cannot kick $b%s$b forcefully." },
08895577 423
424 { "OSMSG_DEFCON_INVALID", "DefCon level %d is invalid, please choose a value between 1 and 5" },
425 { "OSMSG_DEFCON_ALLOWING_ALL", "DefCon is at level 5 and allowing everything" },
426 { "OSMSG_DEFCON_DISALLOWING", "DefCon is at level %d and enforcing:" },
427 { "OSMSG_DEFCON_NO_NEW_CHANNELS", "No Channel Registrations" },
428 { "OSMSG_DEFCON_NO_NEW_NICKS", "No Nickname/Account Registrations" },
429 { "OSMSG_DEFCON_NO_MODE_CHANGE", "No Channel Mode Changes" },
430 { "OSMSG_DEFCON_NO_NEW_CLIENTS", "No New Clients" },
431 { "OSMSG_DEFCON_FORCE_CHANMODES", "Forcing Channel Mode(s): %s" },
432 { "OSMSG_DEFCON_REDUCE_SESSION", "Forcing Reduced Session: %d" },
433 { "OSMSG_DEFCON_OPER_ONLY", "Allowing Services Communication With Opers Only" },
434 { "OSMSG_DEFCON_SILENT_OPER_ONLY", "Allowing Services Communication With Opers Only AND Silently Ignoring Regular Users" },
435 { "OSMSG_DEFCON_GLINE_NEW_CLIENTS", "Glining New Clients" },
0272358e 436 { "OSMSG_DEFCON_SHUN_NEW_CLIENTS", "Shunning New Clients" },
08895577 437 { "OSMSG_DEFCON_NO_NEW_MEMOS", "Disallowing New Memos" },
438
6c34bb5a 439 { "OSMSG_PRIV_UNKNOWN", "Unknown privilege flag %s, see /msg $O HELP PRIVFLAGS for a flag list" },
440 { "OSMSG_PRIV_SET", "Privilege flag %s has been %sset" },
441
d76ed9a9 442 { NULL, NULL }
443};
444
b1bf690d 445#define OPSERV_SYNTAX() svccmd_send_help_brief(user, opserv, cmd)
d76ed9a9 446
08895577 447int DefConLevel = 5;
448int DefCon[6];
449int DefConTimeOut;
450int GlobalOnDefcon = 0;
451int GlobalOnDefconMore = 0;
452int DefConGlineExpire;
453int DefConModesSet = 0;
08895577 454unsigned int DefConSessionLimit;
455char *DefConChanModes;
456char *DefConGlineReason;
457char *DefConMessage;
458char *DefConOffMessage;
459
ec311f39 460extern void add_track_user(struct userNode *user);
d76ed9a9 461typedef int (*discrim_search_func)(struct userNode *match, void *extra);
462
463struct userNode *opserv;
258d1427 464static struct service *opserv_service;
d76ed9a9 465
de9510bc 466/*static dict_t opserv_chan_warn; */ /* data is char* */
d76ed9a9 467static dict_t opserv_reserved_nick_dict; /* data is struct userNode* */
468static struct string_list *opserv_bad_words;
469static dict_t opserv_exempt_channels; /* data is not used */
470static dict_t opserv_trusted_hosts; /* data is struct trusted_host* */
47956fc5 471static dict_t opserv_routing_plans; /* data is struct routingPlan */
472static dict_t opserv_routing_plan_options; /* data is a dict_t key->val list*/
473static dict_t opserv_waiting_connections; /* data is struct waitingConnection */
d76ed9a9 474static dict_t opserv_hostinfo_dict; /* data is struct opserv_hostinfo* */
475static dict_t opserv_user_alerts; /* data is struct opserv_user_alert* */
476static dict_t opserv_nick_based_alerts; /* data is struct opserv_user_alert* */
b7594203 477static dict_t opserv_account_based_alerts; /* data is struct opserv_user_alert* */
d76ed9a9 478static dict_t opserv_channel_alerts; /* data is struct opserv_user_alert* */
479static struct module *opserv_module;
480static struct log_type *OS_LOG;
481static unsigned int new_user_flood;
482static char *level_strings[1001];
7637f48f 483struct string_list *autojoin_channels;
47956fc5 484struct route *opserv_route = NULL; /* Main active routing table from activate_routing()*/
d76ed9a9 485
486static struct {
487 struct chanNode *debug_channel;
488 struct chanNode *alert_channel;
489 struct chanNode *staff_auth_channel;
490 struct policer_params *join_policer_params;
491 struct policer new_user_policer;
492 unsigned long untrusted_max;
493 unsigned long clone_gline_duration;
494 unsigned long block_gline_duration;
d914d1cb 495 unsigned long block_shun_duration;
d76ed9a9 496 unsigned long purge_lock_delay;
497 unsigned long join_flood_moderate;
498 unsigned long join_flood_moderate_threshold;
5a1daaab 499 unsigned long admin_level;
1c5f6697 500 unsigned long silent_level;
d76ed9a9 501} opserv_conf;
502
503struct trusted_host {
504 char *ipaddr;
505 char *issuer;
506 char *reason;
507 unsigned long limit;
508 time_t issued;
509 time_t expires;
510};
511
512struct gag_entry {
513 char *mask;
514 char *owner;
515 char *reason;
516 time_t expires;
517 struct gag_entry *next;
518};
519
520static struct gag_entry *gagList;
521
522struct opserv_hostinfo {
523 struct userList clients;
524 struct trusted_host *trusted;
525};
526
527static void
528opserv_free_hostinfo(void *data)
529{
530 struct opserv_hostinfo *ohi = data;
531 userList_clean(&ohi->clients);
532 free(ohi);
533}
534
47956fc5 535static void
536opserv_free_waiting_connection(void *data)
537{
538 struct waitingConnection *wc = data;
539 free(wc->server);
540 free(wc->target);
541 free(wc);
542}
543
1136f709 544#define DISCRIM_MAX_CHANS 20
545
d76ed9a9 546typedef struct opservDiscrim {
1136f709 547 struct chanNode *channels[DISCRIM_MAX_CHANS];
548 unsigned int channel_count;
549 char *mask_nick, *mask_ident, *mask_host, *mask_info, *mask_version, *server, *reason, *accountmask, *chantarget, *mark, *mask_mark, *modes, *notice_target;
2f61d1d7 550 irc_in_addr_t ip_mask;
551 unsigned long limit;
552 time_t min_ts, max_ts;
0e08a8e0 553 regex_t regex_nick, regex_ident, regex_host, regex_info, regex_version;
554 unsigned int has_regex_nick : 1, has_regex_ident : 1, has_regex_host : 1, has_regex_info : 1, has_regex_version : 1;
d76ed9a9 555 unsigned int min_level, max_level, domain_depth, duration, min_clones, min_channels, max_channels;
2f61d1d7 556 unsigned char ip_mask_bits;
1136f709 557 unsigned int match_opers : 1, match_trusted : 1, option_log : 1;
558 unsigned int chan_req_modes[DISCRIM_MAX_CHANS], chan_no_modes[DISCRIM_MAX_CHANS];
d76ed9a9 559 int authed : 2, info_space : 2;
63665495 560 unsigned int intra_scmp : 2, intra_dcmp : 2;
27eaa617 561 unsigned int use_regex : 1;
1c5f6697 562 unsigned int silent : 1;
5aa400d2 563 unsigned int checkrestrictions : 2;
d76ed9a9 564} *discrim_t;
565
566struct discrim_and_source {
567 discrim_t discrim;
568 struct userNode *source;
258d1427 569 struct userNode *destination;
d76ed9a9 570 dict_t dict;
571 unsigned int disp_limit;
572};
573
258d1427 574static discrim_t opserv_discrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[], int allow_channel);
d76ed9a9 575static unsigned int opserv_discrim_search(discrim_t discrim, discrim_search_func dsf, void *data);
576static int gag_helper_func(struct userNode *match, void *extra);
577static int ungag_helper_func(struct userNode *match, void *extra);
a947ef71 578static void alert_expire(void* name);
d76ed9a9 579
580typedef enum {
581 REACT_NOTICE,
582 REACT_KILL,
d914d1cb 583 REACT_GLINE,
ec311f39 584 REACT_TRACK,
c408f18a 585 REACT_SHUN,
0e08a8e0 586 REACT_SVSJOIN,
39c1a4ef 587 REACT_SVSPART,
d82cf2f0 588 REACT_VERSION,
890866b0 589 REACT_MARK,
590 REACT_NOTICEUSER,
591 REACT_MSGUSER
d76ed9a9 592} opserv_alert_reaction;
593
594struct opserv_user_alert {
595 char *owner;
596 char *text_discrim, *split_discrim;
597 discrim_t discrim;
598 opserv_alert_reaction reaction;
083fb96b 599 int last;
a947ef71 600 time_t expire;
d76ed9a9 601};
602
603/* funny type to make it acceptible to dict_set_free_data, far below */
604static void
605opserv_free_user_alert(void *data)
606{
607 struct opserv_user_alert *alert = data;
1136f709 608 unsigned int i;
609 for(i = 0; i < alert->discrim->channel_count; i++)
610 UnlockChannel(alert->discrim->channels[i]);
d76ed9a9 611 free(alert->owner);
612 free(alert->text_discrim);
613 free(alert->split_discrim);
27eaa617 614 if(alert->discrim->has_regex_nick)
615 regfree(&alert->discrim->regex_nick);
616 if(alert->discrim->has_regex_ident)
617 regfree(&alert->discrim->regex_ident);
618 if(alert->discrim->has_regex_host)
619 regfree(&alert->discrim->regex_host);
620 if(alert->discrim->has_regex_info)
621 regfree(&alert->discrim->regex_info);
0e08a8e0 622 if(alert->discrim->has_regex_version)
623 regfree(&alert->discrim->regex_version);
d76ed9a9 624 free(alert->discrim->reason);
625 free(alert->discrim);
626 free(alert);
627}
628
1136f709 629#if defined(GCC_VARMACROS)
630# define opserv_debug(ARGS...) do { if (opserv_conf.debug_channel) send_channel_notice(opserv_conf.debug_channel, opserv, ARGS); } while (0)
631# define opserv_alert(ARGS...) do { if (opserv_conf.alert_channel) send_channel_notice(opserv_conf.alert_channel, opserv, ARGS); } while (0)
632# define opserv_custom_alert(CHAN, ARGS...) do { if (CHAN) send_target_message(4, (CHAN), opserv, ARGS); else if (opserv_conf.alert_channel) send_channel_notice(opserv_conf.alert_channel, opserv, ARGS); } while (0)
633#elif defined(C99_VARMACROS)
634# define opserv_debug(...) do { if (opserv_conf.debug_channel) send_channel_notice(opserv_conf.debug_channel, opserv, __VA_ARGS__); } while (0)
635# define opserv_alert(...) do { if (opserv_conf.alert_channel) send_channel_notice(opserv_conf.alert_channel, opserv, __VA_ARGS__); } while (0)
636# define opserv_custom_alert(chan, ...) do { if (chan) send_target_message(4, chan, opserv, __VA_ARGS__); else if (opserv_conf.alert_channel) send_channel_notice(opserv_conf.alert_channel, opserv, __VA_ARGS__); } while (0)
637#endif
7637f48f 638
08895577 639char *defconReverseModes(const char *modes)
640{
641 char *newmodes = NULL;
642 unsigned int i = 0;
643 if (!modes) {
644 return NULL;
645 }
646 if (!(newmodes = malloc(sizeof(char) * strlen(modes) + 1))) {
647 return NULL;
648 }
649 for (i = 0; i < strlen(modes); i++) {
650 if (modes[i] == '+')
651 newmodes[i] = '-';
652 else if (modes[i] == '-')
653 newmodes[i] = '+';
654 else
655 newmodes[i] = modes[i];
656 }
657 newmodes[i] = '\0';
658 return newmodes;
659}
660
661int checkDefCon(int level)
662{
663 return DefCon[DefConLevel] & level;
664}
665
666void showDefConSettings(struct userNode *user, struct svccmd *cmd)
667{
668 if (DefConLevel == 5) {
669 reply("OSMSG_DEFCON_ALLOWING_ALL");
670 return;
671 } else
672 reply("OSMSG_DEFCON_DISALLOWING", DefConLevel);
673
674 if (checkDefCon(DEFCON_NO_NEW_CHANNELS))
675 reply("OSMSG_DEFCON_NO_NEW_CHANNELS");
676
677 if (checkDefCon(DEFCON_NO_NEW_NICKS))
678 reply("OSMSG_DEFCON_NO_NEW_NICKS");
679
680 if (checkDefCon(DEFCON_NO_MODE_CHANGE))
681 reply("OSMSG_DEFCON_NO_MODE_CHANGE");
682
683 if (checkDefCon(DEFCON_FORCE_CHAN_MODES) && (DefConChanModes))
684 reply("OSMSG_DEFCON_FORCE_CHANMODES", DefConChanModes);
685
686 if (checkDefCon(DEFCON_REDUCE_SESSION))
687 reply("OSMSG_DEFCON_REDUCE_SESSION", DefConSessionLimit);
688
689 if (checkDefCon(DEFCON_NO_NEW_CLIENTS))
690 reply("OSMSG_DEFCON_NO_NEW_CLIENTS");
691
692 if (checkDefCon(DEFCON_OPER_ONLY))
693 reply("OSMSG_DEFCON_OPER_ONLY");
694
695 if (checkDefCon(DEFCON_SILENT_OPER_ONLY))
696 reply("OSMSG_DEFCON_SILENT_OPER_ONLY");
697
698 if (checkDefCon(DEFCON_GLINE_NEW_CLIENTS))
699 reply("OSMSG_DEFCON_GLINE_NEW_CLIENTS");
700
0272358e 701 if (checkDefCon(DEFCON_SHUN_NEW_CLIENTS))
702 reply("OSMSG_DEFCON_SHUN_NEW_CLIENTS");
703
08895577 704 if (checkDefCon(DEFCON_NO_NEW_MEMOS))
705 reply("OSMSG_DEFCON_NO_NEW_MEMOS");
706
707 return;
708}
709
710void do_mass_mode(char *modes)
711{
712 dict_iterator_t it;
713
714 if (!modes)
715 return;
716
717 for (it = dict_first(channels); it; it = iter_next(it)) {
718 struct chanNode *chan = iter_data(it);
719
720 irc_mode(opserv, chan, modes);
721 }
722
723}
724
725void DefConProcess(struct userNode *user)
726{
727 char *newmodes;
08895577 728
985d4109 729 if (GlobalOnDefcon)
730 global_message_args(MESSAGE_RECIPIENT_LUSERS, "DEFCON_NETWORK_CHANGED", DefConLevel);
08895577 731
985d4109 732 if (GlobalOnDefconMore && GlobalOnDefcon)
733 global_message(MESSAGE_RECIPIENT_LUSERS, DefConMessage);
08895577 734
735 if ((DefConLevel == 5) && !GlobalOnDefconMore && !GlobalOnDefcon)
985d4109 736 global_message(MESSAGE_RECIPIENT_LUSERS, DefConOffMessage);
08895577 737
985d4109 738 if (user)
739 global_message_args(MESSAGE_RECIPIENT_OPERS, "DEFCON_OPER_LEVEL_CHANGE", user->nick, DefConLevel);
740 else
741 global_message_args(MESSAGE_RECIPIENT_OPERS, "DEFCON_TIMEOUT_LEVEL_CHANGE", DefConLevel);
08895577 742
743 if (checkDefCon(DEFCON_FORCE_CHAN_MODES)) {
744 if (DefConChanModes && !DefConModesSet) {
745 if (DefConChanModes[0] == '+' || DefConChanModes[0] == '-') {
746 do_mass_mode(DefConChanModes);
747 DefConModesSet = 1;
748 }
749 }
750 } else {
751 if (DefConChanModes && (DefConModesSet != 0)) {
752 if (DefConChanModes[0] == '+' || DefConChanModes[0] == '-') {
753 if ((newmodes = defconReverseModes(DefConChanModes))) {
754 do_mass_mode(newmodes);
755 free(newmodes);
756 }
757 DefConModesSet = 0;
758 }
759 }
760 }
761
762 return;
763}
764
765void
766defcon_timeout(UNUSED_ARG(void *data))
767{
768 DefConLevel = 5;
769 DefConProcess(NULL);
770}
771
772static MODCMD_FUNC(cmd_defcon)
773{
774 if ((argc < 2) || (atoi(argv[1]) == DefConLevel)) {
775 showDefConSettings(user, cmd);
776 return 1;
777 }
778
779 if ((atoi(argv[1]) < 1) || (atoi(argv[1]) > 5)) {
780 reply("OSMSG_DEFCON_INVALID", atoi(argv[1]));
781 return 0;
782 }
783
784 DefConLevel = atoi(argv[1]);
785 showDefConSettings(user, cmd);
786
787 if (DefConTimeOut > 0) {
9079d26c 788 timeq_del(0, defcon_timeout, NULL, TIMEQ_IGNORE_DATA | TIMEQ_IGNORE_WHEN);
08895577 789 timeq_add(now + DefConTimeOut, defcon_timeout, NULL);
790 }
791
792 DefConProcess(user);
793 return 1;
794}
795
6c34bb5a 796/* TODO
797static MODCMD_FUNC(cmd_privallow)
798{
799//privallow servername/username +/-flag (global is set in conf)
800}
801
802static MODCMD_FUNC(cmd_privdissallow)
803{
804//privdisallow servername/username +/-flag (global is set in conf)
805}
806
807static MODCMD_FUNC(cmd_privlist)
808{
809//privlist servername/user (global with none)
810}
811*/
812
813static MODCMD_FUNC(cmd_privset)
814{
815 struct userNode *target;
816 char *flag;
817 int add = PRIV_ADD;
818
819 flag = argv[2];
820 if (*flag == '-') {
821 add = PRIV_DEL;
822 flag++;
823 } else if (*flag == '+') {
824 add = PRIV_ADD;
825 flag++;
826 }
827
828 target = GetUserH(argv[1]);
829 if (!target) {
830 reply("MSG_NICK_UNKNOWN", argv[1]);
831 return 0;
832 }
833
834 if (check_priv(flag)) {
835 irc_privs(target, flag, add);
836 reply("OSMSG_PRIV_SET", argv[2], (add == 1) ? "" : "un");
837 } else {
838 reply("OSMSG_PRIV_UNKNOWN", argv[2]);
839 return 0;
840 }
841
842 return 1;
843}
844
d76ed9a9 845/* A lot of these commands are very similar to what ChanServ can do,
846 * but OpServ can do them even on channels that aren't registered.
847 */
848
849static MODCMD_FUNC(cmd_access)
850{
851 struct handle_info *hi;
852 const char *target;
853 unsigned int res;
854
855 target = (argc > 1) ? (const char*)argv[1] : user->nick;
856 if (!irccasecmp(target, "*")) {
857 nickserv_show_oper_accounts(user, cmd);
858 return 1;
859 }
860 if (!(hi = modcmd_get_handle_info(user, target)))
861 return 0;
258d1427 862 res = (argc > 2) ? oper_try_set_access(user, opserv_service->bot, hi, strtoul(argv[2], NULL, 0)) : 0;
d76ed9a9 863 reply("OSMSG_USER_ACCESS_IS", target, hi->handle, hi->opserv_level);
864 return res;
865}
866
867static MODCMD_FUNC(cmd_ban)
868{
869 struct mod_chanmode change;
870 struct userNode *victim;
871
872 mod_chanmode_init(&change);
873 change.argc = 1;
874 change.args[0].mode = MODE_BAN;
875 if (is_ircmask(argv[1]))
a32da4c7 876 change.args[0].u.hostmask = strdup(argv[1]);
d76ed9a9 877 else if ((victim = GetUserH(argv[1])))
a32da4c7 878 change.args[0].u.hostmask = generate_hostmask(victim, 0);
d76ed9a9 879 else {
258d1427 880 reply("OSMSG_INVALID_IRCMASK", argv[1]);
881 return 0;
d76ed9a9 882 }
883 modcmd_chanmode_announce(&change);
a32da4c7 884 reply("OSMSG_ADDED_BAN", change.args[0].u.hostmask, channel->name);
885 free((char*)change.args[0].u.hostmask);
d76ed9a9 886 return 1;
887}
888
889static MODCMD_FUNC(cmd_chaninfo)
890{
891 char buffer[MAXLEN];
892 const char *fmt;
893 struct banNode *ban;
2aef5f4b 894 struct exemptNode *exempt;
d76ed9a9 895 struct modeNode *moden;
896 unsigned int n;
897
898 reply("OSMSG_CHANINFO_HEADER", channel->name);
899 fmt = user_find_message(user, "OSMSG_CHANINFO_TIMESTAMP");
900 strftime(buffer, sizeof(buffer), fmt, gmtime(&channel->timestamp));
901 send_message_type(4, user, cmd->parent->bot, "%s", buffer);
902 irc_make_chanmode(channel, buffer);
903 if (channel->bad_channel)
904 reply("OSMSG_CHANINFO_MODES_BADWORD", buffer);
905 else
906 reply("OSMSG_CHANINFO_MODES", buffer);
907 if (channel->topic_time) {
908 fmt = user_find_message(user, "OSMSG_CHANINFO_TOPIC");
909 strftime(buffer, sizeof(buffer), fmt, gmtime(&channel->topic_time));
910 send_message_type(4, user, cmd->parent->bot, buffer, channel->topic_nick, channel->topic);
911 } else {
258d1427 912 irc_fetchtopic(cmd->parent->bot, channel->name);
913 reply("OSMSG_CHANINFO_TOPIC_UNKNOWN");
d76ed9a9 914 }
915 if (channel->banlist.used) {
258d1427 916 reply("OSMSG_CHANINFO_BAN_COUNT", channel->banlist.used);
d76ed9a9 917 fmt = user_find_message(user, "OSMSG_CHANINFO_BAN");
258d1427 918 for (n = 0; n < channel->banlist.used; n++) {
919 ban = channel->banlist.list[n];
920 strftime(buffer, sizeof(buffer), fmt, localtime(&ban->set));
921 send_message_type(4, user, cmd->parent->bot, buffer, ban->ban, ban->who);
922 }
d76ed9a9 923 }
2aef5f4b 924 if (channel->exemptlist.used) {
925 reply("OSMSG_CHANINFO_EXEMPT_COUNT", channel->exemptlist.used);
926 fmt = user_find_message(user, "OSMSG_CHANINFO_EXEMPT");
927 for (n = 0; n < channel->exemptlist.used; n++) {
928 exempt = channel->exemptlist.list[n];
929 strftime(buffer, sizeof(buffer), fmt, localtime(&exempt->set));
930 send_message_type(4, user, cmd->parent->bot, buffer, exempt->exempt, exempt->who);
931 }
932 }
d76ed9a9 933 if ((argc < 2) && (channel->members.used >= 50)) {
934 /* early out unless they ask for users */
935 reply("OSMSG_CHANINFO_MANY_USERS", channel->members.used, argv[0], channel->name);
936 return 1;
937 }
938 reply("OSMSG_CHANINFO_USER_COUNT", channel->members.used);
939 for (n=0; n<channel->members.used; n++) {
258d1427 940 moden = channel->members.list[n];
2f61d1d7 941 if (moden->modes & MODE_CHANOP) {
942 if (moden->oplevel >= 0)
943 send_message_type(4, user, cmd->parent->bot, " @%s:%d (%s@%s)", moden->user->nick, moden->oplevel, moden->user->ident, moden->user->hostname);
944 else
945 send_message_type(4, user, cmd->parent->bot, " @%s (%s@%s)", moden->user->nick, moden->user->ident, moden->user->hostname);
946 }
d76ed9a9 947 }
55342ce8 948 for (n=0; n<channel->members.used; n++) {
949 moden = channel->members.list[n];
950 if ((moden->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE)) == MODE_HALFOP)
951 send_message_type(4, user, cmd->parent->bot, " %s%s (%s@%s)", "%", moden->user->nick, moden->user->ident, moden->user->hostname);
952 }
d76ed9a9 953 for (n=0; n<channel->members.used; n++) {
258d1427 954 moden = channel->members.list[n];
955 if ((moden->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE)) == MODE_VOICE)
d76ed9a9 956 send_message_type(4, user, cmd->parent->bot, " +%s (%s@%s)", moden->user->nick, moden->user->ident, moden->user->hostname);
957 }
958 for (n=0; n<channel->members.used; n++) {
258d1427 959 moden = channel->members.list[n];
960 if ((moden->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE)) == 0)
d76ed9a9 961 send_message_type(4, user, cmd->parent->bot, " %s (%s@%s)", moden->user->nick, moden->user->ident, moden->user->hostname);
962 }
963 return 1;
964}
965
de9510bc 966/* This command has been replaced by 'alert notice channel #foo' */
967/*
2f61d1d7 968static MODCMD_FUNC(cmd_warn)
d76ed9a9 969{
970 char *reason, *message;
971
972 if (!IsChannelName(argv[1])) {
258d1427 973 reply("OSMSG_NEED_CHANNEL", argv[0]);
974 return 0;
d76ed9a9 975 }
976 reason = dict_find(opserv_chan_warn, argv[1], NULL);
977 if (reason) {
978 reply("OSMSG_WARN_EXISTS", argv[1]);
979 return 0;
980 }
981 if (argv[2])
982 reason = strdup(unsplit_string(argv+2, argc-2, NULL));
983 else
984 reason = strdup("No reason");
985 dict_insert(opserv_chan_warn, strdup(argv[1]), reason);
986 reply("OSMSG_WARN_ADDED", argv[1], reason);
987 if (dict_find(channels, argv[1], NULL)) {
57692f5e 988 global_message_args(MESSAGE_RECIPIENT_OPERS, "OSMSG_CHANNEL_ACTIVITY_WARN" argv[1], reason);
d76ed9a9 989 }
990 return 1;
991}
992
993static MODCMD_FUNC(cmd_unwarn)
994{
995 if ((argc < 2) || !IsChannelName(argv[1])) {
996 reply("OSMSG_NEED_CHANNEL", argv[0]);
258d1427 997 return 0;
d76ed9a9 998 }
999 if (!dict_remove(opserv_chan_warn, argv[1])) {
1000 reply("OSMSG_WARN_NOEXIST", argv[1]);
1001 return 0;
1002 }
1003 reply("OSMSG_WARN_DELETED", argv[1]);
1004 return 1;
1005}
de9510bc 1006*/
d76ed9a9 1007
1008static MODCMD_FUNC(cmd_clearbans)
1009{
1010 struct mod_chanmode *change;
1011 unsigned int ii;
1012
1013 change = mod_chanmode_alloc(channel->banlist.used);
1014 for (ii=0; ii<channel->banlist.used; ii++) {
1015 change->args[ii].mode = MODE_REMOVE | MODE_BAN;
ec1a68c8 1016 change->args[ii].u.hostmask = strdup(channel->banlist.list[ii]->ban);
d76ed9a9 1017 }
1018 modcmd_chanmode_announce(change);
ec1a68c8 1019 for (ii=0; ii<change->argc; ++ii)
1020 free((char*)change->args[ii].u.hostmask);
d76ed9a9 1021 mod_chanmode_free(change);
1022 reply("OSMSG_CLEARBANS_DONE", channel->name);
1023 return 1;
1024}
1025
1026static MODCMD_FUNC(cmd_clearmodes)
1027{
1028 struct mod_chanmode change;
1029
1030 if (!channel->modes) {
258d1427 1031 reply("OSMSG_NO_CHANNEL_MODES", channel->name);
d76ed9a9 1032 return 0;
1033 }
1034 mod_chanmode_init(&change);
1035 change.modes_clear = channel->modes;
1036 modcmd_chanmode_announce(&change);
1037 reply("OSMSG_CLEARMODES_DONE", channel->name);
1038 return 1;
1039}
1040
1041static MODCMD_FUNC(cmd_deop)
1042{
1043 struct mod_chanmode *change;
1044 unsigned int arg, count;
1045
1046 change = mod_chanmode_alloc(argc-1);
1047 for (arg = 1, count = 0; arg < argc; ++arg) {
1048 struct userNode *victim = GetUserH(argv[arg]);
1049 struct modeNode *mn;
258d1427 1050 if (!victim || IsService(victim)
d76ed9a9 1051 || !(mn = GetUserMode(channel, victim))
1052 || !(mn->modes & MODE_CHANOP))
1053 continue;
1054 change->args[count].mode = MODE_REMOVE | MODE_CHANOP;
a32da4c7 1055 change->args[count++].u.member = mn;
d76ed9a9 1056 }
1057 if (count) {
1058 change->argc = count;
1059 modcmd_chanmode_announce(change);
1060 }
1061 mod_chanmode_free(change);
1062 reply("OSMSG_DEOP_DONE");
1063 return 1;
1064}
1065
55342ce8 1066static MODCMD_FUNC(cmd_dehop)
1067{
1068 struct mod_chanmode *change;
1069 unsigned int arg, count;
1070
1071 change = mod_chanmode_alloc(argc-1);
1072 for (arg = 1, count = 0; arg < argc; ++arg) {
1073 struct userNode *victim = GetUserH(argv[arg]);
1074 struct modeNode *mn;
1075 if (!victim || IsService(victim)
1076 || !(mn = GetUserMode(channel, victim))
1077 || !(mn->modes & MODE_HALFOP))
1078 continue;
1079 change->args[count].mode = MODE_REMOVE | MODE_HALFOP;
11408ce4 1080 change->args[count++].u.member = mn;
55342ce8 1081 }
1082 if (count) {
1083 change->argc = count;
1084 modcmd_chanmode_announce(change);
1085 }
1086 mod_chanmode_free(change);
1087 reply("OSMSG_DEHOP_DONE");
1088 return 1;
1089}
1090
d76ed9a9 1091static MODCMD_FUNC(cmd_deopall)
1092{
1093 struct mod_chanmode *change;
1094 unsigned int ii, count;
1095
1096 change = mod_chanmode_alloc(channel->members.used);
1097 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 1098 struct modeNode *mn = channel->members.list[ii];
1099 if (IsService(mn->user) || !(mn->modes & MODE_CHANOP))
d76ed9a9 1100 continue;
1101 change->args[count].mode = MODE_REMOVE | MODE_CHANOP;
a32da4c7 1102 change->args[count++].u.member = mn;
d76ed9a9 1103 }
1104 if (count) {
1105 change->argc = count;
1106 modcmd_chanmode_announce(change);
1107 }
1108 mod_chanmode_free(change);
1109 reply("OSMSG_DEOPALL_DONE", channel->name);
1110 return 1;
1111}
1112
55342ce8 1113static MODCMD_FUNC(cmd_dehopall)
1114{
1115 struct mod_chanmode *change;
1116 unsigned int ii, count;
1117
1118 change = mod_chanmode_alloc(channel->members.used);
1119 for (ii = count = 0; ii < channel->members.used; ++ii) {
1120 struct modeNode *mn = channel->members.list[ii];
1121 if (IsService(mn->user) || !(mn->modes & MODE_HALFOP))
1122 continue;
1123 change->args[count].mode = MODE_REMOVE | MODE_HALFOP;
11408ce4 1124 change->args[count++].u.member = mn;
55342ce8 1125 }
1126 if (count) {
1127 change->argc = count;
1128 modcmd_chanmode_announce(change);
1129 }
1130 mod_chanmode_free(change);
1131 reply("OSMSG_DEHOPALL_DONE", channel->name);
1132 return 1;
1133}
1134
7637f48f 1135static MODCMD_FUNC(cmd_resetmax)
1136{
1137 max_clients = dict_size(clients);
1138 max_clients_time = now;
1139 reply("OSMSG_MAXUSERS_RESET", max_clients);
1140 return 1;
1141}
1142
d76ed9a9 1143static MODCMD_FUNC(cmd_rehash)
1144{
1145 extern char *services_config;
1146
1147 if (conf_read(services_config))
258d1427 1148 reply("OSMSG_REHASH_COMPLETE");
d76ed9a9 1149 else
258d1427 1150 reply("OSMSG_REHASH_FAILED");
d76ed9a9 1151 return 1;
1152}
1153
1154static MODCMD_FUNC(cmd_reopen)
1155{
1156 log_reopen();
1157 reply("OSMSG_REOPEN_COMPLETE");
1158 return 1;
1159}
1160
1161static MODCMD_FUNC(cmd_reconnect)
1162{
1163 reply("OSMSG_RECONNECTING");
1164 irc_squit(self, "Reconnecting.", NULL);
1165 return 1;
1166}
1167
1168static MODCMD_FUNC(cmd_jupe)
1169{
1170 extern int force_n2k;
1171 struct server *newsrv;
1172 unsigned int num;
1173 char numeric[COMBO_NUMERIC_LEN+1], srvdesc[SERVERDESCRIPTMAX+1];
1174
1175 num = atoi(argv[2]);
df5f6070 1176 if(num == 0) {
1177 reply("OSMSG_INVALID_NUMERIC");
1178 return 0;
1179 }
d76ed9a9 1180 if ((num < 64) && !force_n2k) {
1181 inttobase64(numeric, num, 1);
1182 inttobase64(numeric+1, 64*64-1, 2);
1183 } else {
1184 inttobase64(numeric, num, 2);
1185 inttobase64(numeric+2, 64*64*64-1, 3);
1186 }
1187#ifdef WITH_PROTOCOL_P10
1188 if (GetServerN(numeric)) {
1189 reply("OSMSG_NUMERIC_COLLIDE", num, numeric);
1190 return 0;
1191 }
1192#endif
1193 if (GetServerH(argv[1])) {
1194 reply("OSMSG_NAME_COLLIDE");
1195 return 0;
1196 }
1197 snprintf(srvdesc, sizeof(srvdesc), "JUPE %s", unsplit_string(argv+3, argc-3, NULL));
df5f6070 1198 if(!strchr(argv[1], '.')) {
1199 reply("OSMSG_INVALID_SERVERNAME");
1200 return 0;
1201 }
d76ed9a9 1202 newsrv = AddServer(self, argv[1], 1, now, now, numeric, srvdesc);
1203 if (!newsrv) {
1204 reply("OSMSG_SRV_CREATE_FAILED");
1205 return 0;
1206 }
1207 irc_server(newsrv);
1208 reply("OSMSG_SERVER_JUPED", argv[1]);
1209 return 1;
1210}
1211
1212static MODCMD_FUNC(cmd_unjupe)
1213{
1214 struct server *srv;
1215 char *reason;
1216
1217 srv = GetServerH(argv[1]);
1218 if (!srv) {
1219 reply("MSG_SERVER_UNKNOWN", argv[1]);
1220 return 0;
1221 }
1222 if (strncmp(srv->description, "JUPE", 4)) {
1223 reply("OSMSG_SERVER_NOT_JUPE");
1224 return 0;
1225 }
1226 reason = (argc > 2) ? unsplit_string(argv+2, argc-2, NULL) : "Unjuping server";
1227 DelServer(srv, 1, reason);
1228 reply("OSMSG_SERVER_UNJUPED");
1229 return 1;
1230}
1231
1232static MODCMD_FUNC(cmd_jump)
1233{
1234 extern struct cManagerNode cManager;
1235 void uplink_select(char *name);
1236 struct uplinkNode *uplink_find(char *name);
1237 struct uplinkNode *uplink;
1238 char *target;
1239
1240 target = unsplit_string(argv+1, argc-1, NULL);
1241
1242 if (!strcmp(cManager.uplink->name, target)) {
258d1427 1243 reply("OSMSG_CURRENT_UPLINK", cManager.uplink->name);
1244 return 0;
d76ed9a9 1245 }
1246
1247 uplink = uplink_find(target);
1248 if (!uplink) {
258d1427 1249 reply("OSMSG_INVALID_UPLINK", target);
1250 return 0;
d76ed9a9 1251 }
1252 if (uplink->flags & UPLINK_UNAVAILABLE) {
1253 reply("OSMSG_UPLINK_DISABLED", uplink->name);
1254 return 0;
1255 }
1256
1257 reply("OSMSG_UPLINK_CONNECTING", uplink->name, uplink->host, uplink->port);
1258 uplink_select(target);
1259 irc_squit(self, "Reconnecting.", NULL);
1260 return 1;
1261}
1262
1263static MODCMD_FUNC(cmd_die)
1264{
1265 char *reason, *text;
1266
1267 text = unsplit_string(argv+1, argc-1, NULL);
1268 reason = alloca(strlen(text) + strlen(user->nick) + 20);
1269 sprintf(reason, "Disconnected by %s [%s]", user->nick, text);
1270 irc_squit(self, reason, text);
1271 quit_services = 1;
1272 return 1;
1273}
1274
1275static MODCMD_FUNC(cmd_restart)
1276{
1277 extern int services_argc;
1278 extern char **services_argv;
1279 char **restart_argv, *reason, *text;
1280
1281 text = unsplit_string(argv+1, argc-1, NULL);
1282 reason = alloca(strlen(text) + strlen(user->nick) + 17);
1283 sprintf(reason, "Restarted by %s [%s]", user->nick, text);
1284 irc_squit(self, reason, text);
1285
1286 /* Append a NULL to the end of argv[]. */
1287 restart_argv = (char **)alloca((services_argc + 1) * sizeof(char *));
1288 memcpy(restart_argv, services_argv, services_argc * sizeof(char *));
1289 restart_argv[services_argc] = NULL;
1290
1291 call_exit_funcs();
1292
1293 /* Don't blink. */
1294 execv(services_argv[0], restart_argv);
1295
1296 /* If we're still here, that means something went wrong. Reconnect. */
1297 return 1;
1298}
1299
1300static struct gline *
9a75756e 1301opserv_block(struct userNode *target, char *src_handle, char *reason, unsigned long duration, int silent)
d76ed9a9 1302{
2f61d1d7 1303 char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
1304 irc_ntop(mask + 2, sizeof(mask) - 2, &target->ip);
1305 if (!reason)
1306 snprintf(reason = alloca(MAXLEN), MAXLEN,
1307 "G-line requested by %s.", src_handle);
1308 if (!duration)
1309 duration = opserv_conf.block_gline_duration;
995043b4 1310 return gline_add(src_handle, mask, duration, reason, now, 1, silent ? 1 : 0);
d76ed9a9 1311}
1312
1313static MODCMD_FUNC(cmd_block)
1314{
1315 struct userNode *target;
1316 struct gline *gline;
1317 char *reason;
1136f709 1318 unsigned long duration = 0;
1319 unsigned int offset = 2;
1320 unsigned int nn;
1321 struct svccmd *gline_cmd;
d76ed9a9 1322
1323 target = GetUserH(argv[1]);
1324 if (!target) {
258d1427 1325 reply("MSG_NICK_UNKNOWN", argv[1]);
1326 return 0;
d76ed9a9 1327 }
1328 if (IsService(target)) {
258d1427 1329 reply("MSG_SERVICE_IMMUNE", target->nick);
1330 return 0;
d76ed9a9 1331 }
1136f709 1332 if (dict_find(opserv_trusted_hosts, irc_ntoa(&target->ip), NULL)) {
1333 reply("OSMSG_BLOCK_TRUSTED", target->nick);
1334 return 0;
1335 }
1336 for(nn = 0; nn < curr_opers.used; nn++) {
1337 if(memcmp(&curr_opers.list[nn]->ip, &target->ip, sizeof(irc_in_addr_t)) == 0) {
1338 reply("OSMSG_BLOCK_OPER", target->nick, irc_ntoa(&target->ip), curr_opers.list[nn]->nick);
1339 return 0;
1340 }
1341 }
1342 if(argc > 2 && (duration = ParseInterval(argv[2]))) {
1343 offset = 3;
1344 }
1345 if(duration && duration != opserv_conf.block_gline_duration) {
1346 /* We require more access when the duration is not the default block duration. */
1347 gline_cmd = dict_find(cmd->parent->commands, "gline", NULL);
1348 if(!gline_cmd)
1349 {
1350 reply("OSMSG_NO_GLINE_CMD");
1351 return 0;
1352 }
1353 if(!svccmd_can_invoke(user, cmd->parent->bot, gline_cmd, channel, SVCCMD_NOISY))
1354 return 0;
1355 }
1356 reason = (argc > offset) ? unsplit_string(argv+offset, argc-offset, NULL) : NULL;
1357 gline = opserv_block(target, user->handle_info->handle, reason, duration, 0);
d76ed9a9 1358 reply("OSMSG_GLINE_ISSUED", gline->target);
1359 return 1;
1360}
1361
1362static MODCMD_FUNC(cmd_gline)
1363{
1364 unsigned long duration;
1365 char *reason;
1366 struct gline *gline;
1367
1368 reason = unsplit_string(argv+3, argc-3, NULL);
1369 if (!is_gline(argv[1]) && !IsChannelName(argv[1]) && (argv[1][0] != '&')) {
258d1427 1370 reply("MSG_INVALID_GLINE", argv[1]);
1371 return 0;
d76ed9a9 1372 }
1373 if (!argv[1][strspn(argv[1], "#&*?@.")] && (strlen(argv[1]) < 10)) {
1374 reply("OSMSG_STUPID_GLINE", argv[1]);
1375 return 0;
1376 }
1377 duration = ParseInterval(argv[2]);
1378 if (!duration) {
1379 reply("MSG_INVALID_DURATION", argv[2]);
1380 return 0;
1381 }
995043b4 1382 gline = gline_add(user->handle_info->handle, argv[1], duration, reason, now, 1, 0);
d76ed9a9 1383 reply("OSMSG_GLINE_ISSUED", gline->target);
1384 return 1;
1385}
1386
1387static MODCMD_FUNC(cmd_ungline)
1388{
1389 if (gline_remove(argv[1], 1))
1390 reply("OSMSG_GLINE_REMOVED", argv[1]);
1391 else
1392 reply("OSMSG_GLINE_FORCE_REMOVED", argv[1]);
1393 return 1;
1394}
1395
1396static MODCMD_FUNC(cmd_refreshg)
1397{
1398 if (argc > 1) {
1399 unsigned int count;
1400 dict_iterator_t it;
1401 struct server *srv;
1402
1403 for (it=dict_first(servers), count=0; it; it=iter_next(it)) {
1404 srv = iter_data(it);
1405 if ((srv == self) || !match_ircglob(srv->name, argv[1]))
1406 continue;
1407 gline_refresh_server(srv);
1408 reply("OSMSG_GLINES_ONE_REFRESHED", srv->name);
1409 count++;
1410 }
1411 if (!count) {
1412 reply("MSG_SERVER_UNKNOWN", argv[1]);
1413 return 0;
1414 }
1415 } else {
1416 gline_refresh_all();
1417 reply("OSMSG_GLINES_REFRESHED");
1418 }
1419 return 1;
1420}
1421
c408f18a 1422static void
0e08a8e0 1423opserv_version(struct userNode *target)
1424{
1425 irc_version_user(opserv, target);
1426}
1427
d82cf2f0 1428static void
1429opserv_mark(struct userNode *target, UNUSED_ARG(char *src_handle), UNUSED_ARG(char *reason), char *mark)
1430{
1431 if(!mark)
1432 return;
1433 irc_mark(target, mark);
1434}
1435
0e08a8e0 1436static void
5aa400d2 1437opserv_svsjoin(struct userNode *target, UNUSED_ARG(char *src_handle), UNUSED_ARG(char *reason), char *channame, unsigned int checkrestrictions)
c408f18a 1438{
1439 struct chanNode *channel;
1440
1441 if(!channame || !IsChannelName(channame)) {
1442 /* Not a valid channel name. We shouldnt ever get this if we check properly in addalert */
1443 return;
1444 }
1445
1446 if (!(channel = GetChannel(channame))) {
1447 channel = AddChannel(channame, now, NULL, NULL, NULL);
1448 }
1449 if (GetUserMode(channel, target)) {
1450 /* already in it */
1451 return;
1452 }
1453
5aa400d2 1454 if (checkrestrictions) {
1455 if (trace_check_bans(target, channel) == 1) {
1456 return; /* found on lamer list */
1457 }
1458
1459 if (channel->modes & MODE_INVITEONLY) {
1460 return; /* channel is invite only */
1461 }
1462
31dca7ac
MB
1463 if (!IsOper(target) && (channel->modes & MODE_OPERSONLY)) {
1464 return; /* user is not oper and channel is opers only */
1465 }
1466
fc55e757 1467 if (!IsAdmin(target) && (channel->modes & MODE_ADMINSONLY)) {
31dca7ac
MB
1468 return; /* user is not admin and channel is admin only */
1469 }
1470
1471 if (target->handle_info && (channel->modes & MODE_REGONLY)) {
1472 return; /* user is not authed and channel is authed only users */
1473 }
1474
fc55e757
MB
1475 if (!IsSSL(target) && (channel->modes & MODE_SSLONLY)) {
1476 return; /* user is not SSL and channel is SSL only */
1477 }
31dca7ac 1478
1e993296 1479 if (channel->limit > 0) {
1480 if (channel->members.used >= channel->limit) {
1481 return; /* channel is invite on */
1482 }
5aa400d2 1483 }
1484
1485 if (*channel->key) {
1486 return; /* channel is password protected */
1487 }
1488 }
1489
c408f18a 1490 irc_svsjoin(opserv, target, channel);
1491 /* Should we tell the user they got joined? -Rubin*/
1492}
1493
39c1a4ef 1494static void
1495opserv_svspart(struct userNode *target, UNUSED_ARG(char *src_handle), UNUSED_ARG(char *reason), char *channame)
1496{
1497 struct chanNode *channel;
1498
1499 if(!channame || !IsChannelName(channame)) {
1500 /* Not a valid channel name. We shouldnt ever get this if we check properly in addalert */
1501 return;
1502 }
1503
1504 if (!(channel = GetChannel(channame))) {
1505 /* channel doesnt exist */
1506 return;
1507 }
1508
1509 if (!GetUserMode(channel, target)) {
1510 /* not in it */
1511 return;
1512 }
1513
1514 irc_svspart(opserv, target, channel);
1515}
1516
d914d1cb 1517static struct shun *
1518opserv_shun(struct userNode *target, char *src_handle, char *reason, unsigned long duration)
1519{
1520 char *mask;
1521 mask = alloca(MAXLEN);
1522 snprintf(mask, MAXLEN, "*@%s", target->hostname);
1523 if (!reason) {
1524 reason = alloca(MAXLEN);
1525 snprintf(reason, MAXLEN, "Shun requested by %s.", src_handle);
1526 }
1527 if (!duration) duration = opserv_conf.block_shun_duration;
995043b4 1528 return shun_add(src_handle, mask, duration, reason, now, 1);
d914d1cb 1529}
1530
1531static MODCMD_FUNC(cmd_sblock)
1532{
1533 struct userNode *target;
1534 struct shun *shun;
1535 char *reason;
1136f709 1536 unsigned long duration = 0;
1537 unsigned int offset = 2;
1538 unsigned int nn;
1539 struct svccmd *shun_cmd;
d914d1cb 1540
1541 target = GetUserH(argv[1]);
1542 if (!target) {
1543 reply("MSG_NICK_UNKNOWN", argv[1]);
1544 return 0;
1545 }
1546 if (IsService(target)) {
1547 reply("MSG_SERVICE_IMMUNE", target->nick);
1548 return 0;
1549 }
1136f709 1550 if (dict_find(opserv_trusted_hosts, irc_ntoa(&target->ip), NULL)) {
1551 reply("OSMSG_BLOCK_TRUSTED", target->nick);
1552 return 0;
1553 }
1554 for(nn = 0; nn < curr_opers.used; nn++) {
1555 if(memcmp(&curr_opers.list[nn]->ip, &target->ip, sizeof(irc_in_addr_t)) == 0) {
1556 reply("OSMSG_SBLOCK_OPER", target->nick, irc_ntoa(&target->ip), curr_opers.list[nn]->nick);
1557 return 0;
1558 }
1559 }
1560 if(argc > 2 && (duration = ParseInterval(argv[2]))) {
1561 offset = 3;
1562 }
1563 if(duration && duration != opserv_conf.block_shun_duration) {
1564 // We require more access when the duration is not the default block duration.
1565 shun_cmd = dict_find(cmd->parent->commands, "shun", NULL);
1566 if(!shun_cmd)
1567 {
1568 reply("OSMSG_NO_SHUN_CMD");
1569 return 0;
1570 }
1571 if(!svccmd_can_invoke(user, cmd->parent->bot, shun_cmd, channel, SVCCMD_NOISY))
1572 return 0;
1573 }
d914d1cb 1574 reason = (argc > 2) ? unsplit_string(argv+2, argc-2, NULL) : NULL;
1136f709 1575 shun = opserv_shun(target, user->handle_info->handle, reason, duration);
d914d1cb 1576 reply("OSMSG_SHUN_ISSUED", shun->target);
1577 return 1;
1578}
1579
1580static MODCMD_FUNC(cmd_shun)
1581{
1582 unsigned long duration;
1583 char *reason;
1584 struct shun *shun;
1585
1586 reason = unsplit_string(argv+3, argc-3, NULL);
1587 if (!is_shun(argv[1]) && !IsChannelName(argv[1]) && (argv[1][0] != '&')) {
1588 reply("MSG_INVALID_SHUN", argv[1]);
1589 return 0;
1590 }
1591 if (!argv[1][strspn(argv[1], "#&*?@.")] && (strlen(argv[1]) < 10)) {
1592 reply("OSMSG_STUPID_SHUN", argv[1]);
1593 return 0;
1594 }
1595 duration = ParseInterval(argv[2]);
1596 if (!duration) {
1597 reply("MSG_INVALID_DURATION", argv[2]);
1598 return 0;
1599 }
995043b4 1600 shun = shun_add(user->handle_info->handle, argv[1], duration, reason, now, 1);
d914d1cb 1601 reply("OSMSG_SHUN_ISSUED", shun->target);
1602 return 1;
1603}
1604
1605static MODCMD_FUNC(cmd_unshun)
1606{
1607 if (shun_remove(argv[1], 1))
1608 reply("OSMSG_SHUN_REMOVED", argv[1]);
1609 else
1610 reply("OSMSG_SHUN_FORCE_REMOVED", argv[1]);
1611 return 1;
1612}
1613
1614static MODCMD_FUNC(cmd_refreshs)
1615{
1616 if (argc > 1) {
1617 unsigned int count;
1618 dict_iterator_t it;
1619 struct server *srv;
1620
1621 for (it=dict_first(servers), count=0; it; it=iter_next(it)) {
1622 srv = iter_data(it);
1623 if ((srv == self) || !match_ircglob(srv->name, argv[1]))
1624 continue;
1625 shun_refresh_server(srv);
1626 reply("OSMSG_SHUNS_ONE_REFRESHED", srv->name);
1627 count++;
1628 }
1629 if (!count) {
1630 reply("MSG_SERVER_UNKNOWN", argv[1]);
1631 return 0;
1632 }
1633 } else {
1634 shun_refresh_all();
1635 reply("OSMSG_SHUNS_REFRESHED");
1636 }
1637 return 1;
1638}
1639
d76ed9a9 1640static void
4b44eb0f 1641opserv_ison(struct userNode *bot, struct userNode *tell, struct userNode *target, const char *message)
d76ed9a9 1642{
1643 struct modeNode *mn;
1644 unsigned int count, here_len, n, maxlen;
1645 char buff[MAXLEN];
1646
1647 maxlen = tell->handle_info ? tell->handle_info->screen_width : 0;
1648 if (!maxlen)
1649 maxlen = MAX_LINE_SIZE;
1650 for (n=count=0; n<target->channels.used; n++) {
258d1427 1651 mn = target->channels.list[n];
1652 here_len = strlen(mn->channel->name);
1653 if ((count + here_len + 4) > maxlen) {
1654 buff[count] = 0;
4b44eb0f 1655 send_message(tell, bot, message, buff);
258d1427 1656 count = 0;
1657 }
1658 if (mn->modes & MODE_CHANOP)
d76ed9a9 1659 buff[count++] = '@';
55342ce8 1660 if (mn->modes & MODE_HALFOP)
1661 buff[count++] = '%';
258d1427 1662 if (mn->modes & MODE_VOICE)
d76ed9a9 1663 buff[count++] = '+';
258d1427 1664 memcpy(buff+count, mn->channel->name, here_len);
1665 count += here_len;
1666 buff[count++] = ' ';
d76ed9a9 1667 }
1668 if (count) {
258d1427 1669 buff[count] = 0;
4b44eb0f 1670 send_message(tell, bot, message, buff);
d76ed9a9 1671 }
1672}
1673
1674static MODCMD_FUNC(cmd_inviteme)
1675{
1676 struct userNode *target;
1677
1678 if (argc < 2) {
258d1427 1679 target = user;
d76ed9a9 1680 } else {
258d1427 1681 target = GetUserH(argv[1]);
1682 if (!target) {
1683 reply("MSG_NICK_UNKNOWN", argv[1]);
1684 return 0;
1685 }
d76ed9a9 1686 }
1687 if (opserv_conf.debug_channel == NULL) {
258d1427 1688 reply("OSMSG_NO_DEBUG_CHANNEL");
1689 return 0;
d76ed9a9 1690 }
1691 if (GetUserMode(opserv_conf.debug_channel, user)) {
a32da4c7 1692 reply("OSMSG_ALREADY_THERE", opserv_conf.debug_channel->name);
d76ed9a9 1693 return 0;
1694 }
1695 irc_invite(cmd->parent->bot, target, opserv_conf.debug_channel);
1696 if (target != user)
258d1427 1697 reply("OSMSG_INVITE_DONE", target->nick, opserv_conf.debug_channel->name);
d76ed9a9 1698 return 1;
1699}
1700
1701static MODCMD_FUNC(cmd_invite)
1702{
1703 if (GetUserMode(channel, user)) {
1704 reply("OSMSG_ALREADY_THERE", channel->name);
1705 return 0;
1706 }
1707 irc_invite(cmd->parent->bot, user, channel);
1708 return 1;
1709}
1710
c408f18a 1711static MODCMD_FUNC(cmd_svsjoin)
1712{
1713 struct userNode *target;
1714
1715
1716 if(!IsChannelName(argv[2])) {
1717 reply("MSG_NOT_CHANNEL_NAME");
1718 return 0;
1719 }
1720 target = GetUserH(argv[1]);
1721 if (!target) {
1722 reply("MSG_NICK_UNKNOWN", argv[1]);
1723 return 0;
1724 }
1725
1726 if (!(channel = GetChannel(argv[2]))) {
1727 channel = AddChannel(argv[2], now, NULL, NULL, NULL);
1728 }
1729 if (GetUserMode(channel, target)) {
57e3ccf6 1730 reply("OSMSG_USER_ALREADY_THERE", target->nick, channel->name);
c408f18a 1731 return 0;
1732 }
1733 irc_svsjoin(opserv, target, channel);
1734 reply("OSMSG_SVSJOIN_SENT");
1735 return 1;
1736}
1737
964a842d 1738static MODCMD_FUNC(cmd_svsnick)
1739{
1740 struct userNode *target;
1741
1742 target = GetUserH(argv[1]);
1743 if (!target) {
1744 reply("MSG_NICK_UNKNOWN", argv[1]);
1745 return 0;
1746 }
1747 if(!is_valid_nick(argv[2])) {
1748 reply("OMSG_BAD_SVSNICK", argv[2]);
1749 return 0;
1750 }
1751 irc_svsnick(opserv, target, argv[2]);
1752 return 1;
1753}
1754
d76ed9a9 1755static MODCMD_FUNC(cmd_join)
1756{
1757 struct userNode *bot = cmd->parent->bot;
1758
1136f709 1759 if (!channel) {
1760 if((argc < 2) || !IsChannelName(argv[1]))
1761 {
1762 reply("MSG_NOT_CHANNEL_NAME");
1763 return 0;
1764 }
1765
2aef5f4b 1766 channel = AddChannel(argv[1], now, NULL, NULL, NULL);
d76ed9a9 1767 AddChannelUser(bot, channel)->modes |= MODE_CHANOP;
1768 } else if (GetUserMode(channel, bot)) {
1769 reply("OSMSG_ALREADY_JOINED", channel->name);
1770 return 0;
1771 } else {
1772 struct mod_chanmode change;
1773 mod_chanmode_init(&change);
1774 change.argc = 1;
1775 change.args[0].mode = MODE_CHANOP;
a32da4c7 1776 change.args[0].u.member = AddChannelUser(bot, channel);
d76ed9a9 1777 modcmd_chanmode_announce(&change);
1778 }
1779 irc_fetchtopic(bot, channel->name);
1780 reply("OSMSG_JOIN_DONE", channel->name);
1781 return 1;
1782}
1783
1784static MODCMD_FUNC(cmd_kick)
1785{
1786 struct userNode *target;
1787 char *reason;
1788
1789 if (argc < 3) {
258d1427 1790 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1791 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1792 } else {
258d1427 1793 reason = unsplit_string(argv+2, argc-2, NULL);
d76ed9a9 1794 }
1795 target = GetUserH(argv[1]);
1796 if (!target) {
258d1427 1797 reply("MSG_NICK_UNKNOWN", argv[1]);
1798 return 0;
d76ed9a9 1799 }
1800 if (!GetUserMode(channel, target)) {
258d1427 1801 reply("OSMSG_NOT_ON_CHANNEL", target->nick, channel->name);
1802 return 0;
d76ed9a9 1803 }
1804 KickChannelUser(target, channel, cmd->parent->bot, reason);
1805 return 1;
1806}
1807
1136f709 1808static MODCMD_FUNC(cmd_forcekick)
1809{
1810 struct userNode *target;
1811 char *reason;
1812
1813 if (argc < 3) {
1814 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1815 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
1816 } else {
1817 reason = unsplit_string(argv+2, argc-2, NULL);
1818 }
1819 target = GetUserH(argv[1]);
1820 if (!target) {
1821 reply("MSG_NICK_UNKNOWN", argv[1]);
1822 return 0;
1823 }
1824 if (!GetUserMode(channel, target)) {
1825 reply("OSMSG_NOT_ON_CHANNEL", target->nick, channel->name);
1826 return 0;
1827 }
1828 if (IsLocal(target)) {
1829 reply("OSMSG_FORCEKICK_LOCAL", target->nick);
1830 return 0;
1831 }
1832 irc_kick(cmd->parent->bot, target, channel, reason);
1833 return 1;
1834}
1835
d76ed9a9 1836static MODCMD_FUNC(cmd_kickall)
1837{
1838 unsigned int limit, n, inchan;
1839 struct modeNode *mn;
1840 char *reason;
1841 struct userNode *bot = cmd->parent->bot;
1842
1843 /* ircu doesn't let servers KICK users, so if OpServ's not in the
1844 * channel, we have to join it in temporarily. */
1845 if (!(inchan = GetUserMode(channel, bot) ? 1 : 0)) {
1846 struct mod_chanmode change;
1847 mod_chanmode_init(&change);
1848 change.args[0].mode = MODE_CHANOP;
a32da4c7 1849 change.args[0].u.member = AddChannelUser(bot, channel);
d76ed9a9 1850 modcmd_chanmode_announce(&change);
1851 }
1852 if (argc < 2) {
258d1427 1853 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1854 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1855 } else {
258d1427 1856 reason = unsplit_string(argv+1, argc-1, NULL);
d76ed9a9 1857 }
1858 limit = user->handle_info->opserv_level;
1859 for (n=channel->members.used; n>0;) {
258d1427 1860 mn = channel->members.list[--n];
1861 if (IsService(mn->user)
1862 || (mn->user->handle_info
1863 && (mn->user->handle_info->opserv_level >= limit))) {
1864 continue;
1865 }
1866 KickChannelUser(mn->user, channel, bot, reason);
d76ed9a9 1867 }
1868 if (!inchan)
1869 DelChannelUser(bot, channel, "My work here is done", 0);
1870 reply("OSMSG_KICKALL_DONE", channel->name);
1871 return 1;
1872}
1873
1874static MODCMD_FUNC(cmd_kickban)
1875{
1876 struct mod_chanmode change;
1877 struct userNode *target;
1878 char *reason;
1879 char *mask;
1880
1881 if (argc == 2) {
258d1427 1882 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1883 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1884 } else {
258d1427 1885 reason = unsplit_string(argv+2, argc-2, NULL);
d76ed9a9 1886 }
1887 target = GetUserH(argv[1]);
1888 if (!target) {
258d1427 1889 reply("MSG_NICK_UNKNOWN", argv[1]);
1890 return 0;
d76ed9a9 1891 }
1892 if (!GetUserMode(channel, target)) {
258d1427 1893 reply("OSMSG_NOT_ON_CHANNEL", target->nick, channel->name);
1894 return 0;
d76ed9a9 1895 }
1896 mod_chanmode_init(&change);
1897 change.argc = 1;
1898 change.args[0].mode = MODE_BAN;
a32da4c7 1899 change.args[0].u.hostmask = mask = generate_hostmask(target, 0);
d76ed9a9 1900 modcmd_chanmode_announce(&change);
1901 KickChannelUser(target, channel, cmd->parent->bot, reason);
1902 free(mask);
1903 return 1;
1904}
1905
1906static MODCMD_FUNC(cmd_kickbanall)
1907{
1908 struct modeNode *mn;
1909 struct userNode *bot = cmd->parent->bot;
1910 struct mod_chanmode *change;
1911 char *reason;
1912 unsigned int limit, n, inchan;
1913
1914 /* ircu doesn't let servers KICK users, so if OpServ's not in the
1915 * channel, we have to join it in temporarily. */
1916 if (!(inchan = GetUserMode(channel, bot) ? 1 : 0)) {
1917 change = mod_chanmode_alloc(2);
1918 change->args[0].mode = MODE_CHANOP;
a32da4c7 1919 change->args[0].u.member = AddChannelUser(bot, channel);
d76ed9a9 1920 change->args[1].mode = MODE_BAN;
a32da4c7 1921 change->args[1].u.hostmask = "*!*@*";
d76ed9a9 1922 } else {
1923 change = mod_chanmode_alloc(1);
1924 change->args[0].mode = MODE_BAN;
a32da4c7 1925 change->args[0].u.hostmask = "*!*@*";
d76ed9a9 1926 }
1927 modcmd_chanmode_announce(change);
a32da4c7 1928 mod_chanmode_free(change);
d76ed9a9 1929 if (argc < 2) {
258d1427 1930 reason = alloca(strlen(OSMSG_KICK_REQUESTED)+strlen(user->nick)+1);
1931 sprintf(reason, OSMSG_KICK_REQUESTED, user->nick);
d76ed9a9 1932 } else {
258d1427 1933 reason = unsplit_string(argv+1, argc-1, NULL);
d76ed9a9 1934 }
1935 /* now kick them */
1936 limit = user->handle_info->opserv_level;
1937 for (n=channel->members.used; n>0; ) {
258d1427 1938 mn = channel->members.list[--n];
1939 if (IsService(mn->user)
1940 || (mn->user->handle_info
1941 && (mn->user->handle_info->opserv_level >= limit))) {
1942 continue;
1943 }
1944 KickChannelUser(mn->user, channel, bot, reason);
d76ed9a9 1945 }
1946 if (!inchan)
1947 DelChannelUser(bot, channel, "My work here is done", 0);
1948 reply("OSMSG_KICKALL_DONE", channel->name);
1949 return 1;
1950}
1951
39c1a4ef 1952static MODCMD_FUNC(cmd_svspart)
1953{
1954 struct userNode *target;
6221421c 1955 struct chanNode *target_channel;
39c1a4ef 1956
1957 if(!IsChannelName(argv[2])) {
1958 reply("MSG_NOT_CHANNEL_NAME");
1959 return 0;
1960 }
4cb36ef0 1961 if(!(target_channel = GetChannel(argv[2])))
6221421c 1962 {
1963 reply("MSG_INVALID_CHANNEL");
1964 return 0;
1965 }
39c1a4ef 1966 target = GetUserH(argv[1]);
1967 if (!target) {
1968 reply("MSG_NICK_UNKNOWN", argv[1]);
1969 return 0;
1970 }
1971
4cb36ef0 1972 if (!GetUserMode(target_channel, target)) {
1973 reply("OSMSG_NOT_ON_CHANNEL", cmd->parent->bot->nick, target_channel->name);
39c1a4ef 1974 return 0;
1975 }
1976
4cb36ef0 1977 irc_svspart(opserv, target, target_channel);
39c1a4ef 1978 reply("OSMSG_SVSPART_SENT");
1979 return 1;
1980}
1981
d76ed9a9 1982static MODCMD_FUNC(cmd_part)
1983{
1984 char *reason;
1985
1136f709 1986 if (!GetUserMode(channel, cmd->parent->bot)) {
1987 reply("OSMSG_NOT_ON_CHANNEL", cmd->parent->bot->nick, channel->name);
d76ed9a9 1988 return 0;
1989 }
1136f709 1990 reason = (argc < 2) ? "Leaving." : unsplit_string(argv+1, argc-1, NULL);
1991 reply("OSMSG_LEAVING", channel->name);
1992 DelChannelUser(cmd->parent->bot, channel, reason, 0);
d76ed9a9 1993 return 1;
1994}
1995
1996static MODCMD_FUNC(cmd_mode)
1997{
1998 if (!modcmd_chanmode(argv+1, argc-1, MCP_ALLOW_OVB|MCP_KEY_FREE|MC_ANNOUNCE)) {
1999 reply("MSG_INVALID_MODES", unsplit_string(argv+1, argc-1, NULL));
2000 return 0;
2001 }
2002 reply("OSMSG_MODE_SET", channel->name);
2003 return 1;
2004}
2005
d82cf2f0 2006int is_valid_mark(char *mark)
2007{
2008 char *ptr;
2009
2010 if(!mark || !*mark)
2011 return 0;
2012 if(strlen(mark) > MARKLEN)
2013 return 0;
2014
2015 for(ptr = mark; *ptr; ptr++) {
2016 if(! (isalnum(*ptr) || *ptr == '-'))
2017 return 0;
2018 }
2019
2020 return 1;
2021}
2022
2023static MODCMD_FUNC(cmd_mark)
2024{
2025 char *mark = argv[2];
2026 struct userNode *victim = GetUserH(argv[1]);
2027
2028 if(!victim)
2029 reply("MSG_NICK_UNKNOWN", argv[1]);
2030 else if(!is_valid_mark(mark))
2031 reply("OSMSG_MARK_INVALID");
2032 else {
2033 irc_mark(victim, mark);
2034 reply("OSMSG_MARK_SET");
2035 return 1;
2036 }
2037 return 0;
2038}
2039
d76ed9a9 2040static MODCMD_FUNC(cmd_op)
2041{
2042 struct mod_chanmode *change;
2043 unsigned int arg, count;
2044
2045 change = mod_chanmode_alloc(argc-1);
2046 for (arg = 1, count = 0; arg < argc; ++arg) {
2047 struct userNode *victim;
2048 struct modeNode *mn;
2049 if (!(victim = GetUserH(argv[arg])))
2050 continue;
2051 if (!(mn = GetUserMode(channel, victim)))
2052 continue;
2053 if (mn->modes & MODE_CHANOP)
2054 continue;
2055 change->args[count].mode = MODE_CHANOP;
a32da4c7 2056 change->args[count++].u.member = mn;
d76ed9a9 2057 }
2058 if (count) {
2059 change->argc = count;
2060 modcmd_chanmode_announce(change);
2061 }
2062 mod_chanmode_free(change);
2063 reply("OSMSG_OP_DONE");
2064 return 1;
2065}
2066
55342ce8 2067static MODCMD_FUNC(cmd_hop)
2068{
2069 struct mod_chanmode *change;
2070 unsigned int arg, count;
2071
2072 change = mod_chanmode_alloc(argc-1);
2073 for (arg = 1, count = 0; arg < argc; ++arg) {
2074 struct userNode *victim;
2075 struct modeNode *mn;
2076 if (!(victim = GetUserH(argv[arg])))
2077 continue;
2078 if (!(mn = GetUserMode(channel, victim)))
2079 continue;
2080 if (mn->modes & MODE_HALFOP)
2081 continue;
2082 change->args[count].mode = MODE_HALFOP;
11408ce4 2083 change->args[count++].u.member = mn;
55342ce8 2084 }
2085 if (count) {
2086 change->argc = count;
2087 modcmd_chanmode_announce(change);
2088 }
2089 mod_chanmode_free(change);
2090 reply("OSMSG_HOP_DONE");
2091 return 1;
2092}
2093
d76ed9a9 2094static MODCMD_FUNC(cmd_opall)
2095{
2096 struct mod_chanmode *change;
2097 unsigned int ii, count;
2098
2099 change = mod_chanmode_alloc(channel->members.used);
2100 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 2101 struct modeNode *mn = channel->members.list[ii];
2102 if (mn->modes & MODE_CHANOP)
d76ed9a9 2103 continue;
2104 change->args[count].mode = MODE_CHANOP;
a32da4c7 2105 change->args[count++].u.member = mn;
d76ed9a9 2106 }
2107 if (count) {
2108 change->argc = count;
258d1427 2109 modcmd_chanmode_announce(change);
d76ed9a9 2110 }
2111 mod_chanmode_free(change);
2112 reply("OSMSG_OPALL_DONE", channel->name);
2113 return 1;
2114}
2115
55342ce8 2116static MODCMD_FUNC(cmd_hopall)
2117{
2118 struct mod_chanmode *change;
2119 unsigned int ii, count;
2120
2121 change = mod_chanmode_alloc(channel->members.used);
2122 for (ii = count = 0; ii < channel->members.used; ++ii) {
2123 struct modeNode *mn = channel->members.list[ii];
2124 if (mn->modes & MODE_HALFOP)
2125 continue;
2126 change->args[count].mode = MODE_HALFOP;
11408ce4 2127 change->args[count++].u.member = mn;
55342ce8 2128 }
2129 if (count) {
2130 change->argc = count;
2131 modcmd_chanmode_announce(change);
2132 }
2133 mod_chanmode_free(change);
2134 reply("OSMSG_HOPALL_DONE", channel->name);
2135 return 1;
2136}
2137
d76ed9a9 2138static MODCMD_FUNC(cmd_whois)
2139{
2140 struct userNode *target;
2141 char buffer[128];
2142 int bpos, herelen;
2143
2144#ifdef WITH_PROTOCOL_P10
2145 if (argv[1][0] == '*')
2146 target = GetUserN(argv[1]+1);
2147 else
d76ed9a9 2148#endif
1117fc5a 2149 target = GetUserH(argv[1]);
d76ed9a9 2150 if (!target) {
2151 reply("MSG_NICK_UNKNOWN", argv[1]);
2152 return 0;
2153 }
2154 reply("OSMSG_WHOIS_NICK", target->nick);
2155 reply("OSMSG_WHOIS_HOST", target->ident, target->hostname);
2156 if (IsFakeHost(target))
2157 reply("OSMSG_WHOIS_FAKEHOST", target->fakehost);
37ef8ee3 2158 reply("OSMSG_WHOIS_CRYPT_HOST", target->crypthost);
2159 reply("OSMSG_WHOIS_CRYPT_IP", target->cryptip);
2f61d1d7 2160 reply("OSMSG_WHOIS_IP", irc_ntoa(&target->ip));
21f6caee 2161
2162 if (target->city) {
2163 reply("OSMSG_WHOIS_COUNTRY", target->country_name);
2164 reply("OSMSG_WHOIS_COUNTRY_CODE", target->country_code);
2165 reply("OSMSG_WHOIS_CITY", target->city);
2166 reply("OSMSG_WHOIS_REGION", target->region);
2167
2168 reply("OSMSG_WHOIS_POSTAL_CODE", target->postal_code);
2169 reply("OSMSG_WHOIS_LATITUDE", target->latitude);
2170 reply("OSMSG_WHOIS_LONGITUDE", target->longitude);
1ad7ac15 2171 /* Only show a map url if we have a city, latitude and longitude.
2172 * Theres not much point of latitude and longitude coordinates are
2173 * returned but no city, the coordinates are useless.
2174 */
2175 if (target->latitude && target->longitude && target->city) {
2176 char map_url[MAXLEN];
2177 snprintf(map_url, sizeof(map_url), "http://www.mapquest.com/maps/map.adp?searchtype=address&formtype=address&latlongtype=decimal&latitude=%f&longitude=%f",
2178 target->latitude, target->longitude);
2179 reply("OSMSG_WHOIS_MAP", map_url);
2180 }
21f6caee 2181 reply("OSMSG_WHOIS_DMA_CODE", target->dma_code);
2182 reply("OSMSG_WHOIS_AREA_CODE", target->area_code);
2183 } else if (target->country_name) {
2184 reply("OSMSG_WHOIS_COUNTRY", target->country_name);
2185 }
0e08a8e0 2186 if(target->version_reply) {
2187 reply("OSMSG_WHOIS_VERSION", target->version_reply);
2188 }
ba4b4d51 2189 if(target->sslfp) {
2190 reply("OSMSG_WHOIS_SSLFP", target->sslfp);
2191 }
2c00fbc2 2192 if(target->mark) {
2193 reply("OSMSG_WHOIS_MARK", target->mark);
2194 }
da5b7dfc 2195 reply("OSMSG_WHOIS_NO_NOTICE", target->no_notice ? "YES":"NO");
21f6caee 2196
d76ed9a9 2197 if (target->modes) {
57fdf922 2198 bpos = irc_user_modes(target, buffer, sizeof(buffer));
d76ed9a9 2199#define buffer_cat(str) (herelen = strlen(str), memcpy(buffer+bpos, str, herelen), bpos += herelen)
d76ed9a9 2200 if (IsGagged(target)) buffer_cat(" (gagged)");
258d1427 2201 if (IsRegistering(target)) buffer_cat(" (registered account)");
2202 buffer[bpos] = 0;
2203 if (bpos > 0)
d76ed9a9 2204 reply("OSMSG_WHOIS_MODES", buffer);
2205 }
2206 reply("OSMSG_WHOIS_INFO", target->info);
2207#ifdef WITH_PROTOCOL_P10
2208 reply("OSMSG_WHOIS_NUMERIC", target->numeric);
2209#endif
2210 reply("OSMSG_WHOIS_SERVER", target->uplink->name);
2211 reply("OSMSG_WHOIS_ACCOUNT", (target->handle_info ? target->handle_info->handle : "Not authenticated"));
89d871d8 2212
2213 reply("OSMSG_WHOIS_PRIVS", client_report_privs(target));
2214
d76ed9a9 2215 intervalString(buffer, now - target->timestamp, user->handle_info);
2216 reply("OSMSG_WHOIS_NICK_AGE", buffer);
2217 if (target->channels.used <= MAX_CHANNELS_WHOIS)
4b44eb0f 2218 opserv_ison(cmd->parent->bot, user, target, "OSMSG_WHOIS_CHANNELS");
d76ed9a9 2219 else
258d1427 2220 reply("OSMSG_WHOIS_HIDECHANS");
d76ed9a9 2221 return 1;
2222}
2223
2224static MODCMD_FUNC(cmd_unban)
2225{
2226 struct mod_chanmode change;
2227 mod_chanmode_init(&change);
2228 change.argc = 1;
2229 change.args[0].mode = MODE_REMOVE | MODE_BAN;
a32da4c7 2230 change.args[0].u.hostmask = argv[1];
d76ed9a9 2231 modcmd_chanmode_announce(&change);
2232 reply("OSMSG_UNBAN_DONE", channel->name);
2233 return 1;
2234}
2235
2236static MODCMD_FUNC(cmd_voiceall)
2237{
2238 struct mod_chanmode *change;
2239 unsigned int ii, count;
2240
2241 change = mod_chanmode_alloc(channel->members.used);
2242 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 2243 struct modeNode *mn = channel->members.list[ii];
2244 if (mn->modes & (MODE_CHANOP|MODE_HALFOP|MODE_VOICE))
d76ed9a9 2245 continue;
2246 change->args[count].mode = MODE_VOICE;
a32da4c7 2247 change->args[count++].u.member = mn;
d76ed9a9 2248 }
2249 if (count) {
2250 change->argc = count;
258d1427 2251 modcmd_chanmode_announce(change);
d76ed9a9 2252 }
2253 mod_chanmode_free(change);
2254 reply("OSMSG_CHANNEL_VOICED", channel->name);
2255 return 1;
2256}
2257
2258static MODCMD_FUNC(cmd_devoiceall)
2259{
2260 struct mod_chanmode *change;
2261 unsigned int ii, count;
2262
2263 change = mod_chanmode_alloc(channel->members.used);
2264 for (ii = count = 0; ii < channel->members.used; ++ii) {
258d1427 2265 struct modeNode *mn = channel->members.list[ii];
2266 if (!(mn->modes & MODE_VOICE))
d76ed9a9 2267 continue;
2268 change->args[count].mode = MODE_REMOVE | MODE_VOICE;
a32da4c7 2269 change->args[count++].u.member = mn;
d76ed9a9 2270 }
2271 if (count) {
2272 change->argc = count;
258d1427 2273 modcmd_chanmode_announce(change);
d76ed9a9 2274 }
2275 mod_chanmode_free(change);
2276 reply("OSMSG_CHANNEL_DEVOICED", channel->name);
2277 return 1;
2278}
2279
2280static MODCMD_FUNC(cmd_stats_bad) {
2281 dict_iterator_t it;
2282 unsigned int ii, end, here_len;
2283 char buffer[400];
2284
2285 /* Show the bad word list.. */
de9510bc 2286 /* TODO: convert nonprinting chars like bold to $b etc in a usable way */
d76ed9a9 2287 for (ii=end=0; ii<opserv_bad_words->used; ii++) {
2288 here_len = strlen(opserv_bad_words->list[ii]);
de9510bc 2289 /* If the line is full output it & start again */
d76ed9a9 2290 if ((end + here_len + 2) > sizeof(buffer)) {
2291 buffer[end] = 0;
2292 reply("OSMSG_BADWORD_LIST", buffer);
2293 end = 0;
2294 }
2295 memcpy(buffer+end, opserv_bad_words->list[ii], here_len);
2296 end += here_len;
2297 buffer[end++] = ' ';
2298 }
2299 buffer[end] = 0;
2300 reply("OSMSG_BADWORD_LIST", buffer);
2301
2302 /* Show the exemption list.. */
2303 for (it=dict_first(opserv_exempt_channels), end=0; it; it=iter_next(it)) {
2304 here_len = strlen(iter_key(it));
2305 if ((end + here_len + 2) > sizeof(buffer)) {
2306 buffer[end] = 0;
2307 reply("OSMSG_EXEMPTED_LIST", buffer);
2308 end = 0;
2309 }
2310 memcpy(buffer+end, iter_key(it), here_len);
2311 end += here_len;
2312 buffer[end++] = ' ';
2313 }
2314 buffer[end] = 0;
2315 reply("OSMSG_EXEMPTED_LIST", buffer);
2316 return 1;
2317}
2318
d76ed9a9 2319static void
2320trace_links(struct userNode *bot, struct userNode *user, struct server *server, unsigned int depth) {
2321 unsigned int nn, pos;
2322 char buffer[400];
2323
2324 for (nn=1; nn<=depth; nn<<=1) ;
2325 for (pos=0, nn>>=1; nn>1; ) {
2326 nn >>= 1;
2327 buffer[pos++] = (depth & nn) ? ((nn == 1) ? '`' : ' ') : '|';
2328 buffer[pos++] = (nn == 1) ? '-': ' ';
2329 }
2330 buffer[pos] = 0;
2331 send_message(user, bot, "OSMSG_LINKS_SERVER", buffer, server->name, server->clients, server->description);
2332 if (!server->children.used)
2333 return;
2334 for (nn=0; nn<server->children.used-1; nn++) {
2335 trace_links(bot, user, server->children.list[nn], depth<<1);
2336 }
2337 trace_links(bot, user, server->children.list[nn], (depth<<1)|1);
2338}
2339
2340static MODCMD_FUNC(cmd_stats_links) {
2341 trace_links(cmd->parent->bot, user, self, 1);
2342 return 1;
2343}
2344
2345
2346static MODCMD_FUNC(cmd_stats_max) {
2347 reply("OSMSG_MAX_CLIENTS", max_clients, asctime(localtime(&max_clients_time)));
2348 return 1;
2349}
2350
2351static MODCMD_FUNC(cmd_stats_network) {
2352 struct helpfile_table tbl;
2353 unsigned int nn, tot_clients;
2354 dict_iterator_t it;
2355
2356 tot_clients = dict_size(clients);
2357 reply("OSMSG_NETWORK_INFO", tot_clients, invis_clients, curr_opers.used);
2358 tbl.length = dict_size(servers)+1;
2359 tbl.width = 3;
2360 tbl.flags = TABLE_NO_FREE;
2361 tbl.contents = calloc(tbl.length, sizeof(*tbl.contents));
2362 tbl.contents[0] = calloc(tbl.width, sizeof(**tbl.contents));
2363 tbl.contents[0][0] = "Server Name";
2364 tbl.contents[0][1] = "Clients";
2365 tbl.contents[0][2] = "Load";
2366 for (it=dict_first(servers), nn=1; it; it=iter_next(it)) {
2367 struct server *server = iter_data(it);
2368 char *buffer = malloc(32);
2369 tbl.contents[nn] = calloc(tbl.width, sizeof(**tbl.contents));
2370 tbl.contents[nn][0] = server->name;
2371 tbl.contents[nn][1] = buffer;
2372 sprintf(buffer, "%u", server->clients);
2373 tbl.contents[nn][2] = buffer + 16;
2374 sprintf(buffer+16, "%3.3g%%", ((double)server->clients/tot_clients)*100);
2375 nn++;
2376 }
2377 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
2378 for (nn=1; nn<tbl.length; nn++) {
2379 free((char*)tbl.contents[nn][1]);
2380 free(tbl.contents[nn]);
2381 }
2382 free(tbl.contents[0]);
2383 free(tbl.contents);
2384 return 1;
2385}
2386
2387static MODCMD_FUNC(cmd_stats_network2) {
2388 struct helpfile_table tbl;
2389 unsigned int nn;
2390 dict_iterator_t it;
2391
2392 tbl.length = dict_size(servers)+1;
2393 tbl.width = 3;
2394 tbl.flags = TABLE_NO_FREE;
2395 tbl.contents = calloc(tbl.length, sizeof(*tbl.contents));
2396 tbl.contents[0] = calloc(tbl.width, sizeof(**tbl.contents));
2397 tbl.contents[0][0] = "Server Name";
2398 tbl.contents[0][1] = "Numeric";
2399 tbl.contents[0][2] = "Link Time";
2400 for (it=dict_first(servers), nn=1; it; it=iter_next(it)) {
2401 struct server *server = iter_data(it);
2402 char *buffer = malloc(64);
2403 int ofs;
2404
2405 tbl.contents[nn] = calloc(tbl.width, sizeof(**tbl.contents));
2406 tbl.contents[nn][0] = server->name;
2407#ifdef WITH_PROTOCOL_P10
2408 sprintf(buffer, "%s (%ld)", server->numeric, base64toint(server->numeric, strlen(server->numeric)));
2409#else
2410 buffer[0] = 0;
2411#endif
2412 tbl.contents[nn][1] = buffer;
2413 ofs = strlen(buffer) + 1;
1136f709 2414 intervalString(buffer + ofs, now - server->link_time, user->handle_info);
d76ed9a9 2415 if (server->self_burst)
2416 strcat(buffer + ofs, " Bursting");
2417 tbl.contents[nn][2] = buffer + ofs;
2418 nn++;
2419 }
2420 table_send(cmd->parent->bot, user->nick, 0, 0, tbl);
2421 for (nn=1; nn<tbl.length; nn++) {
2422 free((char*)tbl.contents[nn][1]);
2423 free(tbl.contents[nn]);
2424 }
2425 free(tbl.contents[0]);
2426 free(tbl.contents);
2427 return 1;
2428}
2429
2430static MODCMD_FUNC(cmd_stats_reserved) {
2431 dict_iterator_t it;
2432
2433 reply("OSMSG_RESERVED_LIST");
2434 for (it = dict_first(opserv_reserved_nick_dict); it; it = iter_next(it))
2435 send_message_type(4, user, cmd->parent->bot, "%s", iter_key(it));
2436 return 1;
2437}
2438
2439static MODCMD_FUNC(cmd_stats_trusted) {
2440 dict_iterator_t it;
2441 struct trusted_host *th;
2442 char length[INTERVALLEN], issued[INTERVALLEN], limit[32];
2443
de9510bc 2444 reply("OSMSG_TRUSTED_LIST");
2445 reply("OSMSG_TRUSTED_LIST_BAR");
2446 reply("OSMSG_TRUSTED_LIST_HEADER");
2447 reply("OSMSG_TRUSTED_LIST_BAR");
d76ed9a9 2448 if (argc > 1) {
2449 th = dict_find(opserv_trusted_hosts, argv[1], NULL);
2450 if (th) {
2451 if (th->issued)
2452 intervalString(issued, now - th->issued, user->handle_info);
2453 if (th->expires)
2454 intervalString(length, th->expires - now, user->handle_info);
2455 if (th->limit)
de9510bc 2456 sprintf(limit, "%lu", th->limit);
d76ed9a9 2457 reply("OSMSG_HOST_IS_TRUSTED",
2458 th->ipaddr,
de9510bc 2459 (th->limit ? limit : "none"),
d76ed9a9 2460 (th->issuer ? th->issuer : "<unknown>"),
de9510bc 2461 (th->issued ? issued : "some time"),
2462 (th->expires ? length : "never"));
2463 reply("OSMSG_HOST_IS_TRUSTED_DESC", (th->reason ? th->reason : "<unknown>"));
d76ed9a9 2464 } else {
2465 reply("OSMSG_HOST_NOT_TRUSTED", argv[1]);
2466 }
2467 } else {
d76ed9a9 2468 for (it = dict_first(opserv_trusted_hosts); it; it = iter_next(it)) {
2469 th = iter_data(it);
2470 if (th->issued)
2471 intervalString(issued, now - th->issued, user->handle_info);
2472 if (th->expires)
2473 intervalString(length, th->expires - now, user->handle_info);
2474 if (th->limit)
de9510bc 2475 sprintf(limit, "%lu", th->limit);
d76ed9a9 2476 reply("OSMSG_HOST_IS_TRUSTED", iter_key(it),
de9510bc 2477 (th->limit ? limit : "none"),
d76ed9a9 2478 (th->issuer ? th->issuer : "<unknown>"),
de9510bc 2479 (th->issued ? issued : "some time"),
2480 (th->expires ? length : "never"));
2481 reply("OSMSG_HOST_IS_TRUSTED_DESC", (th->reason ? th->reason : "<unknown>"));
d76ed9a9 2482 }
2483 }
de9510bc 2484 reply("OSMSG_TRUSTED_LIST_END");
d76ed9a9 2485 return 1;
2486}
2487
2488static MODCMD_FUNC(cmd_stats_uplink) {
2489 extern struct cManagerNode cManager;
2490 struct uplinkNode *uplink;
2491
2492 uplink = cManager.uplink;
2493 reply("OSMSG_UPLINK_START", uplink->name);
2494 reply("OSMSG_UPLINK_ADDRESS", uplink->host, uplink->port);
2495 return 1;
2496}
2497
2498static MODCMD_FUNC(cmd_stats_uptime) {
2499 char uptime[INTERVALLEN];
2500 struct tms buf;
0f6fe38c 2501 extern time_t boot_time;
2502 extern int lines_processed;
2503 static long clocks_per_sec;
d76ed9a9 2504
2505 if (!clocks_per_sec) {
2506#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
2507 clocks_per_sec = sysconf(_SC_CLK_TCK);
2508 if (clocks_per_sec <= 0)
2509#endif
2510 {
2511 log_module(OS_LOG, LOG_ERROR, "Unable to query sysconf(_SC_CLK_TCK), output of 'stats uptime' will be wrong");
2512 clocks_per_sec = CLOCKS_PER_SEC;
2513 }
2514 }
ff3b058a 2515 intervalString(uptime, time(NULL)-boot_time, user->handle_info);
0f6fe38c 2516 times(&buf);
2517 reply("OSMSG_UPTIME_STATS",
2518 uptime, lines_processed,
2519 buf.tms_utime/(double)clocks_per_sec,
2520 buf.tms_stime/(double)clocks_per_sec);
d76ed9a9 2521 return 1;
2522}
2523
2524static MODCMD_FUNC(cmd_stats_alerts) {
2525 dict_iterator_t it;
2526 struct opserv_user_alert *alert;
2527 const char *reaction;
083fb96b 2528 char t_buffer[INTERVALLEN];
a947ef71 2529 char expire_buffer[30];
eaf715f1 2530 char *m = NULL;
d76ed9a9 2531
eaf715f1 2532 if(argc > 1)
2533 m = unsplit_string(argv + 1, argc - 1, NULL);
2534 reply("OSMSG_ALERTS_LIST", m ? m : "*");
de9510bc 2535 reply("OSMSG_ALERTS_BAR");
2536 reply("OSMSG_ALERTS_HEADER");
2537 reply("OSMSG_ALERTS_BAR");
d76ed9a9 2538 for (it = dict_first(opserv_user_alerts); it; it = iter_next(it)) {
2539 alert = iter_data(it);
5aef35cf 2540 if(m && (!match_ircglob(alert->text_discrim, m) && strcasecmp(alert->owner, m) && strcasecmp(iter_key(it), m)))
61eb54f9 2541 continue; /* not a match to requested filter */
d76ed9a9 2542 switch (alert->reaction) {
2543 case REACT_NOTICE: reaction = "notice"; break;
2544 case REACT_KILL: reaction = "kill"; break;
1c5f6697 2545// case REACT_SILENT: reaction = "silent"; break;
d76ed9a9 2546 case REACT_GLINE: reaction = "gline"; break;
ec311f39 2547 case REACT_TRACK: reaction = "track"; break;
d914d1cb 2548 case REACT_SHUN: reaction = "shun"; break;
c408f18a 2549 case REACT_SVSJOIN: reaction = "svsjoin"; break;
39c1a4ef 2550 case REACT_SVSPART: reaction = "svspart"; break;
0e08a8e0 2551 case REACT_VERSION: reaction = "version"; break;
d82cf2f0 2552 case REACT_MARK: reaction = "mark"; break;
890866b0 2553 case REACT_NOTICEUSER: reaction = "noticeuser"; break;
2554 case REACT_MSGUSER: reaction = "msguser"; break;
d76ed9a9 2555 default: reaction = "<unknown>"; break;
2556 }
de9510bc 2557 reply("OSMSG_ALERT_IS", iter_key(it), reaction, alert->owner);
a947ef71 2558 if (alert->expire) {
2559 strftime(expire_buffer, sizeof(expire_buffer), "%Y-%m-%d %H:%M:%S %z", localtime(&alert->expire));
2560 reply("OSMSG_ALERT_EXPIRE", expire_buffer);
2561 }
de9510bc 2562 reply("OSMSG_ALERTS_DESC", alert->text_discrim);
083fb96b 2563 if (alert->last > 0)
2564 reply("OSMSG_ALERTS_LAST", intervalString(t_buffer, now - alert->last, user->handle_info));
2565 else
2566 reply("OSMSG_ALERTS_LAST", "Never");
d76ed9a9 2567 }
de9510bc 2568 reply("OSMSG_ALERT_END");
d76ed9a9 2569 return 1;
2570}
2571
2572static MODCMD_FUNC(cmd_stats_gags) {
2573 struct gag_entry *gag;
2574 struct helpfile_table table;
2575 unsigned int nn;
2576
2577 if (!gagList) {
258d1427 2578 reply("OSMSG_NO_GAGS");
d76ed9a9 2579 return 1;
2580 }
2581 for (nn=0, gag=gagList; gag; nn++, gag=gag->next) ;
2582 table.length = nn+1;
2583 table.width = 4;
2584 table.flags = TABLE_NO_FREE;
2585 table.contents = calloc(table.length, sizeof(char**));
2586 table.contents[0] = calloc(table.width, sizeof(char*));
2587 table.contents[0][0] = "Mask";
2588 table.contents[0][1] = "Owner";
2589 table.contents[0][2] = "Expires";
2590 table.contents[0][3] = "Reason";
2591 for (nn=1, gag=gagList; gag; nn++, gag=gag->next) {
2592 char expstr[INTERVALLEN];
2593 if (gag->expires)
2594 intervalString(expstr, gag->expires - now, user->handle_info);
2595 else
2596 strcpy(expstr, "Never");
2597 table.contents[nn] = calloc(table.width, sizeof(char*));
2598 table.contents[nn][0] = gag->mask;
2599 table.contents[nn][1] = gag->owner;
2600 table.contents[nn][2] = strdup(expstr);
2601 table.contents[nn][3] = gag->reason;
2602 }
2603 table_send(cmd->parent->bot, user->nick, 0, NULL, table);
2604 for (nn=1; nn<table.length; nn++) {
2605 free((char*)table.contents[nn][2]);
2606 free(table.contents[nn]);
2607 }
2608 free(table.contents[0]);
2609 free(table.contents);
2610 return 1;
2611}
2612
2613static MODCMD_FUNC(cmd_stats_timeq) {
2614 reply("OSMSG_TIMEQ_INFO", timeq_size(), timeq_next()-now);
2615 return 1;
2616}
2617
de9510bc 2618/*
d76ed9a9 2619static MODCMD_FUNC(cmd_stats_warn) {
2620 dict_iterator_t it;
2621
2622 reply("OSMSG_WARN_LISTSTART");
2623 for (it=dict_first(opserv_chan_warn); it; it=iter_next(it))
2624 reply("OSMSG_WARN_LISTENTRY", iter_key(it), (char*)iter_data(it));
2625 reply("OSMSG_WARN_LISTEND");
2626 return 1;
2627}
de9510bc 2628*/
d76ed9a9 2629
f14e4f83 2630#if defined(WITH_MALLOC_X3)
ec1a68c8 2631static MODCMD_FUNC(cmd_stats_memory) {
2632 extern unsigned long alloc_count, alloc_size;
0d16e639 2633 send_message_type(MSG_TYPE_NOXLATE, user, cmd->parent->bot,
2634 "%u allocations totalling %u bytes.",
2635 alloc_count, alloc_size);
2636 return 1;
2637}
2638#elif defined(WITH_MALLOC_SLAB)
2639static MODCMD_FUNC(cmd_stats_memory) {
2640 extern unsigned long slab_alloc_count, slab_count, slab_alloc_size;
2641 extern unsigned long big_alloc_count, big_alloc_size;
2642 send_message_type(MSG_TYPE_NOXLATE, user, cmd->parent->bot,
2643 "%u allocations in %u slabs totalling %u bytes.",
2644 slab_alloc_count, slab_count, slab_alloc_size);
63c95a47 2645 send_message_type(MSG_TYPE_NOXLATE, user, cmd->parent->bot,
0d16e639 2646 "%u big allocations totalling %u bytes.",
63c95a47 2647 big_alloc_count, big_alloc_size);
ec1a68c8 2648 return 1;
2649}
2650#endif
2651
d76ed9a9 2652static MODCMD_FUNC(cmd_dump)
2653{
b336c8db 2654 char linedup[MAXLEN], original[MAXLEN];
d76ed9a9 2655
b336c8db 2656 unsplit_string(argv+1, argc-1, original);
d76ed9a9 2657 safestrncpy(linedup, original, sizeof(linedup));
2658 /* assume it's only valid IRC if we can parse it */
2659 if (parse_line(linedup, 1)) {
258d1427 2660 irc_raw(original);
2661 reply("OSMSG_LINE_DUMPED");
d76ed9a9 2662 } else
258d1427 2663 reply("OSMSG_RAW_PARSE_ERROR");
d76ed9a9 2664 return 1;
2665}
2666
2667static MODCMD_FUNC(cmd_raw)
2668{
b336c8db 2669 char linedup[MAXLEN], original[MAXLEN];
d76ed9a9 2670
b336c8db 2671 unsplit_string(argv+1, argc-1, original);
d76ed9a9 2672 safestrncpy(linedup, original, sizeof(linedup));
2673 /* Try to parse the line before sending it; if it's too wrong,
2674 * maybe it will core us instead of our uplink. */
2675 parse_line(linedup, 1);
2676 irc_raw(original);
2677 reply("OSMSG_LINE_DUMPED");
2678 return 1;
2679}
2680
2681static struct userNode *
2682opserv_add_reserve(struct svccmd *cmd, struct userNode *user, const char *nick, const char *ident, const char *host, const char *desc)
2683{
2684 struct userNode *resv = GetUserH(nick);
2685 if (resv) {
258d1427 2686 if (IsService(resv)) {
2687 reply("MSG_SERVICE_IMMUNE", resv->nick);
2688 return NULL;
2689 }
2690 if (resv->handle_info
2691 && resv->handle_info->opserv_level > user->handle_info->opserv_level) {
2692 reply("OSMSG_LEVEL_TOO_LOW");
2693 return NULL;
2694 }
d76ed9a9 2695 }
1136f709 2696 if ((resv = AddLocalUser(nick, ident, host, desc, "+i"))) {
d76ed9a9 2697 dict_insert(opserv_reserved_nick_dict, resv->nick, resv);
2698 }
2699 return resv;
2700}
2701
2702static MODCMD_FUNC(cmd_collide)
2703{
2704 struct userNode *resv;
2705
2706 resv = opserv_add_reserve(cmd, user, argv[1], argv[2], argv[3], unsplit_string(argv+4, argc-4, NULL));
2707 if (resv) {
258d1427 2708 reply("OSMSG_COLLIDED_NICK", resv->nick);
2709 return 1;
d76ed9a9 2710 } else {
2711 reply("OSMSG_CLONE_FAILED", argv[1]);
258d1427 2712 return 0;
d76ed9a9 2713 }
2714}
2715
2716static MODCMD_FUNC(cmd_reserve)
2717{
2718 struct userNode *resv;
2719
2720 resv = opserv_add_reserve(cmd, user, argv[1], argv[2], argv[3], unsplit_string(argv+4, argc-4, NULL));
2721 if (resv) {
258d1427 2722 resv->modes |= FLAGS_PERSISTENT;
2723 reply("OSMSG_RESERVED_NICK", resv->nick);
2724 return 1;
d76ed9a9 2725 } else {
2726 reply("OSMSG_CLONE_FAILED", argv[1]);
258d1427 2727 return 0;
d76ed9a9 2728 }
2729}
2730
2731static int
2732free_reserve(char *nick)
2733{
2734 struct userNode *resv;
2735 unsigned int rlen;
2736 char *reason;
2737
2738 resv = dict_find(opserv_reserved_nick_dict, nick, NULL);
2739 if (!resv)
2740 return 0;
2741
2742 rlen = strlen(resv->nick)+strlen(OSMSG_PART_REASON);
2743 reason = alloca(rlen);
2744 snprintf(reason, rlen, OSMSG_PART_REASON, resv->nick);
2745 DelUser(resv, NULL, 1, reason);
2746 dict_remove(opserv_reserved_nick_dict, nick);
2747 return 1;
2748}
2749
2750static MODCMD_FUNC(cmd_unreserve)
2751{
2752 if (free_reserve(argv[1]))
258d1427 2753 reply("OSMSG_NICK_UNRESERVED", argv[1]);
d76ed9a9 2754 else
258d1427 2755 reply("OSMSG_NOT_RESERVED", argv[1]);
d76ed9a9 2756 return 1;
2757}
2758
2759static void
2760opserv_part_channel(void *data)
2761{
2762 DelChannelUser(opserv, data, "Leaving.", 0);
2763}
2764
2765static int alert_check_user(const char *key, void *data, void *extra);
2766
2767static int
f0fb2e2d 2768opserv_new_user_check(struct userNode *user, UNUSED_ARG(void *extra))
d76ed9a9 2769{
2770 struct opserv_hostinfo *ohi;
2771 struct gag_entry *gag;
2f61d1d7 2772 char addr[IRC_NTOP_MAX_SIZE];
d76ed9a9 2773
2774 /* Check to see if we should ignore them entirely. */
2775 if (IsLocal(user) || IsService(user))
2776 return 0;
2777
2778 /* Check for alerts, and stop if we find one that kills them. */
2779 if (dict_foreach(opserv_user_alerts, alert_check_user, user))
1136f709 2780 return 0;
d76ed9a9 2781
2782 /* Gag them if appropriate. */
2783 for (gag = gagList; gag; gag = gag->next) {
277ad996 2784 if (user_matches_glob(user, gag->mask, MATCH_USENICK, 0)) {
d76ed9a9 2785 gag_helper_func(user, NULL);
2786 break;
2787 }
2788 }
2789
2790 /* Add to host info struct */
2f61d1d7 2791 irc_ntop(addr, sizeof(addr), &user->ip);
2792 if (!(ohi = dict_find(opserv_hostinfo_dict, addr, NULL))) {
d76ed9a9 2793 ohi = calloc(1, sizeof(*ohi));
2f61d1d7 2794 dict_insert(opserv_hostinfo_dict, strdup(addr), ohi);
d76ed9a9 2795 userList_init(&ohi->clients);
2796 }
2797 userList_append(&ohi->clients, user);
2798
2799 /* Only warn of new user floods outside of bursts. */
2800 if (!user->uplink->burst) {
2801 if (!policer_conforms(&opserv_conf.new_user_policer, now, 10)) {
2802 if (!new_user_flood) {
2803 new_user_flood = 1;
2804 opserv_alert("Warning: Possible new-user flood.");
2805 }
2806 } else {
2807 new_user_flood = 0;
2808 }
2809 }
2810
08895577 2811 if (checkDefCon(DEFCON_NO_NEW_CLIENTS)) {
fba880b8 2812 DelUser(user, opserv, 1, DefConGlineReason);
08895577 2813 return 0;
2814 }
2815
0272358e 2816 if ( (checkDefCon(DEFCON_GLINE_NEW_CLIENTS) || checkDefCon(DEFCON_SHUN_NEW_CLIENTS)) && !IsOper(user)) {
2817 char target[IRC_NTOP_MAX_SIZE + 3] = { '*', '@', '\0' };
2818
2819 strcpy(target + 2, user->hostname);
2820 if (checkDefCon(DEFCON_GLINE_NEW_CLIENTS))
995043b4 2821 gline_add(opserv->nick, target, DefConGlineExpire, DefConGlineReason, now, 1, 0);
0272358e 2822 else if (checkDefCon(DEFCON_SHUN_NEW_CLIENTS))
995043b4 2823 shun_add(opserv->nick, target, DefConGlineExpire, DefConGlineReason, now, 1);
0272358e 2824
2825 return 0;
2826 }
2827
d76ed9a9 2828 /* Only warn or G-line if there's an untrusted max and their IP is sane. */
2f61d1d7 2829 if (opserv_conf.untrusted_max
2830 && irc_in_addr_is_valid(user->ip)
2831 && !irc_in_addr_is_loopback(user->ip)) {
2832 struct trusted_host *th = dict_find(opserv_trusted_hosts, addr, NULL);
d76ed9a9 2833 unsigned int limit = th ? th->limit : opserv_conf.untrusted_max;
08895577 2834
2835 if (checkDefCon(DEFCON_REDUCE_SESSION) && !th)
2836 limit = DefConSessionLimit;
2837
d76ed9a9 2838 if (!limit) {
2839 /* 0 means unlimited hosts */
2840 } else if (ohi->clients.used == limit) {
2841 unsigned int nn;
2842 for (nn=0; nn<ohi->clients.used; nn++)
2843 send_message(ohi->clients.list[nn], opserv, "OSMSG_CLONE_WARNING");
2844 } else if (ohi->clients.used > limit) {
2f61d1d7 2845 char target[IRC_NTOP_MAX_SIZE + 3] = { '*', '@', '\0' };
2846 strcpy(target + 2, addr);
995043b4 2847 gline_add(opserv->nick, target, opserv_conf.clone_gline_duration, "Excessive connections from a single host.", now, 1, 1);
d76ed9a9 2848 }
2849 }
2850
2851 return 0;
2852}
2853
2854static void
a6bcc929 2855opserv_user_cleanup(struct userNode *user, UNUSED_ARG(struct userNode *killer), UNUSED_ARG(const char *why), UNUSED_ARG(void *extra))
d76ed9a9 2856{
2857 struct opserv_hostinfo *ohi;
2f61d1d7 2858 char addr[IRC_NTOP_MAX_SIZE];
d76ed9a9 2859
2860 if (IsLocal(user)) {
2861 /* Try to remove it from the reserved nick dict without
2862 * calling free_reserve, because that would call DelUser(),
2863 * and we'd loop back to here. */
2864 dict_remove(opserv_reserved_nick_dict, user->nick);
2865 return;
2866 }
2f61d1d7 2867 irc_ntop(addr, sizeof(addr), &user->ip);
2868 if ((ohi = dict_find(opserv_hostinfo_dict, addr, NULL))) {
d76ed9a9 2869 userList_remove(&ohi->clients, user);
2f61d1d7 2870 if (ohi->clients.used == 0)
2871 dict_remove(opserv_hostinfo_dict, addr);
d76ed9a9 2872 }
2873}
2874
2875int
2876opserv_bad_channel(const char *name)
2877{
2878 unsigned int found;
697f4c9a 2879 int present;
d76ed9a9 2880
697f4c9a 2881 dict_find(opserv_exempt_channels, name, &present);
2882 if (present)
d76ed9a9 2883 return 0;
2884
2885 if (gline_find(name))
2886 return 1;
2887
2888 for (found=0; found<opserv_bad_words->used; ++found)
2889 if (irccasestr(name, opserv_bad_words->list[found]))
2890 return 1;
2891
2892 return 0;
2893}
2894
2895static void
2896opserv_shutdown_channel(struct chanNode *channel, const char *reason)
2897{
2898 struct mod_chanmode *change;
2899 unsigned int nn;
2900
2901 change = mod_chanmode_alloc(2);
2902 change->modes_set = MODE_SECRET | MODE_INVITEONLY;
2903 change->args[0].mode = MODE_CHANOP;
a32da4c7 2904 change->args[0].u.member = AddChannelUser(opserv, channel);
d76ed9a9 2905 change->args[1].mode = MODE_BAN;
a32da4c7 2906 change->args[1].u.hostmask = "*!*@*";
d76ed9a9 2907 mod_chanmode_announce(opserv, channel, change);
2908 mod_chanmode_free(change);
2909 for (nn=channel->members.used; nn>0; ) {
2910 struct modeNode *mNode = channel->members.list[--nn];
2911 if (IsService(mNode->user))
2912 continue;
2913 KickChannelUser(mNode->user, channel, opserv, user_find_message(mNode->user, reason));
2914 }
2915 timeq_add(now + opserv_conf.purge_lock_delay, opserv_part_channel, channel);
2916}
2917
2918static void
157ce145 2919opserv_channel_check(struct chanNode *newchan, UNUSED_ARG(void *extra))
d76ed9a9 2920{
c52666c6 2921 /*char *warning; */
d76ed9a9 2922
2923 if (!newchan->join_policer.params) {
2924 newchan->join_policer.last_req = now;
2925 newchan->join_policer.params = opserv_conf.join_policer_params;
2926 }
c52666c6 2927 /*
d76ed9a9 2928 if ((warning = dict_find(opserv_chan_warn, newchan->name, NULL))) {
57692f5e 2929 global_message_args(MESSAGE_RECIPIENT_OPERS, "OSMSG_CHANNEL_ACTIVITY_WARN", newchan->name, warning);
d76ed9a9 2930 }
c52666c6 2931 */
d76ed9a9 2932
c52666c6 2933 /* Wait until the join check to shut channels down. */
d76ed9a9 2934 newchan->bad_channel = opserv_bad_channel(newchan->name);
2935}
2936
2937static void
1898d8ea 2938opserv_channel_delete(struct chanNode *chan, UNUSED_ARG(void *extra))
d76ed9a9 2939{
2940 timeq_del(0, opserv_part_channel, chan, TIMEQ_IGNORE_WHEN);
2941}
2942
0e08a8e0 2943static void
1136f709 2944opserv_notice_handler(struct userNode *user, struct userNode *bot, const char *text, UNUSED_ARG(int server_qualified))
0e08a8e0 2945{
2946 char *cmd;
1136f709 2947 char *textb;
2948
2949 textb = strdup(text);
2950
0e08a8e0 2951 /* if its a version reply, do an alert check (only alerts with version=something) */
2952 if(bot == opserv) {
2953 if(text[0] == '\001') {
1136f709 2954 textb++;
2955 cmd = mysep(&textb, " ");
50ea69b0 2956 if(cmd && !irccasecmp(cmd, "VERSION")) {
1136f709 2957 char *version = mysep(&textb, "\n");
0e08a8e0 2958 if(!version)
2959 version = "";
2960 /* opserv_debug("Opserv got CTCP VERSION Notice from %s: %s", user->nick, version); */
da5b7dfc 2961 /* user->version_reply = strdup(version); done in parse-p10.c now */
0e08a8e0 2962 dict_foreach(opserv_user_alerts, alert_check_user, user);
2963 }
2964 }
2965 }
2966}
2967
d76ed9a9 2968static int
fb38e2bc 2969opserv_join_check(struct modeNode *mNode, UNUSED_ARG(void *extra))
d76ed9a9 2970{
2971 struct userNode *user = mNode->user;
2972 struct chanNode *channel = mNode->channel;
2973 const char *msg;
2974
2975 if (IsService(user))
2976 return 0;
2977
7e3e9ac5
MB
2978 /* Check for alerts, and stop if we find one that kills them. */
2979 if (dict_foreach(opserv_user_alerts, alert_check_user, user))
ec8177c5 2980 return 1;
d76ed9a9 2981
1136f709 2982 if (opserv && channel->bad_channel) {
d76ed9a9 2983 opserv_debug("Found $b%s$b in bad-word channel $b%s$b; removing the user.", user->nick, channel->name);
2984 if (channel->name[0] != '#')
2985 DelUser(user, opserv, 1, "OSMSG_ILLEGAL_KILL_REASON");
2986 else if (!GetUserMode(channel, opserv))
2987 opserv_shutdown_channel(channel, "OSMSG_ILLEGAL_REASON");
2988 else {
2989 send_message(user, opserv, "OSMSG_ILLEGAL_CHANNEL", channel->name);
2990 msg = user_find_message(user, "OSMSG_ILLEGAL_REASON");
2991 KickChannelUser(user, channel, opserv, msg);
2992 }
2993 return 1;
2994 }
2995
2996 if (user->uplink->burst)
2997 return 0;
2998 if (policer_conforms(&channel->join_policer, now, 1.0)) {
2999 channel->join_flooded = 0;
3000 return 0;
3001 }
3002 if (!channel->join_flooded) {
3003 /* Don't moderate the channel unless it is activated and
3004 the number of users in the channel is over the threshold. */
3005 struct mod_chanmode change;
3006 mod_chanmode_init(&change);
3007 channel->join_flooded = 1;
1136f709 3008 if (opserv && opserv_conf.join_flood_moderate && (channel->members.used > opserv_conf.join_flood_moderate_threshold)) {
d76ed9a9 3009 if (!GetUserMode(channel, opserv)) {
3010 /* If we aren't in the channel, join it. */
3011 change.args[0].mode = MODE_CHANOP;
a32da4c7 3012 change.args[0].u.member = AddChannelUser(opserv, channel);
d76ed9a9 3013 change.argc++;
3014 }
3015 if (!(channel->modes & MODE_MODERATED))
3016 change.modes_set |= MODE_MODERATED;
3017 if (change.modes_set || change.argc)
3018 mod_chanmode_announce(opserv, channel, &change);
3019 send_target_message(0, channel->name, opserv, "OSMSG_FLOOD_MODERATE");
3020 opserv_alert("Warning: Possible join flood in %s (currently %d users; channel moderated).", channel->name, channel->members.used);
3021 } else {
3022 opserv_alert("Warning: Possible join flood in %s (currently %d users).", channel->name, channel->members.used);
3023 }
3024 }
3025 log_module(OS_LOG, LOG_INFO, "Join to %s during flood: "IDENT_FORMAT, channel->name, IDENT_DATA(user));
3026 return 0;
3027}
3028
3029static int
3030opserv_add_bad_word(struct svccmd *cmd, struct userNode *user, const char *new_bad) {
3031 unsigned int bad_idx;
3032
3033 for (bad_idx = 0; bad_idx < opserv_bad_words->used; ++bad_idx) {
3034 char *orig_bad = opserv_bad_words->list[bad_idx];
3035 if (irccasestr(new_bad, orig_bad)) {
3036 if (user)
3037 reply("OSMSG_BAD_REDUNDANT", new_bad, orig_bad);
3038 return 0;
3039 } else if (irccasestr(orig_bad, new_bad)) {
3040 if (user)
3041 reply("OSMSG_BAD_GROWING", orig_bad, new_bad);
3042 free(orig_bad);
3043 opserv_bad_words->list[bad_idx] = strdup(new_bad);
3044 for (bad_idx++; bad_idx < opserv_bad_words->used; bad_idx++) {
3045 orig_bad = opserv_bad_words->list[bad_idx];
3046 if (!irccasestr(orig_bad, new_bad))
3047 continue;
3048 if (user)
3049 reply("OSMSG_BAD_NUKING", orig_bad);
3050 string_list_delete(opserv_bad_words, bad_idx);
3051 bad_idx--;
d76ed9a9 3052 }
3053 return 1;
3054 }
3055 }
3056 string_list_append(opserv_bad_words, strdup(new_bad));
3057 if (user)
3058 reply("OSMSG_ADDED_BAD", new_bad);
3059 return 1;
3060}
3061
47956fc5 3062static int
3063opserv_routing_plan_add_server(struct routingPlan *rp, const char *name, const char *uplink, const unsigned int port, int karma, const char *second, const unsigned int offline)
3064{
3065 struct routingPlanServer *rps;
3066 rps = calloc(1, sizeof(*rps));
3067 if(!rps)
3068 return 0;
3069 /* duplicate servers replace */
3070 rps->uplink = strdup(uplink);
3071 if(second)
3072 rps->secondaryuplink = strdup(second);
3073 else
3074 rps->secondaryuplink = NULL;
3075 rps->port = port ? port : 4400; /* lame hardcodede default port. maybe get from config file somewhere? */
3076 rps->karma = karma;
3077 rps->offline = offline; /* 1 = yes, 0 = no */
3078 dict_insert(rp->servers, strdup(name), rps);
3079 log_module(OS_LOG, LOG_DEBUG, "Adding rp server %s with uplink %s", name, uplink);
3080 return 1;
3081}
3082
3083static void
3084free_routing_plan_server(void *data)
3085{
3086 struct routingPlanServer *rps = data;
3087 free(rps->uplink);
3088 if(rps->secondaryuplink)
3089 free(rps->secondaryuplink);
3090 free(rps);
3091}
3092
3093struct routingPlan*
3094opserv_add_routing_plan(const char *name)
3095{
3096 struct routingPlan *rp;
3097 rp = calloc(1, sizeof(*rp));
3098 if (!rp)
3099 return NULL;
3100 if(dict_find(opserv_routing_plans, name, NULL))
3101 return NULL; /* plan already exists */
3102 rp->servers = dict_new();
3103 dict_set_free_data(rp->servers, free_routing_plan_server);
3104
3105 dict_insert(opserv_routing_plans, strdup(name), rp);
3106 /* TODO: check for duplicate */
3107 return rp;
3108}
3109
3110static void
3111free_routing_plan(void *data)
3112{
3113 struct routingPlan *rp = data;
3114 /* delete all the servers attached to this plan */
3115 dict_delete(rp->servers);
3116 /* free the plan struct */
3117 free(rp);
3118}
3119
47956fc5 3120/*************************************************
3121* Functions to handle the active routing struct */
3122
3123struct routeList
3124*find_routeList_server(struct route *route, const char *server)
3125{
3126 struct routeList *rptr;
3127 if(!server)
3128 return(NULL);
3129 for(rptr = route->servers;rptr;rptr=rptr->next) {
3130 if(!strcasecmp(rptr->server, server))
3131 return(rptr);
3132 }
3133 return(NULL);
3134}
3135
3136/* Wipes out the routing structure, freeing properly.
3137 * note: does NOT free itself, we just re-use it usually.*/
3138void
3139wipe_route_list(struct route *route) {
3140 struct routeList *nextptr, *rptr;
3141 if(!route)
3142 return;
3143 for(rptr = opserv_route->servers; rptr; rptr=nextptr)
3144 {
3145 nextptr = rptr->next;
3146 free(rptr->server);
3147 if(rptr->uplink)
3148 free(rptr->uplink);
3149 if(rptr->secondaryuplink)
3150 free(rptr->secondaryuplink);
3151 free(rptr);
3152 }
3153 route->centered = true;
3154 route->count = 0;
3155 route->maxdepth = 0;
3156 route->servers = NULL;
3157}
3158
3159
3160int
3161rank_outside_rec(struct route *route, char *server, int count)
3162{
3163 struct routeList *rptr;
3164 int n, max = 0;
3165 int i = 0;
3166 if(count > 256) { /* XXX: 256 becomes max # of servers this works with, whats the real #? */
3167 return -1;
3168 }
3169 for(rptr = route->servers; rptr; rptr = rptr->next) {
3170 i++;
3171 if(!strcasecmp(server, rptr->uplink)) {
3172 log_module(MAIN_LOG, LOG_DEBUG, "%d:%d: rank_outside_rec(%s) calling rank_outside_rec(%s)", count, i, rptr->server, rptr->uplink);
3173 n = rank_outside_rec(route, rptr->server, count +1);
3174 if(n < 0) /* handle error condition */
3175 return n;
3176 if(n > max)
3177 max = n;
3178 }
3179 }
3180 if((rptr = find_routeList_server(route, server))) {
3181 rptr->outsideness = max;
3182 return(max + 1);
3183 }
3184 else {
3c10e41e 3185 log_module(MAIN_LOG, LOG_WARNING, "routing struct rank_outsideness() couldnt find %s", server);
47956fc5 3186 return 0;
3187 }
3188}
3189
3190int
3191rank_outsideness(struct route *route)
3192{
3193 log_module(MAIN_LOG, LOG_DEBUG, "rank_outsideness(): Running...");
3194 route->maxdepth = rank_outside_rec(route, self->uplink->name, 0) - 1;
3195 if(route->maxdepth < 0) { /* if the rank failed, remove route */
3196 log_module(MAIN_LOG, LOG_WARNING, "The active routing plan has a loop! auto routing disabled.");
3197 wipe_route_list(route);
3198 return false;
3199 }
3200 return true;
3201}
3202
3203
3204/* Add servers to the routing structure */
3205void
3206add_routestruct_server(struct route *route, const char *server, unsigned int port, char *uplink, char *secondary)
3207{
3208 struct routeList *rptr;
3209 char *hname;
3210 if(find_routeList_server(route, server))
3211 {
3212 log_module(MAIN_LOG, LOG_WARNING, "Routing structure add server Skipping duplicate [%s]. This should never really be possible.", server);
3213 return;
3214 }
3215 rptr = calloc(1, sizeof(*rptr));
3216 rptr->server = strdup(server);
3217 rptr->port = port;
3218 if(!uplink) {
3219 hname = conf_get_data("server/hostname", RECDB_QSTRING);
3220 uplink = hname;
3221 }
3222 rptr->uplink = strdup(uplink);
3223 if(secondary)
3224 rptr->secondaryuplink = strdup(secondary);
3225 /* tack this server on the front of the list */
3226 rptr->next = route->servers;
3227 route->servers = rptr;
3228 route->count++;
3229
3230#ifdef notdef /* I dont quite get this. there could be uncentered things
3231 * added after our own uplink, and this function doesnt center
3232 * as it adds. -Rubin */
3233 /* If the map hasnt been centered yet... */
3234 if(route->centered == false) {
3235 /* AND we just added our own uplink to it... */
3236 if(!strcasecmp(server, self->uplink->name)) {
3237 change_route_uplinks(route); /* recenter it, n mark it centered. */
3238 }
3239 }
3240#endif
3241}
3242
3243/* Recenter the routing struct around our current uplink */
3244int
3245change_route_uplinks(struct route *route)
3246{
3247 struct routeList *rptr;
3248 char lastserver[MAXLEN];
3249 char nextserver[MAXLEN];
3250
3251 if(!route->servers)
3252 return false; /* no map to recenter */
3253 log_module(MAIN_LOG, LOG_DEBUG, "change_route_uplinks(): running...");
3254 char *servicename = conf_get_data("server/hostname", RECDB_QSTRING);
3255 strcpy(lastserver, servicename);
3256 rptr = find_routeList_server(route, self->uplink->name);
3257 if(!rptr) {
3c10e41e 3258 log_module(MAIN_LOG, LOG_WARNING, "Cannot convert routing map to center: My uplink is not on the map! Marking map as uncentered.");
47956fc5 3259 route->centered = false;
3260 return false;
3261 }
3262 if(!strcasecmp(rptr->uplink, servicename)) {
3263 log_module(MAIN_LOG, LOG_DEBUG, "Already centered");
3264 }
3265 else { /* else, center it */
3266 while(rptr) {
3267 strcpy(nextserver, rptr->uplink);
3268 log_module(MAIN_LOG, LOG_DEBUG, "change_route_uplinks() changing %s uplink to %s.", rptr->server, lastserver);
3269 free(rptr->uplink);
3270 rptr->uplink = strdup(lastserver);
3271 strcpy(lastserver, rptr->server);
3272 rptr = find_routeList_server(route, nextserver);
3273 }
3274 }
3275 if(rank_outsideness(route) > 0) {
3276 route->centered = true;
3277 return true;
3278 }
3279 else
3280 return false;
3281}
3282
3283int
3284activate_routing(struct svccmd *cmd, struct userNode *user, char *plan_name)
3285{
3286 static struct routingPlan *rp;
3287 dict_iterator_t it;
3288 char *karma;
3289
3290 if(plan_name) { /* make this the new active plan */
3291 if(!strcmp(plan_name, "*")) {
3292 /* disable routing */
3293 dict_remove(opserv_routing_plan_options, "ACTIVE");
3294 plan_name = NULL;
3295 }
3296 else {
3297 rp = dict_find(opserv_routing_plans, plan_name, NULL);
3298 if(!rp) {
3299 if(cmd && user)
3300 reply("OSMSG_PLAN_NOT_FOUND", plan_name);
3301 else {
3302 /* since it doesnt exist, remove the active setting */
3303 dict_remove(opserv_routing_plan_options, plan_name);
3304 }
3c10e41e 3305 log_module(MAIN_LOG, LOG_WARNING, "activate_routing() couldnt find active routing plan!");
47956fc5 3306 return 0;
3307 }
3308 }
3309 }
3310 else { /* find the active plan in settings */
3311 plan_name = dict_find(opserv_routing_plan_options, "ACTIVE", NULL);
3312 }
3313 if(!plan_name) { /* deactivated, or no plan was set active */
3314 /* TODO: delete routing map if it exists */
3315 wipe_route_list(opserv_route);
3316 return 1;
3317 }
3318
3319 karma = dict_find(opserv_routing_plan_options, "KARMA", NULL);
3320
3321 rp = dict_find(opserv_routing_plans, plan_name, NULL);
3322
3323 /* this should really be done during opserv init */
3324 if(!opserv_route)
3325 opserv_route = calloc(1, sizeof(*opserv_route));
3326
3327 /* Delete the existing active route */
3328 wipe_route_list(opserv_route);
3329
763de701 3330 if(!rp || !rp->servers)
3331 return 1;
47956fc5 3332 for(it = dict_first(rp->servers); it; it = iter_next(it)) {
3333 const char* servername = iter_key(it);
3334 struct routingPlanServer *rps = iter_data(it),
3335 *rp_uplink, *rp_second = NULL;
3336 char *uplink = rps->uplink;
3337 rp_uplink = dict_find(rp->servers, rps->uplink, NULL);
3338 if(rps->secondaryuplink)
3339 rp_second = dict_find(rp->servers, rps->secondaryuplink, NULL);
3340
3341 /* If the normal uplink has bad karma, don't use it as a hub,
3342 * switch to the secondary uplink.
3343 */
3344 if(karma && enabled_string(karma) && rp_uplink && rp_uplink->karma < 0) {
3345 if(rps->secondaryuplink) {
3346 uplink = rps->secondaryuplink;
3347 /* unless the secondary uplinks karma is worse than the uplink. */
3348 if((rp_second = dict_find(rp->servers, uplink, NULL)) && rp_second->karma < rp_uplink->karma)
3349 uplink = rps->uplink;
3350 }
3351 }
3352 /*
3353 * If _WE_ have bad karma, don't link us to our normal uplink, maybe
3354 * its a bad route. switch to secondary. Important: dont neg karma when we arnt on
3355 * our primary uplink, or we'll get stuck on secondary when THAT link is worse.
3356 */
3357 if(karma && enabled_string(karma) && (rps->karma < 0 || rps->offline) ) {
3358 if(rps->secondaryuplink) {
3359 uplink = rps->secondaryuplink;
3360 }
3361 }
3362 log_module(MAIN_LOG, LOG_DEBUG, "activate_routing() adding %s:%d %s", servername, rps->port, uplink);
3363 add_routestruct_server(opserv_route, servername, rps->port, uplink, NULL);
3364 }
3365 if(change_route_uplinks(opserv_route))
9079d26c 3366 {
47956fc5 3367 return 1;
9079d26c 3368 }
47956fc5 3369 else if(user) {
3370 reply("OSMSG_ROUTING_ACTIVATION_ERROR");
3371 activate_routing(cmd, user, "*");
3372 return 0;
3373 }
9079d26c 3374 /* routing activation failed but we dont do anything? */
47956fc5 3375 return 1;
3376}
3377
9079d26c 3378
3379void routing_init()
3380{
3381 activate_routing(NULL, NULL, NULL);
3382
3383 /* start auto-routing system */
3384 reroute_timer_reset(0);
3385}
3386
47956fc5 3387/*******************************************************
3388 * Functions to handle online route configuration via opserv
3389 */
3390static void route_show_option(struct svccmd *cmd, struct userNode *user, char *name)
3391{
3392 char *value = dict_find(opserv_routing_plan_options, name, NULL);
3393 if(value) {
3394 if(!strcmp("RETRY_PERIOD", name)) { /* Show as an interval */
3395 char buff[INTERVALLEN+1];
3396 reply("OSMSG_ROUTINGPLAN_OPTION", name, intervalString(buff, atoi(value), user->handle_info));
3397 }
3398 else if(!strcmp("ACTIVE", name)) {
3399 if(opserv_route && opserv_route->servers)
3400 reply("OSMSG_ROUTINGPLAN_ACTIVE", value);
3401 else
3402 reply("OSMSG_ROUTINGPLAN_OPTION_NOT_SET", name);
3403 }
3404 else {
3405 reply("OSMSG_ROUTINGPLAN_OPTION", name, value);
3406 }
3407 }
3408 else {
3409 reply("OSMSG_ROUTINGPLAN_OPTION_NOT_SET", name);
3410 }
3411}
3412
3413static void route_show_options(struct svccmd *cmd, struct userNode *user)
3414{
3415 char *options[] = {"ACTIVE", "RETRY_PERIOD", "CONN_PINGOUT", "CONN_READERROR", "KARMA", "DEFAULT_PORT", NULL};
3416 int i;
3417 for(i = 0; options[i]; i++) {
3418 route_show_option(cmd, user, options[i]);
3419 }
3420}
3421
3422/* called from timeq */
3423void routing_connect_timeout(void *data)
3424{
3425 struct waitingConnection *wc = data;
3426 struct server *target = GetServerH(wc->target);
3427 if(!target) {
3428 dict_remove(opserv_waiting_connections, wc->server);
3429 return; /* server we wanted to connect new server to is gone, just give up */
3430 }
3431 routing_handle_connect_failure(target, wc->server, "Connection timed out");
3432 /* the following invalidates server variable! */
3433 dict_remove(opserv_waiting_connections, wc->server);
3434}
3435
3436void routing_delete_connect_timer(char *server)
3437{
3438 struct waitingConnection *wc = dict_find(opserv_waiting_connections, server, 0);
3439 if(wc) {
3440 timeq_del(0, routing_connect_timeout, wc, TIMEQ_IGNORE_WHEN);
3441 dict_remove(opserv_waiting_connections, server);
3442 }
3443}
3444
3445
3446void
3447routing_connect_server(char *server, int port, struct server *to)
3448{
3449 struct waitingConnection *wc = calloc(sizeof(*wc), 1);
3450
3451 wc->server = strdup(server);
3452 wc->target = strdup(to->name);
e6dce34c 3453 /* Just to make sure there isn't one left hanging
3454 * if 2 connections are attempted at once..
3455 * */
3456 routing_delete_connect_timer(server);
47956fc5 3457 dict_insert(opserv_waiting_connections, strdup(server), wc);
3458 timeq_add(now + ROUTING_CONNECT_TIMEOUT, routing_connect_timeout, wc);
3459
3460 irc_connect(opserv, server, port, to);
3461}
3462
3463int
3464routing_connect_one(struct route *route, char *server)
3465{
3466 struct routeList *rptr;
3467 struct server *sptr, *suptr;
3468 for(rptr = route->servers; rptr; rptr = rptr->next) {
3469 if(!strcasecmp(rptr->server, server)) {
3470 /* this is the one, connect it */
3471 suptr = GetServerH(rptr->uplink);
3472 sptr = GetServerH(rptr->server);
3473 if(sptr)
3474 return 1; /* already linked */
3475 if(suptr) {
3476 routing_connect_server(rptr->server, rptr->port, suptr);
3477 return 1; /* attempted link */
3478 }
3479 return 0; /* its uplink isnt here to link to */
3480 }
3481 }
da0c436d 3482 log_module(MAIN_LOG, LOG_DEBUG, "Tried to link %s but its not in the active routing struct!", server);
3483 return 0; /* server wasnt found in active route struct. */
47956fc5 3484}
3485
3486int routing_connect_children(struct route *route, char *server)
3487{
3488 struct routeList *rptr;
3489 struct server *sptr, *suptr;
3490 for(rptr = route->servers; rptr; rptr = rptr->next) {
3491 if(!strcasecmp(rptr->uplink, server)) {
3492 /* this is the one, connect it */
3493 suptr = GetServerH(rptr->uplink);
3494 sptr = GetServerH(rptr->server);
3495 if(sptr)
3496 continue; /* already linked */
3497 if(suptr) {
3498 routing_connect_server(rptr->server, rptr->port, suptr);
3499 continue; /* attempted link */
3500 }
3501 continue; /* its uplink isnt here to link to */
3502 }
3503 }
3504 return 1; /* server wasnt found in active route struct ?! */
3505}
3506
3507int reroute(struct route *route, struct userNode *user, struct svccmd *cmd, char *directive)
3508{
3509 struct routeList *rptr;
3510 struct server *sptr, *suptr;
3511 int connect = 0, move = 0, missing = 0, i;
3512 char d = toupper(*directive);
3513
964abe6b 3514 if(!route || !route->servers) {
3515 reply("OSMSG_REROUTING_NOTCONFIGURED");
3516 return 0;
3517 }
47956fc5 3518 if(user) {
3519 if(d == 'N') { /* normal */
3520 irc_wallops("%s", "Attempting a reroute of the network according to loaded map...");
3521 reply("OSMSG_REROUTING_ACC_MAP");
3522 }
3523 else if(d == 'C') { /* only connect */
3524 reply("OSMSG_CONNECTING_MISSING_ONLY");
3525 }
3526 else if(d == 'T') { /* test */
3527 reply("OSMSG_TESTING_REROUTE");
3528 }
3529 else
3530 {
3531 reply("OSMSG_INVALID_DIRECTIVE", directive);
3532 return 0;
3533 }
3534 }
3535 for(i = 0; i <= route->maxdepth-1; i++) {
3536 for(rptr = route->servers; rptr; rptr = rptr->next) {
3537 if(rptr->outsideness == i) {
3538 /* debugging */
3539 if(user && d=='T')
3540 reply("OSMSG_INSPECTING_SERVER", rptr->server);
3541 suptr = GetServerH(rptr->uplink);
3542 if(!suptr) {
3543 if(rptr->secondaryuplink && (suptr = GetServerH(rptr->secondaryuplink))) {
3544 if(user)
3545 reply("OSMSG_COULDNT_FIND_SERVER", rptr->uplink, rptr->secondaryuplink, rptr->server);
3546 }
3547 }
3548 if(suptr) { /* if the proper uplink is connected.. */
3549 sptr = GetServerH(rptr->server);
3550 if(d == 'C' && sptr) {
3551 continue; /* Already linked */
3552 }
3553 /* If server is missing or the uplinks are not the same then... */
3554 else if(!sptr || strcasecmp(sptr->uplink->name, rptr->uplink)) {
3555 if(!sptr) {
3556 connect++;
3557 }
3558 else { /* Server is already connected somewhere */
3559 if(strcasecmp(sptr->uplink->name, rptr->uplink)) {
3560 if(d != 'T') { /* do it for real */
3561 irc_squit_route(sptr, "%s issued reroute.", user ? user->nick : opserv->nick);
3562 }
3563 else { /* just pretend */
3564 reply("OSMSG_SQUIT", rptr->server);
3565 }
3566 move++;
3567 }
3568 }
3569 if(d != 'T') /* do the real thing */
3570 routing_connect_server(rptr->server, rptr->port, suptr);
3571 else /* just pretend */
3572 reply("OSMSG_CONNECT", rptr->server, rptr->port, suptr->name);
3573 }
3574 }
3575 else {
3576 log_module(MAIN_LOG, LOG_DEBUG, "server uplink %s was not found, cant connect %s", rptr->uplink, rptr->server);
3577 missing++;
3578 }
3579 } /* outsideness = 1 */
3580 } /* rptr */
3581 } /* maxdepth */
3582 if(user) { /* report on what we did */
3583 if(!strcasecmp(directive, "C")) {
3584 if(connect > 0)
3585 reply("OSMSG_CONNECTING_MISSING", connect);
3586 else
3587 reply("OSMSG_NO_SERVERS_MISSING");
3588 }
3589 else {
3590 if(move+connect > 0)
3591 reply("OSMSG_REROUTE_COMPLETE", move, connect, move+connect);
3592 else
3593 reply("OSMSG_NO_ROUTING_NECESSARY");
3594 if(missing > 0)
3595 reply("OSMSG_UPLINKS_MISSING", missing);
3596 }
3597 }
3598 return(move+connect);
3599}
3600
3601static MODCMD_FUNC(cmd_reroute) {
3602 char* upper;
3603 upper = argv[1];
3604 if(reroute(opserv_route, user, cmd, upper))
3605 return 1;
3606 else
3607 return 0;
3608}
3609
3610/* reroute_timer(run)
3611 * run - if it is null, just setup the timer
3612 * but dont run reroute now. otherwise reroute
3613 * and setup timer.
3614 */
3615void reroute_timer(void *data) {
9079d26c 3616 /* Delete any other timers such as this one.. */
3617 timeq_del(0, reroute_timer, NULL, TIMEQ_IGNORE_DATA | TIMEQ_IGNORE_WHEN);
3618
47956fc5 3619 if(!opserv_route || !opserv_route->servers)
3620 return; /* no active route */
3621 char *retry_period = dict_find(opserv_routing_plan_options, "RETRY_PERIOD", NULL);
3622 if(!retry_period)
3623 return; /* retry_period invalid */
3624 unsigned int freq = atoi(retry_period);
3625 if(freq < 1)
3626 return; /* retry_period set to 0, disable */
3627
9079d26c 3628 /* opserv_debug("Reroute timer checking reroute"); */
283cfa83 3629 log_module(MAIN_LOG, LOG_DEBUG, "Reroute timer checking reroute()");
9079d26c 3630
47956fc5 3631 /* Do the reroute C attempt */
3632 if(data)
3633 reroute(opserv_route, NULL, NULL, "C");
3634
3635 /* Re-add ourselves to the timer queue */
3636 timeq_add(now + freq, reroute_timer, "run");
3637}
3638
3639void routing_change_karma(struct routingPlanServer *rps, const char *server, int change) {
3640
3641 int oldkarma = rps->karma;
3642 rps->karma += change;
3643 if(rps->karma < KARMA_MIN)
3644 rps->karma = KARMA_MIN;
3645 if(rps->karma > KARMA_MAX)
3646 rps->karma = KARMA_MAX;
3647 log_module(MAIN_LOG, LOG_DEBUG, "Changing %s karma by %d. new karma %d.", server, change, rps->karma);
3648 if(oldkarma > 0 && rps->karma < 0) {
3649 /* we just crossed over to negitive */
3650 log_module(MAIN_LOG, LOG_INFO, "Server %s just went negitive karma!", server);
3651 activate_routing(NULL, NULL, NULL);
3652 }
3653 else if(oldkarma < 0 && rps->karma > 0) {
3654 /* we just crossed over to positive */
3655 log_module(MAIN_LOG, LOG_INFO, "Server %s just went back positive karma.", server);
3656 activate_routing(NULL, NULL, NULL);
3657 }
3658}
3659
3660void routing_karma_timer(void *data) {
3661 time_t next;
3662 time_t timer_init = data ? atoi(data) : 0;
3663 char buf[MAXLEN];
3664
3665 log_module(MAIN_LOG, LOG_DEBUG, "routing_karma_timer() is running. timer_init=%d.", (unsigned int) timer_init);
3666
3667 /* If theres a time passed in, dont run unless that time is overdue. */
3668 if(!timer_init || (timer_init < now)) {
3669 if(opserv_route && opserv_route->servers) {
3670 char *active = dict_find(opserv_routing_plan_options, "ACTIVE", NULL);
3671 struct routingPlan *rp;
3672 if(active && (rp = dict_find(opserv_routing_plans, active, NULL))) {
3673 dict_iterator_t it;
3674 /* Walk through each server in the active routing plan.. */
3675 for(it = dict_first(rp->servers); it; it = iter_next(it)) {
3676 struct routingPlanServer *rps = iter_data(it);
3677 struct server *server = GetServerH(iter_key(it));
3678 /* Give everyone +KARMA_ENTROPE just for nothing */
3679 routing_change_karma(rps, iter_key(it), KARMA_ENTROPE);
3680 /* give an additonal +KARMA_RELIABLE to servers that
3681 * have been linked at least KARMA_TIMER seconds. */
1136f709 3682 if(server && (server->link_time < (now - KARMA_TIMER) ) ) {
47956fc5 3683 routing_change_karma(rps, iter_key(it), KARMA_RELIABLE);
3684 }
3685 }
3686 }
3687 }
3688 }
3689 if(timer_init > now) /* loading a saved value */
3690 next = timer_init;
3691 else /* no scheduled timer, or we missed it. start from now */
3692 next = now + KARMA_TIMER;
3693 /* Save when karma_timer should run again in case we restart before then */
3694 log_module(MAIN_LOG, LOG_DEBUG, "routing_karma_timer() scheduling self to run again at %d", (unsigned int) next);
3695 sprintf(buf, "%u", (unsigned int) next);
3696 dict_insert(opserv_routing_plan_options, "KARMA_TIMER", strdup(buf));
3697 /* add a timer to run this again .. */
3698 timeq_add(next, routing_karma_timer, NULL);
3699}
3700
3701void routing_handle_neg_karma(char *server, char *uplink, int change)
3702{
3703 /* if server's primary uplink is uplink, OR, uplink's primary uplink is server,
3704 * then whichever one, gets its karma changed. */
3705 char *active = dict_find(opserv_routing_plan_options, "ACTIVE", NULL);
3706 struct routingPlan *rp;
3707 struct routingPlanServer *rps;
3708 if(!active)
3709 return;
3710 if(!(rp = dict_find(opserv_routing_plans, active, NULL)))
3711 return;
3712 if((rps = dict_find(rp->servers, server, NULL))) {
3713 if(!strcasecmp(rps->uplink, uplink)) {
3714 /* server's uplink is uplink */
3715 routing_change_karma(rps, server, change);
3716 return;
3717 }
3718 }
3719 if((rps = dict_find(rp->servers, uplink, NULL))) {
3720 if(!strcasecmp(rps->uplink, server)) {
3721 /* uplink's uplink is server */
3722 routing_change_karma(rps, uplink, change);
3723 return;
3724 }
3725 }
3726}
3727
3728void
3729routing_handle_squit(char *server, char *uplink, char *message)
3730{
3731 log_module(MAIN_LOG, LOG_DEBUG, "Routing_handle_squit(%s, %s)", server, message);
3732
3733 char *val;
3734
3735 if(match_ircglob(message, "Ping timeout")) {
3736 routing_handle_neg_karma(server, uplink, KARMA_PINGOUT);
3737 /* if conn_pingout is true, try to reconnect it obaying karma rules. */
3738
3739 val = dict_find(opserv_routing_plan_options, "CONN_PINGOUT", 0);
3740 if(val && enabled_string(val))
3741 routing_connect_one(opserv_route, server);
3742 }
3743 else if(match_ircglob(message, "Read error:*")) {
3744 routing_handle_neg_karma(server, uplink, KARMA_READERROR);
3745 /* if conn_readerror is true, try to reconnect it obaying karma rules. */
3746 val = dict_find(opserv_routing_plan_options, "CONN_READERROR", 0);
3747 if(val && enabled_string(val))
3748 routing_connect_one(opserv_route, server);
3749 }
3750 /* Else whats the message (an oper squit it?) dont interfere */
3751}
3752
3753void
3754routing_handle_connect(char *server, char *uplink)
3755{
3756 char *active;
3757 struct routingPlan *rp;
3758 struct routingPlanServer *rps;
3759 dict_iterator_t it;
3760
3761 log_module(MAIN_LOG, LOG_DEBUG, "routing_handle_connect(%s, %s)", server, uplink);
3762 /* delete a pending connection timer, if any */
3763 routing_delete_connect_timer(server);
3764 /* check if routing is active... */
3765 active = dict_find(opserv_routing_plan_options, "ACTIVE", NULL);
3766 if(!active)
3767 return;
3768 rp = dict_find(opserv_routing_plans, active, NULL);
3769 if(!rp)
3770 return;
3771
3772 /* If its offline, mark it online again.. */
3773 if((rps = dict_find(rp->servers, server, NULL))) {
3774 if(rps->offline == true) {
3775 rps->offline = false;
3776 if(rps->secondaryuplink) {
3777 /* re-activate to move it back to its primary */
3778 activate_routing(NULL, NULL, NULL);
3779 }
3780 }
3781 /* if there are any servers missing who have this server as uplink try to connect them. */
3782 routing_connect_children(opserv_route, server);
3783 }
3784 /* foreach server x3 knows about, if the uplink is this server, call this function on the child. */
3785 for (it=dict_first(servers); it; it=iter_next(it)) {
3786 struct server *sptr = iter_data(it);
3787 if(sptr && sptr->uplink && !strcasecmp(server, sptr->uplink->name)) {
3788 log_module(MAIN_LOG, LOG_DEBUG, "routing_handle_connect calling self on %s's leaf %s", server, sptr->name);
3789 routing_handle_connect(sptr->name, sptr->uplink->name);
3790 }
3791 }
3792}
3793
3794/* Handle a failed attempt at connecting servers
3795 * - we should only get here regarding servers X3 attempted to link, other
3796 * opers link messages go to them not to us
3797 */
3798void
3799routing_handle_connect_failure(struct server *source, char *server, char *message)
3800{
3801 char *active;
3802 struct routingPlan *rp;
3803 struct routingPlanServer *rps;
3c10e41e 3804 log_module(MAIN_LOG, LOG_WARNING, "Failed to connect %s to %s: %s", server, source->name, message);
47956fc5 3805 /* remove the waiting connection n timeq */
3806 routing_delete_connect_timer(server);
3807 /* check if routing is active.. */
3808 active = dict_find(opserv_routing_plan_options, "ACTIVE", NULL);
3809 if(!active)
3810 return;
3811 rp = dict_find(opserv_routing_plans, active, NULL);
3812 if(!rp)
3813 return;
3814
3815 if( ((rps = dict_find(rp->servers, server, NULL)) && !strcasecmp(rps->uplink, source->name))) {
3816 /* failed to connect to its primary uplink */
3817 if(rps->offline == false) {
3818 rps->offline = true;
3819 if(rps->secondaryuplink) {
3820 /* re-activate routing so the secondary
3821 * becomes its uplink, and try again */
3822 activate_routing(NULL, NULL, NULL);
3823 /* attempt to link it again. */
3824 routing_connect_one(opserv_route, server);
bf93ca8d 3825 /* TODO: reconnect any missing servers who
3826 * normally connect to server, using their backups.
3827 * Probably should just issue a reroute C here. */
47956fc5 3828 }
3829 }
3830 }
3831}
3832
3833/* Delete any existing timers, and start the timer again
3834 * using the passed time for the first run.
3835 * - this is called during a retry_period change
9079d26c 3836 * before it has saved the new value.
3837 *
3838 * If time is 0, lookup the interval. */
47956fc5 3839void reroute_timer_reset(unsigned int time)
3840{
9079d26c 3841 timeq_del(0, reroute_timer, NULL, TIMEQ_IGNORE_DATA | TIMEQ_IGNORE_WHEN);
3842 if(time == 0) {
3843 if(!opserv_route || !opserv_route->servers)
3844 return; /* no active route */
3845 char *retry_period = dict_find(opserv_routing_plan_options, "RETRY_PERIOD", NULL);
3846 if(!retry_period)
3847 return; /* retry_period invalid */
3848 time = atoi(retry_period);
3849 if(time < 1)
3850 return; /* retry_period set to 0, disable */
3851
3852 }
47956fc5 3853 timeq_add(now + time, reroute_timer, "run");
3854}
3855
3856static MODCMD_FUNC(cmd_routing_set)
3857{
3858 char *option = argv[1];
3859 char *options[] = {"ACTIVE", "RETRY_PERIOD", "CONN_PINGOUT", "CONN_READERROR", "KARMA", "DEFAULT_PORT", NULL};
3860 int i;
3861 if(argc < 2) {
3862 route_show_options(cmd, user);
3863 }
3864 else {
3865 char *found_option = NULL;
3866 for(i = 0; options[i]; i++) {
3867 if(!strcasecmp(options[i], option))
3868 found_option = options[i];
3869 }
3870 if(!found_option) {
3871 reply("OSMSG_ROUTINGPLAN_OPTION_NOT_FOUND", option);
3872 return 0;
3873 }
3874 if(argc > 2) {
3875 char *value = argv[2];
3876 char buff[MAXLEN]; /* whats the max length of unsigned int as printf'd? */
3877 if(!strcmp(found_option, "ACTIVE")) { /* must be an existing route. */
3878 if(disabled_string(value) || false_string(value)) {
3879 /* make none of the maps active */
3880 activate_routing(cmd, user, "*");
3881 reply("OSMSG_ROUTING_DISABLED");
3882 return 1;
3883 }
3884 else if(!activate_routing(cmd, user, value)) {
3885 /* neg reply handled in activate_routing */
3886 return 0;
3887 }
3888 }
3889 if(!strcmp(found_option, "CONN_READERROR") || !strcmp(found_option, "CONN_PINGOUT") ||
3890 !strcmp(found_option, "KARMA") ) {
3891 if( enabled_string(value)) {
3892 value = "ENABLED";
3893 }
3894 else if( disabled_string(value) ) {
3895 value = "DISABLED";
3896 }
3897 else {
3898 reply("MSG_INVALID_BINARY", value);
3899 return 0;
3900 }
3901 }
3902 if(!strcmp(found_option, "RETRY_PERIOD")) {
3903 unsigned int duration = ParseInterval(value);
3904 sprintf(buff, "%d", duration);
3905 value = buff;
3906 reroute_timer_reset(duration);
3907 }
3908 /* set the value here */
3909 dict_remove(opserv_routing_plan_options, found_option);
3910 dict_insert(opserv_routing_plan_options, strdup(found_option), strdup(value));
3911 route_show_option(cmd, user, found_option);
3912 }
3913 else {
3914 /* show the current value */
3915 route_show_option(cmd, user, found_option);
3916 }
3917 }
3918 return 1;
3919}
3920
3921static MODCMD_FUNC(cmd_stats_routing_plans) {
3922 dict_iterator_t rpit;
3923 dict_iterator_t it;
3924 struct routingPlan *rp;
5c6bff84 3925 if(argc > 1) {
3926 reply("OSMSG_ROUTINGPLAN");
3927 reply("OSMSG_ROUTINGPLAN_BAR");
3928 for(rpit = dict_first(opserv_routing_plans); rpit; rpit = iter_next(rpit)) {
3929 const char* name = iter_key(rpit);
3930 rp = iter_data(rpit);
3931 if(match_ircglob(name, argv[1])) {
3932 reply("OSMSG_ROUTINGPLAN_NAME", name);
3933 for(it = dict_first(rp->servers); it; it = iter_next(it)) {
3934 const char* servername = iter_key(it);
3935 struct routingPlanServer *rps = iter_data(it);
3936 reply("OSMSG_ROUTINGPLAN_SERVER", servername, rps->port, rps->uplink, rps->karma, rps->offline? "offline" : "online", rps->secondaryuplink ? rps->secondaryuplink : "None");
3937 }
3938 }
47956fc5 3939
5c6bff84 3940 }
3941 reply("OSMSG_ROUTINGPLAN_END");
3942 }
3943 else {
3944 reply("OSMSG_ROUTINGPLAN_LIST_HEAD");
3945 reply("OSMSG_ROUTINGPLAN_BAR");
3946 for(rpit = dict_first(opserv_routing_plans); rpit; rpit = iter_next(rpit)) {
3947 const char* name = iter_key(rpit);
3948 reply("OSMSG_ROUTINGPLAN_LIST", name);
3949 }
3950 reply("OSMSG_ROUTINGPLAN_END");
3951 route_show_options(cmd, user);
47956fc5 3952 }
47956fc5 3953 return 1;
3954}
3955
3956
3957static MODCMD_FUNC(cmd_routing_addplan)
3958{
3959 char *name;
3960 name = argv[1];
3961 /* dont allow things like 'off', 'false', '0' because thats how we disable routing. */
3962 if(*name && !disabled_string(name) && !false_string(name)) {
3963 if(opserv_add_routing_plan(name)) {
3964 reply("OSMSG_ADDPLAN_SUCCESS", name);
3965 return 1;
3966 }
3967 else {
3968 reply("OSMSG_ADDPLAN_FAILED", name);
3969 return 0;
3970 }
3971 }
3972 else
3973 {
3974 reply("OSMSG_INVALID_PLAN");
3975 return 0;
3976 }
3977}
3978
3979static MODCMD_FUNC(cmd_routing_delplan)
3980{
3981 char *name = argv[1];
3982 if( dict_remove(opserv_routing_plans, name) ) {
3983 char *active = dict_find(opserv_routing_plan_options, "ACTIVE", NULL);
3984 if(active && !strcasecmp(active, name)) {
3985 /* if this was the active plan, disable routing */
3986 activate_routing(cmd, user, "*");
3987 reply("OSMSG_ROUTING_DISABLED");
3988 }
3989 reply("OSMSG_PLAN_DELETED");
3990 return 1;
3991 }
3992 else {
3993 reply("OSMSG_PLAN_NOT_FOUND", name);
3994 return 0;
3995 }
3996}
3997
3998static MODCMD_FUNC(cmd_routing_addserver)
3999{
4000 char *plan;
4001 char *server;
4002 char *portstr;
4003 char *uplink;
4004 char *second;
4005 unsigned int port;
4006 struct routingPlan *rp;
4007
4008 plan = argv[1];
4009 server = strdup(argv[2]);
4010 server = strtok(server, ":");
4011 portstr = strtok(NULL, ":");
4012 if(portstr)
4013 port = atoi(portstr);
4014 else {
4015 char *str = dict_find(opserv_routing_plan_options, "DEFAULT_PORT", NULL);
4016 uplink = argv[3];
4017 port = str ? atoi(str) : 0;
4018 }
4019 uplink = argv[3];
4020 if(argc > 4)
4021 second = argv[4];
4022 else
4023 second = NULL;
4024
4025 if( (rp = dict_find(opserv_routing_plans, plan, 0))) {
4026 char *active;
4027 opserv_routing_plan_add_server(rp, server, uplink, port, KARMA_DEFAULT, second, 0);
4028 reply("OSMSG_PLAN_SERVER_ADDED", server);
4029 if((active = dict_find(opserv_routing_plan_options, "ACTIVE", 0)) && !strcasecmp(plan, active)) {
4030 /* re-activate routing with new info */
4031 activate_routing(cmd, user, NULL);
4032 }
4033
4034 free(server);
4035 return 1;
4036 }
4037 else {
4038 reply("OSMSG_PLAN_NOT_FOUND", plan);
4039 free(server);
4040 return 0;
4041 }
4042}
4043
4044static MODCMD_FUNC(cmd_routing_delserver)
4045{
4046 char *plan;
4047 char *server;
4048 struct routingPlan *rp;
4049 plan = argv[1];
4050 server = argv[2];
4051 if( (rp = dict_find(opserv_routing_plans, plan, 0))) {
4052 if(dict_remove(rp->servers, server)) {
4053 char *active;
4054 reply("OSMSG_PLAN_SERVER_DELETED");
4055 if((active = dict_find(opserv_routing_plan_options, "ACTIVE", 0)) && !strcasecmp(plan, active)) {
4056 /* re-activate routing with new info */
4057 activate_routing(cmd, user, NULL);
4058 }
4059
4060 return 1;
4061 }
4062 else {
4063 reply("OSMSG_PLAN_SERVER_NOT_FOUND", server);
4064 return 0;
4065 }
4066 }
4067 else {
4068 reply("OSMSG_PLAN_NOT_FOUND", plan);
4069 return 0;
4070 }
4071}
4072
4073
4074/*************************************************
4075 * Functions to deal with 'route map' command */
4076
4077/* Figures out how many downlinks there are for proper
4078 * drawing of the route map */
4079int
4080num_route_downlinks(struct route *route, char *name)
4081{
4082 struct routeList *rptr;
4083 int num = 0;
4084 rptr = route->servers;
4085 while(rptr) {
4086 if(!strcasecmp(rptr->uplink, name))
4087 num++;
4088 rptr = rptr->next;
4089 }
4090 return num;
4091}
4092
4093void
4094show_route_downlinks(struct svccmd *cmd, struct route *route, struct userNode *user, char *name, char *prevpre, char *arrowchar, int reset)
4095{
4096 struct routeList *servPtr;
4097 struct server *sptr;
4098 int j;
4099 char pre[MAXLEN];
4100 char *nextpre;
4101 char *status;
4102 int num = 0;
4103 static int depth = 0;
4104
4105 if(reset)
4106 depth = 0;
4107
4108 nextpre = malloc(MAXLEN);
4109 strcpy(pre, prevpre);
4110
4111 sptr = GetServerH(name);
4112 if((servPtr = find_routeList_server(route, name))) {
4113 if(!sptr)
4114 status = " ";
4115 else if (!strcasecmp(sptr->uplink->name, servPtr->uplink))
4116 status = "X";
4117 else if(servPtr->secondaryuplink && !strcasecmp(sptr->name, servPtr->secondaryuplink))
4118 status = "/";
4119 else
4120 status = "!";
4121 reply("OSMSG_DOWNLINKS_FORMAT_A", pre, arrowchar, name, status);
4122 }
4123 else
4124 reply("OSMSG_DOWNLINKS_FORMAT_B", self->name);
4125 j = num_route_downlinks(route, name);
4126 servPtr = route->servers;
4127 while(servPtr) {
4128 if(!strcasecmp(servPtr->uplink, name)) {
4129 strcpy(nextpre, pre);
4130 if(depth++ > 0) {
4131 if(arrowchar[0] == '`')
4132 strcat(nextpre, " ");
4133 else
4134 strcat(nextpre, "| ");
4135 }
4136 if(j > ++num) {
4137 show_route_downlinks(cmd, route, user, servPtr->server, nextpre, "|", 0);
4138 }
4139 else {
4140 show_route_downlinks(cmd, route, user, servPtr->server, nextpre, "`", 0);
4141 }
4142 }
4143 servPtr = servPtr->next;
4144 }
4145 free(nextpre);
4146}
4147
4148int
4149show_route_map(struct route *route, struct userNode *user, struct svccmd *cmd)
4150{
4151 if(!route || !route->servers) {
4152 reply("OSMSG_ROUTELIST_EMPTY");
4153 return 0;
4154 }
4155
4156 char *serviceName = conf_get_data("server/hostname", RECDB_QSTRING);
4157 reply("OSMSG_ROUTELIST_AS_PLANNED");
4158 show_route_downlinks(cmd, route, user, serviceName, "", "`", 1);
4159 reply("OSMSG_MAP_CENTERED", route->centered ? "is" : "is not", route->maxdepth);
4160 return 1;
4161}
4162
4163static MODCMD_FUNC(cmd_routing_map)
4164{
4165 show_route_map(opserv_route, user, cmd);
4166 return 1;
4167}
4168
4169
4170
4171
4172/* End of auto routing functions *
4173 *********************************/
4174
d76ed9a9 4175static MODCMD_FUNC(cmd_addbad)
4176{
4177 unsigned int arg, count;
4178 dict_iterator_t it;
4179 int bad_found, exempt_found;
4180
4181 /* Create the bad word if it doesn't exist. */
4182 bad_found = !opserv_add_bad_word(cmd, user, argv[1]);
4183
4184 /* Look for exception modifiers. */
4185 for (arg=2; arg<argc; arg++) {
4186 if (!irccasecmp(argv[arg], "except")) {
4187 reply("MSG_DEPRECATED_COMMAND", "addbad ... except", "addexempt");
4188 if (++arg > argc) {
4189 reply("MSG_MISSING_PARAMS", "except");
4190 break;
4191 }
4192 for (count = 0; (arg < argc) && IsChannelName(argv[arg]); arg++) {
4193 dict_find(opserv_exempt_channels, argv[arg], &exempt_found);
4194 if (!exempt_found) {
4195 dict_insert(opserv_exempt_channels, strdup(argv[arg]), NULL);
4196 count++;
4197 }
4198 }
4199 reply("OSMSG_ADDED_EXEMPTIONS", count);
4200 } else {
4201 reply("MSG_DEPRECATED_COMMAND", "addbad (with modifiers)", "addbad");
4202 reply("OSMSG_BAD_MODIFIER", argv[arg]);
4203 }
4204 }
4205
4206 /* Scan for existing channels that match the new bad word. */
4207 if (!bad_found) {
4208 for (it = dict_first(channels); it; it = iter_next(it)) {
1136f709 4209 struct chanNode *chan = iter_data(it);
d76ed9a9 4210
1136f709 4211 if (!opserv_bad_channel(chan->name))
d76ed9a9 4212 continue;
1136f709 4213 chan->bad_channel = 1;
4214 if (chan->name[0] == '#')
4215 opserv_shutdown_channel(chan, "OSMSG_ILLEGAL_REASON");
d76ed9a9 4216 else {
4217 unsigned int nn;
1136f709 4218 for (nn=0; nn<chan->members.used; nn++) {
4219 struct userNode *user = chan->members.list[nn]->user;
d76ed9a9 4220 DelUser(user, cmd->parent->bot, 1, "OSMSG_ILLEGAL_KILL_REASON");
4221 }
4222 }
4223 }
4224 }
4225
4226 return 1;
4227}
4228
4229static MODCMD_FUNC(cmd_delbad)
4230{
4231 dict_iterator_t it;
4232 unsigned int nn;
4233
4234 for (nn=0; nn<opserv_bad_words->used; nn++) {
4235 if (!irccasecmp(opserv_bad_words->list[nn], argv[1])) {
4236 string_list_delete(opserv_bad_words, nn);
4237 for (it = dict_first(channels); it; it = iter_next(it)) {
4238 channel = iter_data(it);
4239 if (irccasestr(channel->name, argv[1])
4240 && !opserv_bad_channel(channel->name)) {
4241 DelChannelUser(cmd->parent->bot, channel, "Channel name no longer contains a bad word.", 1);
4242 timeq_del(0, opserv_part_channel, channel, TIMEQ_IGNORE_WHEN);
4243 channel->bad_channel = 0;
4244 }
4245 }
4246 reply("OSMSG_REMOVED_BAD", argv[1]);
4247 return 1;
4248 }
4249 }
4250 reply("OSMSG_NOT_BAD_WORD", argv[1]);
4251 return 0;
4252}
4253
4254static MODCMD_FUNC(cmd_addexempt)
4255{
4256 const char *chanName;
4257
4258 if ((argc > 1) && IsChannelName(argv[1])) {
4259 chanName = argv[1];
4260 } else {
4261 reply("MSG_NOT_CHANNEL_NAME");
4262 OPSERV_SYNTAX();
4263 return 0;
4264 }
4265 dict_insert(opserv_exempt_channels, strdup(chanName), NULL);
4266 channel = GetChannel(chanName);
4267 if (channel) {
4268 if (channel->bad_channel) {
4269 DelChannelUser(cmd->parent->bot, channel, "Channel is now exempt from bad-word checking.", 1);
4270 timeq_del(0, opserv_part_channel, channel, TIMEQ_IGNORE_WHEN);
4271 }
4272 channel->bad_channel = 0;
4273 }
4274 reply("OSMSG_ADDED_EXEMPTION", chanName);
4275 return 1;
4276}
4277
4278static MODCMD_FUNC(cmd_delexempt)
4279{
4280 const char *chanName;
4281
4282 if ((argc > 1) && IsChannelName(argv[1])) {
4283 chanName = argv[1];
4284 } else {
4285 reply("MSG_NOT_CHANNEL_NAME");
4286 OPSERV_SYNTAX();
4287 return 0;
4288 }
4289 if (!dict_remove(opserv_exempt_channels, chanName)) {
4290 reply("OSMSG_NOT_EXEMPT", chanName);
4291 return 0;
4292 }
4293 reply("OSMSG_REMOVED_EXEMPTION", chanName);
4294 return 1;
4295}
4296
4297static void
4298opserv_expire_trusted_host(void *data)
4299{
4300 struct trusted_host *th = data;
4301 dict_remove(opserv_trusted_hosts, th->ipaddr);
4302}
4303
4304static void
4305opserv_add_trusted_host(const char *ipaddr, unsigned int limit, const char *issuer, time_t issued, time_t expires, const char *reason)
4306{
4307 struct trusted_host *th;
4308 th = calloc(1, sizeof(*th));
4309 if (!th)
4310 return;
4311 th->ipaddr = strdup(ipaddr);
4312 th->reason = reason ? strdup(reason) : NULL;
4313 th->issuer = issuer ? strdup(issuer) : NULL;
4314 th->issued = issued;
4315 th->limit = limit;
4316 th->expires = expires;
4317 dict_insert(opserv_trusted_hosts, th->ipaddr, th);
4318 if (th->expires)
4319 timeq_add(th->expires, opserv_expire_trusted_host, th);
4320}
4321
4322static void
4323free_trusted_host(void *data)
4324{
4325 struct trusted_host *th = data;
4326 free(th->ipaddr);
4327 free(th->reason);
4328 free(th->issuer);
4329 free(th);
4330}
4331
4332static MODCMD_FUNC(cmd_addtrust)
4333{
4334 unsigned long interval;
4335 char *reason, *tmp;
2f61d1d7 4336 irc_in_addr_t tmpaddr;
d76ed9a9 4337 unsigned int count;
4338
4339 if (dict_find(opserv_trusted_hosts, argv[1], NULL)) {
4340 reply("OSMSG_ALREADY_TRUSTED", argv[1]);
4341 return 0;
4342 }
4343
2f61d1d7 4344 if (!irc_pton(&tmpaddr, NULL, argv[1])) {
d76ed9a9 4345 reply("OSMSG_BAD_IP", argv[1]);
4346 return 0;
4347 }
4348
4349 count = strtoul(argv[2], &tmp, 10);
4350 if (*tmp != '\0') {
4351 reply("OSMSG_BAD_NUMBER", argv[2]);
4352 return 0;
4353 }
4354
4355 interval = ParseInterval(argv[3]);
4356 if (!interval && strcmp(argv[3], "0")) {
4357 reply("MSG_INVALID_DURATION", argv[3]);
4358 return 0;
4359 }
4360
4361 reason = unsplit_string(argv+4, argc-4, NULL);
4362 opserv_add_trusted_host(argv[1], count, user->handle_info->handle, now, interval ? (now + interval) : 0, reason);
4363 reply("OSMSG_ADDED_TRUSTED");
4364 return 1;
4365}
4366
4367static MODCMD_FUNC(cmd_edittrust)
4368{
4369 unsigned long interval;
4370 struct trusted_host *th;
4371 char *reason, *tmp;
4372 unsigned int count;
4373
4374 th = dict_find(opserv_trusted_hosts, argv[1], NULL);
4375 if (!th) {
4376 reply("OSMSG_NOT_TRUSTED", argv[1]);
4377 return 0;
4378 }
4379 count = strtoul(argv[2], &tmp, 10);
1136f709 4380 if (*tmp != '\0') {
d76ed9a9 4381 reply("OSMSG_BAD_NUMBER", argv[2]);
4382 return 0;
4383 }
4384 interval = ParseInterval(argv[3]);
4385 if (!interval && strcmp(argv[3], "0")) {
4386 reply("MSG_INVALID_DURATION", argv[3]);
4387 return 0;
4388 }
4389 reason = unsplit_string(argv+4, argc-4, NULL);
4390 if (th->expires)
4391 timeq_del(th->expires, opserv_expire_trusted_host, th, 0);
4392
4393 free(th->reason);
4394 th->reason = strdup(reason);
4395 free(th->issuer);
4396 th->issuer = strdup(user->handle_info->handle);
4397 th->issued = now;
4398 th->limit = count;
4399 if (interval) {
4400 th->expires = now + interval;
4401 timeq_add(th->expires, opserv_expire_trusted_host, th);
4402 } else
4403 th->expires = 0;
4404 reply("OSMSG_UPDATED_TRUSTED", th->ipaddr);
4405 return 1;
4406}
4407
4408static MODCMD_FUNC(cmd_deltrust)
4409{
4410 unsigned int n;
4411
4412 for (n=1; n<argc; n++) {
4413 struct trusted_host *th = dict_find(opserv_trusted_hosts, argv[n], NULL);
4414 if (!th)
4415 continue;
4416 if (th->expires)
4417 timeq_del(th->expires, opserv_expire_trusted_host, th, 0);
4418 dict_remove(opserv_trusted_hosts, argv[n]);
4419 }
4420 reply("OSMSG_REMOVED_TRUSTED");
4421 return 1;
4422}
4423
4424/* This doesn't use dict_t because it's a little simpler to open-code the
4425 * comparisons (and simpler arg-passing for the ADD subcommand).
4426 */
4427static MODCMD_FUNC(cmd_clone)
4428{
4429 int i;
4430 struct userNode *clone;
4431
4432 clone = GetUserH(argv[2]);
4433 if (!irccasecmp(argv[1], "ADD")) {
4434 char *userinfo;
4435 char ident[USERLEN+1];
4436
258d1427 4437 if (argc < 5) {
4438 reply("MSG_MISSING_PARAMS", argv[1]);
4439 OPSERV_SYNTAX();
4440 return 0;
4441 }
4442 if (clone) {
4443 reply("OSMSG_CLONE_EXISTS", argv[2]);
4444 return 0;
4445 }
4446 userinfo = unsplit_string(argv+4, argc-4, NULL);
4447 for (i=0; argv[3][i] && (i<USERLEN); i++) {
4448 if (argv[3][i] == '@') {
4449 ident[i++] = 0;
4450 break;
4451 } else {
d76ed9a9 4452 ident[i] = argv[3][i];
4453 }
258d1427 4454 }
4455 if (!argv[3][i] || (i==USERLEN)) {
4456 reply("OSMSG_NOT_A_HOSTMASK");
4457 return 0;
4458 }
1136f709 4459 if (!(clone = AddLocalUser(argv[2], ident, argv[3]+i, userinfo, "+i"))) {
d76ed9a9 4460 reply("OSMSG_CLONE_FAILED", argv[2]);
4461 return 0;
4462 }
4463 reply("OSMSG_CLONE_ADDED", clone->nick);
258d1427 4464 return 1;
d76ed9a9 4465 }
4466 if (!clone) {
258d1427 4467 reply("MSG_NICK_UNKNOWN", argv[2]);
4468 return 0;
d76ed9a9 4469 }
4470 if (clone->uplink != self || IsService(clone)) {
258d1427 4471 reply("OSMSG_NOT_A_CLONE", clone->nick);
4472 return 0;
d76ed9a9 4473 }
4474 if (!irccasecmp(argv[1], "REMOVE")) {
258d1427 4475 const char *reason;
4476 if (argc > 3) {
4477 reason = unsplit_string(argv+3, argc-3, NULL);
4478 } else {
4479 char *tmp;
4480 tmp = alloca(strlen(clone->nick) + strlen(OSMSG_PART_REASON));
4481 sprintf(tmp, OSMSG_PART_REASON, clone->nick);
4482 reason = tmp;
4483 }
4484 DelUser(clone, NULL, 1, reason);
4485 reply("OSMSG_CLONE_REMOVED", argv[2]);
4486 return 1;
d76ed9a9 4487 }
4488 if (argc < 4) {
258d1427 4489 reply("MSG_MISSING_PARAMS", argv[1]);
4490 OPSERV_SYNTAX();
4491 return 0;
d76ed9a9 4492 }
4493 channel = GetChannel(argv[3]);
4494 if (!irccasecmp(argv[1], "JOIN")) {
258d1427 4495 if (!channel
4496 && !(channel = AddChannel(argv[3], now, NULL, NULL, NULL))) {
4497 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
4498 return 0;
4499 }
4500 AddChannelUser(clone, channel);
4501 reply("OSMSG_CLONE_JOINED", clone->nick, channel->name);
4502 return 1;
d76ed9a9 4503 }
4504 if (!irccasecmp(argv[1], "PART")) {
258d1427 4505 if (!channel) {
4506 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
4507 return 0;
4508 }
4509 if (!GetUserMode(channel, clone)) {
4510 reply("OSMSG_NOT_ON_CHANNEL", clone->nick, channel->name);
4511 return 0;
4512 }
4513 reply("OSMSG_CLONE_PARTED", clone->nick, channel->name);
4514 DelChannelUser(clone, channel, "Leaving.", 0);
4515 return 1;
d76ed9a9 4516 }
4517 if (!irccasecmp(argv[1], "OP")) {
4518 struct mod_chanmode change;
258d1427 4519 if (!channel) {
4520 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
4521 return 0;
4522 }
d76ed9a9 4523 mod_chanmode_init(&change);
4524 change.argc = 1;
4525 change.args[0].mode = MODE_CHANOP;
a32da4c7 4526 change.args[0].u.member = GetUserMode(channel, clone);
4527 if (!change.args[0].u.member) {
d76ed9a9 4528 reply("OSMSG_NOT_ON_CHANNEL", clone->nick, channel->name);
4529 return 0;
258d1427 4530 }
d76ed9a9 4531 modcmd_chanmode_announce(&change);
258d1427 4532 reply("OSMSG_OPS_GIVEN", channel->name, clone->nick);
4533 return 1;
d76ed9a9 4534 }
55342ce8 4535 if (!irccasecmp(argv[1], "HOP")) {
4536 struct mod_chanmode change;
4537 if (!channel) {
4538 reply("MSG_CHANNEL_UNKNOWN", argv[3]);
4539 return 0;
4540 }
4541 mod_chanmode_init(&change);
4542 change.argc = 1;
4543 change.args[0].mode = MODE_HALFOP;
11408ce4 4544 change.args[0].u.member = GetUserMode(channel, clone);
4545 if (!change.args[0].u.member) {
55342ce8 4546 reply("OSMSG_NOT_ON_CHANNEL", clone->nick, channel->name);
4547 return 0;
4548 }
4549 modcmd_chanmode_announce(&change);
4550 reply("OSMSG_HOPS_GIVEN", channel->name, clone->nick);
4551 return 1;
4552 }
d76ed9a9 4553 if (argc < 5) {
258d1427 4554 reply("MSG_MISSING_PARAMS", argv[1]);
4555 OPSERV_SYNTAX();
4556 return 0;
d76ed9a9 4557 }
4558 if (!irccasecmp(argv[1], "SAY")) {
258d1427 4559 char *text = unsplit_string(argv+4, argc-4, NULL);
4560 irc_privmsg(clone, argv[3], text);
4561 reply("OSMSG_CLONE_SAID", clone->nick, argv[3]);
4562 return 1;
d76ed9a9 4563 }
4564 reply("OSMSG_UNKNOWN_SUBCOMMAND", argv[1], argv[0]);
4565 return 0;
4566}
4567
4568static struct helpfile_expansion
4569opserv_help_expand(const char *variable)
4570{
4571 extern struct userNode *message_source;
4572 struct helpfile_expansion exp;
4573 struct service *service;
4574 struct svccmd *cmd;
4575 dict_iterator_t it;
4576 int row;
4577 unsigned int level;
4578
4579 if (!(service = service_find(message_source->nick))) {
4580 exp.type = HF_STRING;
4581 exp.value.str = NULL;
4582 } else if (!irccasecmp(variable, "index")) {
4583 exp.type = HF_TABLE;
4584 exp.value.table.length = 1;
4585 exp.value.table.width = 2;
4586 exp.value.table.flags = TABLE_REPEAT_HEADERS | TABLE_REPEAT_ROWS;
4587 exp.value.table.contents = calloc(dict_size(service->commands)+1, sizeof(char**));
4588 exp.value.table.contents[0] = calloc(exp.value.table.width, sizeof(char*));
4589 exp.value.table.contents[0][0] = "Command";
4590 exp.value.table.contents[0][1] = "Level";
4591 for (it=dict_first(service->commands); it; it=iter_next(it)) {
4592 cmd = iter_data(it);
4593 row = exp.value.table.length++;
4594 exp.value.table.contents[row] = calloc(exp.value.table.width, sizeof(char*));
4595 exp.value.table.contents[row][0] = iter_key(it);
4596 level = cmd->min_opserv_level;
4597 if (!level_strings[level]) {
4598 level_strings[level] = malloc(16);
4599 snprintf(level_strings[level], 16, "%3d", level);
4600 }
4601 exp.value.table.contents[row][1] = level_strings[level];
4602 }
4603 } else if (!strncasecmp(variable, "level", 5)) {
4604 cmd = dict_find(service->commands, variable+6, NULL);
4605 exp.type = HF_STRING;
4606 if (cmd) {
4607 level = cmd->min_opserv_level;
4608 exp.value.str = malloc(16);
4609 snprintf(exp.value.str, 16, "%3d", level);
4610 } else {
4611 exp.value.str = NULL;
4612 }
4613 } else {
4614 exp.type = HF_STRING;
4615 exp.value.str = NULL;
4616 }
4617 return exp;
4618}
4619
4620struct modcmd *
4621opserv_define_func(const char *name, modcmd_func_t *func, int min_level, int reqchan, int min_argc)
4622{
4623 char buf[16], *flags = NULL;
4624 unsigned int iflags = 0;
4625 sprintf(buf, "%d", min_level);
4626 switch (reqchan) {
4627 case 1: flags = "+acceptchan"; break;
4628 case 3: flags = "+acceptpluschan"; /* fall through */
4629 case 2: iflags = MODCMD_REQUIRE_CHANNEL; break;
4630 }
4631 if (flags) {
4632 return modcmd_register(opserv_module, name, func, min_argc, iflags, "level", buf, "flags", flags, "flags", "+oper", NULL);
4633 } else {
4634 return modcmd_register(opserv_module, name, func, min_argc, iflags, "level", buf, "flags", "+oper", NULL);
4635 }
4636}
4637
4638int add_reserved(const char *key, void *data, void *extra)
4639{
7637f48f 4640 struct chanNode *chan;
d76ed9a9 4641 struct record_data *rd = data;
4642 const char *ident, *hostname, *desc;
7637f48f 4643 unsigned int i;
d76ed9a9 4644 struct userNode *reserve;
4645 ident = database_get_data(rd->d.object, KEY_IDENT, RECDB_QSTRING);
4646 if (!ident) {
258d1427 4647 log_module(OS_LOG, LOG_ERROR, "Missing ident for reserve of %s", key);
4648 return 0;
d76ed9a9 4649 }
4650 hostname = database_get_data(rd->d.object, KEY_HOSTNAME, RECDB_QSTRING);
4651 if (!hostname) {
258d1427 4652 log_module(OS_LOG, LOG_ERROR, "Missing hostname for reserve of %s", key);
4653 return 0;
d76ed9a9 4654 }
4655 desc = database_get_data(rd->d.object, KEY_DESC, RECDB_QSTRING);
4656 if (!desc) {
258d1427 4657 log_module(OS_LOG, LOG_ERROR, "Missing description for reserve of %s", key);
4658 return 0;
d76ed9a9 4659 }
1136f709 4660 if ((reserve = AddLocalUser(key, ident, hostname, desc, "+i"))) {
d76ed9a9 4661 reserve->modes |= FLAGS_PERSISTENT;
4662 dict_insert(extra, reserve->nick, reserve);
4663 }
7637f48f 4664
4665 if (autojoin_channels && reserve) {
4666 for (i = 0; i < autojoin_channels->used; i++) {
4667 chan = AddChannel(autojoin_channels->list[i], now, "+nt", NULL, NULL);
4668 AddChannelUser(reserve, chan)->modes |= MODE_VOICE;
4669 }
4670 }
4671
d76ed9a9 4672 return 0;
4673}
4674
4675static unsigned int
4676foreach_matching_user(const char *hostmask, discrim_search_func func, void *extra)
4677{
4678 discrim_t discrim;
4679 char *dupmask;
4680 unsigned int matched;
4681
4682 if (!self->uplink) return 0;
4683 discrim = calloc(1, sizeof(*discrim));
4684 discrim->limit = dict_size(clients);
4685 discrim->max_level = ~0;
4686 discrim->max_ts = now;
4687 discrim->max_channels = INT_MAX;
4688 discrim->authed = -1;
4689 discrim->info_space = -1;
63665495 4690 discrim->intra_scmp = 0;
4691 discrim->intra_dcmp = 0;
27eaa617 4692 discrim->use_regex = 0;
1c5f6697 4693 discrim->silent = 0;
d76ed9a9 4694 dupmask = strdup(hostmask);
4695 if (split_ircmask(dupmask, &discrim->mask_nick, &discrim->mask_ident, &discrim->mask_host)) {
2f61d1d7 4696 if (!irc_pton(&discrim->ip_mask, &discrim->ip_mask_bits, discrim->mask_host))
4697 discrim->ip_mask_bits = 0;
d76ed9a9 4698 matched = opserv_discrim_search(discrim, func, extra);
4699 } else {
258d1427 4700 log_module(OS_LOG, LOG_ERROR, "Couldn't split IRC mask for gag %s!", hostmask);
d76ed9a9 4701 matched = 0;
4702 }
4703 free(discrim);
4704 free(dupmask);
4705 return matched;
4706}
4707
4708static unsigned int
4709gag_free(struct gag_entry *gag)
4710{
4711 unsigned int ungagged;
4712
4713 /* Remove from gag list */
4714 if (gagList == gag) {
4715 gagList = gag->next;
4716 } else {
4717 struct gag_entry *prev;
4718 for (prev = gagList; prev->next != gag; prev = prev->next) ;
4719 prev->next = gag->next;
4720 }
4721
4722 ungagged = foreach_matching_user(gag->mask, ungag_helper_func, NULL);
4723
4724 /* Deallocate storage */
4725 free(gag->reason);
4726 free(gag->owner);
4727 free(gag->mask);
4728 free(gag);
4729
4730 return ungagged;
4731}
4732
4733static void
4734gag_expire(void *data)
4735{
4736 gag_free(data);
4737}
4738
4739unsigned int
4740gag_create(const char *mask, const char *owner, const char *reason, time_t expires)
4741{
4742 struct gag_entry *gag;
4743
4744 /* Create gag and put it into linked list */
4745 gag = calloc(1, sizeof(*gag));
4746 gag->mask = strdup(mask);
4747 gag->owner = strdup(owner ? owner : "<unknown>");
4748 gag->reason = strdup(reason ? reason : "<unknown>");
4749 gag->expires = expires;
4750 if (gag->expires)
4751 timeq_add(gag->expires, gag_expire, gag);
4752 gag->next = gagList;
4753 gagList = gag;
4754
4755 /* If we're linked, see if who the gag applies to */
4756 return foreach_matching_user(mask, gag_helper_func, gag);
4757}
4758
4759static int
4760add_gag_helper(const char *key, void *data, UNUSED_ARG(void *extra))
4761{
4762 struct record_data *rd = data;
4763 char *owner, *reason, *expstr;
4764 time_t expires;
4765
4766 owner = database_get_data(rd->d.object, KEY_OWNER, RECDB_QSTRING);
4767 reason = database_get_data(rd->d.object, KEY_REASON, RECDB_QSTRING);
4768 expstr = database_get_data(rd->d.object, KEY_EXPIRES, RECDB_QSTRING);
4769 expires = expstr ? strtoul(expstr, NULL, 0) : 0;
4770 gag_create(key, owner, reason, expires);
4771
4772 return 0;
4773}
4774
4775static struct opserv_user_alert *
a947ef71 4776opserv_add_user_alert(struct userNode *req, const char *name, opserv_alert_reaction reaction, const char *text_discrim, int last, int expire)
d76ed9a9 4777{
4778 unsigned int wordc;
4779 char *wordv[MAXNUMPARAMS], *discrim_copy;
4780 struct opserv_user_alert *alert;
4781 char *name_dup;
4782
4783 if (dict_find(opserv_user_alerts, name, NULL)) {
258d1427 4784 send_message(req, opserv, "OSMSG_ALERT_EXISTS", name);
4785 return NULL;
d76ed9a9 4786 }
4787 alert = malloc(sizeof(*alert));
4788 alert->owner = strdup(req->handle_info ? req->handle_info->handle : req->nick);
4789 alert->text_discrim = strdup(text_discrim);
083fb96b 4790 alert->last = last;
d76ed9a9 4791 discrim_copy = strdup(text_discrim); /* save a copy of the discrim */
4792 wordc = split_line(discrim_copy, false, ArrayLength(wordv), wordv);
258d1427 4793 alert->discrim = opserv_discrim_create(req, opserv, wordc, wordv, 0);
a947ef71 4794 alert->expire = expire;
d82cf2f0 4795 /* Check for missing required criteria or broken records */
39c1a4ef 4796 if (!alert->discrim || (reaction==REACT_SVSJOIN && !alert->discrim->chantarget) ||
d82cf2f0 4797 (reaction==REACT_SVSPART && !alert->discrim->chantarget) ||
4798 (reaction==REACT_MARK && !alert->discrim->mark)) {
d76ed9a9 4799 free(alert->text_discrim);
4800 free(discrim_copy);
4801 free(alert);
4802 return NULL;
4803 }
4804 alert->split_discrim = discrim_copy;
4805 name_dup = strdup(name);
4806 if (!alert->discrim->reason)
4807 alert->discrim->reason = strdup(name);
4808 alert->reaction = reaction;
4809 dict_insert(opserv_user_alerts, name_dup, alert);
697f4c9a 4810 /* Stick the alert into the appropriate additional alert dict(s).
4811 * For channel alerts, we only use channels and min_channels;
4812 * max_channels would have to be checked on /part, which we do not
4813 * yet do, and which seems of questionable value.
4814 */
1136f709 4815 if (alert->discrim->channel_count || alert->discrim->min_channels)
d76ed9a9 4816 dict_insert(opserv_channel_alerts, name_dup, alert);
697f4c9a 4817 if (alert->discrim->mask_nick)
d76ed9a9 4818 dict_insert(opserv_nick_based_alerts, name_dup, alert);
b7594203 4819 if (alert->discrim->accountmask)
4820 dict_insert(opserv_account_based_alerts, name_dup, alert);
a947ef71 4821
4822 if (alert->expire)
4823 timeq_add(alert->expire, alert_expire, (void*)name_dup);
d76ed9a9 4824 return alert;
4825}
4826
de9510bc 4827/*
d76ed9a9 4828static int
4829add_chan_warn(const char *key, void *data, UNUSED_ARG(void *extra))
4830{
4831 struct record_data *rd = data;
4832 char *reason = GET_RECORD_QSTRING(rd);
4833
de9510bc 4834 * i hope this can't happen *
d76ed9a9 4835 if (!reason)
4836 reason = "No Reason";
4837
4838 dict_insert(opserv_chan_warn, strdup(key), strdup(reason));
4839 return 0;
4840}
de9510bc 4841*/
d76ed9a9 4842
47956fc5 4843
d76ed9a9 4844static int
4845add_user_alert(const char *key, void *data, UNUSED_ARG(void *extra))
4846{
4847 dict_t alert_dict;
083fb96b 4848 char *str;
a947ef71 4849 int last = 0, expire = 0;
d76ed9a9 4850 const char *discrim, *react, *owner;
4851 opserv_alert_reaction reaction;
4852 struct opserv_user_alert *alert;
4853
4854 if (!(alert_dict = GET_RECORD_OBJECT((struct record_data *)data))) {
4855 log_module(OS_LOG, LOG_ERROR, "Bad type (not a record) for alert %s.", key);
4856 return 1;
4857 }
4858 discrim = database_get_data(alert_dict, KEY_DISCRIM, RECDB_QSTRING);
4859 react = database_get_data(alert_dict, KEY_REACTION, RECDB_QSTRING);
083fb96b 4860 str = database_get_data(alert_dict, KEY_LAST, RECDB_QSTRING);
4861 if (str)
4862 last = atoi(str);
a947ef71 4863 str = database_get_data(alert_dict, KEY_EXPIRE, RECDB_QSTRING);
4864 if (str)
4865 expire = atoi(str);
083fb96b 4866
d76ed9a9 4867 if (!react || !irccasecmp(react, "notice"))
4868 reaction = REACT_NOTICE;
4869 else if (!irccasecmp(react, "kill"))
4870 reaction = REACT_KILL;
1c5f6697 4871 /*
9a75756e 4872 else if (!irccasecmp(react, "silent"))
4873 reaction = REACT_SILENT;
1c5f6697 4874 */
d76ed9a9 4875 else if (!irccasecmp(react, "gline"))
4876 reaction = REACT_GLINE;
ec311f39 4877 else if (!irccasecmp(react, "track"))
4878 reaction = REACT_TRACK;
d914d1cb 4879 else if (!irccasecmp(react, "shun"))
4880 reaction = REACT_SHUN;
c408f18a 4881 else if (!irccasecmp(react, "svsjoin"))
4882 reaction = REACT_SVSJOIN;
39c1a4ef 4883 else if (!irccasecmp(react, "svspart"))
4884 reaction = REACT_SVSPART;
0e08a8e0 4885 else if (!irccasecmp(react, "version"))
4886 reaction = REACT_VERSION;
d82cf2f0 4887 else if (!irccasecmp(react, "mark"))
4888 reaction = REACT_MARK;
890866b0 4889 else if (!irccasecmp(react, "noticeuser"))
4890 reaction = REACT_NOTICEUSER;
4891 else if (!irccasecmp(react, "msguser"))
4892 reaction = REACT_MSGUSER;
d76ed9a9 4893 else {
4894 log_module(OS_LOG, LOG_ERROR, "Invalid reaction %s for alert %s.", react, key);
4895 return 0;
4896 }
a947ef71 4897 alert = opserv_add_user_alert(opserv, key, reaction, discrim, last, expire);
d76ed9a9 4898 if (!alert) {
4899 log_module(OS_LOG, LOG_ERROR, "Unable to create alert %s from database.", key);
4900 return 0;
4901 }
4902 owner = database_get_data(alert_dict, KEY_OWNER, RECDB_QSTRING);
4903 free(alert->owner);
4904 alert->owner = strdup(owner ? owner : "<unknown>");
4905 return 0;
4906}
4907
4908static int
4909trusted_host_read(const char *host, void *data, UNUSED_ARG(void *extra))
4910{
4911 struct record_data *rd = data;
4912 const char *limit, *str, *reason, *issuer;
4913 time_t issued, expires;
4914
4915 if (rd->type == RECDB_QSTRING) {
4916 /* old style host by itself */
4917 limit = GET_RECORD_QSTRING(rd);
4918 issued = 0;
4919 issuer = NULL;
4920 expires = 0;
4921 reason = NULL;
4922 } else if (rd->type == RECDB_OBJECT) {
4923 dict_t obj = GET_RECORD_OBJECT(rd);
4924 /* new style structure */
4925 limit = database_get_data(obj, KEY_LIMIT, RECDB_QSTRING);
4926 str = database_get_data(obj, KEY_EXPIRES, RECDB_QSTRING);
4927 expires = str ? ParseInterval(str) : 0;
4928 reason = database_get_data(obj, KEY_REASON, RECDB_QSTRING);
4929 issuer = database_get_data(obj, KEY_ISSUER, RECDB_QSTRING);
4930 str = database_get_data(obj, KEY_ISSUED, RECDB_QSTRING);
4931 issued = str ? ParseInterval(str) : 0;
4932 } else
4933 return 0;
4934
4935 if (expires && (expires < now))
4936 return 0;
4937 opserv_add_trusted_host(host, (limit ? strtoul(limit, NULL, 0) : 0), issuer, issued, expires, reason);
4938 return 0;
4939}
4940
47956fc5 4941static int
4942add_routing_plan_server(const char *name, void *data, void *rp)
4943{
4944 struct record_data *rd = data;
4945 const char *uplink, *portstr, *karma, *second, *offline;
4946
4947 dict_t obj = GET_RECORD_OBJECT(rd);
4948 if(rd->type == RECDB_OBJECT) {
4949 uplink = database_get_data(obj, KEY_UPLINK, RECDB_QSTRING);
4950 second = database_get_data(obj, KEY_SECOND, RECDB_QSTRING);
4951 portstr = database_get_data(obj, KEY_PORT, RECDB_QSTRING);
4952 karma = database_get_data(obj, KEY_KARMA, RECDB_QSTRING);
4953 offline = database_get_data(obj, KEY_OFFLINE, RECDB_QSTRING);
4954 /* create routing plan server named key, with uplink uplink. */
4955 opserv_routing_plan_add_server(rp, name, uplink, portstr ? atoi(portstr) : 0,
4956 karma ? atoi(karma) : KARMA_DEFAULT, second,
4957 offline ? atoi(offline) : 0);
4958 }
4959 return 0;
4960
4961}
4962
4963static int
4964routing_plan_set_option(const char *name, void *data, UNUSED_ARG(void *extra))
4965{
4966 struct record_data *rd = data;
4967 if(rd->type == RECDB_QSTRING)
4968 {
4969 char *value = GET_RECORD_QSTRING(rd);
4970 dict_insert(opserv_routing_plan_options, strdup(name), strdup(value));
4971 }
4972 return 0;
4973}
4974
4975static int
4976add_routing_plan(const char *name, void *data, UNUSED_ARG(void *extra))
4977{
4978 struct record_data *rd = data;
4979 struct routingPlan *rp;
4980
4981 if(rd->type == RECDB_OBJECT) {
4982 dict_t obj = GET_RECORD_OBJECT(rd);
4983 rp = opserv_add_routing_plan(name);
4984 dict_foreach(obj, add_routing_plan_server, rp);
4985 }
4986 return 0;
4987}
4988
d76ed9a9 4989static int
4990opserv_saxdb_read(struct dict *conf_db)
4991{
4992 dict_t object;
4993 struct record_data *rd;
4994 dict_iterator_t it;
4995 unsigned int nn;
4996
4997 if ((object = database_get_data(conf_db, KEY_RESERVES, RECDB_OBJECT)))
4998 dict_foreach(object, add_reserved, opserv_reserved_nick_dict);
4999 if ((rd = database_get_path(conf_db, KEY_BAD_WORDS))) {
5000 switch (rd->type) {
5001 case RECDB_STRING_LIST:
5002 /* Add words one by one just in case there are overlaps from an old DB. */
5003 for (nn=0; nn<rd->d.slist->used; ++nn)
5004 opserv_add_bad_word(NULL, NULL, rd->d.slist->list[nn]);
5005 break;
5006 case RECDB_OBJECT:
5007 for (it=dict_first(rd->d.object); it; it=iter_next(it)) {
5008 opserv_add_bad_word(NULL, NULL, iter_key(it));
5009 rd = iter_data(it);
5010 if (rd->type == RECDB_STRING_LIST)
5011 for (nn=0; nn<rd->d.slist->used; nn++)
5012 dict_insert(opserv_exempt_channels, strdup(rd->d.slist->list[nn]), NULL);
5013 }
5014 break;
5015 default:
5016 /* do nothing */;
5017 }
5018 }
5019 if ((rd = database_get_path(conf_db, KEY_EXEMPT_CHANNELS))
5020 && (rd->type == RECDB_STRING_LIST)) {
5021 for (nn=0; nn<rd->d.slist->used; ++nn)
5022 dict_insert(opserv_exempt_channels, strdup(rd->d.slist->list[nn]), NULL);
5023 }
5024 if ((object = database_get_data(conf_db, KEY_MAX_CLIENTS, RECDB_OBJECT))) {
5025 char *str;
5026 if ((str = database_get_data(object, KEY_MAX, RECDB_QSTRING)))
5027 max_clients = atoi(str);
5028 if ((str = database_get_data(object, KEY_TIME, RECDB_QSTRING)))
5029 max_clients_time = atoi(str);
5030 }
5031 if ((object = database_get_data(conf_db, KEY_TRUSTED_HOSTS, RECDB_OBJECT)))
5032 dict_foreach(object, trusted_host_read, opserv_trusted_hosts);
5033 if ((object = database_get_data(conf_db, KEY_GAGS, RECDB_OBJECT)))
5034 dict_foreach(object, add_gag_helper, NULL);
5035 if ((object = database_get_data(conf_db, KEY_ALERTS, RECDB_OBJECT)))
5036 dict_foreach(object, add_user_alert, NULL);
de9510bc 5037/*
d76ed9a9 5038 if ((object = database_get_data(conf_db, KEY_WARN, RECDB_OBJECT)))
5039 dict_foreach(object, add_chan_warn, NULL);
de9510bc 5040*/
47956fc5 5041
5042 if ((object = database_get_data(conf_db, KEY_ROUTINGPLAN, RECDB_OBJECT)))
5043 dict_foreach(object, add_routing_plan, NULL);
5044
5045 if ((object = database_get_data(conf_db, KEY_ROUTINGPLAN_OPTIONS, RECDB_OBJECT)))
5046 dict_foreach(object, routing_plan_set_option, NULL);
5047
d76ed9a9 5048 return 0;
5049}
5050
5051static int
5052opserv_saxdb_write(struct saxdb_context *ctx)
5053{
5054 struct string_list *slist;
5055 dict_iterator_t it;
5056
5057 /* reserved nicks */
5058 if (dict_size(opserv_reserved_nick_dict)) {
5059 saxdb_start_record(ctx, KEY_RESERVES, 1);
5060 for (it = dict_first(opserv_reserved_nick_dict); it; it = iter_next(it)) {
5061 struct userNode *user = iter_data(it);
5062 if (!IsPersistent(user)) continue;
5063 saxdb_start_record(ctx, iter_key(it), 0);
5064 saxdb_write_string(ctx, KEY_IDENT, user->ident);
5065 saxdb_write_string(ctx, KEY_HOSTNAME, user->hostname);
5066 saxdb_write_string(ctx, KEY_DESC, user->info);
5067 saxdb_end_record(ctx);
5068 }
5069 saxdb_end_record(ctx);
5070 }
5071 /* bad word set */
5072 if (opserv_bad_words->used) {
5073 saxdb_write_string_list(ctx, KEY_BAD_WORDS, opserv_bad_words);
5074 }
47956fc5 5075 /* routing plan options */
5076 if (dict_size(opserv_routing_plan_options)) {
5077 saxdb_start_record(ctx, KEY_ROUTINGPLAN_OPTIONS, 1);
5078 for(it = dict_first(opserv_routing_plan_options); it; it = iter_next(it)) {
5079 saxdb_write_string(ctx, iter_key(it), iter_data(it));
5080 }
5081 saxdb_end_record(ctx);
5082 }
5083 /* routing plans */
5084 if (dict_size(opserv_routing_plans)) {
5085 dict_iterator_t svrit;
5086 struct routingPlan *rp;
5087 struct routingPlanServer *rps;
5088 saxdb_start_record(ctx, KEY_ROUTINGPLAN, 1);
5089 for (it = dict_first(opserv_routing_plans); it; it = iter_next(it)) {
5090 rp = iter_data(it);
5091 saxdb_start_record(ctx, iter_key(it), 0);
5092 for(svrit = dict_first(rp->servers); svrit; svrit = iter_next(svrit)) {
5093 char buf[MAXLEN];
5094 rps = iter_data(svrit);
5095 saxdb_start_record(ctx, iter_key(svrit), 0);
5096 saxdb_write_string(ctx, KEY_UPLINK, rps->uplink);
5097 if(rps->secondaryuplink)
5098 saxdb_write_string(ctx, KEY_SECOND, rps->secondaryuplink);
5099 sprintf(buf, "%d", rps->port);
5100 saxdb_write_string(ctx, KEY_PORT, buf);
5101 sprintf(buf, "%d", rps->karma);
5102 saxdb_write_string(ctx, KEY_KARMA, buf);
5103 sprintf(buf, "%d", rps->offline);
5104 saxdb_write_string(ctx, KEY_OFFLINE, buf);
5105 saxdb_end_record(ctx);
5106 }
5107 saxdb_end_record(ctx);
5108 }
5109 saxdb_end_record(ctx);
5110 }
d76ed9a9 5111 /* insert exempt channel names */
5112 if (dict_size(opserv_exempt_channels)) {
5113 slist = alloc_string_list(dict_size(opserv_exempt_channels));
5114 for (it=dict_first(opserv_exempt_channels); it; it=iter_next(it)) {
5115 string_list_append(slist, strdup(iter_key(it)));
5116 }
5117 saxdb_write_string_list(ctx, KEY_EXEMPT_CHANNELS, slist);
5118 free_string_list(slist);
5119 }
5120 /* trusted hosts takes a little more work */
5121 if (dict_size(opserv_trusted_hosts)) {
5122 saxdb_start_record(ctx, KEY_TRUSTED_HOSTS, 1);
5123 for (it = dict_first(opserv_trusted_hosts); it; it = iter_next(it)) {
5124 struct trusted_host *th = iter_data(it);
5125 saxdb_start_record(ctx, iter_key(it), 0);
5126 if (th->limit) saxdb_write_int(ctx, KEY_LIMIT, th->limit);
5127 if (th->expires) saxdb_write_int(ctx, KEY_EXPIRES, th->expires);
5128 if (th->issued) saxdb_write_int(ctx, KEY_ISSUED, th->issued);
5129 if (th->issuer) saxdb_write_string(ctx, KEY_ISSUER, th->issuer);
5130 if (th->reason) saxdb_write_string(ctx, KEY_REASON, th->reason);
5131 saxdb_end_record(ctx);
5132 }
5133 saxdb_end_record(ctx);
5134 }
5135 /* gags */
5136 if (gagList) {
5137 struct gag_entry *gag;
5138 saxdb_start_record(ctx, KEY_GAGS, 1);
5139 for (gag = gagList; gag; gag = gag->next) {
5140 saxdb_start_record(ctx, gag->mask, 0);
5141 saxdb_write_string(ctx, KEY_OWNER, gag->owner);
5142 saxdb_write_string(ctx, KEY_REASON, gag->reason);
5143 if (gag->expires) saxdb_write_int(ctx, KEY_EXPIRES, gag->expires);
5144 saxdb_end_record(ctx);
5145 }
5146 saxdb_end_record(ctx);
5147 }
5148 /* channel warnings */
de9510bc 5149 /*
d76ed9a9 5150 if (dict_size(opserv_chan_warn)) {
5151 saxdb_start_record(ctx, KEY_WARN, 0);
5152 for (it = dict_first(opserv_chan_warn); it; it = iter_next(it)) {
5153 saxdb_write_string(ctx, iter_key(it), iter_data(it));
5154 }
5155 saxdb_end_record(ctx);
5156 }
de9510bc 5157 */
d76ed9a9 5158 /* alerts */
5159 if (dict_size(opserv_user_alerts)) {
5160 saxdb_start_record(ctx, KEY_ALERTS, 1);
5161 for (it = dict_first(opserv_user_alerts); it; it = iter_next(it)) {
5162 struct opserv_user_alert *alert = iter_data(it);
5163 const char *reaction;
5164 saxdb_start_record(ctx, iter_key(it), 0);
5165 saxdb_write_string(ctx, KEY_DISCRIM, alert->text_discrim);
5166 saxdb_write_string(ctx, KEY_OWNER, alert->owner);
083fb96b 5167 saxdb_write_int(ctx, KEY_LAST, alert->last);
a947ef71 5168 saxdb_write_int(ctx, KEY_EXPIRE, alert->expire);
d76ed9a9 5169 switch (alert->reaction) {
5170 case REACT_NOTICE: reaction = "notice"; break;
5171 case REACT_KILL: reaction = "kill"; break;
1c5f6697 5172// case REACT_SILENT: reaction = "silent"; break;
d76ed9a9 5173 case REACT_GLINE: reaction = "gline"; break;
ec311f39 5174 case REACT_TRACK: reaction = "track"; break;
d914d1cb 5175 case REACT_SHUN: reaction = "shun"; break;
c408f18a 5176 case REACT_SVSJOIN: reaction = "svsjoin"; break;
39c1a4ef 5177 case REACT_SVSPART: reaction = "svspart"; break;
0e08a8e0 5178 case REACT_VERSION: reaction = "version"; break;
d82cf2f0 5179 case REACT_MARK: reaction = "mark"; break;
890866b0 5180 case REACT_NOTICEUSER: reaction = "noticeuser"; break;
5181 case REACT_MSGUSER: reaction = "msguser"; break;
d76ed9a9 5182 default:
5183 reaction = NULL;
5184 log_module(OS_LOG, LOG_ERROR, "Invalid reaction type %d for alert %s (while writing database).", alert->reaction, iter_key(it));
5185 break;
5186 }
5187 if (reaction) saxdb_write_string(ctx, KEY_REACTION, reaction);
5188 saxdb_end_record(ctx);
5189 }
5190 saxdb_end_record(ctx);
5191 }
5192 /* max clients */
5193 saxdb_start_record(ctx, KEY_MAX_CLIENTS, 0);
5194 saxdb_write_int(ctx, KEY_MAX, max_clients);
5195 saxdb_write_int(ctx, KEY_TIME, max_clients_time);
5196 saxdb_end_record(ctx);
5197 return 0;
5198}
5199
5200static int
5201query_keys_helper(const char *key, UNUSED_ARG(void *data), void *extra)
5202{
5203 send_message_type(4, extra, opserv, "$b%s$b", key);
5204 return 0;
5205}
5206
5207static MODCMD_FUNC(cmd_query)
5208{
5209 struct record_data *rd;
5210 unsigned int i;
5211 char *nodename;
5212
5213 if (argc < 2) {
258d1427 5214 reply("OSMSG_OPTION_ROOT");
5215 conf_enum_root(query_keys_helper, user);
5216 return 1;
d76ed9a9 5217 }
5218
5219 nodename = unsplit_string(argv+1, argc-1, NULL);
5220 if (!(rd = conf_get_node(nodename))) {
258d1427 5221 reply("OSMSG_UNKNOWN_OPTION", nodename);
5222 return 0;
d76ed9a9 5223 }
5224
5225 if (rd->type == RECDB_QSTRING)
258d1427 5226 reply("OSMSG_OPTION_IS", nodename, rd->d.qstring);
d76ed9a9 5227 else if (rd->type == RECDB_STRING_LIST) {
258d1427 5228 reply("OSMSG_OPTION_LIST", nodename);
5229 if (rd->d.slist->used)
5230 for (i=0; i<rd->d.slist->used; i++)
5231 send_message_type(4, user, cmd->parent->bot, "$b%s$b", rd->d.slist->list[i]);
5232 else
5233 reply("OSMSG_OPTION_LIST_EMPTY");
d76ed9a9 5234 } else if (rd->type == RECDB_OBJECT) {
258d1427 5235 reply("OSMSG_OPTION_KEYS", nodename);
5236 dict_foreach(rd->d.object, query_keys_helper, user);
d76ed9a9 5237 }
5238
5239 return 1;
5240}
5241
5242static MODCMD_FUNC(cmd_set)
5243{
5244 struct record_data *rd;
5245
5246 /* I originally wanted to be able to fully manipulate the config
5247 db with this, but i wussed out. feel free to fix this - you'll
5248 need to handle quoted strings which have been split, and likely
5249 invent a syntax for it. -Zoot */
5250
5251 if (!(rd = conf_get_node(argv[1]))) {
258d1427 5252 reply("OSMSG_SET_NOT_SET", argv[1]);
5253 return 0;
d76ed9a9 5254 }
5255
5256 if (rd->type != RECDB_QSTRING) {
258d1427 5257 reply("OSMSG_SET_BAD_TYPE", argv[1]);
5258 return 0;
d76ed9a9 5259 }
5260
5261 free(rd->d.qstring);
5262 rd->d.qstring = strdup(argv[2]);
5263 conf_call_reload_funcs();
5264 reply("OSMSG_SET_SUCCESS", argv[1], argv[2]);
5265 return 1;
5266}
5267
5268static MODCMD_FUNC(cmd_settime)
5269{
5270 const char *srv_name_mask = "*";
5271 time_t new_time = now;
5272
5273 if (argc > 1)
5274 srv_name_mask = argv[1];
5275 if (argc > 2)
5276 new_time = time(NULL);
5277 irc_settime(srv_name_mask, new_time);
5278 reply("OSMSG_SETTIME_SUCCESS", srv_name_mask);
5279 return 1;
5280}
5281
5282static discrim_t
258d1427 5283opserv_discrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[], int allow_channel)
d76ed9a9 5284{
5285 unsigned int i, j;
5286 discrim_t discrim;
5287
5288 discrim = calloc(1, sizeof(*discrim));
5289 discrim->limit = 250;
5290 discrim->max_level = ~0;
5291 discrim->max_ts = INT_MAX;
5292 discrim->domain_depth = 2;
5293 discrim->max_channels = INT_MAX;
5294 discrim->authed = -1;
5295 discrim->info_space = -1;
63665495 5296 discrim->intra_dcmp = 0;
5297 discrim->intra_scmp = 0;
1c5f6697 5298 discrim->use_regex = 0;
5299 discrim->silent = 0;
d76ed9a9 5300
5301 for (i=0; i<argc; i++) {
5302 if (irccasecmp(argv[i], "log") == 0) {
5303 discrim->option_log = 1;
5304 continue;
5305 }
5306 /* Assume all other criteria require arguments. */
5307 if (i == argc - 1) {
258d1427 5308 send_message(user, bot, "MSG_MISSING_PARAMS", argv[i]);
d76ed9a9 5309 goto fail;
5310 }
63665495 5311 if (argv[i+1][0] == '&') {
5312 /* Looking for intra-userNode matches */
5313 char *tmp = &(argv[i+1][1]);
5314 if (strcasecmp(tmp, argv[i]) != 0) { /* Don't allow "nick &nick" etc */
5315 if (!strcasecmp(tmp, "nick"))
5316 discrim->intra_dcmp = 1;
5317 else if (!strcasecmp(tmp, "ident"))
5318 discrim->intra_dcmp = 2;
5319 else if (!strcasecmp(tmp, "info"))
5320 discrim->intra_dcmp = 3;
5321 }
5322 }
258d1427 5323 if (irccasecmp(argv[i], "mask") == 0) {
5324 if (!is_ircmask(argv[++i])) {
5325 send_message(user, bot, "OSMSG_INVALID_IRCMASK", argv[i]);
5326 goto fail;
5327 }
5328 if (!split_ircmask(argv[i],
d76ed9a9 5329 &discrim->mask_nick,
5330 &discrim->mask_ident,
5331 &discrim->mask_host)) {
258d1427 5332 send_message(user, bot, "OSMSG_INVALID_IRCMASK", argv[i]);
5333 goto fail;
5334 }
5335 } else if (irccasecmp(argv[i], "nick") == 0) {
63665495 5336 i++;
5337 if (discrim->intra_dcmp > 0)
5338 discrim->intra_scmp = 1;
5339 else
5340 discrim->mask_nick = argv[i];
258d1427 5341 } else if (irccasecmp(argv[i], "ident") == 0) {
63665495 5342 i++;
5343 if (discrim->intra_dcmp > 0)
5344 discrim->intra_scmp = 2;
5345 else
5346 discrim->mask_ident = argv[i];
258d1427 5347 } else if (irccasecmp(argv[i], "host") == 0) {
5348 discrim->mask_host = argv[++i];
5349 } else if (irccasecmp(argv[i], "info") == 0) {
63665495 5350 i++;
5351 if (discrim->intra_dcmp > 0)
5352 discrim->intra_scmp = 3;
5353 else
5354 discrim->mask_info = argv[i];
0e08a8e0 5355 } else if (irccasecmp(argv[i], "version") == 0) {
5356 discrim->mask_version = argv[++i];
258d1427 5357 } else if (irccasecmp(argv[i], "server") == 0) {
5358 discrim->server = argv[++i];
5359 } else if (irccasecmp(argv[i], "ip") == 0) {
2f61d1d7 5360 j = irc_pton(&discrim->ip_mask, &discrim->ip_mask_bits, argv[++i]);
5361 if (!j) {
c092fcad 5362 send_message(user, bot, "OSMSG_BAD_IP", argv[i]);
2f61d1d7 5363 goto fail;
5364 }
d76ed9a9 5365 } else if (irccasecmp(argv[i], "account") == 0) {
5366 if (discrim->authed == 0) {
258d1427 5367 send_message(user, bot, "OSMSG_ACCOUNTMASK_AUTHED");
d76ed9a9 5368 goto fail;
5369 }
5370 discrim->accountmask = argv[++i];
5371 discrim->authed = 1;
2c00fbc2 5372 } else if (irccasecmp(argv[i], "marked") == 0) {
5373 discrim->mask_mark = argv[++i];
c408f18a 5374 } else if (irccasecmp(argv[i], "chantarget") == 0) {
5375 if(!IsChannelName(argv[i+1])) {
5376 send_message(user, bot, "MSG_NOT_CHANNEL_NAME");
5377 goto fail;
5378 }
5379 discrim->chantarget = argv[++i];
5aa400d2 5380 } else if (irccasecmp(argv[i], "checkrestrictions") == 0) {
5381 i++;
5382 if (true_string(argv[i])) {
5383 discrim->checkrestrictions = 1;
5384 } else if (false_string(argv[i])) {
5385 discrim->checkrestrictions = 0;
5386 } else {
5387 send_message(user, bot, "MSG_INVALID_BINARY", argv[i]);
5388 goto fail;
5389 }
d82cf2f0 5390 } else if (irccasecmp(argv[i], "mark") == 0) {
5391 if(!is_valid_mark(argv[i+1])) {
5392 send_message(user, bot, "OSMSG_MARK_INVALID");
5393 goto fail;
5394 }
5395 discrim->mark = argv[++i];
d76ed9a9 5396 } else if (irccasecmp(argv[i], "authed") == 0) {
5397 i++; /* true_string and false_string are macros! */
5398 if (true_string(argv[i])) {
5399 discrim->authed = 1;
5400 } else if (false_string(argv[i])) {
5401 if (discrim->accountmask) {
258d1427 5402 send_message(user, bot, "OSMSG_ACCOUNTMASK_AUTHED");
d76ed9a9 5403 goto fail;
5404 }
5405 discrim->authed = 0;
5406 } else {
258d1427 5407 send_message(user, bot, "MSG_INVALID_BINARY", argv[i]);
d76ed9a9 5408 goto fail;
5409 }
5410 } else if (irccasecmp(argv[i], "info_space") == 0) {
5411 /* XXX: A hack because you can't check explicitly for a space through
5412 * any other means */
5413 i++;
5414 if (true_string(argv[i])) {
5415 discrim->info_space = 1;
5416 } else if (false_string(argv[i])) {
5417 discrim->info_space = 0;
5418 } else {
258d1427 5419 send_message(user, bot, "MSG_INVALID_BINARY", argv[i]);
d76ed9a9 5420 goto fail;
5421 }
27eaa617 5422 } else if (irccasecmp(argv[i], "regex") == 0) {
5423 i++;
5424 if (true_string(argv[i])) {
5425 discrim->use_regex = 1;
5426 } else if (false_string(argv[i])) {
5427 discrim->use_regex = 0;
5428 } else {
c092fcad 5429 send_message(user, bot, "MSG_INVALID_BINARY", argv[i]);
27eaa617 5430 goto fail;
5431 }
1c5f6697 5432 } else if (irccasecmp(argv[i], "silent") == 0) {
5433 i++;
0c0adfe0 5434 if(user != opserv && !oper_has_access(user, opserv, opserv_conf.silent_level, 0)) {
1c5f6697 5435 goto fail;
5436 } else if (true_string(argv[i])) {
5437 discrim->silent = 1;
5438 } else if (false_string(argv[i])) {
5439 discrim->silent = 0;
5440 } else {
c092fcad 5441 send_message(user, bot, "MSG_INVALID_BINARY", argv[i]);
1c5f6697 5442 goto fail;
5443 }
d76ed9a9 5444 } else if (irccasecmp(argv[i], "duration") == 0) {
5445 discrim->duration = ParseInterval(argv[++i]);
1136f709 5446 } else if (irccasecmp(argv[i], "channel") == 0) {
5447 if(discrim->channel_count == DISCRIM_MAX_CHANS)
5448 {
5449 send_message(user, opserv, "OSMSG_TRACE_MAX_CHANNELS", DISCRIM_MAX_CHANS);
5450 goto fail;
5451 }
5452
5453 for (j=0, i++; ; j++) {
5454 switch (argv[i][j]) {
5455 case '#':
5456 goto find_channel;
5457 case '-':
5458 discrim->chan_no_modes[discrim->channel_count] |= MODE_CHANOP | MODE_VOICE;
5459 break;
5460 case '+':
5461 discrim->chan_req_modes[discrim->channel_count] |= MODE_VOICE;
5462 discrim->chan_no_modes[discrim->channel_count] |= MODE_CHANOP;
5463 discrim->chan_no_modes[discrim->channel_count] |= MODE_HALFOP;
5464 break;
5465 case '%':
5466 discrim->chan_req_modes[discrim->channel_count] |= MODE_HALFOP;
5467 discrim->chan_no_modes[discrim->channel_count] |= MODE_CHANOP;
5468 discrim->chan_no_modes[discrim->channel_count] |= MODE_VOICE;
5469 break;
5470 case '@':
5471 discrim->chan_req_modes[discrim->channel_count] |= MODE_CHANOP;
5472 break;
5473 case '\0':
5474 send_message(user, opserv, "MSG_NOT_CHANNEL_NAME");
5475 goto fail;
d76ed9a9 5476 }
1136f709 5477 }
5478 find_channel:
5479 discrim->chan_no_modes[discrim->channel_count] &= ~discrim->chan_req_modes[discrim->channel_count];
5480 if (!(discrim->channels[discrim->channel_count] = GetChannel(argv[i]+j))) {
5481 /* secretly "allow_channel" now means "if a channel name is
5482 * specified, require that it currently exist" */
5483 if (allow_channel) {
5484 send_message(user, opserv, "MSG_CHANNEL_UNKNOWN", argv[i]);
5485 goto fail;
5486 } else {
5487 discrim->channels[discrim->channel_count] = AddChannel(argv[i]+j, now, NULL, NULL, NULL);
258d1427 5488 }
1136f709 5489 }
5490 LockChannel(discrim->channels[discrim->channel_count]);
5491 discrim->channel_count++;
5492 } else if (irccasecmp(argv[i], "numchannels") == 0) {
5493 discrim->min_channels = discrim->max_channels = strtoul(argv[++i], NULL, 10);
5494 } else if (irccasecmp(argv[i], "limit") == 0) {
5495 discrim->limit = strtoul(argv[++i], NULL, 10);
d76ed9a9 5496 } else if (irccasecmp(argv[i], "reason") == 0) {
5497 discrim->reason = strdup(unsplit_string(argv+i+1, argc-i-1, NULL));
5498 i = argc;
1136f709 5499 } else if (irccasecmp(argv[i], "notice_target") == 0 || irccasecmp(argv[i], "target") == 0) {
5500 if (!IsChannelName(argv[i + 1])) {
5501 send_message(user, opserv, "MSG_NOT_CHANNEL_NAME");
5502 goto fail;
5503 }
5504 discrim->notice_target = argv[++i];
d76ed9a9 5505 } else if (irccasecmp(argv[i], "last") == 0) {
5506 discrim->min_ts = now - ParseInterval(argv[++i]);
5507 } else if ((irccasecmp(argv[i], "linked") == 0)
5508 || (irccasecmp(argv[i], "nickage") == 0)) {
5509 const char *cmp = argv[++i];
5510 if (cmp[0] == '<') {
5511 if (cmp[1] == '=') {
5512 discrim->min_ts = now - ParseInterval(cmp+2);
5513 } else {
5514 discrim->min_ts = now - (ParseInterval(cmp+1) - 1);
5515 }
5516 } else if (cmp[0] == '>') {
5517 if (cmp[1] == '=') {
5518 discrim->max_ts = now - ParseInterval(cmp+2);
5519 } else {
5520 discrim->max_ts = now - (ParseInterval(cmp+1) - 1);
5521 }
5522 } else {
1136f709 5523 discrim->min_ts = now - ParseInterval(cmp);
d76ed9a9 5524 }
5525 } else if (irccasecmp(argv[i], "access") == 0) {
5526 const char *cmp = argv[++i];
5527 if (cmp[0] == '<') {
5528 if (discrim->min_level == 0) discrim->min_level = 1;
5529 if (cmp[1] == '=') {
5530 discrim->max_level = strtoul(cmp+2, NULL, 0);
5531 } else {
5532 discrim->max_level = strtoul(cmp+1, NULL, 0) - 1;
5533 }
5534 } else if (cmp[0] == '=') {
5535 discrim->min_level = discrim->max_level = strtoul(cmp+1, NULL, 0);
5536 } else if (cmp[0] == '>') {
5537 if (cmp[1] == '=') {
5538 discrim->min_level = strtoul(cmp+2, NULL, 0);
5539 } else {
5540 discrim->min_level = strtoul(cmp+1, NULL, 0) + 1;
5541 }
5542 } else {
1136f709 5543 discrim->min_level = strtoul(cmp, NULL, 0);
5544 }
5545 } else if (irccasecmp(argv[i], "abuse") == 0) {
5546 const char *abuse_what = argv[++i];
5547 if (irccasecmp(abuse_what, "opers") == 0) {
5548 discrim->match_opers = 1;
5549 } else if (irccasecmp(abuse_what, "trusted") == 0) {
5550 discrim->match_trusted = 1;
d76ed9a9 5551 }
d76ed9a9 5552 } else if (irccasecmp(argv[i], "depth") == 0) {
5553 discrim->domain_depth = strtoul(argv[++i], NULL, 0);
5554 } else if (irccasecmp(argv[i], "clones") == 0) {
5555 discrim->min_clones = strtoul(argv[++i], NULL, 0);
cf8bedff 5556 } else if (irccasecmp(argv[i], "modes") == 0) {
5557 discrim->modes = argv[++i];
d76ed9a9 5558 } else {
258d1427 5559 send_message(user, bot, "MSG_INVALID_CRITERIA", argv[i]);
d76ed9a9 5560 goto fail;
5561 }
5562 }
5563
5564 if (discrim->mask_nick && !strcmp(discrim->mask_nick, "*")) {
258d1427 5565 discrim->mask_nick = 0;
d76ed9a9 5566 }
5567 if (discrim->mask_ident && !strcmp(discrim->mask_ident, "*")) {
5568 discrim->mask_ident = 0;
5569 }
5570 if (discrim->mask_info && !strcmp(discrim->mask_info, "*")) {
258d1427 5571 discrim->mask_info = 0;
d76ed9a9 5572 }
0e08a8e0 5573 if (discrim->mask_version && !strcmp(discrim->mask_version, "*")) {
5574 discrim->mask_version = 0;
5575 }
d76ed9a9 5576 if (discrim->mask_host && !discrim->mask_host[strspn(discrim->mask_host, "*.")]) {
5577 discrim->mask_host = 0;
5578 }
27eaa617 5579
5580 if(discrim->use_regex)
5581 {
5582 if(discrim->mask_nick)
5583 {
928e5a35 5584 int err = regcomp(&discrim->regex_nick, discrim->mask_nick, REG_EXTENDED|REG_NOSUB);
27eaa617 5585 discrim->has_regex_nick = !err;
5586 if(err)
5587 {
5588 char buff[256];
5589 buff[regerror(err, &discrim->regex_nick, buff, sizeof(buff))] = 0;
5590
c092fcad 5591 send_message(user, bot, "OSMSG_INVALID_REGEX", discrim->mask_nick, buff, err);
27eaa617 5592 goto regfail;
5593 }
5594 }
5595
5596 if(discrim->mask_ident)
5597 {
928e5a35 5598 int err = regcomp(&discrim->regex_ident, discrim->mask_ident, REG_EXTENDED|REG_NOSUB);
27eaa617 5599 discrim->has_regex_ident = !err;
5600 if(err)
5601 {
5602 char buff[256];
5603 buff[regerror(err, &discrim->regex_ident, buff, sizeof(buff))] = 0;
5604
c092fcad 5605 send_message(user, bot, "OSMSG_INVALID_REGEX", discrim->mask_ident, buff, err);
27eaa617 5606 goto regfail;
5607 }
5608 }
5609
5610 if(discrim->mask_host)
5611 {
928e5a35 5612 int err = regcomp(&discrim->regex_host, discrim->mask_host, REG_EXTENDED|REG_NOSUB);
27eaa617 5613 discrim->has_regex_host = !err;
5614 if(err)
5615 {
5616 char buff[256];
5617 buff[regerror(err, &discrim->regex_host, buff, sizeof(buff))] = 0;
5618
c092fcad 5619 send_message(user, bot, "OSMSG_INVALID_REGEX", discrim->mask_host, buff, err);
27eaa617 5620 goto regfail;
5621 }
5622 }
5623
5624 if(discrim->mask_info)
5625 {
928e5a35 5626 int err = regcomp(&discrim->regex_info, discrim->mask_info, REG_EXTENDED|REG_NOSUB);
27eaa617 5627 discrim->has_regex_info = !err;
5628 if(err)
5629 {
5630 char buff[256];
5631 buff[regerror(err, &discrim->regex_info, buff, sizeof(buff))] = 0;
5632
c092fcad 5633 send_message(user, bot, "OSMSG_INVALID_REGEX", discrim->mask_info, buff, err);
27eaa617 5634 goto regfail;
5635 }
5636 }
0e08a8e0 5637
5638 if(discrim->mask_version)
5639 {
928e5a35 5640 int err = regcomp(&discrim->regex_version, discrim->mask_version, REG_EXTENDED|REG_NOSUB);
0e08a8e0 5641 discrim->has_regex_version = !err;
5642 if(err)
5643 {
5644 char buff[256];
5645 buff[regerror(err, &discrim->regex_version, buff, sizeof(buff))] = 0;
5646
5647 send_message(user, bot, "OSMSG_INVALID_REGEX", discrim->mask_version, buff, err);
5648 goto regfail;
5649 }
5650 }
27eaa617 5651 }
5652
d76ed9a9 5653 return discrim;
27eaa617 5654
d76ed9a9 5655 fail:
5656 free(discrim);
5657 return NULL;
27eaa617 5658
5659 regfail:
5660 if(discrim->has_regex_nick)
5661 regfree(&discrim->regex_nick);
5662 if(discrim->has_regex_ident)
5663 regfree(&discrim->regex_ident);
5664 if(discrim->has_regex_host)
5665 regfree(&discrim->regex_host);
5666 if(discrim->has_regex_info)
5667 regfree(&discrim->regex_info);
5668
5669 free(discrim);
5670 return NULL;
d76ed9a9 5671}
5672
5673static int
5674discrim_match(discrim_t discrim, struct userNode *user)
5675{
1136f709 5676 unsigned int level, i;
63665495 5677 char *scmp=NULL, *dcmp=NULL;
d76ed9a9 5678
5679 if ((user->timestamp < discrim->min_ts)
5680 || (user->timestamp > discrim->max_ts)
5681 || (user->channels.used < discrim->min_channels)
5682 || (user->channels.used > discrim->max_channels)
5683 || (discrim->authed == 0 && user->handle_info)
5684 || (discrim->authed == 1 && !user->handle_info)
5685 || (discrim->info_space == 0 && user->info[0] == ' ')
5686 || (discrim->info_space == 1 && user->info[0] != ' ')
d76ed9a9 5687 || (discrim->server && !match_ircglob(user->uplink->name, discrim->server))
2c00fbc2 5688 || (discrim->mask_mark && (!user->mark || !match_ircglob(user->mark, discrim->mask_mark)))
d76ed9a9 5689 || (discrim->accountmask && (!user->handle_info || !match_ircglob(user->handle_info->handle, discrim->accountmask)))
2f61d1d7 5690 || (discrim->ip_mask_bits && !irc_check_mask(&user->ip, &discrim->ip_mask, discrim->ip_mask_bits))
5691 )
5692 return 0;
5693
1136f709 5694 for(i = 0; i < discrim->channel_count; i++)
5695 if (!GetUserMode(discrim->channels[i], user))
5696 return 0;
27eaa617 5697
5698 if(discrim->use_regex)
5699 {
5700 if((discrim->has_regex_nick && regexec(&discrim->regex_nick, user->nick, 0, 0, 0))
5701 || (discrim->has_regex_ident && regexec(&discrim->regex_ident, user->ident, 0, 0, 0))
5702 || (discrim->has_regex_host && regexec(&discrim->regex_host, user->hostname, 0, 0, 0))
0e08a8e0 5703 || (discrim->has_regex_info && regexec(&discrim->regex_info, user->info, 0, 0, 0))
5704 || (discrim->has_regex_version && (!user->version_reply || regexec(&discrim->regex_version, user->version_reply, 0, 0, 0)))) {
27eaa617 5705 return 0;
5706 }
5707 }
5708 else
5709 {
5710 if ((discrim->mask_nick && !match_ircglob(user->nick, discrim->mask_nick))
5711 || (discrim->mask_ident && !match_ircglob(user->ident, discrim->mask_ident))
5712 || (discrim->mask_host && !match_ircglob(user->hostname, discrim->mask_host))
0e08a8e0 5713 || (discrim->mask_info && !match_ircglob(user->info, discrim->mask_info))
5714 || (discrim->mask_version && (!user->version_reply || !match_ircglob(user->version_reply, discrim->mask_version))) ) {
27eaa617 5715 return 0;
5716 }
5717 }
5718
63665495 5719 if ((discrim->intra_scmp > 0 && discrim->intra_dcmp > 0)) {
5720 switch(discrim->intra_scmp) {
5721 case 1: scmp=user->nick; break;
5722 case 2: scmp=user->ident; break;
5723 case 3:
5724 scmp=user->info;
5725 if (discrim->info_space == 1) scmp++;
5726 break;
5727 }
5728 switch(discrim->intra_dcmp) {
5729 case 1: dcmp=user->nick; break;
5730 case 2: dcmp=user->ident; break;
5731 case 3: /* When checking INFO, and info_space is enabled
5732 * ignore the first character in a search
5733 * XXX: Should we ignore ALL leading whitespace?
5734 * Also, what about ignoring ~ in ident?
5735 */
5736 dcmp=user->info;
5737 if (discrim->info_space == 1) dcmp++;
5738 break;
5739 }
5740 if (irccasecmp(scmp,dcmp))
5741 return 0;
5742 }
5743
cf8bedff 5744 if (discrim->modes) {
5745 unsigned int ii, matches = 0;
5746 for (ii = 0; ii < strlen(discrim->modes); ii++) {
5747 switch(discrim->modes[ii]) {
5748 case 'O':
5749 if(IsOper(user)) matches++;
5750 break;
5751 case 'o':
5752 if(IsOper(user)) matches++;
5753 break;
5754 case 'i':
5755 if(IsInvisible(user)) matches++;
5756 break;
5757 case 'w':
5758 if(IsWallOp(user)) matches++;
5759 break;
cf8bedff 5760 case 'd':
5761 if(IsDeaf(user)) matches++;
5762 break;
5763 case 'k':
5764 if(IsService(user)) matches++;
5765 break;
5766 case 'g':
5767 if(IsGlobal(user)) matches++;
5768 break;
5769 case 'h':
5770 if(IsSetHost(user)) matches++;
5771 break;
5772 case 'B':
5773 if(IsBotM(user)) matches++;
5774 break;
5775 case 'n':
5776 if(IsHideChans(user)) matches++;
5777 break;
5778 case 'I':
5779 if(IsHideIdle(user)) matches++;
5780 break;
5781 case 'X':
5782 if(IsXtraOp(user)) matches++;
5783 break;
5784 case 'x':
5785 if(IsHiddenHost(user)) matches++;
5786 break;
fc55e757
MB
5787 case 'a':
5788 if(IsAdmin(user)) matches++;
5789 break;
5790 case 'z':
5791 if(IsSSL(user)) matches++;
5792 break;
8d882965
MB
5793 case 'D':
5794 if(IsPrivDeaf(user)) matches++;
5795 break;
5796 case 'R':
5797 if(IsAccountOnly(user)) matches++;
5798 break;
5799 case 'W':
5800 if(IsWhoisNotice(user)) matches++;
5801 break;
5802 case 'H':
5803 if(IsHideOper(user)) matches++;
5804 break;
5805 case 'L':
5806 if(IsHideOper(user)) matches++;
5807 break;
5808 case 'q':
5809 if(IsCommonChansOnly(user)) matches++;
5810 break;
cf8bedff 5811 }
5812 }
5813 if (matches != strlen(discrim->modes)) return 0;
5814 }
5815
1136f709 5816 level = user->handle_info ? user->handle_info->opserv_level : 0;
5817 if ((level < discrim->min_level)
5818 || (level > discrim->max_level)) {
d76ed9a9 5819 return 0;
5820 }
d76ed9a9 5821 if (discrim->min_clones > 1) {
2f61d1d7 5822 struct opserv_hostinfo *ohi = dict_find(opserv_hostinfo_dict, irc_ntoa(&user->ip), NULL);
5823 if (!ohi || (ohi->clients.used < discrim->min_clones))
5824 return 0;
d76ed9a9 5825 }
5826 return 1;
5827}
5828
5829static unsigned int
5830opserv_discrim_search(discrim_t discrim, discrim_search_func dsf, void *data)
5831{
1136f709 5832 unsigned int nn, count, match;
d76ed9a9 5833 struct userList matched;
5834
5835 userList_init(&matched);
5836 /* Try most optimized search methods first */
1136f709 5837 if (discrim->channel_count)
5838 {
5839 for (nn=0; (nn < discrim->channels[0]->members.used)
5840 && (matched.used < discrim->limit);
5841 nn++) {
5842 struct modeNode *mn = discrim->channels[0]->members.list[nn];
5843
5844 if (((mn->modes & discrim->chan_req_modes[0]) != discrim->chan_req_modes[0])
5845 || ((mn->modes & discrim->chan_no_modes[0]) != 0)) {
d76ed9a9 5846 continue;
5847 }
1136f709 5848 if ((match = discrim_match(discrim, mn->user)))
5849 {
5850 unsigned int i;
5851
5852 for (i = 1; i < discrim->channel_count; i++) {
5853 struct modeNode *mn2 = GetUserMode(discrim->channels[i], mn->user);
5854
5855 if (((mn2->modes & discrim->chan_req_modes[i]) != discrim->chan_req_modes[i])
5856 || ((mn2->modes & discrim->chan_no_modes[i]) != 0)) {
5857 match = 0;
5858 break;
5859 }
5860 }
5861
5862 if (match)
5863 userList_append(&matched, mn->user);
d76ed9a9 5864 }
5865 }
2f61d1d7 5866 } else if (discrim->ip_mask_bits == 128) {
5867 struct opserv_hostinfo *ohi = dict_find(opserv_hostinfo_dict, irc_ntoa(&discrim->ip_mask), NULL);
d76ed9a9 5868 if (!ohi) {
5869 userList_clean(&matched);
5870 return 0;
5871 }
5872 for (nn=0; (nn<ohi->clients.used) && (matched.used < discrim->limit); nn++) {
5873 if (discrim_match(discrim, ohi->clients.list[nn])) {
5874 userList_append(&matched, ohi->clients.list[nn]);
5875 }
5876 }
5877 } else {
5878 dict_iterator_t it;
5879 for (it=dict_first(clients); it && (matched.used < discrim->limit); it=iter_next(it)) {
5880 if (discrim_match(discrim, iter_data(it))) {
5881 userList_append(&matched, iter_data(it));
5882 }
5883 }
5884 }
5885
5886 if (!matched.used) {
5887 userList_clean(&matched);
5888 return 0;
5889 }
5890
5891 if (discrim->option_log) {
5892 log_module(OS_LOG, LOG_INFO, "Logging matches for search:");
5893 }
5894 for (nn=0; nn<matched.used; nn++) {
5895 struct userNode *user = matched.list[nn];
5896 if (discrim->option_log) {
5897 log_module(OS_LOG, LOG_INFO, " %s!%s@%s", user->nick, user->ident, user->hostname);
5898 }
5899 if (dsf(user, data)) {
258d1427 5900 /* If a search function returns true, it ran into a
5901 problem. Stop going through the list. */
5902 break;
5903 }
d76ed9a9 5904 }
5905 if (discrim->option_log) {
5906 log_module(OS_LOG, LOG_INFO, "End of matching users.");
5907 }
5908 count = matched.used;
5909 userList_clean(&matched);
5910 return count;
5911}
5912
5913static int
5914trace_print_func(struct userNode *match, void *extra)
5915{
5916 struct discrim_and_source *das = extra;
5917 if (match->handle_info) {
258d1427 5918 send_message_type(4, das->source, das->destination, "%-15s\002 \002%10s\002@\002%s (%s)", match->nick, match->ident, match->hostname, match->handle_info->handle);
d76ed9a9 5919 } else {
258d1427 5920 send_message_type(4, das->source, das->destination, "%-15s\002 \002%10s\002@\002%s", match->nick, match->ident, match->hostname);
d76ed9a9 5921 }
5922 return 0;
5923}
5924
53b62ed9 5925static int
5926trace_privmsg_func(struct userNode *match, void *extra)
5927{
5928 struct discrim_and_source *das = extra;
5929 char *reason;
5930 if (das->discrim->reason) {
5931 reason = das->discrim->reason;
5932 irc_privmsg_user(opserv, match, reason);
5933 }
5934 return 0;
5935}
5936
5937static int
5938trace_notice_func(struct userNode *match, void *extra)
5939{
5940 struct discrim_and_source *das = extra;
5941 char *reason;
5942 if (das->discrim->reason) {
5943 reason = das->discrim->reason;
5944 irc_notice_user(opserv, match, reason);
5945 }
5946 return 0;
5947}
5948
d76ed9a9 5949static int
5950trace_count_func(UNUSED_ARG(struct userNode *match), UNUSED_ARG(void *extra))
5951{
5952 return 0;
5953}
5954
5955static int
1136f709 5956is_oper_victim(struct userNode *user, struct userNode *target, int match_opers, int check_ip)
d76ed9a9 5957{
1136f709 5958 unsigned char is_victim;
5959 unsigned int nn;
5960
5961 is_victim = !(IsService(target)
5962 || (!match_opers && IsOper(target))
5963 || (target->handle_info
5964 && target->handle_info->opserv_level > user->handle_info->opserv_level));
5965
5966 /* If we don't need an ip check or want to hit opers or the the "cheap" check already disqualified the target, we are done. */
5967 if (!check_ip || match_opers || !is_victim)
5968 return is_victim;
5969
5970 for(nn = 0; nn < curr_opers.used; nn++) {
5971 if(memcmp(&curr_opers.list[nn]->ip, &target->ip, sizeof(irc_in_addr_t)) == 0)
5972 return 0;
5973 }
5974
5975 return 1;
5976}
5977
5978static int
5979is_trust_victim(struct userNode *target, int match_trusted)
5980{
5981 return (match_trusted || !dict_find(opserv_trusted_hosts, irc_ntoa(&target->ip), NULL));
d76ed9a9 5982}
5983
5984static int
5985trace_gline_func(struct userNode *match, void *extra)
5986{
5987 struct discrim_and_source *das = extra;
5988
1136f709 5989
5990 if (is_oper_victim(das->source, match, das->discrim->match_opers, 1) && is_trust_victim(match, das->discrim->match_trusted)) {
1c5f6697 5991 opserv_block(match, das->source->handle_info->handle, das->discrim->reason, das->discrim->duration, das->discrim->silent);
d76ed9a9 5992 }
5993
5994 return 0;
5995}
5996
d914d1cb 5997static int
5998trace_shun_func(struct userNode *match, void *extra)
5999{
6000 struct discrim_and_source *das = extra;
6001
1136f709 6002 if (is_oper_victim(das->source, match, das->discrim->match_opers, 1) && is_trust_victim(match, das->discrim->match_trusted)) {
d914d1cb 6003 opserv_shun(match, das->source->handle_info->handle, das->discrim->reason, das->discrim->duration);
6004 }
6005
6006 return 0;
6007}
6008
d76ed9a9 6009static int
6010trace_kill_func(struct userNode *match, void *extra)
6011{
6012 struct discrim_and_source *das = extra;
6013
1136f709 6014 if (is_oper_victim(das->source, match, das->discrim->match_opers, 1)) {
258d1427 6015 char *reason;
d76ed9a9 6016 if (das->discrim->reason) {
6017 reason = das->discrim->reason;
6018 } else {
6019 reason = alloca(strlen(OSMSG_KILL_REQUESTED)+strlen(das->source->nick)+1);
6020 sprintf(reason, OSMSG_KILL_REQUESTED, das->source->nick);
6021 }
6022 DelUser(match, opserv, 1, reason);
6023 }
6024
6025 return 0;
6026}
6027
d82cf2f0 6028static int
6029trace_mark_func(struct userNode *match, void *extra)
6030{
6031 struct discrim_and_source *das = extra;
6032 char *mark = das->discrim->mark;
6033
6034 if(!mark)
6035 return 1;
6036 irc_mark(match, mark);
6037 return 0;
6038}
6039
0e08a8e0 6040static int
6041trace_svsjoin_func(struct userNode *match, void *extra)
6042{
6043 struct discrim_and_source *das = extra;
6044
6045 char *channame = das->discrim->chantarget;
5aa400d2 6046 int checkrestrictions = das->discrim->checkrestrictions;
0e08a8e0 6047 struct chanNode *channel;
6048
a62ba70c 6049 if(!channame || !IsChannelName(channame)) {
0e08a8e0 6050 //reply("MSG_NOT_CHANNEL_NAME");
6051 return 1;
6052 }
6053
6054 if (!(channel = GetChannel(channame))) {
6055 channel = AddChannel(channame, now, NULL, NULL, NULL);
6056 }
5aa400d2 6057
6058 if (checkrestrictions) {
27fa6acf 6059 if (trace_check_bans(match, channel) == 1) {
6060 return 1; /* found on lamer list */
5aa400d2 6061 }
6062
6063 if (channel->modes & MODE_INVITEONLY) {
27fa6acf 6064 return 1; /* channel is invite only */
5aa400d2 6065 }
6066
1e993296 6067 if (channel->limit > 0) {
6068 if (channel->members.used >= channel->limit) {
6069 return 1; /* channel is invite on */
6070 }
5aa400d2 6071 }
6072
6073 if (*channel->key) {
27fa6acf 6074 return 1; /* channel is password protected */
5aa400d2 6075 }
6076 }
6077
0e08a8e0 6078 if (GetUserMode(channel, match)) {
6079// reply("OSMSG_ALREADY_THERE", channel->name);
6080 return 1;
6081 }
6082 irc_svsjoin(opserv, match, channel);
6083 // reply("OSMSG_SVSJOIN_SENT");
6084 return 0;
6085}
6086
39c1a4ef 6087static int
6088trace_svspart_func(struct userNode *match, void *extra)
6089{
6090 struct discrim_and_source *das = extra;
39c1a4ef 6091 char *channame = das->discrim->chantarget;
6092 struct chanNode *channel;
6093
6094 if(!channame || !IsChannelName(channame))
6095 return 1;
6096
6097 if (!(channel = GetChannel(channame)))
6098 return 1;
6099
6100 if (!GetUserMode(channel, match))
6101 return 1;
6102
6103 irc_svspart(opserv, match, channel);
6104 return 0;
6105}
6106
0e08a8e0 6107static int
6108trace_version_func(struct userNode *match, UNUSED_ARG(void *extra))
6109{
6110 irc_version_user(opserv, match);
6111 return 0;
6112}
6113
d76ed9a9 6114static int
6115is_gagged(char *mask)
6116{
6117 struct gag_entry *gag;
6118
6119 for (gag = gagList; gag; gag = gag->next) {
6120 if (match_ircglobs(gag->mask, mask)) return 1;
6121 }
6122 return 0;
6123}
6124
6125static int
6126trace_gag_func(struct userNode *match, void *extra)
6127{
6128 struct discrim_and_source *das = extra;
6129
1136f709 6130 if (is_oper_victim(das->source, match, das->discrim->match_opers, 1) && is_trust_victim(match, das->discrim->match_trusted)) {
d76ed9a9 6131 char *reason, *mask;
6132 int masksize;
6133 if (das->discrim->reason) {
6134 reason = das->discrim->reason;
6135 } else {
6136 reason = alloca(strlen(OSMSG_GAG_REQUESTED)+strlen(das->source->nick)+1);
6137 sprintf(reason, OSMSG_GAG_REQUESTED, das->source->nick);
6138 }
258d1427 6139 masksize = 5+strlen(match->hostname);
6140 mask = alloca(masksize);
d76ed9a9 6141 snprintf(mask, masksize, "*!*@%s", match->hostname);
258d1427 6142 if (!is_gagged(mask)) {
d76ed9a9 6143 gag_create(mask, das->source->handle_info->handle, reason,
6144 das->discrim->duration ? (now + das->discrim->duration) : 0);
6145 }
6146 }
6147
6148 return 0;
6149}
6150
6151static int
6152trace_domains_func(struct userNode *match, void *extra)
6153{
6154 struct discrim_and_source *das = extra;
2f61d1d7 6155 irc_in_addr_t ip;
d76ed9a9 6156 unsigned long *count;
6157 unsigned int depth;
6158 char *hostname;
2f61d1d7 6159 char ipmask[IRC_NTOP_MASK_MAX_SIZE];
d76ed9a9 6160
2f61d1d7 6161 if (irc_pton(&ip, NULL, match->hostname)) {
6162 if (irc_in_addr_is_ipv4(ip)) {
6163 unsigned long matchip = ntohl(ip.in6_32[3]);
6164 /* raw IP address.. use up to first three octets of IP */
6165 switch (das->discrim->domain_depth) {
6166 default:
6167 snprintf(ipmask, sizeof(ipmask), "%lu.%lu.%lu.*", (matchip>>24)&255, (matchip>>16)&255, (matchip>>8)&255);
6168 break;
6169 case 2:
6170 snprintf(ipmask, sizeof(ipmask), "%lu.%lu.*", (matchip>>24)&255, (matchip>>16)&255);
6171 break;
6172 case 1:
6173 snprintf(ipmask, sizeof(ipmask), "%lu.*", (matchip>>24)&255);
6174 break;
6175 }
6176 } else if (irc_in_addr_is_ipv6(ip)) {
6177 switch (das->discrim->domain_depth) {
6178 case 1: depth = 16; goto ipv6_pfx;
6179 case 2: depth = 24; goto ipv6_pfx;
6180 case 3: depth = 32; goto ipv6_pfx;
6181 default: depth = das->discrim->domain_depth;
6182 ipv6_pfx:
6183 irc_ntop_mask(ipmask, sizeof(ipmask), &ip, depth);
6184 }
6185 } else safestrncpy(ipmask, match->hostname, sizeof(ipmask));
6186 ipmask[sizeof(ipmask) - 1] = '\0';
d76ed9a9 6187 hostname = ipmask;
6188 } else {
6189 hostname = match->hostname + strlen(match->hostname);
2f61d1d7 6190 for (depth=das->discrim->domain_depth;
d76ed9a9 6191 depth && (hostname > match->hostname);
6192 depth--) {
6193 hostname--;
6194 while ((hostname > match->hostname) && (*hostname != '.')) hostname--;
6195 }
6196 if (*hostname == '.') hostname++; /* advance past last dot we saw */
6197 }
6198 if (!(count = dict_find(das->dict, hostname, NULL))) {
6199 count = calloc(1, sizeof(*count));
6200 dict_insert(das->dict, strdup(hostname), count);
6201 }
6202 (*count)++;
6203 return 0;
6204}
6205
6206static int
6207opserv_show_hostinfo(const char *key, void *data, void *extra)
6208{
6209 unsigned long *count = data;
6210 struct discrim_and_source *das = extra;
6211
258d1427 6212 send_message_type(4, das->source, das->destination, "%s %lu", key, *count);
d76ed9a9 6213 return !--das->disp_limit;
6214}
6215
6216static MODCMD_FUNC(cmd_trace)
6217{
6218 struct discrim_and_source das;
6219 discrim_search_func action;
1136f709 6220 unsigned int matches, i;
d76ed9a9 6221 struct svccmd *subcmd;
6222 char buf[MAXLEN];
a62ba70c 6223 int ret = 1;
d76ed9a9 6224
6225 sprintf(buf, "trace %s", argv[1]);
258d1427 6226 if (!(subcmd = dict_find(opserv_service->commands, buf, NULL))) {
6227 reply("OSMSG_BAD_ACTION", argv[1]);
d76ed9a9 6228 return 0;
6229 }
258d1427 6230 if (!svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY))
d76ed9a9 6231 return 0;
6232 if (!irccasecmp(argv[1], "print"))
6233 action = trace_print_func;
6234 else if (!irccasecmp(argv[1], "count"))
6235 action = trace_count_func;
53b62ed9 6236 else if (!irccasecmp(argv[1], "privmsg"))
6237 action = trace_privmsg_func;
6238 else if (!irccasecmp(argv[1], "notice"))
6239 action = trace_notice_func;
d76ed9a9 6240 else if (!irccasecmp(argv[1], "domains"))
6241 action = trace_domains_func;
6242 else if (!irccasecmp(argv[1], "gline"))
6243 action = trace_gline_func;
d914d1cb 6244 else if (!irccasecmp(argv[1], "shun"))
6245 action = trace_shun_func;
d76ed9a9 6246 else if (!irccasecmp(argv[1], "kill"))
6247 action = trace_kill_func;
6248 else if (!irccasecmp(argv[1], "gag"))
6249 action = trace_gag_func;
0e08a8e0 6250 else if (!irccasecmp(argv[1], "svsjoin"))
6251 action = trace_svsjoin_func;
39c1a4ef 6252 else if (!irccasecmp(argv[1], "svspart"))
6253 action = trace_svspart_func;
0e08a8e0 6254 else if (!irccasecmp(argv[1], "version"))
6255 action = trace_version_func;
d82cf2f0 6256 else if (!irccasecmp(argv[1], "mark"))
6257 action = trace_mark_func;
d76ed9a9 6258 else {
258d1427 6259 reply("OSMSG_BAD_ACTION", argv[1]);
6260 return 0;
d76ed9a9 6261 }
6262
6263 if (user->handle_info->opserv_level < subcmd->min_opserv_level) {
6264 reply("OSMSG_LEVEL_TOO_LOW");
6265 return 0;
6266 }
6267
6268 das.dict = NULL;
6269 das.source = user;
258d1427 6270 das.destination = cmd->parent->bot;
6271 das.discrim = opserv_discrim_create(user, cmd->parent->bot, argc-2, argv+2, 1);
d76ed9a9 6272 if (!das.discrim)
6273 return 0;
6274
6275 if (action == trace_print_func)
de9510bc 6276 {
8e11460f 6277 reply("OSMSG_USER_SEARCH_RESULTS");
de9510bc 6278 reply("OSMSG_USER_SEARCH_BAR");
6279 reply("OSMSG_USER_SEARCH_HEADER");
6280 reply("OSMSG_USER_SEARCH_BAR");
6281 }
d76ed9a9 6282 else if (action == trace_count_func)
258d1427 6283 das.discrim->limit = INT_MAX;
d76ed9a9 6284 else if ((action == trace_gline_func) && !das.discrim->duration)
6285 das.discrim->duration = opserv_conf.block_gline_duration;
d914d1cb 6286 else if ((action == trace_shun_func) && !das.discrim->duration)
6287 das.discrim->duration = opserv_conf.block_shun_duration;
d76ed9a9 6288 else if (action == trace_domains_func) {
6289 das.dict = dict_new();
6290 dict_set_free_data(das.dict, free);
6291 dict_set_free_keys(das.dict, free);
6292 das.disp_limit = das.discrim->limit;
6293 das.discrim->limit = INT_MAX;
6294 }
d76ed9a9 6295
a62ba70c 6296 if (action == trace_svsjoin_func && !das.discrim->chantarget) {
6297 reply("OSMSG_SVSJOIN_NO_TARGET");
6298 ret = 0;
6299 }
39c1a4ef 6300 else if (action == trace_svspart_func && !das.discrim->chantarget) {
6301 reply("OSMSG_SVSPART_NO_TARGET");
6302 ret = 0;
6303 }
d82cf2f0 6304 else if (action == trace_mark_func && !das.discrim->mark) {
6305 reply("OSMSG_MARK_NO_MARK");
6306 ret = 0;
6307 }
a62ba70c 6308 else {
6309 matches = opserv_discrim_search(das.discrim, action, &das);
d76ed9a9 6310
a62ba70c 6311 if (action == trace_domains_func)
6312 dict_foreach(das.dict, opserv_show_hostinfo, &das);
6313
6314 if (matches)
6315 {
6316 if(action == trace_print_func)
6317 reply("OSMSG_USER_SEARCH_COUNT_BAR", matches);
6318 else
6319 reply("OSMSG_USER_SEARCH_COUNT", matches);
6320 }
8e11460f 6321 else
a62ba70c 6322 reply("MSG_NO_MATCHES");
8e11460f 6323 }
d76ed9a9 6324
1136f709 6325 for (i = 0; i < das.discrim->channel_count; i++)
6326 UnlockChannel(das.discrim->channels[i]);
d76ed9a9 6327 free(das.discrim->reason);
27eaa617 6328
6329 if(das.discrim->has_regex_nick)
6330 regfree(&das.discrim->regex_nick);
6331 if(das.discrim->has_regex_ident)
6332 regfree(&das.discrim->regex_ident);
6333 if(das.discrim->has_regex_host)
6334 regfree(&das.discrim->regex_host);
6335 if(das.discrim->has_regex_info)
6336 regfree(&das.discrim->regex_info);
0e08a8e0 6337 if(das.discrim->has_regex_version)
6338 regfree(&das.discrim->regex_version);
27eaa617 6339
d76ed9a9 6340 free(das.discrim);
6341 dict_delete(das.dict);
a62ba70c 6342 return ret;
d76ed9a9 6343}
6344
258d1427 6345typedef void (*cdiscrim_search_func)(struct chanNode *match, void *data, struct userNode *bot);
d76ed9a9 6346
6347typedef struct channel_discrim {
6348 char *name, *topic;
6349
6350 unsigned int min_users, max_users;
6351 time_t min_ts, max_ts;
6352 unsigned int limit;
6353} *cdiscrim_t;
6354
258d1427 6355static cdiscrim_t opserv_cdiscrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[]);
6356static unsigned int opserv_cdiscrim_search(cdiscrim_t discrim, cdiscrim_search_func dsf, void *data, struct userNode *bot);
d76ed9a9 6357
6358static time_t
6359smart_parse_time(const char *str) {
6360 /* If an interval-style string is given, treat as time before now.
6361 * If it's all digits, treat directly as a Unix timestamp. */
6362 return str[strspn(str, "0123456789")] ? (time_t)(now - ParseInterval(str)) : (time_t)atoi(str);
6363}
6364
6365static cdiscrim_t
258d1427 6366opserv_cdiscrim_create(struct userNode *user, struct userNode *bot, unsigned int argc, char *argv[])
d76ed9a9 6367{
6368 cdiscrim_t discrim;
6369 unsigned int i;
6370
6371 discrim = calloc(1, sizeof(*discrim));
6372 discrim->limit = 25;
1136f709 6373 discrim->max_users = ~0;
6374 /* So, time_t is frequently signed. Fun. */
6375 discrim->max_ts = (1ul << (CHAR_BIT * sizeof(time_t) - 1)) - 1;
d76ed9a9 6376
6377 for (i = 0; i < argc; i++) {
258d1427 6378 /* Assume all criteria require arguments. */
6379 if (i == (argc - 1)) {
6380 send_message(user, bot, "MSG_MISSING_PARAMS", argv[i]);
6381 return NULL;
6382 }
6383
6384 if (!irccasecmp(argv[i], "name"))
6385 discrim->name = argv[++i];
6386 else if (!irccasecmp(argv[i], "topic"))
6387 discrim->topic = argv[++i];
6388 else if (!irccasecmp(argv[i], "users")) {
6389 const char *cmp = argv[++i];
d76ed9a9 6390 if (cmp[0] == '<') {
6391 if (cmp[1] == '=')
6392 discrim->max_users = strtoul(cmp+2, NULL, 0);
6393 else
6394 discrim->max_users = strtoul(cmp+1, NULL, 0) - 1;
6395 } else if (cmp[0] == '=') {
6396 discrim->min_users = discrim->max_users = strtoul(cmp+1, NULL, 0);
6397 } else if (cmp[0] == '>') {
6398 if (cmp[1] == '=')
6399 discrim->min_users = strtoul(cmp+2, NULL, 0);
6400 else
6401 discrim->min_users = strtoul(cmp+1, NULL, 0) + 1;
6402 } else {
1136f709 6403 discrim->min_users = strtoul(cmp, NULL, 0);
d76ed9a9 6404 }
258d1427 6405 } else if (!irccasecmp(argv[i], "timestamp")) {
6406 const char *cmp = argv[++i];
d76ed9a9 6407 if (cmp[0] == '<') {
6408 if (cmp[1] == '=')
6409 discrim->max_ts = smart_parse_time(cmp+2);
6410 else
6411 discrim->max_ts = smart_parse_time(cmp+1)-1;
6412 } else if (cmp[0] == '=') {
6413 discrim->min_ts = discrim->max_ts = smart_parse_time(cmp+1);
6414 } else if (cmp[0] == '>') {
6415 if (cmp[1] == '=')
6416 discrim->min_ts = smart_parse_time(cmp+2);
6417 else
6418 discrim->min_ts = smart_parse_time(cmp+1)+1;
6419 } else {
6420 discrim->min_ts = smart_parse_time(cmp);
6421 }
258d1427 6422 } else if (!irccasecmp(argv[i], "limit")) {
6423 discrim->limit = strtoul(argv[++i], NULL, 10);
6424 } else {
6425 send_message(user, bot, "MSG_INVALID_CRITERIA", argv[i]);
6426 goto fail;
6427 }
d76ed9a9 6428 }
6429
6430 if (discrim->name && !strcmp(discrim->name, "*"))
258d1427 6431 discrim->name = 0;
d76ed9a9 6432 if (discrim->topic && !strcmp(discrim->topic, "*"))
258d1427 6433 discrim->topic = 0;
d76ed9a9 6434
6435 return discrim;
6436 fail:
6437 free(discrim);
6438 return NULL;
6439}
6440
6441static int
6442cdiscrim_match(cdiscrim_t discrim, struct chanNode *chan)
6443{
6444 if ((discrim->name && !match_ircglob(chan->name, discrim->name)) ||
6445 (discrim->topic && !match_ircglob(chan->topic, discrim->topic)) ||
1136f709 6446 (chan->members.used < discrim->min_users) ||
6447 (chan->members.used > discrim->max_users) ||
6448 (chan->timestamp < discrim->min_ts) ||
6449 (chan->timestamp > discrim->max_ts)) {
258d1427 6450 return 0;
d76ed9a9 6451 }
6452 return 1;
6453}
6454
258d1427 6455static unsigned int opserv_cdiscrim_search(cdiscrim_t discrim, cdiscrim_search_func dsf, void *data, struct userNode *bot)
d76ed9a9 6456{
6457 unsigned int count = 0;
6458 dict_iterator_t it, next;
6459
6460 for (it = dict_first(channels); it && count < discrim->limit ; it = next) {
258d1427 6461 struct chanNode *chan = iter_data(it);
d76ed9a9 6462
258d1427 6463 /* Hold on to the next channel in case we decide to
6464 add actions that destructively modify the channel. */
6465 next = iter_next(it);
6466 if ((chan->members.used > 0) && cdiscrim_match(discrim, chan)) {
6467 dsf(chan, data, bot);
6468 count++;
6469 }
d76ed9a9 6470 }
6471
6472 return count;
6473}
6474
258d1427 6475void channel_count(UNUSED_ARG(struct chanNode *channel), UNUSED_ARG(void *data), UNUSED_ARG(struct userNode *bot))
d76ed9a9 6476{
6477}
6478
258d1427 6479void channel_print(struct chanNode *channel, void *data, struct userNode *bot)
d76ed9a9 6480{
6481 char modes[MAXLEN];
6482 irc_make_chanmode(channel, modes);
258d1427 6483 send_message(data, bot, "OSMSG_CSEARCH_CHANNEL_INFO", channel->name, channel->members.used, modes, channel->topic);
d76ed9a9 6484}
6485
6486static MODCMD_FUNC(cmd_csearch)
6487{
6488 cdiscrim_t discrim;
6489 unsigned int matches;
6490 cdiscrim_search_func action;
6491 struct svccmd *subcmd;
6492 char buf[MAXLEN];
6493
6494 if (!irccasecmp(argv[1], "count"))
258d1427 6495 action = channel_count;
d76ed9a9 6496 else if (!irccasecmp(argv[1], "print"))
258d1427 6497 action = channel_print;
d76ed9a9 6498 else {
258d1427 6499 reply("OSMSG_BAD_ACTION", argv[1]);
6500 return 0;
d76ed9a9 6501 }
6502
6503 sprintf(buf, "%s %s", argv[0], argv[0]);
258d1427 6504 if ((subcmd = dict_find(opserv_service->commands, buf, NULL))
6505 && !svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)) {
d76ed9a9 6506 return 0;
6507 }
6508
258d1427 6509 discrim = opserv_cdiscrim_create(user, cmd->parent->bot, argc - 2, argv + 2);
d76ed9a9 6510 if (!discrim)
258d1427 6511 return 0;
d76ed9a9 6512
6513 if (action == channel_print)
258d1427 6514 reply("OSMSG_CHANNEL_SEARCH_RESULTS");
d76ed9a9 6515 else if (action == channel_count)
258d1427 6516 discrim->limit = INT_MAX;
d76ed9a9 6517
258d1427 6518 matches = opserv_cdiscrim_search(discrim, action, user, cmd->parent->bot);
d76ed9a9 6519
6520 if (matches)
258d1427 6521 reply("MSG_MATCH_COUNT", matches);
d76ed9a9 6522 else
258d1427 6523 reply("MSG_NO_MATCHES");
d76ed9a9 6524
6525 free(discrim);
6526 return 1;
6527}
6528
6529static MODCMD_FUNC(cmd_gsync)
6530{
6531 struct server *src;
6532 if (argc > 1) {
6533 src = GetServerH(argv[1]);
6534 if (!src) {
6535 reply("MSG_SERVER_UNKNOWN", argv[1]);
6536 return 0;
6537 }
6538 } else {
6539 src = self->uplink;
6540 }
6541 irc_stats(cmd->parent->bot, src, 'G');
6542 reply("OSMSG_GSYNC_RUNNING", src->name);
6543 return 1;
6544}
6545
d914d1cb 6546static MODCMD_FUNC(cmd_ssync)
6547{
6548 struct server *src;
6549 if (argc > 1) {
6550 src = GetServerH(argv[1]);
6551 if (!src) {
6552 reply("MSG_SERVER_UNKNOWN", argv[1]);
6553 return 0;
6554 }
6555 } else {
6556 src = self->uplink;
6557 }
6558 irc_stats(cmd->parent->bot, src, 'S');
6559 reply("OSMSG_SSYNC_RUNNING", src->name);
6560 return 1;
6561}
6562
d76ed9a9 6563struct gline_extra {
6564 struct userNode *user;
6565 struct string_list *glines;
258d1427 6566 struct userNode *bot;
d76ed9a9 6567};
6568
6569static void
6570gtrace_print_func(struct gline *gline, void *extra)
6571{
6572 struct gline_extra *xtra = extra;
1136f709 6573 char issued[INTERVALLEN];
1136f709 6574 char expires[INTERVALLEN];
6575
6576 intervalString(issued, now - gline->issued, xtra->user->handle_info);
1136f709 6577 if (gline->expires)
6578 intervalString(expires, gline->expires - now, xtra->user->handle_info);
6579 else
6580 strcpy(expires, "never");
995043b4 6581 send_message(xtra->user, opserv, "OSMSG_GTRACE_FORMAT", gline->target, issued, gline->issuer, expires, gline->reason);
1136f709 6582}
6583
6584static MODCMD_FUNC(cmd_stats_glines) {
6585 if (argc < 2) {
6586 reply("OSMSG_GLINE_COUNT", gline_count());
6587 return 1;
6588 } else if (argc < 3) {
6589 struct gline_extra extra;
6590 struct gline *gl;
6591
6592 extra.user = user;
6593 gl = gline_find(argv[1]);
6594 if (!gl)
6595 reply("OSMSG_NO_GLINE", argv[1]);
6596 else
6597 gtrace_print_func(gl, &extra);
6598 return 1;
6599 } else return 0;
d76ed9a9 6600}
6601
6602static void
6603gtrace_count_func(UNUSED_ARG(struct gline *gline), UNUSED_ARG(void *extra))
6604{
6605}
6606
6607static void
6608gtrace_ungline_func(struct gline *gline, void *extra)
6609{
6610 struct gline_extra *xtra = extra;
6611 string_list_append(xtra->glines, strdup(gline->target));
6612}
6613
6614static MODCMD_FUNC(cmd_gtrace)
6615{
6616 struct gline_discrim *discrim;
6617 gline_search_func action;
6618 unsigned int matches, nn;
6619 struct gline_extra extra;
6620 struct svccmd *subcmd;
6621 char buf[MAXLEN];
6622
6623 if (!irccasecmp(argv[1], "print"))
6624 action = gtrace_print_func;
6625 else if (!irccasecmp(argv[1], "count"))
6626 action = gtrace_count_func;
6627 else if (!irccasecmp(argv[1], "ungline"))
6628 action = gtrace_ungline_func;
6629 else {
6630 reply("OSMSG_BAD_ACTION", argv[1]);
6631 return 0;
6632 }
6633 sprintf(buf, "%s %s", argv[0], argv[0]);
258d1427 6634 if ((subcmd = dict_find(opserv_service->commands, buf, NULL))
6635 && !svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)) {
d76ed9a9 6636 return 0;
6637 }
6638
6639 discrim = gline_discrim_create(user, cmd->parent->bot, argc-2, argv+2);
6640 if (!discrim)
6641 return 0;
6642
6643 if (action == gtrace_print_func)
6644 reply("OSMSG_GLINE_SEARCH_RESULTS");
6645 else if (action == gtrace_count_func)
6646 discrim->limit = INT_MAX;
6647
6648 extra.user = user;
6649 extra.glines = alloc_string_list(4);
258d1427 6650 extra.bot = cmd->parent->bot;
d76ed9a9 6651 matches = gline_discrim_search(discrim, action, &extra);
6652
6653 if (action == gtrace_ungline_func)
6654 for (nn=0; nn<extra.glines->used; nn++)
6655 gline_remove(extra.glines->list[nn], 1);
6656 free_string_list(extra.glines);
6657
6658 if (matches)
6659 reply("MSG_MATCH_COUNT", matches);
6660 else
6661 reply("MSG_NO_MATCHES");
6662 free(discrim->alt_target_mask);
6663 free(discrim);
6664 return 1;
6665}
6666
d914d1cb 6667struct shun_extra {
6668 struct userNode *user;
6669 struct string_list *shuns;
6670 struct userNode *bot;
6671};
6672
6673static void
6674strace_print_func(struct shun *shun, void *extra)
6675{
6676 struct shun_extra *xtra = extra;
1136f709 6677 char issued[INTERVALLEN];
1136f709 6678 char expires[INTERVALLEN];
6679
6680 intervalString(issued, now - shun->issued, xtra->user->handle_info);
1136f709 6681 if (shun->expires)
6682 intervalString(expires, shun->expires - now, xtra->user->handle_info);
6683 else
6684 strcpy(expires, "never");
995043b4 6685 send_message(xtra->user, opserv, "OSMSG_STRACE_FORMAT", shun->target, issued, shun->issuer, expires, shun->reason);
1136f709 6686}
6687
6688static MODCMD_FUNC(cmd_stats_shuns) {
6689 if (argc < 2) {
6690 reply("OSMSG_SHUN_COUNT", shun_count());
6691 return 1;
6692 } else if (argc < 3) {
6693 struct shun_extra extra;
6694 struct shun *gl;
6695
6696 extra.user = user;
6697 gl = shun_find(argv[1]);
6698 if (!gl)
6699 reply("OSMSG_NO_SHUN", argv[1]);
6700 else
6701 strace_print_func(gl, &extra);
6702 return 1;
6703 } else return 0;
d914d1cb 6704}
6705
6706static void
6707strace_count_func(UNUSED_ARG(struct shun *shun), UNUSED_ARG(void *extra))
6708{
6709}
6710
6711static void
6712strace_unshun_func(struct shun *shun, void *extra)
6713{
6714 struct shun_extra *xtra = extra;
6715 string_list_append(xtra->shuns, strdup(shun->target));
6716}
6717
6718static MODCMD_FUNC(cmd_strace)
6719{
6720 struct shun_discrim *discrim;
6721 shun_search_func action;
6722 unsigned int matches, nn;
6723 struct shun_extra extra;
6724 struct svccmd *subcmd;
6725 char buf[MAXLEN];
6726
6727 if (!irccasecmp(argv[1], "print"))
6728 action = strace_print_func;
6729 else if (!irccasecmp(argv[1], "count"))
6730 action = strace_count_func;
6731 else if (!irccasecmp(argv[1], "unshun"))
6732 action = strace_unshun_func;
6733 else {
6734 reply("OSMSG_BAD_ACTION", argv[1]);
6735 return 0;
6736 }
6737 sprintf(buf, "%s %s", argv[0], argv[0]);
6738 if ((subcmd = dict_find(opserv_service->commands, buf, NULL))
6739 && !svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)) {
6740 return 0;
6741 }
6742
6743 discrim = shun_discrim_create(user, cmd->parent->bot, argc-2, argv+2);
6744 if (!discrim)
6745 return 0;
6746
6747 if (action == strace_print_func)
6748 reply("OSMSG_SHUN_SEARCH_RESULTS");
6749 else if (action == strace_count_func)
6750 discrim->limit = INT_MAX;
6751
6752 extra.user = user;
6753 extra.shuns = alloc_string_list(4);
6754 extra.bot = cmd->parent->bot;
6755 matches = shun_discrim_search(discrim, action, &extra);
6756
6757 if (action == strace_unshun_func)
6758 for (nn=0; nn<extra.shuns->used; nn++)
6759 shun_remove(extra.shuns->list[nn], 1);
6760 free_string_list(extra.shuns);
6761
6762 if (matches)
6763 reply("MSG_MATCH_COUNT", matches);
6764 else
6765 reply("MSG_NO_MATCHES");
6766 free(discrim->alt_target_mask);
6767 free(discrim);
6768 return 1;
6769}
6770
d76ed9a9 6771static int
6772alert_check_user(const char *key, void *data, void *extra)
6773{
6774 struct opserv_user_alert *alert = data;
6775 struct userNode *user = extra;
6776
6777 if (!discrim_match(alert->discrim, user))
6778 return 0;
6779
6780 if ((alert->reaction != REACT_NOTICE)
6781 && IsOper(user)
6782 && !alert->discrim->match_opers) {
6783 return 0;
6784 }
6785
1136f709 6786 if ((alert->reaction != REACT_NOTICE)
6787 && !is_trust_victim(user, alert->discrim->match_trusted)) {
6788 return 0;
6789 }
6790
d76ed9a9 6791 /* The user matches the alert criteria, so trigger the reaction. */
6792 if (alert->discrim->option_log)
6793 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);
6794
083fb96b 6795 alert->last = now;
6796
d76ed9a9 6797 /* Return 1 to halt alert matching, such as when killing the user
6798 that triggered the alert. */
6799 switch (alert->reaction) {
6800 case REACT_KILL:
6801 DelUser(user, opserv, 1, alert->discrim->reason);
6802 return 1;
6803 case REACT_GLINE:
1c5f6697 6804 opserv_block(user, alert->owner, alert->discrim->reason, alert->discrim->duration, alert->discrim->silent);
d76ed9a9 6805 return 1;
d914d1cb 6806 case REACT_SHUN:
6807 opserv_shun(user, alert->owner, alert->discrim->reason, alert->discrim->duration);
6808 return 1;
c408f18a 6809 case REACT_SVSJOIN:
5aa400d2 6810 opserv_svsjoin(user, alert->owner, alert->discrim->reason, alert->discrim->chantarget, alert->discrim->checkrestrictions);
0e08a8e0 6811 break;
39c1a4ef 6812 case REACT_SVSPART:
6813 opserv_svspart(user, alert->owner, alert->discrim->reason, alert->discrim->chantarget);
6814 break;
0e08a8e0 6815 case REACT_VERSION:
6816 /* Don't auto-version a user who we already have a version on, because the version reply itself
6817 * re-triggers this check...
6818 * TODO: maybe safer if we didn't even check react_version type alerts for the 2nd check?
6819 * sort of like we only look at channel alerts on join. -Rubin
6820 */
6821 if(!user->version_reply)
6822 opserv_version(user);
6823 break;
d82cf2f0 6824 case REACT_MARK:
6825 opserv_mark(user, alert->owner, alert->discrim->reason, alert->discrim->mark);
6826 break;
d76ed9a9 6827 default:
6828 log_module(OS_LOG, LOG_ERROR, "Invalid reaction type %d for alert %s.", alert->reaction, key);
6829 /* fall through to REACT_NOTICE case */
6830 case REACT_NOTICE:
1136f709 6831 opserv_custom_alert(alert->discrim->notice_target, "Alert $b%s$b triggered by user $b%s$b!%s@%s (%s).", key, user->nick, user->ident, user->hostname, alert->discrim->reason);
d76ed9a9 6832 break;
ec311f39 6833 case REACT_TRACK:
6834#ifdef HAVE_TRACK
0f6fe38c 6835 opserv_alert("Alert $b%s$b triggered by user $b%s$b!%s@%s (%s) (Tracking).", key, user->nick, user->ident, user->hostname, alert->discrim->reason);
ec311f39 6836 add_track_user(user);
6837#endif
6838 break;
890866b0 6839 case REACT_NOTICEUSER:
6840 irc_notice_user(opserv, user, alert->discrim->reason);
6841 break;
6842 case REACT_MSGUSER:
6843 irc_privmsg_user(opserv, user, alert->discrim->reason);
6844 break;
d76ed9a9 6845 }
6846 return 0;
6847}
6848
b7594203 6849static void
81ac4787 6850opserv_alert_check_account(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle), UNUSED_ARG(void *extra))
b7594203 6851{
6852 dict_foreach(opserv_account_based_alerts, alert_check_user, user);
6853}
6854
d76ed9a9 6855static void
63189c10 6856opserv_alert_check_nick(struct userNode *user, UNUSED_ARG(const char *old_nick), UNUSED_ARG(void *extra))
d76ed9a9 6857{
6858 struct gag_entry *gag;
f1d4a7db 6859
da5d4243 6860 dict_foreach(opserv_nick_based_alerts, alert_check_user, user);
f1d4a7db 6861
d76ed9a9 6862 /* Gag them if appropriate (and only if). */
6863 user->modes &= ~FLAGS_GAGGED;
6864 for (gag = gagList; gag; gag = gag->next) {
277ad996 6865 if (user_matches_glob(user, gag->mask, MATCH_USENICK, 0)) {
d76ed9a9 6866 gag_helper_func(user, NULL);
6867 break;
6868 }
6869 }
6870}
6871
6872static void
81ac4787 6873opserv_staff_alert(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle), UNUSED_ARG(void *extra))
d76ed9a9 6874{
6875 const char *type;
6876
6877 if (!opserv_conf.staff_auth_channel
6878 || user->uplink->burst
6879 || !user->handle_info)
6880 return;
6881 else if (user->handle_info->opserv_level)
6882 type = "OPER";
6883 else if (IsNetworkHelper(user))
6884 type = "NETWORK HELPER";
6885 else if (IsSupportHelper(user))
6886 type = "SUPPORT HELPER";
6887 else
6888 return;
6889
2f61d1d7 6890 if (irc_in_addr_is_valid(user->ip))
d76ed9a9 6891 send_channel_notice(opserv_conf.staff_auth_channel, opserv, IDENT_FORMAT" authed to %s account %s", IDENT_DATA(user), type, user->handle_info->handle);
6892 else
6893 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);
6894}
6895
6896static MODCMD_FUNC(cmd_log)
6897{
6898 struct logSearch *discrim;
6899 unsigned int matches;
6900 struct logReport report;
6901
6902 discrim = log_discrim_create(cmd->parent->bot, user, argc, argv);
6903 if (!discrim)
6904 return 0;
6905
6906 reply("OSMSG_LOG_SEARCH_RESULTS");
6907 report.reporter = opserv;
6908 report.user = user;
6909 matches = log_entry_search(discrim, log_report_entry, &report);
6910
6911 if (matches)
258d1427 6912 reply("MSG_MATCH_COUNT", matches);
d76ed9a9 6913 else
258d1427 6914 reply("MSG_NO_MATCHES");
d76ed9a9 6915
6916 free(discrim);
6917 return 1;
6918}
6919
6920static int
6921gag_helper_func(struct userNode *match, UNUSED_ARG(void *extra))
6922{
6923 if (IsOper(match) || IsLocal(match))
6924 return 0;
6925 match->modes |= FLAGS_GAGGED;
6926 return 0;
6927}
6928
6929static MODCMD_FUNC(cmd_gag)
6930{
6931 struct gag_entry *gag;
6932 unsigned int gagged;
6933 unsigned long duration;
6934 char *reason;
6935
6936 reason = unsplit_string(argv + 3, argc - 3, NULL);
6937
6938 if (!is_ircmask(argv[1])) {
258d1427 6939 reply("OSMSG_INVALID_IRCMASK", argv[1]);
d76ed9a9 6940 return 0;
6941 }
6942
6943 for (gag = gagList; gag; gag = gag->next)
258d1427 6944 if (match_ircglobs(gag->mask, argv[1]))
d76ed9a9 6945 break;
6946
6947 if (gag) {
258d1427 6948 reply("OSMSG_REDUNDANT_GAG", argv[1]);
6949 return 0;
d76ed9a9 6950 }
6951
6952 duration = ParseInterval(argv[2]);
6953 gagged = gag_create(argv[1], user->handle_info->handle, reason, (duration?now+duration:0));
6954
6955 if (gagged)
258d1427 6956 reply("OSMSG_GAG_APPLIED", argv[1], gagged);
d76ed9a9 6957 else
258d1427 6958 reply("OSMSG_GAG_ADDED", argv[1]);
d76ed9a9 6959 return 1;
6960}
6961
6962static int
6963ungag_helper_func(struct userNode *match, UNUSED_ARG(void *extra))
6964{
6965 match->modes &= ~FLAGS_GAGGED;
6966 return 0;
6967}
6968
6969static MODCMD_FUNC(cmd_ungag)
6970{
6971 struct gag_entry *gag;
6972 unsigned int ungagged;
6973
6974 for (gag = gagList; gag; gag = gag->next)
258d1427 6975 if (!strcmp(gag->mask, argv[1]))
d76ed9a9 6976 break;
6977
6978 if (!gag) {
258d1427 6979 reply("OSMSG_GAG_NOT_FOUND", argv[1]);
6980 return 0;
d76ed9a9 6981 }
6982
6983 timeq_del(gag->expires, gag_expire, gag, 0);
6984 ungagged = gag_free(gag);
6985
6986 if (ungagged)
258d1427 6987 reply("OSMSG_UNGAG_APPLIED", argv[1], ungagged);
d76ed9a9 6988 else
258d1427 6989 reply("OSMSG_UNGAG_ADDED", argv[1]);
d76ed9a9 6990 return 1;
6991}
6992
6993static MODCMD_FUNC(cmd_addalert)
6994{
6995 opserv_alert_reaction reaction;
6996 struct svccmd *subcmd;
6997 const char *name;
6998 char buf[MAXLEN];
a947ef71 6999 int expire = 0;
d76ed9a9 7000
7001 name = argv[1];
7002 sprintf(buf, "addalert %s", argv[2]);
258d1427 7003 if (!(subcmd = dict_find(opserv_service->commands, buf, NULL))) {
7004 reply("OSMSG_UNKNOWN_REACTION", argv[2]);
7005 return 0;
d76ed9a9 7006 }
7007 if (!irccasecmp(argv[2], "notice"))
7008 reaction = REACT_NOTICE;
7009 else if (!irccasecmp(argv[2], "kill"))
7010 reaction = REACT_KILL;
7011 else if (!irccasecmp(argv[2], "gline"))
7012 reaction = REACT_GLINE;
ec311f39 7013 else if (!irccasecmp(argv[2], "track")) {
7014#ifndef HAVE_TRACK
c092fcad 7015 reply("OSMSG_TRACK_DISABLED");
ec311f39 7016 return 0;
7017#else
7018 reaction = REACT_TRACK;
7019#endif
7020 } else if (!irccasecmp(argv[2], "shun"))
d914d1cb 7021 reaction = REACT_SHUN;
c408f18a 7022 else if(!irccasecmp(argv[2], "svsjoin"))
7023 reaction = REACT_SVSJOIN;
39c1a4ef 7024 else if(!irccasecmp(argv[2], "svspart"))
7025 reaction = REACT_SVSPART;
0e08a8e0 7026 else if(!irccasecmp(argv[2], "version"))
7027 reaction = REACT_VERSION;
d82cf2f0 7028 else if(!irccasecmp(argv[2], "mark"))
7029 reaction = REACT_MARK;
890866b0 7030 else if(!irccasecmp(argv[2], "noticeuser"))
7031 reaction = REACT_NOTICEUSER;
7032 else if(!irccasecmp(argv[2], "msguser"))
7033 reaction = REACT_MSGUSER;
d76ed9a9 7034 else {
258d1427 7035 reply("OSMSG_UNKNOWN_REACTION", argv[2]);
7036 return 0;
d76ed9a9 7037 }
a947ef71 7038
7039 if (argc >= 4 && !irccasecmp(argv[3], "expire"))
7040 expire = now + ParseInterval(argv[4]);
7041
258d1427 7042 if (!svccmd_can_invoke(user, opserv_service->bot, subcmd, channel, SVCCMD_NOISY)
a947ef71 7043 || !opserv_add_user_alert(user, name, reaction, unsplit_string(argv + (expire ? 5 : 3), argc - (expire ? 5 : 3), NULL), 0, expire)) {
a62ba70c 7044 reply("OSMSG_ALERT_ADD_FAILED");
d76ed9a9 7045 return 0;
a62ba70c 7046 }
d76ed9a9 7047 reply("OSMSG_ADDED_ALERT", name);
7048 return 1;
7049}
7050
a947ef71 7051static int delete_alert(char const* name)
7052{
7053 dict_remove(opserv_nick_based_alerts, (char const*)name);
7054 dict_remove(opserv_channel_alerts, (char const*)name);
7055 dict_remove(opserv_account_based_alerts, (char const*)name);
7056 return dict_remove(opserv_user_alerts, (char const*)name);
7057}
7058
7059static void alert_expire(void* name)
7060{
7061 int present = 0;
7062 struct opserv_user_alert* alert = NULL;
7063
7064 alert = dict_find(opserv_user_alerts, (char const*)name, &present);
7065
7066 if (present && alert && alert->expire > 0 && alert->expire <= now)
7067 delete_alert(name);
7068}
7069
d76ed9a9 7070static MODCMD_FUNC(cmd_delalert)
7071{
7072 unsigned int i;
7073 for (i=1; i<argc; i++) {
a947ef71 7074 if (delete_alert(argv[i]))
258d1427 7075 reply("OSMSG_REMOVED_ALERT", argv[i]);
d76ed9a9 7076 else
258d1427 7077 reply("OSMSG_NO_SUCH_ALERT", argv[i]);
d76ed9a9 7078 }
7079 return 1;
7080}
7081
7082static void
7083opserv_conf_read(void)
7084{
7637f48f 7085 struct chanNode *chan;
7086 unsigned int i;
d76ed9a9 7087 struct record_data *rd;
7088 dict_t conf_node, child;
7089 const char *str, *str2;
7090 struct policer_params *pp;
7091 dict_iterator_t it;
7092
7093 rd = conf_get_node(OPSERV_CONF_NAME);
7094 if (!rd || rd->type != RECDB_OBJECT) {
258d1427 7095 log_module(OS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", OPSERV_CONF_NAME);
7096 return;
d76ed9a9 7097 }
7098 conf_node = rd->d.object;
7099 str = database_get_data(conf_node, KEY_DEBUG_CHANNEL, RECDB_QSTRING);
7100 if (opserv && str) {
7101 str2 = database_get_data(conf_node, KEY_DEBUG_CHANNEL_MODES, RECDB_QSTRING);
7102 if (!str2)
7103 str2 = "+tinms";
258d1427 7104 opserv_conf.debug_channel = AddChannel(str, now, str2, NULL, NULL);
d76ed9a9 7105 AddChannelUser(opserv, opserv_conf.debug_channel)->modes |= MODE_CHANOP;
7106 } else {
258d1427 7107 opserv_conf.debug_channel = NULL;
d76ed9a9 7108 }
7109 str = database_get_data(conf_node, KEY_ALERT_CHANNEL, RECDB_QSTRING);
7110 if (opserv && str) {
7111 str2 = database_get_data(conf_node, KEY_ALERT_CHANNEL_MODES, RECDB_QSTRING);
7112 if (!str2)
7113 str2 = "+tns";
258d1427 7114 opserv_conf.alert_channel = AddChannel(str, now, str2, NULL, NULL);
d76ed9a9 7115 AddChannelUser(opserv, opserv_conf.alert_channel)->modes |= MODE_CHANOP;
7116 } else {
258d1427 7117 opserv_conf.alert_channel = NULL;
d76ed9a9 7118 }
7119 str = database_get_data(conf_node, KEY_STAFF_AUTH_CHANNEL, RECDB_QSTRING);
7120 if (opserv && str) {
7121 str2 = database_get_data(conf_node, KEY_STAFF_AUTH_CHANNEL_MODES, RECDB_QSTRING);
7122 if (!str2)
7123 str2 = "+timns";
2aef5f4b 7124 opserv_conf.staff_auth_channel = AddChannel(str, now, str2, NULL, NULL);
d76ed9a9 7125 AddChannelUser(opserv, opserv_conf.staff_auth_channel)->modes |= MODE_CHANOP;
7126 } else {
7127 opserv_conf.staff_auth_channel = NULL;
7128 }
5a1daaab 7129
7130 str = database_get_data(conf_node, KEY_ADMIN_LEVEL, RECDB_QSTRING);
7131 opserv_conf.admin_level = str ? strtoul(str, NULL, 0): 800;
7132
1c5f6697 7133 str = database_get_data(conf_node, KEY_SILENT_LEVEL, RECDB_QSTRING);
7134 opserv_conf.silent_level = str ? strtoul(str, NULL, 0): 700;
7135
d76ed9a9 7136 str = database_get_data(conf_node, KEY_UNTRUSTED_MAX, RECDB_QSTRING);
7137 opserv_conf.untrusted_max = str ? strtoul(str, NULL, 0) : 5;
7138 str = database_get_data(conf_node, KEY_PURGE_LOCK_DELAY, RECDB_QSTRING);
7139 opserv_conf.purge_lock_delay = str ? strtoul(str, NULL, 0) : 60;
7140 str = database_get_data(conf_node, KEY_JOIN_FLOOD_MODERATE, RECDB_QSTRING);
7141 opserv_conf.join_flood_moderate = str ? strtoul(str, NULL, 0) : 1;
7142 str = database_get_data(conf_node, KEY_JOIN_FLOOD_MODERATE_THRESH, RECDB_QSTRING);
7143 opserv_conf.join_flood_moderate_threshold = str ? strtoul(str, NULL, 0) : 50;
7144 str = database_get_data(conf_node, KEY_NICK, RECDB_QSTRING);
7145 if (opserv && str)
7146 NickChange(opserv, str, 0);
d914d1cb 7147
d76ed9a9 7148 str = database_get_data(conf_node, KEY_CLONE_GLINE_DURATION, RECDB_QSTRING);
7149 opserv_conf.clone_gline_duration = str ? ParseInterval(str) : 3600;
7150 str = database_get_data(conf_node, KEY_BLOCK_GLINE_DURATION, RECDB_QSTRING);
7151 opserv_conf.block_gline_duration = str ? ParseInterval(str) : 3600;
7152
7637f48f 7153 free_string_list(autojoin_channels);
7154 autojoin_channels = database_get_data(conf_node, KEY_AUTOJOIN_CHANNELS, RECDB_STRING_LIST);
7155
7156 if(autojoin_channels)
7157 autojoin_channels = string_list_copy(autojoin_channels);
7158
7159 if (autojoin_channels && opserv) {
7160 for (i = 0; i < autojoin_channels->used; i++) {
7161 chan = AddChannel(autojoin_channels->list[i], now, "+nt", NULL, NULL);
7162 AddChannelUser(opserv, chan)->modes |= MODE_CHANOP;
7163 }
7164 }
7165
d914d1cb 7166 str = database_get_data(conf_node, KEY_BLOCK_SHUN_DURATION, RECDB_QSTRING);
7167 opserv_conf.block_shun_duration = str ? ParseInterval(str) : 3600;
7168
d76ed9a9 7169 if (!opserv_conf.join_policer_params)
7170 opserv_conf.join_policer_params = policer_params_new();
7171 policer_params_set(opserv_conf.join_policer_params, "size", "20");
7172 policer_params_set(opserv_conf.join_policer_params, "drain-rate", "1");
7173 if ((child = database_get_data(conf_node, KEY_JOIN_POLICER, RECDB_OBJECT)))
258d1427 7174 dict_foreach(child, set_policer_param, opserv_conf.join_policer_params);
d76ed9a9 7175
7176 for (it = dict_first(channels); it; it = iter_next(it)) {
7177 struct chanNode *cNode = iter_data(it);
7178 cNode->join_policer.params = opserv_conf.join_policer_params;
7179 }
7180
7181 if (opserv_conf.new_user_policer.params)
7182 pp = opserv_conf.new_user_policer.params;
7183 else
7184 pp = opserv_conf.new_user_policer.params = policer_params_new();
7185 policer_params_set(pp, "size", "200");
7186 policer_params_set(pp, "drain-rate", "3");
7187 if ((child = database_get_data(conf_node, KEY_NEW_USER_POLICER, RECDB_OBJECT)))
258d1427 7188 dict_foreach(child, set_policer_param, pp);
08895577 7189
7190 /* Defcon configuration */
7191 DefCon[0] = 0;
7192 str = database_get_data(conf_node, KEY_DEFCON1, RECDB_QSTRING);
7193 DefCon[1] = str ? atoi(str) : 415;
7194 str = database_get_data(conf_node, KEY_DEFCON2, RECDB_QSTRING);
7195 DefCon[2] = str ? atoi(str) : 159;
7196 str = database_get_data(conf_node, KEY_DEFCON3, RECDB_QSTRING);
7197 DefCon[3] = str ? atoi(str) : 31;
7198 str = database_get_data(conf_node, KEY_DEFCON4, RECDB_QSTRING);
7199 DefCon[4] = str? atoi(str) : 23;
7200 DefCon[5] = 0;
7201
7202 str = database_get_data(conf_node, KEY_DEFCON_LEVEL, RECDB_QSTRING);
7203 DefConLevel = str ? atoi(str) : 5;
7204
7205 str = database_get_data(conf_node, KEY_DEFCON_CHANMODES, RECDB_QSTRING);
7206 DefConChanModes = str ? strdup(str) : "+r";
7207
7208 str = database_get_data(conf_node, KEY_DEFCON_SESSION_LIMIT, RECDB_QSTRING);
7209 DefConSessionLimit = str ? atoi(str) : 2;
7210
7211 str = database_get_data(conf_node, KEY_DEFCON_TIMEOUT, RECDB_QSTRING);
7212 DefConTimeOut = str ? ParseInterval(str) : 900;
7213
7214 str = database_get_data(conf_node, KEY_DEFCON_GLINE_DURATION, RECDB_QSTRING);
7215 DefConGlineExpire = str ? ParseInterval(str) : 300;
7216
08895577 7217 str = database_get_data(conf_node, KEY_DEFCON_GLOBAL, RECDB_QSTRING);
7218 GlobalOnDefcon = str ? atoi(str) : 0;
7219
7220 str = database_get_data(conf_node, KEY_DEFCON_GLOBAL_MORE, RECDB_QSTRING);
7221 GlobalOnDefconMore = str ? atoi(str) : 0;
7222
7223 str = database_get_data(conf_node, KEY_DEFCON_MESSAGE, RECDB_QSTRING);
7224 DefConMessage = str ? strdup(str) : "Put your message to send your users here. Dont forget to uncomment GlobalOnDefconMore";
7225
7226 str = database_get_data(conf_node, KEY_DEFCON_OFF_MESSAGE, RECDB_QSTRING);
7227 DefConOffMessage = str? strdup(str) : "Services are now back to normal, sorry for any inconvenience";
7228
7229 str = database_get_data(conf_node, KEY_DEFCON_GLINE_REASON, RECDB_QSTRING);
7230 DefConGlineReason = str ? strdup(str) : "This network is currently not accepting connections, please try again later";
d76ed9a9 7231}
7232
5a1daaab 7233/* lame way to export opserv_conf value to nickserv.c ... */
7234unsigned int
7235opserv_conf_admin_level()
7236{
7237 return(opserv_conf.admin_level);
7238}
7239
d76ed9a9 7240static void
7241opserv_db_init(void) {
7242 /* set up opserv_trusted_hosts dict */
7243 dict_delete(opserv_trusted_hosts);
7244 opserv_trusted_hosts = dict_new();
7245 dict_set_free_data(opserv_trusted_hosts, free_trusted_host);
47956fc5 7246
7247 opserv_routing_plan_options = dict_new();
7248
7249 opserv_routing_plans = dict_new();
7250 dict_set_free_data(opserv_routing_plans, free_routing_plan);
d76ed9a9 7251 /* set up opserv_chan_warn dict */
de9510bc 7252
7253/* alert trace notice channel #x replaces warnings
d76ed9a9 7254 dict_delete(opserv_chan_warn);
7255 opserv_chan_warn = dict_new();
7256 dict_set_free_keys(opserv_chan_warn, free);
7257 dict_set_free_data(opserv_chan_warn, free);
de9510bc 7258*/
d76ed9a9 7259 /* set up opserv_user_alerts */
7260 dict_delete(opserv_channel_alerts);
7261 opserv_channel_alerts = dict_new();
7262 dict_delete(opserv_nick_based_alerts);
7263 opserv_nick_based_alerts = dict_new();
b7594203 7264 dict_delete(opserv_account_based_alerts);
7265 opserv_account_based_alerts = dict_new();
d76ed9a9 7266 dict_delete(opserv_user_alerts);
7267 opserv_user_alerts = dict_new();
7268 dict_set_free_keys(opserv_user_alerts, free);
7269 dict_set_free_data(opserv_user_alerts, opserv_free_user_alert);
7270 /* set up opserv_bad_words */
7271 free_string_list(opserv_bad_words);
7272 opserv_bad_words = alloc_string_list(4);
7273 /* and opserv_exempt_channels */
7274 dict_delete(opserv_exempt_channels);
7275 opserv_exempt_channels = dict_new();
7276 dict_set_free_keys(opserv_exempt_channels, free);
7277}
7278
7279static void
30874d66 7280opserv_db_cleanup(UNUSED_ARG(void* extra))
d76ed9a9 7281{
7282 unsigned int nn;
7283
de9510bc 7284/* dict_delete(opserv_chan_warn); */
d76ed9a9 7285 dict_delete(opserv_reserved_nick_dict);
7286 free_string_list(opserv_bad_words);
7287 dict_delete(opserv_exempt_channels);
7288 dict_delete(opserv_trusted_hosts);
a6bcc929 7289 unreg_del_user_func(opserv_user_cleanup, NULL);
d76ed9a9 7290 dict_delete(opserv_hostinfo_dict);
7291 dict_delete(opserv_nick_based_alerts);
b7594203 7292 dict_delete(opserv_account_based_alerts);
d76ed9a9 7293 dict_delete(opserv_channel_alerts);
7294 dict_delete(opserv_user_alerts);
7295 for (nn=0; nn<ArrayLength(level_strings); ++nn)
7296 free(level_strings[nn]);
7297 while (gagList)
7298 gag_free(gagList);
7299 policer_params_delete(opserv_conf.join_policer_params);
7300 policer_params_delete(opserv_conf.new_user_policer.params);
7301}
7302
7303void
7304init_opserv(const char *nick)
7305{
7306 OS_LOG = log_register_type("OpServ", "file:opserv.log");
a32da4c7 7307 if (nick) {
7308 const char *modes = conf_get_data("services/opserv/modes", RECDB_QSTRING);
1136f709 7309 opserv = AddLocalUser(nick, nick, NULL, "Oper Services", modes);
a32da4c7 7310 }
d76ed9a9 7311 conf_register_reload(opserv_conf_read);
7312
7313 memset(level_strings, 0, sizeof(level_strings));
7314 opserv_module = module_register("OpServ", OS_LOG, "opserv.help", opserv_help_expand);
7315 opserv_define_func("ACCESS", cmd_access, 0, 0, 0);
7316 opserv_define_func("ADDALERT", cmd_addalert, 800, 0, 4);
7317 opserv_define_func("ADDALERT NOTICE", NULL, 0, 0, 0);
890866b0 7318 opserv_define_func("ADDALERT NOTICEUSER", NULL, 0, 0, 0);
7319 opserv_define_func("ADDALERT MSGUSER", NULL, 0, 0, 0);
9a75756e 7320 opserv_define_func("ADDALERT SILENT", NULL, 900, 0, 0);
d76ed9a9 7321 opserv_define_func("ADDALERT GLINE", NULL, 900, 0, 0);
d914d1cb 7322 opserv_define_func("ADDALERT SHUN", NULL, 900, 0, 0);
ec311f39 7323 opserv_define_func("ADDALERT TRACK", NULL, 900, 0, 0);
d76ed9a9 7324 opserv_define_func("ADDALERT KILL", NULL, 900, 0, 0);
c408f18a 7325 opserv_define_func("ADDALERT SVSJOIN", NULL, 999, 0, 0);
39c1a4ef 7326 opserv_define_func("ADDALERT SVSPART", NULL, 999, 0, 0);
0e08a8e0 7327 opserv_define_func("ADDALERT VERSION", NULL, 999, 0, 0);
d82cf2f0 7328 opserv_define_func("ADDALERT MARK", NULL, 999, 0, 0);
d76ed9a9 7329 opserv_define_func("ADDBAD", cmd_addbad, 800, 0, 2);
7330 opserv_define_func("ADDEXEMPT", cmd_addexempt, 800, 0, 2);
7331 opserv_define_func("ADDTRUST", cmd_addtrust, 800, 0, 5);
7332 opserv_define_func("BAN", cmd_ban, 100, 2, 2);
7333 opserv_define_func("BLOCK", cmd_block, 100, 0, 2);
7334 opserv_define_func("CHANINFO", cmd_chaninfo, 0, 3, 0);
7335 opserv_define_func("CLEARBANS", cmd_clearbans, 300, 2, 0);
7336 opserv_define_func("CLEARMODES", cmd_clearmodes, 400, 2, 0);
7337 opserv_define_func("CLONE", cmd_clone, 999, 0, 3);
7338 opserv_define_func("COLLIDE", cmd_collide, 800, 0, 5);
7339 opserv_define_func("CSEARCH", cmd_csearch, 100, 0, 3);
7340 opserv_define_func("CSEARCH COUNT", cmd_csearch, 0, 0, 0);
7341 opserv_define_func("CSEARCH PRINT", cmd_csearch, 0, 0, 0);
7342 opserv_define_func("DELALERT", cmd_delalert, 800, 0, 2);
7343 opserv_define_func("DELBAD", cmd_delbad, 800, 0, 2);
7344 opserv_define_func("DELEXEMPT", cmd_delexempt, 800, 0, 2);
7345 opserv_define_func("DELTRUST", cmd_deltrust, 800, 0, 2);
7346 opserv_define_func("DEOP", cmd_deop, 100, 2, 2);
7347 opserv_define_func("DEOPALL", cmd_deopall, 400, 2, 0);
08895577 7348 opserv_define_func("DEFCON", cmd_defcon, 900, 0, 0);
55342ce8 7349 opserv_define_func("DEHOP", cmd_dehop, 100, 2, 2);
7350 opserv_define_func("DEHOPALL", cmd_dehopall, 400, 2, 0);
d76ed9a9 7351 opserv_define_func("DEVOICEALL", cmd_devoiceall, 300, 2, 0);
7352 opserv_define_func("DIE", cmd_die, 900, 0, 2);
7353 opserv_define_func("DUMP", cmd_dump, 999, 0, 2);
7354 opserv_define_func("EDITTRUST", cmd_edittrust, 800, 0, 5);
7355 opserv_define_func("GAG", cmd_gag, 600, 0, 4);
7356 opserv_define_func("GLINE", cmd_gline, 600, 0, 4);
7357 opserv_define_func("GSYNC", cmd_gsync, 600, 0, 0);
7358 opserv_define_func("GTRACE", cmd_gtrace, 100, 0, 3);
7359 opserv_define_func("GTRACE COUNT", NULL, 0, 0, 0);
7360 opserv_define_func("GTRACE PRINT", NULL, 0, 0, 0);
d914d1cb 7361 opserv_define_func("SBLOCK", cmd_sblock, 100, 0, 2);
7362 opserv_define_func("SHUN", cmd_shun, 600, 0, 4);
7363 opserv_define_func("SSYNC", cmd_ssync, 600, 0, 0);
7364 opserv_define_func("STRACE", cmd_strace, 100, 0, 3);
7365 opserv_define_func("STRACE COUNT", NULL, 0, 0, 0);
7366 opserv_define_func("STRACE PRINT", NULL, 0, 0, 0);
d76ed9a9 7367 opserv_define_func("INVITE", cmd_invite, 100, 2, 0);
7368 opserv_define_func("INVITEME", cmd_inviteme, 100, 0, 0);
1136f709 7369 opserv_define_func("JOIN", cmd_join, 601, 1, 0);
964a842d 7370 opserv_define_func("SVSNICK", cmd_svsnick, 999, 0, 3);
c408f18a 7371 opserv_define_func("SVSJOIN", cmd_svsjoin, 999, 0, 3);
39c1a4ef 7372 opserv_define_func("SVSPART", cmd_svspart, 999, 0, 3);
d76ed9a9 7373 opserv_define_func("JUMP", cmd_jump, 900, 0, 2);
7374 opserv_define_func("JUPE", cmd_jupe, 900, 0, 4);
7375 opserv_define_func("KICK", cmd_kick, 100, 2, 2);
1136f709 7376 opserv_define_func("FORCEKICK", cmd_forcekick, 800, 2, 2);
d76ed9a9 7377 opserv_define_func("KICKALL", cmd_kickall, 400, 2, 0);
7378 opserv_define_func("KICKBAN", cmd_kickban, 100, 2, 2);
7379 opserv_define_func("KICKBANALL", cmd_kickbanall, 450, 2, 0);
7380 opserv_define_func("LOG", cmd_log, 900, 0, 2);
7381 opserv_define_func("MODE", cmd_mode, 100, 2, 2);
d82cf2f0 7382 opserv_define_func("MARK", cmd_mark, 900, 0, 3);
d76ed9a9 7383 opserv_define_func("OP", cmd_op, 100, 2, 2);
7384 opserv_define_func("OPALL", cmd_opall, 400, 2, 0);
55342ce8 7385 opserv_define_func("HOP", cmd_hop, 100, 2, 2);
7386 opserv_define_func("HOPALL", cmd_hopall, 400, 2, 0);
47956fc5 7387 opserv_define_func("MAP", cmd_stats_links, 0, 0, 0);
6c34bb5a 7388 opserv_define_func("PRIVSET", cmd_privset, 900, 0, 3);
1136f709 7389 opserv_define_func("PART", cmd_part, 601, 2, 0);
d76ed9a9 7390 opserv_define_func("QUERY", cmd_query, 0, 0, 0);
7391 opserv_define_func("RAW", cmd_raw, 999, 0, 2);
7392 opserv_define_func("RECONNECT", cmd_reconnect, 900, 0, 0);
7393 opserv_define_func("REFRESHG", cmd_refreshg, 600, 0, 0);
d914d1cb 7394 opserv_define_func("REFRESHS", cmd_refreshs, 600, 0, 0);
d76ed9a9 7395 opserv_define_func("REHASH", cmd_rehash, 900, 0, 0);
7396 opserv_define_func("REOPEN", cmd_reopen, 900, 0, 0);
7637f48f 7397 opserv_define_func("RESETMAX", cmd_resetmax, 900, 0, 0);
d76ed9a9 7398 opserv_define_func("RESERVE", cmd_reserve, 800, 0, 5);
7399 opserv_define_func("RESTART", cmd_restart, 900, 0, 2);
47956fc5 7400 opserv_define_func("ROUTING ADDPLAN", cmd_routing_addplan, 800, 0, 2);
7401 opserv_define_func("ROUTING DELPLAN", cmd_routing_delplan, 800, 0, 2);
7402 opserv_define_func("ROUTING ADDSERVER", cmd_routing_addserver, 800, 0, 4);
7403 opserv_define_func("ROUTING DELSERVER", cmd_routing_delserver, 800, 0, 3);
7404 opserv_define_func("ROUTING MAP", cmd_routing_map, 800, 0, 0);
7405 opserv_define_func("ROUTING SET", cmd_routing_set, 800, 0, 0);
7406 opserv_define_func("REROUTE", cmd_reroute, 800, 0, 2);
d76ed9a9 7407 opserv_define_func("SET", cmd_set, 900, 0, 3);
7408 opserv_define_func("SETTIME", cmd_settime, 901, 0, 0);
7409 opserv_define_func("STATS ALERTS", cmd_stats_alerts, 0, 0, 0);
7410 opserv_define_func("STATS BAD", cmd_stats_bad, 0, 0, 0);
7411 opserv_define_func("STATS GAGS", cmd_stats_gags, 0, 0, 0);
7412 opserv_define_func("STATS GLINES", cmd_stats_glines, 0, 0, 0);
d914d1cb 7413 opserv_define_func("STATS SHUNS", cmd_stats_shuns, 0, 0, 0);
d76ed9a9 7414 opserv_define_func("STATS LINKS", cmd_stats_links, 0, 0, 0);
7415 opserv_define_func("STATS MAX", cmd_stats_max, 0, 0, 0);
7416 opserv_define_func("STATS NETWORK", cmd_stats_network, 0, 0, 0);
7417 opserv_define_func("STATS NETWORK2", cmd_stats_network2, 0, 0, 0);
7418 opserv_define_func("STATS RESERVED", cmd_stats_reserved, 0, 0, 0);
47956fc5 7419 opserv_define_func("STATS ROUTING", cmd_stats_routing_plans, 0, 0, 0);
d76ed9a9 7420 opserv_define_func("STATS TIMEQ", cmd_stats_timeq, 0, 0, 0);
7421 opserv_define_func("STATS TRUSTED", cmd_stats_trusted, 0, 0, 0);
7422 opserv_define_func("STATS UPLINK", cmd_stats_uplink, 0, 0, 0);
7423 opserv_define_func("STATS UPTIME", cmd_stats_uptime, 0, 0, 0);
de9510bc 7424/* opserv_define_func("STATS WARN", cmd_stats_warn, 0, 0, 0); */
f14e4f83 7425#if defined(WITH_MALLOC_X3) || defined(WITH_MALLOC_SLAB)
ec1a68c8 7426 opserv_define_func("STATS MEMORY", cmd_stats_memory, 0, 0, 0);
7427#endif
d76ed9a9 7428 opserv_define_func("TRACE", cmd_trace, 100, 0, 3);
7429 opserv_define_func("TRACE PRINT", NULL, 0, 0, 0);
7430 opserv_define_func("TRACE COUNT", NULL, 0, 0, 0);
53b62ed9 7431 opserv_define_func("TRACE PRIVMSG", NULL, 0, 0, 0);
7432 opserv_define_func("TRACE NOTICE", NULL, 0, 0, 0);
d76ed9a9 7433 opserv_define_func("TRACE DOMAINS", NULL, 0, 0, 0);
7434 opserv_define_func("TRACE GLINE", NULL, 600, 0, 0);
d914d1cb 7435 opserv_define_func("TRACE SHUN", NULL, 600, 0, 0);
d76ed9a9 7436 opserv_define_func("TRACE GAG", NULL, 600, 0, 0);
7437 opserv_define_func("TRACE KILL", NULL, 600, 0, 0);
0e08a8e0 7438 opserv_define_func("TRACE VERSION", NULL, 999, 0, 0);
7439 opserv_define_func("TRACE SVSJOIN", NULL, 999, 0, 0);
39c1a4ef 7440 opserv_define_func("TRACE SVSPART", NULL, 999, 0, 0);
d82cf2f0 7441 opserv_define_func("TRACE MARK", NULL, 999, 0, 0);
d76ed9a9 7442 opserv_define_func("UNBAN", cmd_unban, 100, 2, 2);
7443 opserv_define_func("UNGAG", cmd_ungag, 600, 0, 2);
7444 opserv_define_func("UNGLINE", cmd_ungline, 600, 0, 2);
7445 modcmd_register(opserv_module, "GTRACE UNGLINE", NULL, 0, 0, "template", "ungline", NULL);
d914d1cb 7446 opserv_define_func("UNSHUN", cmd_unshun, 600, 0, 2);
7447 modcmd_register(opserv_module, "GTRACE UNSHUN", NULL, 0, 0, "template", "unshun", NULL);
d76ed9a9 7448 opserv_define_func("UNJUPE", cmd_unjupe, 900, 0, 2);
7449 opserv_define_func("UNRESERVE", cmd_unreserve, 800, 0, 2);
de9510bc 7450/* opserv_define_func("UNWARN", cmd_unwarn, 800, 0, 0); */
d76ed9a9 7451 opserv_define_func("VOICEALL", cmd_voiceall, 300, 2, 0);
de9510bc 7452/* opserv_define_func("WARN", cmd_warn, 800, 0, 2); */
d76ed9a9 7453 opserv_define_func("WHOIS", cmd_whois, 0, 0, 2);
7454
7455 opserv_reserved_nick_dict = dict_new();
7456 opserv_hostinfo_dict = dict_new();
47956fc5 7457
d76ed9a9 7458 dict_set_free_keys(opserv_hostinfo_dict, free);
7459 dict_set_free_data(opserv_hostinfo_dict, opserv_free_hostinfo);
7460
47956fc5 7461 opserv_waiting_connections = dict_new();
7462 dict_set_free_data(opserv_waiting_connections, opserv_free_waiting_connection);
7463
f0fb2e2d 7464 reg_new_user_func(opserv_new_user_check, NULL);
63189c10 7465 reg_nick_change_func(opserv_alert_check_nick, NULL);
a6bcc929 7466 reg_del_user_func(opserv_user_cleanup, NULL);
157ce145 7467 reg_new_channel_func(opserv_channel_check, NULL);
1898d8ea 7468 reg_del_channel_func(opserv_channel_delete, NULL);
fb38e2bc 7469 reg_join_func(opserv_join_check, NULL);
81ac4787 7470 reg_auth_func(opserv_staff_alert, NULL);
7471 reg_auth_func(opserv_alert_check_account, NULL);
0e08a8e0 7472 reg_notice_func(opserv, opserv_notice_handler);
d76ed9a9 7473
7474 opserv_db_init();
7475 saxdb_register("OpServ", opserv_saxdb_read, opserv_saxdb_write);
7476 if (nick)
258d1427 7477 {
7478 opserv_service = service_register(opserv);
7479 opserv_service->trigger = '?';
7480 }
d76ed9a9 7481
47956fc5 7482 /* start auto-routing system */
9079d26c 7483 /* this cant be done here, because the routing system isnt marked active yet. */
7484 /* reroute_timer(NULL); */
7485
47956fc5 7486 /* start the karma timer, using the saved one if available */
7487 routing_karma_timer(dict_find(opserv_routing_plan_options, "KARMA_TIMER", NULL));
7488
30874d66 7489 reg_exit_func(opserv_db_cleanup, NULL);
d76ed9a9 7490 message_register_table(msgtab);
7491}