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