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