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