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