mcabber/src/jabglue.c
changeset 213 4fcdbfdf7c20
parent 208 8b08f34922c5
child 221 73f6ce668ba8
equal deleted inserted replaced
212:465d98d2f8e3 213:4fcdbfdf7c20
    24 #include "../libjabber/jabber.h"
    24 #include "../libjabber/jabber.h"
    25 #include "jabglue.h"
    25 #include "jabglue.h"
    26 #include "roster.h"
    26 #include "roster.h"
    27 #include "screen.h"
    27 #include "screen.h"
    28 #include "hooks.h"
    28 #include "hooks.h"
       
    29 #include "utf8.h"
    29 #include "utils.h"
    30 #include "utils.h"
    30 
    31 
    31 #define JABBERPORT      5222
    32 #define JABBERPORT      5222
    32 #define JABBERSSLPORT   5223
    33 #define JABBERSSLPORT   5223
    33 
    34 
   288   mystatus = st;
   289   mystatus = st;
   289 }
   290 }
   290 
   291 
   291 void jb_send_msg(const char *jid, const char *text)
   292 void jb_send_msg(const char *jid, const char *text)
   292 {
   293 {
   293   xmlnode x = jutil_msgnew(TMSG_CHAT, (char*)jid, 0, (char*)text);
   294   char *buffer = utf8_encode(text);
       
   295   xmlnode x = jutil_msgnew(TMSG_CHAT, (char*)jid, 0, (char*)buffer);
   294   jab_send(jc, x);
   296   jab_send(jc, x);
   295   xmlnode_free(x);
   297   xmlnode_free(x);
       
   298   free(buffer);
   296   jb_reset_keepalive();
   299   jb_reset_keepalive();
   297 }
   300 }
   298 
   301 
       
   302 // Note: the caller should check the jid is correct
   299 void jb_addbuddy(const char *jid, const char *group)
   303 void jb_addbuddy(const char *jid, const char *group)
   300 {
   304 {
   301   xmlnode x, y, z;
   305   xmlnode x, y, z;
   302   char *cleanjid;
   306   char *cleanjid;
   303 
   307 
   315   y = xmlnode_get_tag(x, "query");
   319   y = xmlnode_get_tag(x, "query");
   316   z = xmlnode_insert_tag(y, "item");
   320   z = xmlnode_insert_tag(y, "item");
   317   xmlnode_put_attrib(z, "jid", jid);
   321   xmlnode_put_attrib(z, "jid", jid);
   318 
   322 
   319   if (group) {
   323   if (group) {
       
   324     char *group_utf8 = utf8_encode(group);
   320     z = xmlnode_insert_tag(z, "group");
   325     z = xmlnode_insert_tag(z, "group");
   321     xmlnode_insert_cdata(z, group, (unsigned) -1);
   326     xmlnode_insert_cdata(z, group_utf8, (unsigned) -1);
       
   327     free(group_utf8);
   322   }
   328   }
   323 
   329 
   324   jab_send(jc, x);
   330   jab_send(jc, x);
   325   xmlnode_free(x);
   331   xmlnode_free(x);
   326 
   332 
   327   cleanjid = jidtodisp(jid);
   333   cleanjid = jidtodisp(jid);
   328   roster_add_user(cleanjid, NULL, group, ROSTER_TYPE_USER);
   334   roster_add_user(cleanjid, NULL, group, ROSTER_TYPE_USER);
   329   g_free(cleanjid);
   335   g_free(cleanjid);
   330   buddylist_build();
   336   buddylist_build();
   331 
   337 
   332   // maybe not needed: if user appears his status will change
   338   // useless IMHO: if user appears his status will change
   333   //update_roster = TRUE;
   339   //update_roster = TRUE;
   334 }
   340 }
   335 
   341 
   336 void jb_delbuddy(const char *jid)
   342 void jb_delbuddy(const char *jid)
   337 {
   343 {
   377 
   383 
   378 void jb_updatebuddy(const char *jid, const char *name, const char *group)
   384 void jb_updatebuddy(const char *jid, const char *name, const char *group)
   379 {
   385 {
   380   xmlnode x, y;
   386   xmlnode x, y;
   381   char *cleanjid;
   387   char *cleanjid;
       
   388   char *name_utf8;
   382 
   389 
   383   if (!online) return;
   390   if (!online) return;
   384 
   391 
   385   // XXX We should check name's and group's correctness
   392   // XXX We should check name's and group's correctness
   386 
   393 
   387   cleanjid = jidtodisp(jid);
   394   cleanjid = jidtodisp(jid);
       
   395   name_utf8 = utf8_encode(name);
   388 
   396 
   389   x = jutil_iqnew(JPACKET__SET, NS_ROSTER);
   397   x = jutil_iqnew(JPACKET__SET, NS_ROSTER);
   390   y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item");
   398   y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item");
   391   xmlnode_put_attrib(y, "jid", cleanjid);
   399   xmlnode_put_attrib(y, "jid", cleanjid);
   392   xmlnode_put_attrib(y, "name", name);
   400   xmlnode_put_attrib(y, "name", name_utf8);
   393 
   401 
   394   if (group) {
   402   if (group) {
       
   403     char *group_utf8 = utf8_encode(group);
   395     y = xmlnode_insert_tag(y, "group");
   404     y = xmlnode_insert_tag(y, "group");
   396     xmlnode_insert_cdata(y, group, (unsigned) -1);
   405     xmlnode_insert_cdata(y, group_utf8, (unsigned) -1);
       
   406     free(group_utf8);
   397   }
   407   }
   398 
   408 
   399   jab_send(jc, x);
   409   jab_send(jc, x);
   400   xmlnode_free(x);
   410   xmlnode_free(x);
       
   411   free(name_utf8);
   401   g_free(cleanjid);
   412   g_free(cleanjid);
   402 }
   413 }
   403 
   414 
   404 void postlogin()
   415 void postlogin()
   405 {
   416 {
   474 
   485 
   475   for (y = xmlnode_get_tag(x, "item"); y; y = xmlnode_get_nextsibling(y)) {
   486   for (y = xmlnode_get_tag(x, "item"); y; y = xmlnode_get_nextsibling(y)) {
   476     const char *alias = xmlnode_get_attrib(y, "jid");
   487     const char *alias = xmlnode_get_attrib(y, "jid");
   477     //const char *sub = xmlnode_get_attrib(y, "subscription"); // TODO Not used
   488     //const char *sub = xmlnode_get_attrib(y, "subscription"); // TODO Not used
   478     const char *name = xmlnode_get_attrib(y, "name");
   489     const char *name = xmlnode_get_attrib(y, "name");
   479     const char *group = NULL;
   490     char *group = NULL;
   480 
   491 
   481     z = xmlnode_get_tag(y, "group");
   492     z = xmlnode_get_tag(y, "group");
   482     if (z) group = xmlnode_get_data(z);
   493     if (z) group = xmlnode_get_data(z);
   483 
   494 
   484     if (alias) {
   495     if (alias) {
   485       char *buddyname;
   496       char *buddyname;
   486       char *cleanalias = jidtodisp(alias);
   497       char *cleanalias = jidtodisp(alias);
   487       if (name)
   498       char *name_noutf8 = NULL;
   488         buddyname = (char*)name;
   499       char *group_noutf8 = NULL;
   489       else
   500 
       
   501       if (name) {
       
   502         name_noutf8 = utf8_decode(name);
       
   503         buddyname = name_noutf8;
       
   504       } else
   490         buddyname = cleanalias;
   505         buddyname = cleanalias;
   491 
   506 
   492       roster_add_user(cleanalias, buddyname, group, ROSTER_TYPE_USER);
   507       if (group) group_noutf8 = utf8_decode(group);
       
   508       roster_add_user(cleanalias, buddyname, group_noutf8, ROSTER_TYPE_USER);
       
   509       if (name_noutf8)  free(name_noutf8);
       
   510       if (group_noutf8) free(group_noutf8);
   493       g_free(cleanalias);
   511       g_free(cleanalias);
   494     }
   512     }
   495   }
   513   }
   496 
   514 
   497   postlogin();
   515   postlogin();
   499 
   517 
   500 void gotmessage(char *type, const char *from, const char *body,
   518 void gotmessage(char *type, const char *from, const char *body,
   501         const char *enc)
   519         const char *enc)
   502 {
   520 {
   503   char *jid;
   521   char *jid;
       
   522   char *buffer = utf8_decode(body);
   504 
   523 
   505   /*
   524   /*
   506   //char *u, *h, *r;
   525   //char *u, *h, *r;
   507   //jidsplit(from, &u, &h, &r);
   526   //jidsplit(from, &u, &h, &r);
   508   // Maybe we should remember the resource?
   527   // Maybe we should remember the resource?
   510     scr_LogPrint("There is an extra part in message (resource?): %s", r);
   529     scr_LogPrint("There is an extra part in message (resource?): %s", r);
   511   //scr_LogPrint("Msg from <%s>, type=%s", jidtodisp(from), type);
   530   //scr_LogPrint("Msg from <%s>, type=%s", jidtodisp(from), type);
   512   */
   531   */
   513 
   532 
   514   jid = jidtodisp(from);
   533   jid = jidtodisp(from);
   515   hk_message_in(jid, 0, body);
   534   hk_message_in(jid, 0, buffer);
   516   g_free(jid);
   535   g_free(jid);
       
   536   free(buffer);
   517 }
   537 }
   518 
   538 
   519 void statehandler(jconn conn, int state)
   539 void statehandler(jconn conn, int state)
   520 {
   540 {
   521   static int previous_state = -1;
   541   static int previous_state = -1;