mcabber/src/jabglue.c
changeset 384 d59e9b8c91d3
parent 374 bd5638c21834
parent 383 39c5da51b387
child 387 2bbeef7b4fdd
equal deleted inserted replaced
382:4c6e8392e465 384:d59e9b8c91d3
    25 #include "../libjabber/jabber.h"
    25 #include "../libjabber/jabber.h"
    26 #include "jabglue.h"
    26 #include "jabglue.h"
    27 #include "roster.h"
    27 #include "roster.h"
    28 #include "screen.h"
    28 #include "screen.h"
    29 #include "hooks.h"
    29 #include "hooks.h"
    30 #include "utf8.h"
       
    31 #include "utils.h"
    30 #include "utils.h"
    32 #include "settings.h"
    31 #include "settings.h"
    33 
    32 
    34 #define JABBERPORT      5222
    33 #define JABBERPORT      5222
    35 #define JABBERSSLPORT   5223
    34 #define JABBERSSLPORT   5223
   236 }
   235 }
   237 
   236 
   238 void jb_setstatus(enum imstatus st, const char *msg)
   237 void jb_setstatus(enum imstatus st, const char *msg)
   239 {
   238 {
   240   xmlnode x;
   239   xmlnode x;
   241   char *utf8_msg;
   240   gchar *utf8_msg;
   242 
   241 
   243   if (!online) return;
   242   if (!online) return;
   244 
   243 
   245   x = jutil_presnew(JPACKET__UNKNOWN, 0, 0);
   244   x = jutil_presnew(JPACKET__UNKNOWN, 0, 0);
   246 
   245 
   285   }
   284   }
   286 
   285 
   287   if (!msg)
   286   if (!msg)
   288       msg = settings_get_status_msg(st);
   287       msg = settings_get_status_msg(st);
   289 
   288 
   290   utf8_msg = utf8_encode(msg);
   289   utf8_msg = g_locale_to_utf8(msg, -1, NULL, NULL, NULL);
   291   xmlnode_insert_cdata(xmlnode_insert_tag(x, "status"), utf8_msg,
   290   xmlnode_insert_cdata(xmlnode_insert_tag(x, "status"), utf8_msg,
   292           (unsigned) -1);
   291           (unsigned) -1);
   293 
   292 
   294   jab_send(jc, x);
   293   jab_send(jc, x);
   295   xmlnode_free(x);
   294   xmlnode_free(x);
   296   free(utf8_msg);
   295   g_free(utf8_msg);
   297 
   296 
   298   //sendvisibility();   ???
   297   //sendvisibility();   ???
   299 
   298 
   300   // We'll need to update the roster if we switch to/from offline because
   299   // We'll need to update the roster if we switch to/from offline because
   301   // we don't know the presences of buddies when offline...
   300   // we don't know the presences of buddies when offline...
   306   mystatus = st;
   305   mystatus = st;
   307 }
   306 }
   308 
   307 
   309 void jb_send_msg(const char *jid, const char *text)
   308 void jb_send_msg(const char *jid, const char *text)
   310 {
   309 {
   311   char *buffer = utf8_encode(text);
   310   gchar *buffer = g_locale_to_utf8(text, -1, NULL, NULL, NULL);
   312   xmlnode x = jutil_msgnew(TMSG_CHAT, (char*)jid, 0, (char*)buffer);
   311   xmlnode x = jutil_msgnew(TMSG_CHAT, (char*)jid, 0, (char*)buffer);
   313   jab_send(jc, x);
   312   jab_send(jc, x);
   314   xmlnode_free(x);
   313   xmlnode_free(x);
   315   free(buffer);
   314   g_free(buffer);
   316   jb_reset_keepalive();
   315   jb_reset_keepalive();
   317 }
   316 }
   318 
   317 
   319 // Note: the caller should check the jid is correct
   318 // Note: the caller should check the jid is correct
   320 void jb_addbuddy(const char *jid, const char *name, const char *group)
   319 void jb_addbuddy(const char *jid, const char *name, const char *group)
   336   y = xmlnode_get_tag(x, "query");
   335   y = xmlnode_get_tag(x, "query");
   337   z = xmlnode_insert_tag(y, "item");
   336   z = xmlnode_insert_tag(y, "item");
   338   xmlnode_put_attrib(z, "jid", jid);
   337   xmlnode_put_attrib(z, "jid", jid);
   339 
   338 
   340   if (name) {
   339   if (name) {
   341     char *name_utf8 = utf8_encode(name);
   340     gchar *name_utf8 = g_locale_to_utf8(name, -1, NULL, NULL, NULL);
   342     z = xmlnode_insert_tag(z, "name");
   341     z = xmlnode_insert_tag(z, "name");
   343     xmlnode_insert_cdata(z, name_utf8, (unsigned) -1);
   342     xmlnode_insert_cdata(z, name_utf8, (unsigned) -1);
   344     free(name_utf8);
   343     g_free(name_utf8);
   345   }
   344   }
   346 
   345 
   347   if (group) {
   346   if (group) {
   348     char *group_utf8 = utf8_encode(group);
   347     char *group_utf8 = g_locale_to_utf8(group, -1, NULL, NULL, NULL);
   349     z = xmlnode_insert_tag(z, "group");
   348     z = xmlnode_insert_tag(z, "group");
   350     xmlnode_insert_cdata(z, group_utf8, (unsigned) -1);
   349     xmlnode_insert_cdata(z, group_utf8, (unsigned) -1);
   351     free(group_utf8);
   350     g_free(group_utf8);
   352   }
   351   }
   353 
   352 
   354   jab_send(jc, x);
   353   jab_send(jc, x);
   355   xmlnode_free(x);
   354   xmlnode_free(x);
   356 
   355 
   406 
   405 
   407 void jb_updatebuddy(const char *jid, const char *name, const char *group)
   406 void jb_updatebuddy(const char *jid, const char *name, const char *group)
   408 {
   407 {
   409   xmlnode x, y;
   408   xmlnode x, y;
   410   char *cleanjid;
   409   char *cleanjid;
   411   char *name_utf8;
   410   gchar *name_utf8;
   412 
   411 
   413   if (!online) return;
   412   if (!online) return;
   414 
   413 
   415   // XXX We should check name's and group's correctness
   414   // XXX We should check name's and group's correctness
   416 
   415 
   417   cleanjid = jidtodisp(jid);
   416   cleanjid = jidtodisp(jid);
   418   name_utf8 = utf8_encode(name);
   417   name_utf8 = g_locale_to_utf8(name, -1, NULL, NULL, NULL);
   419 
   418 
   420   x = jutil_iqnew(JPACKET__SET, NS_ROSTER);
   419   x = jutil_iqnew(JPACKET__SET, NS_ROSTER);
   421   y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item");
   420   y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item");
   422   xmlnode_put_attrib(y, "jid", cleanjid);
   421   xmlnode_put_attrib(y, "jid", cleanjid);
   423   xmlnode_put_attrib(y, "name", name_utf8);
   422   xmlnode_put_attrib(y, "name", name_utf8);
   424 
   423 
   425   if (group) {
   424   if (group) {
   426     char *group_utf8 = utf8_encode(group);
   425     gchar *group_utf8 = g_locale_to_utf8(group, -1, NULL, NULL, NULL);
   427     y = xmlnode_insert_tag(y, "group");
   426     y = xmlnode_insert_tag(y, "group");
   428     xmlnode_insert_cdata(y, group_utf8, (unsigned) -1);
   427     xmlnode_insert_cdata(y, group_utf8, (unsigned) -1);
   429     free(group_utf8);
   428     g_free(group_utf8);
   430   }
   429   }
   431 
   430 
   432   jab_send(jc, x);
   431   jab_send(jc, x);
   433   xmlnode_free(x);
   432   xmlnode_free(x);
   434   free(name_utf8);
   433   g_free(name_utf8);
   435   g_free(cleanjid);
   434   g_free(cleanjid);
   436 }
   435 }
   437 
   436 
   438 void postlogin()
   437 void postlogin()
   439 {
   438 {
   516     if (z) group = xmlnode_get_data(z);
   515     if (z) group = xmlnode_get_data(z);
   517 
   516 
   518     if (alias) {
   517     if (alias) {
   519       char *buddyname;
   518       char *buddyname;
   520       char *cleanalias = jidtodisp(alias);
   519       char *cleanalias = jidtodisp(alias);
   521       char *name_noutf8 = NULL;
   520       gchar *name_noutf8 = NULL;
   522       char *group_noutf8 = NULL;
   521       gchar *group_noutf8 = NULL;
   523 
   522 
   524       if (name) {
   523       if (name) {
   525         name_noutf8 = utf8_decode(name);
   524         name_noutf8 = g_locale_from_utf8(name, -1, NULL, NULL, NULL);
   526         buddyname = name_noutf8;
   525         buddyname = name_noutf8;
   527       } else
   526       } else
   528         buddyname = cleanalias;
   527         buddyname = cleanalias;
   529 
   528 
   530       if (group) group_noutf8 = utf8_decode(group);
   529       if (group)
       
   530         group_noutf8 = g_locale_from_utf8(group, -1, NULL, NULL, NULL);
       
   531 
   531       roster_add_user(cleanalias, buddyname, group_noutf8, ROSTER_TYPE_USER);
   532       roster_add_user(cleanalias, buddyname, group_noutf8, ROSTER_TYPE_USER);
   532       if (name_noutf8)  free(name_noutf8);
   533       if (name_noutf8)  g_free(name_noutf8);
   533       if (group_noutf8) free(group_noutf8);
   534       if (group_noutf8) g_free(group_noutf8);
   534       g_free(cleanalias);
   535       g_free(cleanalias);
   535     }
   536     }
   536   }
   537   }
   537 
   538 
   538   postlogin();
   539   postlogin();
   540 
   541 
   541 void gotmessage(char *type, const char *from, const char *body,
   542 void gotmessage(char *type, const char *from, const char *body,
   542         const char *enc, time_t timestamp)
   543         const char *enc, time_t timestamp)
   543 {
   544 {
   544   char *jid;
   545   char *jid;
   545   char *buffer = utf8_decode(body);
   546   gchar *buffer = g_locale_from_utf8(body, -1, NULL, NULL, NULL);
   546 
   547 
   547   /*
   548   /*
   548   //char *u, *h, *r;
   549   //char *u, *h, *r;
   549   //jidsplit(from, &u, &h, &r);
   550   //jidsplit(from, &u, &h, &r);
   550   // Maybe we should remember the resource?
   551   // Maybe we should remember the resource?
   556   */
   557   */
   557 
   558 
   558   jid = jidtodisp(from);
   559   jid = jidtodisp(from);
   559   hk_message_in(jid, timestamp, buffer, type);
   560   hk_message_in(jid, timestamp, buffer, type);
   560   g_free(jid);
   561   g_free(jid);
   561   free(buffer);
   562   g_free(buffer);
   562 }
   563 }
   563 
   564 
   564 void statehandler(jconn conn, int state)
   565 void statehandler(jconn conn, int state)
   565 {
   566 {
   566   static int previous_state = -1;
   567   static int previous_state = -1;
   849 
   850 
   850         if (type && !strcmp(type, "unavailable"))
   851         if (type && !strcmp(type, "unavailable"))
   851           ust = offline;
   852           ust = offline;
   852 
   853 
   853         if ((x = xmlnode_get_tag(packet->x, "status")) != NULL)
   854         if ((x = xmlnode_get_tag(packet->x, "status")) != NULL)
   854           p = utf8_decode(xmlnode_get_data(x));
   855           p = g_locale_from_utf8(xmlnode_get_data(x), -1, NULL, NULL, NULL);
   855         else
   856         else
   856           p = NULL;
   857           p = NULL;
   857 
   858 
   858         r = jidtodisp(from);
   859         r = jidtodisp(from);
   859         // Call hk_statuschange() if status has changed or if the
   860         // Call hk_statuschange() if status has changed or if the
   860         // status message is different
   861         // status message is different
   861         m = roster_getstatusmsg(r);
   862         m = roster_getstatusmsg(r);
   862         if ((ust != roster_getstatus(r)) || (p && (!m || strcmp(p, m))))
   863         if ((ust != roster_getstatus(r)) || (p && (!m || strcmp(p, m))))
   863           hk_statuschange(r, 0, ust, p);
   864           hk_statuschange(r, 0, ust, p);
   864         g_free(r);
   865         g_free(r);
   865         if (p) free(p);
   866         if (p) g_free(p);
   866         break;
   867         break;
   867 
   868 
   868     case JPACKET_S10N:
   869     case JPACKET_S10N:
   869         scr_LogPrint(LPRINT_LOGNORM, "Received (un)subscription packet "
   870         scr_LogPrint(LPRINT_LOGNORM, "Received (un)subscription packet "
   870                      "(type=%s)", ((type) ? type : ""));
   871                      "(type=%s)", ((type) ? type : ""));