Added IM and composition indication, and tested

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@268 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/include/pjsip-simple/errno.h b/pjsip/include/pjsip-simple/errno.h
index 966aed9..c3d4fe8 100644
--- a/pjsip/include/pjsip-simple/errno.h
+++ b/pjsip/include/pjsip-simple/errno.h
@@ -28,6 +28,9 @@
 #define PJSIP_SIMPLE_ERRNO_START  (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*2)
 
 
+/************************************************************
+ * EVENT PACKAGE ERRORS
+ ***********************************************************/
 /**
  * @hideinitializer
  * No event package with the specified name.
@@ -40,6 +43,9 @@
 #define PJSIP_SIMPLE_EPKGEXISTS	    (PJSIP_SIMPLE_ERRNO_START+2)    /*270002*/
 
 
+/************************************************************
+ * PRESENCE ERROR
+ ***********************************************************/
 /**
  * @hideinitializer
  * Expecting SUBSCRIBE request
@@ -72,6 +78,15 @@
 #define PJSIP_SIMPLE_EBADXPIDF	    (PJSIP_SIMPLE_ERRNO_START+25)   /*270025*/
 
 
+/************************************************************
+ * ISCOMPOSING ERRORS
+ ***********************************************************/
+/**
+ * @hideinitializer
+ * Bad isComposing XML message.
+ */
+#define PJSIP_SIMPLE_EBADISCOMPOSE  (PJSIP_SIMPLE_ERRNO_START+40)   /*270040*/
+
 
 #endif	/* __PJSIP_SIMPLE_ERRNO_H__ */
 
diff --git a/pjsip/include/pjsip-simple/iscomposing.h b/pjsip/include/pjsip-simple/iscomposing.h
new file mode 100644
index 0000000..b5544ea
--- /dev/null
+++ b/pjsip/include/pjsip-simple/iscomposing.h
@@ -0,0 +1,121 @@
+/* $Id$ */
+/* 
+ * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
+ *
+ * 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 
+ */
+#ifndef __PJSIP_SIMPLE_ISCOMPOSING_H__
+#define __PJSIP_SIMPLE_ISCOMPOSING_H__
+
+/**
+ * @file iscomposing.h
+ * @brief Support for Indication of Message Composition (RFC 3994)
+ */
+#include <pjsip-simple/types.h>
+#include <pjlib-util/xml.h>
+
+
+PJ_BEGIN_DECL
+
+
+/**
+ * Create XML message with MIME type "application/im-iscomposing+xml"
+ * to indicate the message composition status.
+ *
+ * @param pool		    Pool to allocate memory.
+ * @param is_composing	    Message composition indication status. Set to
+ *			    PJ_TRUE (or non-zero) to indicate that application
+ *			    is currently composing an instant message.
+ * @param lst_actv	    Optional attribute to indicate time of last
+ *			    activity. If none is to be specified, the value
+ *			    MUST be set to NULL.
+ * @param content_tp	    Optional attribute to indicate the content type of
+ *			    message being composed. If none is to be specified, 
+ *			    the value MUST be set to NULL.
+ * @param refresh	    Optional attribute to indicate the interval when
+ *			    next indication will be sent, only when 
+ *			    is_composing is non-zero. If none is to be 
+ *			    specified, the value MUST be set to -1.
+ *
+ * @return		    An XML message containing the message indication.
+ *			    NULL will be returned when there's not enough
+ *			    memory to allocate the message.
+ */
+PJ_DECL(pj_xml_node*) pjsip_iscomposing_create_xml(pj_pool_t *pool,
+						   pj_bool_t is_composing,
+						   const pj_time_val *lst_actv,
+						   const pj_str_t *content_tp,
+						   int refresh);
+
+
+/**
+ * Create message body with Content-Type "application/im-iscomposing+xml"
+ * to indicate the message composition status.
+ *
+ * @param pool		    Pool to allocate memory.
+ * @param is_composing	    Message composition indication status. Set to
+ *			    PJ_TRUE (or non-zero) to indicate that application
+ *			    is currently composing an instant message.
+ * @param lst_actv	    Optional attribute to indicate time of last
+ *			    activity. If none is to be specified, the value
+ *			    MUST be set to NULL.
+ * @param content_tp	    Optional attribute to indicate the content type of
+ *			    message being composed. If none is to be specified, 
+ *			    the value MUST be set to NULL.
+ * @param refresh	    Optional attribute to indicate the interval when
+ *			    next indication will be sent, only when 
+ *			    is_composing is non-zero. If none is to be 
+ *			    specified, the value MUST be set to -1.
+ *
+ * @return		    The SIP message body containing XML message 
+ *			    indication. NULL will be returned when there's not
+ *			    enough memory to allocate the message.
+ */
+PJ_DECL(pjsip_msg_body*) pjsip_iscomposing_create_body( pj_pool_t *pool,
+						   pj_bool_t is_composing,
+						   const pj_time_val *lst_actv,
+						   const pj_str_t *content_tp,
+						   int refresh);
+
+
+/**
+ * Parse the buffer and return message composition indication in the 
+ * message.
+ *
+ * @param pool		    Pool to allocate memory for the parsing process.
+ * @param msg		    The message to be parsed.
+ * @param len		    Length of the message.
+ * @param p_is_composing    Optional pointer to receive iscomposing status.
+ * @param p_last_active	    Optional pointer to receive last active attribute.
+ * @param p_content_type    Optional pointer to receive content type attribute.
+ * @param p_refresh	    Optional pointer to receive refresh time.
+ *
+ * @return		    PJ_SUCCESS if message can be successfully parsed.
+ */
+PJ_DECL(pj_status_t) pjsip_iscomposing_parse( pj_pool_t *pool,
+					      char *msg,
+					      pj_size_t len,
+					      pj_bool_t *p_is_composing,
+					      pj_str_t **p_last_active,
+					      pj_str_t **p_content_type,
+					      int *p_refresh );
+
+
+
+PJ_END_DECL
+
+
+#endif	/* __PJSIP_SIMPLE_ISCOMPOSING_H__ */
+
diff --git a/pjsip/include/pjsip_simple.h b/pjsip/include/pjsip_simple.h
index a8c955c..fd7c41a 100644
--- a/pjsip/include/pjsip_simple.h
+++ b/pjsip/include/pjsip_simple.h
@@ -34,6 +34,7 @@
 #define __PJSIP_SIMPLE_H__
 
 #include <pjsip-simple/evsub.h>
+#include <pjsip-simple/iscomposing.h>
 #include <pjsip-simple/presence.h>
 #include <pjsip-simple/pidf.h>
 #include <pjsip-simple/xpidf.h>
diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h
index b6d2fcd..d120a06 100644
--- a/pjsip/include/pjsua-lib/pjsua.h
+++ b/pjsip/include/pjsua-lib/pjsua.h
@@ -354,6 +354,17 @@
 
 
 /**
+ * Send instant messaging inside INVITE session.
+ */
+void pjsua_call_send_im(int call_index, const char *text);
+
+
+/**
+ * Send IM typing indication inside INVITE session.
+ */
+void pjsua_call_typing(int call_index, pj_bool_t is_typing);
+
+/**
  * Terminate all calls.
  */
 void pjsua_call_hangup_all(void);
@@ -405,6 +416,57 @@
 
 
 /*****************************************************************************
+ * PJSUA Instant Messaging (pjsua_im.c)
+ */
+
+/**
+ * The MESSAGE method (defined in pjsua_im.c)
+ */
+extern const pjsip_method pjsip_message_method;
+
+
+/**
+ * Init IM module handler to handle incoming MESSAGE outside dialog.
+ */
+pj_status_t pjsua_im_init();
+
+
+/**
+ * Create Accept header for MESSAGE.
+ */
+pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool);
+
+/**
+ * Send IM outside dialog.
+ */
+pj_status_t pjsua_im_send(int acc_index, const char *dst_uri, 
+			  const char *text);
+
+
+/**
+ * Send typing indication outside dialog.
+ */
+pj_status_t pjsua_im_typing(int acc_index, const char *dst_uri, 
+			    pj_bool_t is_typing);
+
+
+/**
+ * Private: check if we can accept the message.
+ *	    If not, then p_accept header will be filled with a valid
+ *	    Accept header.
+ */
+pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
+				const pjsip_accept_hdr **p_accept_hdr);
+
+/**
+ * Private: process pager message.
+ *	    This may trigger pjsua_ui_on_pager() or pjsua_ui_on_typing().
+ */
+void pjsua_im_process_pager(int call_id, const pj_str_t *from,
+			    const pj_str_t *to, pjsip_rx_data *rdata);
+
+
+/*****************************************************************************
  * User Interface API.
  *
  * The UI API specifies functions that will be called by pjsua upon
@@ -414,12 +476,27 @@
 /**
  * Notify UI when invite state has changed.
  */
-void pjsua_ui_inv_on_state_changed(int call_index, pjsip_event *e);
+void pjsua_ui_on_call_state(int call_index, pjsip_event *e);
 
 /**
  * Notify UI when registration status has changed.
  */
-void pjsua_ui_regc_on_state_changed(int acc_index);
+void pjsua_ui_on_reg_state(int acc_index);
+
+/**
+ * Notify UI on incoming pager (i.e. MESSAGE request).
+ * Argument call_index will be -1 if MESSAGE request is not related to an 
+ * existing call.
+ */
+void pjsua_ui_on_pager(int call_index, const pj_str_t *from,
+		       const pj_str_t *to, const pj_str_t *txt);
+
+
+/**
+ * Notify UI about typing indication.
+ */
+void pjsua_ui_on_typing(int call_index, const pj_str_t *from,
+		        const pj_str_t *to, pj_bool_t is_typing);
 
 
 /*****************************************************************************