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