New file for action handlers, new struct to represent a content element.
authorNicolas Cornu <nicolas.cornu@ensi-bourges.fr>
Sun, 06 Jun 2010 17:36:23 +0200
changeset 13 4e6245ccd73c
parent 12 eb167636e5bc
child 14 77966ed56e14
New file for action handlers, new struct to represent a content element.
action-handlers.c
action-handlers.h
check.c
check.h
jingle.c
jingle.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/action-handlers.c	Sun Jun 06 17:36:23 2010 +0200
@@ -0,0 +1,31 @@
+/*
+ * action-handlers.c
+ *
+ * Copyrigth (C) 2010 Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include <glib.h>
+
+#include "jingle.h"
+#include "check.h"
+
+void handle_session_initiate(LmMessage *m, JingleNode *jn, GError *err)
+{
+  // do stuff here
+  
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/action-handlers.h	Sun Jun 06 17:36:23 2010 +0200
@@ -0,0 +1,10 @@
+#ifndef __JINGLE_ACTION_HANDLERS_H__
+#define __JINGLE_ACTION_HANDLERS_H__ 1
+
+#include <glib.h>
+#include <loudmouth/loudmouth.h>
+
+
+void handle_session_initiate(LmMessage *m, LmMessageNode *j);
+
+#endif
--- a/check.c	Tue Jun 01 11:51:25 2010 +0200
+++ b/check.c	Sun Jun 06 17:36:23 2010 +0200
@@ -26,6 +26,11 @@
 #include "jingle.h"
 
 
+JingleContentNode *check_content(LmMessageNode *node, GError **err);
+gint index_in_array(const gchar *str, const gchar **array);
+
+
+
 const gchar *jingle_content_creator[] = {
   "initiator",
   "responder",
@@ -45,32 +50,32 @@
  * Populate a jingle_data struct from a <jingle> element.
  * Check if the element is in compliance with the XEP.
  */
-gboolean check_jingle(LmMessageNode *node, JingleData *jd, GError **err)
+gboolean check_jingle(LmMessageNode *node, JingleNode *jn, GError **err)
 {
-  gint nb_reason = 0;
+  //gint nb_reason = 0;
   LmMessageNode *child = NULL;
   const gchar *actionstr;
+  JingleContentNode *cn;
 
   actionstr     = lm_message_node_get_attribute(node, "action");
-  jd->initiator = lm_message_node_get_attribute(node, "initiator");
-  jd->responder = lm_message_node_get_attribute(node, "responder");
-  jd->sid       = lm_message_node_get_attribute(node, "sid");
+  jn->initiator = lm_message_node_get_attribute(node, "initiator");
+  jn->responder = lm_message_node_get_attribute(node, "responder");
+  jn->sid       = lm_message_node_get_attribute(node, "sid");
 
-  if (actionstr == NULL || jd->sid == NULL) {
+  if (actionstr == NULL || jn->sid == NULL) {
     g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
                 "an attribute of the jingle element is missing");
     return FALSE;
   }
 
-  jd->action = jingle_action_from_str(actionstr);
-  if (jd->action == JINGLE_UNKNOWN_ACTION) {
+  jn->action = jingle_action_from_str(actionstr);
+  if (jn->action == JINGLE_UNKNOWN_ACTION) {
     g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_BADVALUE,
                 "the action attribute is invalid");
     return FALSE;
   }
 
-  // check childs
-  for (child = node->children; child; child = child->next) {
+  /*for (child = node->children; child; child = child->next) {
     if (!g_strcmp0(child->name, "reason"))
       nb_reason++;
   }
@@ -79,11 +84,75 @@
     g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_BADELEM,
                 "too many reason elements");
     return FALSE;
+  }*/
+  
+  for (child = node->children; child; child = child->next) {
+    if (!g_strcmp0(child->name, "content")) {
+      cn = check_content(node, err);
+      if(cn == NULL) {
+        g_assert (*err != NULL);
+        return FALSE;
+	  }
+	  jn->content = g_list_append(jn->content, cn);
+    }
   }
 
   return TRUE;
 }
 
+JingleContentNode *check_content(LmMessageNode *node, GError **err)
+{
+  JingleContentNode *cn = g_new0(JingleContentNode, 1);
+  const gchar *creatorstr, *sendersstr;
+  gint tmp, tmp2;
+
+  creatorstr      = lm_message_node_get_attribute(node, "action");
+  cn->disposition = lm_message_node_get_attribute(node, "disposition");
+  cn->name        = lm_message_node_get_attribute(node, "name");
+  sendersstr      = lm_message_node_get_attribute(node, "senders");
+
+  if (creatorstr == NULL || cn->name == NULL) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
+                "an attribute of the content element is missing");
+    g_free(cn);
+    return NULL;
+  }
+  
+  tmp = index_in_array(creatorstr, jingle_content_creator);
+  tmp2 = index_in_array(sendersstr, jingle_content_senders);
+  if (tmp < 0 || tmp2 < 0) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_BADVALUE,
+                "the attribute creator or sender is invalid");
+    g_free(cn);
+    return NULL;
+  }
+  cn->creator = (JingleCreator)tmp;
+  cn->senders = (JingleSenders)tmp2;
+  
+
+  cn->description = lm_message_node_get_child(node, "description");
+  cn->transport   = lm_message_node_get_child(node, "transport");
+  if (cn->description == NULL || cn->transport == NULL) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
+                "a child element of content is missing");
+    g_free(cn);
+    return NULL;
+  }
+  
+  return cn;
+}
+
+gint index_in_array(const gchar *str, const gchar **array)
+{
+  gint i;
+  for (i = 0; array[i]; i++) {
+    if (!g_strcmp0(array[i], str)) {
+      return i;
+    }
+  }
+  return -1;
+}
+
 GQuark jingle_check_error_quark()
 {
   return g_quark_from_string("JINGLE_CHECK_ERROR");
--- a/check.h	Tue Jun 01 11:51:25 2010 +0200
+++ b/check.h	Sun Jun 06 17:36:23 2010 +0200
@@ -1,5 +1,5 @@
-#ifndef __PARSE_H__
-#define __PARSE_H__ 1
+#ifndef __JINGLE_CHECK_H__
+#define __JINGLE_CHECK_H__ 1
 
 #include <glib.h>
 #include <loudmouth/loudmouth.h>
@@ -15,22 +15,8 @@
   JINGLE_CHECK_ERROR_BADVALUE  // the value of an attribute is incorrect
 } JingleCheckError;
 
-typedef struct {
-  JingleAction action;
-  const gchar* initiator; // optional
-  const gchar* responder; // optional
-  const gchar* sid;       // required
-} JingleData;
 
-typedef struct {
-  const gchar* creator;     // required (initiator, responder)
-  const gchar* disposition; // optional, default=session
-  const gchar* name;        // required
-  const gchar* senders;     // optional (both, initiator, none, responder)
-} ContentData;
-
-
-int check_jingle(LmMessageNode* node, JingleData *jd, GError **err);
+int check_jingle(LmMessageNode* node, JingleNode *jd, GError **err);
 GQuark jingle_check_error_quark();
 
 #endif
--- a/jingle.c	Tue Jun 01 11:51:25 2010 +0200
+++ b/jingle.c	Sun Jun 06 17:36:23 2010 +0200
@@ -74,7 +74,7 @@
   .api             = MCABBER_API_VERSION,
   .version         = MCABBER_VERSION,
   .description     = "Main Jingle module,"
-    " required for file transport, voip...\n",
+                     " required for file transport, voip...\n",
   .requires        = NULL,
   .init            = jingle_init,
   .uninit          = jingle_uninit,
@@ -83,15 +83,15 @@
 
 
 LmHandlerResult jingle_handle_iq(LmMessageHandler *handler,
-    LmConnection *connection,
-    LmMessage *message,
-    gpointer user_data)
+                                 LmConnection *connection,
+                                 LmMessage *message,
+                                 gpointer user_data)
 {
   LmMessageSubType iqtype = lm_message_get_sub_type(message);
   if (iqtype != LM_MESSAGE_SUB_TYPE_SET)
     return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
 
-  JingleData jd;
+  JingleNode *jn = g_new0(JingleNode, 1);
   GError *error;
   LmMessageNode *root = lm_message_get_node(message)->children;
   LmMessageNode *node = lm_message_node_get_child(root, "jingle");
@@ -104,21 +104,22 @@
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
   }
 
-  check_jingle(node, &jd, &error);
+  check_jingle(node, jn, &error);
   if (error != NULL) {
     if (error->code == JINGLE_CHECK_ERROR) {
       // request malformed, we reply with a bad-request
-      jingle_error_iq(message, "cancel", "bad-request", NULL);
+      jingle_send_iq_error(message, "cancel", "bad-request", NULL);
     }
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
   }
   
   scr_log_print(LPRINT_DEBUG, "jingle: Received a valid jingle IQ");
   
-  if (jingle_action_list[jd.action].handler != NULL)
-    jingle_action_list[jd.action].handler(NULL);
+  if (jingle_action_list[jn->action].handler != NULL)
+    jingle_action_list[jn->action].handler(message, jn, &error);
   else
-    jingle_error_iq(message, "cancel", "feature-not-implemented", "unsupported-info");
+    jingle_send_iq_error(message, "cancel", "feature-not-implemented",
+                         "unsupported-info");
 
   return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 }
@@ -142,8 +143,8 @@
 /**
  * Reply to a Jingle IQ with an error.
  */
-void jingle_error_iq(LmMessage *m, const gchar *errtype,
-                     const gchar *cond, const gchar *jinglecond)
+LmMessage *jingle_new_iq_error(LmMessage *m, const gchar *errtype,
+                               const gchar *cond, const gchar *jinglecond)
 {
   LmMessage *r;
   LmMessageNode *err, *tmpnode;
@@ -151,21 +152,30 @@
   r = lm_message_new_iq_from_query(m, LM_MESSAGE_SUB_TYPE_ERROR);
   err = lm_message_node_add_child(r->node, "error", NULL);
   lm_message_node_set_attribute(err, "type", errtype);
-  
+
   // error condition as defined by RFC 3920bis section 8.3.3
   if (cond != NULL) {
     tmpnode = lm_message_node_add_child(err, cond, NULL);
     lm_message_node_set_attribute(tmpnode, "xmlns", NS_XMPP_STANZAS);
   }
-  
+
   // jingle error condition as defined by XEP-0166 section 10
   if (jinglecond != NULL) {
     tmpnode = lm_message_node_add_child(err, jinglecond, NULL);
     lm_message_node_set_attribute(tmpnode, "xmlns", NS_JINGLE_ERRORS);
   }
-  
-  lm_connection_send(lconnection, r, NULL);
-  lm_message_unref(r);
+
+  return r;
+}
+
+void jingle_send_iq_error(LmMessage *m, const gchar *errtype,
+                          const gchar *cond, const gchar *jinglecond)
+{
+  LmMessage *r = jingle_new_iq_error(m, errtype, cond, jinglecond);
+  if (r) {
+	  lm_connection_send(lconnection, r, NULL);
+	  lm_message_unref(r);
+  }
 }
 
 /**
@@ -173,7 +183,7 @@
  */
 JingleAction jingle_action_from_str(const gchar* string)
 {
-  guint i, actstrlen = sizeof(jingle_action_list)/sizeof(struct JingleActionList);
+  guint i, actstrlen = sizeof(jingle_action_list)/sizeof(jingle_action_list[0]);
   for (i = 0; i < actstrlen; i++)
     if (!g_strcmp0(jingle_action_list[i].name, string))
       return (JingleAction) i;
--- a/jingle.h	Tue Jun 01 11:51:25 2010 +0200
+++ b/jingle.h	Sun Jun 06 17:36:23 2010 +0200
@@ -27,13 +27,83 @@
   JINGLE_TRANSPORT_REPLACE,
 } JingleAction;
 
+typedef enum {
+  JINGLE_CREATOR_INITIATOR,
+  JINGLE_CREATOR_RESPONDER,
+} JingleCreator;
+
+typedef enum {
+  JINGLE_SENDERS_BOTH,
+  JINGLE_SENDERS_INITIATOR,
+  JINGLE_SENDERS_NONE,
+  JINGLE_SENDERS_RESPONDER,
+} JingleSenders;
+
+typedef struct {
+  /* action attribute */
+  JingleAction action;
+
+  /* full JID of the entity that has initiated the session flow.
+   * may be different from the 'from' address on the IQ-set of the
+   * session-initiate message.
+   * recommended for session-initiate, not recommended otherwise. */
+  const gchar *initiator;
+
+  /* full JID of the entity that has replied to the initation.
+   * can be different from the 'to' address on the IQ-set.
+   * recommended for session-accept, not recommended otherwise. */
+  const gchar *responder;
+
+  /* Random session identifier generated by the initator. */
+  const gchar *sid;
+
+  /* Doubly-linked list of JingleContentNode. */
+  GList *content;
+
+} JingleNode;
+
+typedef struct {
+  /* which party originally generated the content type.
+   * the defined values are "initiator" and "responder"
+   * (where the default is "initiator"). required. */
+  JingleCreator creator;
+
+  /* how the content definition is to be interpreted by the recipient.
+   * optional, the default value is "session". */
+  const gchar *disposition; // optional, default=session
+
+  /* A unique name or identifier for the content type
+   * according to the creator. required.*/
+  const gchar *name;
+
+  /* which parties in the session will be generating content.
+   * allowable values are both, initiator, none, responder.
+   * default is both.
+   * required for content-modify, optional otherwise */
+  JingleSenders senders;
+
+  /* each content element (must) contain one description
+   * child element that specifies a desired application.
+   * the content of this node is app specific. */
+  LmMessageNode *description;
+
+  /* each content element (must) contain one transport
+   * child element that specifies a potential transport
+   * method */
+  LmMessageNode *transport;
+
+} JingleContentNode;
+
 struct JingleActionList {
   const gchar  *name;
-  void (*handler)(void *arg1);
+  void (*handler)(LmMessage *, JingleNode *, GError **);
 };
 
-void jingle_error_iq(LmMessage *m, const gchar *errtype,
-                     const gchar *cond, const gchar *jinglecond);
+
+LmMessage *jingle_new_iq_error(LmMessage *m, const gchar *errtype,
+                               const gchar *cond, const gchar *jinglecond);
+void jingle_send_iq_error(LmMessage *m, const gchar *errtype,
+                          const gchar *cond, const gchar *jinglecond);
 void jingle_ack_iq(LmMessage *m);
 JingleAction jingle_action_from_str(const gchar* string);