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