diff mcabber/src/jabglue.c @ 988:6e2bfd1ffded

Add ids to message stanzas if needed
author Mikael Berthe <mikael@lilotux.net>
date Mon, 30 Oct 2006 23:37:58 +0100
parents f47e312560af
children 859ab76e5093
line wrap: on
line diff
--- a/mcabber/src/jabglue.c	Mon Oct 30 20:18:00 2006 +0100
+++ b/mcabber/src/jabglue.c	Mon Oct 30 23:37:58 2006 +0100
@@ -411,8 +411,21 @@
   scr_UpdateMainStatus(TRUE);
 }
 
+//  new_msgid()
+// Generate a new id string.  The caller should free it.
+static char *new_msgid(void)
+{
+  static guint msg_idn;
+  time_t now;
+  time(&now);
+  if (!msg_idn)
+    srand(now);
+  msg_idn += 1U + (unsigned int) (9.0 * (rand() / (RAND_MAX + 1.0)));
+  return g_strdup_printf("%u%d", msg_idn, (int)(now%10L));
+}
+
 void jb_send_msg(const char *jid, const char *text, int type,
-                 const char *subject)
+                 const char *subject, const char *msgid)
 {
   xmlnode x;
   gchar *strtype;
@@ -479,9 +492,19 @@
     event = xmlnode_insert_tag(x, "x");
     xmlnode_put_attrib(event, "xmlns", NS_EVENT);
     xmlnode_insert_tag(event, "composing");
+
+    // An id is mandatory when using JEP-0022.
+    if (!msgid) {
+      msgid = new_msgid();
+      // FIXME update last_msgid_sent
+      // We do not update it when the msgid is provided by the caller,
+      // because this is probably a special message (e.g. delivered...)
+    }
   }
 #endif
 
+  xmlnode_put_attrib(x, "id", msgid);
+
   jab_send(jc, x);
   xmlnode_free(x);