1 /* opserv.c - IRC Operator assistance service
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
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.
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.
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.
34 #include <tre/regex.h>
36 #ifdef HAVE_SYS_TIMES_H
37 #include <sys/times.h>
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
42 #ifdef HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
47 #define OPSERV_CONF_NAME "services/opserv"
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"
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"
112 /* Routing karma values: */
113 /* What value we start out with when new servers are added: */
114 #define KARMA_DEFAULT 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 */
129 #define ROUTING_CONNECT_TIMEOUT 30 /* 30 seconds */
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."
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." },
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." },
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." },
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" },
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" },
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" },
414 #define OPSERV_SYNTAX() svccmd_send_help_brief(user, opserv, cmd)
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
;
427 char *DefConOffMessage
;
429 extern void add_track_user(struct userNode
*user
);
430 typedef int (*discrim_search_func
)(struct userNode
*match
, void *extra
);
432 struct userNode
*opserv
;
433 static struct service
*opserv_service
;
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()*/
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
;
471 struct trusted_host
{
485 struct gag_entry
*next
;
488 static struct gag_entry
*gagList
;
490 struct opserv_hostinfo
{
491 struct userList clients
;
492 struct trusted_host
*trusted
;
496 opserv_free_hostinfo(void *data
)
498 struct opserv_hostinfo
*ohi
= data
;
499 userList_clean(&ohi
->clients
);
504 opserv_free_waiting_connection(void *data
)
506 struct waitingConnection
*wc
= data
;
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
;
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;
530 struct discrim_and_source
{
532 struct userNode
*source
;
533 struct userNode
*destination
;
535 unsigned int disp_limit
;
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
);
550 } opserv_alert_reaction
;
552 struct opserv_user_alert
{
554 char *text_discrim
, *split_discrim
;
556 opserv_alert_reaction reaction
;
559 /* funny type to make it acceptible to dict_set_free_data, far below */
561 opserv_free_user_alert(void *data
)
563 struct opserv_user_alert
*alert
= data
;
564 if (alert
->discrim
->channel
)
565 UnlockChannel(alert
->discrim
->channel
);
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
);
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)
586 char *defconReverseModes(const char *modes
)
588 char *newmodes
= NULL
;
593 if (!(newmodes
= malloc(sizeof(char) * strlen(modes
) + 1))) {
596 for (i
= 0; i
< strlen(modes
); i
++) {
599 else if (modes
[i
] == '-')
602 newmodes
[i
] = modes
[i
];
608 int checkDefCon(int level
)
610 return DefCon
[DefConLevel
] & level
;
613 void showDefConSettings(struct userNode
*user
, struct svccmd
*cmd
)
615 if (DefConLevel
== 5) {
616 reply("OSMSG_DEFCON_ALLOWING_ALL");
619 reply("OSMSG_DEFCON_DISALLOWING", DefConLevel
);
621 if (checkDefCon(DEFCON_NO_NEW_CHANNELS
))
622 reply("OSMSG_DEFCON_NO_NEW_CHANNELS");
624 if (checkDefCon(DEFCON_NO_NEW_NICKS
))
625 reply("OSMSG_DEFCON_NO_NEW_NICKS");
627 if (checkDefCon(DEFCON_NO_MODE_CHANGE
))
628 reply("OSMSG_DEFCON_NO_MODE_CHANGE");
630 if (checkDefCon(DEFCON_FORCE_CHAN_MODES
) && (DefConChanModes
))
631 reply("OSMSG_DEFCON_FORCE_CHANMODES", DefConChanModes
);
633 if (checkDefCon(DEFCON_REDUCE_SESSION
))
634 reply("OSMSG_DEFCON_REDUCE_SESSION", DefConSessionLimit
);
636 if (checkDefCon(DEFCON_NO_NEW_CLIENTS
))
637 reply("OSMSG_DEFCON_NO_NEW_CLIENTS");
639 if (checkDefCon(DEFCON_OPER_ONLY
))
640 reply("OSMSG_DEFCON_OPER_ONLY");
642 if (checkDefCon(DEFCON_SILENT_OPER_ONLY
))
643 reply("OSMSG_DEFCON_SILENT_OPER_ONLY");
645 if (checkDefCon(DEFCON_GLINE_NEW_CLIENTS
))
646 reply("OSMSG_DEFCON_GLINE_NEW_CLIENTS");
648 if (checkDefCon(DEFCON_SHUN_NEW_CLIENTS
))
649 reply("OSMSG_DEFCON_SHUN_NEW_CLIENTS");
651 if (checkDefCon(DEFCON_NO_NEW_MEMOS
))
652 reply("OSMSG_DEFCON_NO_NEW_MEMOS");
657 void do_mass_mode(char *modes
)
664 for (it
= dict_first(channels
); it
; it
= iter_next(it
)) {
665 struct chanNode
*chan
= iter_data(it
);
667 irc_mode(opserv
, chan
, modes
);
672 void DefConProcess(struct userNode
*user
)
677 global_message_args(MESSAGE_RECIPIENT_LUSERS
, "DEFCON_NETWORK_CHANGED", DefConLevel
);
679 if (GlobalOnDefconMore
&& GlobalOnDefcon
)
680 global_message(MESSAGE_RECIPIENT_LUSERS
, DefConMessage
);
682 if ((DefConLevel
== 5) && !GlobalOnDefconMore
&& !GlobalOnDefcon
)
683 global_message(MESSAGE_RECIPIENT_LUSERS
, DefConOffMessage
);
686 global_message_args(MESSAGE_RECIPIENT_OPERS
, "DEFCON_OPER_LEVEL_CHANGE", user
->nick
, DefConLevel
);
688 global_message_args(MESSAGE_RECIPIENT_OPERS
, "DEFCON_TIMEOUT_LEVEL_CHANGE", DefConLevel
);
690 if (checkDefCon(DEFCON_FORCE_CHAN_MODES
)) {
691 if (DefConChanModes
&& !DefConModesSet
) {
692 if (DefConChanModes
[0] == '+' || DefConChanModes
[0] == '-') {
693 do_mass_mode(DefConChanModes
);
698 if (DefConChanModes
&& (DefConModesSet
!= 0)) {
699 if (DefConChanModes
[0] == '+' || DefConChanModes
[0] == '-') {
700 if ((newmodes
= defconReverseModes(DefConChanModes
))) {
701 do_mass_mode(newmodes
);
713 defcon_timeout(UNUSED_ARG(void *data
))
719 static MODCMD_FUNC(cmd_defcon
)
721 if ((argc
< 2) || (atoi(argv
[1]) == DefConLevel
)) {
722 showDefConSettings(user
, cmd
);
726 if ((atoi(argv
[1]) < 1) || (atoi(argv
[1]) > 5)) {
727 reply("OSMSG_DEFCON_INVALID", atoi(argv
[1]));
731 DefConLevel
= atoi(argv
[1]);
732 showDefConSettings(user
, cmd
);
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
);
744 static MODCMD_FUNC(cmd_privallow)
746 //privallow servername/username +/-flag (global is set in conf)
749 static MODCMD_FUNC(cmd_privdissallow)
751 //privdisallow servername/username +/-flag (global is set in conf)
754 static MODCMD_FUNC(cmd_privlist)
756 //privlist servername/user (global with none)
760 static MODCMD_FUNC(cmd_privset
)
762 struct userNode
*target
;
770 } else if (*flag
== '+') {
775 target
= GetUserH(argv
[1]);
777 reply("MSG_NICK_UNKNOWN", argv
[1]);
781 if (check_priv(flag
)) {
782 irc_privs(target
, flag
, add
);
783 reply("OSMSG_PRIV_SET", argv
[2], (add
== 1) ? "" : "un");
785 reply("OSMSG_PRIV_UNKNOWN", argv
[2]);
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.
796 static MODCMD_FUNC(cmd_access
)
798 struct handle_info
*hi
;
802 target
= (argc
> 1) ? (const char*)argv
[1] : user
->nick
;
803 if (!irccasecmp(target
, "*")) {
804 nickserv_show_oper_accounts(user
, cmd
);
807 if (!(hi
= modcmd_get_handle_info(user
, target
)))
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
);
814 static MODCMD_FUNC(cmd_ban
)
816 struct mod_chanmode change
;
817 struct userNode
*victim
;
819 mod_chanmode_init(&change
);
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);
827 reply("OSMSG_INVALID_IRCMASK", argv
[1]);
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
);
836 static MODCMD_FUNC(cmd_chaninfo
)
841 struct exemptNode
*exempt
;
842 struct modeNode
*moden
;
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
);
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
);
859 irc_fetchtopic(cmd
->parent
->bot
, channel
->name
);
860 reply("OSMSG_CHANINFO_TOPIC_UNKNOWN");
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
);
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
);
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
);
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
);
892 send_message_type(4, user
, cmd
->parent
->bot
, " @%s (%s@%s)", moden
->user
->nick
, moden
->user
->ident
, moden
->user
->hostname
);
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
);
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
);
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
);
913 /* This command has been replaced by 'alert notice channel #foo' */
915 static MODCMD_FUNC(cmd_warn)
917 char *reason, *message;
919 if (!IsChannelName(argv[1])) {
920 reply("OSMSG_NEED_CHANNEL", argv[0]);
923 reason = dict_find(opserv_chan_warn, argv[1], NULL);
925 reply("OSMSG_WARN_EXISTS", argv[1]);
929 reason = strdup(unsplit_string(argv+2, argc-2, NULL));
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);
940 static MODCMD_FUNC(cmd_unwarn)
942 if ((argc < 2) || !IsChannelName(argv[1])) {
943 reply("OSMSG_NEED_CHANNEL", argv[0]);
946 if (!dict_remove(opserv_chan_warn, argv[1])) {
947 reply("OSMSG_WARN_NOEXIST", argv[1]);
950 reply("OSMSG_WARN_DELETED", argv[1]);
955 static MODCMD_FUNC(cmd_clearbans
)
957 struct mod_chanmode
*change
;
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
);
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
);
973 static MODCMD_FUNC(cmd_clearmodes
)
975 struct mod_chanmode change
;
977 if (!channel
->modes
) {
978 reply("OSMSG_NO_CHANNEL_MODES", channel
->name
);
981 mod_chanmode_init(&change
);
982 change
.modes_clear
= channel
->modes
;
983 modcmd_chanmode_announce(&change
);
984 reply("OSMSG_CLEARMODES_DONE", channel
->name
);
988 static MODCMD_FUNC(cmd_deop
)
990 struct mod_chanmode
*change
;
991 unsigned int arg
, count
;
993 change
= mod_chanmode_alloc(argc
-1);
994 for (arg
= 1, count
= 0; arg
< argc
; ++arg
) {
995 struct userNode
*victim
= GetUserH(argv
[arg
]);
997 if (!victim
|| IsService(victim
)
998 || !(mn
= GetUserMode(channel
, victim
))
999 || !(mn
->modes
& MODE_CHANOP
))
1001 change
->args
[count
].mode
= MODE_REMOVE
| MODE_CHANOP
;
1002 change
->args
[count
++].u
.member
= mn
;
1005 change
->argc
= count
;
1006 modcmd_chanmode_announce(change
);
1008 mod_chanmode_free(change
);
1009 reply("OSMSG_DEOP_DONE");
1013 static MODCMD_FUNC(cmd_dehop
)
1015 struct mod_chanmode
*change
;
1016 unsigned int arg
, count
;
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
))
1026 change
->args
[count
].mode
= MODE_REMOVE
| MODE_HALFOP
;
1027 change
->args
[count
++].u
.member
= mn
;
1030 change
->argc
= count
;
1031 modcmd_chanmode_announce(change
);
1033 mod_chanmode_free(change
);
1034 reply("OSMSG_DEHOP_DONE");
1038 static MODCMD_FUNC(cmd_deopall
)
1040 struct mod_chanmode
*change
;
1041 unsigned int ii
, count
;
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
))
1048 change
->args
[count
].mode
= MODE_REMOVE
| MODE_CHANOP
;
1049 change
->args
[count
++].u
.member
= mn
;
1052 change
->argc
= count
;
1053 modcmd_chanmode_announce(change
);
1055 mod_chanmode_free(change
);
1056 reply("OSMSG_DEOPALL_DONE", channel
->name
);
1060 static MODCMD_FUNC(cmd_dehopall
)
1062 struct mod_chanmode
*change
;
1063 unsigned int ii
, count
;
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
))
1070 change
->args
[count
].mode
= MODE_REMOVE
| MODE_HALFOP
;
1071 change
->args
[count
++].u
.member
= mn
;
1074 change
->argc
= count
;
1075 modcmd_chanmode_announce(change
);
1077 mod_chanmode_free(change
);
1078 reply("OSMSG_DEHOPALL_DONE", channel
->name
);
1082 static MODCMD_FUNC(cmd_resetmax
)
1084 max_clients
= dict_size(clients
);
1085 max_clients_time
= now
;
1086 reply("OSMSG_MAXUSERS_RESET", max_clients
);
1090 static MODCMD_FUNC(cmd_rehash
)
1092 extern char *services_config
;
1094 if (conf_read(services_config
))
1095 reply("OSMSG_REHASH_COMPLETE");
1097 reply("OSMSG_REHASH_FAILED");
1101 static MODCMD_FUNC(cmd_reopen
)
1104 reply("OSMSG_REOPEN_COMPLETE");
1108 static MODCMD_FUNC(cmd_reconnect
)
1110 reply("OSMSG_RECONNECTING");
1111 irc_squit(self
, "Reconnecting.", NULL
);
1115 static MODCMD_FUNC(cmd_jupe
)
1117 extern int force_n2k
;
1118 struct server
*newsrv
;
1120 char numeric
[COMBO_NUMERIC_LEN
+1], srvdesc
[SERVERDESCRIPTMAX
+1];
1122 num
= atoi(argv
[2]);
1124 reply("OSMSG_INVALID_NUMERIC");
1127 if ((num
< 64) && !force_n2k
) {
1128 inttobase64(numeric
, num
, 1);
1129 inttobase64(numeric
+1, 64*64-1, 2);
1131 inttobase64(numeric
, num
, 2);
1132 inttobase64(numeric
+2, 64*64*64-1, 3);
1134 #ifdef WITH_PROTOCOL_P10
1135 if (GetServerN(numeric
)) {
1136 reply("OSMSG_NUMERIC_COLLIDE", num
, numeric
);
1140 if (GetServerH(argv
[1])) {
1141 reply("OSMSG_NAME_COLLIDE");
1144 snprintf(srvdesc
, sizeof(srvdesc
), "JUPE %s", unsplit_string(argv
+3, argc
-3, NULL
));
1145 if(!strchr(argv
[1], '.')) {
1146 reply("OSMSG_INVALID_SERVERNAME");
1149 newsrv
= AddServer(self
, argv
[1], 1, now
, now
, numeric
, srvdesc
);
1151 reply("OSMSG_SRV_CREATE_FAILED");
1155 reply("OSMSG_SERVER_JUPED", argv
[1]);
1159 static MODCMD_FUNC(cmd_unjupe
)
1164 srv
= GetServerH(argv
[1]);
1166 reply("MSG_SERVER_UNKNOWN", argv
[1]);
1169 if (strncmp(srv
->description
, "JUPE", 4)) {
1170 reply("OSMSG_SERVER_NOT_JUPE");
1173 reason
= (argc
> 2) ? unsplit_string(argv
+2, argc
-2, NULL
) : "Unjuping server";
1174 DelServer(srv
, 1, reason
);
1175 reply("OSMSG_SERVER_UNJUPED");
1179 static MODCMD_FUNC(cmd_jump
)
1181 extern struct cManagerNode cManager
;
1182 void uplink_select(char *name
);
1183 struct uplinkNode
*uplink_find(char *name
);
1184 struct uplinkNode
*uplink
;
1187 target
= unsplit_string(argv
+1, argc
-1, NULL
);
1189 if (!strcmp(cManager
.uplink
->name
, target
)) {
1190 reply("OSMSG_CURRENT_UPLINK", cManager
.uplink
->name
);
1194 uplink
= uplink_find(target
);
1196 reply("OSMSG_INVALID_UPLINK", target
);
1199 if (uplink
->flags
& UPLINK_UNAVAILABLE
) {
1200 reply("OSMSG_UPLINK_DISABLED", uplink
->name
);
1204 reply("OSMSG_UPLINK_CONNECTING", uplink
->name
, uplink
->host
, uplink
->port
);
1205 uplink_select(target
);
1206 irc_squit(self
, "Reconnecting.", NULL
);
1210 static MODCMD_FUNC(cmd_die
)
1212 char *reason
, *text
;
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
);
1222 static MODCMD_FUNC(cmd_restart
)
1224 extern int services_argc
;
1225 extern char **services_argv
;
1226 char **restart_argv
, *reason
, *text
;
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
);
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
;
1241 execv(services_argv
[0], restart_argv
);
1243 /* If we're still here, that means something went wrong. Reconnect. */
1247 static struct gline
*
1248 opserv_block(struct userNode
*target
, char *src_handle
, char *reason
, unsigned long duration
, int silent
)
1250 char mask
[IRC_NTOP_MAX_SIZE
+3] = { '*', '@', '\0' };
1251 irc_ntop(mask
+ 2, sizeof(mask
) - 2, &target
->ip
);
1253 snprintf(reason
= alloca(MAXLEN
), MAXLEN
,
1254 "G-line requested by %s.", src_handle
);
1256 duration
= opserv_conf
.block_gline_duration
;
1257 return gline_add(src_handle
, mask
, duration
, reason
, now
, 1, silent
? 1 : 0);
1260 static MODCMD_FUNC(cmd_block
)
1262 struct userNode
*target
;
1263 struct gline
*gline
;
1266 target
= GetUserH(argv
[1]);
1268 reply("MSG_NICK_UNKNOWN", argv
[1]);
1271 if (IsService(target
)) {
1272 reply("MSG_SERVICE_IMMUNE", target
->nick
);
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
);
1281 static MODCMD_FUNC(cmd_gline
)
1283 unsigned long duration
;
1285 struct gline
*gline
;
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]);
1292 if (!argv
[1][strspn(argv
[1], "#&*?@.")] && (strlen(argv
[1]) < 10)) {
1293 reply("OSMSG_STUPID_GLINE", argv
[1]);
1296 duration
= ParseInterval(argv
[2]);
1298 reply("MSG_INVALID_DURATION", argv
[2]);
1301 gline
= gline_add(user
->handle_info
->handle
, argv
[1], duration
, reason
, now
, 1, 0);
1302 reply("OSMSG_GLINE_ISSUED", gline
->target
);
1306 static MODCMD_FUNC(cmd_ungline
)
1308 if (gline_remove(argv
[1], 1))
1309 reply("OSMSG_GLINE_REMOVED", argv
[1]);
1311 reply("OSMSG_GLINE_FORCE_REMOVED", argv
[1]);
1315 static MODCMD_FUNC(cmd_refreshg
)
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]))
1326 gline_refresh_server(srv
);
1327 reply("OSMSG_GLINES_ONE_REFRESHED", srv
->name
);
1331 reply("MSG_SERVER_UNKNOWN", argv
[1]);
1335 gline_refresh_all();
1336 reply("OSMSG_GLINES_REFRESHED");
1342 opserv_svsjoin(struct userNode
*target
, char *src_handle
, char *reason
, char *channame
)
1344 struct chanNode
*channel
;
1346 if(!channame
|| !IsChannelName(channame
)) {
1347 /* Not a valid channel name. We shouldnt ever get this if we check properly in addalert */
1351 if (!(channel
= GetChannel(channame
))) {
1352 channel
= AddChannel(channame
, now
, NULL
, NULL
, NULL
);
1354 if (GetUserMode(channel
, target
)) {
1359 irc_svsjoin(opserv
, target
, channel
);
1360 /* Should we tell the user they got joined? -Rubin*/
1363 static struct shun
*
1364 opserv_shun(struct userNode
*target
, char *src_handle
, char *reason
, unsigned long duration
)
1367 mask
= alloca(MAXLEN
);
1368 snprintf(mask
, MAXLEN
, "*@%s", target
->hostname
);
1370 reason
= alloca(MAXLEN
);
1371 snprintf(reason
, MAXLEN
, "Shun requested by %s.", src_handle
);
1373 if (!duration
) duration
= opserv_conf
.block_shun_duration
;
1374 return shun_add(src_handle
, mask
, duration
, reason
, now
, 1);
1377 static MODCMD_FUNC(cmd_sblock
)
1379 struct userNode
*target
;
1383 target
= GetUserH(argv
[1]);
1385 reply("MSG_NICK_UNKNOWN", argv
[1]);
1388 if (IsService(target
)) {
1389 reply("MSG_SERVICE_IMMUNE", target
->nick
);
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
);
1398 static MODCMD_FUNC(cmd_shun
)
1400 unsigned long duration
;
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]);
1409 if (!argv
[1][strspn(argv
[1], "#&*?@.")] && (strlen(argv
[1]) < 10)) {
1410 reply("OSMSG_STUPID_SHUN", argv
[1]);
1413 duration
= ParseInterval(argv
[2]);
1415 reply("MSG_INVALID_DURATION", argv
[2]);
1418 shun
= shun_add(user
->handle_info
->handle
, argv
[1], duration
, reason
, now
, 1);
1419 reply("OSMSG_SHUN_ISSUED", shun
->target
);
1423 static MODCMD_FUNC(cmd_unshun
)
1425 if (shun_remove(argv
[1], 1))
1426 reply("OSMSG_SHUN_REMOVED", argv
[1]);
1428 reply("OSMSG_SHUN_FORCE_REMOVED", argv
[1]);
1432 static MODCMD_FUNC(cmd_refreshs
)
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]))
1443 shun_refresh_server(srv
);
1444 reply("OSMSG_SHUNS_ONE_REFRESHED", srv
->name
);
1448 reply("MSG_SERVER_UNKNOWN", argv
[1]);
1453 reply("OSMSG_SHUNS_REFRESHED");
1459 opserv_ison(struct userNode
*tell
, struct userNode
*target
, const char *message
)
1461 struct modeNode
*mn
;
1462 unsigned int count
, here_len
, n
, maxlen
;
1465 maxlen
= tell
->handle_info
? tell
->handle_info
->screen_width
: 0;
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
) {
1473 send_message(tell
, opserv
, message
, buff
);
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
);
1484 buff
[count
++] = ' ';
1488 send_message(tell
, opserv
, message
, buff
);
1492 static MODCMD_FUNC(cmd_inviteme
)
1494 struct userNode
*target
;
1499 target
= GetUserH(argv
[1]);
1501 reply("MSG_NICK_UNKNOWN", argv
[1]);
1505 if (opserv_conf
.debug_channel
== NULL
) {
1506 reply("OSMSG_NO_DEBUG_CHANNEL");
1509 if (GetUserMode(opserv_conf
.debug_channel
, user
)) {
1510 reply("OSMSG_ALREADY_THERE", opserv_conf
.debug_channel
->name
);
1513 irc_invite(cmd
->parent
->bot
, target
, opserv_conf
.debug_channel
);
1515 reply("OSMSG_INVITE_DONE", target
->nick
, opserv_conf
.debug_channel
->name
);
1519 static MODCMD_FUNC(cmd_invite
)
1521 if (GetUserMode(channel
, user
)) {
1522 reply("OSMSG_ALREADY_THERE", channel
->name
);
1525 irc_invite(cmd
->parent
->bot
, user
, channel
);
1529 static MODCMD_FUNC(cmd_svsjoin
)
1531 struct userNode
*target
;
1534 if(!IsChannelName(argv
[2])) {
1535 reply("MSG_NOT_CHANNEL_NAME");
1538 target
= GetUserH(argv
[1]);
1540 reply("MSG_NICK_UNKNOWN", argv
[1]);
1544 if (!(channel
= GetChannel(argv
[2]))) {
1545 channel
= AddChannel(argv
[2], now
, NULL
, NULL
, NULL
);
1547 if (GetUserMode(channel
, target
)) {
1548 reply("OSMSG_ALREADY_THERE", channel
->name
);
1551 irc_svsjoin(opserv
, target
, channel
);
1552 reply("OSMSG_SVSJOIN_SENT");
1556 static MODCMD_FUNC(cmd_join
)
1558 struct userNode
*bot
= cmd
->parent
->bot
;
1560 if (!IsChannelName(argv
[1])) {
1561 reply("MSG_NOT_CHANNEL_NAME");
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
);
1570 struct mod_chanmode change
;
1571 mod_chanmode_init(&change
);
1573 change
.args
[0].mode
= MODE_CHANOP
;
1574 change
.args
[0].u
.member
= AddChannelUser(bot
, channel
);
1575 modcmd_chanmode_announce(&change
);
1577 irc_fetchtopic(bot
, channel
->name
);
1578 reply("OSMSG_JOIN_DONE", channel
->name
);
1582 static MODCMD_FUNC(cmd_kick
)
1584 struct userNode
*target
;
1588 reason
= alloca(strlen(OSMSG_KICK_REQUESTED
)+strlen(user
->nick
)+1);
1589 sprintf(reason
, OSMSG_KICK_REQUESTED
, user
->nick
);
1591 reason
= unsplit_string(argv
+2, argc
-2, NULL
);
1593 target
= GetUserH(argv
[1]);
1595 reply("MSG_NICK_UNKNOWN", argv
[1]);
1598 if (!GetUserMode(channel
, target
)) {
1599 reply("OSMSG_NOT_ON_CHANNEL", target
->nick
, channel
->name
);
1602 KickChannelUser(target
, channel
, cmd
->parent
->bot
, reason
);
1606 static MODCMD_FUNC(cmd_kickall
)
1608 unsigned int limit
, n
, inchan
;
1609 struct modeNode
*mn
;
1611 struct userNode
*bot
= cmd
->parent
->bot
;
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
);
1623 reason
= alloca(strlen(OSMSG_KICK_REQUESTED
)+strlen(user
->nick
)+1);
1624 sprintf(reason
, OSMSG_KICK_REQUESTED
, user
->nick
);
1626 reason
= unsplit_string(argv
+1, argc
-1, NULL
);
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
))) {
1636 KickChannelUser(mn
->user
, channel
, bot
, reason
);
1639 DelChannelUser(bot
, channel
, "My work here is done", 0);
1640 reply("OSMSG_KICKALL_DONE", channel
->name
);
1644 static MODCMD_FUNC(cmd_kickban
)
1646 struct mod_chanmode change
;
1647 struct userNode
*target
;
1652 reason
= alloca(strlen(OSMSG_KICK_REQUESTED
)+strlen(user
->nick
)+1);
1653 sprintf(reason
, OSMSG_KICK_REQUESTED
, user
->nick
);
1655 reason
= unsplit_string(argv
+2, argc
-2, NULL
);
1657 target
= GetUserH(argv
[1]);
1659 reply("MSG_NICK_UNKNOWN", argv
[1]);
1662 if (!GetUserMode(channel
, target
)) {
1663 reply("OSMSG_NOT_ON_CHANNEL", target
->nick
, channel
->name
);
1666 mod_chanmode_init(&change
);
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
);
1676 static MODCMD_FUNC(cmd_kickbanall
)
1678 struct modeNode
*mn
;
1679 struct userNode
*bot
= cmd
->parent
->bot
;
1680 struct mod_chanmode
*change
;
1682 unsigned int limit
, n
, inchan
;
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
= "*!*@*";
1693 change
= mod_chanmode_alloc(1);
1694 change
->args
[0].mode
= MODE_BAN
;
1695 change
->args
[0].u
.hostmask
= "*!*@*";
1697 modcmd_chanmode_announce(change
);
1698 mod_chanmode_free(change
);
1700 reason
= alloca(strlen(OSMSG_KICK_REQUESTED
)+strlen(user
->nick
)+1);
1701 sprintf(reason
, OSMSG_KICK_REQUESTED
, user
->nick
);
1703 reason
= unsplit_string(argv
+1, argc
-1, NULL
);
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
))) {
1714 KickChannelUser(mn
->user
, channel
, bot
, reason
);
1717 DelChannelUser(bot
, channel
, "My work here is done", 0);
1718 reply("OSMSG_KICKALL_DONE", channel
->name
);
1722 static MODCMD_FUNC(cmd_part
)
1726 if (!IsChannelName(argv
[1])) {
1727 reply("MSG_NOT_CHANNEL_NAME");
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
);
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);
1742 static MODCMD_FUNC(cmd_mode
)
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
));
1748 reply("OSMSG_MODE_SET", channel
->name
);
1752 static MODCMD_FUNC(cmd_op
)
1754 struct mod_chanmode
*change
;
1755 unsigned int arg
, count
;
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
])))
1763 if (!(mn
= GetUserMode(channel
, victim
)))
1765 if (mn
->modes
& MODE_CHANOP
)
1767 change
->args
[count
].mode
= MODE_CHANOP
;
1768 change
->args
[count
++].u
.member
= mn
;
1771 change
->argc
= count
;
1772 modcmd_chanmode_announce(change
);
1774 mod_chanmode_free(change
);
1775 reply("OSMSG_OP_DONE");
1779 static MODCMD_FUNC(cmd_hop
)
1781 struct mod_chanmode
*change
;
1782 unsigned int arg
, count
;
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
])))
1790 if (!(mn
= GetUserMode(channel
, victim
)))
1792 if (mn
->modes
& MODE_HALFOP
)
1794 change
->args
[count
].mode
= MODE_HALFOP
;
1795 change
->args
[count
++].u
.member
= mn
;
1798 change
->argc
= count
;
1799 modcmd_chanmode_announce(change
);
1801 mod_chanmode_free(change
);
1802 reply("OSMSG_HOP_DONE");
1806 static MODCMD_FUNC(cmd_opall
)
1808 struct mod_chanmode
*change
;
1809 unsigned int ii
, count
;
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
)
1816 change
->args
[count
].mode
= MODE_CHANOP
;
1817 change
->args
[count
++].u
.member
= mn
;
1820 change
->argc
= count
;
1821 modcmd_chanmode_announce(change
);
1823 mod_chanmode_free(change
);
1824 reply("OSMSG_OPALL_DONE", channel
->name
);
1828 static MODCMD_FUNC(cmd_hopall
)
1830 struct mod_chanmode
*change
;
1831 unsigned int ii
, count
;
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
)
1838 change
->args
[count
].mode
= MODE_HALFOP
;
1839 change
->args
[count
++].u
.member
= mn
;
1842 change
->argc
= count
;
1843 modcmd_chanmode_announce(change
);
1845 mod_chanmode_free(change
);
1846 reply("OSMSG_HOPALL_DONE", channel
->name
);
1850 static MODCMD_FUNC(cmd_whois
)
1852 struct userNode
*target
;
1856 #ifdef WITH_PROTOCOL_P10
1857 if (argv
[1][0] == '*')
1858 target
= GetUserN(argv
[1]+1);
1861 target
= GetUserH(argv
[1]);
1863 reply("MSG_NICK_UNKNOWN", argv
[1]);
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
));
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
);
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.
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
);
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
);
1899 if (target
->modes
) {
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';
1908 // sethost - reed/apples
1909 // if (IsHelperIrcu(target)) buffer[bpos++] = 'h';
1910 if (IsSetHost(target
)) buffer
[bpos
++] = 'h';
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)");
1920 reply("OSMSG_WHOIS_MODES", buffer
);
1922 reply("OSMSG_WHOIS_INFO", target
->info
);
1923 #ifdef WITH_PROTOCOL_P10
1924 reply("OSMSG_WHOIS_NUMERIC", target
->numeric
);
1926 reply("OSMSG_WHOIS_SERVER", target
->uplink
->name
);
1927 reply("OSMSG_WHOIS_ACCOUNT", (target
->handle_info
? target
->handle_info
->handle
: "Not authenticated"));
1929 reply("OSMSG_WHOIS_PRIVS", client_report_privs(target
));
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");
1936 reply("OSMSG_WHOIS_HIDECHANS");
1940 static MODCMD_FUNC(cmd_unban
)
1942 struct mod_chanmode change
;
1943 mod_chanmode_init(&change
);
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
);
1952 static MODCMD_FUNC(cmd_voiceall
)
1954 struct mod_chanmode
*change
;
1955 unsigned int ii
, count
;
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
))
1962 change
->args
[count
].mode
= MODE_VOICE
;
1963 change
->args
[count
++].u
.member
= mn
;
1966 change
->argc
= count
;
1967 modcmd_chanmode_announce(change
);
1969 mod_chanmode_free(change
);
1970 reply("OSMSG_CHANNEL_VOICED", channel
->name
);
1974 static MODCMD_FUNC(cmd_devoiceall
)
1976 struct mod_chanmode
*change
;
1977 unsigned int ii
, count
;
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
))
1984 change
->args
[count
].mode
= MODE_REMOVE
| MODE_VOICE
;
1985 change
->args
[count
++].u
.member
= mn
;
1988 change
->argc
= count
;
1989 modcmd_chanmode_announce(change
);
1991 mod_chanmode_free(change
);
1992 reply("OSMSG_CHANNEL_DEVOICED", channel
->name
);
1996 static MODCMD_FUNC(cmd_stats_bad
) {
1998 unsigned int ii
, end
, here_len
;
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
)) {
2008 reply("OSMSG_BADWORD_LIST", buffer
);
2011 memcpy(buffer
+end
, opserv_bad_words
->list
[ii
], here_len
);
2013 buffer
[end
++] = ' ';
2016 reply("OSMSG_BADWORD_LIST", buffer
);
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
)) {
2023 reply("OSMSG_EXEMPTED_LIST", buffer
);
2026 memcpy(buffer
+end
, iter_key(it
), here_len
);
2028 buffer
[end
++] = ' ';
2031 reply("OSMSG_EXEMPTED_LIST", buffer
);
2035 static MODCMD_FUNC(cmd_stats_glines
) {
2036 reply("OSMSG_GLINE_COUNT", gline_count());
2040 static MODCMD_FUNC(cmd_stats_shuns
) {
2041 reply("OSMSG_SHUN_COUNT", shun_count());
2046 trace_links(struct userNode
*bot
, struct userNode
*user
, struct server
*server
, unsigned int depth
) {
2047 unsigned int nn
, pos
;
2050 for (nn
=1; nn
<=depth
; nn
<<=1) ;
2051 for (pos
=0, nn
>>=1; nn
>1; ) {
2053 buffer
[pos
++] = (depth
& nn
) ? ((nn
== 1) ? '`' : ' ') : '|';
2054 buffer
[pos
++] = (nn
== 1) ? '-': ' ';
2057 send_message(user
, bot
, "OSMSG_LINKS_SERVER", buffer
, server
->name
, server
->clients
, server
->description
);
2058 if (!server
->children
.used
)
2060 for (nn
=0; nn
<server
->children
.used
-1; nn
++) {
2061 trace_links(bot
, user
, server
->children
.list
[nn
], depth
<<1);
2063 trace_links(bot
, user
, server
->children
.list
[nn
], (depth
<<1)|1);
2066 static MODCMD_FUNC(cmd_stats_links
) {
2067 trace_links(cmd
->parent
->bot
, user
, self
, 1);
2072 static MODCMD_FUNC(cmd_stats_max
) {
2073 reply("OSMSG_MAX_CLIENTS", max_clients
, asctime(localtime(&max_clients_time
)));
2077 static MODCMD_FUNC(cmd_stats_network
) {
2078 struct helpfile_table tbl
;
2079 unsigned int nn
, tot_clients
;
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;
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);
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
]);
2108 free(tbl
.contents
[0]);
2113 static MODCMD_FUNC(cmd_stats_network2
) {
2114 struct helpfile_table tbl
;
2118 tbl
.length
= dict_size(servers
)+1;
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);
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
)));
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
;
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
]);
2151 free(tbl
.contents
[0]);
2156 static MODCMD_FUNC(cmd_stats_reserved
) {
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
));
2165 static MODCMD_FUNC(cmd_stats_trusted
) {
2167 struct trusted_host
*th
;
2168 char length
[INTERVALLEN
], issued
[INTERVALLEN
], limit
[32];
2170 reply("OSMSG_TRUSTED_LIST");
2171 reply("OSMSG_TRUSTED_LIST_BAR");
2172 reply("OSMSG_TRUSTED_LIST_HEADER");
2173 reply("OSMSG_TRUSTED_LIST_BAR");
2175 th
= dict_find(opserv_trusted_hosts
, argv
[1], NULL
);
2178 intervalString(issued
, now
- th
->issued
, user
->handle_info
);
2180 intervalString(length
, th
->expires
- now
, user
->handle_info
);
2182 sprintf(limit
, "%lu", th
->limit
);
2183 reply("OSMSG_HOST_IS_TRUSTED",
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>"));
2191 reply("OSMSG_HOST_NOT_TRUSTED", argv
[1]);
2194 for (it
= dict_first(opserv_trusted_hosts
); it
; it
= iter_next(it
)) {
2197 intervalString(issued
, now
- th
->issued
, user
->handle_info
);
2199 intervalString(length
, th
->expires
- now
, user
->handle_info
);
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>"));
2210 reply("OSMSG_TRUSTED_LIST_END");
2214 static MODCMD_FUNC(cmd_stats_uplink
) {
2215 extern struct cManagerNode cManager
;
2216 struct uplinkNode
*uplink
;
2218 uplink
= cManager
.uplink
;
2219 reply("OSMSG_UPLINK_START", uplink
->name
);
2220 reply("OSMSG_UPLINK_ADDRESS", uplink
->host
, uplink
->port
);
2224 static MODCMD_FUNC(cmd_stats_uptime
) {
2225 char uptime
[INTERVALLEN
];
2227 extern time_t boot_time
;
2228 extern int lines_processed
;
2229 static long clocks_per_sec
;
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)
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
;
2241 intervalString(uptime
, time(NULL
)-boot_time
, user
->handle_info
);
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
);
2250 static MODCMD_FUNC(cmd_stats_alerts
) {
2252 struct opserv_user_alert
*alert
;
2253 const char *reaction
;
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;
2271 reply("OSMSG_ALERT_IS", iter_key(it
), reaction
, alert
->owner
);
2272 reply("OSMSG_ALERTS_DESC", alert
->text_discrim
);
2274 reply("OSMSG_ALERT_END");
2278 static MODCMD_FUNC(cmd_stats_gags
) {
2279 struct gag_entry
*gag
;
2280 struct helpfile_table table
;
2284 reply("OSMSG_NO_GAGS");
2287 for (nn
=0, gag
=gagList
; gag
; nn
++, gag
=gag
->next
) ;
2288 table
.length
= nn
+1;
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
];
2300 intervalString(expstr
, gag
->expires
- now
, user
->handle_info
);
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
;
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
]);
2314 free(table
.contents
[0]);
2315 free(table
.contents
);
2319 static MODCMD_FUNC(cmd_stats_timeq
) {
2320 reply("OSMSG_TIMEQ_INFO", timeq_size(), timeq_next()-now
);
2325 static MODCMD_FUNC(cmd_stats_warn) {
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");
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
);
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
);
2358 static MODCMD_FUNC(cmd_dump
)
2360 char linedup
[MAXLEN
], original
[MAXLEN
];
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)) {
2367 reply("OSMSG_LINE_DUMPED");
2369 reply("OSMSG_RAW_PARSE_ERROR");
2373 static MODCMD_FUNC(cmd_raw
)
2375 char linedup
[MAXLEN
], original
[MAXLEN
];
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);
2383 reply("OSMSG_LINE_DUMPED");
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
)
2390 struct userNode
*resv
= GetUserH(nick
);
2392 if (IsService(resv
)) {
2393 reply("MSG_SERVICE_IMMUNE", resv
->nick
);
2396 if (resv
->handle_info
2397 && resv
->handle_info
->opserv_level
> user
->handle_info
->opserv_level
) {
2398 reply("OSMSG_LEVEL_TOO_LOW");
2402 if ((resv
= AddClone(nick
, ident
, host
, desc
))) {
2403 dict_insert(opserv_reserved_nick_dict
, resv
->nick
, resv
);
2408 static MODCMD_FUNC(cmd_collide
)
2410 struct userNode
*resv
;
2412 resv
= opserv_add_reserve(cmd
, user
, argv
[1], argv
[2], argv
[3], unsplit_string(argv
+4, argc
-4, NULL
));
2414 reply("OSMSG_COLLIDED_NICK", resv
->nick
);
2417 reply("OSMSG_CLONE_FAILED", argv
[1]);
2422 static MODCMD_FUNC(cmd_reserve
)
2424 struct userNode
*resv
;
2426 resv
= opserv_add_reserve(cmd
, user
, argv
[1], argv
[2], argv
[3], unsplit_string(argv
+4, argc
-4, NULL
));
2428 resv
->modes
|= FLAGS_PERSISTENT
;
2429 reply("OSMSG_RESERVED_NICK", resv
->nick
);
2432 reply("OSMSG_CLONE_FAILED", argv
[1]);
2438 free_reserve(char *nick
)
2440 struct userNode
*resv
;
2444 resv
= dict_find(opserv_reserved_nick_dict
, nick
, NULL
);
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
);
2456 static MODCMD_FUNC(cmd_unreserve
)
2458 if (free_reserve(argv
[1]))
2459 reply("OSMSG_NICK_UNRESERVED", argv
[1]);
2461 reply("OSMSG_NOT_RESERVED", argv
[1]);
2466 opserv_part_channel(void *data
)
2468 DelChannelUser(opserv
, data
, "Leaving.", 0);
2471 static int alert_check_user(const char *key
, void *data
, void *extra
);
2474 opserv_new_user_check(struct userNode
*user
)
2476 struct opserv_hostinfo
*ohi
;
2477 struct gag_entry
*gag
;
2478 char addr
[IRC_NTOP_MAX_SIZE
];
2480 /* Check to see if we should ignore them entirely. */
2481 if (IsLocal(user
) || IsService(user
))
2484 /* Check for alerts, and stop if we find one that kills them. */
2485 if (dict_foreach(opserv_user_alerts
, alert_check_user
, user
))
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
);
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
);
2503 userList_append(&ohi
->clients
, user
);
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
) {
2510 opserv_alert("Warning: Possible new-user flood.");
2517 if (checkDefCon(DEFCON_NO_NEW_CLIENTS
)) {
2518 irc_kill(opserv
, user
, DefConGlineReason
);
2522 if ( (checkDefCon(DEFCON_GLINE_NEW_CLIENTS
) || checkDefCon(DEFCON_SHUN_NEW_CLIENTS
)) && !IsOper(user
)) {
2523 char target
[IRC_NTOP_MAX_SIZE
+ 3] = { '*', '@', '\0' };
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);
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
;
2541 if (checkDefCon(DEFCON_REDUCE_SESSION
) && !th
)
2542 limit
= DefConSessionLimit
;
2545 /* 0 means unlimited hosts */
2546 } else if (ohi
->clients
.used
== limit
) {
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);
2561 opserv_user_cleanup(struct userNode
*user
, UNUSED_ARG(struct userNode
*killer
), UNUSED_ARG(const char *why
))
2563 struct opserv_hostinfo
*ohi
;
2564 char addr
[IRC_NTOP_MAX_SIZE
];
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
);
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
);
2582 opserv_bad_channel(const char *name
)
2587 dict_find(opserv_exempt_channels
, name
, &present
);
2591 if (gline_find(name
))
2594 for (found
=0; found
<opserv_bad_words
->used
; ++found
)
2595 if (irccasestr(name
, opserv_bad_words
->list
[found
]))
2602 opserv_shutdown_channel(struct chanNode
*channel
, const char *reason
)
2604 struct mod_chanmode
*change
;
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
))
2619 KickChannelUser(mNode
->user
, channel
, opserv
, user_find_message(mNode
->user
, reason
));
2621 timeq_add(now
+ opserv_conf
.purge_lock_delay
, opserv_part_channel
, channel
);
2625 opserv_channel_check(struct chanNode
*newchan
)
2629 if (!newchan
->join_policer
.params
) {
2630 newchan
->join_policer
.last_req
= now
;
2631 newchan
->join_policer
.params
= opserv_conf
.join_policer_params
;
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);
2639 /* Wait until the join check to shut channels down. */
2640 newchan
->bad_channel
= opserv_bad_channel(newchan
->name
);
2644 opserv_channel_delete(struct chanNode
*chan
)
2646 timeq_del(0, opserv_part_channel
, chan
, TIMEQ_IGNORE_WHEN
);
2650 opserv_join_check(struct modeNode
*mNode
)
2652 struct userNode
*user
= mNode
->user
;
2653 struct chanNode
*channel
= mNode
->channel
;
2656 if (IsService(user
))
2659 dict_foreach(opserv_channel_alerts
, alert_check_user
, user
);
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");
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
);
2675 if (user
->uplink
->burst
)
2677 if (policer_conforms(&channel
->join_policer
, now
, 1.0)) {
2678 channel
->join_flooded
= 0;
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
);
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
);
2701 opserv_alert("Warning: Possible join flood in %s (currently %d users).", channel
->name
, channel
->members
.used
);
2704 log_module(OS_LOG
, LOG_INFO
, "Join to %s during flood: "IDENT_FORMAT
, channel
->name
, IDENT_DATA(user
));
2709 opserv_add_bad_word(struct svccmd
*cmd
, struct userNode
*user
, const char *new_bad
) {
2710 unsigned int bad_idx
;
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
)) {
2716 reply("OSMSG_BAD_REDUNDANT", new_bad
, orig_bad
);
2718 } else if (irccasestr(orig_bad
, new_bad
)) {
2720 reply("OSMSG_BAD_GROWING", orig_bad
, new_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
))
2728 reply("OSMSG_BAD_NUKING", orig_bad
);
2729 string_list_delete(opserv_bad_words
, bad_idx
);
2736 string_list_append(opserv_bad_words
, strdup(new_bad
));
2738 reply("OSMSG_ADDED_BAD", new_bad
);
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
)
2745 struct routingPlanServer
*rps
;
2746 rps
= calloc(1, sizeof(*rps
));
2749 /* duplicate servers replace */
2750 rps
->uplink
= strdup(uplink
);
2752 rps
->secondaryuplink
= strdup(second
);
2754 rps
->secondaryuplink
= NULL
;
2755 rps
->port
= port
? port
: 4400; /* lame hardcodede default port. maybe get from config file somewhere? */
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
);
2764 free_routing_plan_server(void *data
)
2766 struct routingPlanServer
*rps
= data
;
2768 if(rps
->secondaryuplink
)
2769 free(rps
->secondaryuplink
);
2774 opserv_add_routing_plan(const char *name
)
2776 struct routingPlan
*rp
;
2777 rp
= calloc(1, sizeof(*rp
));
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
);
2785 dict_insert(opserv_routing_plans
, strdup(name
), rp
);
2786 /* TODO: check for duplicate */
2791 free_routing_plan(void *data
)
2793 struct routingPlan
*rp
= data
;
2794 /* delete all the servers attached to this plan */
2795 dict_delete(rp
->servers
);
2796 /* free the plan struct */
2800 /*************************************************
2801 * Functions to handle the active routing struct */
2804 *find_routeList_server(struct route
*route
, const char *server
)
2806 struct routeList
*rptr
;
2809 for(rptr
= route
->servers
;rptr
;rptr
=rptr
->next
) {
2810 if(!strcasecmp(rptr
->server
, server
))
2816 /* Wipes out the routing structure, freeing properly.
2817 * note: does NOT free itself, we just re-use it usually.*/
2819 wipe_route_list(struct route
*route
) {
2820 struct routeList
*nextptr
, *rptr
;
2823 for(rptr
= opserv_route
->servers
; rptr
; rptr
=nextptr
)
2825 nextptr
= rptr
->next
;
2829 if(rptr
->secondaryuplink
)
2830 free(rptr
->secondaryuplink
);
2833 route
->centered
= true;
2835 route
->maxdepth
= 0;
2836 route
->servers
= NULL
;
2841 rank_outside_rec(struct route
*route
, char *server
, int count
)
2843 struct routeList
*rptr
;
2846 if(count
> 256) { /* XXX: 256 becomes max # of servers this works with, whats the real #? */
2849 for(rptr
= route
->servers
; rptr
; rptr
= rptr
->next
) {
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 */
2860 if((rptr
= find_routeList_server(route
, server
))) {
2861 rptr
->outsideness
= max
;
2865 log_module(MAIN_LOG
, LOG_ERROR
, "routing struct rank_outsideness() couldnt find %s", server
);
2871 rank_outsideness(struct route
*route
)
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
);
2884 /* Add servers to the routing structure */
2886 add_routestruct_server(struct route
*route
, const char *server
, unsigned int port
, char *uplink
, char *secondary
)
2888 struct routeList
*rptr
;
2890 if(find_routeList_server(route
, server
))
2892 log_module(MAIN_LOG
, LOG_WARNING
, "Routing structure add server Skipping duplicate [%s]. This should never really be possible.", server
);
2895 rptr
= calloc(1, sizeof(*rptr
));
2896 rptr
->server
= strdup(server
);
2899 hname
= conf_get_data("server/hostname", RECDB_QSTRING
);
2902 rptr
->uplink
= strdup(uplink
);
2904 rptr
->secondaryuplink
= strdup(secondary
);
2905 /* tack this server on the front of the list */
2906 rptr
->next
= route
->servers
;
2907 route
->servers
= rptr
;
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. */
2923 /* Recenter the routing struct around our current uplink */
2925 change_route_uplinks(struct route
*route
)
2927 struct routeList
*rptr
;
2928 char lastserver
[MAXLEN
];
2929 char nextserver
[MAXLEN
];
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
);
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;
2942 if(!strcasecmp(rptr
->uplink
, servicename
)) {
2943 log_module(MAIN_LOG
, LOG_DEBUG
, "Already centered");
2945 else { /* else, center it */
2947 strcpy(nextserver
, rptr
->uplink
);
2948 log_module(MAIN_LOG
, LOG_DEBUG
, "change_route_uplinks() changing %s uplink to %s.", rptr
->server
, lastserver
);
2950 rptr
->uplink
= strdup(lastserver
);
2951 strcpy(lastserver
, rptr
->server
);
2952 rptr
= find_routeList_server(route
, nextserver
);
2955 if(rank_outsideness(route
) > 0) {
2956 route
->centered
= true;
2964 activate_routing(struct svccmd
*cmd
, struct userNode
*user
, char *plan_name
)
2966 static struct routingPlan
*rp
;
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");
2977 rp
= dict_find(opserv_routing_plans
, plan_name
, NULL
);
2980 reply("OSMSG_PLAN_NOT_FOUND", plan_name
);
2982 /* since it doesnt exist, remove the active setting */
2983 dict_remove(opserv_routing_plan_options
, plan_name
);
2985 log_module(MAIN_LOG
, LOG_ERROR
, "activate_routing() couldnt find active routing plan!");
2990 else { /* find the active plan in settings */
2991 plan_name
= dict_find(opserv_routing_plan_options
, "ACTIVE", NULL
);
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
);
2999 karma
= dict_find(opserv_routing_plan_options
, "KARMA", NULL
);
3001 rp
= dict_find(opserv_routing_plans
, plan_name
, NULL
);
3003 /* this should really be done during opserv init */
3005 opserv_route
= calloc(1, sizeof(*opserv_route
));
3007 /* Delete the existing active route */
3008 wipe_route_list(opserv_route
);
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
);
3019 /* If the normal uplink has bad karma, don't use it as a hub,
3020 * switch to the secondary uplink.
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
;
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.
3035 if(karma
&& enabled_string(karma
) && (rps
->karma
< 0 || rps
->offline
) ) {
3036 if(rps
->secondaryuplink
) {
3037 uplink
= rps
->secondaryuplink
;
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
);
3043 if(change_route_uplinks(opserv_route
))
3046 reply("OSMSG_ROUTING_ACTIVATION_ERROR");
3047 activate_routing(cmd
, user
, "*");
3053 /*******************************************************
3054 * Functions to handle online route configuration via opserv
3056 static void route_show_option(struct svccmd
*cmd
, struct userNode
*user
, char *name
)
3058 char *value
= dict_find(opserv_routing_plan_options
, name
, NULL
);
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
));
3064 else if(!strcmp("ACTIVE", name
)) {
3065 if(opserv_route
&& opserv_route
->servers
)
3066 reply("OSMSG_ROUTINGPLAN_ACTIVE", value
);
3068 reply("OSMSG_ROUTINGPLAN_OPTION_NOT_SET", name
);
3071 reply("OSMSG_ROUTINGPLAN_OPTION", name
, value
);
3075 reply("OSMSG_ROUTINGPLAN_OPTION_NOT_SET", name
);
3079 static void route_show_options(struct svccmd
*cmd
, struct userNode
*user
)
3081 char *options
[] = {"ACTIVE", "RETRY_PERIOD", "CONN_PINGOUT", "CONN_READERROR", "KARMA", "DEFAULT_PORT", NULL
};
3083 for(i
= 0; options
[i
]; i
++) {
3084 route_show_option(cmd
, user
, options
[i
]);
3088 /* called from timeq */
3089 void routing_connect_timeout(void *data
)
3091 struct waitingConnection
*wc
= data
;
3092 struct server
*target
= GetServerH(wc
->target
);
3094 dict_remove(opserv_waiting_connections
, wc
->server
);
3095 return; /* server we wanted to connect new server to is gone, just give up */
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
);
3102 void routing_delete_connect_timer(char *server
)
3104 struct waitingConnection
*wc
= dict_find(opserv_waiting_connections
, server
, 0);
3106 timeq_del(0, routing_connect_timeout
, wc
, TIMEQ_IGNORE_WHEN
);
3107 dict_remove(opserv_waiting_connections
, server
);
3113 routing_connect_server(char *server
, int port
, struct server
*to
)
3115 struct waitingConnection
*wc
= calloc(sizeof(*wc
), 1);
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..
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
);
3126 irc_connect(opserv
, server
, port
, to
);
3130 routing_connect_one(struct route
*route
, char *server
)
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
);
3140 return 1; /* already linked */
3142 routing_connect_server(rptr
->server
, rptr
->port
, suptr
);
3143 return 1; /* attempted link */
3145 return 0; /* its uplink isnt here to link to */
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. */
3152 int routing_connect_children(struct route
*route
, char *server
)
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
);
3162 continue; /* already linked */
3164 routing_connect_server(rptr
->server
, rptr
->port
, suptr
);
3165 continue; /* attempted link */
3167 continue; /* its uplink isnt here to link to */
3170 return 1; /* server wasnt found in active route struct ?! */
3173 int reroute(struct route
*route
, struct userNode
*user
, struct svccmd
*cmd
, char *directive
)
3175 struct routeList
*rptr
;
3176 struct server
*sptr
, *suptr
;
3177 int connect
= 0, move
= 0, missing
= 0, i
;
3178 char d
= toupper(*directive
);
3180 if(!route
|| !route
->servers
) {
3181 reply("OSMSG_REROUTING_NOTCONFIGURED");
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");
3189 else if(d
== 'C') { /* only connect */
3190 reply("OSMSG_CONNECTING_MISSING_ONLY");
3192 else if(d
== 'T') { /* test */
3193 reply("OSMSG_TESTING_REROUTE");
3197 reply("OSMSG_INVALID_DIRECTIVE", directive
);
3201 for(i
= 0; i
<= route
->maxdepth
-1; i
++) {
3202 for(rptr
= route
->servers
; rptr
; rptr
= rptr
->next
) {
3203 if(rptr
->outsideness
== i
) {
3206 reply("OSMSG_INSPECTING_SERVER", rptr
->server
);
3207 suptr
= GetServerH(rptr
->uplink
);
3209 if(rptr
->secondaryuplink
&& (suptr
= GetServerH(rptr
->secondaryuplink
))) {
3211 reply("OSMSG_COULDNT_FIND_SERVER", rptr
->uplink
, rptr
->secondaryuplink
, rptr
->server
);
3214 if(suptr
) { /* if the proper uplink is connected.. */
3215 sptr
= GetServerH(rptr
->server
);
3216 if(d
== 'C' && sptr
) {
3217 continue; /* Already linked */
3219 /* If server is missing or the uplinks are not the same then... */
3220 else if(!sptr
|| strcasecmp(sptr
->uplink
->name
, rptr
->uplink
)) {
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
);
3229 else { /* just pretend */
3230 reply("OSMSG_SQUIT", rptr
->server
);
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
);
3242 log_module(MAIN_LOG
, LOG_DEBUG
, "server uplink %s was not found, cant connect %s", rptr
->uplink
, rptr
->server
);
3245 } /* outsideness = 1 */
3248 if(user
) { /* report on what we did */
3249 if(!strcasecmp(directive
, "C")) {
3251 reply("OSMSG_CONNECTING_MISSING", connect
);
3253 reply("OSMSG_NO_SERVERS_MISSING");
3256 if(move
+connect
> 0)
3257 reply("OSMSG_REROUTE_COMPLETE", move
, connect
, move
+connect
);
3259 reply("OSMSG_NO_ROUTING_NECESSARY");
3261 reply("OSMSG_UPLINKS_MISSING", missing
);
3264 return(move
+connect
);
3267 static MODCMD_FUNC(cmd_reroute
) {
3270 if(reroute(opserv_route
, user
, cmd
, upper
))
3276 /* reroute_timer(run)
3277 * run - if it is null, just setup the timer
3278 * but dont run reroute now. otherwise reroute
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
);
3286 return; /* retry_period invalid */
3287 unsigned int freq
= atoi(retry_period
);
3289 return; /* retry_period set to 0, disable */
3291 /* Do the reroute C attempt */
3293 reroute(opserv_route
, NULL
, NULL
, "C");
3295 /* Re-add ourselves to the timer queue */
3296 timeq_add(now
+ freq
, reroute_timer
, "run");
3299 void routing_change_karma(struct routingPlanServer
*rps
, const char *server
, int change
) {
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
);
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
);
3320 void routing_karma_timer(void *data
) {
3322 time_t timer_init
= data
? atoi(data
) : 0;
3325 log_module(MAIN_LOG
, LOG_DEBUG
, "routing_karma_timer() is running. timer_init=%d.", (unsigned int) timer_init
);
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
))) {
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
);
3349 if(timer_init
> now
) /* loading a saved value */
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
);
3361 void routing_handle_neg_karma(char *server
, char *uplink
, int change
)
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
;
3370 if(!(rp
= dict_find(opserv_routing_plans
, active
, NULL
)))
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
);
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
);
3389 routing_handle_squit(char *server
, char *uplink
, char *message
)
3391 log_module(MAIN_LOG
, LOG_DEBUG
, "Routing_handle_squit(%s, %s)", server
, message
);
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. */
3399 val
= dict_find(opserv_routing_plan_options
, "CONN_PINGOUT", 0);
3400 if(val
&& enabled_string(val
))
3401 routing_connect_one(opserv_route
, server
);
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
);
3410 /* Else whats the message (an oper squit it?) dont interfere */
3414 routing_handle_connect(char *server
, char *uplink
)
3417 struct routingPlan
*rp
;
3418 struct routingPlanServer
*rps
;
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
);
3428 rp
= dict_find(opserv_routing_plans
, active
, NULL
);
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
);
3441 /* if there are any servers missing who have this server as uplink try to connect them. */
3442 routing_connect_children(opserv_route
, server
);
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
);
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
3459 routing_handle_connect_failure(struct server
*source
, char *server
, char *message
)
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
);
3471 rp
= dict_find(opserv_routing_plans
, active
, NULL
);
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. */
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
)
3499 timeq_del(0, reroute_timer
, NULL
, TIMEQ_IGNORE_DATA
& TIMEQ_IGNORE_WHEN
);
3500 timeq_add(now
+ time
, reroute_timer
, "run");
3503 static MODCMD_FUNC(cmd_routing_set
)
3505 char *option
= argv
[1];
3506 char *options
[] = {"ACTIVE", "RETRY_PERIOD", "CONN_PINGOUT", "CONN_READERROR", "KARMA", "DEFAULT_PORT", NULL
};
3509 route_show_options(cmd
, user
);
3512 char *found_option
= NULL
;
3513 for(i
= 0; options
[i
]; i
++) {
3514 if(!strcasecmp(options
[i
], option
))
3515 found_option
= options
[i
];
3518 reply("OSMSG_ROUTINGPLAN_OPTION_NOT_FOUND", option
);
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");
3531 else if(!activate_routing(cmd
, user
, value
)) {
3532 /* neg reply handled in activate_routing */
3536 if(!strcmp(found_option
, "CONN_READERROR") || !strcmp(found_option
, "CONN_PINGOUT") ||
3537 !strcmp(found_option
, "KARMA") ) {
3538 if( enabled_string(value
)) {
3541 else if( disabled_string(value
) ) {
3545 reply("MSG_INVALID_BINARY", value
);
3549 if(!strcmp(found_option
, "RETRY_PERIOD")) {
3550 unsigned int duration
= ParseInterval(value
);
3551 sprintf(buff
, "%d", duration
);
3553 reroute_timer_reset(duration
);
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
);
3561 /* show the current value */
3562 route_show_option(cmd
, user
, found_option
);
3568 static MODCMD_FUNC(cmd_stats_routing_plans
) {
3569 dict_iterator_t rpit
;
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");
3585 reply("OSMSG_ROUTINGPLAN_END");
3586 route_show_options(cmd
, user
);
3591 static MODCMD_FUNC(cmd_routing_addplan
)
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
);
3602 reply("OSMSG_ADDPLAN_FAILED", name
);
3608 reply("OSMSG_INVALID_PLAN");
3613 static MODCMD_FUNC(cmd_routing_delplan
)
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");
3623 reply("OSMSG_PLAN_DELETED");
3627 reply("OSMSG_PLAN_NOT_FOUND", name
);
3632 static MODCMD_FUNC(cmd_routing_addserver
)
3640 struct routingPlan
*rp
;
3643 server
= strdup(argv
[2]);
3644 server
= strtok(server
, ":");
3645 portstr
= strtok(NULL
, ":");
3647 port
= atoi(portstr
);
3649 char *str
= dict_find(opserv_routing_plan_options
, "DEFAULT_PORT", NULL
);
3651 port
= str
? atoi(str
) : 0;
3659 if( (rp
= dict_find(opserv_routing_plans
, plan
, 0))) {
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
);
3672 reply("OSMSG_PLAN_NOT_FOUND", plan
);
3678 static MODCMD_FUNC(cmd_routing_delserver
)
3682 struct routingPlan
*rp
;
3685 if( (rp
= dict_find(opserv_routing_plans
, plan
, 0))) {
3686 if(dict_remove(rp
->servers
, server
)) {
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
);
3697 reply("OSMSG_PLAN_SERVER_NOT_FOUND", server
);
3702 reply("OSMSG_PLAN_NOT_FOUND", plan
);
3708 /*************************************************
3709 * Functions to deal with 'route map' command */
3711 /* Figures out how many downlinks there are for proper
3712 * drawing of the route map */
3714 num_route_downlinks(struct route
*route
, char *name
)
3716 struct routeList
*rptr
;
3718 rptr
= route
->servers
;
3720 if(!strcasecmp(rptr
->uplink
, name
))
3728 show_route_downlinks(struct svccmd
*cmd
, struct route
*route
, struct userNode
*user
, char *name
, char *prevpre
, char *arrowchar
, int reset
)
3730 struct routeList
*servPtr
;
3731 struct server
*sptr
;
3737 static int depth
= 0;
3742 nextpre
= malloc(MAXLEN
);
3743 strcpy(pre
, prevpre
);
3745 sptr
= GetServerH(name
);
3746 if((servPtr
= find_routeList_server(route
, name
))) {
3749 else if (!strcasecmp(sptr
->uplink
->name
, servPtr
->uplink
))
3751 else if(servPtr
->secondaryuplink
&& !strcasecmp(sptr
->name
, servPtr
->secondaryuplink
))
3755 reply("OSMSG_DOWNLINKS_FORMAT_A", pre
, arrowchar
, name
, status
);
3758 reply("OSMSG_DOWNLINKS_FORMAT_B", self
->name
);
3759 j
= num_route_downlinks(route
, name
);
3760 servPtr
= route
->servers
;
3762 if(!strcasecmp(servPtr
->uplink
, name
)) {
3763 strcpy(nextpre
, pre
);
3765 if(arrowchar
[0] == '`')
3766 strcat(nextpre
, " ");
3768 strcat(nextpre
, "| ");
3771 show_route_downlinks(cmd
, route
, user
, servPtr
->server
, nextpre
, "|", 0);
3774 show_route_downlinks(cmd
, route
, user
, servPtr
->server
, nextpre
, "`", 0);
3777 servPtr
= servPtr
->next
;
3783 show_route_map(struct route
*route
, struct userNode
*user
, struct svccmd
*cmd
)
3785 if(!route
|| !route
->servers
) {
3786 reply("OSMSG_ROUTELIST_EMPTY");
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
);
3797 static MODCMD_FUNC(cmd_routing_map
)
3799 show_route_map(opserv_route
, user
, cmd
);
3806 /* End of auto routing functions *
3807 *********************************/
3809 static MODCMD_FUNC(cmd_addbad
)
3811 unsigned int arg
, count
;
3813 int bad_found
, exempt_found
;
3815 /* Create the bad word if it doesn't exist. */
3816 bad_found
= !opserv_add_bad_word(cmd
, user
, argv
[1]);
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");
3823 reply("MSG_MISSING_PARAMS", "except");
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
);
3833 reply("OSMSG_ADDED_EXEMPTIONS", count
);
3835 reply("MSG_DEPRECATED_COMMAND", "addbad (with modifiers)", "addbad");
3836 reply("OSMSG_BAD_MODIFIER", argv
[arg
]);
3840 /* Scan for existing channels that match the new bad word. */
3842 for (it
= dict_first(channels
); it
; it
= iter_next(it
)) {
3843 struct chanNode
*channel
= iter_data(it
);
3845 if (!opserv_bad_channel(channel
->name
))
3847 channel
->bad_channel
= 1;
3848 if (channel
->name
[0] == '#')
3849 opserv_shutdown_channel(channel
, "OSMSG_ILLEGAL_REASON");
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");
3863 static MODCMD_FUNC(cmd_delbad
)
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;
3880 reply("OSMSG_REMOVED_BAD", argv
[1]);
3884 reply("OSMSG_NOT_BAD_WORD", argv
[1]);
3888 static MODCMD_FUNC(cmd_addexempt
)
3890 const char *chanName
;
3892 if ((argc
> 1) && IsChannelName(argv
[1])) {
3895 reply("MSG_NOT_CHANNEL_NAME");
3899 dict_insert(opserv_exempt_channels
, strdup(chanName
), NULL
);
3900 channel
= GetChannel(chanName
);
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
);
3906 channel
->bad_channel
= 0;
3908 reply("OSMSG_ADDED_EXEMPTION", chanName
);
3912 static MODCMD_FUNC(cmd_delexempt
)
3914 const char *chanName
;
3916 if ((argc
> 1) && IsChannelName(argv
[1])) {
3919 reply("MSG_NOT_CHANNEL_NAME");
3923 if (!dict_remove(opserv_exempt_channels
, chanName
)) {
3924 reply("OSMSG_NOT_EXEMPT", chanName
);
3927 reply("OSMSG_REMOVED_EXEMPTION", chanName
);
3932 opserv_expire_trusted_host(void *data
)
3934 struct trusted_host
*th
= data
;
3935 dict_remove(opserv_trusted_hosts
, th
->ipaddr
);
3939 opserv_add_trusted_host(const char *ipaddr
, unsigned int limit
, const char *issuer
, time_t issued
, time_t expires
, const char *reason
)
3941 struct trusted_host
*th
;
3942 th
= calloc(1, sizeof(*th
));
3945 th
->ipaddr
= strdup(ipaddr
);
3946 th
->reason
= reason
? strdup(reason
) : NULL
;
3947 th
->issuer
= issuer
? strdup(issuer
) : NULL
;
3948 th
->issued
= issued
;
3950 th
->expires
= expires
;
3951 dict_insert(opserv_trusted_hosts
, th
->ipaddr
, th
);
3953 timeq_add(th
->expires
, opserv_expire_trusted_host
, th
);
3957 free_trusted_host(void *data
)
3959 struct trusted_host
*th
= data
;
3966 static MODCMD_FUNC(cmd_addtrust
)
3968 unsigned long interval
;
3970 irc_in_addr_t tmpaddr
;
3973 if (dict_find(opserv_trusted_hosts
, argv
[1], NULL
)) {
3974 reply("OSMSG_ALREADY_TRUSTED", argv
[1]);
3978 if (!irc_pton(&tmpaddr
, NULL
, argv
[1])) {
3979 reply("OSMSG_BAD_IP", argv
[1]);
3983 count
= strtoul(argv
[2], &tmp
, 10);
3985 reply("OSMSG_BAD_NUMBER", argv
[2]);
3989 interval
= ParseInterval(argv
[3]);
3990 if (!interval
&& strcmp(argv
[3], "0")) {
3991 reply("MSG_INVALID_DURATION", argv
[3]);
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");
4001 static MODCMD_FUNC(cmd_edittrust
)
4003 unsigned long interval
;
4004 struct trusted_host
*th
;
4008 th
= dict_find(opserv_trusted_hosts
, argv
[1], NULL
);
4010 reply("OSMSG_NOT_TRUSTED", argv
[1]);
4013 count
= strtoul(argv
[2], &tmp
, 10);
4014 if (!count
|| *tmp
) {
4015 reply("OSMSG_BAD_NUMBER", argv
[2]);
4018 interval
= ParseInterval(argv
[3]);
4019 if (!interval
&& strcmp(argv
[3], "0")) {
4020 reply("MSG_INVALID_DURATION", argv
[3]);
4023 reason
= unsplit_string(argv
+4, argc
-4, NULL
);
4025 timeq_del(th
->expires
, opserv_expire_trusted_host
, th
, 0);
4028 th
->reason
= strdup(reason
);
4030 th
->issuer
= strdup(user
->handle_info
->handle
);
4034 th
->expires
= now
+ interval
;
4035 timeq_add(th
->expires
, opserv_expire_trusted_host
, th
);
4038 reply("OSMSG_UPDATED_TRUSTED", th
->ipaddr
);
4042 static MODCMD_FUNC(cmd_deltrust
)
4046 for (n
=1; n
<argc
; n
++) {
4047 struct trusted_host
*th
= dict_find(opserv_trusted_hosts
, argv
[n
], NULL
);
4051 timeq_del(th
->expires
, opserv_expire_trusted_host
, th
, 0);
4052 dict_remove(opserv_trusted_hosts
, argv
[n
]);
4054 reply("OSMSG_REMOVED_TRUSTED");
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).
4061 static MODCMD_FUNC(cmd_clone
)
4064 struct userNode
*clone
;
4066 clone
= GetUserH(argv
[2]);
4067 if (!irccasecmp(argv
[1], "ADD")) {
4069 char ident
[USERLEN
+1];
4072 reply("MSG_MISSING_PARAMS", argv
[1]);
4077 reply("OSMSG_CLONE_EXISTS", argv
[2]);
4080 userinfo
= unsplit_string(argv
+4, argc
-4, NULL
);
4081 for (i
=0; argv
[3][i
] && (i
<USERLEN
); i
++) {
4082 if (argv
[3][i
] == '@') {
4086 ident
[i
] = argv
[3][i
];
4089 if (!argv
[3][i
] || (i
==USERLEN
)) {
4090 reply("OSMSG_NOT_A_HOSTMASK");
4093 if (!(clone
= AddClone(argv
[2], ident
, argv
[3]+i
, userinfo
))) {
4094 reply("OSMSG_CLONE_FAILED", argv
[2]);
4097 reply("OSMSG_CLONE_ADDED", clone
->nick
);
4101 reply("MSG_NICK_UNKNOWN", argv
[2]);
4104 if (clone
->uplink
!= self
|| IsService(clone
)) {
4105 reply("OSMSG_NOT_A_CLONE", clone
->nick
);
4108 if (!irccasecmp(argv
[1], "REMOVE")) {
4111 reason
= unsplit_string(argv
+3, argc
-3, NULL
);
4114 tmp
= alloca(strlen(clone
->nick
) + strlen(OSMSG_PART_REASON
));
4115 sprintf(tmp
, OSMSG_PART_REASON
, clone
->nick
);
4118 DelUser(clone
, NULL
, 1, reason
);
4119 reply("OSMSG_CLONE_REMOVED", argv
[2]);
4123 reply("MSG_MISSING_PARAMS", argv
[1]);
4127 channel
= GetChannel(argv
[3]);
4128 if (!irccasecmp(argv
[1], "JOIN")) {
4130 && !(channel
= AddChannel(argv
[3], now
, NULL
, NULL
, NULL
))) {
4131 reply("MSG_CHANNEL_UNKNOWN", argv
[3]);
4134 AddChannelUser(clone
, channel
);
4135 reply("OSMSG_CLONE_JOINED", clone
->nick
, channel
->name
);
4138 if (!irccasecmp(argv
[1], "PART")) {
4140 reply("MSG_CHANNEL_UNKNOWN", argv
[3]);
4143 if (!GetUserMode(channel
, clone
)) {
4144 reply("OSMSG_NOT_ON_CHANNEL", clone
->nick
, channel
->name
);
4147 reply("OSMSG_CLONE_PARTED", clone
->nick
, channel
->name
);
4148 DelChannelUser(clone
, channel
, "Leaving.", 0);
4151 if (!irccasecmp(argv
[1], "OP")) {
4152 struct mod_chanmode change
;
4154 reply("MSG_CHANNEL_UNKNOWN", argv
[3]);
4157 mod_chanmode_init(&change
);
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
);
4165 modcmd_chanmode_announce(&change
);
4166 reply("OSMSG_OPS_GIVEN", channel
->name
, clone
->nick
);
4169 if (!irccasecmp(argv
[1], "HOP")) {
4170 struct mod_chanmode change
;
4172 reply("MSG_CHANNEL_UNKNOWN", argv
[3]);
4175 mod_chanmode_init(&change
);
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
);
4183 modcmd_chanmode_announce(&change
);
4184 reply("OSMSG_HOPS_GIVEN", channel
->name
, clone
->nick
);
4188 reply("MSG_MISSING_PARAMS", argv
[1]);
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]);
4198 reply("OSMSG_UNKNOWN_SUBCOMMAND", argv
[1], argv
[0]);
4202 static struct helpfile_expansion
4203 opserv_help_expand(const char *variable
)
4205 extern struct userNode
*message_source
;
4206 struct helpfile_expansion exp
;
4207 struct service
*service
;
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
);
4235 exp
.value
.table
.contents
[row
][1] = level_strings
[level
];
4237 } else if (!strncasecmp(variable
, "level", 5)) {
4238 cmd
= dict_find(service
->commands
, variable
+6, NULL
);
4239 exp
.type
= HF_STRING
;
4241 level
= cmd
->min_opserv_level
;
4242 exp
.value
.str
= malloc(16);
4243 snprintf(exp
.value
.str
, 16, "%3d", level
);
4245 exp
.value
.str
= NULL
;
4248 exp
.type
= HF_STRING
;
4249 exp
.value
.str
= NULL
;
4255 opserv_define_func(const char *name
, modcmd_func_t
*func
, int min_level
, int reqchan
, int min_argc
)
4257 char buf
[16], *flags
= NULL
;
4258 unsigned int iflags
= 0;
4259 sprintf(buf
, "%d", min_level
);
4261 case 1: flags
= "+acceptchan"; break;
4262 case 3: flags
= "+acceptpluschan"; /* fall through */
4263 case 2: iflags
= MODCMD_REQUIRE_CHANNEL
; break;
4266 return modcmd_register(opserv_module
, name
, func
, min_argc
, iflags
, "level", buf
, "flags", flags
, "flags", "+oper", NULL
);
4268 return modcmd_register(opserv_module
, name
, func
, min_argc
, iflags
, "level", buf
, "flags", "+oper", NULL
);
4272 int add_reserved(const char *key
, void *data
, void *extra
)
4274 struct chanNode
*chan
;
4275 struct record_data
*rd
= data
;
4276 const char *ident
, *hostname
, *desc
;
4278 struct userNode
*reserve
;
4279 ident
= database_get_data(rd
->d
.object
, KEY_IDENT
, RECDB_QSTRING
);
4281 log_module(OS_LOG
, LOG_ERROR
, "Missing ident for reserve of %s", key
);
4284 hostname
= database_get_data(rd
->d
.object
, KEY_HOSTNAME
, RECDB_QSTRING
);
4286 log_module(OS_LOG
, LOG_ERROR
, "Missing hostname for reserve of %s", key
);
4289 desc
= database_get_data(rd
->d
.object
, KEY_DESC
, RECDB_QSTRING
);
4291 log_module(OS_LOG
, LOG_ERROR
, "Missing description for reserve of %s", key
);
4294 if ((reserve
= AddClone(key
, ident
, hostname
, desc
))) {
4295 reserve
->modes
|= FLAGS_PERSISTENT
;
4296 dict_insert(extra
, reserve
->nick
, reserve
);
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
;
4310 foreach_matching_user(const char *hostmask
, discrim_search_func func
, void *extra
)
4314 unsigned int matched
;
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
);
4334 log_module(OS_LOG
, LOG_ERROR
, "Couldn't split IRC mask for gag %s!", hostmask
);
4343 gag_free(struct gag_entry
*gag
)
4345 unsigned int ungagged
;
4347 /* Remove from gag list */
4348 if (gagList
== gag
) {
4349 gagList
= gag
->next
;
4351 struct gag_entry
*prev
;
4352 for (prev
= gagList
; prev
->next
!= gag
; prev
= prev
->next
) ;
4353 prev
->next
= gag
->next
;
4356 ungagged
= foreach_matching_user(gag
->mask
, ungag_helper_func
, NULL
);
4358 /* Deallocate storage */
4368 gag_expire(void *data
)
4374 gag_create(const char *mask
, const char *owner
, const char *reason
, time_t expires
)
4376 struct gag_entry
*gag
;
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
;
4385 timeq_add(gag
->expires
, gag_expire
, gag
);
4386 gag
->next
= gagList
;
4389 /* If we're linked, see if who the gag applies to */
4390 return foreach_matching_user(mask
, gag_helper_func
, gag
);
4394 add_gag_helper(const char *key
, void *data
, UNUSED_ARG(void *extra
))
4396 struct record_data
*rd
= data
;
4397 char *owner
, *reason
, *expstr
;
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
);
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
)
4413 char *wordv
[MAXNUMPARAMS
], *discrim_copy
;
4414 struct opserv_user_alert
*alert
;
4417 if (dict_find(opserv_user_alerts
, name
, NULL
)) {
4418 send_message(req
, opserv
, "OSMSG_ALERT_EXISTS", name
);
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
);
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.
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
);
4453 add_chan_warn(const char *key, void *data, UNUSED_ARG(void *extra))
4455 struct record_data *rd = data;
4456 char *reason = GET_RECORD_QSTRING(rd);
4458 * i hope this can't happen *
4460 reason = "No Reason";
4462 dict_insert(opserv_chan_warn, strdup(key), strdup(reason));
4469 add_user_alert(const char *key
, void *data
, UNUSED_ARG(void *extra
))
4472 const char *discrim
, *react
, *owner
;
4473 opserv_alert_reaction reaction
;
4474 struct opserv_user_alert
*alert
;
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
);
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
;
4487 else if (!irccasecmp(react, "silent"))
4488 reaction = REACT_SILENT;
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
;
4499 log_module(OS_LOG
, LOG_ERROR
, "Invalid reaction %s for alert %s.", react
, key
);
4502 alert
= opserv_add_user_alert(opserv
, key
, reaction
, discrim
);
4504 log_module(OS_LOG
, LOG_ERROR
, "Unable to create alert %s from database.", key
);
4507 owner
= database_get_data(alert_dict
, KEY_OWNER
, RECDB_QSTRING
);
4509 alert
->owner
= strdup(owner
? owner
: "<unknown>");
4514 trusted_host_read(const char *host
, void *data
, UNUSED_ARG(void *extra
))
4516 struct record_data
*rd
= data
;
4517 const char *limit
, *str
, *reason
, *issuer
;
4518 time_t issued
, expires
;
4520 if (rd
->type
== RECDB_QSTRING
) {
4521 /* old style host by itself */
4522 limit
= GET_RECORD_QSTRING(rd
);
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;
4540 if (expires
&& (expires
< now
))
4542 opserv_add_trusted_host(host
, (limit
? strtoul(limit
, NULL
, 0) : 0), issuer
, issued
, expires
, reason
);
4547 add_routing_plan_server(const char *name
, void *data
, void *rp
)
4549 struct record_data
*rd
= data
;
4550 const char *uplink
, *portstr
, *karma
, *second
, *offline
;
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);
4569 routing_plan_set_option(const char *name
, void *data
, UNUSED_ARG(void *extra
))
4571 struct record_data
*rd
= data
;
4572 if(rd
->type
== RECDB_QSTRING
)
4574 char *value
= GET_RECORD_QSTRING(rd
);
4575 dict_insert(opserv_routing_plan_options
, strdup(name
), strdup(value
));
4581 add_routing_plan(const char *name
, void *data
, UNUSED_ARG(void *extra
))
4583 struct record_data
*rd
= data
;
4584 struct routingPlan
*rp
;
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
);
4595 opserv_saxdb_read(struct dict
*conf_db
)
4598 struct record_data
*rd
;
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
))) {
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
]);
4612 for (it
=dict_first(rd
->d
.object
); it
; it
=iter_next(it
)) {
4613 opserv_add_bad_word(NULL
, NULL
, iter_key(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
);
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
);
4629 if ((object
= database_get_data(conf_db
, KEY_MAX_CLIENTS
, RECDB_OBJECT
))) {
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
);
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
);
4643 if ((object = database_get_data(conf_db, KEY_WARN, RECDB_OBJECT)))
4644 dict_foreach(object, add_chan_warn, NULL);
4647 if ((object
= database_get_data(conf_db
, KEY_ROUTINGPLAN
, RECDB_OBJECT
)))
4648 dict_foreach(object
, add_routing_plan
, NULL
);
4650 if ((object
= database_get_data(conf_db
, KEY_ROUTINGPLAN_OPTIONS
, RECDB_OBJECT
)))
4651 dict_foreach(object
, routing_plan_set_option
, NULL
);
4657 opserv_saxdb_write(struct saxdb_context
*ctx
)
4659 struct string_list
*slist
;
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
);
4674 saxdb_end_record(ctx
);
4677 if (opserv_bad_words
->used
) {
4678 saxdb_write_string_list(ctx
, KEY_BAD_WORDS
, opserv_bad_words
);
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
));
4686 saxdb_end_record(ctx
);
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
)) {
4696 saxdb_start_record(ctx
, iter_key(it
), 0);
4697 for(svrit
= dict_first(rp
->servers
); svrit
; svrit
= iter_next(svrit
)) {
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
);
4712 saxdb_end_record(ctx
);
4714 saxdb_end_record(ctx
);
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
)));
4722 saxdb_write_string_list(ctx
, KEY_EXEMPT_CHANNELS
, slist
);
4723 free_string_list(slist
);
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
);
4738 saxdb_end_record(ctx
);
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
);
4751 saxdb_end_record(ctx
);
4753 /* channel warnings */
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));
4760 saxdb_end_record(ctx);
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;
4782 log_module(OS_LOG
, LOG_ERROR
, "Invalid reaction type %d for alert %s (while writing database).", alert
->reaction
, iter_key(it
));
4785 if (reaction
) saxdb_write_string(ctx
, KEY_REACTION
, reaction
);
4786 saxdb_end_record(ctx
);
4788 saxdb_end_record(ctx
);
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
);
4799 query_keys_helper(const char *key
, UNUSED_ARG(void *data
), void *extra
)
4801 send_message_type(4, extra
, opserv
, "$b%s$b", key
);
4805 static MODCMD_FUNC(cmd_query
)
4807 struct record_data
*rd
;
4812 reply("OSMSG_OPTION_ROOT");
4813 conf_enum_root(query_keys_helper
, user
);
4817 nodename
= unsplit_string(argv
+1, argc
-1, NULL
);
4818 if (!(rd
= conf_get_node(nodename
))) {
4819 reply("OSMSG_UNKNOWN_OPTION", nodename
);
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
]);
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
);
4840 static MODCMD_FUNC(cmd_set
)
4842 struct record_data
*rd
;
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 */
4849 if (!(rd
= conf_get_node(argv
[1]))) {
4850 reply("OSMSG_SET_NOT_SET", argv
[1]);
4854 if (rd
->type
!= RECDB_QSTRING
) {
4855 reply("OSMSG_SET_BAD_TYPE", argv
[1]);
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]);
4866 static MODCMD_FUNC(cmd_settime
)
4868 const char *srv_name_mask
= "*";
4869 time_t new_time
= now
;
4872 srv_name_mask
= argv
[1];
4874 new_time
= time(NULL
);
4875 irc_settime(srv_name_mask
, new_time
);
4876 reply("OSMSG_SETTIME_SUCCESS", srv_name_mask
);
4881 opserv_discrim_create(struct userNode
*user
, struct userNode
*bot
, unsigned int argc
, char *argv
[], int allow_channel
)
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;
4899 for (i
=0; i
<argc
; i
++) {
4900 if (irccasecmp(argv
[i
], "log") == 0) {
4901 discrim
->option_log
= 1;
4904 /* Assume all other criteria require arguments. */
4905 if (i
== argc
- 1) {
4906 send_message(user
, bot
, "MSG_MISSING_PARAMS", argv
[i
]);
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;
4921 if (irccasecmp(argv
[i
], "mask") == 0) {
4922 if (!is_ircmask(argv
[++i
])) {
4923 send_message(user
, bot
, "OSMSG_INVALID_IRCMASK", argv
[i
]);
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
]);
4933 } else if (irccasecmp(argv
[i
], "nick") == 0) {
4935 if (discrim
->intra_dcmp
> 0)
4936 discrim
->intra_scmp
= 1;
4938 discrim
->mask_nick
= argv
[i
];
4939 } else if (irccasecmp(argv
[i
], "ident") == 0) {
4941 if (discrim
->intra_dcmp
> 0)
4942 discrim
->intra_scmp
= 2;
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) {
4949 if (discrim
->intra_dcmp
> 0)
4950 discrim
->intra_scmp
= 3;
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
]);
4958 send_message(user
, bot
, "OSMSG_BAD_IP", argv
[i
]);
4961 } else if (irccasecmp(argv
[i
], "account") == 0) {
4962 if (discrim
->authed
== 0) {
4963 send_message(user
, bot
, "OSMSG_ACCOUNTMASK_AUTHED");
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");
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");
4983 discrim
->authed
= 0;
4985 send_message(user
, bot
, "MSG_INVALID_BINARY", argv
[i
]);
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 */
4992 if (true_string(argv
[i
])) {
4993 discrim
->info_space
= 1;
4994 } else if (false_string(argv
[i
])) {
4995 discrim
->info_space
= 0;
4997 send_message(user
, bot
, "MSG_INVALID_BINARY", argv
[i
]);
5000 } else if (irccasecmp(argv
[i
], "regex") == 0) {
5002 if (true_string(argv
[i
])) {
5003 discrim
->use_regex
= 1;
5004 } else if (false_string(argv
[i
])) {
5005 discrim
->use_regex
= 0;
5007 send_message(user
, bot
, "MSG_INVALID_BINARY", argv
[i
]);
5010 } else if (irccasecmp(argv
[i
], "silent") == 0) {
5012 if(user
!= opserv
&& !oper_has_access(user
, opserv
, opserv_conf
.silent_level
, 0)) {
5014 } else if (true_string(argv
[i
])) {
5015 discrim
->silent
= 1;
5016 } else if (false_string(argv
[i
])) {
5017 discrim
->silent
= 0;
5019 send_message(user
, bot
, "MSG_INVALID_BINARY", argv
[i
]);
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
]) {
5030 discrim
->chan_no_modes
|= MODE_CHANOP
| MODE_HALFOP
| MODE_VOICE
;
5033 discrim
->chan_req_modes
|= MODE_VOICE
;
5034 discrim
->chan_no_modes
|= MODE_CHANOP
;
5035 discrim
->chan_no_modes
|= MODE_HALFOP
;
5038 discrim
->chan_req_modes
|= MODE_HALFOP
;
5039 discrim
->chan_no_modes
|= MODE_CHANOP
;
5040 discrim
->chan_no_modes
|= MODE_VOICE
;
5043 discrim
->chan_req_modes
|= MODE_CHANOP
;
5046 send_message(user
, bot
, "MSG_NOT_CHANNEL_NAME");
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
]);
5059 discrim
->channel
= AddChannel(argv
[i
]+j
, now
, NULL
, NULL
, NULL
);
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
));
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);
5079 discrim
->min_ts
= now
- (ParseInterval(cmp
+1) - 1);
5081 } else if (cmp
[0] == '>') {
5082 if (cmp
[1] == '=') {
5083 discrim
->max_ts
= now
- ParseInterval(cmp
+2);
5085 discrim
->max_ts
= now
- (ParseInterval(cmp
+1) - 1);
5088 discrim
->min_ts
= now
- ParseInterval(cmp
+2);
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);
5097 discrim
->max_level
= strtoul(cmp
+1, NULL
, 0) - 1;
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);
5105 discrim
->min_level
= strtoul(cmp
+1, NULL
, 0) + 1;
5108 discrim
->min_level
= strtoul(cmp
+2, NULL
, 0);
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);
5118 send_message(user
, bot
, "MSG_INVALID_CRITERIA", argv
[i
]);
5123 if (discrim
->mask_nick
&& !strcmp(discrim
->mask_nick
, "*")) {
5124 discrim
->mask_nick
= 0;
5126 if (discrim
->mask_ident
&& !strcmp(discrim
->mask_ident
, "*")) {
5127 discrim
->mask_ident
= 0;
5129 if (discrim
->mask_info
&& !strcmp(discrim
->mask_info
, "*")) {
5130 discrim
->mask_info
= 0;
5132 if (discrim
->mask_host
&& !discrim
->mask_host
[strspn(discrim
->mask_host
, "*.")]) {
5133 discrim
->mask_host
= 0;
5136 if(discrim
->use_regex
)
5138 if(discrim
->mask_nick
)
5140 int err
= regcomp(&discrim
->regex_nick
, discrim
->mask_nick
, REG_EXTENDED
|REG_ICASE
|REG_NOSUB
);
5141 discrim
->has_regex_nick
= !err
;
5145 buff
[regerror(err
, &discrim
->regex_nick
, buff
, sizeof(buff
))] = 0;
5147 send_message(user
, bot
, "OSMSG_INVALID_REGEX", discrim
->mask_nick
, buff
, err
);
5152 if(discrim
->mask_ident
)
5154 int err
= regcomp(&discrim
->regex_ident
, discrim
->mask_ident
, REG_EXTENDED
|REG_ICASE
|REG_NOSUB
);
5155 discrim
->has_regex_ident
= !err
;
5159 buff
[regerror(err
, &discrim
->regex_ident
, buff
, sizeof(buff
))] = 0;
5161 send_message(user
, bot
, "OSMSG_INVALID_REGEX", discrim
->mask_ident
, buff
, err
);
5166 if(discrim
->mask_host
)
5168 int err
= regcomp(&discrim
->regex_host
, discrim
->mask_host
, REG_EXTENDED
|REG_ICASE
|REG_NOSUB
);
5169 discrim
->has_regex_host
= !err
;
5173 buff
[regerror(err
, &discrim
->regex_host
, buff
, sizeof(buff
))] = 0;
5175 send_message(user
, bot
, "OSMSG_INVALID_REGEX", discrim
->mask_host
, buff
, err
);
5180 if(discrim
->mask_info
)
5182 int err
= regcomp(&discrim
->regex_info
, discrim
->mask_info
, REG_EXTENDED
|REG_ICASE
|REG_NOSUB
);
5183 discrim
->has_regex_info
= !err
;
5187 buff
[regerror(err
, &discrim
->regex_info
, buff
, sizeof(buff
))] = 0;
5189 send_message(user
, bot
, "OSMSG_INVALID_REGEX", discrim
->mask_info
, buff
, err
);
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
);
5216 discrim_match(discrim_t discrim
, struct userNode
*user
)
5218 unsigned int access
;
5219 char *scmp
=NULL
, *dcmp
=NULL
;
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
))
5235 if (discrim
->channel
&& !GetUserMode(discrim
->channel
, user
))
5238 if(discrim
->use_regex
)
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))) {
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
))) {
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;
5263 if (discrim
->info_space
== 1) scmp
++;
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?
5275 if (discrim
->info_space
== 1) dcmp
++;
5278 if (irccasecmp(scmp
,dcmp
))
5282 access
= user
->handle_info
? user
->handle_info
->opserv_level
: 0;
5283 if ((access
< discrim
->min_level
)
5284 || (access
> discrim
->max_level
)) {
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
))
5296 opserv_discrim_search(discrim_t discrim
, discrim_search_func dsf
, void *data
)
5298 unsigned int nn
, count
;
5299 struct userList matched
;
5301 userList_init(&matched
);
5302 /* Try most optimized search methods first */
5303 if (discrim
->channel
) {
5305 (nn
< discrim
->channel
->members
.used
)
5306 && (matched
.used
< discrim
->limit
);
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)) {
5313 if (discrim_match(discrim
, mn
->user
)) {
5314 userList_append(&matched
, mn
->user
);
5317 } else if (discrim
->ip_mask_bits
== 128) {
5318 struct opserv_hostinfo
*ohi
= dict_find(opserv_hostinfo_dict
, irc_ntoa(&discrim
->ip_mask
), NULL
);
5320 userList_clean(&matched
);
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
]);
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
));
5337 if (!matched
.used
) {
5338 userList_clean(&matched
);
5342 if (discrim
->option_log
) {
5343 log_module(OS_LOG
, LOG_INFO
, "Logging matches for search:");
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
);
5350 if (dsf(user
, data
)) {
5351 /* If a search function returns true, it ran into a
5352 problem. Stop going through the list. */
5356 if (discrim
->option_log
) {
5357 log_module(OS_LOG
, LOG_INFO
, "End of matching users.");
5359 count
= matched
.used
;
5360 userList_clean(&matched
);
5365 trace_print_func(struct userNode
*match
, void *extra
)
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
);
5371 send_message_type(4, das
->source
, das
->destination
, "%-15s\002 \002%10s\002@\002%s", match
->nick
, match
->ident
, match
->hostname
);
5377 trace_count_func(UNUSED_ARG(struct userNode
*match
), UNUSED_ARG(void *extra
))
5383 is_oper_victim(struct userNode
*user
, struct userNode
*target
, int match_opers
)
5385 return !(IsService(target
)
5386 || (!match_opers
&& IsOper(target
))
5387 || (target
->handle_info
5388 && target
->handle_info
->opserv_level
> user
->handle_info
->opserv_level
));
5392 trace_gline_func(struct userNode
*match
, void *extra
)
5394 struct discrim_and_source
*das
= extra
;
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
);
5404 trace_shun_func(struct userNode
*match
, void *extra
)
5406 struct discrim_and_source
*das
= extra
;
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
);
5416 trace_kill_func(struct userNode
*match
, void *extra
)
5418 struct discrim_and_source
*das
= extra
;
5420 if (is_oper_victim(das
->source
, match
, das
->discrim
->match_opers
)) {
5422 if (das
->discrim
->reason
) {
5423 reason
= das
->discrim
->reason
;
5425 reason
= alloca(strlen(OSMSG_KILL_REQUESTED
)+strlen(das
->source
->nick
)+1);
5426 sprintf(reason
, OSMSG_KILL_REQUESTED
, das
->source
->nick
);
5428 DelUser(match
, opserv
, 1, reason
);
5435 is_gagged(char *mask
)
5437 struct gag_entry
*gag
;
5439 for (gag
= gagList
; gag
; gag
= gag
->next
) {
5440 if (match_ircglobs(gag
->mask
, mask
)) return 1;
5446 trace_gag_func(struct userNode
*match
, void *extra
)
5448 struct discrim_and_source
*das
= extra
;
5450 if (is_oper_victim(das
->source
, match
, das
->discrim
->match_opers
)) {
5451 char *reason
, *mask
;
5453 if (das
->discrim
->reason
) {
5454 reason
= das
->discrim
->reason
;
5456 reason
= alloca(strlen(OSMSG_GAG_REQUESTED
)+strlen(das
->source
->nick
)+1);
5457 sprintf(reason
, OSMSG_GAG_REQUESTED
, das
->source
->nick
);
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);
5472 trace_domains_func(struct userNode
*match
, void *extra
)
5474 struct discrim_and_source
*das
= extra
;
5476 unsigned long *count
;
5479 char ipmask
[IRC_NTOP_MASK_MAX_SIZE
];
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
) {
5487 snprintf(ipmask
, sizeof(ipmask
), "%lu.%lu.%lu.*", (matchip
>>24)&255, (matchip
>>16)&255, (matchip
>>8)&255);
5490 snprintf(ipmask
, sizeof(ipmask
), "%lu.%lu.*", (matchip
>>24)&255, (matchip
>>16)&255);
5493 snprintf(ipmask
, sizeof(ipmask
), "%lu.*", (matchip
>>24)&255);
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
;
5503 irc_ntop_mask(ipmask
, sizeof(ipmask
), &ip
, depth
);
5505 } else safestrncpy(ipmask
, match
->hostname
, sizeof(ipmask
));
5506 ipmask
[sizeof(ipmask
) - 1] = '\0';
5509 hostname
= match
->hostname
+ strlen(match
->hostname
);
5510 for (depth
=das
->discrim
->domain_depth
;
5511 depth
&& (hostname
> match
->hostname
);
5514 while ((hostname
> match
->hostname
) && (*hostname
!= '.')) hostname
--;
5516 if (*hostname
== '.') hostname
++; /* advance past last dot we saw */
5518 if (!(count
= dict_find(das
->dict
, hostname
, NULL
))) {
5519 count
= calloc(1, sizeof(*count
));
5520 dict_insert(das
->dict
, strdup(hostname
), count
);
5527 opserv_show_hostinfo(const char *key
, void *data
, void *extra
)
5529 unsigned long *count
= data
;
5530 struct discrim_and_source
*das
= extra
;
5532 send_message_type(4, das
->source
, das
->destination
, "%s %lu", key
, *count
);
5533 return !--das
->disp_limit
;
5536 static MODCMD_FUNC(cmd_trace
)
5538 struct discrim_and_source das
;
5539 discrim_search_func action
;
5540 unsigned int matches
;
5541 struct svccmd
*subcmd
;
5544 sprintf(buf
, "trace %s", argv
[1]);
5545 if (!(subcmd
= dict_find(opserv_service
->commands
, buf
, NULL
))) {
5546 reply("OSMSG_BAD_ACTION", argv
[1]);
5549 if (!svccmd_can_invoke(user
, opserv_service
->bot
, subcmd
, channel
, SVCCMD_NOISY
))
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
;
5566 reply("OSMSG_BAD_ACTION", argv
[1]);
5570 if (user
->handle_info
->opserv_level
< subcmd
->min_opserv_level
) {
5571 reply("OSMSG_LEVEL_TOO_LOW");
5577 das
.destination
= cmd
->parent
->bot
;
5578 das
.discrim
= opserv_discrim_create(user
, cmd
->parent
->bot
, argc
-2, argv
+2, 1);
5582 if (action
== trace_print_func
)
5584 reply("OSMSG_USER_SEARCH_RESULTS");
5585 reply("OSMSG_USER_SEARCH_BAR");
5586 reply("OSMSG_USER_SEARCH_HEADER");
5587 reply("OSMSG_USER_SEARCH_BAR");
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
;
5602 matches
= opserv_discrim_search(das
.discrim
, action
, &das
);
5604 if (action
== trace_domains_func
)
5605 dict_foreach(das
.dict
, opserv_show_hostinfo
, &das
);
5609 if(action
== trace_print_func
)
5610 reply("OSMSG_USER_SEARCH_COUNT_BAR", matches
);
5612 reply("OSMSG_USER_SEARCH_COUNT", matches
);
5615 reply("MSG_NO_MATCHES");
5617 if (das
.discrim
->channel
)
5618 UnlockChannel(das
.discrim
->channel
);
5619 free(das
.discrim
->reason
);
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
);
5631 dict_delete(das
.dict
);
5635 typedef void (*cdiscrim_search_func
)(struct chanNode
*match
, void *data
, struct userNode
*bot
);
5637 typedef struct channel_discrim
{
5640 unsigned int min_users
, max_users
;
5641 time_t min_ts
, max_ts
;
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
);
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
);
5656 opserv_cdiscrim_create(struct userNode
*user
, struct userNode
*bot
, unsigned int argc
, char *argv
[])
5661 discrim
= calloc(1, sizeof(*discrim
));
5662 discrim
->limit
= 25;
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
]);
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] == '<') {
5679 discrim
->max_users
= strtoul(cmp
+2, NULL
, 0);
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] == '>') {
5686 discrim
->min_users
= strtoul(cmp
+2, NULL
, 0);
5688 discrim
->min_users
= strtoul(cmp
+1, NULL
, 0) + 1;
5690 discrim
->min_users
= strtoul(cmp
+2, NULL
, 0);
5692 } else if (!irccasecmp(argv
[i
], "timestamp")) {
5693 const char *cmp
= argv
[++i
];
5694 if (cmp
[0] == '<') {
5696 discrim
->max_ts
= smart_parse_time(cmp
+2);
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] == '>') {
5703 discrim
->min_ts
= smart_parse_time(cmp
+2);
5705 discrim
->min_ts
= smart_parse_time(cmp
+1)+1;
5707 discrim
->min_ts
= smart_parse_time(cmp
);
5709 } else if (!irccasecmp(argv
[i
], "limit")) {
5710 discrim
->limit
= strtoul(argv
[++i
], NULL
, 10);
5712 send_message(user
, bot
, "MSG_INVALID_CRITERIA", argv
[i
]);
5717 if (discrim
->name
&& !strcmp(discrim
->name
, "*"))
5719 if (discrim
->topic
&& !strcmp(discrim
->topic
, "*"))
5729 cdiscrim_match(cdiscrim_t discrim
, struct chanNode
*chan
)
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
)) {
5742 static unsigned int opserv_cdiscrim_search(cdiscrim_t discrim
, cdiscrim_search_func dsf
, void *data
, struct userNode
*bot
)
5744 unsigned int count
= 0;
5745 dict_iterator_t it
, next
;
5747 for (it
= dict_first(channels
); it
&& count
< discrim
->limit
; it
= next
) {
5748 struct chanNode
*chan
= iter_data(it
);
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
);
5762 void channel_count(UNUSED_ARG(struct chanNode
*channel
), UNUSED_ARG(void *data
), UNUSED_ARG(struct userNode
*bot
))
5766 void channel_print(struct chanNode
*channel
, void *data
, struct userNode
*bot
)
5769 irc_make_chanmode(channel
, modes
);
5770 send_message(data
, bot
, "OSMSG_CSEARCH_CHANNEL_INFO", channel
->name
, channel
->members
.used
, modes
, channel
->topic
);
5773 static MODCMD_FUNC(cmd_csearch
)
5776 unsigned int matches
;
5777 cdiscrim_search_func action
;
5778 struct svccmd
*subcmd
;
5781 if (!irccasecmp(argv
[1], "count"))
5782 action
= channel_count
;
5783 else if (!irccasecmp(argv
[1], "print"))
5784 action
= channel_print
;
5786 reply("OSMSG_BAD_ACTION", argv
[1]);
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
)) {
5796 discrim
= opserv_cdiscrim_create(user
, cmd
->parent
->bot
, argc
- 2, argv
+ 2);
5800 if (action
== channel_print
)
5801 reply("OSMSG_CHANNEL_SEARCH_RESULTS");
5802 else if (action
== channel_count
)
5803 discrim
->limit
= INT_MAX
;
5805 matches
= opserv_cdiscrim_search(discrim
, action
, user
, cmd
->parent
->bot
);
5808 reply("MSG_MATCH_COUNT", matches
);
5810 reply("MSG_NO_MATCHES");
5816 static MODCMD_FUNC(cmd_gsync
)
5820 src
= GetServerH(argv
[1]);
5822 reply("MSG_SERVER_UNKNOWN", argv
[1]);
5828 irc_stats(cmd
->parent
->bot
, src
, 'G');
5829 reply("OSMSG_GSYNC_RUNNING", src
->name
);
5833 static MODCMD_FUNC(cmd_ssync
)
5837 src
= GetServerH(argv
[1]);
5839 reply("MSG_SERVER_UNKNOWN", argv
[1]);
5845 irc_stats(cmd
->parent
->bot
, src
, 'S');
5846 reply("OSMSG_SSYNC_RUNNING", src
->name
);
5850 struct gline_extra
{
5851 struct userNode
*user
;
5852 struct string_list
*glines
;
5853 struct userNode
*bot
;
5857 gtrace_print_func(struct gline
*gline
, void *extra
)
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
);
5868 gtrace_count_func(UNUSED_ARG(struct gline
*gline
), UNUSED_ARG(void *extra
))
5873 gtrace_ungline_func(struct gline
*gline
, void *extra
)
5875 struct gline_extra
*xtra
= extra
;
5876 string_list_append(xtra
->glines
, strdup(gline
->target
));
5879 static MODCMD_FUNC(cmd_gtrace
)
5881 struct gline_discrim
*discrim
;
5882 gline_search_func action
;
5883 unsigned int matches
, nn
;
5884 struct gline_extra extra
;
5885 struct svccmd
*subcmd
;
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
;
5895 reply("OSMSG_BAD_ACTION", argv
[1]);
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
)) {
5904 discrim
= gline_discrim_create(user
, cmd
->parent
->bot
, argc
-2, argv
+2);
5908 if (action
== gtrace_print_func
)
5909 reply("OSMSG_GLINE_SEARCH_RESULTS");
5910 else if (action
== gtrace_count_func
)
5911 discrim
->limit
= INT_MAX
;
5914 extra
.glines
= alloc_string_list(4);
5915 extra
.bot
= cmd
->parent
->bot
;
5916 matches
= gline_discrim_search(discrim
, action
, &extra
);
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
);
5924 reply("MSG_MATCH_COUNT", matches
);
5926 reply("MSG_NO_MATCHES");
5927 free(discrim
->alt_target_mask
);
5933 struct userNode
*user
;
5934 struct string_list
*shuns
;
5935 struct userNode
*bot
;
5939 strace_print_func(struct shun
*shun
, void *extra
)
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
);
5950 strace_count_func(UNUSED_ARG(struct shun
*shun
), UNUSED_ARG(void *extra
))
5955 strace_unshun_func(struct shun
*shun
, void *extra
)
5957 struct shun_extra
*xtra
= extra
;
5958 string_list_append(xtra
->shuns
, strdup(shun
->target
));
5961 static MODCMD_FUNC(cmd_strace
)
5963 struct shun_discrim
*discrim
;
5964 shun_search_func action
;
5965 unsigned int matches
, nn
;
5966 struct shun_extra extra
;
5967 struct svccmd
*subcmd
;
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
;
5977 reply("OSMSG_BAD_ACTION", argv
[1]);
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
)) {
5986 discrim
= shun_discrim_create(user
, cmd
->parent
->bot
, argc
-2, argv
+2);
5990 if (action
== strace_print_func
)
5991 reply("OSMSG_SHUN_SEARCH_RESULTS");
5992 else if (action
== strace_count_func
)
5993 discrim
->limit
= INT_MAX
;
5996 extra
.shuns
= alloc_string_list(4);
5997 extra
.bot
= cmd
->parent
->bot
;
5998 matches
= shun_discrim_search(discrim
, action
, &extra
);
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
);
6006 reply("MSG_MATCH_COUNT", matches
);
6008 reply("MSG_NO_MATCHES");
6009 free(discrim
->alt_target_mask
);
6015 alert_check_user(const char *key
, void *data
, void *extra
)
6017 struct opserv_user_alert
*alert
= data
;
6018 struct userNode
*user
= extra
;
6020 if (!discrim_match(alert
->discrim
, user
))
6023 if ((alert
->reaction
!= REACT_NOTICE
)
6025 && !alert
->discrim
->match_opers
) {
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
);
6033 /* Return 1 to halt alert matching, such as when killing the user
6034 that triggered the alert. */
6035 switch (alert
->reaction
) {
6037 DelUser(user
, opserv
, 1, alert
->discrim
->reason
);
6041 opserv_block(user, alert->owner, alert->discrim->reason, alert->discrim->duration, 1);
6045 opserv_block(user
, alert
->owner
, alert
->discrim
->reason
, alert
->discrim
->duration
, alert
->discrim
->silent
);
6048 opserv_shun(user
, alert
->owner
, alert
->discrim
->reason
, alert
->discrim
->duration
);
6051 opserv_svsjoin(user
, alert
->owner
, alert
->discrim
->reason
, alert
->discrim
->chantarget
);
6053 log_module(OS_LOG
, LOG_ERROR
, "Invalid reaction type %d for alert %s.", alert
->reaction
, key
);
6054 /* fall through to REACT_NOTICE case */
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
);
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
);
6069 opserv_alert_check_nick(struct userNode
*user
, UNUSED_ARG(const char *old_nick
))
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
);
6084 opserv_staff_alert(struct userNode
*user
, UNUSED_ARG(struct handle_info
*old_handle
))
6088 if (!opserv_conf
.staff_auth_channel
6089 || user
->uplink
->burst
6090 || !user
->handle_info
)
6092 else if (user
->handle_info
->opserv_level
)
6094 else if (IsNetworkHelper(user
))
6095 type
= "NETWORK HELPER";
6096 else if (IsSupportHelper(user
))
6097 type
= "SUPPORT HELPER";
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
);
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
);
6107 static MODCMD_FUNC(cmd_log
)
6109 struct logSearch
*discrim
;
6110 unsigned int matches
;
6111 struct logReport report
;
6113 discrim
= log_discrim_create(cmd
->parent
->bot
, user
, argc
, argv
);
6117 reply("OSMSG_LOG_SEARCH_RESULTS");
6118 report
.reporter
= opserv
;
6120 matches
= log_entry_search(discrim
, log_report_entry
, &report
);
6123 reply("MSG_MATCH_COUNT", matches
);
6125 reply("MSG_NO_MATCHES");
6132 gag_helper_func(struct userNode
*match
, UNUSED_ARG(void *extra
))
6134 if (IsOper(match
) || IsLocal(match
))
6136 match
->modes
|= FLAGS_GAGGED
;
6140 static MODCMD_FUNC(cmd_gag
)
6142 struct gag_entry
*gag
;
6143 unsigned int gagged
;
6144 unsigned long duration
;
6147 reason
= unsplit_string(argv
+ 3, argc
- 3, NULL
);
6149 if (!is_ircmask(argv
[1])) {
6150 reply("OSMSG_INVALID_IRCMASK", argv
[1]);
6154 for (gag
= gagList
; gag
; gag
= gag
->next
)
6155 if (match_ircglobs(gag
->mask
, argv
[1]))
6159 reply("OSMSG_REDUNDANT_GAG", argv
[1]);
6163 duration
= ParseInterval(argv
[2]);
6164 gagged
= gag_create(argv
[1], user
->handle_info
->handle
, reason
, (duration
?now
+duration
:0));
6167 reply("OSMSG_GAG_APPLIED", argv
[1], gagged
);
6169 reply("OSMSG_GAG_ADDED", argv
[1]);
6174 ungag_helper_func(struct userNode
*match
, UNUSED_ARG(void *extra
))
6176 match
->modes
&= ~FLAGS_GAGGED
;
6180 static MODCMD_FUNC(cmd_ungag
)
6182 struct gag_entry
*gag
;
6183 unsigned int ungagged
;
6185 for (gag
= gagList
; gag
; gag
= gag
->next
)
6186 if (!strcmp(gag
->mask
, argv
[1]))
6190 reply("OSMSG_GAG_NOT_FOUND", argv
[1]);
6194 timeq_del(gag
->expires
, gag_expire
, gag
, 0);
6195 ungagged
= gag_free(gag
);
6198 reply("OSMSG_UNGAG_APPLIED", argv
[1], ungagged
);
6200 reply("OSMSG_UNGAG_ADDED", argv
[1]);
6204 static MODCMD_FUNC(cmd_addalert
)
6206 opserv_alert_reaction reaction
;
6207 struct svccmd
*subcmd
;
6212 sprintf(buf
, "addalert %s", argv
[2]);
6213 if (!(subcmd
= dict_find(opserv_service
->commands
, buf
, NULL
))) {
6214 reply("OSMSG_UNKNOWN_REACTION", argv
[2]);
6217 if (!irccasecmp(argv
[2], "notice"))
6218 reaction
= REACT_NOTICE
;
6219 else if (!irccasecmp(argv
[2], "kill"))
6220 reaction
= REACT_KILL
;
6222 else if (!irccasecmp(argv[2], "silent"))
6223 reaction = REACT_SILENT;
6225 else if (!irccasecmp(argv
[2], "gline"))
6226 reaction
= REACT_GLINE
;
6227 else if (!irccasecmp(argv
[2], "track")) {
6229 reply("OSMSG_TRACK_DISABLED");
6232 reaction
= REACT_TRACK
;
6234 } else if (!irccasecmp(argv
[2], "shun"))
6235 reaction
= REACT_SHUN
;
6236 else if(!irccasecmp(argv
[2], "svsjoin"))
6237 reaction
= REACT_SVSJOIN
;
6239 reply("OSMSG_UNKNOWN_REACTION", argv
[2]);
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
)))
6245 reply("OSMSG_ADDED_ALERT", name
);
6249 static MODCMD_FUNC(cmd_delalert
)
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
]);
6258 reply("OSMSG_NO_SUCH_ALERT", argv
[i
]);
6264 opserv_conf_read(void)
6266 struct chanNode
*chan
;
6268 struct record_data
*rd
;
6269 dict_t conf_node
, child
;
6270 const char *str
, *str2
;
6271 struct policer_params
*pp
;
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
);
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
);
6285 opserv_conf
.debug_channel
= AddChannel(str
, now
, str2
, NULL
, NULL
);
6286 AddChannelUser(opserv
, opserv_conf
.debug_channel
)->modes
|= MODE_CHANOP
;
6288 opserv_conf
.debug_channel
= NULL
;
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
);
6295 opserv_conf
.alert_channel
= AddChannel(str
, now
, str2
, NULL
, NULL
);
6296 AddChannelUser(opserv
, opserv_conf
.alert_channel
)->modes
|= MODE_CHANOP
;
6298 opserv_conf
.alert_channel
= NULL
;
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
);
6305 opserv_conf
.staff_auth_channel
= AddChannel(str
, now
, str2
, NULL
, NULL
);
6306 AddChannelUser(opserv
, opserv_conf
.staff_auth_channel
)->modes
|= MODE_CHANOP
;
6308 opserv_conf
.staff_auth_channel
= NULL
;
6311 str
= database_get_data(conf_node
, KEY_ADMIN_LEVEL
, RECDB_QSTRING
);
6312 opserv_conf
.admin_level
= str
? strtoul(str
, NULL
, 0): 800;
6314 str
= database_get_data(conf_node
, KEY_SILENT_LEVEL
, RECDB_QSTRING
);
6315 opserv_conf
.silent_level
= str
? strtoul(str
, NULL
, 0): 700;
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
);
6327 NickChange(opserv
, str
, 0);
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;
6334 free_string_list(autojoin_channels
);
6335 autojoin_channels
= database_get_data(conf_node
, KEY_AUTOJOIN_CHANNELS
, RECDB_STRING_LIST
);
6337 if(autojoin_channels
)
6338 autojoin_channels
= string_list_copy(autojoin_channels
);
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
;
6347 str
= database_get_data(conf_node
, KEY_BLOCK_SHUN_DURATION
, RECDB_QSTRING
);
6348 opserv_conf
.block_shun_duration
= str
? ParseInterval(str
) : 3600;
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
);
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
;
6362 if (opserv_conf
.new_user_policer
.params
)
6363 pp
= opserv_conf
.new_user_policer
.params
;
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
);
6371 /* Defcon configuration */
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;
6383 str
= database_get_data(conf_node
, KEY_DEFCON_LEVEL
, RECDB_QSTRING
);
6384 DefConLevel
= str
? atoi(str
) : 5;
6386 str
= database_get_data(conf_node
, KEY_DEFCON_CHANMODES
, RECDB_QSTRING
);
6387 DefConChanModes
= str
? strdup(str
) : "+r";
6389 str
= database_get_data(conf_node
, KEY_DEFCON_SESSION_LIMIT
, RECDB_QSTRING
);
6390 DefConSessionLimit
= str
? atoi(str
) : 2;
6392 str
= database_get_data(conf_node
, KEY_DEFCON_TIMEOUT
, RECDB_QSTRING
);
6393 DefConTimeOut
= str
? ParseInterval(str
) : 900;
6395 str
= database_get_data(conf_node
, KEY_DEFCON_GLINE_DURATION
, RECDB_QSTRING
);
6396 DefConGlineExpire
= str
? ParseInterval(str
) : 300;
6398 str
= database_get_data(conf_node
, KEY_DEFCON_GLOBAL
, RECDB_QSTRING
);
6399 GlobalOnDefcon
= str
? atoi(str
) : 0;
6401 str
= database_get_data(conf_node
, KEY_DEFCON_GLOBAL_MORE
, RECDB_QSTRING
);
6402 GlobalOnDefconMore
= str
? atoi(str
) : 0;
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";
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";
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";
6414 /* lame way to export opserv_conf value to nickserv.c ... */
6416 opserv_conf_admin_level()
6418 return(opserv_conf
.admin_level
);
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
);
6428 opserv_routing_plan_options
= dict_new();
6430 opserv_routing_plans
= dict_new();
6431 dict_set_free_data(opserv_routing_plans
, free_routing_plan
);
6432 /* set up opserv_chan_warn dict */
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);
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
);
6459 opserv_db_cleanup(void)
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
]);
6477 policer_params_delete(opserv_conf
.join_policer_params
);
6478 policer_params_delete(opserv_conf
.new_user_policer
.params
);
6482 init_opserv(const char *nick
)
6484 OS_LOG
= log_register_type("OpServ", "file:opserv.log");
6486 const char *modes
= conf_get_data("services/opserv/modes", RECDB_QSTRING
);
6487 opserv
= AddService(nick
, modes
? modes
: NULL
, "Oper Services", NULL
);
6489 conf_register_reload(opserv_conf_read
);
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);
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);
6618 opserv_reserved_nick_dict
= dict_new();
6619 opserv_hostinfo_dict
= dict_new();
6621 dict_set_free_keys(opserv_hostinfo_dict
, free
);
6622 dict_set_free_data(opserv_hostinfo_dict
, opserv_free_hostinfo
);
6624 opserv_waiting_connections
= dict_new();
6625 dict_set_free_data(opserv_waiting_connections
, opserv_free_waiting_connection
);
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
);
6636 saxdb_register("OpServ", opserv_saxdb_read
, opserv_saxdb_write
);
6639 opserv_service
= service_register(opserv
);
6640 opserv_service
->trigger
= '?';
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
));
6648 reg_exit_func(opserv_db_cleanup
);
6649 message_register_table(msgtab
);