Mercurial > hg > nnchat
comparison main.c @ 417:ccee77fe4525
Fix protocol message parsing.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 24 May 2012 07:27:28 +0300 |
parents | 8bb69e749d97 |
children | 8ca09a6cca09 |
comparison
equal
deleted
inserted
replaced
416:8bb69e749d97 | 417:ccee77fe4525 |
---|---|
638 } | 638 } |
639 | 639 |
640 | 640 |
641 int nnproto_parse_user(nn_conn_t *conn) | 641 int nnproto_parse_user(nn_conn_t *conn) |
642 { | 642 { |
643 static const char *msg = "</USER><MESSAGE>"; | |
644 char *p = conn->ptr; | |
645 BOOL isMine, isIgnored = FALSE; | 643 BOOL isMine, isIgnored = FALSE; |
646 char *s, *t, *userName; | 644 char *name, *msg, *t; |
647 | 645 |
648 /* Find start of the message */ | 646 /* Find start of the message */ |
649 s = strstr(p, msg); | 647 name = conn->ptr; |
650 if (!s) return 1; | 648 t = nn_conn_buf_strstr(conn, "</USER><MESSAGE>"); |
651 *s = 0; | 649 if (!t) return 1; |
652 s += strlen(msg); | 650 *t = 0; |
651 msg = conn->ptr; | |
653 | 652 |
654 /* Find end of the message */ | 653 /* Find end of the message */ |
655 t = strstr(s, "</MESSAGE>"); | 654 t = nn_conn_buf_strstr(conn, "</MESSAGE>"); |
656 if (!t) return 3; | 655 if (!t) return 3; |
657 *t = 0; | 656 *t = 0; |
658 | 657 |
659 /* Decode message string */ | 658 /* Decode message string */ |
660 s = nn_decode_str1(s); | 659 msg = nn_decode_str1(msg); |
661 if (!s) return -1; | 660 if (!msg) return -1; |
662 | 661 |
663 /* Decode username */ | 662 /* Decode username */ |
664 userName = nn_decode_str1(p); | 663 name = nn_decode_str1(name); |
665 if (!userName) | 664 if (!name) |
666 { | 665 { |
667 th_free(s); | 666 th_free(msg); |
668 return -2; | 667 return -2; |
669 } | 668 } |
670 | 669 |
671 /* Check if the username is on our ignore list and | 670 /* Check if the username is on our ignore list and |
672 * that it is not our OWN username! | 671 * that it is not our OWN username! |
673 */ | 672 */ |
674 isMine = strcmp(userName, optUserName) == 0; | 673 isMine = strcmp(name, optUserName) == 0; |
675 isIgnored = setIgnoreMode && !isMine && checkIgnoreList(userName); | 674 isIgnored = setIgnoreMode && !isMine && checkIgnoreList(name); |
676 | 675 |
677 /* Is it a special control message? */ | 676 /* Is it a special control message? */ |
678 if (*s == '/') | 677 if (*msg == '/') |
679 { | 678 { |
680 /* Ignore room join/leave messages */ | 679 /* Ignore room join/leave messages */ |
681 if (!optDebug && (strstr(s, "left the room") || strstr(s, "joined the room from"))) | 680 if (!optDebug && (strstr(msg, "left the room") || strstr(msg, "joined the room from"))) |
682 goto done; | 681 goto done; |
683 | 682 |
684 t = nn_strip_tags(s + 1); | 683 t = nn_strip_tags(msg + 1); |
685 if (!strncmp(t, "BPRV ", 5)) | 684 if (!strncmp(t, "BPRV ", 5)) |
686 { | 685 { |
687 char *name, *tmp, *msg, *h; | 686 char *in_name, *tmp, *in_msg, *h; |
688 nn_window_t *win; | 687 nn_window_t *win; |
689 h = nn_decode_str2(t + 1); | 688 h = nn_decode_str2(t + 1); |
690 | 689 |
691 if (!strncmp(t, "BPRV from ", 10)) | 690 if (!strncmp(t, "BPRV from ", 10)) |
692 { | 691 { |
693 name = nn_decode_str2(t + 10); | 692 in_name = nn_decode_str2(t + 10); |
694 isMine = FALSE; | 693 isMine = FALSE; |
695 } | 694 } |
696 else | 695 else |
697 { | 696 { |
698 name = nn_decode_str2(t + 8); | 697 in_name = nn_decode_str2(t + 8); |
699 isMine = TRUE; | 698 isMine = TRUE; |
700 } | 699 } |
701 | 700 |
702 for (tmp = name; *tmp && *tmp != ':'; tmp++); | 701 for (tmp = in_name; *tmp && *tmp != ':'; tmp++); |
703 if (tmp[0] != 0 && tmp[1] == ' ') | 702 if (tmp[0] != 0 && tmp[1] == ' ') |
704 msg = tmp + 2; | 703 in_msg = tmp + 2; |
705 else | 704 else |
706 msg = ""; | 705 in_msg = ""; |
707 *tmp = 0; | 706 *tmp = 0; |
708 | 707 |
709 isIgnored = setIgnoreMode && checkIgnoreList(name); | 708 isIgnored = setIgnoreMode && checkIgnoreList(in_name); |
710 win = findWindow(name); | 709 win = findWindow(in_name); |
711 | 710 |
712 if (win != NULL) | 711 if (win != NULL) |
713 { | 712 { |
714 printMsgF(win, isIgnored ? 0 : LOG_WINDOW, | 713 printMsgF(win, isIgnored ? 0 : LOG_WINDOW, |
715 "½5½<½%d½%s½5½>½0½ %s\n", | 714 "½5½<½%d½%s½5½>½0½ %s\n", |
716 isMine ? 14 : 15, isMine ? optUserName : name, msg); | 715 isMine ? 14 : 15, isMine ? optUserName : in_name, in_msg); |
717 | 716 |
718 printMsgF(NULL, LOG_FILE, "½11½%s½0½\n", h); | 717 printMsgF(NULL, LOG_FILE, "½11½%s½0½\n", h); |
719 } | 718 } |
720 else | 719 else |
721 { | 720 { |
722 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE), | 721 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE), |
723 "½11½%s½0½\n", h); | 722 "½11½%s½0½\n", h); |
724 } | 723 } |
725 th_free(name); | 724 th_free(in_name); |
726 th_free(h); | 725 th_free(h); |
727 } | 726 } |
728 else | 727 else |
729 { | 728 { |
730 /* It's an action (/me) */ | 729 /* It's an action (/me) */ |
737 } | 736 } |
738 else | 737 else |
739 { | 738 { |
740 /* It's a normal message */ | 739 /* It's a normal message */ |
741 char *h; | 740 char *h; |
742 t = nn_strip_tags(s); | 741 t = nn_strip_tags(msg); |
743 h = nn_decode_str2(t); | 742 h = nn_decode_str2(t); |
744 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE), | 743 printMsgF(NULL, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE), |
745 "½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, userName, h); | 744 "½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, name, h); |
746 th_free(h); | 745 th_free(h); |
747 th_free(t); | 746 th_free(t); |
748 } | 747 } |
749 | 748 |
750 done: | 749 done: |
751 th_free(s); | 750 th_free(msg); |
752 th_free(userName); | 751 th_free(name); |
753 return 0; | 752 return 0; |
754 } | 753 } |
755 | 754 |
756 | 755 |
757 int nnproto_parse_login(nn_conn_t *conn) | 756 int nnproto_parse_login(nn_conn_t *conn) |