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