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