mcabber/src/jab_iq.c
changeset 577 5c6d364130ee
parent 576 8b3db0b555a1
child 578 b3ab662757a0
equal deleted inserted replaced
576:8b3db0b555a1 577:5c6d364130ee
       
     1 /*
       
     2  * jab_iq.c     -- Jabber protocol IQ-related fonctions
       
     3  *
       
     4  * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
       
     5  * Some parts initially came from the centericq project:
       
     6  * Copyright (C) 2002-2005 by Konstantin Klyagin <konst@konst.org.ua>
       
     7  *
       
     8  * This program is free software; you can redistribute it and/or modify
       
     9  * it under the terms of the GNU General Public License as published by
       
    10  * the Free Software Foundation; either version 2 of the License, or (at
       
    11  * your option) any later version.
       
    12  *
       
    13  * This program is distributed in the hope that it will be useful, but
       
    14  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16  * General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU General Public License
       
    19  * along with this program; if not, write to the Free Software
       
    20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
       
    21  * USA
       
    22  */
       
    23 
       
    24 #include "jabglue.h"
       
    25 #include "jab_priv.h"
       
    26 #include "roster.h"
       
    27 #include "utils.h"
       
    28 #include "logprint.h"
       
    29 
       
    30 int s_id; // XXX
       
    31 
       
    32 static void gotloggedin(void)
       
    33 {
       
    34   xmlnode x;
       
    35 
       
    36   x = jutil_iqnew(JPACKET__GET, NS_AGENTS);
       
    37   xmlnode_put_attrib(x, "id", "Agent List");
       
    38   jab_send(jc, x);
       
    39   xmlnode_free(x);
       
    40 
       
    41   x = jutil_iqnew(JPACKET__GET, NS_ROSTER);
       
    42   xmlnode_put_attrib(x, "id", "Roster");
       
    43   jab_send(jc, x);
       
    44   xmlnode_free(x);
       
    45 }
       
    46 
       
    47 static void gotroster(xmlnode x)
       
    48 {
       
    49   xmlnode y;
       
    50   const char *jid, *name, *group;
       
    51   char *buddyname;
       
    52   char *cleanalias;
       
    53 
       
    54   for (y = xmlnode_get_tag(x, "item"); y; y = xmlnode_get_nextsibling(y)) {
       
    55     gchar *name_noutf8 = NULL;
       
    56     gchar *group_noutf8 = NULL;
       
    57 
       
    58     jid = xmlnode_get_attrib(y, "jid");
       
    59     name = xmlnode_get_attrib(y, "name");
       
    60     //sub = xmlnode_get_attrib(y, "subscription"); // TODO Not used
       
    61 
       
    62     group = xmlnode_get_tag_data(y, "group");
       
    63 
       
    64     if (!jid)
       
    65       continue;
       
    66 
       
    67     buddyname = cleanalias = jidtodisp(jid);
       
    68 
       
    69     if (name) {
       
    70       name_noutf8 = from_utf8(name);
       
    71       if (name_noutf8)
       
    72         buddyname = name_noutf8;
       
    73       else
       
    74         scr_LogPrint(LPRINT_LOG, "Decoding of buddy alias has failed: %s",
       
    75                      name);
       
    76     }
       
    77 
       
    78     if (group) {
       
    79       group_noutf8 = from_utf8(group);
       
    80       if (!group_noutf8)
       
    81         scr_LogPrint(LPRINT_LOG, "Decoding of buddy group has failed: %s",
       
    82                      group);
       
    83     }
       
    84 
       
    85     roster_add_user(cleanalias, buddyname, group_noutf8, ROSTER_TYPE_USER);
       
    86     if (name_noutf8)  g_free(name_noutf8);
       
    87     if (group_noutf8) g_free(group_noutf8);
       
    88     g_free(cleanalias);
       
    89   }
       
    90 
       
    91   // Post-login stuff
       
    92   jb_setstatus(available, NULL, NULL);
       
    93   buddylist_build();
       
    94 }
       
    95 
       
    96 static void gotagents(jconn conn, xmlnode x)
       
    97 {
       
    98   xmlnode y;
       
    99   const char *alias;
       
   100   const char *name, *desc;
       
   101 
       
   102   y = xmlnode_get_tag(x, "agent");
       
   103 
       
   104   for (; y; y = xmlnode_get_nextsibling(y)) {
       
   105     enum agtype atype = unknown;
       
   106 
       
   107     alias = xmlnode_get_attrib(y, "jid");
       
   108     if (!alias)
       
   109       continue;
       
   110 
       
   111     name = xmlnode_get_tag_data(y, "name");
       
   112     desc = xmlnode_get_tag_data(y, "description");
       
   113     // TODO
       
   114     // service = xmlnode_get_tag_data(y, "service");
       
   115 
       
   116     if (xmlnode_get_tag(y, TMSG_GROUPCHAT))   atype = groupchat;
       
   117     else if (xmlnode_get_tag(y, "transport")) atype = transport;
       
   118     else if (xmlnode_get_tag(y, "search"))    atype = search;
       
   119 
       
   120     if (atype == transport) {
       
   121       char *cleanjid = jidtodisp(alias);
       
   122       roster_add_user(cleanjid, NULL, JABBER_AGENT_GROUP,
       
   123                       ROSTER_TYPE_AGENT);
       
   124       g_free(cleanjid);
       
   125     }
       
   126     if (alias && name && desc) {
       
   127       scr_LogPrint(LPRINT_LOGNORM, "Agent: %s / %s / %s / type=%d",
       
   128                    alias, name, desc, atype);
       
   129 
       
   130       if (atype == search) {
       
   131         x = jutil_iqnew (JPACKET__GET, NS_SEARCH);
       
   132         xmlnode_put_attrib(x, "to", alias);
       
   133         xmlnode_put_attrib(x, "id", "Agent info");
       
   134         jab_send(conn, x);
       
   135         xmlnode_free(x);
       
   136       }
       
   137 
       
   138       if (xmlnode_get_tag(y, "register")) {
       
   139         x = jutil_iqnew (JPACKET__GET, NS_REGISTER);
       
   140         xmlnode_put_attrib(x, "to", alias);
       
   141         xmlnode_put_attrib(x, "id", "Agent info");
       
   142         jab_send(conn, x);
       
   143         xmlnode_free(x);
       
   144       }
       
   145     }
       
   146   }
       
   147 
       
   148   /*
       
   149   if (find(jhook.agents.begin(), jhook.agents.end(), DEFAULT_CONFSERV) == jhook.agents.end())
       
   150     jhook.agents.insert(jhook.agents.begin(), agent(DEFAULT_CONFSERV, DEFAULT_CONFSERV,
       
   151                 _("Default Jabber conference server"), agent::atGroupchat));
       
   152 
       
   153   */
       
   154 }
       
   155 
       
   156 static void handle_iq_result(jconn conn, char *from, xmlnode xmldata)
       
   157 {
       
   158   xmlnode x;
       
   159   char *p;
       
   160   char *ns;
       
   161 
       
   162   if ((p = xmlnode_get_attrib(xmldata, "id")) != NULL) {
       
   163     int iid = atoi(p);
       
   164 
       
   165     //scr_LogPrint(LPRINT_DEBUG, "iid = %d", iid);
       
   166     if (iid == s_id) {
       
   167       if (!regmode) {
       
   168         if (jstate == STATE_GETAUTH) {
       
   169           if ((x = xmlnode_get_tag(xmldata, "query")) != NULL)
       
   170             if (!xmlnode_get_tag(x, "digest")) {
       
   171               jc->sid = 0;
       
   172             }
       
   173 
       
   174           s_id = atoi(jab_auth(jc));
       
   175           jstate = STATE_SENDAUTH;
       
   176         } else {
       
   177           gotloggedin();
       
   178           jstate = STATE_LOGGED;
       
   179         }
       
   180       } else {
       
   181         regdone = TRUE;
       
   182       }
       
   183       return;
       
   184     }
       
   185 
       
   186     if (!strcmp(p, "VCARDreq")) {
       
   187       x = xmlnode_get_firstchild(xmldata);
       
   188       if (!x) x = xmldata;
       
   189 
       
   190       //jhook.gotvcard(ic, x); TODO
       
   191       scr_LogPrint(LPRINT_LOGNORM, "Got VCARD");
       
   192       return;
       
   193     } else if (!strcmp(p, "versionreq")) {
       
   194       // jhook.gotversion(ic, xmldata); TODO
       
   195       scr_LogPrint(LPRINT_LOGNORM, "Got version");
       
   196       return;
       
   197     }
       
   198   }
       
   199 
       
   200   x = xmlnode_get_tag(xmldata, "query");
       
   201   if (!x) return;
       
   202 
       
   203   ns = xmlnode_get_attrib(x, "xmlns");
       
   204   if (!ns) return;
       
   205 
       
   206   if (!strcmp(ns, NS_ROSTER)) {
       
   207     gotroster(x);
       
   208   } else if (!strcmp(ns, NS_AGENTS)) {
       
   209     gotagents(conn, x);
       
   210   } else if (!strcmp(ns, NS_SEARCH) || !strcmp(ns, NS_REGISTER)) {
       
   211     char *id = xmlnode_get_attrib(xmldata, "id");
       
   212     if (!id) id = "";
       
   213 
       
   214     if (!strcmp(id, "Agent info")) {
       
   215       // jhook.gotagentinfo(xmldata); TODO
       
   216       scr_LogPrint(LPRINT_LOGNORM, "Got agent info");
       
   217     } else if (!strcmp(id, "Lookup")) {
       
   218       // jhook.gotsearchresults(xmldata); TODO
       
   219       scr_LogPrint(LPRINT_LOGNORM, "Got search results");
       
   220     } else if (!strcmp(id, "Register")) {
       
   221       if (!from)
       
   222         return;
       
   223       x = jutil_iqnew(JPACKET__GET, NS_REGISTER);
       
   224       xmlnode_put_attrib(x, "to", from);
       
   225       xmlnode_put_attrib(x, "id", "Agent info");
       
   226       jab_send(conn, x);
       
   227       xmlnode_free(x);
       
   228     }
       
   229 
       
   230   }
       
   231 }
       
   232 
       
   233 static void handle_iq_get(jconn conn, char *from, xmlnode xmldata)
       
   234 {
       
   235   char *id;
       
   236   xmlnode x, y, z;
       
   237 
       
   238   id = xmlnode_get_attrib(xmldata, "id");
       
   239   if (!id) return;
       
   240 
       
   241   // Nothing implemented yet.
       
   242   x = xmlnode_new_tag("iq");
       
   243   xmlnode_put_attrib(x, "type", "result");
       
   244   xmlnode_put_attrib(x, "to", from);
       
   245   xmlnode_put_attrib(x, "id", id);
       
   246   xmlnode_put_attrib(x, "type", TMSG_ERROR);
       
   247   y = xmlnode_insert_tag(x, TMSG_ERROR);
       
   248   xmlnode_put_attrib(y, "code", "503");
       
   249   xmlnode_put_attrib(y, "type", "cancel");
       
   250   z = xmlnode_insert_tag(y, "feature-not-implemented");
       
   251   xmlnode_put_attrib(z, "xmlns",
       
   252                      "urn:ietf:params:xml:ns:xmpp-stanzas");
       
   253   jab_send(conn, x);
       
   254   xmlnode_free(x);
       
   255 }
       
   256 
       
   257 static void handle_iq_set(jconn conn, char *from, xmlnode xmldata)
       
   258 {
       
   259   /* FIXME: send error */
       
   260 }
       
   261 
       
   262 void handle_packet_iq(jconn conn, char *type, char *from, xmlnode xmldata)
       
   263 {
       
   264   if (!type)
       
   265     return;
       
   266 
       
   267   if (!strcmp(type, "result")) {
       
   268     handle_iq_result(conn, from, xmldata);
       
   269   } else if (!strcmp(type, "get")) {
       
   270     handle_iq_get(conn, from, xmldata);
       
   271   } else if (!strcmp(type, "set")) {
       
   272     handle_iq_set(conn, from, xmldata);
       
   273   } else if (!strcmp(type, TMSG_ERROR)) {
       
   274     xmlnode x = xmlnode_get_tag(xmldata, TMSG_ERROR);
       
   275     if (x)
       
   276       display_server_error(x);
       
   277   }
       
   278 }
       
   279 
       
   280 /* vim: set expandtab cindent cinoptions=>2:2(0:  For Vim users... */