iscomposing.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pjsip-simple/iscomposing.h>
  20. #include <pjsip-simple/errno.h>
  21. #include <pjsip/sip_msg.h>
  22. #include <pjlib-util/errno.h>
  23. #include <pj/pool.h>
  24. #include <pj/string.h>
  25. /* MIME */
  26. static const pj_str_t STR_MIME_TYPE = { "application", 11 };
  27. static const pj_str_t STR_MIME_SUBTYPE = { "im-iscomposing+xml", 18 };
  28. /* XML node constants. */
  29. static const pj_str_t STR_ISCOMPOSING = { "isComposing", 11 };
  30. static const pj_str_t STR_STATE = { "state", 5 };
  31. static const pj_str_t STR_ACTIVE = { "active", 6 };
  32. static const pj_str_t STR_IDLE = { "idle", 4 };
  33. static const pj_str_t STR_LASTACTIVE = { "lastactive", 10 };
  34. static const pj_str_t STR_CONTENTTYPE = { "contenttype", 11 };
  35. static const pj_str_t STR_REFRESH = { "refresh", 7 };
  36. /* XML attributes constants */
  37. static const pj_str_t STR_XMLNS_NAME = { "xmlns", 5 };
  38. static const pj_str_t STR_XMLNS_VAL = { "urn:ietf:params:xml:ns:im-iscomposing", 37 };
  39. static const pj_str_t STR_XMLNS_XSI_NAME = { "xmlns:xsi", 9 };
  40. static const pj_str_t STR_XMLNS_XSI_VAL = { "http://www.w3.org/2001/XMLSchema-instance", 41 };
  41. static const pj_str_t STR_XSI_SLOC_NAME = { "xsi:schemaLocation", 18 };
  42. static const pj_str_t STR_XSI_SLOC_VAL = { "urn:ietf:params:xml:ns:im-composing iscomposing.xsd", 51 };
  43. PJ_DEF(pj_xml_node*) pjsip_iscomposing_create_xml( pj_pool_t *pool,
  44. pj_bool_t is_composing,
  45. const pj_time_val *lst_actv,
  46. const pj_str_t *content_tp,
  47. int refresh)
  48. {
  49. pj_xml_node *doc, *node;
  50. pj_xml_attr *attr;
  51. /* Root document. */
  52. doc = pj_xml_node_new(pool, &STR_ISCOMPOSING);
  53. /* Add attributes */
  54. attr = pj_xml_attr_new(pool, &STR_XMLNS_NAME, &STR_XMLNS_VAL);
  55. pj_xml_add_attr(doc, attr);
  56. attr = pj_xml_attr_new(pool, &STR_XMLNS_XSI_NAME, &STR_XMLNS_XSI_VAL);
  57. pj_xml_add_attr(doc, attr);
  58. attr = pj_xml_attr_new(pool, &STR_XSI_SLOC_NAME, &STR_XSI_SLOC_VAL);
  59. pj_xml_add_attr(doc, attr);
  60. /* Add state. */
  61. node = pj_xml_node_new(pool, &STR_STATE);
  62. if (is_composing)
  63. node->content = STR_ACTIVE;
  64. else
  65. node->content = STR_IDLE;
  66. pj_xml_add_node(doc, node);
  67. /* Add lastactive, if any. */
  68. PJ_UNUSED_ARG(lst_actv);
  69. //if (!is_composing && lst_actv) {
  70. // PJ_TODO(IMPLEMENT_LAST_ACTIVE_ATTRIBUTE);
  71. //}
  72. /* Add contenttype, if any. */
  73. if (content_tp) {
  74. node = pj_xml_node_new(pool, &STR_CONTENTTYPE);
  75. pj_strdup(pool, &node->content, content_tp);
  76. pj_xml_add_node(doc, node);
  77. }
  78. /* Add refresh, if any. */
  79. if (is_composing && refresh > 1 && refresh < 3601) {
  80. node = pj_xml_node_new(pool, &STR_REFRESH);
  81. node->content.ptr = (char*) pj_pool_alloc(pool, 10);
  82. node->content.slen = pj_utoa(refresh, node->content.ptr);
  83. pj_xml_add_node(doc, node);
  84. }
  85. /* Done! */
  86. return doc;
  87. }
  88. /*
  89. * Function to print XML message body.
  90. */
  91. static int xml_print_body( struct pjsip_msg_body *msg_body,
  92. char *buf, pj_size_t size)
  93. {
  94. return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
  95. PJ_TRUE);
  96. }
  97. /*
  98. * Function to clone XML document.
  99. */
  100. static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
  101. {
  102. PJ_UNUSED_ARG(len);
  103. return pj_xml_clone( pool, (const pj_xml_node*)data);
  104. }
  105. PJ_DEF(pjsip_msg_body*) pjsip_iscomposing_create_body( pj_pool_t *pool,
  106. pj_bool_t is_composing,
  107. const pj_time_val *lst_actv,
  108. const pj_str_t *content_tp,
  109. int refresh)
  110. {
  111. pj_xml_node *doc;
  112. pjsip_msg_body *body;
  113. doc = pjsip_iscomposing_create_xml( pool, is_composing, lst_actv,
  114. content_tp, refresh);
  115. if (doc == NULL)
  116. return NULL;
  117. body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
  118. body->content_type.type = STR_MIME_TYPE;
  119. body->content_type.subtype = STR_MIME_SUBTYPE;
  120. body->data = doc;
  121. body->len = 0;
  122. body->print_body = &xml_print_body;
  123. body->clone_data = &xml_clone_data;
  124. return body;
  125. }
  126. PJ_DEF(pj_status_t) pjsip_iscomposing_parse( pj_pool_t *pool,
  127. char *msg,
  128. pj_size_t len,
  129. pj_bool_t *p_is_composing,
  130. pj_str_t **p_last_active,
  131. pj_str_t **p_content_type,
  132. int *p_refresh )
  133. {
  134. pj_xml_node *doc, *node;
  135. /* Set defaults: */
  136. if (p_is_composing) *p_is_composing = PJ_FALSE;
  137. if (p_last_active) *p_last_active = NULL;
  138. if (p_content_type) *p_content_type = NULL;
  139. /* Parse XML */
  140. doc = pj_xml_parse( pool, msg, len);
  141. if (!doc)
  142. return PJLIB_UTIL_EINXML;
  143. /* Root document must be "isComposing" */
  144. if (pj_stricmp(&doc->name, &STR_ISCOMPOSING) != 0)
  145. return PJSIP_SIMPLE_EBADISCOMPOSE;
  146. /* Get the status. */
  147. if (p_is_composing) {
  148. node = pj_xml_find_node(doc, &STR_STATE);
  149. if (node == NULL)
  150. return PJSIP_SIMPLE_EBADISCOMPOSE;
  151. *p_is_composing = (pj_stricmp(&node->content, &STR_ACTIVE)==0);
  152. }
  153. /* Get last active. */
  154. if (p_last_active) {
  155. node = pj_xml_find_node(doc, &STR_LASTACTIVE);
  156. if (node)
  157. *p_last_active = &node->content;
  158. }
  159. /* Get content type */
  160. if (p_content_type) {
  161. node = pj_xml_find_node(doc, &STR_CONTENTTYPE);
  162. if (node)
  163. *p_content_type = &node->content;
  164. }
  165. /* Get refresh */
  166. if (p_refresh) {
  167. node = pj_xml_find_node(doc, &STR_REFRESH);
  168. if (node)
  169. *p_refresh = pj_strtoul(&node->content);
  170. }
  171. return PJ_SUCCESS;
  172. }