changeset 355:c5a7a7273986

Add some external actions Now the external command can get the following parameters: - MSG IN jid - MSG OUT jid - STATUS $S jid ($S == status uppercase letter: O, I, F, D, N, A, _)
author Mikael Berthe <mikael@lilotux.net>
date Fri, 22 Jul 2005 21:45:45 +0100
parents 80ccf8e6c23f
children a30ca1b77fa2
files mcabber/contrib/eventcmd mcabber/mcabberrc.example mcabber/src/hooks.c
diffstat 3 files changed, 47 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/contrib/eventcmd	Fri Jul 22 20:21:41 2005 +0100
+++ b/mcabber/contrib/eventcmd	Fri Jul 22 21:45:45 2005 +0100
@@ -26,5 +26,8 @@
       # Outgoing message for buddy $arg2
       ;;
   esac
+elif [ $event == "STATUS" ]; then
+  # Buddy $arg2 status is $arg1 (_, O, I, F, D, N, A)
+  echo > /dev/null
 fi
 
--- a/mcabber/mcabberrc.example	Fri Jul 22 20:21:41 2005 +0100
+++ b/mcabber/mcabberrc.example	Fri Jul 22 21:45:45 2005 +0100
@@ -42,9 +42,11 @@
 
 # External command for events
 # You can specify a script or process to be launched when an event occurs.
-# For now it is called the following way:
-#   $events_command MSG IN jabber@id
-# ... when receiving a message (see sample script in contrib/).
+# The command is called the following way:
+#   $events_command MSG IN jabber@id        (when receiving a message)
+#   $events_command MSG OUT jabber@id       (when sending a message)
+#   $events_command STATUS X jabber@id      (new buddy status is X)
+# See sample script in contrib/ directory.
 #set events_command = /home/mikael/.mcabber/eventcmd
 
 # Debug logging
--- a/mcabber/src/hooks.c	Fri Jul 22 20:21:41 2005 +0100
+++ b/mcabber/src/hooks.c	Fri Jul 22 21:45:45 2005 +0100
@@ -29,7 +29,7 @@
 #include "utf8.h"
 #include "hbuf.h"
 
-static char *extcommand;
+static char *extcmd;
 
 inline void hk_message_in(const char *jid, time_t timestamp, const char *msg,
                           const char *type)
@@ -56,6 +56,7 @@
   // We don't log the message if it is an error message
   if (!(message_flags & HBB_PREFIX_ERR))
     hlog_write_message(jid, timestamp, FALSE, msg);
+  // External command
   hk_ext_cmd(jid, 'M', 'R', NULL);
   // We need to rebuild the list if the sender is unknown or
   // if the sender is offline/invisible and hide_offline_buddies is set
@@ -71,6 +72,8 @@
 {
   scr_WriteOutgoingMessage(jid, msg);
   hlog_write_message(jid, timestamp, TRUE, msg);
+  // External command
+  hk_ext_cmd(jid, 'M', 'S', NULL);
 }
 
 inline void hk_statuschange(const char *jid, time_t timestamp, 
@@ -83,6 +86,8 @@
   buddylist_build();
   scr_DrawRoster();
   hlog_write_status(jid, 0, status, status_msg);
+  // External command
+  hk_ext_cmd(jid, 'S', imstatus2char[status], NULL);
 }
 
 inline void hk_mystatuschange(time_t timestamp,
@@ -105,12 +110,12 @@
 // Can be called with parameter NULL to reset and free memory.
 void hk_ext_cmd_init(const char *command)
 {
-  if (extcommand) {
-    g_free(extcommand);
-    extcommand = NULL;
+  if (extcmd) {
+    g_free(extcmd);
+    extcmd = NULL;
   }
   if (command)
-    extcommand = g_strdup(command);
+    extcmd = g_strdup(command);
 }
 
 //  hk_ext_cmd()
@@ -119,21 +124,44 @@
 void hk_ext_cmd(const char *jid, guchar type, guchar info, const char *data)
 {
   pid_t pid;
+  char *arg_type = NULL;
+  char *arg_info = NULL;
+  char *arg_data = NULL;
+  char status_str[2];
 
-  if (!extcommand) return;
+  if (!extcmd) return;
 
-  // For now we'll only handle incoming messages
-  if (type != 'M') return;
-  if (info != 'R') return;
+  // Prepare arg_* (external command parameters)
+  switch (type) {
+    case 'M':
+        arg_type = "MSG";
+        if (info == 'R')
+          arg_info = "IN";
+        else if (info == 'S')
+          arg_info = "OUT";
+
+        break;
+    case 'S':
+        arg_type = "STATUS";
+        if (strchr(imstatus2char, tolower(info))) {
+          status_str[0] = toupper(info);
+          status_str[1] = 0;
+          arg_info = status_str;
+        }
+        break;
+    default:
+        return;
+  }
+
+  if (!arg_type || !arg_info) return;
 
   if ((pid=fork()) == -1) {
     scr_LogPrint("Fork error, cannot launch external command.");
     return;
   }
 
-  // I don't remember what I should do with the parent process...
   if (pid == 0) { // child
-    if (execl(extcommand, extcommand, "MSG", "IN", jid, NULL) == -1) {
+    if (execl(extcmd, extcmd, arg_type, arg_info, jid, arg_data) == -1) {
       // ut_WriteLog("Cannot execute external command.\n");
       exit(1);
     }