Mercurial > hg
comparison mcabber/src/commands.c @ 557:c72a66dfd2d4
Use split_arg() in most commands
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Thu, 01 Dec 2005 00:12:03 +0100 |
parents | 11ac80e41c7d |
children | db019a5f874f |
comparison
equal
deleted
inserted
replaced
556:c4fee1a2c478 | 557:c72a66dfd2d4 |
---|---|
354 /* It is (probably) a command -- except for verbatim multi-line mode */ | 354 /* It is (probably) a command -- except for verbatim multi-line mode */ |
355 return process_command(line); | 355 return process_command(line); |
356 } | 356 } |
357 | 357 |
358 /* Commands callback functions */ | 358 /* Commands callback functions */ |
359 /* All these do_*() functions will be called with a "arg" parameter */ | |
360 /* (with arg not null) */ | |
359 | 361 |
360 static void do_roster(char *arg) | 362 static void do_roster(char *arg) |
361 { | 363 { |
362 if (!strcasecmp(arg, "top")) { | 364 if (!strcasecmp(arg, "top")) { |
363 scr_RosterTop(); | 365 scr_RosterTop(); |
410 // Set your Jabber status. | 412 // Set your Jabber status. |
411 // - if recipient is not NULL, the status is sent to this contact only | 413 // - if recipient is not NULL, the status is sent to this contact only |
412 // - arg must be "status message" (message is optional) | 414 // - arg must be "status message" (message is optional) |
413 static void setstatus(const char *recipient, const char *arg) | 415 static void setstatus(const char *recipient, const char *arg) |
414 { | 416 { |
417 char **paramlst; | |
418 char *status; | |
419 char *msg; | |
415 enum imstatus st; | 420 enum imstatus st; |
416 int len; | |
417 char *msg; | |
418 | 421 |
419 if (!jb_getonline()) { | 422 if (!jb_getonline()) { |
420 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); | 423 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); |
421 return; | 424 return; |
422 } | 425 } |
423 | 426 |
424 msg = strchr(arg, ' '); | 427 paramlst = split_arg(arg, 2, 0); // status, message |
425 if (!msg) | 428 status = *paramlst; |
426 len = strlen(arg); | 429 msg = *(paramlst+1); |
427 else | 430 |
428 len = msg - arg; | 431 if (!status) { |
429 | 432 free_arg_lst(paramlst); |
430 if (!strncasecmp(arg, "offline", len)) st = offline; | 433 return; |
431 else if (!strncasecmp(arg, "online", len)) st = available; | 434 } |
432 else if (!strncasecmp(arg, "avail", len)) st = available; | 435 |
433 else if (!strncasecmp(arg, "away", len)) st = away; | 436 if (!strcasecmp(status, "offline")) st = offline; |
434 else if (!strncasecmp(arg, "invisible", len)) st = invisible; | 437 else if (!strcasecmp(status, "online")) st = available; |
435 else if (!strncasecmp(arg, "dnd", len)) st = dontdisturb; | 438 else if (!strcasecmp(status, "avail")) st = available; |
436 else if (!strncasecmp(arg, "notavail", len)) st = notavail; | 439 else if (!strcasecmp(status, "away")) st = away; |
437 else if (!strncasecmp(arg, "free", len)) st = freeforchat; | 440 else if (!strcasecmp(status, "invisible")) st = invisible; |
441 else if (!strcasecmp(status, "dnd")) st = dontdisturb; | |
442 else if (!strcasecmp(status, "notavail")) st = notavail; | |
443 else if (!strcasecmp(status, "free")) st = freeforchat; | |
438 else { | 444 else { |
439 scr_LogPrint(LPRINT_NORMAL, "Unrecognized status!"); | 445 scr_LogPrint(LPRINT_NORMAL, "Unrecognized status!"); |
446 free_arg_lst(paramlst); | |
440 return; | 447 return; |
441 } | 448 } |
442 | 449 |
443 // Use provided message, unless requested status is "invisible" | 450 // Use provided message, unless requested status is "invisible" |
444 if (msg && st != invisible) { | 451 if (msg && st != invisible) { |
445 for (msg++ ; *msg && *msg == ' ' ; msg++) ; | |
446 if (!*msg) msg = NULL; | 452 if (!*msg) msg = NULL; |
447 } else | 453 } else |
448 msg = NULL; | 454 msg = NULL; |
449 | 455 |
450 // If a recipient is specified, let's don't use default status messages | 456 // If a recipient is specified, let's don't use default status messages |
451 if (recipient && !msg) | 457 if (recipient && !msg) |
452 msg = ""; | 458 msg = ""; |
453 | 459 |
454 jb_setstatus(st, recipient, msg); | 460 jb_setstatus(st, recipient, msg); |
461 | |
462 free_arg_lst(paramlst); | |
455 } | 463 } |
456 | 464 |
457 static void do_status(char *arg) | 465 static void do_status(char *arg) |
458 { | 466 { |
459 if (!arg || (!*arg)) { | 467 if (!*arg) { |
460 const char *sm = jb_getstatusmsg(); | 468 const char *sm = jb_getstatusmsg(); |
461 scr_LogPrint(LPRINT_NORMAL, "Your status is: [%c] %s", | 469 scr_LogPrint(LPRINT_NORMAL, "Your status is: [%c] %s", |
462 imstatus2char[jb_getstatus()], | 470 imstatus2char[jb_getstatus()], |
463 (sm ? sm : "")); | 471 (sm ? sm : "")); |
464 return; | 472 return; |
466 setstatus(NULL, arg); | 474 setstatus(NULL, arg); |
467 } | 475 } |
468 | 476 |
469 static void do_status_to(char *arg) | 477 static void do_status_to(char *arg) |
470 { | 478 { |
471 char *id, *st; | 479 char **paramlst; |
472 if (!arg || (*arg == 0)) { | 480 char *jid, *st, *msg; |
473 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); | 481 |
474 return; | 482 paramlst = split_arg(arg, 3, 1); // jid, status, [message] |
475 } | 483 jid = *paramlst; |
476 | 484 st = *(paramlst+1); |
477 // Split recipient jid, status | 485 msg = *(paramlst+2); |
478 id = g_strdup(arg); | 486 |
479 st = strchr(id, ' '); | 487 if (!jid || !st) { |
480 if (!st) { | 488 scr_LogPrint(LPRINT_NORMAL, "Wrong usage"); |
481 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); | 489 } else if (check_jid_syntax(jid)) { |
482 g_free(id); | 490 scr_LogPrint(LPRINT_NORMAL, "<%s> is not a valid Jabber id", jid); |
483 return; | |
484 } | |
485 | |
486 *st++ = 0; | |
487 while (*st && *st == ' ') | |
488 st++; | |
489 | |
490 if (check_jid_syntax(id)) { | |
491 scr_LogPrint(LPRINT_NORMAL, "<%s> is not a valid Jabber id", id); | |
492 } else { | 491 } else { |
493 mc_strtolower(id); | 492 char *cmd; |
494 scr_LogPrint(LPRINT_LOGNORM, "Sending to <%s> /status %s", id, st); | 493 if (!msg) |
495 setstatus(id, st); | 494 msg = ""; |
496 } | 495 mc_strtolower(jid); |
497 g_free(id); | 496 cmd = g_strdup_printf("%s %s", st, msg); |
497 scr_LogPrint(LPRINT_LOGNORM, "Sending to <%s> /status %s", jid, cmd); | |
498 setstatus(jid, cmd); | |
499 g_free(cmd); | |
500 } | |
501 free_arg_lst(paramlst); | |
498 } | 502 } |
499 | 503 |
500 static void do_add(char *arg) | 504 static void do_add(char *arg) |
501 { | 505 { |
506 char **paramlst; | |
502 char *id, *nick; | 507 char *id, *nick; |
503 | 508 |
504 if (!jb_getonline()) { | 509 if (!jb_getonline()) { |
505 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); | 510 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); |
506 return; | 511 return; |
507 } | 512 } |
508 | 513 |
509 if (!arg || (!*arg)) { | 514 paramlst = split_arg(arg, 2, 0); // jid, [nickname] |
510 scr_LogPrint(LPRINT_NORMAL, "Wrong usage"); | 515 id = *paramlst; |
511 return; | 516 nick = *(paramlst+1); |
512 } | |
513 | |
514 id = g_strdup(arg); | |
515 nick = strchr(id, ' '); | |
516 if (nick) { | |
517 *nick++ = 0; | |
518 while (*nick && *nick == ' ') | |
519 nick++; | |
520 } | |
521 | 517 |
522 if (check_jid_syntax(id)) { | 518 if (check_jid_syntax(id)) { |
523 scr_LogPrint(LPRINT_NORMAL, "<%s> is not a valid Jabber id", id); | 519 if (!id) |
520 scr_LogPrint(LPRINT_NORMAL, "Wrong usage"); | |
521 else | |
522 scr_LogPrint(LPRINT_NORMAL, "<%s> is not a valid Jabber id", id); | |
524 } else { | 523 } else { |
525 mc_strtolower(id); | 524 mc_strtolower(id); |
526 // 2nd parameter = optional nickname | 525 // 2nd parameter = optional nickname |
527 jb_addbuddy(id, nick, NULL); | 526 jb_addbuddy(id, nick, NULL); |
528 scr_LogPrint(LPRINT_LOGNORM, "Sent presence notification request to <%s>", | 527 scr_LogPrint(LPRINT_LOGNORM, "Sent presence notification request to <%s>", |
529 id); | 528 id); |
530 } | 529 } |
531 g_free(id); | 530 free_arg_lst(paramlst); |
532 } | 531 } |
533 | 532 |
534 static void do_del(char *arg) | 533 static void do_del(char *arg) |
535 { | 534 { |
536 const char *jid; | 535 const char *jid; |
537 | 536 |
538 if (arg && (*arg)) { | 537 if (*arg) { |
539 scr_LogPrint(LPRINT_NORMAL, "Wrong usage"); | 538 scr_LogPrint(LPRINT_NORMAL, "Wrong usage"); |
540 return; | 539 return; |
541 } | 540 } |
542 | 541 |
543 if (!current_buddy) return; | 542 if (!current_buddy) return; |
560 static void do_group(char *arg) | 559 static void do_group(char *arg) |
561 { | 560 { |
562 gpointer group; | 561 gpointer group; |
563 guint leave_windowbuddy; | 562 guint leave_windowbuddy; |
564 | 563 |
565 if (!arg || (!*arg)) { | 564 if (!*arg) { |
566 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); | 565 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); |
567 return; | 566 return; |
568 } | 567 } |
569 | 568 |
570 if (!current_buddy) return; | 569 if (!current_buddy) return; |
675 scr_set_multimode(FALSE); | 674 scr_set_multimode(FALSE); |
676 } | 675 } |
677 | 676 |
678 static void do_say_to(char *arg) | 677 static void do_say_to(char *arg) |
679 { | 678 { |
679 char **paramlst; | |
680 char *jid, *msg; | 680 char *jid, *msg; |
681 char *bare_jid, *p; | 681 char *bare_jid, *p; |
682 | 682 |
683 if (!jb_getonline()) { | 683 if (!jb_getonline()) { |
684 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); | 684 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); |
685 return; | 685 return; |
686 } | 686 } |
687 | 687 |
688 msg = strchr(arg, ' '); | 688 paramlst = split_arg(arg, 2, 1); // jid, message |
689 if (!msg) { | 689 jid = *paramlst; |
690 msg = *(paramlst+1); | |
691 | |
692 if (check_jid_syntax(jid)) { | |
693 if (!jid) | |
694 scr_LogPrint(LPRINT_NORMAL, "Wrong usage"); | |
695 else | |
696 scr_LogPrint(LPRINT_NORMAL, "<%s> is not a valid Jabber id", jid); | |
697 free_arg_lst(paramlst); | |
698 return; | |
699 } | |
700 | |
701 if (!msg || !*msg) { | |
690 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); | 702 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); |
691 return; | 703 free_arg_lst(paramlst); |
692 } | |
693 | |
694 jid = g_strndup(arg, msg - arg); | |
695 | |
696 if (check_jid_syntax(jid)) { | |
697 scr_LogPrint(LPRINT_NORMAL, "<%s> is not a valid Jabber id", jid); | |
698 g_free(jid); | |
699 return; | |
700 } | |
701 | |
702 while (*msg == ' ') msg++; | |
703 if (!*msg) { | |
704 scr_LogPrint(LPRINT_NORMAL, "Wrong or missing parameter"); | |
705 g_free(jid); | |
706 return; | 704 return; |
707 } | 705 } |
708 | 706 |
709 // We must use the bare jid in hk_message_out() | 707 // We must use the bare jid in hk_message_out() |
710 p = strchr(jid, '/'); | 708 p = strchr(jid, '/'); |
724 // local part (UI, logging, etc.) | 722 // local part (UI, logging, etc.) |
725 hk_message_out(bare_jid, p, 0, msg); | 723 hk_message_out(bare_jid, p, 0, msg); |
726 | 724 |
727 // Network part | 725 // Network part |
728 jb_send_msg(jid, msg, ROSTER_TYPE_USER, NULL); | 726 jb_send_msg(jid, msg, ROSTER_TYPE_USER, NULL); |
729 g_free(jid); | 727 |
730 if (p) g_free(bare_jid); | 728 if (p) g_free(bare_jid); |
729 free_arg_lst(paramlst); | |
731 } | 730 } |
732 | 731 |
733 static void do_buffer(char *arg) | 732 static void do_buffer(char *arg) |
734 { | 733 { |
735 int search_dir = 0; | 734 int search_dir = 0; |
919 gpointer bud; | 918 gpointer bud; |
920 const char *jid, *group; | 919 const char *jid, *group; |
921 guint type; | 920 guint type; |
922 char *newname, *p; | 921 char *newname, *p; |
923 | 922 |
924 if (!arg || (!*arg)) { | 923 if (!*arg) { |
925 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); | 924 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); |
926 return; | 925 return; |
927 } | 926 } |
928 | 927 |
929 if (!current_buddy) return; | 928 if (!current_buddy) return; |
941 newname = g_strdup(arg); | 940 newname = g_strdup(arg); |
942 // Remove trailing space | 941 // Remove trailing space |
943 for (p = newname; *p; p++) ; | 942 for (p = newname; *p; p++) ; |
944 while (p > newname && *p == ' ') *p = 0; | 943 while (p > newname && *p == ' ') *p = 0; |
945 | 944 |
945 strip_arg_special_chars(newname); | |
946 | |
946 buddy_setname(bud, newname); | 947 buddy_setname(bud, newname); |
947 jb_updatebuddy(jid, newname, group); | 948 jb_updatebuddy(jid, newname, group); |
948 | 949 |
949 g_free(newname); | 950 g_free(newname); |
950 update_roster = TRUE; | 951 update_roster = TRUE; |
971 | 972 |
972 newgroupname = g_strdup(arg); | 973 newgroupname = g_strdup(arg); |
973 // Remove trailing space | 974 // Remove trailing space |
974 for (p = newgroupname; *p; p++) ; | 975 for (p = newgroupname; *p; p++) ; |
975 while (p > newgroupname && *p == ' ') *p = 0; | 976 while (p > newgroupname && *p == ' ') *p = 0; |
977 | |
978 strip_arg_special_chars(newgroupname); | |
976 | 979 |
977 // Call to buddy_setgroup() should be at the end, as current implementation | 980 // Call to buddy_setgroup() should be at the end, as current implementation |
978 // clones the buddy and deletes the old one (and thus, jid and name are | 981 // clones the buddy and deletes the old one (and thus, jid and name are |
979 // freed) | 982 // freed) |
980 jb_updatebuddy(jid, name, newgroupname); | 983 jb_updatebuddy(jid, name, newgroupname); |
1080 settings_set(SETTINGS_TYPE_BINDING, keycode, value); | 1083 settings_set(SETTINGS_TYPE_BINDING, keycode, value); |
1081 } | 1084 } |
1082 | 1085 |
1083 static void do_rawxml(char *arg) | 1086 static void do_rawxml(char *arg) |
1084 { | 1087 { |
1088 char **paramlst; | |
1089 char *subcmd; | |
1090 | |
1085 if (!jb_getonline()) { | 1091 if (!jb_getonline()) { |
1086 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); | 1092 scr_LogPrint(LPRINT_NORMAL, "You are not connected"); |
1087 return; | 1093 return; |
1088 } | 1094 } |
1089 | 1095 |
1090 if (!strncasecmp(arg, "send ", 5)) { | 1096 paramlst = split_arg(arg, 2, 1); // subcmd, arg |
1091 gchar *buffer; | 1097 subcmd = *paramlst; |
1092 for (arg += 5; *arg && *arg == ' '; arg++) | 1098 arg = *(paramlst+1); |
1093 ; | 1099 |
1094 buffer = to_utf8(arg); | 1100 if (!subcmd || !*subcmd) { |
1095 if (!buffer) { | |
1096 scr_LogPrint(LPRINT_NORMAL, "Conversion error in XML string"); | |
1097 return; | |
1098 } | |
1099 scr_LogPrint(LPRINT_NORMAL, "Sending XML string"); | |
1100 jb_send_raw(buffer); | |
1101 g_free(buffer); | |
1102 } else { | |
1103 scr_LogPrint(LPRINT_NORMAL, "Please read the manual page" | 1101 scr_LogPrint(LPRINT_NORMAL, "Please read the manual page" |
1104 " before using /rawxml :-)"); | 1102 " before using /rawxml :-)"); |
1105 } | 1103 free_arg_lst(paramlst); |
1104 return; | |
1105 } | |
1106 | |
1107 if (!strcasecmp(subcmd, "send")) { | |
1108 gchar *buffer; | |
1109 | |
1110 if (!subcmd || !*subcmd) { | |
1111 scr_LogPrint(LPRINT_NORMAL, "Missing parameter"); | |
1112 free_arg_lst(paramlst); | |
1113 return; | |
1114 } | |
1115 | |
1116 // We don't strip_arg_special_chars() here, because it would be a pain for | |
1117 // the user to escape quotes in a XML stream... | |
1118 | |
1119 buffer = to_utf8(arg); | |
1120 if (buffer) { | |
1121 scr_LogPrint(LPRINT_NORMAL, "Sending XML string"); | |
1122 jb_send_raw(buffer); | |
1123 g_free(buffer); | |
1124 } else { | |
1125 scr_LogPrint(LPRINT_NORMAL, "Conversion error in XML string"); | |
1126 } | |
1127 } else { | |
1128 scr_LogPrint(LPRINT_NORMAL, "Unrecognized parameter!"); | |
1129 } | |
1130 | |
1131 free_arg_lst(paramlst); | |
1106 } | 1132 } |
1107 | 1133 |
1108 // check_room_subcommand(arg, param_needed, buddy_must_be_a_room) | 1134 // check_room_subcommand(arg, param_needed, buddy_must_be_a_room) |
1109 // - Check if this is a room, if buddy_must_be_a_room is not null | 1135 // - Check if this is a room, if buddy_must_be_a_room is not null |
1110 // - Check there is at least 1 parameter, if param_needed is true | 1136 // - Check there is at least 1 parameter, if param_needed is true |