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