comparison mcabber/src/jabglue.c @ 426:2706ef3e25a7

Better handling of server error codes/messages
author Mikael Berthe <mikael@lilotux.net>
date Mon, 05 Sep 2005 22:56:57 +0200
parents 569f333e51e2
children d03663d2e7d9
comparison
equal deleted inserted replaced
425:03f1e37759a6 426:2706ef3e25a7
575 hk_message_in(jid, timestamp, buffer, type); 575 hk_message_in(jid, timestamp, buffer, type);
576 g_free(jid); 576 g_free(jid);
577 g_free(buffer); 577 g_free(buffer);
578 } 578 }
579 579
580 const char *errormsg(int code) 580 const char *defaulterrormsg(int code)
581 { 581 {
582 const char *desc; 582 const char *desc;
583 583
584 switch(code) { 584 switch(code) {
585 case 401: desc = "Unauthorized"; 585 case 401: desc = "Unauthorized";
617 default: 617 default:
618 desc = NULL; 618 desc = NULL;
619 } 619 }
620 620
621 return desc; 621 return desc;
622 }
623
624 void display_server_error(xmlnode x)
625 {
626 char *s;
627 const char *desc;
628 int code;
629
630 if ((s = xmlnode_get_attrib(x, "code")) != NULL) {
631 code = atoi(s);
632
633 // Default message
634 desc = defaulterrormsg(atoi(s));
635
636 // Error tag data is better, if available
637 s = xmlnode_get_data(x);
638 if (s && *s) desc = s;
639
640 // And sometimes there is a text message
641 s = xmlnode_get_tag_data(x, "text");
642 if (s && *s) desc = s; // FIXME utf8??
643
644 scr_LogPrint(LPRINT_LOGNORM, "Error code from server: %d %s", code, desc);
645 }
622 } 646 }
623 647
624 void statehandler(jconn conn, int state) 648 void statehandler(jconn conn, int state)
625 { 649 {
626 static int previous_state = -1; 650 static int previous_state = -1;
717 if ((p = xmlnode_get_attrib(x, "stamp")) != NULL) 741 if ((p = xmlnode_get_attrib(x, "stamp")) != NULL)
718 timestamp = from_iso8601(p, 1); 742 timestamp = from_iso8601(p, 1);
719 } 743 }
720 744
721 if (type && !strcmp(type, "error")) { 745 if (type && !strcmp(type, "error")) {
722 if ((x = xmlnode_get_tag(packet->x, "error")) != NULL) { 746 if ((x = xmlnode_get_tag(packet->x, "error")) != NULL)
723 if ((p = xmlnode_get_attrib(x, "code")) != NULL) { 747 display_server_error(x);
724 const char *desc;
725 int code = atoi(p);
726 desc = errormsg(atoi(p));
727 scr_LogPrint(LPRINT_LOGNORM, "Error code from server: %d %s",
728 code, (desc ? desc : ""));
729 }
730 }
731 } 748 }
732 if (from && body) 749 if (from && body)
733 gotmessage(type, from, body, enc, timestamp); 750 gotmessage(type, from, body, enc, timestamp);
734 if (tmp) 751 if (tmp)
735 g_free(tmp); 752 g_free(tmp);
850 867
851 } 868 }
852 } 869 }
853 } else if (!strcmp(type, "set")) { 870 } else if (!strcmp(type, "set")) {
854 } else if (!strcmp(type, "error")) { 871 } else if (!strcmp(type, "error")) {
855 char *name=NULL; 872 if ((x = xmlnode_get_tag(packet->x, "error")) != NULL)
856 char *text=NULL; 873 display_server_error(x);
857 int code = 0;
858 const char *desc;
859
860 x = xmlnode_get_tag(packet->x, "error");
861 p = xmlnode_get_attrib(x, "code"); if (p) code = atoi(p);
862 p = xmlnode_get_attrib(x, "id"); if (p) name = p;
863 p = xmlnode_get_tag_data(packet->x, "error"); if (p) desc = p;
864
865 // Sometimes there is a text message
866 x = xmlnode_get_tag(x, "text");
867 p = xmlnode_get_data(x); if (p) text = p;
868
869 desc = errormsg(code);
870
871 scr_LogPrint(LPRINT_LOGNORM, "Error code from server: %d %s",
872 code, (desc ? desc : ""));
873 if (text)
874 scr_LogPrint(LPRINT_LOGNORM, "Server message: %s", text);
875
876 if (name)
877 scr_LogPrint(LPRINT_DEBUG, "Error id: %s", name);
878 } 874 }
879 break; 875 break;
880 876
881 case JPACKET_PRESENCE: 877 case JPACKET_PRESENCE:
878 r = jidtodisp(from);
879 if (type && !strcmp(type, "error")) {
880 scr_LogPrint(LPRINT_LOGNORM, "Error presence packet from <%s>", r);
881 if ((x = xmlnode_get_tag(packet->x, "error")) != NULL)
882 display_server_error(x);
883 g_free(r);
884 break;
885 }
882 x = xmlnode_get_tag(packet->x, "show"); 886 x = xmlnode_get_tag(packet->x, "show");
883 ust = available; 887 ust = available;
884 888
885 if (x) { 889 if (x) {
886 p = xmlnode_get_data(x); if (p) ns = p; 890 p = xmlnode_get_data(x); if (p) ns = p;
899 if ((x = xmlnode_get_tag(packet->x, "status")) != NULL) 903 if ((x = xmlnode_get_tag(packet->x, "status")) != NULL)
900 p = from_utf8(xmlnode_get_data(x)); 904 p = from_utf8(xmlnode_get_data(x));
901 else 905 else
902 p = NULL; 906 p = NULL;
903 907
904 r = jidtodisp(from);
905 // Call hk_statuschange() if status has changed or if the 908 // Call hk_statuschange() if status has changed or if the
906 // status message is different 909 // status message is different
907 m = roster_getstatusmsg(r); 910 m = roster_getstatusmsg(r);
908 if ((ust != roster_getstatus(r)) || (p && (!m || strcmp(p, m)))) 911 if ((ust != roster_getstatus(r)) || (p && (!m || strcmp(p, m))))
909 hk_statuschange(r, 0, ust, p); 912 hk_statuschange(r, 0, ust, p);