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