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