Add the parser of node and the CMakeList.txt
authorNicolas Cornu <nicolas.cornu@ensi-bourges.fr>
Tue, 25 May 2010 00:40:38 +0200
changeset 1 6a9a25d32364
parent 0 6784d21ed078
child 2 a48121a74017
Add the parser of node and the CMakeList.txt
CMakeLists.txt
jingle.c
parse.c
parse.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CMakeLists.txt	Tue May 25 00:40:38 2010 +0200
@@ -0,0 +1,73 @@
+## Copyright 2010 Cornu Nicolas
+# This file is part of mcabber module writing howto examples.
+#
+# Examples are 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, see <http://www.gnu.org/licenses/>.
+
+cmake_minimum_required(VERSION 2.6) 
+project(Notify) 
+set(PROJECT_VERSION "0.0.1")
+
+## User settable options
+ 
+## Check for build dependencies
+find_package(PkgConfig REQUIRED) 
+pkg_check_modules(GLIB REQUIRED glib-2.0) 
+pkg_check_modules(GTK REQUIRED gtk+-2.0) 
+pkg_check_modules(LM REQUIRED loudmouth-1.0) 
+pkg_check_modules(MCABBER REQUIRED mcabber)
+link_directories(${GLIB_LIBRARY_DIRS}
+				 ${LM_LIBRARY_DIRS}
+				 ${MCABBER_LIBRARY_DIRS}
+				 ${GTK_LIBRARY_DIRS})
+
+## Target definitions
+add_library(jingle MODULE jingle.c) 
+
+## Compiler setup
+#configure_file(config.h.in config.h)
+include_directories(SYSTEM ${GLIB_INCLUDE_DIRS} 
+                    ${LM_INCLUDE_DIRS}
+					${MCABBER_INCLUDE_DIRS}
+					${GTK_INCLUDE_DIRS})
+target_link_libraries(jingle ${GLIB_LIBRARIES} 
+					  ${LM_LIBRARIES}
+					  ${MCABBER_LIBRARIES}
+					  ${GTK_LIBRARIES}
+					 )
+include_directories(${notify_SOURCE_DIR} 
+                    ${notify_BINARY_DIR})
+set_target_properties(jingle PROPERTIES COMPILE_FLAGS "-Wall")
+
+## Packaging information
+set(CPACK_PACKAGE_NAME libmcabber-jingle)
+set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
+set(CPACK_PACKAGE_VENDOR "alkino")
+set(CPACK_PACKAGE_CONTACT "Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>")
+set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Some notification for Mcabber")
+#set(CPACK_RESOURCE_FILE_LICENSE ${notify_SOURCE_DIR}/COPYING)
+set(CPACK_SOURCE_GENERATOR TBZ2)
+set(CPACK_GENERATOR DEB CACHE TEXT "Binary package generator, eg DEB, RPM, TGZ, NSIS...")
+set(CPACK_DEBIAN_PACKAGE_SECTION libs)
+find_program(DPKG_EXECUTABLE dpkg)
+if(DPKG_EXECUTABLE)
+	execute_process(COMMAND ${DPKG_EXECUTABLE} --print-architecture OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE)
+else()
+	set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE i386 CACHE TEXT "Architecture of package")
+endif()
+set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
+set(CPACK_SOURCE_IGNORE_FILES "/\\\\..*;\\\\.swp;~$;/build/;\\\\.tar\\\\.;\\\\.deb;\\\\.so")
+include(CPack)
+
+## Installation
+install(TARGETS jingle DESTINATION lib/mcabber) 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jingle.c	Tue May 25 00:40:38 2010 +0200
@@ -0,0 +1,149 @@
+/*
+ * pep.c                -- Common pep routines
+ *
+ * Copyrigth (C) 2009      Myhailo Danylenko <isbear@ukrpost.net>
+ *
+ * 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 <loudmouth/loudmouth.h>
+#include <string.h>
+
+#include <mcabber/xmpp.h>
+#include <mcabber/hooks.h>
+#include <mcabber/modules.h>
+#include <mcabber/logprint.h>
+
+#define NS_JINGLE "urn:xmpp:jingle:1"
+
+struct info_iq {
+  const gchar *from, *to, *type, *id;
+};
+struct info_jingle {
+  const gchar *xmlns, *action, *initiator, *sid;
+};
+
+static void mcjingle_init   (void);
+static void mcjingle_uninit (void);
+
+void parse_iq (LmMessageNode *iq, struct info_iq *ii) {
+  if(!strcmp(iq->name, "iq")) {
+    ii->to = ii->from = ii->type = ii->id = NULL;
+    return;
+  }
+
+  ii->from = lm_message_node_get_attribute (iq, "from");
+  ii->to   = lm_message_node_get_attribute (iq, "to");
+  ii->type = lm_message_node_get_attribute (iq, "type");
+  ii->id   = lm_message_node_get_attribute (iq, "id");
+}
+void parse_jingle (LmMessageNode *jingle, struct info_jingle *ij) {
+  if(!strcmp(ij->name, "jingle")) {
+    ij->to = ij->from = ij->type = ij->id = NULL;
+    return;
+  }
+
+  ii->from = lm_message_node_get_attribute (iq, "from");
+  ii->to   = lm_message_node_get_attribute (iq, "to");
+  ii->type = lm_message_node_get_attribute (iq, "type");
+  ii->id   = lm_message_node_get_attribute (iq, "id");
+}
+
+
+LmMessageHandler* lm = NULL;
+
+module_info_t info_jingle = {
+  .branch          = MCABBER_BRANCH,
+  .api             = MCABBER_API_VERSION,
+  .version         = MCABBER_VERSION,
+  .description     = "A module for jingle",
+  .requires        = NULL,
+  .init            = mcjingle_init,
+  .uninit          = mcjingle_uninit,
+  .next            = NULL,
+};
+
+LmHandlerResult gestionnaire_lm(LmMessageHandler *handler,
+                                LmConnection *connection, LmMessage *message,
+                                gpointer user_data)
+{
+  LmMessageNode * test = lm_message_get_node(message)->children;
+  struct info_iq ii;
+  parse_iq(lm_message_get_node(message), &ii);
+  if(!strcmp(test->name, "jingle")) {
+    if(!strcmp(lm_message_node_get_attribute(test, "xmlns"), NS_JINGLE)) {
+    	gchar* action = g_strdup(lm_message_node_get_attribute(test, "action"));
+    	gchar* initiator = g_strdup(lm_message_node_get_attribute(test, "initiator"));
+    	gchar* responder = g_strdup(lm_message_node_get_attribute(test, "responder"));
+    	gchar* sid = g_strdup(lm_message_node_get_attribute(test, "sid"));
+    	if(!strcmp(action, "content-accept")) {
+     	} else if (!strcmp(action, "content-add")) {
+   	} else if (!strcmp(action, "content-modify")) {
+   	} else if (!strcmp(action, "content-reject")) {
+   	} else if (!strcmp(action, "content-remove")) {
+   	} else if (!strcmp(action, "description-info")) {
+   	} else if (!strcmp(action, "security-info")) {
+   	} else if (!strcmp(action, "session-accept")) {
+   	} else if (!strcmp(action, "session-info")) {
+   	} else if (!strcmp(action, "session-initiate")) {
+   	  LmMessageNode *child = NULL;
+   	  gchar* disposition = NULL;
+   	  for(child = test->children; child; child = child->next) {
+   	    if(!strcmp((disposition = lm_message_node_get_attribute(child, "disposition")), "session"))
+   	      break;
+   	    if(!disposition) {
+   	      disposition = g_strdup("session");
+   	      break;
+   	    }
+   	  }
+   	  if(strcmp(disposition, "session")) {
+   	    //ERREUR
+   	  }
+   	  
+   	} else if (!strcmp(action, "session-terminate")) {
+     	} else if (!strcmp(action, "transport-accept")) {
+     	} else if (!strcmp(action, "transport-info")) {
+     	} else if (!strcmp(action, "transport-reject")) {
+     	} else if (!strcmp(action, "transport-replace")) {
+      } else {
+  		  { // action inconnue => réponse XEP 0166 : 7.2
+
+  		  
+  		  } 		
+  		}
+    } else {
+      scr_log_print(LPRINT_NORMAL, "jingle : Namespace inconnu (%s)", lm_message_node_get_attribute(test, "xmlns"));
+    }
+  }
+  
+  return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+                                                         
+/* Initialization */
+static void mcjingle_init(void)
+{
+  lm = lm_message_handler_new(gestionnaire_lm, NULL, NULL);
+  lm_connection_register_message_handler (lconnection, lm, LM_MESSAGE_TYPE_IQ, LM_HANDLER_PRIORITY_FIRST);
+}
+
+/* Uninitialization */
+static void mcjingle_uninit(void)
+{
+  lm_connection_unregister_message_handler (lconnection, lm, LM_MESSAGE_TYPE_IQ);
+  lm_message_handler_invalidate (lm);
+  lm_message_handler_unref (lm);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/parse.c	Tue May 25 00:40:38 2010 +0200
@@ -0,0 +1,112 @@
+#include <loudmouth/loudmouth.h>
+#include <string.h>
+
+#include "parse.h"
+
+int parse_jingle (LmMessageNode *node, struct info_jingle *ij)
+{
+  int nb_reason = 0;
+  LmMessageNode *child = NULL;
+  
+  if(!strcmp(ij->name, "jingle"))
+    return PARSE_ERROR_NAME;
+
+  ij->action    = attrcpy(lm_message_node_get_attribute(node, "action"));
+  ij->initiator = attrcpy(lm_message_node_get_attribute(node, "initiator"));
+  ij->responder = attrcpy(lm_message_node_get_attribute(node, "responder"));
+  ij->sid       = attrcpy(lm_message_node_get_attribute(node, "sid"));
+   
+  // check required
+  if(ij->action == NULL || ij->sid == NULL)
+    return PARSE_ERROR_REQUIRED;
+    
+  // check restrictions
+ if(!check_restriction(ij->action, {"content-accept", "content-add",
+  "content-modify", "content-reject", "content-remove", "description-info",
+  "security-info", "session-accept", "session-info", "session-initiate",
+  "session-terminate", "transport-accept", "transport-info", "transport-reject",
+  "transport-replace", NULL}))
+   return PARSE_ERROR_RESTRICTION;
+  
+  // check childs
+  for(child = node->children; child; child = child->next) {
+    if(!strcmp(child->name, "reason"))
+      nb_reason++;
+  }
+  
+  if(reason > 1)
+    return PARSE_ERROR_TOO_MANY_CHILDS;
+    
+  return PARSE_OK;
+}
+
+
+void free_jingle(struct info_jingle *ij)
+{
+  free(ij->action);
+  free(ij->initiator);
+  free(ij->responder);
+  free(ij->sid);
+}
+
+
+int parse_content(LmMessageNode* node, struct info_content* ic)
+{
+  if(!strcmp(ic->name, "content"))
+    return PARSE_ERROR_NAME;
+
+  ic->creator     = attrcpy(lm_message_node_get_attribute(node, "creator"));
+  ic->disposition = attrcpy(lm_message_node_get_attribute(node, "disposition"));
+  ic->name        = attrcpy(lm_message_node_get_attribute(node, "name"));
+  ic->senders     = attrcpy(lm_message_node_get_attribute(node, "senders"));
+
+  // Put default if none
+  if(ic->disposition == NULL)
+    ic->disposition = attrcpy("session");
+
+  // check required
+  if(ic->creator == NULL || ic->name == NULL)
+    return PARSE_ERROR_REQUIRED;
+
+  // check restrictions
+  if(!check_restriction(ic->creator, {"initiator", "responder", NULL}))
+    return PARSE_ERROR_RESTRICTION;
+  if(!check_restriction(ic->senders, {"both", "initiator", "none", "responder", NULL}))
+    ic->senders = NULL; // because it's optional
+    
+  return PARSE_OK;
+}
+
+
+void free_content(struct info_content *ic)
+{
+  free(ic->creator);
+  free(ic->disposition);
+  free(ic->name);
+  free(ic->senders);
+}
+
+
+int check_restriction(const char* name, const char** values)
+{
+  const char* value;
+  int found = 0;
+  value = values[0];
+  while(value && !found) {
+    if(!strcmp(name, value))
+      found = 1;
+    value++;
+  }
+  return found;
+}
+
+
+char* attrcpy(const char* attr)
+{
+  char *tmp = NULL;
+  if(attr != NULL) {
+    tmp = (char*) malloc((strlen(attr)+1) * sizeof(char));
+    strcpy(tmp, attr);
+  }
+  return tmp;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/parse.h	Tue May 25 00:40:38 2010 +0200
@@ -0,0 +1,41 @@
+#include <loudmouth/loudmouth.h>
+
+#define PARSE_OK                    0
+#define PARSE_ERROR_NAME            1
+#define PARSE_ERROR_REQUIRED        2
+#define PARSE_ERROR_RESTRICTION     3
+#define PARSE_ERROR_TOO_MANY_CHILDS 4
+
+struct info_iq {
+  char* from;
+  char* id;
+  char* to;
+  char* set;
+};
+
+struct info_jingle {
+  char* action;      // required (content-accept, content-add,
+  // content-modify, content-reject, content-remove, description-info
+  // security-info, session-accept, session-info, session-initiate,
+  // session-terminate, transport-accept, transport-info, transport-reject,
+  // transport-replace)
+  char* initiator;   // optional
+  char* responder;   // optional
+  char* sid;         // required
+};
+
+struct info_content {
+  char* creator;     // required (initiator, responder)
+  char* disposition; // optional, default=session
+  char* name;        // required
+  char* senders;     // optional (both, initiator, none, responder)
+};
+
+int parse_jingle(LmMessageNode* node, struct info_jingle* ij);
+void free_jingle(struct info_jingle* ij);
+int parse_content(LmMessageNode* node, struct info_content* ic);
+void free_content(struct info_content* ic);
+
+int check_restriction(const char* name, const char** values)
+
+char* attrcpy(const char* attr);