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