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