modules_k/pua/add_events.c
4b79a143
 /*
  * $Id: add_events.c  2007-05-03 15:05:20Z anca_vamanu $
  *
  * pua module - presence user agent module
  *
  * Copyright (C) 2007 Voice Sistem S.R.L.
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
4b79a143
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
4b79a143
  * 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
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
4b79a143
  * 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
  *
  *	initial version 2007-05-03 (anca)
  */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <libxml/parser.h>
 
 #include "event_list.h"
 #include "add_events.h"
1d908e24
 #include "pua.h"
aab0d76c
 #include "pidf.h"
4b79a143
 
3d69afa8
 extern int dlginfo_increase_version;
 
c70d27ce
 int pua_add_events(void)
4b79a143
 {
 	/* add presence */
 	if(add_pua_event(PRESENCE_EVENT, "presence", "application/pidf+xml", 
 				pres_process_body)< 0)
 	{
85a70026
 		LM_ERR("while adding event presence\n");
4b79a143
 		return -1;
 	}
 
3d69afa8
 	/* add dialog */
 	if (dlginfo_increase_version) {
 		if(add_pua_event(DIALOG_EVENT, "dialog", "application/dialog-info+xml",
 					bla_process_body)< 0)
 		{
 			LM_ERR("while adding event dialog w/ dlginfo_increase_version\n");
 			return -1;
 		}
 	} else {
 		if(add_pua_event(DIALOG_EVENT, "dialog", "application/dialog-info+xml",
 					dlg_process_body)< 0)
 		{
 			LM_ERR("while adding event dialog w/o dlginfo_increase_version\n");
 			return -1;
 		}
 	}
 
4b79a143
 	/* add dialog;sla */
 	if(add_pua_event(BLA_EVENT, "dialog;sla", "application/dialog-info+xml",
 				bla_process_body)< 0)
 	{
3d69afa8
 		LM_ERR("while adding event dialog;sla\n");
4b79a143
 		return -1;
 	}
 
 	/* add message-summary*/
 	if(add_pua_event(MSGSUM_EVENT, "message-summary", 
 				"application/simple-message-summary", mwi_process_body)< 0)
 	{
3d69afa8
 		LM_ERR("while adding event message-summary\n");
4b79a143
 		return -1;
 	}
 	
 	/* add presence;winfo */
30db13bb
 	if(add_pua_event(PWINFO_EVENT, "presence.winfo", NULL, NULL)< 0)
4b79a143
 	{
3d69afa8
 		LM_ERR("while adding event presence.winfo\n");
4b79a143
 		return -1;
 	}
 	
 	return 0;
 
 }	
 
feaff680
 int pres_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple_param)
4b79a143
 {
 
 	xmlDocPtr doc= NULL;
 	xmlNodePtr node= NULL;
 	char* tuple_id= NULL, *person_id= NULL;
 	int tuple_id_len= 0;
 	char buf[50];
 	str* body= NULL;
7a6b60d0
 	int alloc_tuple= 0;
feaff680
 	str* tuple= NULL;
4b79a143
 
 	doc= xmlParseMemory(publ->body->s, publ->body->len );
 	if(doc== NULL)
 	{
85a70026
 		LM_ERR("while parsing xml memory\n");
4b79a143
 		goto error;
 	}
 
00f39965
 	node= xmlDocGetNodeByName(doc, "tuple", NULL);
4b79a143
 	if(node == NULL)
 	{
85a70026
 		LM_ERR("while extracting tuple node\n");
4b79a143
 		goto error;
 	}
25446861
 	tuple= *(tuple_param);
 
00f39965
 	tuple_id= xmlNodeGetAttrContentByName(node, "id");
4b79a143
 	if(tuple_id== NULL)
7a6b60d0
 	{
feaff680
 
7a6b60d0
 		if(tuple== NULL)	// generate a tuple_id
 		{
 			tuple_id= buf;
 			tuple_id_len= sprintf(tuple_id, "%p", publ);
 			tuple_id[tuple_id_len]= '\0'; 
 
 			tuple=(str*)pkg_malloc(sizeof(str));
 			if(tuple== NULL)
 			{
85a70026
 				LM_ERR("No more memory\n");
7a6b60d0
 				goto error;
 			}
 			tuple->s= (char*)pkg_malloc(tuple_id_len* sizeof(char));
 			if(tuple->s== NULL)
 			{
85a70026
 				LM_ERR("NO more memory\n");
7a6b60d0
 				goto error;
 			}
 			memcpy(tuple->s, tuple_id, tuple_id_len);
 			tuple->len= tuple_id_len;
feaff680
 
 			*tuple_param= tuple;
7a6b60d0
 			alloc_tuple= 1;
feaff680
 
85a70026
 			LM_DBG("allocated tuple_id\n\n");
7a6b60d0
 		}
 		else
 		{
 			tuple_id= buf;
 			tuple_id_len= tuple->len;
 			memcpy(tuple_id, tuple->s, tuple_id_len);
 			tuple_id[tuple_id_len]= '\0';
25446861
 		}
7a6b60d0
 		/* add tuple id */
4b79a143
 		if(!xmlNewProp(node, BAD_CAST "id", BAD_CAST tuple_id))
 		{
85a70026
 			LM_ERR("while extracting xml"
4b79a143
 						" node\n");
 			goto error;
 		}
 	}
 	else
 	{
7a6b60d0
 		if(tuple== NULL)
 		{
 			strcpy(buf, tuple_id);
 			xmlFree(tuple_id);
 			tuple_id= buf;
 			tuple_id_len= strlen(tuple_id);
 		
 			tuple=(str*)pkg_malloc(sizeof(str));
 			if(tuple== NULL)
 			{
85a70026
 				LM_ERR("No more memory\n");
7a6b60d0
 				goto error;
 			}
 			tuple->s= (char*)pkg_malloc(tuple_id_len* sizeof(char));
 			if(tuple->s== NULL)
 			{
85a70026
 				LM_ERR("NO more memory\n");
7a6b60d0
 				goto error;
 			}
 			memcpy(tuple->s, tuple_id, tuple_id_len);
 			tuple->len= tuple_id_len;
25446861
 			*tuple_param= tuple;
7a6b60d0
 			alloc_tuple= 1;
25446861
 		}
 	}
7a6b60d0
 
00f39965
 	node= xmlDocGetNodeByName(doc, "person", NULL);
4b79a143
 	if(node)
 	{
85a70026
 		LM_DBG("found person node\n");
00f39965
 		person_id= xmlNodeGetAttrContentByName(node, "id");
4b79a143
 		if(person_id== NULL)
 		{	
 			if(!xmlNewProp(node, BAD_CAST "id", BAD_CAST tuple_id))
 			{
85a70026
 				LM_ERR("while extracting xml"
4b79a143
 						" node\n");
 				goto error;
 			}
 		}
 		else
 		{
 			xmlFree(person_id);
 		}
 	}	
 	body= (str*)pkg_malloc(sizeof(str));
 	if(body== NULL)
 	{
85a70026
 		LM_ERR("NO more memory left\n");
4b79a143
 		goto error;
 	}
 	memset(body, 0, sizeof(str));
 	xmlDocDumpFormatMemory(doc,(xmlChar**)(void*)&body->s, &body->len, 1);	
 	if(body->s== NULL || body->len== 0)
 	{
85a70026
 		LM_ERR("while dumping xml format\n");
4b79a143
 		goto error;
 	}	
 	xmlFreeDoc(doc);
 	doc= NULL;
 	
 	*fin_body= body;
 	xmlMemoryDump();
 	xmlCleanupParser();
 	return 1;
 
 error:
 	if(doc)
 		xmlFreeDoc(doc);
 	if(body)
 		pkg_free(body);
7a6b60d0
 	if(tuple && alloc_tuple)
 	{
 		if(tuple->s)
 			pkg_free(tuple->s);
 		pkg_free(tuple);
 		tuple= NULL;
 	}
4b79a143
 	return -1;
 
 }	
 
feaff680
 int bla_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
4b79a143
 {
 	xmlNodePtr node= NULL;
 	xmlDocPtr doc= NULL;
 	char* version;
 	str* body= NULL;
 	int len;
 	str* init_body;
 
 	init_body= publ->body;
 
 	doc= xmlParseMemory(init_body->s, init_body->len );
 	if(doc== NULL)
 	{
85a70026
 		LM_ERR("while parsing xml memory\n");
4b79a143
 		goto error;
 	}
 	/* change version and state*/
00f39965
 	node= xmlDocGetNodeByName(doc, "dialog-info", NULL);
4b79a143
 	if(node == NULL)
 	{
85a70026
 		LM_ERR("while extracting dialog-info node\n");
4b79a143
 		goto error;
 	}
 	version= int2str(ver,&len);
 	version[len]= '\0';
 
 	if( xmlSetProp(node, (const xmlChar *)"version",(const xmlChar*)version)== NULL)
 	{
85a70026
 		LM_ERR("while setting version attribute\n");
4b79a143
 		goto error;	
 	}
 	body= (str*)pkg_malloc(sizeof(str));
 	if(body== NULL)
 	{
85a70026
 		LM_ERR("NO more memory left\n");
4b79a143
 		goto error;
 	}
 	memset(body, 0, sizeof(str));
 	xmlDocDumpFormatMemory(doc, (xmlChar**)(void*)&body->s, &body->len, 1);	
 
 	xmlFreeDoc(doc);
 	doc= NULL;
 	*fin_body= body;	
 	if(*fin_body== NULL)
85a70026
 		LM_DBG("NULL fin_body\n");
4b79a143
 
 	xmlMemoryDump();
 	xmlCleanupParser();
85a70026
 	LM_DBG("successful\n");
4b79a143
 	return 1;
 
 error:
 	if(doc)
 		xmlFreeDoc(doc);
 	if(body)
 		pkg_free(body);
 	
 	xmlMemoryDump();
 	xmlCleanupParser();
 	return -1;
 }
 
feaff680
 int mwi_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
4b79a143
 {
 	*fin_body= publ->body;
 	return 0;
 }
 
3d69afa8
 int dlg_process_body(publ_info_t* publ, str** fin_body, int ver, str** tuple)
 {
 	*fin_body= publ->body;
 	return 0;
 }