1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* User manager (root) 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 "commands.h" |
---|
28 | #include "crypting.h" |
---|
29 | #include "bitlbee.h" |
---|
30 | #include "help.h" |
---|
31 | |
---|
32 | #include <string.h> |
---|
33 | |
---|
34 | void root_command_string( irc_t *irc, user_t *u, char *command, int flags ) |
---|
35 | { |
---|
36 | char *cmd[IRC_MAX_ARGS]; |
---|
37 | char *s; |
---|
38 | int k; |
---|
39 | char q = 0; |
---|
40 | |
---|
41 | memset( cmd, 0, sizeof( cmd ) ); |
---|
42 | cmd[0] = command; |
---|
43 | k = 1; |
---|
44 | for( s = command; *s && k < ( IRC_MAX_ARGS - 1 ); s ++ ) |
---|
45 | if( *s == ' ' && !q ) |
---|
46 | { |
---|
47 | *s = 0; |
---|
48 | while( *++s == ' ' ); |
---|
49 | if( *s == '"' || *s == '\'' ) |
---|
50 | { |
---|
51 | q = *s; |
---|
52 | s ++; |
---|
53 | } |
---|
54 | if( *s ) |
---|
55 | { |
---|
56 | cmd[k++] = s; |
---|
57 | s --; |
---|
58 | } |
---|
59 | } |
---|
60 | else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) ) |
---|
61 | { |
---|
62 | char *cpy; |
---|
63 | |
---|
64 | for( cpy = s; *cpy; cpy ++ ) |
---|
65 | cpy[0] = cpy[1]; |
---|
66 | } |
---|
67 | else if( *s == q ) |
---|
68 | { |
---|
69 | q = *s = 0; |
---|
70 | } |
---|
71 | cmd[k] = NULL; |
---|
72 | |
---|
73 | root_command( irc, cmd ); |
---|
74 | } |
---|
75 | |
---|
76 | void root_command( irc_t *irc, char *cmd[] ) |
---|
77 | { |
---|
78 | int i; |
---|
79 | |
---|
80 | if( !cmd[0] ) |
---|
81 | return; |
---|
82 | |
---|
83 | for( i = 0; commands[i].command; i++ ) |
---|
84 | if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) |
---|
85 | { |
---|
86 | if( !cmd[commands[i].required_parameters] ) |
---|
87 | { |
---|
88 | irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters ); |
---|
89 | return; |
---|
90 | } |
---|
91 | commands[i].execute( irc, cmd ); |
---|
92 | return; |
---|
93 | } |
---|
94 | |
---|
95 | irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] ); |
---|
96 | } |
---|
97 | |
---|
98 | static void cmd_help( irc_t *irc, char **cmd ) |
---|
99 | { |
---|
100 | char param[80]; |
---|
101 | int i; |
---|
102 | char *s; |
---|
103 | |
---|
104 | memset( param, 0, sizeof(param) ); |
---|
105 | for ( i = 1; (cmd[i] != NULL && ( strlen(param) < (sizeof(param)-1) ) ); i++ ) { |
---|
106 | if ( i != 1 ) // prepend space except for the first parameter |
---|
107 | strcat(param, " "); |
---|
108 | strncat( param, cmd[i], sizeof(param) - strlen(param) - 1 ); |
---|
109 | } |
---|
110 | |
---|
111 | s = help_get( &(global.help), param ); |
---|
112 | if( !s ) s = help_get( &(global.help), "" ); |
---|
113 | |
---|
114 | if( s ) |
---|
115 | { |
---|
116 | irc_usermsg( irc, "%s", s ); |
---|
117 | g_free( s ); |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | irc_usermsg( irc, "Error opening helpfile." ); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | static void cmd_identify( irc_t *irc, char **cmd ) |
---|
126 | { |
---|
127 | storage_status_t status = storage_load( irc->nick, cmd[1], irc ); |
---|
128 | |
---|
129 | switch (status) { |
---|
130 | case STORAGE_INVALID_PASSWORD: |
---|
131 | irc_usermsg( irc, "Incorrect password" ); |
---|
132 | break; |
---|
133 | case STORAGE_NO_SUCH_USER: |
---|
134 | irc_usermsg( irc, "The nick is (probably) not registered" ); |
---|
135 | break; |
---|
136 | case STORAGE_OK: |
---|
137 | irc_usermsg( irc, "Password accepted" ); |
---|
138 | irc_umode_set( irc, "+R", 1 ); |
---|
139 | break; |
---|
140 | default: |
---|
141 | irc_usermsg( irc, "Something very weird happened" ); |
---|
142 | break; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | static void cmd_register( irc_t *irc, char **cmd ) |
---|
147 | { |
---|
148 | if( global.conf->authmode == AUTHMODE_REGISTERED ) |
---|
149 | { |
---|
150 | irc_usermsg( irc, "This server does not allow registering new accounts" ); |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | irc_setpass( irc, cmd[1] ); |
---|
155 | switch( storage_save( irc, FALSE )) { |
---|
156 | case STORAGE_ALREADY_EXISTS: |
---|
157 | irc_usermsg( irc, "Nick is already registered" ); |
---|
158 | break; |
---|
159 | |
---|
160 | case STORAGE_OK: |
---|
161 | irc->status = USTATUS_IDENTIFIED; |
---|
162 | irc_umode_set( irc, "+R", 1 ); |
---|
163 | break; |
---|
164 | |
---|
165 | default: |
---|
166 | irc_usermsg( irc, "Error registering" ); |
---|
167 | break; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | static void cmd_drop( irc_t *irc, char **cmd ) |
---|
172 | { |
---|
173 | storage_status_t status; |
---|
174 | |
---|
175 | status = storage_remove (irc->nick, cmd[1]); |
---|
176 | switch (status) { |
---|
177 | case STORAGE_NO_SUCH_USER: |
---|
178 | irc_usermsg( irc, "That account does not exist" ); |
---|
179 | break; |
---|
180 | case STORAGE_INVALID_PASSWORD: |
---|
181 | irc_usermsg( irc, "Password invalid" ); |
---|
182 | break; |
---|
183 | case STORAGE_OK: |
---|
184 | irc_setpass( irc, NULL ); |
---|
185 | irc->status = USTATUS_LOGGED_IN; |
---|
186 | irc_umode_set( irc, "-R", 1 ); |
---|
187 | irc_usermsg( irc, "Account `%s' removed", irc->nick ); |
---|
188 | break; |
---|
189 | default: |
---|
190 | irc_usermsg( irc, "Error: '%d'", status ); |
---|
191 | break; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | static void cmd_account( irc_t *irc, char **cmd ) |
---|
196 | { |
---|
197 | account_t *a; |
---|
198 | |
---|
199 | if( global.conf->authmode == AUTHMODE_REGISTERED && irc->status < USTATUS_IDENTIFIED ) |
---|
200 | { |
---|
201 | irc_usermsg( irc, "This server only accepts registered users" ); |
---|
202 | return; |
---|
203 | } |
---|
204 | |
---|
205 | if( g_strcasecmp( cmd[1], "add" ) == 0 ) |
---|
206 | { |
---|
207 | struct prpl *prpl; |
---|
208 | |
---|
209 | if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL ) |
---|
210 | { |
---|
211 | irc_usermsg( irc, "Not enough parameters" ); |
---|
212 | return; |
---|
213 | } |
---|
214 | |
---|
215 | prpl = find_protocol(cmd[2]); |
---|
216 | |
---|
217 | if( prpl == NULL ) |
---|
218 | { |
---|
219 | irc_usermsg( irc, "Unknown protocol" ); |
---|
220 | return; |
---|
221 | } |
---|
222 | |
---|
223 | a = account_add( irc, prpl, cmd[3], cmd[4] ); |
---|
224 | |
---|
225 | if( cmd[5] ) |
---|
226 | a->server = g_strdup( cmd[5] ); |
---|
227 | |
---|
228 | irc_usermsg( irc, "Account successfully added" ); |
---|
229 | } |
---|
230 | else if( g_strcasecmp( cmd[1], "del" ) == 0 ) |
---|
231 | { |
---|
232 | if( !cmd[2] ) |
---|
233 | { |
---|
234 | irc_usermsg( irc, "Not enough parameters given (need %d)", 2 ); |
---|
235 | } |
---|
236 | else if( !( a = account_get( irc, cmd[2] ) ) ) |
---|
237 | { |
---|
238 | irc_usermsg( irc, "Invalid account" ); |
---|
239 | } |
---|
240 | else if( a->gc ) |
---|
241 | { |
---|
242 | irc_usermsg( irc, "Account is still logged in, can't delete" ); |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | account_del( irc, a ); |
---|
247 | irc_usermsg( irc, "Account deleted" ); |
---|
248 | } |
---|
249 | } |
---|
250 | else if( g_strcasecmp( cmd[1], "list" ) == 0 ) |
---|
251 | { |
---|
252 | int i = 0; |
---|
253 | |
---|
254 | if( strchr( irc->umode, 'b' ) ) |
---|
255 | irc_usermsg( irc, "Account list:" ); |
---|
256 | |
---|
257 | for( a = irc->accounts; a; a = a->next ) |
---|
258 | { |
---|
259 | char *con; |
---|
260 | |
---|
261 | if( a->gc && ( a->gc->flags & OPT_LOGGED_IN ) ) |
---|
262 | con = " (connected)"; |
---|
263 | else if( a->gc ) |
---|
264 | con = " (connecting)"; |
---|
265 | else if( a->reconnect ) |
---|
266 | con = " (awaiting reconnect)"; |
---|
267 | else |
---|
268 | con = ""; |
---|
269 | |
---|
270 | irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con ); |
---|
271 | |
---|
272 | i ++; |
---|
273 | } |
---|
274 | irc_usermsg( irc, "End of account list" ); |
---|
275 | } |
---|
276 | else if( g_strcasecmp( cmd[1], "on" ) == 0 ) |
---|
277 | { |
---|
278 | if( cmd[2] ) |
---|
279 | { |
---|
280 | if( ( a = account_get( irc, cmd[2] ) ) ) |
---|
281 | { |
---|
282 | if( a->gc ) |
---|
283 | { |
---|
284 | irc_usermsg( irc, "Account already online" ); |
---|
285 | return; |
---|
286 | } |
---|
287 | else |
---|
288 | { |
---|
289 | account_on( irc, a ); |
---|
290 | } |
---|
291 | } |
---|
292 | else |
---|
293 | { |
---|
294 | irc_usermsg( irc, "Invalid account" ); |
---|
295 | return; |
---|
296 | } |
---|
297 | } |
---|
298 | else |
---|
299 | { |
---|
300 | if ( irc->accounts ) { |
---|
301 | irc_usermsg( irc, "Trying to get all accounts connected..." ); |
---|
302 | |
---|
303 | for( a = irc->accounts; a; a = a->next ) |
---|
304 | if( !a->gc ) |
---|
305 | account_on( irc, a ); |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | irc_usermsg( irc, "No accounts known. Use 'account add' to add one." ); |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | else if( g_strcasecmp( cmd[1], "off" ) == 0 ) |
---|
314 | { |
---|
315 | if( !cmd[2] ) |
---|
316 | { |
---|
317 | irc_usermsg( irc, "Deactivating all active (re)connections..." ); |
---|
318 | |
---|
319 | for( a = irc->accounts; a; a = a->next ) |
---|
320 | { |
---|
321 | if( a->gc ) |
---|
322 | account_off( irc, a ); |
---|
323 | else if( a->reconnect ) |
---|
324 | cancel_auto_reconnect( a ); |
---|
325 | } |
---|
326 | } |
---|
327 | else if( ( a = account_get( irc, cmd[2] ) ) ) |
---|
328 | { |
---|
329 | if( a->gc ) |
---|
330 | { |
---|
331 | account_off( irc, a ); |
---|
332 | } |
---|
333 | else if( a->reconnect ) |
---|
334 | { |
---|
335 | cancel_auto_reconnect( a ); |
---|
336 | irc_usermsg( irc, "Reconnect cancelled" ); |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | irc_usermsg( irc, "Account already offline" ); |
---|
341 | return; |
---|
342 | } |
---|
343 | } |
---|
344 | else |
---|
345 | { |
---|
346 | irc_usermsg( irc, "Invalid account" ); |
---|
347 | return; |
---|
348 | } |
---|
349 | } |
---|
350 | else |
---|
351 | { |
---|
352 | irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] ); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | static void cmd_add( irc_t *irc, char **cmd ) |
---|
357 | { |
---|
358 | account_t *a; |
---|
359 | int add_for_real = 1; |
---|
360 | |
---|
361 | if( g_strcasecmp( cmd[1], "-tmp" ) == 0 ) |
---|
362 | { |
---|
363 | add_for_real = 0; |
---|
364 | cmd ++; /* So evil... :-D */ |
---|
365 | } |
---|
366 | |
---|
367 | if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
368 | { |
---|
369 | irc_usermsg( irc, "Invalid account" ); |
---|
370 | return; |
---|
371 | } |
---|
372 | else if( !( a->gc && ( a->gc->flags & OPT_LOGGED_IN ) ) ) |
---|
373 | { |
---|
374 | irc_usermsg( irc, "That account is not on-line" ); |
---|
375 | return; |
---|
376 | } |
---|
377 | |
---|
378 | if( cmd[3] ) |
---|
379 | { |
---|
380 | if( !nick_ok( cmd[3] ) ) |
---|
381 | { |
---|
382 | irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] ); |
---|
383 | return; |
---|
384 | } |
---|
385 | else if( user_find( irc, cmd[3] ) ) |
---|
386 | { |
---|
387 | irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] ); |
---|
388 | return; |
---|
389 | } |
---|
390 | else |
---|
391 | { |
---|
392 | nick_set( irc, cmd[2], a->gc->prpl, cmd[3] ); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | /* By making this optional, you can talk to people without having to |
---|
397 | add them to your *real* (server-side) contact list. */ |
---|
398 | if( add_for_real ) |
---|
399 | a->gc->prpl->add_buddy( a->gc, cmd[2] ); |
---|
400 | |
---|
401 | add_buddy( a->gc, NULL, cmd[2], cmd[2] ); |
---|
402 | |
---|
403 | irc_usermsg( irc, "User `%s' added to your contact list as `%s'", cmd[2], user_findhandle( a->gc, cmd[2] )->nick ); |
---|
404 | } |
---|
405 | |
---|
406 | static void cmd_info( irc_t *irc, char **cmd ) |
---|
407 | { |
---|
408 | struct gaim_connection *gc; |
---|
409 | account_t *a; |
---|
410 | |
---|
411 | if( !cmd[2] ) |
---|
412 | { |
---|
413 | user_t *u = user_find( irc, cmd[1] ); |
---|
414 | if( !u || !u->gc ) |
---|
415 | { |
---|
416 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
417 | return; |
---|
418 | } |
---|
419 | gc = u->gc; |
---|
420 | cmd[2] = u->handle; |
---|
421 | } |
---|
422 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
423 | { |
---|
424 | irc_usermsg( irc, "Invalid account" ); |
---|
425 | return; |
---|
426 | } |
---|
427 | else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) ) |
---|
428 | { |
---|
429 | irc_usermsg( irc, "That account is not on-line" ); |
---|
430 | return; |
---|
431 | } |
---|
432 | |
---|
433 | if( !gc->prpl->get_info ) |
---|
434 | { |
---|
435 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
436 | } |
---|
437 | else |
---|
438 | { |
---|
439 | gc->prpl->get_info( gc, cmd[2] ); |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | static void cmd_rename( irc_t *irc, char **cmd ) |
---|
444 | { |
---|
445 | user_t *u; |
---|
446 | |
---|
447 | if( g_strcasecmp( cmd[1], irc->nick ) == 0 ) |
---|
448 | { |
---|
449 | irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] ); |
---|
450 | } |
---|
451 | else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) ) |
---|
452 | { |
---|
453 | irc_usermsg( irc, "Nick `%s' already exists", cmd[2] ); |
---|
454 | } |
---|
455 | else if( !nick_ok( cmd[2] ) ) |
---|
456 | { |
---|
457 | irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] ); |
---|
458 | } |
---|
459 | else if( !( u = user_find( irc, cmd[1] ) ) ) |
---|
460 | { |
---|
461 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
462 | } |
---|
463 | else |
---|
464 | { |
---|
465 | user_rename( irc, cmd[1], cmd[2] ); |
---|
466 | irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] ); |
---|
467 | if( g_strcasecmp( cmd[1], irc->mynick ) == 0 ) |
---|
468 | { |
---|
469 | g_free( irc->mynick ); |
---|
470 | irc->mynick = g_strdup( cmd[2] ); |
---|
471 | } |
---|
472 | else if( u->send_handler == buddy_send_handler ) |
---|
473 | { |
---|
474 | nick_set( irc, u->handle, u->gc->prpl, cmd[2] ); |
---|
475 | } |
---|
476 | |
---|
477 | irc_usermsg( irc, "Nick successfully changed" ); |
---|
478 | } |
---|
479 | } |
---|
480 | |
---|
481 | static void cmd_remove( irc_t *irc, char **cmd ) |
---|
482 | { |
---|
483 | user_t *u; |
---|
484 | char *s; |
---|
485 | |
---|
486 | if( !( u = user_find( irc, cmd[1] ) ) || !u->gc ) |
---|
487 | { |
---|
488 | irc_usermsg( irc, "Buddy `%s' not found", cmd[1] ); |
---|
489 | return; |
---|
490 | } |
---|
491 | s = g_strdup( u->handle ); |
---|
492 | |
---|
493 | u->gc->prpl->remove_buddy( u->gc, u->handle, NULL ); |
---|
494 | user_del( irc, cmd[1] ); |
---|
495 | nick_del( irc, cmd[1] ); |
---|
496 | |
---|
497 | irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] ); |
---|
498 | g_free( s ); |
---|
499 | |
---|
500 | return; |
---|
501 | } |
---|
502 | |
---|
503 | static void cmd_block( irc_t *irc, char **cmd ) |
---|
504 | { |
---|
505 | struct gaim_connection *gc; |
---|
506 | account_t *a; |
---|
507 | |
---|
508 | if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->gc ) |
---|
509 | { |
---|
510 | char *format; |
---|
511 | GSList *l; |
---|
512 | |
---|
513 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
514 | format = "%s\t%s"; |
---|
515 | else |
---|
516 | format = "%-32.32s %-16.16s"; |
---|
517 | |
---|
518 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
519 | for( l = a->gc->deny; l; l = l->next ) |
---|
520 | { |
---|
521 | user_t *u = user_findhandle( a->gc, l->data ); |
---|
522 | irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" ); |
---|
523 | } |
---|
524 | irc_usermsg( irc, "End of list." ); |
---|
525 | |
---|
526 | return; |
---|
527 | } |
---|
528 | else if( !cmd[2] ) |
---|
529 | { |
---|
530 | user_t *u = user_find( irc, cmd[1] ); |
---|
531 | if( !u || !u->gc ) |
---|
532 | { |
---|
533 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
534 | return; |
---|
535 | } |
---|
536 | gc = u->gc; |
---|
537 | cmd[2] = u->handle; |
---|
538 | } |
---|
539 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
540 | { |
---|
541 | irc_usermsg( irc, "Invalid account" ); |
---|
542 | return; |
---|
543 | } |
---|
544 | else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) ) |
---|
545 | { |
---|
546 | irc_usermsg( irc, "That account is not on-line" ); |
---|
547 | return; |
---|
548 | } |
---|
549 | |
---|
550 | if( !gc->prpl->add_deny || !gc->prpl->rem_permit ) |
---|
551 | { |
---|
552 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
553 | } |
---|
554 | else |
---|
555 | { |
---|
556 | gc->prpl->rem_permit( gc, cmd[2] ); |
---|
557 | gc->prpl->add_deny( gc, cmd[2] ); |
---|
558 | irc_usermsg( irc, "Buddy `%s' moved from your permit- to your deny-list", cmd[2] ); |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | static void cmd_allow( irc_t *irc, char **cmd ) |
---|
563 | { |
---|
564 | struct gaim_connection *gc; |
---|
565 | account_t *a; |
---|
566 | |
---|
567 | if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->gc ) |
---|
568 | { |
---|
569 | char *format; |
---|
570 | GSList *l; |
---|
571 | |
---|
572 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
573 | format = "%s\t%s"; |
---|
574 | else |
---|
575 | format = "%-32.32s %-16.16s"; |
---|
576 | |
---|
577 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
578 | for( l = a->gc->deny; l; l = l->next ) |
---|
579 | { |
---|
580 | user_t *u = user_findhandle( a->gc, l->data ); |
---|
581 | irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" ); |
---|
582 | } |
---|
583 | irc_usermsg( irc, "End of list." ); |
---|
584 | |
---|
585 | return; |
---|
586 | } |
---|
587 | else if( !cmd[2] ) |
---|
588 | { |
---|
589 | user_t *u = user_find( irc, cmd[1] ); |
---|
590 | if( !u || !u->gc ) |
---|
591 | { |
---|
592 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
593 | return; |
---|
594 | } |
---|
595 | gc = u->gc; |
---|
596 | cmd[2] = u->handle; |
---|
597 | } |
---|
598 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
599 | { |
---|
600 | irc_usermsg( irc, "Invalid account" ); |
---|
601 | return; |
---|
602 | } |
---|
603 | else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) ) |
---|
604 | { |
---|
605 | irc_usermsg( irc, "That account is not on-line" ); |
---|
606 | return; |
---|
607 | } |
---|
608 | |
---|
609 | if( !gc->prpl->rem_deny || !gc->prpl->add_permit ) |
---|
610 | { |
---|
611 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
612 | } |
---|
613 | else |
---|
614 | { |
---|
615 | gc->prpl->rem_deny( gc, cmd[2] ); |
---|
616 | gc->prpl->add_permit( gc, cmd[2] ); |
---|
617 | |
---|
618 | irc_usermsg( irc, "Buddy `%s' moved from your deny- to your permit-list", cmd[2] ); |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | static void cmd_yesno( irc_t *irc, char **cmd ) |
---|
623 | { |
---|
624 | query_t *q = NULL; |
---|
625 | int numq = 0; |
---|
626 | |
---|
627 | if( irc->queries == NULL ) |
---|
628 | { |
---|
629 | irc_usermsg( irc, "Did I ask you something?" ); |
---|
630 | return; |
---|
631 | } |
---|
632 | |
---|
633 | /* If there's an argument, the user seems to want to answer another question than the |
---|
634 | first/last (depending on the query_order setting) one. */ |
---|
635 | if( cmd[1] ) |
---|
636 | { |
---|
637 | if( sscanf( cmd[1], "%d", &numq ) != 1 ) |
---|
638 | { |
---|
639 | irc_usermsg( irc, "Invalid query number" ); |
---|
640 | return; |
---|
641 | } |
---|
642 | |
---|
643 | for( q = irc->queries; q; q = q->next, numq -- ) |
---|
644 | if( numq == 0 ) |
---|
645 | break; |
---|
646 | |
---|
647 | if( !q ) |
---|
648 | { |
---|
649 | irc_usermsg( irc, "Uhm, I never asked you something like that..." ); |
---|
650 | return; |
---|
651 | } |
---|
652 | } |
---|
653 | |
---|
654 | if( g_strcasecmp( cmd[0], "yes" ) == 0 ) |
---|
655 | query_answer( irc, q, 1 ); |
---|
656 | else if( g_strcasecmp( cmd[0], "no" ) == 0 ) |
---|
657 | query_answer( irc, q, 0 ); |
---|
658 | } |
---|
659 | |
---|
660 | static void cmd_set( irc_t *irc, char **cmd ) |
---|
661 | { |
---|
662 | if( cmd[1] && cmd[2] ) |
---|
663 | { |
---|
664 | set_setstr( irc, cmd[1], cmd[2] ); |
---|
665 | |
---|
666 | if( ( strcmp( cmd[2], "=" ) ) == 0 && cmd[3] ) |
---|
667 | irc_usermsg( irc, "Warning: Correct syntax: \002set <variable> <value>\002 (without =)" ); |
---|
668 | } |
---|
669 | if( cmd[1] ) /* else 'forgotten' on purpose.. Must show new value after changing */ |
---|
670 | { |
---|
671 | char *s = set_getstr( irc, cmd[1] ); |
---|
672 | if( s ) |
---|
673 | irc_usermsg( irc, "%s = `%s'", cmd[1], s ); |
---|
674 | } |
---|
675 | else |
---|
676 | { |
---|
677 | set_t *s = irc->set; |
---|
678 | while( s ) |
---|
679 | { |
---|
680 | if( s->value || s->def ) |
---|
681 | irc_usermsg( irc, "%s = `%s'", s->key, s->value?s->value:s->def ); |
---|
682 | s = s->next; |
---|
683 | } |
---|
684 | } |
---|
685 | } |
---|
686 | |
---|
687 | static void cmd_save( irc_t *irc, char **cmd ) |
---|
688 | { |
---|
689 | if( storage_save( irc, TRUE ) == STORAGE_OK ) |
---|
690 | irc_usermsg( irc, "Configuration saved" ); |
---|
691 | else |
---|
692 | irc_usermsg( irc, "Configuration could not be saved!" ); |
---|
693 | } |
---|
694 | |
---|
695 | static void cmd_blist( irc_t *irc, char **cmd ) |
---|
696 | { |
---|
697 | int online = 0, away = 0, offline = 0; |
---|
698 | user_t *u; |
---|
699 | char s[256]; |
---|
700 | char *format; |
---|
701 | int n_online = 0, n_away = 0, n_offline = 0; |
---|
702 | |
---|
703 | if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 ) |
---|
704 | online = offline = away = 1; |
---|
705 | else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 ) |
---|
706 | offline = 1; |
---|
707 | else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 ) |
---|
708 | away = 1; |
---|
709 | else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 ) |
---|
710 | online = 1; |
---|
711 | else |
---|
712 | online = away = 1; |
---|
713 | |
---|
714 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
715 | format = "%s\t%s\t%s"; |
---|
716 | else |
---|
717 | format = "%-16.16s %-40.40s %s"; |
---|
718 | |
---|
719 | irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" ); |
---|
720 | |
---|
721 | for( u = irc->users; u; u = u->next ) if( u->gc && u->online && !u->away ) |
---|
722 | { |
---|
723 | if( online == 1 ) |
---|
724 | { |
---|
725 | g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->gc->user->prpl->name ); |
---|
726 | irc_usermsg( irc, format, u->nick, s, "Online" ); |
---|
727 | } |
---|
728 | |
---|
729 | n_online ++; |
---|
730 | } |
---|
731 | |
---|
732 | for( u = irc->users; u; u = u->next ) if( u->gc && u->online && u->away ) |
---|
733 | { |
---|
734 | if( away == 1 ) |
---|
735 | { |
---|
736 | g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->gc->user->prpl->name ); |
---|
737 | irc_usermsg( irc, format, u->nick, s, u->away ); |
---|
738 | } |
---|
739 | n_away ++; |
---|
740 | } |
---|
741 | |
---|
742 | for( u = irc->users; u; u = u->next ) if( u->gc && !u->online ) |
---|
743 | { |
---|
744 | if( offline == 1 ) |
---|
745 | { |
---|
746 | g_snprintf( s, sizeof( s ) - 1, "%s@%s (%s)", u->user, u->host, u->gc->user->prpl->name ); |
---|
747 | irc_usermsg( irc, format, u->nick, s, "Offline" ); |
---|
748 | } |
---|
749 | n_offline ++; |
---|
750 | } |
---|
751 | |
---|
752 | irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline ); |
---|
753 | } |
---|
754 | |
---|
755 | static void cmd_nick( irc_t *irc, char **cmd ) |
---|
756 | { |
---|
757 | account_t *a; |
---|
758 | |
---|
759 | if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) ) |
---|
760 | { |
---|
761 | irc_usermsg( irc, "Invalid account"); |
---|
762 | } |
---|
763 | else if( !( a->gc && ( a->gc->flags & OPT_LOGGED_IN ) ) ) |
---|
764 | { |
---|
765 | irc_usermsg( irc, "That account is not on-line" ); |
---|
766 | } |
---|
767 | else if ( !cmd[2] ) |
---|
768 | { |
---|
769 | irc_usermsg( irc, "Your name is `%s'" , a->gc->displayname ? a->gc->displayname : "NULL" ); |
---|
770 | } |
---|
771 | else if ( !a->gc->prpl->set_info ) |
---|
772 | { |
---|
773 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
774 | } |
---|
775 | else |
---|
776 | { |
---|
777 | irc_usermsg( irc, "Setting your name to `%s'", cmd[2] ); |
---|
778 | |
---|
779 | a->gc->prpl->set_info( a->gc, cmd[2] ); |
---|
780 | } |
---|
781 | } |
---|
782 | |
---|
783 | static void cmd_qlist( irc_t *irc, char **cmd ) |
---|
784 | { |
---|
785 | query_t *q = irc->queries; |
---|
786 | int num; |
---|
787 | |
---|
788 | if( !q ) |
---|
789 | { |
---|
790 | irc_usermsg( irc, "There are no pending questions." ); |
---|
791 | return; |
---|
792 | } |
---|
793 | |
---|
794 | irc_usermsg( irc, "Pending queries:" ); |
---|
795 | |
---|
796 | for( num = 0; q; q = q->next, num ++ ) |
---|
797 | if( q->gc ) /* Not necessary yet, but it might come later */ |
---|
798 | irc_usermsg( irc, "%d, %s(%s): %s", num, q->gc->prpl->name, q->gc->username, q->question ); |
---|
799 | else |
---|
800 | irc_usermsg( irc, "%d, BitlBee: %s", num, q->question ); |
---|
801 | } |
---|
802 | |
---|
803 | static void cmd_import_buddies( irc_t *irc, char **cmd ) |
---|
804 | { |
---|
805 | struct gaim_connection *gc; |
---|
806 | account_t *a; |
---|
807 | nick_t *n; |
---|
808 | |
---|
809 | if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
810 | { |
---|
811 | irc_usermsg( irc, "Invalid account" ); |
---|
812 | return; |
---|
813 | } |
---|
814 | else if( !( ( gc = a->gc ) && ( a->gc->flags & OPT_LOGGED_IN ) ) ) |
---|
815 | { |
---|
816 | irc_usermsg( irc, "That account is not on-line" ); |
---|
817 | return; |
---|
818 | } |
---|
819 | |
---|
820 | if( cmd[2] ) |
---|
821 | { |
---|
822 | if( g_strcasecmp( cmd[2], "clear" ) == 0 ) |
---|
823 | { |
---|
824 | user_t *u; |
---|
825 | |
---|
826 | for( u = irc->users; u; u = u->next ) |
---|
827 | if( u->gc == gc ) |
---|
828 | { |
---|
829 | u->gc->prpl->remove_buddy( u->gc, u->handle, NULL ); |
---|
830 | user_del( irc, u->nick ); |
---|
831 | } |
---|
832 | |
---|
833 | irc_usermsg( irc, "Old buddy list cleared." ); |
---|
834 | } |
---|
835 | else |
---|
836 | { |
---|
837 | irc_usermsg( irc, "Invalid argument: %s", cmd[2] ); |
---|
838 | return; |
---|
839 | } |
---|
840 | } |
---|
841 | |
---|
842 | for( n = gc->irc->nicks; n; n = n->next ) |
---|
843 | { |
---|
844 | if( n->proto == gc->prpl && !user_findhandle( gc, n->handle ) ) |
---|
845 | { |
---|
846 | gc->prpl->add_buddy( gc, n->handle ); |
---|
847 | add_buddy( gc, NULL, n->handle, NULL ); |
---|
848 | } |
---|
849 | } |
---|
850 | |
---|
851 | irc_usermsg( irc, "Sent all add requests. Please wait for a while, the server needs some time to handle all the adds." ); |
---|
852 | } |
---|
853 | |
---|
854 | const command_t commands[] = { |
---|
855 | { "help", 0, cmd_help, 0 }, |
---|
856 | { "identify", 1, cmd_identify, 0 }, |
---|
857 | { "register", 1, cmd_register, 0 }, |
---|
858 | { "drop", 1, cmd_drop, 0 }, |
---|
859 | { "account", 1, cmd_account, 0 }, |
---|
860 | { "add", 2, cmd_add, 0 }, |
---|
861 | { "info", 1, cmd_info, 0 }, |
---|
862 | { "rename", 2, cmd_rename, 0 }, |
---|
863 | { "remove", 1, cmd_remove, 0 }, |
---|
864 | { "block", 1, cmd_block, 0 }, |
---|
865 | { "allow", 1, cmd_allow, 0 }, |
---|
866 | { "save", 0, cmd_save, 0 }, |
---|
867 | { "set", 0, cmd_set, 0 }, |
---|
868 | { "yes", 0, cmd_yesno, 0 }, |
---|
869 | { "no", 0, cmd_yesno, 0 }, |
---|
870 | { "blist", 0, cmd_blist, 0 }, |
---|
871 | { "nick", 1, cmd_nick, 0 }, |
---|
872 | { "import_buddies", 1, cmd_import_buddies, 0 }, |
---|
873 | { "qlist", 0, cmd_qlist, 0 }, |
---|
874 | { NULL } |
---|
875 | }; |
---|