Changeset c5bc47b for root_commands.c
- Timestamp:
- 2009-10-17T17:24:52Z (15 years ago)
- Branches:
- master
- Children:
- c48a033
- Parents:
- 0c41177 (diff), 2e44b1f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
root_commands.c
r0c41177 rc5bc47b 78 78 } 79 79 80 #define MIN_ARGS( x, y... ) \ 81 do \ 82 { \ 83 int blaat; \ 84 for( blaat = 0; blaat <= x; blaat ++ ) \ 85 if( cmd[blaat] == NULL ) \ 86 { \ 87 irc_usermsg( irc, "Not enough parameters given (need %d).", x ); \ 88 return y; \ 89 } \ 90 } while( 0 ) 91 80 92 void root_command( irc_t *irc, char *cmd[] ) 81 93 { … … 88 100 if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) 89 101 { 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 } 102 MIN_ARGS( commands[i].required_parameters ); 103 95 104 commands[i].execute( irc, cmd ); 96 105 return; … … 250 259 } 251 260 261 typedef set_t** (*cmd_set_findhead)( irc_t*, char* ); 262 typedef int (*cmd_set_checkflags)( irc_t*, set_t *set ); 263 264 static int cmd_set_real( irc_t *irc, char **cmd, cmd_set_findhead findhead, cmd_set_checkflags checkflags ) 265 { 266 char *set_full = NULL, *set_name = NULL, *tmp; 267 set_t **head; 268 269 if( cmd[1] && g_strncasecmp( cmd[1], "-del", 4 ) == 0 ) 270 { 271 MIN_ARGS( 2, 0 ); 272 set_full = cmd[2]; 273 } 274 else 275 set_full = cmd[1]; 276 277 if( findhead == NULL ) 278 { 279 set_name = set_full; 280 281 head = &irc->set; 282 } 283 else 284 { 285 char *id; 286 287 if( ( tmp = strchr( set_full, '/' ) ) ) 288 { 289 id = g_strndup( set_full, ( tmp - set_full ) ); 290 set_name = tmp + 1; 291 } 292 else 293 { 294 id = g_strdup( set_full ); 295 } 296 297 if( ( head = findhead( irc, id ) ) == NULL ) 298 { 299 g_free( id ); 300 irc_usermsg( irc, "Could not find setting." ); 301 return 0; 302 } 303 g_free( id ); 304 } 305 306 if( cmd[1] && cmd[2] && set_name ) 307 { 308 set_t *s = set_find( head, set_name ); 309 int st; 310 311 if( s && checkflags && checkflags( irc, s ) == 0 ) 312 return 0; 313 314 if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 ) 315 st = set_reset( head, set_name ); 316 else 317 st = set_setstr( head, set_name, cmd[2] ); 318 319 if( set_getstr( head, set_name ) == NULL ) 320 { 321 if( st ) 322 irc_usermsg( irc, "Setting changed successfully" ); 323 else 324 irc_usermsg( irc, "Failed to change setting" ); 325 } 326 else 327 { 328 cmd_showset( irc, head, set_name ); 329 } 330 } 331 else if( set_name ) 332 { 333 cmd_showset( irc, head, set_name ); 334 } 335 else 336 { 337 set_t *s = *head; 338 while( s ) 339 { 340 cmd_showset( irc, &s, s->key ); 341 s = s->next; 342 } 343 } 344 345 return 1; 346 } 347 348 static set_t **cmd_account_set_findhead( irc_t *irc, char *id ) 349 { 350 account_t *a; 351 352 if( ( a = account_get( irc, id ) ) ) 353 return &a->set; 354 else 355 return NULL; 356 } 357 358 static int cmd_account_set_checkflags( irc_t *irc, set_t *s ) 359 { 360 account_t *a = s->data; 361 362 if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY ) 363 { 364 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" ); 365 return 0; 366 } 367 else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY ) 368 { 369 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" ); 370 return 0; 371 } 372 373 return 1; 374 } 375 252 376 static void cmd_account( irc_t *irc, char **cmd ) 253 377 { … … 264 388 struct prpl *prpl; 265 389 266 if( cmd[2] == NULL || cmd[3] == NULL || cmd[4] == NULL ) 267 { 268 irc_usermsg( irc, "Not enough parameters" ); 269 return; 270 } 271 272 prpl = find_protocol(cmd[2]); 390 MIN_ARGS( 4 ); 391 392 prpl = find_protocol( cmd[2] ); 273 393 274 394 if( prpl == NULL ) … … 290 410 else if( g_strcasecmp( cmd[1], "del" ) == 0 ) 291 411 { 292 if( !cmd[2] ) 293 { 294 irc_usermsg( irc, "Not enough parameters given (need %d)", 2 ); 295 } 296 else if( !( a = account_get( irc, cmd[2] ) ) ) 412 MIN_ARGS( 2 ); 413 414 if( !( a = account_get( irc, cmd[2] ) ) ) 297 415 { 298 416 irc_usermsg( irc, "Invalid account" ); … … 422 540 else if( g_strcasecmp( cmd[1], "set" ) == 0 ) 423 541 { 424 char *acc_handle, *set_name = NULL, *tmp; 425 426 if( !cmd[2] ) 427 { 428 irc_usermsg( irc, "Not enough parameters given (need %d)", 2 ); 429 return; 430 } 431 432 if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 ) 433 acc_handle = g_strdup( cmd[3] ); 434 else 435 acc_handle = g_strdup( cmd[2] ); 436 437 if( !acc_handle ) 438 { 439 irc_usermsg( irc, "Not enough parameters given (need %d)", 3 ); 440 return; 441 } 442 443 if( ( tmp = strchr( acc_handle, '/' ) ) ) 444 { 445 *tmp = 0; 446 set_name = tmp + 1; 447 } 448 449 if( ( a = account_get( irc, acc_handle ) ) == NULL ) 450 { 451 g_free( acc_handle ); 452 irc_usermsg( irc, "Invalid account" ); 453 return; 454 } 455 456 if( cmd[3] && set_name ) 457 { 458 set_t *s = set_find( &a->set, set_name ); 459 int st; 460 461 if( a->ic && s && s->flags & ACC_SET_OFFLINE_ONLY ) 462 { 463 g_free( acc_handle ); 464 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "off" ); 465 return; 466 } 467 else if( !a->ic && s && s->flags & ACC_SET_ONLINE_ONLY ) 468 { 469 g_free( acc_handle ); 470 irc_usermsg( irc, "This setting can only be changed when the account is %s-line", "on" ); 471 return; 472 } 473 474 if( g_strncasecmp( cmd[2], "-del", 4 ) == 0 ) 475 st = set_reset( &a->set, set_name ); 476 else 477 st = set_setstr( &a->set, set_name, cmd[3] ); 478 479 if( set_getstr( &a->set, set_name ) == NULL ) 480 { 481 if( st ) 482 irc_usermsg( irc, "Setting changed successfully" ); 483 else 484 irc_usermsg( irc, "Failed to change setting" ); 485 } 486 else 487 { 488 cmd_showset( irc, &a->set, set_name ); 489 } 490 } 491 else if( set_name ) 492 { 493 cmd_showset( irc, &a->set, set_name ); 494 } 495 else 496 { 497 set_t *s = a->set; 498 while( s ) 499 { 500 cmd_showset( irc, &s, s->key ); 501 s = s->next; 502 } 503 } 504 505 g_free( acc_handle ); 506 } 507 else 508 { 509 irc_usermsg( irc, "Unknown command: account %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[1] ); 542 MIN_ARGS( 2 ); 543 544 cmd_set_real( irc, cmd + 1, cmd_account_set_findhead, cmd_account_set_checkflags ); 545 } 546 else 547 { 548 irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "account", cmd[1] ); 510 549 } 511 550 } … … 518 557 if( g_strcasecmp( cmd[1], "-tmp" ) == 0 ) 519 558 { 559 MIN_ARGS( 3 ); 520 560 add_on_server = 0; 521 561 cmd ++; … … 837 877 static void cmd_set( irc_t *irc, char **cmd ) 838 878 { 839 char *set_name = cmd[1]; 840 841 if( cmd[1] && cmd[2] ) 842 { 843 int st; 844 845 if( g_strncasecmp( cmd[1], "-del", 4 ) == 0 ) 846 { 847 st = set_reset( &irc->set, cmd[2] ); 848 set_name = cmd[2]; 849 } 850 else 851 { 852 st = set_setstr( &irc->set, cmd[1], cmd[2] ); 853 } 854 855 /* Normally we just show the variable's new/unchanged 856 value as feedback to the user, but this has always 857 caused confusion when changing the password. Give 858 other feedback instead: */ 859 if( set_getstr( &irc->set, set_name ) == NULL ) 860 { 861 if( st ) 862 irc_usermsg( irc, "Setting changed successfully" ); 863 else 864 irc_usermsg( irc, "Failed to change setting" ); 865 } 866 else 867 { 868 cmd_showset( irc, &irc->set, set_name ); 869 } 870 } 871 else if( set_name ) 872 { 873 cmd_showset( irc, &irc->set, set_name ); 874 875 if( strchr( set_name, '/' ) ) 876 irc_usermsg( irc, "Warning: / found in setting name, you're probably looking for the `account set' command." ); 877 } 878 else 879 { 880 set_t *s = irc->set; 881 while( s ) 882 { 883 cmd_showset( irc, &s, s->key ); 884 s = s->next; 885 } 886 } 879 cmd_set_real( irc, cmd, NULL, NULL ); 887 880 } 888 881 … … 1007 1000 static void cmd_join_chat( irc_t *irc, char **cmd ) 1008 1001 { 1002 irc_usermsg( irc, "This command is now obsolete. " 1003 "Please try the `chat' command instead." ); 1004 } 1005 1006 static set_t **cmd_chat_set_findhead( irc_t *irc, char *id ) 1007 { 1008 struct chat *c; 1009 1010 if( ( c = chat_get( irc, id ) ) ) 1011 return &c->set; 1012 else 1013 return NULL; 1014 } 1015 1016 static void cmd_chat( irc_t *irc, char **cmd ) 1017 { 1018 account_t *acc; 1019 struct chat *c; 1020 1021 if( g_strcasecmp( cmd[1], "add" ) == 0 ) 1022 { 1023 char *channel, *s; 1024 1025 MIN_ARGS( 3 ); 1026 1027 if( !( acc = account_get( irc, cmd[2] ) ) ) 1028 { 1029 irc_usermsg( irc, "Invalid account" ); 1030 return; 1031 } 1032 1033 if( cmd[4] == NULL ) 1034 { 1035 channel = g_strdup( cmd[3] ); 1036 if( ( s = strchr( channel, '@' ) ) ) 1037 *s = 0; 1038 } 1039 else 1040 { 1041 channel = g_strdup( cmd[4] ); 1042 } 1043 1044 if( strchr( CTYPES, channel[0] ) == NULL ) 1045 { 1046 s = g_strdup_printf( "%c%s", CTYPES[0], channel ); 1047 g_free( channel ); 1048 channel = s; 1049 } 1050 1051 if( ( c = chat_add( irc, acc, cmd[3], channel ) ) ) 1052 irc_usermsg( irc, "Chatroom added successfully." ); 1053 else 1054 irc_usermsg( irc, "Could not add chatroom." ); 1055 1056 g_free( channel ); 1057 } 1058 else if( g_strcasecmp( cmd[1], "list" ) == 0 ) 1059 { 1060 int i = 0; 1061 1062 if( strchr( irc->umode, 'b' ) ) 1063 irc_usermsg( irc, "Chatroom list:" ); 1064 1065 for( c = irc->chatrooms; c; c = c->next ) 1066 { 1067 irc_usermsg( irc, "%2d. %s(%s) %s, %s", i, c->acc->prpl->name, 1068 c->acc->user, c->handle, c->channel ); 1069 1070 i ++; 1071 } 1072 irc_usermsg( irc, "End of chatroom list" ); 1073 } 1074 else if( g_strcasecmp( cmd[1], "set" ) == 0 ) 1075 { 1076 MIN_ARGS( 2 ); 1077 1078 cmd_set_real( irc, cmd + 1, cmd_chat_set_findhead, NULL ); 1079 } 1080 else if( g_strcasecmp( cmd[1], "del" ) == 0 ) 1081 { 1082 MIN_ARGS( 2 ); 1083 1084 if( ( c = chat_get( irc, cmd[2] ) ) ) 1085 { 1086 chat_del( irc, c ); 1087 } 1088 else 1089 { 1090 irc_usermsg( irc, "Could not remove chat." ); 1091 } 1092 } 1093 else if( g_strcasecmp( cmd[1], "with" ) == 0 ) 1094 { 1095 user_t *u; 1096 1097 MIN_ARGS( 2 ); 1098 1099 if( ( u = user_find( irc, cmd[2] ) ) && u->ic && u->ic->acc->prpl->chat_with ) 1100 { 1101 if( !u->ic->acc->prpl->chat_with( u->ic, u->handle ) ) 1102 { 1103 irc_usermsg( irc, "(Possible) failure while trying to open " 1104 "a groupchat with %s.", u->nick ); 1105 } 1106 } 1107 else 1108 { 1109 irc_usermsg( irc, "Can't open a groupchat with %s.", cmd[2] ); 1110 } 1111 } 1112 else 1113 { 1114 irc_usermsg( irc, "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat", cmd[1] ); 1115 } 1116 1117 1118 1119 #if 0 1009 1120 account_t *a; 1010 1121 struct im_connection *ic; … … 1032 1143 if( cmd[3] ) 1033 1144 { 1034 if( cmd[3][0] != '#' && cmd[3][0] != '&')1145 if( strchr( CTYPES, cmd[3][0] ) == NULL ) 1035 1146 channel = g_strdup_printf( "&%s", cmd[3] ); 1036 1147 else … … 1075 1186 g_free( channel ); 1076 1187 } 1188 #endif 1077 1189 } 1078 1190 … … 1097 1209 { "qlist", 0, cmd_qlist, 0 }, 1098 1210 { "join_chat", 2, cmd_join_chat, 0 }, 1211 { "chat", 1, cmd_chat, 0 }, 1099 1212 { NULL } 1100 1213 };
Note: See TracChangeset
for help on using the changeset viewer.