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