comparison mcabber/src/jabglue.c @ 577:5c6d364130ee

Move IQ Jabber stuff to a separate file
author Mikael Berthe <mikael@lilotux.net>
date Sun, 04 Dec 2005 11:06:59 +0100
parents 8b3db0b555a1
children b3ab662757a0
comparison
equal deleted inserted replaced
576:8b3db0b555a1 577:5c6d364130ee
22 */ 22 */
23 23
24 #define _GNU_SOURCE /* We need glibc for strptime */ 24 #define _GNU_SOURCE /* We need glibc for strptime */
25 #include "../libjabber/jabber.h" 25 #include "../libjabber/jabber.h"
26 #include "jabglue.h" 26 #include "jabglue.h"
27 #include "jab_priv.h"
27 #include "roster.h" 28 #include "roster.h"
28 #include "screen.h" 29 #include "screen.h"
29 #include "hooks.h" 30 #include "hooks.h"
30 #include "utils.h" 31 #include "utils.h"
31 #include "settings.h" 32 #include "settings.h"
33 #include "histolog.h" 34 #include "histolog.h"
34 35
35 #define JABBERPORT 5222 36 #define JABBERPORT 5222
36 #define JABBERSSLPORT 5223 37 #define JABBERSSLPORT 5223
37 38
38 #define JABBER_AGENT_GROUP "Jabber Agents"
39
40 jconn jc; 39 jconn jc;
40 int regmode, regdone;
41
42 char imstatus2char[imstatus_size+1] = {
43 '_', 'o', 'i', 'f', 'd', 'n', 'a', '\0'
44 };
45
41 static time_t LastPingTime; 46 static time_t LastPingTime;
42 static unsigned int KeepaliveDelay; 47 static unsigned int KeepaliveDelay;
43 static int s_id;
44 static int regmode, regdone;
45 static enum imstatus mystatus = offline; 48 static enum imstatus mystatus = offline;
46 static gchar *mystatusmsg; 49 static gchar *mystatusmsg;
47 static unsigned char online; 50 static unsigned char online;
48 51
49 char imstatus2char[imstatus_size+1] = {
50 '_', 'o', 'i', 'f', 'd', 'n', 'a', '\0'
51 };
52
53 static enum {
54 STATE_CONNECTING,
55 STATE_GETAUTH,
56 STATE_SENDAUTH,
57 STATE_LOGGED
58 } jstate;
59
60 struct T_presence {
61 enum imstatus st;
62 const char *msg;
63 };
64
65
66 static void statehandler(jconn, int); 52 static void statehandler(jconn, int);
67 static void packethandler(jconn, jpacket); 53 static void packethandler(jconn, jpacket);
68 54
69 static void logger(jconn j, int io, const char *buf) 55 static void logger(jconn j, int io, const char *buf)
70 { 56 {
72 } 58 }
73 59
74 // jidtodisp(jid) 60 // jidtodisp(jid)
75 // Strips the resource part from the jid 61 // Strips the resource part from the jid
76 // The caller should g_free the result after use. 62 // The caller should g_free the result after use.
77 static char *jidtodisp(const char *jid) 63 char *jidtodisp(const char *jid)
78 { 64 {
79 char *ptr; 65 char *ptr;
80 char *alias; 66 char *alias;
81 67
82 alias = g_strdup(jid); 68 alias = g_strdup(jid);
674 jab_send(jc, x); 660 jab_send(jc, x);
675 xmlnode_free(x); 661 xmlnode_free(x);
676 jb_reset_keepalive(); 662 jb_reset_keepalive();
677 } 663 }
678 664
679 static void postlogin()
680 {
681 //int i;
682
683 //flogged = TRUE;
684 //ourstatus = available;
685
686 //setautostatus(jhook.manualstatus);
687
688 jb_setstatus(available, NULL, NULL);
689 buddylist_build();
690 /*
691 for (i = 0; i < clist.count; i++) {
692 c = (icqcontact *) clist.at(i);
693
694 if (c->getdesc().pname == proto)
695 if (ischannel(c))
696 if (c->getbasicinfo().requiresauth)
697 c->setstatus(available);
698 }
699 */
700
701 /*
702 agents.insert(agents.begin(), agent("vcard", "Jabber VCard", "", agent::atStandard));
703 agents.begin()->params[agent::ptRegister].enabled = TRUE;
704
705 string buf;
706 ifstream f(conf.getconfigfname("jabber-infoset").c_str());
707
708 if (f.is_open()) {
709 icqcontact *c = clist.get(contactroot);
710
711 c->clear();
712 icqcontact::basicinfo bi = c->getbasicinfo();
713 icqcontact::reginfo ri = c->getreginfo();
714
715 ri.service = agents.begin()->name;
716 getstring(f, buf); c->setnick(buf);
717 getstring(f, buf); bi.email = buf;
718 getstring(f, buf); bi.fname = buf;
719 getstring(f, buf); bi.lname = buf;
720 f.close();
721
722 c->setbasicinfo(bi);
723 c->setreginfo(ri);
724
725 sendupdateuserinfo(*c);
726 unlink(conf.getconfigfname("jabber-infoset").c_str());
727 }
728 */
729 }
730
731 static void gotloggedin(void)
732 {
733 xmlnode x;
734
735 x = jutil_iqnew(JPACKET__GET, NS_AGENTS);
736 xmlnode_put_attrib(x, "id", "Agent List");
737 jab_send(jc, x);
738 xmlnode_free(x);
739
740 x = jutil_iqnew(JPACKET__GET, NS_ROSTER);
741 xmlnode_put_attrib(x, "id", "Roster");
742 jab_send(jc, x);
743 xmlnode_free(x);
744 }
745
746 static void gotroster(xmlnode x)
747 {
748 xmlnode y, z;
749
750 for (y = xmlnode_get_tag(x, "item"); y; y = xmlnode_get_nextsibling(y)) {
751 const char *alias = xmlnode_get_attrib(y, "jid");
752 //const char *sub = xmlnode_get_attrib(y, "subscription"); // TODO Not used
753 const char *name = xmlnode_get_attrib(y, "name");
754 char *group = NULL;
755
756 z = xmlnode_get_tag(y, "group");
757 if (z) group = xmlnode_get_data(z);
758
759 if (alias) {
760 char *buddyname;
761 char *cleanalias = jidtodisp(alias);
762 gchar *name_noutf8 = NULL;
763 gchar *group_noutf8 = NULL;
764
765 buddyname = cleanalias;
766 if (name) {
767 name_noutf8 = from_utf8(name);
768 if (name_noutf8)
769 buddyname = name_noutf8;
770 else
771 scr_LogPrint(LPRINT_LOG, "Decoding of buddy alias has failed: %s",
772 name);
773 }
774
775 if (group) {
776 group_noutf8 = from_utf8(group);
777 if (!group_noutf8)
778 scr_LogPrint(LPRINT_LOG, "Decoding of buddy group has failed: %s",
779 group);
780 }
781
782 roster_add_user(cleanalias, buddyname, group_noutf8, ROSTER_TYPE_USER);
783 if (name_noutf8) g_free(name_noutf8);
784 if (group_noutf8) g_free(group_noutf8);
785 g_free(cleanalias);
786 }
787 }
788
789 postlogin();
790 }
791
792 static void gotmessage(char *type, const char *from, const char *body, 665 static void gotmessage(char *type, const char *from, const char *body,
793 const char *enc, time_t timestamp) 666 const char *enc, time_t timestamp)
794 { 667 {
795 char *jid; 668 char *jid;
796 const char *rname; 669 const char *rname;
862 } 735 }
863 736
864 // display_server_error(x) 737 // display_server_error(x)
865 // Display the error to the user 738 // Display the error to the user
866 // x: error tag xmlnode pointer 739 // x: error tag xmlnode pointer
867 static void display_server_error(xmlnode x) 740 void display_server_error(xmlnode x)
868 { 741 {
869 const char *desc = NULL; 742 const char *desc = NULL;
870 int code = 0; 743 int code = 0;
871 char *s; 744 char *s;
872 const char *p; 745 const char *p;
941 814
942 default: 815 default:
943 break; 816 break;
944 } 817 }
945 previous_state = state; 818 previous_state = state;
946 }
947
948 static void handle_packet_iq(jconn conn, char *type, char *from,
949 xmlnode xmldata)
950 {
951 char *p;
952 xmlnode x, y;
953 char *ns = NULL;
954 char *id=NULL;
955
956 if (!type)
957 return;
958
959 if (!strcmp(type, "result")) {
960
961 if ((p = xmlnode_get_attrib(xmldata, "id")) != NULL) {
962 int iid = atoi(p);
963
964 //scr_LogPrint(LPRINT_DEBUG, "iid = %d", iid);
965 if (iid == s_id) {
966 if (!regmode) {
967 if (jstate == STATE_GETAUTH) {
968 if ((x = xmlnode_get_tag(xmldata, "query")) != NULL)
969 if (!xmlnode_get_tag(x, "digest")) {
970 jc->sid = 0;
971 }
972
973 s_id = atoi(jab_auth(jc));
974 jstate = STATE_SENDAUTH;
975 } else {
976 gotloggedin();
977 jstate = STATE_LOGGED;
978 }
979 } else {
980 regdone = TRUE;
981 }
982 return;
983 }
984
985 if (!strcmp(p, "VCARDreq")) {
986 x = xmlnode_get_firstchild(xmldata);
987 if (!x) x = xmldata;
988
989 //jhook.gotvcard(ic, x); TODO
990 scr_LogPrint(LPRINT_LOGNORM, "Got VCARD");
991 return;
992 } else if (!strcmp(p, "versionreq")) {
993 // jhook.gotversion(ic, xmldata); TODO
994 scr_LogPrint(LPRINT_LOGNORM, "Got version");
995 return;
996 }
997 }
998
999 if ((x = xmlnode_get_tag(xmldata, "query")) != NULL) {
1000 p = xmlnode_get_attrib(x, "xmlns"); if (p) ns = p;
1001
1002 if (!strcmp(ns, NS_ROSTER)) {
1003 gotroster(x);
1004 } else if (!strcmp(ns, NS_AGENTS)) {
1005 y = xmlnode_get_tag(x, "agent");
1006 for (; y; y = xmlnode_get_nextsibling(y)) {
1007 const char *alias = xmlnode_get_attrib(y, "jid");
1008
1009 if (alias) {
1010 const char *name = xmlnode_get_tag_data(y, "name");
1011 const char *desc = xmlnode_get_tag_data(y, "description");
1012 // TODO
1013 // const char *service = xmlnode_get_tag_data(y, "service");
1014 enum agtype atype = unknown;
1015
1016 if (xmlnode_get_tag(y, TMSG_GROUPCHAT)) atype = groupchat;
1017 else if (xmlnode_get_tag(y, "transport")) atype = transport;
1018 else if (xmlnode_get_tag(y, "search")) atype = search;
1019
1020 if (atype == transport) {
1021 char *cleanjid = jidtodisp(alias);
1022 roster_add_user(cleanjid, NULL, JABBER_AGENT_GROUP,
1023 ROSTER_TYPE_AGENT);
1024 g_free(cleanjid);
1025 }
1026 if (alias && name && desc) {
1027 scr_LogPrint(LPRINT_LOGNORM,
1028 "Agent: %s / %s / %s / type=%d",
1029 alias, name, desc, atype);
1030
1031 if (atype == search) {
1032 x = jutil_iqnew (JPACKET__GET, NS_SEARCH);
1033 xmlnode_put_attrib(x, "to", alias);
1034 xmlnode_put_attrib(x, "id", "Agent info");
1035 jab_send(conn, x);
1036 xmlnode_free(x);
1037 }
1038
1039 if (xmlnode_get_tag(y, "register")) {
1040 x = jutil_iqnew (JPACKET__GET, NS_REGISTER);
1041 xmlnode_put_attrib(x, "to", alias);
1042 xmlnode_put_attrib(x, "id", "Agent info");
1043 jab_send(conn, x);
1044 xmlnode_free(x);
1045 }
1046 }
1047 }
1048 }
1049
1050 /*
1051 if (find(jhook.agents.begin(), jhook.agents.end(), DEFAULT_CONFSERV) == jhook.agents.end())
1052 jhook.agents.insert(jhook.agents.begin(), agent(DEFAULT_CONFSERV, DEFAULT_CONFSERV,
1053 _("Default Jabber conference server"), agent::atGroupchat));
1054
1055 */
1056 } else if (!strcmp(ns, NS_SEARCH) || !strcmp(ns, NS_REGISTER)) {
1057 p = xmlnode_get_attrib(xmldata, "id"); id = p ? p : (char*)"";
1058
1059 if (!strcmp(id, "Agent info")) {
1060 // jhook.gotagentinfo(xmldata); TODO
1061 scr_LogPrint(LPRINT_LOGNORM, "Got agent info");
1062 } else if (!strcmp(id, "Lookup")) {
1063 // jhook.gotsearchresults(xmldata); TODO
1064 scr_LogPrint(LPRINT_LOGNORM, "Got search results");
1065 } else if (!strcmp(id, "Register")) {
1066 if (!from)
1067 return;
1068 x = jutil_iqnew(JPACKET__GET, NS_REGISTER);
1069 xmlnode_put_attrib(x, "to", from);
1070 xmlnode_put_attrib(x, "id", "Agent info");
1071 jab_send(conn, x);
1072 xmlnode_free(x);
1073 }
1074
1075 }
1076 }
1077 } else if (!strcmp(type, "get")) {
1078 p = xmlnode_get_attrib(xmldata, "id");
1079 if (p) {
1080 xmlnode z;
1081
1082 id = p;
1083 x = xmlnode_new_tag("iq");
1084 xmlnode_put_attrib(x, "type", "result");
1085 xmlnode_put_attrib(x, "to", from);
1086 xmlnode_put_attrib(x, "id", id);
1087 xmlnode_put_attrib(x, "type", TMSG_ERROR);
1088 y = xmlnode_insert_tag(x, TMSG_ERROR);
1089 xmlnode_put_attrib(y, "code", "503");
1090 xmlnode_put_attrib(y, "type", "cancel");
1091 z = xmlnode_insert_tag(y, "feature-not-implemented");
1092 xmlnode_put_attrib(z, "xmlns",
1093 "urn:ietf:params:xml:ns:xmpp-stanzas");
1094 jab_send(conn, x);
1095 xmlnode_free(x);
1096 }
1097 } else if (!strcmp(type, "set")) {
1098 /* FIXME: send error */
1099 } else if (!strcmp(type, TMSG_ERROR)) {
1100 if ((x = xmlnode_get_tag(xmldata, TMSG_ERROR)) != NULL)
1101 display_server_error(x);
1102 }
1103 } 819 }
1104 820
1105 static void handle_packet_presence(jconn conn, char *type, char *from, 821 static void handle_packet_presence(jconn conn, char *type, char *from,
1106 xmlnode xmldata) 822 xmlnode xmldata)
1107 { 823 {