killpresence/killpresence.c
changeset 44 3f94f3522960
parent 43 d759a8b8dbbe
child 45 8e4d38e667ef
equal deleted inserted replaced
43:d759a8b8dbbe 44:3f94f3522960
    34 #include <mcabber/commands.h>
    34 #include <mcabber/commands.h>
    35 #include <mcabber/compl.h>
    35 #include <mcabber/compl.h>
    36 #include <mcabber/roster.h>
    36 #include <mcabber/roster.h>
    37 #include <mcabber/screen.h>
    37 #include <mcabber/screen.h>
    38 #include <mcabber/utils.h>
    38 #include <mcabber/utils.h>
       
    39 #include <mcabber/xmpp.h>
    39 
    40 
    40 static void killpresence_init(void);
    41 static void killpresence_init(void);
    41 static void killpresence_uninit(void);
    42 static void killpresence_uninit(void);
    42 
    43 
    43 /* Module description */
    44 /* Module description */
    44 module_info_t info_killpresence = {
    45 module_info_t info_killpresence = {
    45         .branch         = MCABBER_BRANCH,
    46         .branch         = MCABBER_BRANCH,
    46         .api            = MCABBER_API_VERSION,
    47         .api            = MCABBER_API_VERSION,
    47         .version        = "0.03",
    48         .version        = "0.10",
    48         .description    = "Ignore an item's current presence(s)\n"
    49         .description    = "Ignore an item's current presence(s)\n"
    49                           " Provides the following commands:\n"
    50                           " Provides the following commands:\n"
    50                           " /killpresence $fulljid\n"
    51                           " /killpresence $fulljid\n"
    51                           " /killchatstates $fulljid",
    52                           " /killchatstates $fulljid"
       
    53                           " /probe $barejid",
    52         .requires       = NULL,
    54         .requires       = NULL,
    53         .init           = killpresence_init,
    55         .init           = killpresence_init,
    54         .uninit         = killpresence_uninit,
    56         .uninit         = killpresence_uninit,
    55         .next           = NULL,
    57         .next           = NULL,
    56 };
    58 };
    57 
    59 
    58 #ifdef MCABBER_API_HAVE_CMD_ID
    60 #ifdef MCABBER_API_HAVE_CMD_ID
    59 static gpointer killpresence_cmdid, killchatstates_cmdid;
    61 static gpointer killpresence_cmdid, killchatstates_cmdid, probe_cmdid;
    60 #endif
    62 #endif
    61 
    63 
    62 static void do_killpresence(char *args)
    64 static void do_killpresence(char *args)
    63 {
    65 {
    64   char *jid_utf8, *res;
    66   char *jid_utf8, *res;
   180 #else
   182 #else
   181   scr_log_print(LPRINT_NORMAL, "No Chat State support.");
   183   scr_log_print(LPRINT_NORMAL, "No Chat State support.");
   182 #endif
   184 #endif
   183 }
   185 }
   184 
   186 
       
   187 static void do_probe(char *args)
       
   188 {
       
   189   char *jid_utf8;
       
   190   LmMessage *m;
       
   191   const char *targetjid = NULL;
       
   192 
       
   193   if (!args || !*args) {
       
   194     scr_log_print(LPRINT_NORMAL, "I need a JID.");
       
   195     return;
       
   196   }
       
   197   if (strchr(args, JID_RESOURCE_SEPARATOR)) {
       
   198     scr_log_print(LPRINT_NORMAL, "I need a *bare* JID.");
       
   199     // XXX We could just drop the resource...
       
   200     return;
       
   201   }
       
   202 
       
   203   if (!xmpp_is_online())
       
   204     return;
       
   205 
       
   206   jid_utf8 = to_utf8(args);
       
   207   if (!jid_utf8)
       
   208     return;
       
   209 
       
   210   if (!strcmp(jid_utf8, ".")) {
       
   211     if (current_buddy)
       
   212       targetjid = CURRENT_JID;
       
   213   } else {
       
   214     targetjid = jid_utf8;
       
   215   }
       
   216 
       
   217   // Create presence message with type "probe"
       
   218   m = lm_message_new(targetjid, LM_MESSAGE_TYPE_PRESENCE);
       
   219   lm_message_node_set_attribute(m->node, "type", "probe");
       
   220   lm_connection_send(lconnection, m, NULL);
       
   221   lm_message_unref(m);
       
   222   scr_log_print(LPRINT_LOGNORM, "Presence probe sent to <%s>.", targetjid);
       
   223   g_free(jid_utf8);
       
   224 }
       
   225 
   185 
   226 
   186 /* Initialization */
   227 /* Initialization */
   187 static void killpresence_init(void)
   228 static void killpresence_init(void)
   188 {
   229 {
   189   /* Add command */
   230   /* Add command */
   190 #ifdef MCABBER_API_HAVE_CMD_ID
   231 #ifdef MCABBER_API_HAVE_CMD_ID
   191   killpresence_cmdid = cmd_add("killpresence", "Ignore presence",
   232   killpresence_cmdid = cmd_add("killpresence", "Ignore presence",
   192                                COMPL_JID, 0, do_killpresence, NULL);
   233                                COMPL_JID, 0, do_killpresence, NULL);
   193   killchatstates_cmdid = cmd_add("killchatstates", "Reset chatstates",
   234   killchatstates_cmdid = cmd_add("killchatstates", "Reset chatstates",
   194                                  COMPL_JID, 0, do_killchatstates, NULL);
   235                                  COMPL_JID, 0, do_killchatstates, NULL);
       
   236   probe_cmdid = cmd_add("probe", "Send a presence probe",
       
   237                                  COMPL_JID, 0, do_probe, NULL);
   195 #else
   238 #else
   196   cmd_add("killpresence", "Ignore presence", COMPL_JID, 0,
   239   cmd_add("killpresence", "Ignore presence", COMPL_JID, 0,
   197           do_killpresence, NULL);
   240           do_killpresence, NULL);
   198   cmd_add("killchatstates", "Reset chatstates", COMPL_JID, 0,
   241   cmd_add("killchatstates", "Reset chatstates", COMPL_JID, 0,
   199           do_killchatstates, NULL);
   242           do_killchatstates, NULL);
       
   243   cmd_add("probe", "Send a presence probe", COMPL_JID, 0,
       
   244           do_probe, NULL);
   200 #endif
   245 #endif
   201 }
   246 }
   202 
   247 
   203 /* Uninitialization */
   248 /* Uninitialization */
   204 static void killpresence_uninit(void)
   249 static void killpresence_uninit(void)
   205 {
   250 {
   206   /* Unregister command */
   251   /* Unregister command */
   207 #ifdef MCABBER_API_HAVE_CMD_ID
   252 #ifdef MCABBER_API_HAVE_CMD_ID
   208   cmd_del(killpresence_cmdid);
   253   cmd_del(killpresence_cmdid);
   209   cmd_del(killchatstates_cmdid);
   254   cmd_del(killchatstates_cmdid);
       
   255   cmd_del(probe_cmdid);
   210 #else
   256 #else
   211   cmd_del("killpresence");
   257   cmd_del("killpresence");
   212   cmd_del("killchatstates");
   258   cmd_del("killchatstates");
       
   259   cmd_del("probe");
   213 #endif
   260 #endif
   214 }
   261 }
   215 
   262 
   216 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */
   263 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */