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