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