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