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 | else |
---|
60 | { |
---|
61 | break; |
---|
62 | } |
---|
63 | } |
---|
64 | else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) ) |
---|
65 | { |
---|
66 | char *cpy; |
---|
67 | |
---|
68 | for( cpy = s; *cpy; cpy ++ ) |
---|
69 | cpy[0] = cpy[1]; |
---|
70 | } |
---|
71 | else if( *s == q ) |
---|
72 | { |
---|
73 | q = *s = 0; |
---|
74 | } |
---|
75 | cmd[k] = NULL; |
---|
76 | |
---|
77 | root_command( irc, cmd ); |
---|
78 | } |
---|
79 | |
---|
80 | void root_command( irc_t *irc, char *cmd[] ) |
---|
81 | { |
---|
82 | int i; |
---|
83 | |
---|
84 | if( !cmd[0] ) |
---|
85 | return; |
---|
86 | |
---|
87 | for( i = 0; commands[i].command; i++ ) |
---|
88 | if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) |
---|
89 | { |
---|
90 | if( !cmd[commands[i].required_parameters] ) |
---|
91 | { |
---|
92 | irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters ); |
---|
93 | return; |
---|
94 | } |
---|
95 | commands[i].execute( irc, cmd ); |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] ); |
---|
100 | } |
---|
101 | |
---|
102 | static void cmd_help( irc_t *irc, char **cmd ) |
---|
103 | { |
---|
104 | char param[80]; |
---|
105 | int i; |
---|
106 | char *s; |
---|
107 | |
---|
108 | memset( param, 0, sizeof(param) ); |
---|
109 | for ( i = 1; (cmd[i] != NULL && ( strlen(param) < (sizeof(param)-1) ) ); i++ ) { |
---|
110 | if ( i != 1 ) // prepend space except for the first parameter |
---|
111 | strcat(param, " "); |
---|
112 | strncat( param, cmd[i], sizeof(param) - strlen(param) - 1 ); |
---|
113 | } |
---|
114 | |
---|
115 | s = help_get( &(global.help), param ); |
---|
116 | if( !s ) s = help_get( &(global.help), "" ); |
---|
117 | |
---|
118 | if( s ) |
---|
119 | { |
---|
120 | irc_usermsg( irc, "%s", s ); |
---|
121 | g_free( s ); |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | irc_usermsg( irc, "Error opening helpfile." ); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | static void cmd_account( irc_t *irc, char **cmd ); |
---|
130 | |
---|
131 | static void cmd_identify( irc_t *irc, char **cmd ) |
---|
132 | { |
---|
133 | storage_status_t status = storage_load( irc->nick, cmd[1], irc ); |
---|
134 | char *account_on[] = { "account", "on", NULL }; |
---|
135 | |
---|
136 | switch (status) { |
---|
137 | case STORAGE_INVALID_PASSWORD: |
---|
138 | irc_usermsg( irc, "Incorrect password" ); |
---|
139 | break; |
---|
140 | case STORAGE_NO_SUCH_USER: |
---|
141 | irc_usermsg( irc, "The nick is (probably) not registered" ); |
---|
142 | break; |
---|
143 | case STORAGE_OK: |
---|
144 | irc_usermsg( irc, "Password accepted, settings and accounts loaded" ); |
---|
145 | irc_umode_set( irc, "+R", 1 ); |
---|
146 | if( set_getbool( &irc->set, "auto_connect" ) ) |
---|
147 | cmd_account( irc, account_on ); |
---|
148 | break; |
---|
149 | case STORAGE_OTHER_ERROR: |
---|
150 | default: |
---|
151 | irc_usermsg( irc, "Unknown error while loading configuration" ); |
---|
152 | break; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | static void cmd_register( irc_t *irc, char **cmd ) |
---|
157 | { |
---|
158 | if( global.conf->authmode == AUTHMODE_REGISTERED ) |
---|
159 | { |
---|
160 | irc_usermsg( irc, "This server does not allow registering new accounts" ); |
---|
161 | return; |
---|
162 | } |
---|
163 | |
---|
164 | irc_setpass( irc, cmd[1] ); |
---|
165 | switch( storage_save( irc, FALSE )) { |
---|
166 | case STORAGE_ALREADY_EXISTS: |
---|
167 | irc_usermsg( irc, "Nick is already registered" ); |
---|
168 | break; |
---|
169 | |
---|
170 | case STORAGE_OK: |
---|
171 | irc_usermsg( irc, "Account successfully created" ); |
---|
172 | irc->status |= USTATUS_IDENTIFIED; |
---|
173 | irc_umode_set( irc, "+R", 1 ); |
---|
174 | break; |
---|
175 | |
---|
176 | default: |
---|
177 | irc_usermsg( irc, "Error registering" ); |
---|
178 | break; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | static void cmd_drop( irc_t *irc, char **cmd ) |
---|
183 | { |
---|
184 | storage_status_t status; |
---|
185 | |
---|
186 | status = storage_remove (irc->nick, cmd[1]); |
---|
187 | switch (status) { |
---|
188 | case STORAGE_NO_SUCH_USER: |
---|
189 | irc_usermsg( irc, "That account does not exist" ); |
---|
190 | break; |
---|
191 | case STORAGE_INVALID_PASSWORD: |
---|
192 | irc_usermsg( irc, "Password invalid" ); |
---|
193 | break; |
---|
194 | case STORAGE_OK: |
---|
195 | irc_setpass( irc, NULL ); |
---|
196 | irc->status &= ~USTATUS_IDENTIFIED; |
---|
197 | irc_umode_set( irc, "-R", 1 ); |
---|
198 | irc_usermsg( irc, "Account `%s' removed", irc->nick ); |
---|
199 | break; |
---|
200 | default: |
---|
201 | irc_usermsg( irc, "Error: `%d'", status ); |
---|
202 | break; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | void cmd_account_del_yes( gpointer w, void *data ) |
---|
207 | { |
---|
208 | account_t **aptr = data; |
---|
209 | irc_t *irc = (*aptr)->irc; |
---|
210 | |
---|
211 | if( (*aptr)->ic ) |
---|
212 | { |
---|
213 | irc_usermsg( irc, "Account is still logged in, can't delete" ); |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | account_del( irc, (*aptr) ); |
---|
218 | irc_usermsg( irc, "Account deleted" ); |
---|
219 | } |
---|
220 | g_free( aptr ); |
---|
221 | } |
---|
222 | |
---|
223 | void cmd_account_del_no( gpointer w, void *data ) |
---|
224 | { |
---|
225 | g_free( data ); |
---|
226 | } |
---|
227 | |
---|
228 | static void cmd_account( irc_t *irc, char **cmd ) |
---|
229 | { |
---|
230 | account_t *a; |
---|
231 | |
---|
232 | if( global.conf->authmode == AUTHMODE_REGISTERED && !( irc->status & USTATUS_IDENTIFIED ) ) |
---|
233 | { |
---|
234 | irc_usermsg( irc, "This server only accepts registered users" ); |
---|
235 | return; |
---|
236 | } |
---|
237 | |
---|
238 | if( g_strcasecmp( cmd[1], "add" ) == 0 ) |
---|
239 | { |
---|
240 | struct prpl *prpl; |
---|
241 | |
---|
242 | if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL ) |
---|
243 | { |
---|
244 | irc_usermsg( irc, "Not enough parameters" ); |
---|
245 | return; |
---|
246 | } |
---|
247 | |
---|
248 | prpl = find_protocol(cmd[2]); |
---|
249 | |
---|
250 | if( prpl == NULL ) |
---|
251 | { |
---|
252 | irc_usermsg( irc, "Unknown protocol" ); |
---|
253 | return; |
---|
254 | } |
---|
255 | |
---|
256 | a = account_add( irc, prpl, cmd[3], cmd[4] ); |
---|
257 | if( cmd[5] ) |
---|
258 | { |
---|
259 | irc_usermsg( irc, "Warning: Passing a servername/other flags to `account add' " |
---|
260 | "is now deprecated. Use `account set' instead." ); |
---|
261 | set_setstr( &a->set, "server", cmd[5] ); |
---|
262 | } |
---|
263 | |
---|
264 | irc_usermsg( irc, "Account successfully added" ); |
---|
265 | } |
---|
266 | else if( g_strcasecmp( cmd[1], "del" ) == 0 ) |
---|
267 | { |
---|
268 | if( !cmd[2] ) |
---|
269 | { |
---|
270 | irc_usermsg( irc, "Not enough parameters given (need %d)", 2 ); |
---|
271 | } |
---|
272 | else if( !( a = account_get( irc, cmd[2] ) ) ) |
---|
273 | { |
---|
274 | irc_usermsg( irc, "Invalid account" ); |
---|
275 | } |
---|
276 | else if( a->ic ) |
---|
277 | { |
---|
278 | irc_usermsg( irc, "Account is still logged in, can't delete" ); |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | account_t **aptr; |
---|
283 | char *msg; |
---|
284 | |
---|
285 | aptr = g_malloc( sizeof( aptr ) ); |
---|
286 | *aptr = a; |
---|
287 | |
---|
288 | msg = g_strdup_printf( "If you remove this account (%s(%s)), BitlBee will " |
---|
289 | "also forget all your saved nicknames. If you want " |
---|
290 | "to change your username/password, use the `account " |
---|
291 | "set' command. Are you sure you want to delete this " |
---|
292 | "account?", a->prpl->name, a->user ); |
---|
293 | query_add( irc, NULL, msg, cmd_account_del_yes, cmd_account_del_no, aptr ); |
---|
294 | g_free( msg ); |
---|
295 | } |
---|
296 | } |
---|
297 | else if( g_strcasecmp( cmd[1], "list" ) == 0 ) |
---|
298 | { |
---|
299 | int i = 0; |
---|
300 | |
---|
301 | if( strchr( irc->umode, 'b' ) ) |
---|
302 | irc_usermsg( irc, "Account list:" ); |
---|
303 | |
---|
304 | for( a = irc->accounts; a; a = a->next ) |
---|
305 | { |
---|
306 | char *con; |
---|
307 | |
---|
308 | if( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) |
---|
309 | con = " (connected)"; |
---|
310 | else if( a->ic ) |
---|
311 | con = " (connecting)"; |
---|
312 | else if( a->reconnect ) |
---|
313 | con = " (awaiting reconnect)"; |
---|
314 | else |
---|
315 | con = ""; |
---|
316 | |
---|
317 | irc_usermsg( irc, "%2d. %s, %s%s", i, a->prpl->name, a->user, con ); |
---|
318 | |
---|
319 | i ++; |
---|
320 | } |
---|
321 | irc_usermsg( irc, "End of account list" ); |
---|
322 | } |
---|
323 | else if( g_strcasecmp( cmd[1], "on" ) == 0 ) |
---|
324 | { |
---|
325 | if( cmd[2] ) |
---|
326 | { |
---|
327 | if( ( a = account_get( irc, cmd[2] ) ) ) |
---|
328 | { |
---|
329 | if( a->ic ) |
---|
330 | { |
---|
331 | irc_usermsg( irc, "Account already online" ); |
---|
332 | return; |
---|
333 | } |
---|
334 | else |
---|
335 | { |
---|
336 | account_on( irc, a ); |
---|
337 | } |
---|
338 | } |
---|
339 | else |
---|
340 | { |
---|
341 | irc_usermsg( irc, "Invalid account" ); |
---|
342 | return; |
---|
343 | } |
---|
344 | } |
---|
345 | else |
---|
346 | { |
---|
347 | if ( irc->accounts ) { |
---|
348 | irc_usermsg( irc, "Trying to get all accounts connected..." ); |
---|
349 | |
---|
350 | for( a = irc->accounts; a; a = a->next ) |
---|
351 | if( !a->ic && a->auto_connect ) |
---|
352 | account_on( irc, a ); |
---|
353 | } |
---|
354 | else |
---|
355 | { |
---|
356 | irc_usermsg( irc, "No accounts known. Use `account add' to add one." ); |
---|
357 | } |
---|
358 | } |
---|
359 | } |
---|
360 | else if( g_strcasecmp( cmd[1], "off" ) == 0 ) |
---|
361 | { |
---|
362 | if( !cmd[2] ) |
---|
363 | { |
---|
364 | irc_usermsg( irc, "Deactivating all active (re)connections..." ); |
---|
365 | |
---|
366 | for( a = irc->accounts; a; a = a->next ) |
---|
367 | { |
---|
368 | if( a->ic ) |
---|
369 | account_off( irc, a ); |
---|
370 | else if( a->reconnect ) |
---|
371 | cancel_auto_reconnect( a ); |
---|
372 | } |
---|
373 | } |
---|
374 | else if( ( a = account_get( irc, cmd[2] ) ) ) |
---|
375 | { |
---|
376 | if( a->ic ) |
---|
377 | { |
---|
378 | account_off( irc, a ); |
---|
379 | } |
---|
380 | else if( a->reconnect ) |
---|
381 | { |
---|
382 | cancel_auto_reconnect( a ); |
---|
383 | irc_usermsg( irc, "Reconnect cancelled" ); |
---|
384 | } |
---|
385 | else |
---|
386 | { |
---|
387 | irc_usermsg( irc, "Account already offline" ); |
---|
388 | return; |
---|
389 | } |
---|
390 | } |
---|
391 | else |
---|
392 | { |
---|
393 | irc_usermsg( irc, "Invalid account" ); |
---|
394 | return; |
---|
395 | } |
---|
396 | } |
---|
397 | else if( g_strcasecmp( cmd[1], "set" ) == 0 ) |
---|
398 | { |
---|
399 | char *acc_handle, *set_name = NULL, *tmp; |
---|
400 | |
---|
401 | if( !cmd[2] ) |
---|
402 | { |
---|
403 | irc_usermsg( irc, "Not enough parameters given (need %d)", 2 ); |
---|
404 | return; |
---|
405 | } |
---|
406 | |
---|
407 | if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 ) |
---|
408 | acc_handle = g_strdup( cmd[3] ); |
---|
409 | else |
---|
410 | acc_handle = g_strdup( cmd[2] ); |
---|
411 | |
---|
412 | if( ( tmp = strchr( acc_handle, '/' ) ) ) |
---|
413 | { |
---|
414 | *tmp = 0; |
---|
415 | set_name = tmp + 1; |
---|
416 | } |
---|
417 | |
---|
418 | if( ( a = account_get( irc, acc_handle ) ) == NULL ) |
---|
419 | { |
---|
420 | g_free( acc_handle ); |
---|
421 | irc_usermsg( irc, "Invalid account" ); |
---|
422 | return; |
---|
423 | } |
---|
424 | |
---|
425 | if( cmd[3] && set_name ) |
---|
426 | { |
---|
427 | set_t *s = set_find( &a->set, set_name ); |
---|
428 | |
---|
429 | if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY ) |
---|
430 | { |
---|
431 | g_free( acc_handle ); |
---|
432 | irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" ); |
---|
433 | return; |
---|
434 | } |
---|
435 | else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY ) |
---|
436 | { |
---|
437 | g_free( acc_handle ); |
---|
438 | irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" ); |
---|
439 | return; |
---|
440 | } |
---|
441 | |
---|
442 | if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 ) |
---|
443 | set_reset( &a->set, set_name ); |
---|
444 | else |
---|
445 | set_setstr( &a->set, set_name, cmd[3] ); |
---|
446 | } |
---|
447 | if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */ |
---|
448 | { |
---|
449 | char *s = set_getstr( &a->set, set_name ); |
---|
450 | if( s ) |
---|
451 | irc_usermsg( irc, "%s = `%s'", set_name, s ); |
---|
452 | else |
---|
453 | irc_usermsg( irc, "%s is empty", set_name ); |
---|
454 | } |
---|
455 | else |
---|
456 | { |
---|
457 | set_t *s = a->set; |
---|
458 | while( s ) |
---|
459 | { |
---|
460 | if( s->value || s->def ) |
---|
461 | irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def ); |
---|
462 | else |
---|
463 | irc_usermsg( irc, "%s is empty", s->key ); |
---|
464 | s = s->next; |
---|
465 | } |
---|
466 | } |
---|
467 | |
---|
468 | g_free( acc_handle ); |
---|
469 | } |
---|
470 | else |
---|
471 | { |
---|
472 | irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] ); |
---|
473 | } |
---|
474 | } |
---|
475 | |
---|
476 | static void cmd_add( irc_t *irc, char **cmd ) |
---|
477 | { |
---|
478 | account_t *a; |
---|
479 | int add_on_server = 1; |
---|
480 | |
---|
481 | if( g_strcasecmp( cmd[1], "-tmp" ) == 0 ) |
---|
482 | { |
---|
483 | add_on_server = 0; |
---|
484 | cmd ++; |
---|
485 | } |
---|
486 | |
---|
487 | if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
488 | { |
---|
489 | irc_usermsg( irc, "Invalid account" ); |
---|
490 | return; |
---|
491 | } |
---|
492 | else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
493 | { |
---|
494 | irc_usermsg( irc, "That account is not on-line" ); |
---|
495 | return; |
---|
496 | } |
---|
497 | |
---|
498 | if( cmd[3] ) |
---|
499 | { |
---|
500 | if( !nick_ok( cmd[3] ) ) |
---|
501 | { |
---|
502 | irc_usermsg( irc, "The requested nick `%s' is invalid", cmd[3] ); |
---|
503 | return; |
---|
504 | } |
---|
505 | else if( user_find( irc, cmd[3] ) ) |
---|
506 | { |
---|
507 | irc_usermsg( irc, "The requested nick `%s' already exists", cmd[3] ); |
---|
508 | return; |
---|
509 | } |
---|
510 | else |
---|
511 | { |
---|
512 | nick_set( a, cmd[2], cmd[3] ); |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | if( add_on_server ) |
---|
517 | a->ic->acc->prpl->add_buddy( a->ic, cmd[2], NULL ); |
---|
518 | else |
---|
519 | /* Yeah, officially this is a call-*back*... So if we just |
---|
520 | called add_buddy, we'll wait for the IM server to respond |
---|
521 | before we do this. */ |
---|
522 | imcb_add_buddy( a->ic, cmd[2], NULL ); |
---|
523 | |
---|
524 | irc_usermsg( irc, "Adding `%s' to your contact list", cmd[2] ); |
---|
525 | } |
---|
526 | |
---|
527 | static void cmd_info( irc_t *irc, char **cmd ) |
---|
528 | { |
---|
529 | struct im_connection *ic; |
---|
530 | account_t *a; |
---|
531 | |
---|
532 | if( !cmd[2] ) |
---|
533 | { |
---|
534 | user_t *u = user_find( irc, cmd[1] ); |
---|
535 | if( !u || !u->ic ) |
---|
536 | { |
---|
537 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
538 | return; |
---|
539 | } |
---|
540 | ic = u->ic; |
---|
541 | cmd[2] = u->handle; |
---|
542 | } |
---|
543 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
544 | { |
---|
545 | irc_usermsg( irc, "Invalid account" ); |
---|
546 | return; |
---|
547 | } |
---|
548 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
549 | { |
---|
550 | irc_usermsg( irc, "That account is not on-line" ); |
---|
551 | return; |
---|
552 | } |
---|
553 | |
---|
554 | if( !ic->acc->prpl->get_info ) |
---|
555 | { |
---|
556 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
557 | } |
---|
558 | else |
---|
559 | { |
---|
560 | ic->acc->prpl->get_info( ic, cmd[2] ); |
---|
561 | } |
---|
562 | } |
---|
563 | |
---|
564 | static void cmd_rename( irc_t *irc, char **cmd ) |
---|
565 | { |
---|
566 | user_t *u; |
---|
567 | |
---|
568 | if( g_strcasecmp( cmd[1], irc->nick ) == 0 ) |
---|
569 | { |
---|
570 | irc_usermsg( irc, "Nick `%s' can't be changed", cmd[1] ); |
---|
571 | } |
---|
572 | else if( user_find( irc, cmd[2] ) && ( nick_cmp( cmd[1], cmd[2] ) != 0 ) ) |
---|
573 | { |
---|
574 | irc_usermsg( irc, "Nick `%s' already exists", cmd[2] ); |
---|
575 | } |
---|
576 | else if( !nick_ok( cmd[2] ) ) |
---|
577 | { |
---|
578 | irc_usermsg( irc, "Nick `%s' is invalid", cmd[2] ); |
---|
579 | } |
---|
580 | else if( !( u = user_find( irc, cmd[1] ) ) ) |
---|
581 | { |
---|
582 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
583 | } |
---|
584 | else |
---|
585 | { |
---|
586 | user_rename( irc, cmd[1], cmd[2] ); |
---|
587 | irc_write( irc, ":%s!%s@%s NICK %s", cmd[1], u->user, u->host, cmd[2] ); |
---|
588 | if( g_strcasecmp( cmd[1], irc->mynick ) == 0 ) |
---|
589 | { |
---|
590 | g_free( irc->mynick ); |
---|
591 | irc->mynick = g_strdup( cmd[2] ); |
---|
592 | } |
---|
593 | else if( u->send_handler == buddy_send_handler ) |
---|
594 | { |
---|
595 | nick_set( u->ic->acc, u->handle, cmd[2] ); |
---|
596 | } |
---|
597 | |
---|
598 | irc_usermsg( irc, "Nick successfully changed" ); |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | static void cmd_remove( irc_t *irc, char **cmd ) |
---|
603 | { |
---|
604 | user_t *u; |
---|
605 | char *s; |
---|
606 | |
---|
607 | if( !( u = user_find( irc, cmd[1] ) ) || !u->ic ) |
---|
608 | { |
---|
609 | irc_usermsg( irc, "Buddy `%s' not found", cmd[1] ); |
---|
610 | return; |
---|
611 | } |
---|
612 | s = g_strdup( u->handle ); |
---|
613 | |
---|
614 | u->ic->acc->prpl->remove_buddy( u->ic, u->handle, NULL ); |
---|
615 | nick_del( u->ic->acc, u->handle ); |
---|
616 | user_del( irc, cmd[1] ); |
---|
617 | |
---|
618 | irc_usermsg( irc, "Buddy `%s' (nick %s) removed from contact list", s, cmd[1] ); |
---|
619 | g_free( s ); |
---|
620 | |
---|
621 | return; |
---|
622 | } |
---|
623 | |
---|
624 | static void cmd_block( irc_t *irc, char **cmd ) |
---|
625 | { |
---|
626 | struct im_connection *ic; |
---|
627 | account_t *a; |
---|
628 | |
---|
629 | if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic ) |
---|
630 | { |
---|
631 | char *format; |
---|
632 | GSList *l; |
---|
633 | |
---|
634 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
635 | format = "%s\t%s"; |
---|
636 | else |
---|
637 | format = "%-32.32s %-16.16s"; |
---|
638 | |
---|
639 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
640 | for( l = a->ic->deny; l; l = l->next ) |
---|
641 | { |
---|
642 | user_t *u = user_findhandle( a->ic, l->data ); |
---|
643 | irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" ); |
---|
644 | } |
---|
645 | irc_usermsg( irc, "End of list." ); |
---|
646 | |
---|
647 | return; |
---|
648 | } |
---|
649 | else if( !cmd[2] ) |
---|
650 | { |
---|
651 | user_t *u = user_find( irc, cmd[1] ); |
---|
652 | if( !u || !u->ic ) |
---|
653 | { |
---|
654 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
655 | return; |
---|
656 | } |
---|
657 | ic = u->ic; |
---|
658 | cmd[2] = u->handle; |
---|
659 | } |
---|
660 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
661 | { |
---|
662 | irc_usermsg( irc, "Invalid account" ); |
---|
663 | return; |
---|
664 | } |
---|
665 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
666 | { |
---|
667 | irc_usermsg( irc, "That account is not on-line" ); |
---|
668 | return; |
---|
669 | } |
---|
670 | |
---|
671 | if( !ic->acc->prpl->add_deny || !ic->acc->prpl->rem_permit ) |
---|
672 | { |
---|
673 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
674 | } |
---|
675 | else |
---|
676 | { |
---|
677 | imc_rem_allow( ic, cmd[2] ); |
---|
678 | imc_add_block( ic, cmd[2] ); |
---|
679 | irc_usermsg( irc, "Buddy `%s' moved from your allow- to your block-list", cmd[2] ); |
---|
680 | } |
---|
681 | } |
---|
682 | |
---|
683 | static void cmd_allow( irc_t *irc, char **cmd ) |
---|
684 | { |
---|
685 | struct im_connection *ic; |
---|
686 | account_t *a; |
---|
687 | |
---|
688 | if( !cmd[2] && ( a = account_get( irc, cmd[1] ) ) && a->ic ) |
---|
689 | { |
---|
690 | char *format; |
---|
691 | GSList *l; |
---|
692 | |
---|
693 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
694 | format = "%s\t%s"; |
---|
695 | else |
---|
696 | format = "%-32.32s %-16.16s"; |
---|
697 | |
---|
698 | irc_usermsg( irc, format, "Handle", "Nickname" ); |
---|
699 | for( l = a->ic->permit; l; l = l->next ) |
---|
700 | { |
---|
701 | user_t *u = user_findhandle( a->ic, l->data ); |
---|
702 | irc_usermsg( irc, format, l->data, u ? u->nick : "(none)" ); |
---|
703 | } |
---|
704 | irc_usermsg( irc, "End of list." ); |
---|
705 | |
---|
706 | return; |
---|
707 | } |
---|
708 | else if( !cmd[2] ) |
---|
709 | { |
---|
710 | user_t *u = user_find( irc, cmd[1] ); |
---|
711 | if( !u || !u->ic ) |
---|
712 | { |
---|
713 | irc_usermsg( irc, "Nick `%s' does not exist", cmd[1] ); |
---|
714 | return; |
---|
715 | } |
---|
716 | ic = u->ic; |
---|
717 | cmd[2] = u->handle; |
---|
718 | } |
---|
719 | else if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
720 | { |
---|
721 | irc_usermsg( irc, "Invalid account" ); |
---|
722 | return; |
---|
723 | } |
---|
724 | else if( !( ( ic = a->ic ) && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
725 | { |
---|
726 | irc_usermsg( irc, "That account is not on-line" ); |
---|
727 | return; |
---|
728 | } |
---|
729 | |
---|
730 | if( !ic->acc->prpl->rem_deny || !ic->acc->prpl->add_permit ) |
---|
731 | { |
---|
732 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
733 | } |
---|
734 | else |
---|
735 | { |
---|
736 | imc_rem_block( ic, cmd[2] ); |
---|
737 | imc_add_allow( ic, cmd[2] ); |
---|
738 | |
---|
739 | irc_usermsg( irc, "Buddy `%s' moved from your block- to your allow-list", cmd[2] ); |
---|
740 | } |
---|
741 | } |
---|
742 | |
---|
743 | static void cmd_yesno( irc_t *irc, char **cmd ) |
---|
744 | { |
---|
745 | query_t *q = NULL; |
---|
746 | int numq = 0; |
---|
747 | |
---|
748 | if( irc->queries == NULL ) |
---|
749 | { |
---|
750 | irc_usermsg( irc, "Did I ask you something?" ); |
---|
751 | return; |
---|
752 | } |
---|
753 | |
---|
754 | /* If there's an argument, the user seems to want to answer another question than the |
---|
755 | first/last (depending on the query_order setting) one. */ |
---|
756 | if( cmd[1] ) |
---|
757 | { |
---|
758 | if( sscanf( cmd[1], "%d", &numq ) != 1 ) |
---|
759 | { |
---|
760 | irc_usermsg( irc, "Invalid query number" ); |
---|
761 | return; |
---|
762 | } |
---|
763 | |
---|
764 | for( q = irc->queries; q; q = q->next, numq -- ) |
---|
765 | if( numq == 0 ) |
---|
766 | break; |
---|
767 | |
---|
768 | if( !q ) |
---|
769 | { |
---|
770 | irc_usermsg( irc, "Uhm, I never asked you something like that..." ); |
---|
771 | return; |
---|
772 | } |
---|
773 | } |
---|
774 | |
---|
775 | if( g_strcasecmp( cmd[0], "yes" ) == 0 ) |
---|
776 | query_answer( irc, q, 1 ); |
---|
777 | else if( g_strcasecmp( cmd[0], "no" ) == 0 ) |
---|
778 | query_answer( irc, q, 0 ); |
---|
779 | } |
---|
780 | |
---|
781 | static void cmd_set( irc_t *irc, char **cmd ) |
---|
782 | { |
---|
783 | char *set_name = cmd[1]; |
---|
784 | |
---|
785 | if( cmd[1] && cmd[2] ) |
---|
786 | { |
---|
787 | if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 ) |
---|
788 | { |
---|
789 | set_reset( &irc->set, cmd[2] ); |
---|
790 | set_name = cmd[2]; |
---|
791 | } |
---|
792 | else |
---|
793 | { |
---|
794 | set_setstr( &irc->set, cmd[1], cmd[2] ); |
---|
795 | } |
---|
796 | } |
---|
797 | if( set_name ) /* else 'forgotten' on purpose.. Must show new value after changing */ |
---|
798 | { |
---|
799 | char *s = set_getstr( &irc->set, set_name ); |
---|
800 | if( s ) |
---|
801 | irc_usermsg( irc, "%s = `%s'", set_name, s ); |
---|
802 | else |
---|
803 | irc_usermsg( irc, "%s is empty", set_name ); |
---|
804 | |
---|
805 | if( strchr( set_name, '/' ) ) |
---|
806 | irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." ); |
---|
807 | } |
---|
808 | else |
---|
809 | { |
---|
810 | set_t *s = irc->set; |
---|
811 | while( s ) |
---|
812 | { |
---|
813 | if( s->value || s->def ) |
---|
814 | irc_usermsg( irc, "%s = `%s'", s->key, s->value ? s->value : s->def ); |
---|
815 | else |
---|
816 | irc_usermsg( irc, "%s is empty", s->key ); |
---|
817 | s = s->next; |
---|
818 | } |
---|
819 | } |
---|
820 | } |
---|
821 | |
---|
822 | static void cmd_save( irc_t *irc, char **cmd ) |
---|
823 | { |
---|
824 | if( storage_save( irc, TRUE ) == STORAGE_OK ) |
---|
825 | irc_usermsg( irc, "Configuration saved" ); |
---|
826 | else |
---|
827 | irc_usermsg( irc, "Configuration could not be saved!" ); |
---|
828 | } |
---|
829 | |
---|
830 | static void cmd_blist( irc_t *irc, char **cmd ) |
---|
831 | { |
---|
832 | int online = 0, away = 0, offline = 0; |
---|
833 | user_t *u; |
---|
834 | char s[256]; |
---|
835 | char *format; |
---|
836 | int n_online = 0, n_away = 0, n_offline = 0; |
---|
837 | |
---|
838 | if( cmd[1] && g_strcasecmp( cmd[1], "all" ) == 0 ) |
---|
839 | online = offline = away = 1; |
---|
840 | else if( cmd[1] && g_strcasecmp( cmd[1], "offline" ) == 0 ) |
---|
841 | offline = 1; |
---|
842 | else if( cmd[1] && g_strcasecmp( cmd[1], "away" ) == 0 ) |
---|
843 | away = 1; |
---|
844 | else if( cmd[1] && g_strcasecmp( cmd[1], "online" ) == 0 ) |
---|
845 | online = 1; |
---|
846 | else |
---|
847 | online = away = 1; |
---|
848 | |
---|
849 | if( strchr( irc->umode, 'b' ) != NULL ) |
---|
850 | format = "%s\t%s\t%s"; |
---|
851 | else |
---|
852 | format = "%-16.16s %-40.40s %s"; |
---|
853 | |
---|
854 | irc_usermsg( irc, format, "Nick", "User/Host/Network", "Status" ); |
---|
855 | |
---|
856 | for( u = irc->users; u; u = u->next ) if( u->ic && u->online && !u->away ) |
---|
857 | { |
---|
858 | if( online == 1 ) |
---|
859 | { |
---|
860 | g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user ); |
---|
861 | irc_usermsg( irc, format, u->nick, s, "Online" ); |
---|
862 | } |
---|
863 | |
---|
864 | n_online ++; |
---|
865 | } |
---|
866 | |
---|
867 | for( u = irc->users; u; u = u->next ) if( u->ic && u->online && u->away ) |
---|
868 | { |
---|
869 | if( away == 1 ) |
---|
870 | { |
---|
871 | g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user ); |
---|
872 | irc_usermsg( irc, format, u->nick, s, u->away ); |
---|
873 | } |
---|
874 | n_away ++; |
---|
875 | } |
---|
876 | |
---|
877 | for( u = irc->users; u; u = u->next ) if( u->ic && !u->online ) |
---|
878 | { |
---|
879 | if( offline == 1 ) |
---|
880 | { |
---|
881 | g_snprintf( s, sizeof( s ) - 1, "%s@%s %s(%s)", u->user, u->host, u->ic->acc->prpl->name, u->ic->acc->user ); |
---|
882 | irc_usermsg( irc, format, u->nick, s, "Offline" ); |
---|
883 | } |
---|
884 | n_offline ++; |
---|
885 | } |
---|
886 | |
---|
887 | irc_usermsg( irc, "%d buddies (%d available, %d away, %d offline)", n_online + n_away + n_offline, n_online, n_away, n_offline ); |
---|
888 | } |
---|
889 | |
---|
890 | static void cmd_nick( irc_t *irc, char **cmd ) |
---|
891 | { |
---|
892 | account_t *a; |
---|
893 | |
---|
894 | if( !cmd[1] || !( a = account_get( irc, cmd[1] ) ) ) |
---|
895 | { |
---|
896 | irc_usermsg( irc, "Invalid account"); |
---|
897 | } |
---|
898 | else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
899 | { |
---|
900 | irc_usermsg( irc, "That account is not on-line" ); |
---|
901 | } |
---|
902 | else if ( !cmd[2] ) |
---|
903 | { |
---|
904 | irc_usermsg( irc, "Your name is `%s'" , a->ic->displayname ? a->ic->displayname : "NULL" ); |
---|
905 | } |
---|
906 | else if ( !a->prpl->set_my_name ) |
---|
907 | { |
---|
908 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
909 | } |
---|
910 | else |
---|
911 | { |
---|
912 | irc_usermsg( irc, "Setting your name to `%s'", cmd[2] ); |
---|
913 | |
---|
914 | a->prpl->set_my_name( a->ic, cmd[2] ); |
---|
915 | } |
---|
916 | } |
---|
917 | |
---|
918 | static void cmd_qlist( irc_t *irc, char **cmd ) |
---|
919 | { |
---|
920 | query_t *q = irc->queries; |
---|
921 | int num; |
---|
922 | |
---|
923 | if( !q ) |
---|
924 | { |
---|
925 | irc_usermsg( irc, "There are no pending questions." ); |
---|
926 | return; |
---|
927 | } |
---|
928 | |
---|
929 | irc_usermsg( irc, "Pending queries:" ); |
---|
930 | |
---|
931 | for( num = 0; q; q = q->next, num ++ ) |
---|
932 | if( q->ic ) /* Not necessary yet, but it might come later */ |
---|
933 | irc_usermsg( irc, "%d, %s(%s): %s", num, q->ic->acc->prpl->name, q->ic->acc->user, q->question ); |
---|
934 | else |
---|
935 | irc_usermsg( irc, "%d, BitlBee: %s", num, q->question ); |
---|
936 | } |
---|
937 | |
---|
938 | static void cmd_join_chat( irc_t *irc, char **cmd ) |
---|
939 | { |
---|
940 | account_t *a; |
---|
941 | struct im_connection *ic; |
---|
942 | char *chat, *channel, *nick = NULL, *password = NULL; |
---|
943 | struct groupchat *c; |
---|
944 | |
---|
945 | if( !( a = account_get( irc, cmd[1] ) ) ) |
---|
946 | { |
---|
947 | irc_usermsg( irc, "Invalid account" ); |
---|
948 | return; |
---|
949 | } |
---|
950 | else if( !( a->ic && ( a->ic->flags & OPT_LOGGED_IN ) ) ) |
---|
951 | { |
---|
952 | irc_usermsg( irc, "That account is not on-line" ); |
---|
953 | return; |
---|
954 | } |
---|
955 | else if( a->prpl->chat_join == NULL ) |
---|
956 | { |
---|
957 | irc_usermsg( irc, "Command `%s' not supported by this protocol", cmd[0] ); |
---|
958 | return; |
---|
959 | } |
---|
960 | ic = a->ic; |
---|
961 | |
---|
962 | chat = cmd[2]; |
---|
963 | if( cmd[3] ) |
---|
964 | { |
---|
965 | if( cmd[3][0] != '#' && cmd[3][0] != '&' ) |
---|
966 | channel = g_strdup_printf( "&%s", cmd[3] ); |
---|
967 | else |
---|
968 | channel = g_strdup( cmd[3] ); |
---|
969 | } |
---|
970 | else |
---|
971 | { |
---|
972 | char *s; |
---|
973 | |
---|
974 | channel = g_strdup_printf( "&%s", chat ); |
---|
975 | if( ( s = strchr( channel, '@' ) ) ) |
---|
976 | *s = 0; |
---|
977 | } |
---|
978 | if( cmd[3] && cmd[4] ) |
---|
979 | nick = cmd[4]; |
---|
980 | else |
---|
981 | nick = irc->nick; |
---|
982 | if( cmd[3] && cmd[4] && cmd[5] ) |
---|
983 | password = cmd[5]; |
---|
984 | |
---|
985 | if( !nick_ok( channel + 1 ) ) |
---|
986 | { |
---|
987 | irc_usermsg( irc, "Invalid channel name: %s", channel ); |
---|
988 | g_free( channel ); |
---|
989 | return; |
---|
990 | } |
---|
991 | else if( g_strcasecmp( channel, irc->channel ) == 0 || irc_chat_by_channel( irc, channel ) ) |
---|
992 | { |
---|
993 | irc_usermsg( irc, "Channel already exists: %s", channel ); |
---|
994 | g_free( channel ); |
---|
995 | return; |
---|
996 | } |
---|
997 | |
---|
998 | if( ( c = a->prpl->chat_join( ic, chat, nick, password ) ) ) |
---|
999 | { |
---|
1000 | g_free( c->channel ); |
---|
1001 | c->channel = channel; |
---|
1002 | } |
---|
1003 | else |
---|
1004 | { |
---|
1005 | irc_usermsg( irc, "Tried to join chat, not sure if this was successful" ); |
---|
1006 | g_free( channel ); |
---|
1007 | } |
---|
1008 | } |
---|
1009 | |
---|
1010 | const command_t commands[] = { |
---|
1011 | { "help", 0, cmd_help, 0 }, |
---|
1012 | { "identify", 1, cmd_identify, 0 }, |
---|
1013 | { "register", 1, cmd_register, 0 }, |
---|
1014 | { "drop", 1, cmd_drop, 0 }, |
---|
1015 | { "account", 1, cmd_account, 0 }, |
---|
1016 | { "add", 2, cmd_add, 0 }, |
---|
1017 | { "info", 1, cmd_info, 0 }, |
---|
1018 | { "rename", 2, cmd_rename, 0 }, |
---|
1019 | { "remove", 1, cmd_remove, 0 }, |
---|
1020 | { "block", 1, cmd_block, 0 }, |
---|
1021 | { "allow", 1, cmd_allow, 0 }, |
---|
1022 | { "save", 0, cmd_save, 0 }, |
---|
1023 | { "set", 0, cmd_set, 0 }, |
---|
1024 | { "yes", 0, cmd_yesno, 0 }, |
---|
1025 | { "no", 0, cmd_yesno, 0 }, |
---|
1026 | { "blist", 0, cmd_blist, 0 }, |
---|
1027 | { "nick", 1, cmd_nick, 0 }, |
---|
1028 | { "qlist", 0, cmd_qlist, 0 }, |
---|
1029 | { "join_chat", 2, cmd_join_chat, 0 }, |
---|
1030 | { NULL } |
---|
1031 | }; |
---|