Index: modules/presence/notify.c
===================================================================
RCS file: /cvsroot/openser/sip-server/modules/presence/notify.c,v
retrieving revision 1.18
diff -u -r1.18 notify.c
--- modules/presence/notify.c	14 Feb 2007 13:21:02 -0000	1.18
+++ modules/presence/notify.c	19 Feb 2007 20:53:18 -0000
@@ -29,7 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-
+#include <assert.h>
 #include "../../trim.h"
 #include "../../ut.h"
 #include "../../globals.h"
@@ -49,20 +49,26 @@
 
 void p_tm_callback( struct cell *t, int type, struct tmcb_params *ps);
 
-void printf_subs(subs_t* subs)
+void printf_subs(subs_t* subs, const char *comment)
 {
-	LOG(L_INFO,"\n\t[p_user]= %.*s  [p_domain]= %.*s\n\t[w_user]= %.*s "  
-			"[w_domain]= %.*s\n",
-			subs->to_user.len, subs->to_user.s, subs->to_domain.len,
-			subs->to_domain.s,
-			subs->from_user.len, subs->from_user.s, subs->from_domain.len,
-			subs->from_domain.s);
-	LOG(L_INFO,"[event]= %.*s\n\t[staus]= %.*s\n\t[expires]= %d\n",
-			subs->event.len, subs->event.s,	subs->status.len, subs->status.s,
-			subs->expires );
-	LOG(L_INFO,"[to_tag]= %.*s\n\t[from_tag]= %.*s\n",
-			subs->to_tag.len, subs->to_tag.s,	subs->from_tag.len, subs->from_tag.s);
-
+	LOG(L_INFO,"subs:%s"
+		"\n\t[p_user]= %.*s [p_domain]= %.*s"
+		"\n\t[w_user]= %.*s [w_domain]= %.*s"
+		"\n\t[event]= %.*s [status]= %.*s"
+		"\n\t[expires]= %d"
+		"\n\t[to_tag]= %.*s [from_tag]= %.*s"
+		"\n\t[callid]= %.*s\n",
+		comment,
+		subs->to_user.len, subs->to_user.s,
+		subs->to_domain.len, subs->to_domain.s,
+		subs->from_user.len, subs->from_user.s,
+		subs->from_domain.len, subs->from_domain.s,
+		subs->event.len, subs->event.s,
+		subs->status.len, subs->status.s,
+		subs->expires,
+		subs->to_tag.len, subs->to_tag.s,
+		subs->from_tag.len, subs->from_tag.s,
+		subs->callid.len, subs->callid.s);
 }
 
 str* build_str_hdr(str event, str status, int expires_t, str reason)
@@ -158,6 +164,11 @@
 			strncpy(str_hdr->s+str_hdr->len,"application/pidf+xml" , 20);
 			str_hdr->len += 20;
 		}
+		else if (event.len ==MSGSUMMARY_LEN)
+		{
+			strncpy(str_hdr->s+str_hdr->len,"application/simple-message-summary" , 34);
+			str_hdr->len += 34;
+		}
 		else
 		{
 			strncpy(str_hdr->s+str_hdr->len,"application/watcherinfo+xml" , 27);
@@ -266,7 +277,7 @@
 	query_vals[n_query_cols].type = DB_STR;
 	query_vals[n_query_cols].nul = 0;
 	query_vals[n_query_cols].val.str_val.s = "presence";
-	query_vals[n_query_cols].val.str_val.len = strlen("presence");
+	query_vals[n_query_cols].val.str_val.len = PRES_LEN;
 	n_query_cols++;
 
 	result_cols[status_col=n_result_cols++] = "status" ;
@@ -427,6 +438,14 @@
 	query_vals[n_query_cols].val.str_val.len = user.len;
 	n_query_cols++;
 
+	query_cols[n_query_cols] = "event";
+	query_ops[n_query_cols] = OP_EQ;
+	query_vals[n_query_cols].type = DB_STR;
+	query_vals[n_query_cols].nul = 0;
+	query_vals[n_query_cols].val.str_val.s = "presence";
+	query_vals[n_query_cols].val.str_val.len = PRES_LEN;
+	n_query_cols++;
+
 	result_cols[body_col=n_result_cols++] = "body" ;
 	result_cols[expires_col=n_result_cols++] = "expires";
 	result_cols[etag_col=n_result_cols++] = "etag";
@@ -545,6 +564,117 @@
 	return NULL;
 }
 
+str* get_ms_notify_body(str user, str host, str* etag)
+{
+	db_key_t query_cols[4];
+	db_op_t  query_ops[4];
+	db_val_t query_vals[4];
+	db_key_t result_cols[4];
+	db_res_t *result = NULL;
+	int body_col, expires_col;
+	str* notify_body=NULL;	
+	db_row_t *row =NULL;	
+	db_val_t *row_vals=NULL;
+	int n_result_cols = 0;
+	int n_query_cols = 0;
+
+	if (user.len)
+		LOG(L_DBG,"PRESENCE:get_ms_notify_body: user:%.*s\n",user.len,user.s);
+	if (host.len)
+		LOG(L_DBG,"PRESENCE:get_ms_notify_body: host:%.*s\n",host.len,host.s);
+	if (etag && etag->len)
+		LOG(L_DBG,"PRESENCE:get_ms_notify_body: etag:%.*s\n",etag->len,etag->s);
+
+	query_cols[n_query_cols] = "domain";
+	query_ops[n_query_cols] = OP_EQ;
+	query_vals[n_query_cols].type = DB_STR;
+	query_vals[n_query_cols].nul = 0;
+	query_vals[n_query_cols].val.str_val.s = host.s;
+	query_vals[n_query_cols].val.str_val.len = host.len;
+	n_query_cols++;
+
+	query_cols[n_query_cols] = "username";
+	query_ops[n_query_cols] = OP_EQ;
+	query_vals[n_query_cols].type = DB_STR;
+	query_vals[n_query_cols].nul = 0;
+	query_vals[n_query_cols].val.str_val.s = user.s;
+	query_vals[n_query_cols].val.str_val.len = user.len;
+	n_query_cols++;
+
+	query_cols[n_query_cols] = "event";
+	query_ops[n_query_cols] = OP_EQ;
+	query_vals[n_query_cols].type = DB_STR;
+	query_vals[n_query_cols].nul = 0;
+	query_vals[n_query_cols].val.str_val.s = "message-summary";
+	query_vals[n_query_cols].val.str_val.len = MSGSUMMARY_LEN;
+	n_query_cols++;
+
+	if (etag && etag->s && etag->len) {
+		query_cols[n_query_cols] = "etag";
+		query_ops[n_query_cols] = OP_EQ;
+		query_vals[n_query_cols].type = DB_STR;
+		query_vals[n_query_cols].nul = 0;
+		query_vals[n_query_cols].val.str_val.s = etag->s;
+		query_vals[n_query_cols].val.str_val.len = etag->len;
+		n_query_cols++;
+	}
+
+	result_cols[body_col=n_result_cols++] = "body" ;
+	result_cols[expires_col=n_result_cols++] = "expires";
+
+	if (pa_dbf.use_table(pa_db, presentity_table) < 0) 
+	{
+		LOG(L_ERR, "PRESENCE:get_ms_notify_body: Error in use_table\n");
+		goto error;
+	}
+
+	LOG(L_INFO,"PRESENCE:get_ms_notify_body: querying presentity\n");
+	if (pa_dbf.query (pa_db, query_cols, query_ops, query_vals,
+		 result_cols, n_query_cols, n_result_cols, "received_time",  &result) < 0) 
+	{
+		LOG(L_ERR, "PRESENCE:get_ms_notify_body: Error while querying"
+				" presentity\n");
+		goto error;
+	}
+	
+	if (!result || result->n <=0 )
+	{
+		LOG(L_INFO,"PRESENCE:get_ms_notify_body: The query returned no"
+			" result\nThere is no presence information recorded for"
+			"the user\n");
+		goto error;
+	}
+
+	{
+		int jj;
+		for (jj=0;jj<result->n;jj++)
+		{
+			row = &result->rows[jj];
+			row_vals = ROW_VALUES(row);
+			LOG(
+				L_DBG,
+				"PRESENCE:get_ms_notify_body:body[%d/%d]:%.*s\n",jj,result->n-1,
+				row_vals[body_col].val.str_val.len,row_vals[body_col].val.str_val.s);
+		}
+	}
+	if(!(notify_body=pkg_malloc(sizeof (str))))
+		goto error;
+	notify_body->len=strlen(row_vals[body_col].val.str_val.s);
+	if(!(notify_body->s=pkg_malloc(notify_body->len)))
+	{
+		pkg_free (notify_body);
+		goto error;
+	}
+	memcpy(notify_body->s, row_vals[body_col].val.str_val.s, notify_body->len);
+	
+error:
+
+	if(result!=NULL)
+		pa_dbf.free_result(pa_db, result);
+	return notify_body;
+}
+
+
 static inline int shm_strdup(str* dst, str* src)
 {
 	dst->s = shm_malloc(src->len);
@@ -663,24 +793,24 @@
 }
 
 
-subs_t** get_subs_dialog(str* p_user, str* p_domain, char* event, int *n)
+subs_t** get_subs_dialog(str* p_user, str* p_domain, str* event, int *n)
 {
 
-	subs_t** subs_array= NULL;
-	subs_t* subs= NULL;
+	subs_t** subs_array = NULL;
+	subs_t* subs = NULL;
 	db_key_t query_cols[6];
 	db_op_t  query_ops[6];
 	db_val_t query_vals[6];
 	db_key_t result_cols[15];
 	int n_result_cols = 0, n_query_cols = 0;
-	db_row_t *row ;	
-	db_val_t *row_vals ;
+	db_row_t *row = NULL;
+	db_val_t *row_vals = NULL;
 	db_res_t *result = NULL;
 	int size= 0;
-	str from_user, from_domain, to_tag, from_tag;
-	str event_id, callid, record_route, contact, status;
-	int from_user_col, from_domain_col, to_tag_col, from_tag_col;
-	int expires_col= 0,callid_col, cseq_col, i, status_col =0, event_id_col = 0;
+	str from_user={0}, from_domain={0}, to_tag={0}, from_tag={0};
+	str callid={0}, record_route={0}, contact={0}, status={0}, event_id={0};
+	int from_user_col=0, from_domain_col=0, to_tag_col=0, from_tag_col=0, event_id_col=0;
+	int expires_col= 0,callid_col=0, cseq_col=0, i=0, status_col =0;
 	int version_col = 0, record_route_col = 0, contact_col = 0;
 
 	if (pa_dbf.use_table(pa_db, active_watchers_table) < 0) 
@@ -689,7 +819,10 @@
 		return NULL;
 	}
 
-	LOG(L_INFO,"PRESENCE:get_subs_dialog:querying database table = active_watchers\n");
+	LOG(
+		L_INFO,
+		"PRESENCE:get_subs_dialog:querying database table = active_watchers, event:'%.*s'\n",
+		event->len, event->s);
 	query_cols[n_query_cols] = "to_domain";
 	query_ops[n_query_cols] = OP_EQ;
 	query_vals[n_query_cols].type = DB_STR;
@@ -710,8 +843,8 @@
 	query_ops[n_query_cols] = OP_EQ;
 	query_vals[n_query_cols].type = DB_STR;
 	query_vals[n_query_cols].nul = 0;
-	query_vals[n_query_cols].val.str_val.s = event;
-	query_vals[n_query_cols].val.str_val.len = strlen(event);
+	query_vals[n_query_cols].val.str_val.s = event->s;
+	query_vals[n_query_cols].val.str_val.len = event->len;
 	n_query_cols++;
 
 	result_cols[from_user_col=n_result_cols++] = "from_user" ;
@@ -724,9 +857,9 @@
 	result_cols[record_route_col=n_result_cols++] = "record_route";
 	result_cols[contact_col=n_result_cols++] = "contact";
 	result_cols[expires_col=n_result_cols++] = "expires";
-	result_cols[status_col=n_result_cols++] = "status"; 
+	result_cols[status_col=n_result_cols++] = "status";
 	
-	if(strlen(event)== strlen( "presence.winfo"))
+	if(event->len == strlen( "presence.winfo"))
 	{
 		result_cols[version_col=n_result_cols++] = "version";
 	}
@@ -747,10 +880,10 @@
 
 	if (result->n <=0 )
 	{
-		LOG(L_ERR, "PRESENCE: get_subs_dialog:The query for subscribtion for"
-				" [user]= %.*s,[domain]= %.*s for [event]= %s returned no"
-				" result\n",p_user->len, p_user->s, p_domain->len, 
-				p_domain->s,event);
+		LOG(L_DBG, "PRESENCE: get_subs_dialog:The query for subscribtion for"
+			" [username]= %.*s,[domain]= %.*s for [event]= %.*s returned no"
+			" result\n",p_user->len, p_user->s, p_domain->len, 
+			p_domain->s,event->len, event->s);
 		pa_dbf.free_result(pa_db, result);
 		result = NULL;
 		return NULL;
@@ -776,9 +909,10 @@
 		from_domain.s= row_vals[from_domain_col].val.str_val.s;
 		from_domain.len= strlen(from_domain.s);
 		event_id.s=row_vals[event_id_col].val.str_val.s;
-		event_id.len= strlen(event_id.s);
 		if(event_id.s== NULL)
 			event_id.len = 0;
+		else
+			event_id.len= strlen(event_id.s);
 		to_tag.s= row_vals[to_tag_col].val.str_val.s;
 		to_tag.len= strlen(to_tag.s);
 		from_tag.s= row_vals[from_tag_col].val.str_val.s; 
@@ -806,7 +940,7 @@
 		}
 		
 		size= sizeof(subs_t)+ (p_user->len+ p_domain->len+ from_user.len+ 
-				from_domain.len+ event_id.len+ + strlen(event)+ to_tag.len+ 
+				from_domain.len+ event_id.len+ event->len + to_tag.len+ 
 				from_tag.len+ callid.len+ record_route.len+ contact.len)* sizeof(char);
 
 		DBG("PRESENCE: get_subs_dialog: size = %d\n\n", size);
@@ -837,10 +971,15 @@
 		size+= p_domain->len;
 		
 		subs->event.s= (char*)subs+ size;
-		memcpy(subs->event.s, event, strlen(event));
-		subs->event.len= strlen(event);
+		memcpy(subs->event.s, event->s, event->len);
+		subs->event.len= event->len;
 		size+= subs->event.len;
 
+		subs->event_id.s= (char*)subs+ size;
+		memcpy(subs->event_id.s, event_id.s, event_id.len);
+		subs->event_id.len = event_id.len;
+		size+= subs->event_id.len;
+
 		subs->from_user.s= (char*)subs+ size;
 		memcpy(subs->from_user.s, from_user.s, from_user.len);
 		subs->from_user.len = from_user.len;
@@ -866,14 +1005,6 @@
 		subs->callid.len = callid.len;
 		size+= callid.len;
 		
-		if(event_id.s && event_id.len)
-		{
-			subs->event_id.s= (char*)subs+ size;
-			memcpy(subs->event_id.s, event_id.s, event_id.len);
-			subs->event_id.len = event_id.len;
-			size+= event_id.len;
-		}
-
 		if(record_route.s && record_route.len)
 		{	
 			subs->record_route.s =(char*)subs+ size;
@@ -909,7 +1040,7 @@
 		subs->expires = row_vals[expires_col].val.int_val - 
 			(int)time(NULL);
 
-		if(strlen(event) == PWINFO_LEN)
+		if(event->len == PWINFO_LEN)
 			subs->version = row_vals[version_col].val.int_val;
 		
 		subs_array[i]= subs;
@@ -936,9 +1067,7 @@
 	
 }
 
-
-
-int query_db_notify(str* p_user, str* p_domain, char* event, 
+int query_db_notify(str* p_user, str* p_domain, str* event, 
 		subs_t* watcher_subs, str* etag )
 {
 	subs_t** subs_array = NULL;
@@ -948,12 +1077,12 @@
 	subs_array= get_subs_dialog(p_user, p_domain, event, &n);
 	if(subs_array == NULL)
 	{
-		LOG(L_ERR, "PRESENCE:query_db_notify: Could not get subs_dialog from"
+		LOG(L_DBG, "PRESENCE:query_db_notify: Could not get subs_dialog from"
 				" database\n");
 		goto error;
 	}
 	
-	if(PRES_LEN == strlen(event) )
+	if(PRES_LEN == event->len )
 	{
 		notify_body = get_p_notify_body(*p_user, *p_domain, etag);
 		if(notify_body == NULL)
@@ -962,14 +1091,25 @@
 					" notify_body\n");
 			/* goto error; */
 		}
-	}	
+	}
+	else if (MSGSUMMARY_LEN == event->len)
+	{
+		notify_body = get_ms_notify_body (*p_user, *p_domain, etag);
+		if(notify_body == NULL)
+		{
+			LOG(L_DBG, "PRESENCE:query_db_notify: Could not get the"
+					" notify_body for message summary\n");
+			/* goto error; */
+		}
+	}
 
 	for(i =0; i<n; i++)
 	{
 		if(notify(subs_array[i], watcher_subs, notify_body, 0)< 0 )
 		{
-			LOG(L_DBG, "PRESENCE:query_db_notify: Could not send notify for"
-					"%s\n", event);
+			LOG(L_DBG,
+				"PRESENCE:query_db_notify: Could not send notify for "
+				"%.*s\n", event->len, event->s);
 		}
 	}
 
@@ -984,8 +1124,12 @@
 	}
 	if(notify_body!=NULL)
 	{
-		if(notify_body->s)
-			free(notify_body->s);
+		if(notify_body->s) {
+			if (MSGSUMMARY_LEN == event->len)
+				pkg_free(notify_body->s);
+			else	 /* presence. and winfo alloc'd w/ xml -- ke :< */
+				free(notify_body->s);
+		}
 		pkg_free(notify_body);
 	}
 
@@ -1023,7 +1167,7 @@
 
 	if(xcap_tree == NULL)
 	{
-		LOG(L_ERR, "PRESENCE: is_watcher_allowed: The authorization document"
+		LOG(L_DBG, "PRESENCE: is_watcher_allowed: The authorization document"
 				" is NULL\n");
 		return NULL;
 	}
@@ -1286,14 +1430,13 @@
 
 	if(result && result->n<=0)
 	{
-		LOG(L_ERR, "PRESENCE:get_xcap_tree:The query in table xcap for"
+		LOG(L_DBG, "PRESENCE:get_xcap_tree:The query in table xcap for"
 				" [username]=%.*s , domain=%.*s returned no result\n",
 				user.len, user.s, domain.len, domain.s);
 		goto error;
 	}
-	LOG(L_ERR, "PRESENCE:get_xcap_tree:The query in table xcap for"
-			" [username]=%.*s , domain=%.*s returned result",	user.len,
-			user.s, domain.len, domain.s );
+	LOG(L_DBG, "PRESENCE:get_xcap_tree:The query in table xcap for"
+			" [username]=%.*s , domain=%.*s returned result",	user.len, user.s, domain.len, domain.s );
 
 	row = &result->rows[0];
 	row_vals = ROW_VALUES(row);
@@ -1342,14 +1485,14 @@
 
 
 	LOG(L_INFO, "PRESENCE:notify:dialog informations:\n");
-	printf_subs(subs);
+	printf_subs(subs, "notify-1");
 
 	if(force_null_body)
 		goto jump_over_body;
 
     /* getting the notify body */
 
-	if ( subs->event.len == PRES_LEN)
+	if ( subs->event.len == PRES_LEN || subs->event.len == MSGSUMMARY_LEN)
 	{	
 		xcap_tree = get_xcap_tree(subs->to_user, subs->to_domain);
 		if(xcap_tree == NULL)
@@ -1444,7 +1587,16 @@
 				notify_body = get_wi_notify_body(subs, watcher_subs );
 				if(notify_body == NULL)
 				{
-					DBG("PRESENCE:notify: Could not get the notify_body\n");
+					DBG("PRESENCE:notify: Could not get the notify_body for winfo\n");
+					goto error;
+				}
+			}
+			else if(MSGSUMMARY_LEN == subs->event.len)	
+			{	
+				notify_body = get_ms_notify_body(subs->to_user, subs->to_domain, NULL );
+				if(notify_body == NULL)
+				{
+					DBG("PRESENCE:notify: Could not get the notify_body for message-summary\n");
 					goto error;
 				}
 			}
@@ -1469,7 +1621,7 @@
 					if(notify_body == NULL)
 					{
 						LOG(L_DBG, "PRESENCE:notify: Could not get the"
-								" notify_body\n");
+								" notify_body for presence\n");
 						/* goto error; */
 					}
 				}	
@@ -1502,10 +1654,10 @@
 		}
 	}
 	else
-		final_body= notify_body;
+		final_body = NULL;
 
 jump_over_body:
-
+	
 	/* built extra headers */	
 	uandd_to_uri(subs->to_user, subs->to_domain, &p_uri);
 	
@@ -1519,15 +1671,14 @@
 			subs->from_domain.len, subs->from_domain.s,subs->event.len,
 			subs->event.s);
 
-	printf_subs(subs);
-	str_hdr = build_str_hdr( subs->event,subs->status, subs->expires,
+	printf_subs(subs,"notify-2");
+	str_hdr = build_str_hdr(subs->event,subs->status, subs->expires,
 			subs->reason );
 	if(str_hdr == NULL|| str_hdr->s== NULL|| str_hdr->len==0)
 	{
 		LOG(L_ERR, "PRESENCE:notify:ERROR while building headers \n");
 		goto error;
 	}	
-	LOG(L_INFO, "PRESENCE:notify: headers:%.*s\n ", str_hdr->len, str_hdr->s);
 
 	/* construct the dlg_t structure */
 	td = build_dlg_t(p_uri, subs);
@@ -1551,16 +1702,16 @@
 		goto error;	
 	}	
 
-	if(final_body != NULL)
+	if(final_body && final_body->len)
 		DBG("body :\n:%.*s\n", final_body->len, final_body->s);	
 			
 	result = tmb.t_request_within
-		(&met,						             
-		str_hdr,                               
-		final_body,                           
-		td,					                  
-		p_tm_callback,				        
-		(void*)cb_param);				
+		(&met, 
+		 str_hdr,
+		 final_body?final_body:notify_body,
+		 td,
+		 p_tm_callback, 
+		 (void*)cb_param);
 
 	if(result < 0)
 	{
@@ -1598,8 +1749,7 @@
 		update_vals[n_update_keys].type = DB_INT;
 		update_vals[n_update_keys].nul = 0;
 		update_vals[n_update_keys].val.int_val = subs->version +1;
-
-			n_update_keys++;
+		n_update_keys++;
 	}
 	if(pa_dbf.update(pa_db,db_keys, 0, db_vals, update_keys, update_vals, 1,
 			n_update_keys )<0 )
@@ -1615,8 +1765,11 @@
 			pkg_free(td->rem_uri.s);
 		free_tm_dlg(td);
 	}
-	if(str_hdr!=NULL)
+
+	if(str_hdr!=NULL) {
 		pkg_free(str_hdr);
+		str_hdr=0;
+	}
 
 	if(n_body == NULL)
 	{
@@ -1651,6 +1804,7 @@
 			pkg_free(td->rem_uri.s);
 		free_tm_dlg(td);
 	}
+
 	if(str_hdr!=NULL)
 		pkg_free(str_hdr);
 
@@ -1692,7 +1846,7 @@
 		return;
 	}
 	
-	LOG(L_DBG, "PRESENCE:p_tm_callback: completed with status %d [watcher_id:"
+	LOG(L_INFO, "PRESENCE:p_tm_callback: completed with status %d [watcher_id:"
 			"%p/%s]\n",ps->code, ps->param, ((c_back_param*)(*ps->param))->w_id);
 
 	if(ps->code >= 300)
@@ -1712,6 +1866,8 @@
 		db_vals[0].type = DB_STRING;
 		db_vals[0].nul = 0;
 		db_vals[0].val.string_val = ((c_back_param*)(*ps->param))->w_id ;
+		LOG(L_INFO,"PRESENCE: p_tm_callback: removiing entry for to_tag:%s\n",
+			((c_back_param*)(*ps->param))->w_id);
 
 		if (pa_dbf.delete(pa_db, db_keys, db_ops, db_vals, 1) < 0) 
 			LOG(L_ERR,"PRESENCE: p_tm_callback: ERROR cleaning expired"
@@ -1726,7 +1882,7 @@
 		{
 			if(notify( ((c_back_param*)(*ps->param))->wi_subs, NULL, NULL, 1)< 0)
 			{
-				LOG(L_ERR, "PRESENCE:update_subscribtion: Could not send"
+				LOG(L_DBG, "PRESENCE:update_subscribtion: Could not send"
 					" notify for presence\n");
 			}
 		}
@@ -1768,7 +1924,7 @@
 	size =  sizeof(c_back_param);
 	cb_param->w_id = (char*)cb_param + size;
 	strncpy(cb_param->w_id, to_tag.s ,to_tag.len ) ;
-		cb_param->w_id[to_tag.len] = '\0';	
+	cb_param->w_id[to_tag.len] = '\0';	
 	
 	if(!(subs&& subs->send_on_cback))
 	{
Index: modules/presence/notify.h
===================================================================
RCS file: /cvsroot/openser/sip-server/modules/presence/notify.h,v
retrieving revision 1.4
diff -u -r1.4 notify.h
--- modules/presence/notify.h	14 Feb 2007 13:21:02 -0000	1.4
+++ modules/presence/notify.h	19 Feb 2007 20:53:18 -0000
@@ -38,6 +38,7 @@
 //extern char wi_event[16] ;
 #define PRES_LEN 8
 #define PWINFO_LEN 14
+#define MSGSUMMARY_LEN 15
 
 #define PRES_RULES 1;
 #define RESOURCE_LIST 2;
@@ -62,7 +63,7 @@
 
 void PRINT_DLG(FILE* out, dlg_t* _d);
 
-void printf_subs(subs_t* subs);
+void printf_subs(subs_t* subs ,const char* comment);
 
 //str* build_str_hdr(str event, str status, int expires_t, str reason);
 
@@ -70,7 +71,7 @@
 
 dlg_t* build_dlg_t (str p_uri, subs_t* subs);
 
-int query_db_notify(str* p_user, str* p_domain, char* event, subs_t *subs, str* etag);
+int query_db_notify(str* p_user, str* p_domain, str* event, subs_t *subs, str* etag);
 
 int notify(subs_t* subs, subs_t* watcher_subs, str* n_body, int force_null_body);
 
Index: modules/presence/presentity.c
===================================================================
RCS file: /cvsroot/openser/sip-server/modules/presence/presentity.c,v
retrieving revision 1.5
diff -u -r1.5 presentity.c
--- modules/presence/presentity.c	13 Dec 2006 15:18:49 -0000	1.5
+++ modules/presence/presentity.c	19 Feb 2007 20:53:18 -0000
@@ -47,14 +47,16 @@
 extern db_con_t* pa_db;
 extern db_func_t pa_dbf;
 
-int new_presentity( str* domain,str* user,
-		int expires, int received_time, str* etag, presentity_t **p)
+int new_presentity(
+	str* domain,str* user, int expires, int received_time,
+	str* etag, str* event, presentity_t **p)
 {
 	presentity_t *presentity;
 	int size;
+	char *s=0;
 	
 	/* alocating memory for presentity */
-	size = sizeof(presentity_t)+ domain->len+ user->len+ etag->len + 1;
+	size = sizeof(presentity_t)+ domain->len+ user->len+ etag->len + event->len + 4;
 	presentity = (presentity_t*)pkg_malloc(size);
 	if(presentity == NULL)
 	{
@@ -63,22 +65,25 @@
 	}
 	memset(presentity, 0, sizeof(presentity_t));
 
-    
-	presentity->domain.s = ((char*)presentity)+sizeof(presentity_t);
-	strncpy(presentity->domain.s, domain->s, domain->len);
-	presentity->domain.s[domain->len] = 0;
-	presentity->domain.len = domain->len;
-	
-	presentity->user.s = ((char*)presentity)+sizeof(presentity_t)+domain->len;
-	strncpy(presentity->user.s, user->s, user->len);
-	presentity->user.s[user->len] = 0;
-	presentity->user.len = user->len;
-
-	presentity->etag.s = ((char*)presentity)+sizeof(presentity_t)
-							+domain->len+ user->len;
-	strncpy(presentity->etag.s, etag->s, etag->len);
-	presentity->etag.s[etag->len] = 0;
-	presentity->etag.len = etag->len;
+	s=presentity->domain.s = (char*) (presentity+1);
+	strncpy(s, domain->s, domain->len);
+	s+=presentity->domain.len=domain->len;
+	*s++=0;
+	
+	presentity->user.s = s;
+	strncpy(s, user->s, user->len);
+	s+=presentity->user.len=user->len;
+	*s++=0;
+	
+	presentity->etag.s = s;
+	strncpy(s, etag->s, etag->len);
+	s+=presentity->etag.len=etag->len;
+	*s++=0;
+
+	presentity->event.s = s;
+	strncpy(s,event->s, event->len);
+	s+=presentity->event.len=event->len;
+	*s++=0;
 
 	presentity->expires = expires;
 	presentity->received_time = received_time;
@@ -136,7 +141,7 @@
 	if(presentity->expires == 0) 
 	{
 		query_db_notify( &presentity->user, &presentity->domain, 
-					"presence", NULL, &presentity->etag);
+					&presentity->event, NULL, &presentity->etag);
 			
 		if (pa_dbf.use_table(pa_db, presentity_table) < 0) 
 		{
@@ -195,7 +200,7 @@
 			goto error;
 		}
 
-		LOG(L_INFO, "PRESENCE:update_presentity: inserting %d cols into"
+		LOG(L_INFO, "PRESENCE:update_presentity: inserting %d cols into "
 				"table\n",
 				n_query_cols);
 				
@@ -236,14 +241,16 @@
 			LOG(L_INFO,"PRESENCE:update_presentity:The presentity status" 
 					" is offline; do not send notify\n");
 		}
-		else			/* send notify with presence information */
+		else			   /* send notify with presence/m-s information */
+		{
 			if (query_db_notify(&presentity->user, &presentity->domain,
-						"presence", NULL, NULL)<0)
+								&presentity->event, NULL, NULL)<0)
 			{
 				LOG(L_INFO," PRESENCE:update_presentity:Could not send"
-						" notify for event presence\n");
-			//	goto error;
+					" notify for event presence\n");
+				//	goto error;
 			}
+		}
 		xmlFree(status);
 		xmlFreeDoc(doc);
 		xmlCleanupParser();
@@ -335,7 +342,7 @@
 
 			/* presentity body is updated so send notify to all watchers */
 			if (query_db_notify(&presentity->user, &presentity->domain,
-						"presence", NULL, NULL)<0)
+						&presentity->event, NULL, NULL)<0)
 			{
 				LOG(L_ERR," PRESENCE:update_presentity: Could not send Notify\n");
 		//		goto error;
@@ -364,6 +371,236 @@
 
 }
 
+int update_non_presentity(presentity_t* presentity, str* body, int new_t)
+{
+	str res_body;
+	
+	if( !use_db )
+		return 0;
+
+	db_key_t query_cols[7];
+	db_op_t  query_ops[7];
+	db_val_t query_vals[7], update_vals[3];
+	db_key_t result_cols[4], update_keys[3];
+	db_res_t *result= NULL;
+
+	int n_query_cols = 0;
+	int n_result_cols = 0;
+	int n_update_cols = 0;
+	int body_col;
+    
+	query_cols[0] = "domain";
+	query_ops[0] = OP_EQ;
+	query_vals[0].type = DB_STR;
+	query_vals[0].nul = 0;
+	query_vals[0].val.str_val.s = presentity->domain.s;
+	query_vals[0].val.str_val.len = presentity->domain.len;
+
+	n_query_cols++;
+	
+	query_cols[1] = "username";
+	query_ops[1] = OP_EQ;
+	query_vals[1].type = DB_STR;
+	query_vals[1].nul = 0;
+	query_vals[1].val.str_val.s = presentity->user.s;
+	query_vals[1].val.str_val.len = presentity->user.len;
+	n_query_cols++;
+
+	
+	query_cols[n_query_cols] = "etag";
+	query_ops[n_query_cols] = OP_EQ;
+	query_vals[n_query_cols].type = DB_STR;
+	query_vals[n_query_cols].nul = 0;
+	query_vals[n_query_cols].val.str_val.s = presentity->etag.s;
+	query_vals[n_query_cols].val.str_val.len = presentity->etag.len;
+	n_query_cols++;
+			
+
+	if(presentity->expires == 0) 
+	{
+		query_db_notify( &presentity->user, &presentity->domain, 
+					&presentity->event, NULL, &presentity->etag);
+			
+		if (pa_dbf.use_table(pa_db, presentity_table) < 0) 
+		{
+			LOG(L_ERR, "PRESENCE:update_non_presentity: Error in use_table\n");
+			goto error;
+		}
+		LOG(L_DBG,"PRESENCE:update_non_presentity: expires =0 -> deleting"
+				" from database\n");
+		if(pa_dbf.delete(pa_db, query_cols, 0 ,query_vals,n_query_cols)< 0 )
+		{
+			LOG(L_ERR, "PRESENCE:update_non_presentity: ERROR cleaning"
+					" unsubscribed messages\n");
+		}
+		DBG("PRESENCE:update_non_presentity:delete from db %.*s\n",
+				presentity->user.len,presentity->user.s );
+		return 1;
+	}
+
+	if(new_t) /* daca a fost generat un nou etag insereaza */
+	{
+		/* insert new record into database */	
+		query_cols[n_query_cols] = "event";
+		query_vals[n_query_cols].type = DB_STR;
+		query_vals[n_query_cols].nul = 0;
+		query_vals[n_query_cols].val.str_val.s = presentity->event.s;
+		query_vals[n_query_cols].val.str_val.len= presentity->event.len;
+		n_query_cols++;
+			
+		query_cols[n_query_cols] = "expires";
+		query_vals[n_query_cols].type = DB_INT;
+		query_vals[n_query_cols].nul = 0;
+		query_vals[n_query_cols].val.int_val = presentity->expires+
+				(int)time(NULL);
+		n_query_cols++;
+	
+		query_cols[n_query_cols] = "received_time";
+		query_vals[n_query_cols].type = DB_INT;
+		query_vals[n_query_cols].nul = 0;
+		query_vals[n_query_cols].val.int_val = presentity->received_time;
+		n_query_cols++;
+
+		query_cols[n_query_cols] = "body";
+		query_vals[n_query_cols].type = DB_BLOB;
+		query_vals[n_query_cols].nul = 0;
+		query_vals[n_query_cols].val.str_val.s = body->s;
+		query_vals[n_query_cols].val.str_val.len = body->len;
+		n_query_cols++;
+			
+		if (pa_dbf.use_table(pa_db, presentity_table) < 0) 
+		{
+			LOG(L_ERR, "PRESENCE:update_non_presentity: Error in use_table\n");
+			goto error;
+		}
+
+		LOG(L_DBG, "PRESENCE:update_non_presentity: inserting %d cols into "
+				"table\n",
+				n_query_cols);
+				
+		if (pa_dbf.insert(pa_db, query_cols, query_vals, n_query_cols) < 0) 
+		{
+			LOG(L_ERR, "PRESENCE:update_non_presentity: Error while"
+					" inserting new presentity\n");
+			goto error;
+		}
+	}
+	else
+	{
+		result_cols[body_col=n_result_cols++] = "body" ;
+		if (pa_dbf.use_table(pa_db, presentity_table) < 0) 
+		{
+			LOG(L_ERR, "PRESENCE:update_non_presentity: Error in use_table\n");
+			goto error;
+		}
+
+		LOG(L_DBG,"PRESENCE:update_non_presentity: querying presentity  \n");
+		if (pa_dbf.query (pa_db, query_cols, query_ops, query_vals,
+			 result_cols, 3, n_result_cols, 0, &result) < 0) 
+		{
+			LOG(L_ERR, "PRESENCE:update_non_presentity: Error while querying"
+					" presentity\n");
+			goto error;
+		}
+		if(result== NULL)
+			goto error;
+
+		if (result->n > 0)
+		{
+			if(body==NULL || body->s==NULL) /* if there is no body update expires value */
+			{
+				update_keys[0] = "expires";
+				update_vals[0].type = DB_INT;
+				update_vals[0].nul = 0;
+				update_vals[0].val.int_val = presentity->expires + (int)time(NULL);
+
+				update_keys[1] = "received_time";
+				update_vals[1].type = DB_INT;
+				update_vals[1].nul = 0;
+				update_vals[1].val.int_val = presentity->received_time;
+
+
+				if( pa_dbf.update( pa_db,query_cols, query_ops,
+				query_vals, update_keys, update_vals, n_query_cols,2 )<0) 
+				{
+					LOG( L_ERR , "PRESENCE:update_non_presentity:ERROR while"
+							" updating presence information\n");
+					goto error;
+				}
+				pa_dbf.free_result(pa_db, result);
+				return 0;
+			}
+
+			db_row_t *row = &result->rows[0];
+			db_val_t *row_vals = ROW_VALUES(row);
+		
+			res_body.s = row_vals[body_col].val.str_val.s;	
+			res_body.len = row_vals[body_col].val.str_val.len;
+				
+			/* write the new body*/
+			update_keys[0] = "body";
+			update_vals[0].type = DB_BLOB;
+			update_vals[0].nul = 0;
+			update_vals[0].val.str_val.s = body->s;
+			update_vals[0].val.str_val.len=body->len;
+			n_update_cols++;
+			
+			update_keys[n_update_cols] = "expires";
+			update_vals[n_update_cols].type = DB_INT;
+			update_vals[n_update_cols].nul = 0;
+			update_vals[n_update_cols].val.int_val = presentity->expires+ (int)time(NULL);
+			n_update_cols++;
+
+			update_keys[n_update_cols] = "received_time";
+			update_vals[n_update_cols].type = DB_INT;
+			update_vals[n_update_cols].nul = 0;
+			update_vals[n_update_cols].val.int_val = presentity->received_time;
+			n_update_cols++;
+
+
+			if( pa_dbf.update( pa_db,query_cols, query_ops, query_vals,
+			update_keys,update_vals, n_query_cols, n_update_cols )<0) 
+			{
+				LOG( L_ERR , "PRESENCE:update_non_presentity: ERROR while"
+						" updating presence information\n");
+				goto error;
+			}
+
+			pa_dbf.free_result(pa_db, result);
+			result= NULL;
+
+			/* presentity body is updated so send notify to all watchers */
+			if(	query_db_notify(
+					&presentity->user, &presentity->domain, 
+					&presentity->event, NULL, &presentity->etag)<0)
+			{
+				LOG(L_DBG," PRESENCE:update_non_presentity: Could not send Notify\n");
+		//		goto error;
+			}
+		}  
+		else  /* if there isn't no registration with those 3 values */
+		{
+			LOG(L_DBG, "PRESENCE:update_non_presentity: No E_Tag match\n");
+			if(result)
+				pa_dbf.free_result(pa_db, result);
+			return 412;	
+		}
+	
+	}
+	
+	return 0;
+
+error:
+	LOG(L_ERR, "PRESENCE:update_non_presentity: ERROR occured\n");
+	if(result)
+	{
+		pa_dbf.free_result(pa_db, result);
+		result= NULL;
+	}
+	return -1;
+
+}
+
 void free_presentity(presentity_t* presentity)
 {
 	pkg_free(presentity);
Index: modules/presence/presentity.h
===================================================================
RCS file: /cvsroot/openser/sip-server/modules/presence/presentity.h,v
retrieving revision 1.1
diff -u -r1.1 presentity.h
--- modules/presence/presentity.h	13 Oct 2006 12:21:23 -0000	1.1
+++ modules/presence/presentity.h	19 Feb 2007 20:53:18 -0000
@@ -45,17 +45,20 @@
 	int expires;
 	int received_time;
 	str etag;
+	str event;
 //	xmlDocPtr body;
 
 } presentity_t;
 
 /* create new presentity */
 int new_presentity(str* domain,str* user, int expires, int received_time, 
-		str* t_tag, presentity_t** p);
+				   str* t_tag, str *event, presentity_t** p);
 
 /* update presentity in database */
 int update_presentity(presentity_t* p, str* body, int t_new);
 
+/* update non-presence entity in db */
+int update_non_presentity(presentity_t* presentity, str* body, int new_t);
 /* free memory */
 void free_presentity(presentity_t* p);
 
Index: modules/presence/publish.c
===================================================================
RCS file: /cvsroot/openser/sip-server/modules/presence/publish.c,v
retrieving revision 1.11
diff -u -r1.11 publish.c
--- modules/presence/publish.c	5 Feb 2007 16:22:40 -0000	1.11
+++ modules/presence/publish.c	19 Feb 2007 20:53:19 -0000
@@ -76,9 +76,10 @@
 	if( size <0 )
 	{
 		LOG(L_ERR, "PRESENCE: generate_ETag: ERROR unsuccessfull sprintf\n ");
+		pkg_free (etag);
 		return NULL;
 	}
-	LOG(L_ERR, "PRESENCE: generate_ETag: etag= %.*s / %d\n ", size, etag, size);
+	LOG(L_DBG, "PRESENCE: generate_ETag: etag= %.*s / %d\n ", size, etag, size);
 	etag[size] = '\0';
 	return etag;
 }
@@ -88,14 +89,12 @@
 	db_key_t db_keys[5];
 	db_val_t db_vals[5];
 	db_op_t  db_ops[5] ;
-	db_key_t result_cols[3];
+	db_key_t result_cols[4];
 	db_res_t *result = NULL;
 	db_row_t *row ;	
 	db_val_t *row_vals ;
-	int i =0, size= 0;
+	int i =0;
 	presentity_t** p= NULL;
-	presentity_t* pres= NULL;
-	int user_len, domain_len, etag_len;
 	int n= 0;
 
 	if (pa_dbf.use_table(pa_db, presentity_table) < 0) 
@@ -116,9 +115,10 @@
 	result_cols[0] = "username";
 	result_cols[1] = "domain";
 	result_cols[2] = "etag";
+	result_cols[3] = "event";
 
 	if(pa_dbf.query(pa_db, db_keys, db_ops, db_vals, result_cols,
-						1, 3, 0, &result )< 0)
+						1, 4, 0, &result )< 0)
 	{
 		LOG(L_ERR,
 			"PRESENCE:msg_presentity_clean: ERROR while querying database"
@@ -150,39 +150,17 @@
 	for(i = 0; i< n; i++)
 	{	
 		row = &result->rows[i];
-		row_vals = ROW_VALUES(row);	
-		
-		user_len = strlen(row_vals[0].val.str_val.s);
-		domain_len = strlen(row_vals[1].val.str_val.s);
-		etag_len= strlen(row_vals[2].val.str_val.s);
-		
-		size= sizeof(presentity_t)+ user_len+ domain_len+ etag_len; 
-		pres= (presentity_t*)pkg_malloc(size);
-		if(pres== NULL)
+		row_vals = ROW_VALUES(row);
+
+		str _user={row_vals[0].val.str_val.s, strlen (row_vals[0].val.str_val.s)};
+		str _domain={row_vals[1].val.str_val.s, strlen (row_vals[1].val.str_val.s)};
+		str _etag={row_vals[2].val.str_val.s,strlen (row_vals[2].val.str_val.s)};
+		str _event={row_vals[3].val.str_val.s,strlen (row_vals[3].val.str_val.s)};
+		if (new_presentity (&_domain, &_user, 0, 0, &_etag, &_event, &p[i]))
 		{
 			LOG(L_ERR, "PRESENCE:msg_presentity_clean:  ERROR while allocating memory\n");
 			goto error;
-		}
-		memset(pres, 0, size);
-		size= sizeof(presentity_t);
-		
-		pres->user.s= (char*)pres+ size;	
-		memcpy(pres->user.s, row_vals[0].val.str_val.s, user_len);
-		pres->user.len= user_len;
-		size+= user_len;
-
-		pres->domain.s= (char*)pres+ size;
-		memcpy(pres->domain.s, row_vals[1].val.str_val.s, domain_len);
-		pres->domain.len= domain_len;
-		size+= domain_len;
-
-		pres->etag.s= (char*)pres+ size;
-		memcpy(pres->etag.s, row_vals[2].val.str_val.s, etag_len);
-		pres->etag.len= etag_len;
-		size+= etag_len;
-		
-		p[i]= pres;
-
+		}		
 	}
 
 	pa_dbf.free_result(pa_db, result);
@@ -190,11 +168,10 @@
 	
 	for(i= 0; i<n ; i++)
 	{
-
 		LOG(L_INFO, "PRESENCE:msg_presentity_clean:found expired publish"
 				" for [user]=%.*s  [domanin]=%.*s\n",p[i]->user.len,p[i]->user.s,
 				p[i]->domain.len, p[i]->domain.s);
-		query_db_notify( &p[i]->user, &p[i]->domain, "presence", NULL, &p[i]->etag);
+		query_db_notify( &p[i]->user, &p[i]->domain, &p[i]->event, NULL, &p[i]->etag);
 
 	}
 
@@ -212,7 +189,7 @@
 	for(i= 0; i< n; i++)
 	{
 		if(p[i])
-			pkg_free(p[i]);
+			free_presentity (p[i]);
 	}
 	pkg_free(p);
 
@@ -242,15 +219,15 @@
 int handle_publish(struct sip_msg* msg, char* str1, char* str2)
 {
 	struct sip_uri uri;
-	str body;
-	unsigned int idx;
+	str body={0};
+	int idx;
 	struct to_body *pto, TO;
 	int lexpire;
 	presentity_t* presentity = 0;
 	struct hdr_field* hdr;
 	int found= 0, etag_gen = 0, update_p = 0;
-	str etag;
-	str hdr_append, hdr_append2 ;
+	str etag={0};
+	str hdr_append={0}, hdr_append2={0};
 	int error_ret = -1; /* error return code */
 	xmlDocPtr doc= NULL;
 
@@ -264,11 +241,15 @@
 	memset(&body, 0, sizeof(str));
 	/* inspecting the Event header field */
 	if( (!msg->event ) ||(msg->event->body.len<=0) ||
-	( strncmp(msg->event->body.s, "presence",8 )!=0) )
+		((strncmp(msg->event->body.s, "presence",PRES_LEN )!=0) &&
+		 (strncmp(msg->event->body.s, "message-summary",MSGSUMMARY_LEN )!=0)))
 	{
-		LOG(L_ERR, "PRESENCE: handle_publish:Missing or unsupported event"
+		if (msg->event && msg->event->body.len>0)
+			LOG(L_ERR, "PRESENCE: handle_publish:Missing or unsupported event"
 				" header field value [%.*s]\n", msg->event->body.len,
 				msg->event->body.s);
+		else
+			LOG(L_ERR, "PRESENCE: handle_publish:Missing event header");
 
 		if (slb.reply(msg, 489, &pu_489_rpl) == -1)
 		{
@@ -309,7 +290,6 @@
 
 		etag.s = hdr->body.s;
 		etag.len = hdr->body.len;
-		etag.s[ etag.len] = '\0';
 		DBG("PRESENCE:handle_publish: existing etag  = %.*s \n", etag.len,
 				etag.s);
 	}
@@ -327,7 +307,8 @@
 		lexpire = ((exp_body_t*)msg->expires->parsed)->val;
 		DBG("PRESENCE: handle_publish: lexpire= %d\n", lexpire);
 
-	} else 
+	}
+	else 
 	{
 		DBG("PRESENCE: handle_publish: 'expires' not found; default=%d\n",
 				default_expires);
@@ -408,27 +389,30 @@
 			goto error;
 		}
 		/* content-length (if present) must be already parsed */
-
 		body.len = get_content_length( msg );
-		doc= xmlParseMemory( body.s , body.len );
-		if(doc== NULL)
-		{
-			LOG(L_ERR, "PRESENCE: handle_publish: Bad body format\n");
-			if( slb.reply( msg, 415, &pu_415_rpl)== -1)
+
+		if (msg->event->body.len != MSGSUMMARY_LEN){
+			doc= xmlParseMemory( body.s , body.len );
+			if(doc== NULL)
 			{
-				LOG(L_ERR,"PRESENCE: handle_publish: ERORR while sending"
+				LOG(L_ERR, "PRESENCE: handle_publish: Bad body format\n");
+				if( slb.reply( msg, 415, &pu_415_rpl)== -1)
+				{
+					LOG(L_ERR,"PRESENCE: handle_publish: ERORR while sending"
 						" reply\n");
+				}
+				error_ret = 0;
+				goto error;
 			}
-			error_ret = 0;
-			goto error;
+			xmlFreeDoc(doc); doc=NULL;
 		}
-		xmlFreeDoc(doc);
 	}	
 	
 	/* now we have all the necessary values */
 	/* fill in the filds of the structure */
-	if(new_presentity(&uri.host, &uri.user, lexpire ,(int)time(NULL), &etag,&presentity)
-			!=0)
+	if(new_presentity(
+		   &uri.host, &uri.user, lexpire ,(int)time(NULL),
+		   &etag, &msg->event->body, &presentity) !=0)
 	{
 		LOG(L_ERR,"PRESENCE: handle_publish: ERORR creating presentity\n");
 		goto error;
@@ -438,8 +422,12 @@
 
 	lock_set_get( set, idx );
 	/* querry the database and update or insert */
-	update_p= update_presentity(presentity, &body, etag_gen);
-	lock_set_release( set, idx );
+	if (MSGSUMMARY_LEN == msg->event->body.len)
+		update_p=update_non_presentity (
+			presentity, &body, etag_gen);
+	else
+		update_p= update_presentity(presentity, &body, etag_gen);
+    lock_set_release( set, idx );
 
 	if(update_p <0)
 	{
@@ -476,7 +464,6 @@
 			pkg_free(hdr_append.s);
 			goto error;
 		}
-		pkg_free(hdr_append.s);
 
 		hdr_append2.s = (char *)pkg_malloc( sizeof(char)*16+etag.len );
 		if(hdr_append2.s == NULL)
@@ -484,8 +471,7 @@
 			LOG(L_ERR,"PRESENCE:handle_publish:ERROR unable to add lump_rl\n");
 			goto error;
 		}
-		hdr_append2.s[0]='\0';
-		hdr_append2.len = sprintf(hdr_append2.s, "SIP-ETag: %s\r\n", etag.s);
+		hdr_append2.len = sprintf(hdr_append2.s, "SIP-ETag: %.*s\r\n", etag.len,etag.s);
 		if(hdr_append2.len < 0)
 		{
 			LOG(L_ERR, "PRESENCE:handle_publish:ERROR unsuccessful sprintf\n ");
@@ -493,7 +479,7 @@
 			goto error;
 		}
 		hdr_append2.s[hdr_append2.len]= '\0';
-		LOG(L_ERR, "PRESENCE: handle_publish: sip-etag: [%.*s/%d]\n",
+		LOG(L_DBG, "PRESENCE: handle_publish: sip-etag: [%.*s/%d]\n",
 				hdr_append2.len,hdr_append2.s,hdr_append2.len);
 		if (add_lump_rpl(msg, hdr_append2.s, hdr_append2.len, LUMP_RPL_HDR)==0 )
 		{
@@ -501,13 +487,14 @@
 			pkg_free(hdr_append2.s);
 			goto error;
 		}
-		pkg_free(hdr_append2.s);
 
 		if( slb.reply( msg, 200, &pu_200_rpl)== -1)
 		{
 			LOG(L_ERR,"PRESENCE: handle_publish: ERORR while sending reply\n");
 			goto error;
 		}
+		pkg_free(hdr_append.s);
+		pkg_free(hdr_append2.s);
 	}
 
 	if(update_p == 412)
Index: modules/presence/subscribe.c
===================================================================
RCS file: /cvsroot/openser/sip-server/modules/presence/subscribe.c,v
retrieving revision 1.16
diff -u -r1.16 subscribe.c
--- modules/presence/subscribe.c	14 Feb 2007 13:21:02 -0000	1.16
+++ modules/presence/subscribe.c	19 Feb 2007 20:53:19 -0000
@@ -116,14 +116,16 @@
 	}
 	hdr_append.len = sprintf(hdr_append.s, "Expires: %d\r\n", lexpire);
 	
-	strncpy(hdr_append.s+hdr_append.len ,"Contact: <", 10);
-	hdr_append.len += 10;
-	strncpy(hdr_append.s+hdr_append.len, server_address.s, server_address.len);
-	hdr_append.len += server_address.len;
-	strncpy(hdr_append.s+hdr_append.len, ">", 1);
-	hdr_append.len += 1;
-	strncpy(hdr_append.s+hdr_append.len, CRLF, CRLF_LEN);
-	hdr_append.len += CRLF_LEN;
+	if( msg->contact==NULL || msg->contact->body.s==NULL) {
+		strncpy(hdr_append.s+hdr_append.len ,"Contact: <", 10);
+		hdr_append.len += 10;
+		strncpy(hdr_append.s+hdr_append.len, server_address.s, server_address.len);
+		hdr_append.len += server_address.len;
+		strncpy(hdr_append.s+hdr_append.len, ">", 1);
+		hdr_append.len += 1;
+		strncpy(hdr_append.s+hdr_append.len, CRLF, CRLF_LEN);
+		hdr_append.len += CRLF_LEN;
+	}
 
 	hdr_append.s[hdr_append.len]= '\0';
 	
@@ -160,13 +162,15 @@
 	}
 	hdr_append.len = sprintf(hdr_append.s, "Expires: %d\r\n", lexpire);
 	
-	strncpy(hdr_append.s+hdr_append.len ,"Contact: <", 10);
-	hdr_append.len += 10;
-	strncpy(hdr_append.s+hdr_append.len, server_address.s, server_address.len);
-	hdr_append.len += server_address.len;
-	strncpy(hdr_append.s+hdr_append.len, ">", 1);
-	hdr_append.len += 1;
-	hdr_append.s[hdr_append.len]= '\0';
+	if( msg->contact==NULL || msg->contact->body.s==NULL) {
+		strncpy(hdr_append.s+hdr_append.len ,"Contact: <", 10);
+		hdr_append.len += 10;
+		strncpy(hdr_append.s+hdr_append.len, server_address.s, server_address.len);
+		hdr_append.len += server_address.len;
+		strncpy(hdr_append.s+hdr_append.len, ">", 1);
+		hdr_append.len += 1;
+		hdr_append.s[hdr_append.len]= '\0';
+	}
 
 	if (add_lump_rpl( msg, hdr_append.s, hdr_append.len, LUMP_RPL_HDR)==0 )
 	{
@@ -202,7 +206,7 @@
 	int version_col= 0, i ;
 
 	DBG("PRESENCE: update_subscribtion ...\n");
-	printf_subs(subs);	
+	printf_subs(subs,"update_subscribtion-1");	
 	
 	query_cols[n_query_cols] = "to_user";
 	query_ops[n_query_cols] = OP_EQ;
@@ -250,7 +254,7 @@
 	query_vals[n_query_cols].nul = 0;
 	if( subs->event_id.s != NULL)
 	{
-		query_vals[n_query_cols].val.str_val.s = subs->event_id.s;
+		query_vals[n_query_cols].val.str_val.s = subs->event_id.s;  
 		query_vals[n_query_cols].val.str_val.len = subs->event_id.len;
 	} else {
 		query_vals[n_query_cols].val.str_val.s = "";
@@ -294,7 +298,7 @@
 
 	if( to_tag_gen ==0) /*if a SUBSCRIBE within a dialog */
 	{
-		LOG(L_INFO,"PRESENCE:update_subscribtion: querying database  \n");
+		LOG(L_INFO,"PRESENCE:update_subscribtion: querying database (in-dialog)  \n");
 		if (pa_dbf.query (pa_db, query_cols, query_ops, query_vals,
 			 result_cols, n_query_cols, n_result_cols, 0,  &result) < 0) 
 		{
@@ -337,6 +341,7 @@
 			}
 			if(subs->event.len == strlen("presence"))
 			{	
+				str _pw={"presence.winfo",PWINFO_LEN};
 				if( send_202ok(msg, subs->expires, rtag) <0)
 				{
 					LOG(L_ERR, "PRESENCE:update_subscribtion:ERROR while"
@@ -344,10 +349,10 @@
 					goto error;
 				}
 		
-				if(query_db_notify(&subs->to_user,&subs->to_domain,"presence.winfo",
+				if(query_db_notify(&subs->to_user,&subs->to_domain,&_pw,
 							subs, NULL)< 0)
 				{
-					LOG(L_ERR, "PRESENCE:update_subscribtion:Could not send"
+					LOG(L_DBG, "PRESENCE:update_subscribtion:Could not send"
 							" notify for presence.winfo\n");
 				}
 			}	
@@ -360,6 +365,7 @@
 					goto error;
 				}
 			}
+				
 			return 1;
 		}
 
@@ -422,15 +428,15 @@
 			}
 
 
-			DBG("PRESENCE:update_subscribtion:Inserting into database:"		
-				"\nn_query_cols:%d\n",n_query_cols);
+			LOG(L_INFO,
+				"PRESENCE:update_subscribtion:Inserting into database (not in-dialog). n_query_cols:%d\n",n_query_cols);
 			for(i = 0;i< n_query_cols-2; i++)
 			{
 				if(query_vals[i].type==DB_STR)
-				DBG("[%d] = %s %.*s\n",i, query_cols[i], 
-					query_vals[i].val.str_val.len,query_vals[i].val.str_val.s );
+					LOG(L_INFO,"[%d] = %s %.*s\n",i, query_cols[i], 
+						query_vals[i].val.str_val.len,query_vals[i].val.str_val.s );
 				if(query_vals[i].type==DB_INT)
-					DBG("[%d] = %s %d\n",i, query_cols[i], 
+					LOG(L_INFO,"[%d] = %s %d\n",i, query_cols[i], 
 						query_vals[i].val.int_val);
 			}
 	
@@ -448,23 +454,23 @@
 		 
 	}
 
-/* reply_and_notify  */
-
-	if(subs->event.len == strlen("presence"))
-	{	
+	/* reply_and_notify  */
+	if(subs->event.len == PRES_LEN)
+	{
+		str _pw={"presence.winfo",PWINFO_LEN};
 		if( send_202ok(msg, subs->expires, rtag) <0)
 		{
 			LOG(L_ERR, "PRESENCE:update_subscribtion:ERROR while"
-					" sending 202 OK\n");
+				" sending 202 OK\n");
 			goto error;
 		}
-		
-		if(query_db_notify(&subs->to_user,&subs->to_domain,"presence.winfo",
-					subs, NULL)< 0)
+			
+		if(query_db_notify(&subs->to_user,&subs->to_domain,&_pw, subs, NULL)< 0)
 		{
-			LOG(L_ERR, "PRESENCE:update_subscribtion:Could not send"
-					" notify for presence.winfo\n");
+			LOG(L_DBG, "PRESENCE:update_subscribtion:Could not send"
+				" notify for presence.winfo\n");
 		}
+
 		if(subs->send_on_cback== 0)
 		{	
 			if(notify(subs, NULL, NULL, 0)< 0)
@@ -473,7 +479,24 @@
 					" notify for presence\n");
 			}
 		}
-			
+
+	}
+	else if(subs->event.len == MSGSUMMARY_LEN)
+	{	
+		if( send_202ok(msg, subs->expires, rtag) <0)
+		{
+			LOG(L_ERR, "PRESENCE:update_subscribtion:ERROR while"
+				" sending 202 OK\n");
+			goto error;
+		}
+		if(subs->send_on_cback== 0)
+		{	
+			if(notify(subs, NULL, NULL, 0)< 0)
+			{
+				LOG(L_DBG, "PRESENCE:update_subscribtion: Could not send"
+					" notify for message-summary\n");
+			}
+		}
 	}
 	else /* if a new subscribe for winfo */
 	{
@@ -489,12 +512,11 @@
 				" sending notify\n");
 		}
 	}
-	
 	return 0;
 	
 error:
 
-	LOG(L_ERR, "PRESENCE:update_presentity: ERROR occured\n");
+	LOG(L_ERR, "PRESENCE:update_subsription: ERROR occured\n");
 	return -1;
 
 }
@@ -526,6 +548,7 @@
 		return ;
 	}
 
+	LOG(L_INFO,"PRESENCE:msg_watchers_clean: ceaning pending subscriptions.\n");
 	if (pa_dbf.delete(pa_db, db_keys, db_ops, db_vals, 2) < 0) 
 		LOG(L_ERR,"PRESENCE:msg_watchers_clean: ERROR cleaning pending "
 				" subscriptions\n");
@@ -616,9 +639,8 @@
 		subs.from_domain.len = strlen(row_vals[from_domain_col].val.str_val.s);
 		
 		subs.event_id.s = row_vals[event_id_col].val.str_val.s;
-		if(subs.event_id.s)
-			subs.event_id.len = strlen(subs.event_id.s);
-			
+		subs.event_id.len = strlen(row_vals[event_id_col].val.str_val.s);
+		
 		subs.to_tag.s = row_vals[to_tag_col].val.str_val.s;
 		subs.to_tag.len = strlen(row_vals[to_tag_col].val.str_val.s);
 		
@@ -629,14 +651,13 @@
 		subs.callid.len = strlen(row_vals[callid_col].val.str_val.s);
 	
 		subs.contact.s = row_vals[contact_col].val.str_val.s;
-		if(subs.contact.s)
-			subs.contact.len = strlen(row_vals[contact_col].val.str_val.s);
+		subs.contact.len = strlen(row_vals[contact_col].val.str_val.s);
 
 		subs.cseq = row_vals[cseq_col].val.int_val;
 
 		subs.record_route.s = row_vals[record_route_col].val.str_val.s;
-		if(subs.record_route.s )
-			subs.record_route.len = strlen(subs.record_route.s);
+		subs.record_route.len =
+			strlen(row_vals[record_route_col].val.str_val.s);
 	
 		subs.expires = 0;
 	
@@ -660,6 +681,7 @@
 				" messages\n");
 
 	pa_dbf.free_result(pa_db, result);
+	LOG(L_INFO,"PRESENCE:msg_active_watchers_clean: done cleaning expired messages\n");
 	return;
 }
 
@@ -709,8 +731,9 @@
 
 	/* inspecting the Event header field */
 	if( (!msg->event ) ||(msg->event->body.len<=0) ||
-		((strncmp(msg->event->body.s, "presence",8 )!=0)&&
-		(strncmp(msg->event->body.s, "presence.winfo",14 )!=0)) )
+		((strncmp(msg->event->body.s, "presence",PRES_LEN )!=0)&&
+		 (strncmp(msg->event->body.s, "message-summary",MSGSUMMARY_LEN )!=0) &&
+		 (strncmp(msg->event->body.s, "presence.winfo",PWINFO_LEN )!=0)) )
 	{
 		LOG(L_ERR, "PRESENCE: handle_subscribe:Missing or unsupported event"
 				" header field value\n");
@@ -860,7 +883,7 @@
 	/*generate to_tag if it's not included in the message*/
 	if (pto->tag_value.s==NULL || pto->tag_value.len==0 )
 	{  
-		LOG(L_INFO,"PRESENCE:handle_subscribe: generating from_tag\n");
+		LOG(L_INFO,"PRESENCE:handle_subscribe: generating to_tag\n");
 		to_tag_gen = 1;
 		/*generate to_tag then insert it in avp*/
 		
@@ -1090,7 +1113,7 @@
 	
 	}
 
-	printf_subs(&subs);	
+	printf_subs(&subs,"handle_subscribe-1");	
 	if( update_subscribtion(msg, &subs, &rtag_value, to_tag_gen) <0 )
 	{	
 		LOG(L_ERR,"PRESENCE:handle_subscribe: ERROR while updating database\n");
