comparison mcabber/mcabber/commands.c @ 2164:038c4d601011

Simplify handling of command '/quit'
author franky
date Fri, 17 Oct 2014 22:31:03 +0200
parents 798baf5db4eb
children f14537ee3476
comparison
equal deleted inserted replaced
2163:0ac8eea728d1 2164:038c4d601011
53 #define IMSTATUS_DONOTDISTURB "dnd" 53 #define IMSTATUS_DONOTDISTURB "dnd"
54 #ifdef WITH_DEPRECATED_STATUS_INVISIBLE 54 #ifdef WITH_DEPRECATED_STATUS_INVISIBLE
55 # define IMSTATUS_INVISIBLE "invisible" 55 # define IMSTATUS_INVISIBLE "invisible"
56 #endif 56 #endif
57 57
58 // Return value container for the following functions
59 static int retval_for_cmds;
60
61 // Commands callbacks 58 // Commands callbacks
62 static void do_roster(char *arg); 59 static void do_roster(char *arg);
63 static void do_status(char *arg); 60 static void do_status(char *arg);
64 static void do_status_to(char *arg); 61 static void do_status_to(char *arg);
65 static void do_add(char *arg); 62 static void do_add(char *arg);
76 static void do_set(char *arg); 73 static void do_set(char *arg);
77 static void do_alias(char *arg); 74 static void do_alias(char *arg);
78 static void do_bind(char *arg); 75 static void do_bind(char *arg);
79 static void do_connect(char *arg); 76 static void do_connect(char *arg);
80 static void do_disconnect(char *arg); 77 static void do_disconnect(char *arg);
78 static void do_quit(char *arg);
81 static void do_rawxml(char *arg); 79 static void do_rawxml(char *arg);
82 static void do_room(char *arg); 80 static void do_room(char *arg);
83 static void do_authorization(char *arg); 81 static void do_authorization(char *arg);
84 static void do_version(char *arg); 82 static void do_version(char *arg);
85 static void do_request(char *arg); 83 static void do_request(char *arg);
216 COMPL_MULTILINE, 0, &do_msay, NULL); 214 COMPL_MULTILINE, 0, &do_msay, NULL);
217 cmd_add("otr", "Manage OTR settings", COMPL_OTR, COMPL_JID, &do_otr, NULL); 215 cmd_add("otr", "Manage OTR settings", COMPL_OTR, COMPL_JID, &do_otr, NULL);
218 cmd_add("otrpolicy", "Manage OTR policies", COMPL_JID, COMPL_OTRPOLICY, 216 cmd_add("otrpolicy", "Manage OTR policies", COMPL_JID, COMPL_OTRPOLICY,
219 &do_otrpolicy, NULL); 217 &do_otrpolicy, NULL);
220 cmd_add("pgp", "Manage PGP settings", COMPL_PGP, COMPL_JID, &do_pgp, NULL); 218 cmd_add("pgp", "Manage PGP settings", COMPL_PGP, COMPL_JID, &do_pgp, NULL);
221 cmd_add("quit", "Exit the software", 0, 0, NULL, NULL); 219 cmd_add("quit", "Exit the software", 0, 0, &do_quit, NULL);
222 cmd_add("rawxml", "Send a raw XML string", 0, 0, &do_rawxml, NULL); 220 cmd_add("rawxml", "Send a raw XML string", 0, 0, &do_rawxml, NULL);
223 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename, NULL); 221 cmd_add("rename", "Rename the current buddy", 0, 0, &do_rename, NULL);
224 cmd_add("request", "Send a Jabber IQ request", COMPL_REQUEST, COMPL_JID, 222 cmd_add("request", "Send a Jabber IQ request", COMPL_REQUEST, COMPL_JID,
225 &do_request, NULL); 223 &do_request, NULL);
226 cmd_add("room", "MUC actions command", COMPL_ROOM, 0, &do_room, NULL); 224 cmd_add("room", "MUC actions command", COMPL_ROOM, 0, &do_room, NULL);
462 460
463 // process_command(line, iscmd) 461 // process_command(line, iscmd)
464 // Process a command line. 462 // Process a command line.
465 // If iscmd is TRUE, process the command even if verbatim mmode is set; 463 // If iscmd is TRUE, process the command even if verbatim mmode is set;
466 // it is intended to be used for key bindings. 464 // it is intended to be used for key bindings.
467 // Return 255 if this is the /quit command, and 0 for the other commands. 465 void process_command(const char *line, guint iscmd)
468 int process_command(const char *line, guint iscmd)
469 { 466 {
470 char *p; 467 char *p;
471 char *xpline; 468 char *xpline;
472 cmd *curcmd; 469 cmd *curcmd;
473 470
474 if (!line) 471 if (!line)
475 return 0; 472 return;
476 473
477 // We do alias expansion here 474 // We do alias expansion here
478 if (iscmd || scr_get_multimode() != 2) 475 if (iscmd || scr_get_multimode() != 2)
479 xpline = expandalias(line); 476 xpline = expandalias(line);
480 else 477 else
487 // Remove trailing spaces: 484 // Remove trailing spaces:
488 for (p=xpline ; *p ; p++) 485 for (p=xpline ; *p ; p++)
489 ; 486 ;
490 for (p-- ; p>xpline && (*p == ' ') ; p--) 487 for (p-- ; p>xpline && (*p == ' ') ; p--)
491 *p = 0; 488 *p = 0;
492
493 // Command "quit"?
494 if ((iscmd || scr_get_multimode() != 2)
495 && (!strncasecmp(xpline, mkcmdstr("quit"), strlen(mkcmdstr("quit"))))) {
496 if (!xpline[5] || xpline[5] == ' ') {
497 g_free(xpline);
498 return 255;
499 }
500 } else if (iscmd && !strncasecmp(xpline, "quit", 4) &&
501 (!xpline[4] || xpline[4] == ' ')) {
502 // If iscmd is true we can have the command without the command prefix
503 // character (usually '/').
504 g_free(xpline);
505 return 255;
506 }
507 489
508 // If verbatim multi-line mode, we check if another /msay command is typed 490 // If verbatim multi-line mode, we check if another /msay command is typed
509 if (!iscmd && scr_get_multimode() == 2 491 if (!iscmd && scr_get_multimode() == 2
510 && (strncasecmp(xpline, mkcmdstr("msay "), strlen(mkcmdstr("msay "))))) { 492 && (strncasecmp(xpline, mkcmdstr("msay "), strlen(mkcmdstr("msay "))))) {
511 // It isn't an /msay command 493 // It isn't an /msay command
512 scr_append_multiline(xpline); 494 scr_append_multiline(xpline);
513 g_free(xpline); 495 g_free(xpline);
514 return 0; 496 return;
515 } 497 }
516 498
517 // Commands handling 499 // Commands handling
518 curcmd = cmd_get(xpline); 500 curcmd = cmd_get(xpline);
519 501
520 if (!curcmd) { 502 if (!curcmd) {
521 scr_LogPrint(LPRINT_NORMAL, "Unrecognized command. " 503 scr_LogPrint(LPRINT_NORMAL, "Unrecognized command. "
522 "Please see the manual for a list of known commands."); 504 "Please see the manual for a list of known commands.");
523 g_free(xpline); 505 g_free(xpline);
524 return 0; 506 return;
525 } 507 }
526 if (!curcmd->func) { 508 if (!curcmd->func) {
527 scr_LogPrint(LPRINT_NORMAL, 509 scr_LogPrint(LPRINT_NORMAL,
528 "This functionality is not yet implemented, sorry."); 510 "This functionality is not yet implemented, sorry.");
529 g_free(xpline); 511 g_free(xpline);
530 return 0; 512 return;
531 } 513 }
532 // Lets go to the command parameters 514 // Lets go to the command parameters
533 for (p = xpline+1; *p && (*p != ' ') ; p++) 515 for (p = xpline+1; *p && (*p != ' ') ; p++)
534 ; 516 ;
535 // Skip spaces 517 // Skip spaces
536 while (*p && (*p == ' ')) 518 while (*p && (*p == ' '))
537 p++; 519 p++;
538 // Call command-specific function 520 // Call command-specific function
539 retval_for_cmds = 0;
540 #ifdef MODULES_ENABLE 521 #ifdef MODULES_ENABLE
541 if (curcmd->userdata) 522 if (curcmd->userdata)
542 (*(void (*)(char *p, gpointer u))curcmd->func)(p, curcmd->userdata); 523 (*(void (*)(char *p, gpointer u))curcmd->func)(p, curcmd->userdata);
543 else 524 else
544 (*curcmd->func)(p); 525 (*curcmd->func)(p);
545 #else 526 #else
546 (*curcmd->func)(p); 527 (*curcmd->func)(p);
547 #endif 528 #endif
548 g_free(xpline); 529 g_free(xpline);
549 return retval_for_cmds;
550 } 530 }
551 531
552 // process_line(line) 532 // process_line(line)
553 // Process a command/message line. 533 // Process a command/message line.
554 // If this isn't a command, this is a message and it is sent to the 534 // If this isn't a command, this is a message and it is sent to the
555 // currently selected buddy. 535 // currently selected buddy.
556 // Return 255 if the line is the /quit command, or 0. 536 void process_line(const char *line)
557 int process_line(const char *line)
558 { 537 {
559 if (!*line) { // User only pressed enter 538 if (!*line) { // User only pressed enter
560 if (scr_get_multimode()) { 539 if (scr_get_multimode()) {
561 scr_append_multiline(""); 540 scr_append_multiline("");
562 return 0; 541 return;
563 } 542 }
564 if (current_buddy) { 543 if (current_buddy) {
565 if (buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_GROUP) 544 if (buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_GROUP)
566 do_group("toggle"); 545 do_group("toggle");
567 else { 546 else {
568 // Enter chat mode 547 // Enter chat mode
569 scr_set_chatmode(TRUE); 548 scr_set_chatmode(TRUE);
570 scr_show_buddy_window(); 549 scr_show_buddy_window();
571 } 550 }
572 } 551 }
573 return 0; 552 return;
574 } 553 }
575 554
576 if (*line != COMMAND_CHAR) { 555 if (*line != COMMAND_CHAR) {
577 // This isn't a command 556 // This isn't a command
578 if (scr_get_multimode()) 557 if (scr_get_multimode())
579 scr_append_multiline(line); 558 scr_append_multiline(line);
580 else 559 else
581 say_cmd((char*)line, 0); 560 say_cmd((char*)line, 0);
582 return 0; 561 return;
583 } 562 }
584 563
585 /* It is _probably_ a command -- except for verbatim multi-line mode */ 564 /* It is _probably_ a command -- except for verbatim multi-line mode */
586 return process_command(line, FALSE); 565 process_command(line, FALSE);
587 } 566 }
588 567
589 // Helper routine for buffer item_{lock,unlock,toggle_lock} 568 // Helper routine for buffer item_{lock,unlock,toggle_lock}
590 // "lock" values: 1=lock 0=unlock -1=invert 569 // "lock" values: 1=lock 0=unlock -1=invert
591 static void roster_buddylock(char *bjid, int lock) 570 static void roster_buddylock(char *bjid, int lock)
2479 settings_set(SETTINGS_TYPE_BINDING, k_code, value_utf8); 2458 settings_set(SETTINGS_TYPE_BINDING, k_code, value_utf8);
2480 g_free(value_utf8); 2459 g_free(value_utf8);
2481 g_free(value); 2460 g_free(value);
2482 } 2461 }
2483 g_free(k_code); 2462 g_free(k_code);
2463 }
2464
2465 static void do_quit(char *arg)
2466 {
2467 mcabber_set_terminate_ui();
2484 } 2468 }
2485 2469
2486 static void do_rawxml(char *arg) 2470 static void do_rawxml(char *arg)
2487 { 2471 {
2488 char **paramlst; 2472 char **paramlst;
4072 } else if (!strcasecmp(arg, "iline_bdel")) { 4056 } else if (!strcasecmp(arg, "iline_bdel")) {
4073 readline_backward_kill_iline(); 4057 readline_backward_kill_iline();
4074 } else if (!strcasecmp(arg, "send_multiline")) { 4058 } else if (!strcasecmp(arg, "send_multiline")) {
4075 readline_send_multiline(); 4059 readline_send_multiline();
4076 } else if (!strcasecmp(arg, "iline_accept")) { 4060 } else if (!strcasecmp(arg, "iline_accept")) {
4077 retval_for_cmds = readline_accept_line(FALSE); 4061 readline_accept_line(FALSE);
4078 } else if (!strcasecmp(arg, "iline_accept_down_hist")) { 4062 } else if (!strcasecmp(arg, "iline_accept_down_hist")) {
4079 retval_for_cmds = readline_accept_line(TRUE); 4063 readline_accept_line(TRUE);
4080 } else if (!strcasecmp(arg, "compl_cancel")) { 4064 } else if (!strcasecmp(arg, "compl_cancel")) {
4081 readline_cancel_completion(); 4065 readline_cancel_completion();
4082 } else if (!strcasecmp(arg, "compl_do_fwd")) { 4066 } else if (!strcasecmp(arg, "compl_do_fwd")) {
4083 readline_do_completion(TRUE); 4067 readline_do_completion(TRUE);
4084 } else if (!strcasecmp(arg, "compl_do_bwd")) { 4068 } else if (!strcasecmp(arg, "compl_do_bwd")) {