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