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