1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* IRC commands */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "bitlbee.h" |
---|
28 | #include "ipc.h" |
---|
29 | #include "chat.h" |
---|
30 | |
---|
31 | static void irc_cmd_pass( irc_t *irc, char **cmd ) |
---|
32 | { |
---|
33 | if( irc->status & USTATUS_LOGGED_IN ) |
---|
34 | { |
---|
35 | char *send_cmd[] = { "identify", cmd[1], NULL }; |
---|
36 | |
---|
37 | /* We're already logged in, this client seems to send the PASS |
---|
38 | command last. (Possibly it won't send it at all if it turns |
---|
39 | out we don't require it, which will break this feature.) |
---|
40 | Try to identify using the given password. */ |
---|
41 | return root_command( irc, send_cmd ); |
---|
42 | } |
---|
43 | /* Handling in pre-logged-in state, first see if this server is |
---|
44 | password-protected: */ |
---|
45 | else if( global.conf->auth_pass && |
---|
46 | ( strncmp( global.conf->auth_pass, "md5:", 4 ) == 0 ? |
---|
47 | md5_verify_password( cmd[1], global.conf->auth_pass + 4 ) == 0 : |
---|
48 | strcmp( cmd[1], global.conf->auth_pass ) == 0 ) ) |
---|
49 | { |
---|
50 | irc->status |= USTATUS_AUTHORIZED; |
---|
51 | irc_check_login( irc ); |
---|
52 | } |
---|
53 | else if( global.conf->auth_pass ) |
---|
54 | { |
---|
55 | irc_send_num( irc, 464, ":Incorrect password" ); |
---|
56 | } |
---|
57 | else |
---|
58 | { |
---|
59 | /* Remember the password and try to identify after USER/NICK. */ |
---|
60 | /*irc_setpass( irc, cmd[1] ); */ |
---|
61 | irc_check_login( irc ); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | static void irc_cmd_user( irc_t *irc, char **cmd ) |
---|
66 | { |
---|
67 | irc->user->user = g_strdup( cmd[1] ); |
---|
68 | irc->user->fullname = g_strdup( cmd[4] ); |
---|
69 | |
---|
70 | irc_check_login( irc ); |
---|
71 | } |
---|
72 | |
---|
73 | static void irc_cmd_nick( irc_t *irc, char **cmd ) |
---|
74 | { |
---|
75 | if( irc_user_by_name( irc, cmd[1] ) ) |
---|
76 | { |
---|
77 | irc_send_num( irc, 433, ":This nick is already in use" ); |
---|
78 | } |
---|
79 | else if( !nick_ok( cmd[1] ) ) |
---|
80 | { |
---|
81 | /* [SH] Invalid characters. */ |
---|
82 | irc_send_num( irc, 432, ":This nick contains invalid characters" ); |
---|
83 | } |
---|
84 | else if( irc->user->nick ) |
---|
85 | { |
---|
86 | if( irc->status & USTATUS_IDENTIFIED ) |
---|
87 | { |
---|
88 | irc_setpass( irc, NULL ); |
---|
89 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
90 | irc_umode_set( irc, "-R", 1 ); |
---|
91 | } |
---|
92 | |
---|
93 | irc_user_set_nick( irc->user, cmd[1] ); |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | irc->user->nick = g_strdup( cmd[1] ); |
---|
98 | |
---|
99 | irc_check_login( irc ); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | static void irc_cmd_quit( irc_t *irc, char **cmd ) |
---|
104 | { |
---|
105 | if( cmd[1] && *cmd[1] ) |
---|
106 | irc_abort( irc, 0, "Quit: %s", cmd[1] ); |
---|
107 | else |
---|
108 | irc_abort( irc, 0, "Leaving..." ); |
---|
109 | } |
---|
110 | |
---|
111 | static void irc_cmd_ping( irc_t *irc, char **cmd ) |
---|
112 | { |
---|
113 | irc_write( irc, ":%s PONG %s :%s", irc->root->host, |
---|
114 | irc->root->host, cmd[1]?cmd[1]:irc->root->host ); |
---|
115 | } |
---|
116 | |
---|
117 | static void irc_cmd_pong( irc_t *irc, char **cmd ) |
---|
118 | { |
---|
119 | /* We could check the value we get back from the user, but in |
---|
120 | fact we don't care, we're just happy s/he's still alive. */ |
---|
121 | irc->last_pong = gettime(); |
---|
122 | irc->pinging = 0; |
---|
123 | } |
---|
124 | |
---|
125 | static void irc_cmd_join( irc_t *irc, char **cmd ) |
---|
126 | { |
---|
127 | irc_channel_t *ic; |
---|
128 | |
---|
129 | if( ( ic = irc_channel_by_name( irc, cmd[1] ) ) == NULL ) |
---|
130 | ic = irc_channel_new( irc, cmd[1] ); |
---|
131 | |
---|
132 | if( ic == NULL ) |
---|
133 | { |
---|
134 | irc_send_num( irc, 479, "%s :Invalid channel name", cmd[1] ); |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
139 | return; /* Dude, you're already there... |
---|
140 | RFC doesn't have any reply for that though? */ |
---|
141 | |
---|
142 | irc_channel_add_user( ic, irc->user ); |
---|
143 | } |
---|
144 | |
---|
145 | static void irc_cmd_names( irc_t *irc, char **cmd ) |
---|
146 | { |
---|
147 | irc_channel_t *ic; |
---|
148 | |
---|
149 | if( cmd[1] && ( ic = irc_channel_by_name( irc, cmd[1] ) ) ) |
---|
150 | irc_send_names( ic ); |
---|
151 | /* With no args, we should show /names of all chans. Make the code |
---|
152 | below work well if necessary. |
---|
153 | else |
---|
154 | { |
---|
155 | GSList *l; |
---|
156 | |
---|
157 | for( l = irc->channels; l; l = l->next ) |
---|
158 | irc_send_names( l->data ); |
---|
159 | } |
---|
160 | */ |
---|
161 | } |
---|
162 | |
---|
163 | static void irc_cmd_part( irc_t *irc, char **cmd ) |
---|
164 | { |
---|
165 | irc_channel_t *ic; |
---|
166 | |
---|
167 | if( ( ic = irc_channel_by_name( irc, cmd[1] ) ) == NULL ) |
---|
168 | { |
---|
169 | irc_send_num( irc, 403, "%s :No such channel", cmd[1] ); |
---|
170 | } |
---|
171 | else if( !irc_channel_del_user( ic, irc->user ) ) |
---|
172 | { |
---|
173 | irc_send_num( irc, 442, "%s :You're not on that channel", cmd[1] ); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | static void irc_cmd_whois( irc_t *irc, char **cmd ) |
---|
178 | { |
---|
179 | char *nick = cmd[1]; |
---|
180 | irc_user_t *iu = irc_user_by_name( irc, nick ); |
---|
181 | |
---|
182 | if( iu ) |
---|
183 | irc_send_whois( iu ); |
---|
184 | else |
---|
185 | irc_send_num( irc, 401, "%s :Nick does not exist", nick ); |
---|
186 | } |
---|
187 | |
---|
188 | static void irc_cmd_whowas( irc_t *irc, char **cmd ) |
---|
189 | { |
---|
190 | /* For some reason irssi tries a whowas when whois fails. We can |
---|
191 | ignore this, but then the user never gets a "user not found" |
---|
192 | message from irssi which is a bit annoying. So just respond |
---|
193 | with not-found and irssi users will get better error messages */ |
---|
194 | |
---|
195 | irc_send_num( irc, 406, "%s :Nick does not exist", cmd[1] ); |
---|
196 | irc_send_num( irc, 369, "%s :End of WHOWAS", cmd[1] ); |
---|
197 | } |
---|
198 | |
---|
199 | static void irc_cmd_motd( irc_t *irc, char **cmd ) |
---|
200 | { |
---|
201 | irc_send_motd( irc ); |
---|
202 | } |
---|
203 | |
---|
204 | static void irc_cmd_mode( irc_t *irc, char **cmd ) |
---|
205 | { |
---|
206 | if( irc_channel_name_ok( cmd[1] ) ) |
---|
207 | { |
---|
208 | irc_channel_t *ic; |
---|
209 | |
---|
210 | if( ( ic = irc_channel_by_name( irc, cmd[1] ) ) == NULL ) |
---|
211 | irc_send_num( irc, 403, "%s :No such channel", cmd[1] ); |
---|
212 | else if( cmd[2] ) |
---|
213 | { |
---|
214 | if( *cmd[2] == '+' || *cmd[2] == '-' ) |
---|
215 | irc_send_num( irc, 477, "%s :Can't change channel modes", cmd[1] ); |
---|
216 | else if( *cmd[2] == 'b' ) |
---|
217 | irc_send_num( irc, 368, "%s :No bans possible", cmd[1] ); |
---|
218 | } |
---|
219 | else |
---|
220 | irc_send_num( irc, 324, "%s +%s", cmd[1], ic->mode ); |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | if( nick_cmp( cmd[1], irc->user->nick ) == 0 ) |
---|
225 | { |
---|
226 | if( cmd[2] ) |
---|
227 | irc_umode_set( irc, cmd[2], 0 ); |
---|
228 | else |
---|
229 | irc_send_num( irc, 221, "+%s", irc->umode ); |
---|
230 | } |
---|
231 | else |
---|
232 | irc_send_num( irc, 502, ":Don't touch their modes" ); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | static void irc_cmd_who( irc_t *irc, char **cmd ) |
---|
237 | { |
---|
238 | char *channel = cmd[1]; |
---|
239 | irc_channel_t *ic; |
---|
240 | |
---|
241 | if( !channel || *channel == '0' || *channel == '*' || !*channel ) |
---|
242 | irc_send_who( irc, irc->users, "**" ); |
---|
243 | else if( ( ic = irc_channel_by_name( irc, channel ) ) ) |
---|
244 | irc_send_who( irc, ic->users, channel ); |
---|
245 | else |
---|
246 | irc_send_num( irc, 403, "%s :No such channel", channel ); |
---|
247 | } |
---|
248 | |
---|
249 | static void irc_cmd_privmsg( irc_t *irc, char **cmd ) |
---|
250 | { |
---|
251 | irc_channel_t *ic; |
---|
252 | irc_user_t *iu; |
---|
253 | |
---|
254 | if( !cmd[2] ) |
---|
255 | { |
---|
256 | irc_send_num( irc, 412, ":No text to send" ); |
---|
257 | return; |
---|
258 | } |
---|
259 | |
---|
260 | /* Don't treat CTCP actions as real CTCPs, just convert them right now. */ |
---|
261 | if( g_strncasecmp( cmd[2], "\001ACTION", 7 ) == 0 ) |
---|
262 | { |
---|
263 | cmd[2] += 4; |
---|
264 | strcpy( cmd[2], "/me" ); |
---|
265 | if( cmd[2][strlen(cmd[2])-1] == '\001' ) |
---|
266 | cmd[2][strlen(cmd[2])-1] = '\0'; |
---|
267 | } |
---|
268 | |
---|
269 | if( irc_channel_name_ok( cmd[1] ) && |
---|
270 | ( ic = irc_channel_by_name( irc, cmd[1] ) ) ) |
---|
271 | { |
---|
272 | if( ic->f->privmsg ) |
---|
273 | ic->f->privmsg( ic, cmd[2] ); |
---|
274 | } |
---|
275 | else if( ( iu = irc_user_by_name( irc, cmd[1] ) ) ) |
---|
276 | { |
---|
277 | if( cmd[2][0] == '\001' ) |
---|
278 | { |
---|
279 | char **ctcp; |
---|
280 | |
---|
281 | if( iu->f->ctcp == NULL ) |
---|
282 | return; |
---|
283 | if( cmd[2][strlen(cmd[2])-1] == '\001' ) |
---|
284 | cmd[2][strlen(cmd[2])-1] = '\0'; |
---|
285 | |
---|
286 | ctcp = split_command_parts( cmd[2] + 1 ); |
---|
287 | iu->f->ctcp( iu, ctcp ); |
---|
288 | } |
---|
289 | else if( iu->f->privmsg ) |
---|
290 | iu->f->privmsg( iu, cmd[2] ); |
---|
291 | } |
---|
292 | else |
---|
293 | { |
---|
294 | irc_send_num( irc, 401, "%s :No such nick/channel", cmd[1] ); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | #if 0 |
---|
299 | else if( irc->nick && g_strcasecmp( cmd[1], irc->nick ) == 0 ) |
---|
300 | { |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | if( g_strcasecmp( cmd[1], irc->channel ) == 0 ) |
---|
305 | { |
---|
306 | unsigned int i; |
---|
307 | char *t = set_getstr( &irc->set, "default_target" ); |
---|
308 | |
---|
309 | if( g_strcasecmp( t, "last" ) == 0 && irc->last_target ) |
---|
310 | cmd[1] = irc->last_target; |
---|
311 | else if( g_strcasecmp( t, "root" ) == 0 ) |
---|
312 | cmd[1] = irc->mynick; |
---|
313 | |
---|
314 | for( i = 0; i < strlen( cmd[2] ); i ++ ) |
---|
315 | { |
---|
316 | if( cmd[2][i] == ' ' ) break; |
---|
317 | if( cmd[2][i] == ':' || cmd[2][i] == ',' ) |
---|
318 | { |
---|
319 | cmd[1] = cmd[2]; |
---|
320 | cmd[2] += i; |
---|
321 | *cmd[2] = 0; |
---|
322 | while( *(++cmd[2]) == ' ' ); |
---|
323 | break; |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | irc->is_private = 0; |
---|
328 | |
---|
329 | if( cmd[1] != irc->last_target ) |
---|
330 | { |
---|
331 | g_free( irc->last_target ); |
---|
332 | irc->last_target = g_strdup( cmd[1] ); |
---|
333 | } |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | irc->is_private = 1; |
---|
338 | } |
---|
339 | irc_send( irc, cmd[1], cmd[2], ( g_strcasecmp( cmd[0], "NOTICE" ) == 0 ) ? OPT_AWAY : 0 ); |
---|
340 | } |
---|
341 | #endif |
---|
342 | } |
---|
343 | |
---|
344 | static void irc_cmd_nickserv( irc_t *irc, char **cmd ) |
---|
345 | { |
---|
346 | /* [SH] This aliases the NickServ command to PRIVMSG root */ |
---|
347 | /* [TV] This aliases the NS command to PRIVMSG root as well */ |
---|
348 | root_command( irc, cmd + 1 ); |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | |
---|
353 | static void irc_cmd_oper( irc_t *irc, char **cmd ) |
---|
354 | { |
---|
355 | if( global.conf->oper_pass && |
---|
356 | ( strncmp( global.conf->oper_pass, "md5:", 4 ) == 0 ? |
---|
357 | md5_verify_password( cmd[2], global.conf->oper_pass + 4 ) == 0 : |
---|
358 | strcmp( cmd[2], global.conf->oper_pass ) == 0 ) ) |
---|
359 | { |
---|
360 | irc_umode_set( irc, "+o", 1 ); |
---|
361 | irc_send_num( irc, 381, ":Password accepted" ); |
---|
362 | } |
---|
363 | else |
---|
364 | { |
---|
365 | irc_send_num( irc, 432, ":Incorrect password" ); |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | #if 0 |
---|
370 | static void irc_cmd_invite( irc_t *irc, char **cmd ) |
---|
371 | { |
---|
372 | char *nick = cmd[1], *channel = cmd[2]; |
---|
373 | struct groupchat *c = irc_chat_by_channel( irc, channel ); |
---|
374 | user_t *u = user_find( irc, nick ); |
---|
375 | |
---|
376 | if( u && c && ( u->ic == c->ic ) ) |
---|
377 | if( c->ic && c->ic->acc->prpl->chat_invite ) |
---|
378 | { |
---|
379 | c->ic->acc->prpl->chat_invite( c, u->handle, NULL ); |
---|
380 | irc_send_num( irc, 341, "%s %s", nick, channel ); |
---|
381 | return; |
---|
382 | } |
---|
383 | |
---|
384 | irc_send_num( irc, 482, "%s :Invite impossible; User/Channel non-existent or incompatible", channel ); |
---|
385 | } |
---|
386 | #endif |
---|
387 | |
---|
388 | static void irc_cmd_userhost( irc_t *irc, char **cmd ) |
---|
389 | { |
---|
390 | int i; |
---|
391 | |
---|
392 | /* [TV] Usable USERHOST-implementation according to |
---|
393 | RFC1459. Without this, mIRC shows an error |
---|
394 | while connecting, and the used way of rejecting |
---|
395 | breaks standards. |
---|
396 | */ |
---|
397 | |
---|
398 | for( i = 1; cmd[i]; i ++ ) |
---|
399 | { |
---|
400 | irc_user_t *iu = irc_user_by_name( irc, cmd[i] ); |
---|
401 | |
---|
402 | if( iu ) |
---|
403 | irc_send_num( irc, 302, ":%s=%c%s@%s", iu->nick, |
---|
404 | irc_user_get_away( iu ) ? '-' : '+', |
---|
405 | iu->user, iu->host ); |
---|
406 | } |
---|
407 | } |
---|
408 | |
---|
409 | static void irc_cmd_ison( irc_t *irc, char **cmd ) |
---|
410 | { |
---|
411 | char buff[IRC_MAX_LINE]; |
---|
412 | int lenleft, i; |
---|
413 | |
---|
414 | buff[0] = '\0'; |
---|
415 | |
---|
416 | /* [SH] Leave room for : and \0 */ |
---|
417 | lenleft = IRC_MAX_LINE - 2; |
---|
418 | |
---|
419 | for( i = 1; cmd[i]; i ++ ) |
---|
420 | { |
---|
421 | char *this, *next; |
---|
422 | |
---|
423 | this = cmd[i]; |
---|
424 | while( *this ) |
---|
425 | { |
---|
426 | irc_user_t *iu; |
---|
427 | |
---|
428 | if( ( next = strchr( this, ' ' ) ) ) |
---|
429 | *next = 0; |
---|
430 | |
---|
431 | if( ( iu = irc_user_by_name( irc, this ) ) && |
---|
432 | iu->bu && iu->bu->flags & BEE_USER_ONLINE ) |
---|
433 | { |
---|
434 | lenleft -= strlen( iu->nick ) + 1; |
---|
435 | |
---|
436 | if( lenleft < 0 ) |
---|
437 | break; |
---|
438 | |
---|
439 | strcat( buff, iu->nick ); |
---|
440 | strcat( buff, " " ); |
---|
441 | } |
---|
442 | |
---|
443 | if( next ) |
---|
444 | { |
---|
445 | *next = ' '; |
---|
446 | this = next + 1; |
---|
447 | } |
---|
448 | else |
---|
449 | { |
---|
450 | break; |
---|
451 | } |
---|
452 | } |
---|
453 | |
---|
454 | /* *sigh* */ |
---|
455 | if( lenleft < 0 ) |
---|
456 | break; |
---|
457 | } |
---|
458 | |
---|
459 | if( strlen( buff ) > 0 ) |
---|
460 | buff[strlen(buff)-1] = '\0'; |
---|
461 | |
---|
462 | irc_send_num( irc, 303, ":%s", buff ); |
---|
463 | } |
---|
464 | |
---|
465 | static void irc_cmd_watch( irc_t *irc, char **cmd ) |
---|
466 | { |
---|
467 | int i; |
---|
468 | |
---|
469 | /* Obviously we could also mark a user structure as being |
---|
470 | watched, but what if the WATCH command is sent right |
---|
471 | after connecting? The user won't exist yet then... */ |
---|
472 | for( i = 1; cmd[i]; i ++ ) |
---|
473 | { |
---|
474 | char *nick; |
---|
475 | irc_user_t *iu; |
---|
476 | |
---|
477 | if( !cmd[i][0] || !cmd[i][1] ) |
---|
478 | break; |
---|
479 | |
---|
480 | nick = g_strdup( cmd[i] + 1 ); |
---|
481 | nick_lc( nick ); |
---|
482 | |
---|
483 | iu = irc_user_by_name( irc, nick ); |
---|
484 | |
---|
485 | if( cmd[i][0] == '+' ) |
---|
486 | { |
---|
487 | if( !g_hash_table_lookup( irc->watches, nick ) ) |
---|
488 | g_hash_table_insert( irc->watches, nick, nick ); |
---|
489 | |
---|
490 | if( iu && iu->bu && iu->bu->flags & BEE_USER_ONLINE ) |
---|
491 | irc_send_num( irc, 604, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
492 | iu->host, (int) time( NULL ), "is online" ); |
---|
493 | else |
---|
494 | irc_send_num( irc, 605, "%s %s %s %d :%s", nick, "*", "*", |
---|
495 | (int) time( NULL ), "is offline" ); |
---|
496 | } |
---|
497 | else if( cmd[i][0] == '-' ) |
---|
498 | { |
---|
499 | gpointer okey, ovalue; |
---|
500 | |
---|
501 | if( g_hash_table_lookup_extended( irc->watches, nick, &okey, &ovalue ) ) |
---|
502 | { |
---|
503 | g_hash_table_remove( irc->watches, okey ); |
---|
504 | g_free( okey ); |
---|
505 | |
---|
506 | irc_send_num( irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching" ); |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | #if 0 |
---|
513 | static void irc_cmd_topic( irc_t *irc, char **cmd ) |
---|
514 | { |
---|
515 | char *channel = cmd[1]; |
---|
516 | char *topic = cmd[2]; |
---|
517 | |
---|
518 | if( topic ) |
---|
519 | { |
---|
520 | /* Send the topic */ |
---|
521 | struct groupchat *c = irc_chat_by_channel( irc, channel ); |
---|
522 | if( c && c->ic && c->ic->acc->prpl->chat_topic ) |
---|
523 | c->ic->acc->prpl->chat_topic( c, topic ); |
---|
524 | } |
---|
525 | else |
---|
526 | { |
---|
527 | /* Get the topic */ |
---|
528 | irc_topic( irc, channel ); |
---|
529 | } |
---|
530 | } |
---|
531 | #endif |
---|
532 | |
---|
533 | static void irc_cmd_away( irc_t *irc, char **cmd ) |
---|
534 | { |
---|
535 | char *set; |
---|
536 | |
---|
537 | if( cmd[1] && *cmd[1] ) |
---|
538 | { |
---|
539 | char away[strlen(cmd[1])+1]; |
---|
540 | int i, j; |
---|
541 | |
---|
542 | /* Copy away string, but skip control chars. Mainly because |
---|
543 | Jabber really doesn't like them. */ |
---|
544 | for( i = j = 0; cmd[1][i]; i ++ ) |
---|
545 | if( ( away[j] = cmd[1][i] ) >= ' ' ) |
---|
546 | j ++; |
---|
547 | away[j] = '\0'; |
---|
548 | |
---|
549 | irc_send_num( irc, 306, ":You're now away: %s", away ); |
---|
550 | set = away; |
---|
551 | } |
---|
552 | else |
---|
553 | { |
---|
554 | irc_send_num( irc, 305, ":Welcome back" ); |
---|
555 | set = NULL; |
---|
556 | } |
---|
557 | |
---|
558 | set_setstr( &irc->b->set, "away", set ); |
---|
559 | } |
---|
560 | |
---|
561 | static void irc_cmd_version( irc_t *irc, char **cmd ) |
---|
562 | { |
---|
563 | irc_send_num( irc, 351, "bitlbee-%s. %s :%s/%s ", |
---|
564 | BITLBEE_VERSION, irc->root->host, ARCH, CPU ); |
---|
565 | } |
---|
566 | |
---|
567 | static void irc_cmd_completions( irc_t *irc, char **cmd ) |
---|
568 | { |
---|
569 | help_t *h; |
---|
570 | set_t *s; |
---|
571 | int i; |
---|
572 | |
---|
573 | irc_send_msg_raw( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS OK" ); |
---|
574 | |
---|
575 | for( i = 0; commands[i].command; i ++ ) |
---|
576 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS %s", commands[i].command ); |
---|
577 | |
---|
578 | for( h = global.help; h; h = h->next ) |
---|
579 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS help %s", h->title ); |
---|
580 | |
---|
581 | for( s = irc->b->set; s; s = s->next ) |
---|
582 | irc_send_msg_f( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS set %s", s->key ); |
---|
583 | |
---|
584 | irc_send_msg_raw( irc->root, "NOTICE", irc->user->nick, "COMPLETIONS END" ); |
---|
585 | } |
---|
586 | |
---|
587 | static void irc_cmd_rehash( irc_t *irc, char **cmd ) |
---|
588 | { |
---|
589 | if( global.conf->runmode == RUNMODE_INETD ) |
---|
590 | ipc_master_cmd_rehash( NULL, NULL ); |
---|
591 | else |
---|
592 | ipc_to_master( cmd ); |
---|
593 | |
---|
594 | irc_send_num( irc, 382, "%s :Rehashing", global.conf_file ); |
---|
595 | } |
---|
596 | |
---|
597 | static const command_t irc_commands[] = { |
---|
598 | { "pass", 1, irc_cmd_pass, 0 }, |
---|
599 | { "user", 4, irc_cmd_user, IRC_CMD_PRE_LOGIN }, |
---|
600 | { "nick", 1, irc_cmd_nick, 0 }, |
---|
601 | { "quit", 0, irc_cmd_quit, 0 }, |
---|
602 | { "ping", 0, irc_cmd_ping, 0 }, |
---|
603 | { "pong", 0, irc_cmd_pong, IRC_CMD_LOGGED_IN }, |
---|
604 | { "join", 1, irc_cmd_join, IRC_CMD_LOGGED_IN }, |
---|
605 | { "names", 1, irc_cmd_names, IRC_CMD_LOGGED_IN }, |
---|
606 | { "part", 1, irc_cmd_part, IRC_CMD_LOGGED_IN }, |
---|
607 | { "whois", 1, irc_cmd_whois, IRC_CMD_LOGGED_IN }, |
---|
608 | { "whowas", 1, irc_cmd_whowas, IRC_CMD_LOGGED_IN }, |
---|
609 | { "motd", 0, irc_cmd_motd, IRC_CMD_LOGGED_IN }, |
---|
610 | { "mode", 1, irc_cmd_mode, IRC_CMD_LOGGED_IN }, |
---|
611 | { "who", 0, irc_cmd_who, IRC_CMD_LOGGED_IN }, |
---|
612 | { "privmsg", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, |
---|
613 | { "nickserv", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
614 | { "ns", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, |
---|
615 | { "away", 0, irc_cmd_away, IRC_CMD_LOGGED_IN }, |
---|
616 | { "version", 0, irc_cmd_version, IRC_CMD_LOGGED_IN }, |
---|
617 | { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN }, |
---|
618 | { "userhost", 1, irc_cmd_userhost, IRC_CMD_LOGGED_IN }, |
---|
619 | { "ison", 1, irc_cmd_ison, IRC_CMD_LOGGED_IN }, |
---|
620 | { "watch", 1, irc_cmd_watch, IRC_CMD_LOGGED_IN }, |
---|
621 | #if 0 |
---|
622 | { "invite", 2, irc_cmd_invite, IRC_CMD_LOGGED_IN }, |
---|
623 | { "notice", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, |
---|
624 | { "topic", 1, irc_cmd_topic, IRC_CMD_LOGGED_IN }, |
---|
625 | #endif |
---|
626 | { "oper", 2, irc_cmd_oper, IRC_CMD_LOGGED_IN }, |
---|
627 | { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
628 | { "deaf", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
629 | { "wallops", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
630 | { "wall", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
631 | { "rehash", 0, irc_cmd_rehash, IRC_CMD_OPER_ONLY }, |
---|
632 | { "restart", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
633 | { "kill", 2, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, |
---|
634 | { NULL } |
---|
635 | }; |
---|
636 | |
---|
637 | void irc_exec( irc_t *irc, char *cmd[] ) |
---|
638 | { |
---|
639 | int i, n_arg; |
---|
640 | |
---|
641 | if( !cmd[0] ) |
---|
642 | return; |
---|
643 | |
---|
644 | for( i = 0; irc_commands[i].command; i++ ) |
---|
645 | if( g_strcasecmp( irc_commands[i].command, cmd[0] ) == 0 ) |
---|
646 | { |
---|
647 | /* There should be no typo in the next line: */ |
---|
648 | for( n_arg = 0; cmd[n_arg]; n_arg ++ ); n_arg --; |
---|
649 | |
---|
650 | if( irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status & USTATUS_LOGGED_IN ) |
---|
651 | { |
---|
652 | irc_send_num( irc, 462, ":Only allowed before logging in" ); |
---|
653 | } |
---|
654 | else if( irc_commands[i].flags & IRC_CMD_LOGGED_IN && !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
655 | { |
---|
656 | irc_send_num( irc, 451, ":Register first" ); |
---|
657 | } |
---|
658 | else if( irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr( irc->umode, 'o' ) ) |
---|
659 | { |
---|
660 | irc_send_num( irc, 481, ":Permission denied - You're not an IRC operator" ); |
---|
661 | } |
---|
662 | else if( n_arg < irc_commands[i].required_parameters ) |
---|
663 | { |
---|
664 | irc_send_num( irc, 461, "%s :Need more parameters", cmd[0] ); |
---|
665 | } |
---|
666 | else if( irc_commands[i].flags & IRC_CMD_TO_MASTER ) |
---|
667 | { |
---|
668 | /* IPC doesn't make sense in inetd mode, |
---|
669 | but the function will catch that. */ |
---|
670 | ipc_to_master( cmd ); |
---|
671 | } |
---|
672 | else |
---|
673 | { |
---|
674 | irc_commands[i].execute( irc, cmd ); |
---|
675 | } |
---|
676 | |
---|
677 | return; |
---|
678 | } |
---|
679 | |
---|
680 | if( irc->status >= USTATUS_LOGGED_IN ) |
---|
681 | irc_send_num( irc, 421, "%s :Unknown command", cmd[0] ); |
---|
682 | } |
---|