Index: ul_publish.c
===================================================================
--- ul_publish.c	(revision 7)
+++ ul_publish.c	(revision 71)
@@ -47,6 +47,10 @@
 #include "pua_usrloc.h"
 
 #define BUF_LEN   256
+#define REG_BY_PUBLISH
+
+#ifdef REG_BY_PUBLISH
+
 static char* device_buffer = NULL;
 static char* state_buffer = NULL;
 static char* content_buffer = NULL;
@@ -60,7 +64,7 @@
 	}
 	
 	if (source)
-	{
+	{	
 		*dst = malloc(len+1);
 		strncpy(*dst, source, len);
 		(*dst)[len] = 0;
@@ -67,8 +71,11 @@
 	}
 }
 
+#endif
+
 int pua_set_publish(struct sip_msg* msg , char* s1, char* s2)
 {
+#ifdef REG_BY_PUBLISH
 	const char* first_delimiter = NULL;
 	const char* second_delimiter = NULL;
 	// Prepare place to store data
@@ -95,17 +102,17 @@
 	}
 	if (strlen(s1) && (!device_buffer || !state_buffer || !content_buffer))
 	{
-		LM_ERR("Failed to parse PresenceInReg header\n");
+		LM_WARN("Failed to parse PresenceInReg header\n");
 		return 1;
 	}
 	
 	if (device_buffer)
-		LM_ERR("Device: %s\n", device_buffer);
+		LM_DBG("Device: %s\n", device_buffer);
 	if (state_buffer)
-		LM_ERR("State: %s\n", state_buffer);
+		LM_DBG("State: %s\n", state_buffer);
 	if (content_buffer)
-		LM_ERR("Content: %s\n", content_buffer);
-		
+		LM_DBG("Content: %s\n", content_buffer);
+#endif
 	pua_ul_publish= 1;
 	if(pua_ul_bmask!=0)
 		setbflag(0, pua_ul_bflag);
@@ -132,7 +139,11 @@
 	LM_DBG("expires= %d\n", p->expires);
 }	
 
-str* build_pidf(ucontact_t* c, int open, const char* tuple_id, const char* content)
+str* build_pidf(ucontact_t* c
+#ifdef REG_BY_PUBLISH
+	, int open, const char* tuple_id, const char* content
+#endif
+)
 {
 	xmlDocPtr  doc = NULL; 
 	xmlNodePtr root_node = NULL;
@@ -139,8 +150,9 @@
 	xmlNodePtr tuple_node = NULL;
 	xmlNodePtr status_node = NULL;
 	xmlNodePtr basic_node = NULL;
+#ifdef REG_BY_PUBLISH
 	xmlNodePtr note_node = NULL;
-	
+#endif
 	str *body= NULL;
 	str pres_uri= {NULL, 0};
 	char buf[BUF_LEN];
@@ -148,8 +160,12 @@
 
 	if(c->expires< (int)time(NULL))
 	{
-		LM_ERR("found expired \n\n");
+		LM_ERR("PUBLISH: found expired \n\n");
+#ifdef REG_BY_PUBLISH
+		open = 0;
+#else
 		return NULL;
+#endif
 	}
 
 	pres_uri.s = buf;
@@ -211,9 +227,11 @@
 
 	tuple_node =xmlNewChild(root_node, NULL, BAD_CAST "tuple", NULL) ;
 	
+#ifdef REG_BY_PUBLISH
 	// Override default tuple id
 	xmlNewProp(tuple_node, BAD_CAST "id", BAD_CAST tuple_id);
-	
+#endif
+
 	if( tuple_node ==NULL)
 	{
 		LM_ERR("while adding child\n");
@@ -227,14 +245,21 @@
 		goto error;
 	}
 	
+#ifdef REG_BY_PUBLISH
 	basic_node = xmlNewChild(status_node, NULL, BAD_CAST "basic",
 		open ? (BAD_CAST "open") : (BAD_CAST "close")) ;
-	
+#else
+	basic_node = xmlNewChild(status_node, NULL, BAD_CAST "basic",
+		BAD_CAST "open");
+#endif
+
 	if( basic_node ==NULL)
 	{
 		LM_ERR("while adding child\n");
 		goto error;
 	}
+	
+#ifdef REG_BY_PUBLISH
 	/* LM_ERR("Content is %s \n", content); */
 	
 	note_node = xmlNewChild(tuple_node, NULL, BAD_CAST "note", BAD_CAST content);
@@ -243,7 +268,8 @@
 		LM_ERR("while adding child\n");
 		goto error;
 	}
-	
+#endif
+
 	body = (str*)pkg_malloc(sizeof(str));
 	if(body == NULL)
 	{
@@ -287,11 +313,13 @@
 	content_type.s= "application/pidf+xml";
 	content_type.len= 20;
 
-/*	if(pua_ul_publish==0 && pua_ul_bmask==0)
+#ifndef REG_BY_PUBLISH
+	if(pua_ul_publish==0 && pua_ul_bmask==0)
 	{
 		LM_INFO("should not send ul publish\n");
 		return;
-	} */
+	}
+#endif
 	if(pua_ul_bmask!=0 && (c->cflags & pua_ul_bmask)==0)
 	{
 		LM_INFO("not marked for publish\n");
@@ -313,14 +341,25 @@
 			}
 		}
 	}
+#ifdef REG_BY_PUBLISH
 	int online = type & UL_CONTACT_INSERT || type & UL_CONTACT_UPDATE;
 	if (online && state_buffer)
 		online = strcmp(state_buffer, "closed") != 0;
 	
 	body= build_pidf(c, online, device_buffer ? device_buffer : "device", content_buffer ? content_buffer : "");
-	if(body == NULL || body->s == NULL)
+	if(online && (body == NULL || body->s == NULL))
 		goto error;
-	
+#else
+	if(type & UL_CONTACT_INSERT)
+	{
+		body= build_pidf(c);
+		if(body == NULL || body->s == NULL)
+			goto error;
+	}
+	else
+		body = NULL;
+#endif
+
 	uri.s = (char*)pkg_malloc(sizeof(char)*(c->aor->len+default_domain.len+6));
 	if(uri.s == NULL)
 		goto error;
@@ -381,12 +420,16 @@
 	publ->content_type.len= content_type.len;
 	size+= content_type.len;
 
-/*	if(type & UL_CONTACT_EXPIRE || type & UL_CONTACT_DELETE)
+
+	if(type & UL_CONTACT_EXPIRE || type & UL_CONTACT_DELETE)
 		publ->expires= 0;
 	else 
-		publ->expires= c->expires - (int)time(NULL); */
-	publ->expires = 900;
-	
+#ifndef REG_BY_PUBLISH
+		publ->expires= c->expires - (int)time(NULL); 
+#else
+		publ->expires = 900;
+#endif
+
 	if(type & UL_CONTACT_INSERT)
 		publ->flag|= INSERT_TYPE;
 	else
@@ -398,7 +441,11 @@
 	print_publ(publ);
 	if((error=pua_send_publish(publ))< 0)
 	{
+#ifdef REG_BY_PUBLISH
 		LM_ERR("while sending publish\n");
+#else
+		LM_ERR("while sending publish for ul event %d\n", type);
+#endif
 		if(type & UL_CONTACT_UPDATE && error == ERR_PUBLISH_NO_BODY) {
 			/* This error can occur if Kamailio was restarted/stopped and for any reason couldn't store a pua
 			 * entry in 'pua' DB table. It can also occur if 'pua' table is cleaned externally while Kamailio
