changeset 1305:9bc68473f8a3

-n and -f flags to message-sending commands Allows sending normal and headline messages
author Michal 'vorner' Vaner <vorner@ucw.cz>
date Sat, 08 Sep 2007 13:21:07 +0200
parents 8ada97e5eb75
children a7d21918234c
files mcabber/doc/help/en/hlp_msay.txt mcabber/doc/help/en/hlp_say.txt mcabber/doc/help/en/hlp_say_to.txt mcabber/src/commands.c mcabber/src/jabglue.c mcabber/src/jabglue.h
diffstat 6 files changed, 98 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/doc/help/en/hlp_msay.txt	Fri Sep 07 17:07:35 2007 +0200
+++ b/mcabber/doc/help/en/hlp_msay.txt	Sat Sep 08 13:21:07 2007 +0200
@@ -11,11 +11,13 @@
  Enter multi-line mode
 /msay verbatim
  Enter verbatim multi-line mode
-/msay send
+/msay send [-n|-h]
  Send the current multi-line message to the currently selected buddy
-/msay send_to jid
+/msay send_to [-n|-h] jid
  Send the current multi-line message to "jid"
 /msay toggle
  Switch to/from multi-line mode (begin/send)
 /msay abort
  Leave multi-line mode without sending the message
+
+The -n or -h flags turn the message to "normal" or "headline" accordingly, as opposed to default "chat" message.
--- a/mcabber/doc/help/en/hlp_say.txt	Fri Sep 07 17:07:35 2007 +0200
+++ b/mcabber/doc/help/en/hlp_say.txt	Sat Sep 08 13:21:07 2007 +0200
@@ -1,4 +1,5 @@
 
- /SAY text
+ /SAY [-n|-h|--] text
 
 Send the "text" message to the currently selected buddy.  It can be useful if you want to send a message beginning with a slash, for example.
+The "-n" flag turn the message to "normal" type, "-h" to "headline". "--" can be used to send chat message beginning with -n or -h.
--- a/mcabber/doc/help/en/hlp_say_to.txt	Fri Sep 07 17:07:35 2007 +0200
+++ b/mcabber/doc/help/en/hlp_say_to.txt	Sat Sep 08 13:21:07 2007 +0200
@@ -1,5 +1,6 @@
 
- /SAY_TO jid text
+ /SAY_TO [-n|-h|--] jid text
 
 Send the "text" message to the specified jid.
 Please note that this command doesn't set the default resource for a contact, so if you want to send several messages to a specific resource you will have to use "/say_to" for each message.
+The "-n" flag turn the message to "normal" type, "-h" to "headline". "--" can be used to send chat message beginning with -n or -h.
--- a/mcabber/src/commands.c	Fri Sep 07 17:07:35 2007 +0200
+++ b/mcabber/src/commands.c	Sat Sep 08 13:21:07 2007 +0200
@@ -82,6 +82,8 @@
 static void do_otr(char *arg);
 static void do_otrpolicy(char *arg);
 
+static void do_say_internal(char *arg, int parse_flags);
+
 // Global variable for the commands list
 static GSList *Commands;
 
@@ -347,52 +349,6 @@
   return NULL;
 }
 
-//  send_message(msg)
-// Write the message in the buddy's window and send the message on
-// the network.
-static void send_message(const char *msg, const char *subj)
-{
-  const char *bjid;
-  gint crypted;
-
-  if (!jb_getonline()) {
-    scr_LogPrint(LPRINT_NORMAL, "You are not connected.");
-    return;
-  }
-
-  if (!current_buddy) {
-    scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
-    return;
-  }
-
-  bjid = CURRENT_JID;
-  if (!bjid) {
-    scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
-    return;
-  }
-
-  // Network part
-  jb_send_msg(bjid, msg, buddy_gettype(BUDDATA(current_buddy)), subj, NULL,
-              &crypted);
-
-  if (crypted == -1) {
-    scr_LogPrint(LPRINT_LOGNORM, "Encryption error.  Message was not sent.");
-    return;
-  }
-
-  // Hook
-  if (buddy_gettype(BUDDATA(current_buddy)) != ROSTER_TYPE_ROOM) {
-    // local part (UI, logging, etc.)
-    gchar *hmsg;
-    if (subj)
-      hmsg = g_strdup_printf("[%s]\n%s", subj, msg);
-    else
-      hmsg = (char*)msg;
-    hk_message_out(bjid, NULL, 0, hmsg, crypted);
-    if (hmsg != msg) g_free(hmsg);
-  }
-}
-
 //  process_command(line, iscmd)
 // Process a command line.
 // If iscmd is TRUE, process the command even if verbatim mmode is set;
@@ -491,7 +447,7 @@
     if (scr_get_multimode())
       scr_append_multiline(line);
     else
-      do_say(line);
+      do_say_internal(line, 0);
     return 0;
   }
 
@@ -1059,13 +1015,19 @@
   if (leave_buddywindow) scr_ShowBuddyWindow();
 }
 
-static int send_message_to(const char *fjid, const char *msg, const char *subj)
+static int send_message_to(const char *fjid, const char *msg, const char *subj,
+                           const char *type_overwrite)
 {
   char *bare_jid, *rp;
   char *hmsg;
   gint crypted;
   gint retval = 0;
-
+  int isroom;
+
+  if (!jb_getonline()) {
+    scr_LogPrint(LPRINT_NORMAL, "You are not connected.");
+    return 1;
+  }
   if (!fjid || !*fjid) {
     scr_LogPrint(LPRINT_NORMAL, "You must specify a Jabber ID.");
     return 1;
@@ -1090,10 +1052,12 @@
 
   // Check if we're sending a message to a conference room
   // If not, we must make sure rp is NULL, for hk_message_out()
+  isroom = !!roster_find(bare_jid, jidsearch, ROSTER_TYPE_ROOM);
   if (rp) {
-    if (roster_find(bare_jid, jidsearch, ROSTER_TYPE_ROOM)) rp++;
+    if (isroom) rp++;
     else rp = NULL;
   }
+  isroom = isroom && (!rp || !*rp);
 
   // local part (UI, logging, etc.)
   if (subj)
@@ -1102,7 +1066,9 @@
     hmsg = (char*)msg;
 
   // Network part
-  jb_send_msg(fjid, msg, ROSTER_TYPE_USER, subj, NULL, &crypted);
+  jb_send_msg(fjid, msg, (isroom ? ROSTER_TYPE_ROOM : ROSTER_TYPE_USER),
+              subj, NULL, &crypted,
+              type_overwrite);
 
   if (crypted == -1) {
     scr_LogPrint(LPRINT_LOGNORM, "Encryption error.  Message was not sent.");
@@ -1111,7 +1077,8 @@
   }
 
   // Hook
-  hk_message_out(bare_jid, rp, 0, hmsg, crypted);
+  if(!isroom)
+    hk_message_out(bare_jid, rp, 0, hmsg, crypted);
 
 send_message_to_return:
   if (hmsg != msg) g_free(hmsg);
@@ -1119,9 +1086,52 @@
   return retval;
 }
 
-static void do_say(char *arg)
+//  send_message(msg, subj, type_overwrite)
+// Write the message in the buddy's window and send the message on
+// the network.
+static void send_message(const char *msg, const char *subj,
+                         const char *type_overwrite)
+{
+  const char *bjid;
+
+  if (!current_buddy) {
+    scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
+    return;
+  }
+
+  bjid = CURRENT_JID;
+  if (!bjid) {
+    scr_LogPrint(LPRINT_NORMAL, "No buddy is currently selected.");
+    return;
+  }
+
+  send_message_to(bjid, msg, subj, type_overwrite);
+}
+
+static const char *scan_mtype(char **arg)
+{
+  //Try splitting it
+  char **parlist = split_arg(*arg, 2, 1);
+  const char *result = NULL;
+  //Is it any good parameter?
+  if(parlist && *parlist) {
+    if(!strcmp("-n", *parlist)) {
+      result = TMSG_NORMAL;
+    } else if(!strcmp("-h", *parlist)) {
+      result = TMSG_HEADLINE;
+    }
+    if(result || (!strcmp("--", *parlist)))
+      *arg += strlen(*arg) - (parlist[1] ? strlen(parlist[1]) : 0);
+  }
+  //Anything found? -> skip it
+  free_arg_lst(parlist);
+  return result;
+}
+
+static void do_say_internal(char *arg, int parse_flags)
 {
   gpointer bud;
+  const char *msgtype = NULL;
 
   scr_set_chatmode(TRUE);
   scr_ShowBuddyWindow();
@@ -1140,11 +1150,17 @@
   }
 
   buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
+  if(parse_flags)
+    msgtype = scan_mtype(&arg);
   arg = to_utf8(arg);
-  send_message(arg, NULL);
+  send_message(arg, NULL, msgtype);
   g_free(arg);
 }
 
+static void do_say(char *arg) {
+  do_say_internal(arg, 1);
+}
+
 static void do_msay(char *arg)
 {
   /* Parameters: begin verbatim abort send send_to */
@@ -1221,12 +1237,13 @@
   if (!strcasecmp(subcmd, "send_to")) {
     int err = FALSE;
     gchar *msg_utf8;
+    const char *msg_type = scan_mtype(&arg);
     // Let's send to the specified JID.  We leave now if there
     // has been an error (so we don't leave multi-line mode).
     arg = to_utf8(arg);
     msg_utf8 = to_utf8(scr_get_multiline());
     if (msg_utf8) {
-      err = send_message_to(arg, msg_utf8, scr_get_multimode_subj());
+      err = send_message_to(arg, msg_utf8, scr_get_multimode_subj(), msg_type);
       g_free(msg_utf8);
     }
     g_free(arg);
@@ -1251,7 +1268,7 @@
     buddy_setflags(bud, ROSTER_FLAG_LOCK, TRUE);
     msg_utf8 = to_utf8(scr_get_multiline());
     if (msg_utf8) {
-      send_message(msg_utf8, scr_get_multimode_subj());
+      send_message(msg_utf8, scr_get_multimode_subj(), scan_mtype(&arg));
       g_free(msg_utf8);
     }
   }
@@ -1265,12 +1282,14 @@
 {
   char **paramlst;
   char *fjid, *msg;
+  const char *msg_type = NULL;
 
   if (!jb_getonline()) {
     scr_LogPrint(LPRINT_NORMAL, "You are not connected.");
     return;
   }
 
+  msg_type = scan_mtype(&arg);
   paramlst = split_arg(arg, 2, 1); // jid, message
   fjid = *paramlst;
   msg = *(paramlst+1);
@@ -1284,7 +1303,7 @@
   fjid = to_utf8(fjid);
   msg = to_utf8(msg);
 
-  send_message_to(fjid, msg, NULL);
+  send_message_to(fjid, msg, NULL, msg_type);
 
   g_free(fjid);
   g_free(msg);
@@ -2286,7 +2305,7 @@
   arg = to_utf8(arg);
   // Set the topic
   msg = g_strdup_printf("%s has set the topic to: %s", mkcmdstr("me"), arg);
-  jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg, NULL, NULL);
+  jb_send_msg(buddy_getjid(bud), msg, ROSTER_TYPE_ROOM, arg, NULL, NULL, NULL);
   g_free(arg);
   g_free(msg);
 }
--- a/mcabber/src/jabglue.c	Fri Sep 07 17:07:35 2007 +0200
+++ b/mcabber/src/jabglue.c	Sat Sep 08 13:21:07 2007 +0200
@@ -557,10 +557,11 @@
 // message has been PGP-encrypted.  If encryption enforcement is set and
 // encryption fails, *encrypted is set to -1.
 void jb_send_msg(const char *fjid, const char *text, int type,
-                 const char *subject, const char *msgid, gint *encrypted)
+                 const char *subject, const char *msgid, gint *encrypted,
+                 const char *type_overwrite)
 {
   xmlnode x;
-  gchar *strtype;
+  const gchar *strtype;
 #ifdef HAVE_LIBOTR
   int otr_msg = 0;
 #endif
@@ -583,10 +584,14 @@
 
   if (!online) return;
 
-  if (type == ROSTER_TYPE_ROOM)
-    strtype = TMSG_GROUPCHAT;
-  else
-    strtype = TMSG_CHAT;
+  if (type_overwrite)
+    strtype = type_overwrite;
+  else {
+    if (type == ROSTER_TYPE_ROOM)
+      strtype = TMSG_GROUPCHAT;
+    else
+      strtype = TMSG_CHAT;
+  }
 
 #if defined HAVE_GPGME || defined HAVE_LIBOTR || defined JEP0022 || defined JEP0085
   rname = strchr(fjid, JID_RESOURCE_SEPARATOR);
--- a/mcabber/src/jabglue.h	Fri Sep 07 17:07:35 2007 +0200
+++ b/mcabber/src/jabglue.h	Sat Sep 08 13:21:07 2007 +0200
@@ -62,7 +62,8 @@
                   int do_not_sign);
 inline void jb_setprevstatus(void);
 void jb_send_msg(const char *fjid, const char *text, int type,
-                 const char *subject, const char *id, gint *encrypted);
+                 const char *subject, const char *id, gint *encrypted,
+                 const char *type_overwrite);
 void jb_send_raw(const char *str);
 void jb_send_chatstate(gpointer buddy, guint chatstate);
 void jb_keepalive(void);