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