presence_body.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/presence.h>
  20. #include <pjsip-simple/errno.h>
  21. #include <pjsip/sip_msg.h>
  22. #include <pjsip/sip_transport.h>
  23. #include <pj/guid.h>
  24. #include <pj/log.h>
  25. #include <pj/os.h>
  26. #include <pj/pool.h>
  27. #include <pj/string.h>
  28. #define THIS_FILE "presence_body.c"
  29. static const pj_str_t STR_APPLICATION = { "application", 11 };
  30. static const pj_str_t STR_PIDF_XML = { "pidf+xml", 8 };
  31. static const pj_str_t STR_XPIDF_XML = { "xpidf+xml", 9 };
  32. /*
  33. * Function to print XML message body.
  34. */
  35. static int pres_print_body(struct pjsip_msg_body *msg_body,
  36. char *buf, pj_size_t size)
  37. {
  38. return pj_xml_print((const pj_xml_node*)msg_body->data, buf, size,
  39. PJ_TRUE);
  40. }
  41. /*
  42. * Function to clone XML document.
  43. */
  44. static void* xml_clone_data(pj_pool_t *pool, const void *data, unsigned len)
  45. {
  46. PJ_UNUSED_ARG(len);
  47. return pj_xml_clone( pool, (const pj_xml_node*) data);
  48. }
  49. /*
  50. * This is a utility function to create PIDF message body from PJSIP
  51. * presence status (pjsip_pres_status).
  52. */
  53. PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool,
  54. const pjsip_pres_status *status,
  55. const pj_str_t *entity,
  56. pjsip_msg_body **p_body )
  57. {
  58. pjpidf_pres *pidf;
  59. pjsip_msg_body *body;
  60. unsigned i;
  61. /* Create <presence>. */
  62. pidf = pjpidf_create(pool, entity);
  63. /* Create <tuple> */
  64. for (i=0; i<status->info_cnt; ++i) {
  65. pjpidf_tuple *pidf_tuple;
  66. pjpidf_status *pidf_status;
  67. pj_str_t id;
  68. /* Add tuple id. */
  69. if (status->info[i].id.slen == 0) {
  70. /* xs:ID must start with letter */
  71. //pj_create_unique_string(pool, &id);
  72. id.ptr = (char*)pj_pool_alloc(pool, pj_GUID_STRING_LENGTH()+2);
  73. id.ptr += 2;
  74. pj_generate_unique_string(&id);
  75. id.ptr -= 2;
  76. id.ptr[0] = 'p';
  77. id.ptr[1] = 'j';
  78. id.slen += 2;
  79. } else {
  80. id = status->info[i].id;
  81. }
  82. pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id);
  83. /* Set <contact> */
  84. if (status->info[i].contact.slen)
  85. pjpidf_tuple_set_contact(pool, pidf_tuple,
  86. &status->info[i].contact);
  87. /* Set basic status */
  88. pidf_status = pjpidf_tuple_get_status(pidf_tuple);
  89. pjpidf_status_set_basic_open(pidf_status,
  90. status->info[i].basic_open);
  91. /* Add <timestamp> if configured */
  92. #if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP
  93. if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) {
  94. char buf[50];
  95. int tslen = 0;
  96. pj_time_val tv;
  97. pj_parsed_time pt;
  98. pj_status_t s;
  99. pj_gettimeofday(&tv);
  100. /* Convert time to GMT (some platforms may not support it
  101. * such as WinCE).
  102. */
  103. s = pj_time_local_to_gmt(&tv);
  104. if (s != PJ_SUCCESS) {
  105. PJ_PERROR(4,(THIS_FILE,s,
  106. "Warning: failed to convert PIDF time to GMT"));
  107. }
  108. pj_time_decode( &tv, &pt);
  109. tslen = pj_ansi_snprintf(buf, sizeof(buf),
  110. "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  111. pt.year, pt.mon+1, pt.day,
  112. pt.hour, pt.min, pt.sec, pt.msec);
  113. if (tslen > 0 && tslen < (int)sizeof(buf)) {
  114. pj_str_t time = pj_str(buf);
  115. pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time);
  116. }
  117. }
  118. #endif
  119. }
  120. /* Create <person> (RPID) */
  121. if (status->info_cnt) {
  122. pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid);
  123. }
  124. body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
  125. body->data = pidf;
  126. body->content_type.type = STR_APPLICATION;
  127. body->content_type.subtype = STR_PIDF_XML;
  128. body->print_body = &pres_print_body;
  129. body->clone_data = &xml_clone_data;
  130. *p_body = body;
  131. return PJ_SUCCESS;
  132. }
  133. /*
  134. * This is a utility function to create X-PIDF message body from PJSIP
  135. * presence status (pjsip_pres_status).
  136. */
  137. PJ_DEF(pj_status_t) pjsip_pres_create_xpidf( pj_pool_t *pool,
  138. const pjsip_pres_status *status,
  139. const pj_str_t *entity,
  140. pjsip_msg_body **p_body )
  141. {
  142. /* Note: PJSIP implementation of XPIDF is not complete!
  143. */
  144. pjxpidf_pres *xpidf;
  145. pjsip_msg_body *body;
  146. PJ_LOG(4,(THIS_FILE, "Warning: XPIDF format is not fully supported "
  147. "by PJSIP"));
  148. /* Create XPIDF document. */
  149. xpidf = pjxpidf_create(pool, entity);
  150. /* Set basic status. */
  151. if (status->info_cnt > 0)
  152. pjxpidf_set_status( xpidf, status->info[0].basic_open);
  153. else
  154. pjxpidf_set_status( xpidf, PJ_FALSE);
  155. body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
  156. body->data = xpidf;
  157. body->content_type.type = STR_APPLICATION;
  158. body->content_type.subtype = STR_XPIDF_XML;
  159. body->print_body = &pres_print_body;
  160. body->clone_data = &xml_clone_data;
  161. *p_body = body;
  162. return PJ_SUCCESS;
  163. }
  164. /*
  165. * This is a utility function to parse PIDF body into PJSIP presence status.
  166. */
  167. PJ_DEF(pj_status_t) pjsip_pres_parse_pidf( pjsip_rx_data *rdata,
  168. pj_pool_t *pool,
  169. pjsip_pres_status *pres_status)
  170. {
  171. return pjsip_pres_parse_pidf2((char*)rdata->msg_info.msg->body->data,
  172. rdata->msg_info.msg->body->len,
  173. pool, pres_status);
  174. }
  175. PJ_DEF(pj_status_t) pjsip_pres_parse_pidf2(char *body, unsigned body_len,
  176. pj_pool_t *pool,
  177. pjsip_pres_status *pres_status)
  178. {
  179. pjpidf_pres *pidf;
  180. pjpidf_tuple *pidf_tuple;
  181. pidf = pjpidf_parse(pool, body, body_len);
  182. if (pidf == NULL)
  183. return PJSIP_SIMPLE_EBADPIDF;
  184. pres_status->info_cnt = 0;
  185. pidf_tuple = pjpidf_pres_get_first_tuple(pidf);
  186. while (pidf_tuple && pres_status->info_cnt < PJSIP_PRES_STATUS_MAX_INFO) {
  187. pjpidf_status *pidf_status;
  188. pres_status->info[pres_status->info_cnt].tuple_node =
  189. pj_xml_clone(pool, pidf_tuple);
  190. pj_strdup(pool,
  191. &pres_status->info[pres_status->info_cnt].id,
  192. pjpidf_tuple_get_id(pidf_tuple));
  193. pj_strdup(pool,
  194. &pres_status->info[pres_status->info_cnt].contact,
  195. pjpidf_tuple_get_contact(pidf_tuple));
  196. pidf_status = pjpidf_tuple_get_status(pidf_tuple);
  197. if (pidf_status) {
  198. pres_status->info[pres_status->info_cnt].basic_open =
  199. pjpidf_status_is_basic_open(pidf_status);
  200. } else {
  201. pres_status->info[pres_status->info_cnt].basic_open = PJ_FALSE;
  202. }
  203. pidf_tuple = pjpidf_pres_get_next_tuple( pidf, pidf_tuple );
  204. pres_status->info_cnt++;
  205. }
  206. /* Parse <person> (RPID) */
  207. pjrpid_get_element(pidf, pool, &pres_status->info[0].rpid);
  208. return PJ_SUCCESS;
  209. }
  210. /*
  211. * This is a utility function to parse X-PIDF body into PJSIP presence status.
  212. */
  213. PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf(pjsip_rx_data *rdata,
  214. pj_pool_t *pool,
  215. pjsip_pres_status *pres_status)
  216. {
  217. return pjsip_pres_parse_xpidf2((char*)rdata->msg_info.msg->body->data,
  218. rdata->msg_info.msg->body->len,
  219. pool, pres_status);
  220. }
  221. PJ_DEF(pj_status_t) pjsip_pres_parse_xpidf2(char *body, unsigned body_len,
  222. pj_pool_t *pool,
  223. pjsip_pres_status *pres_status)
  224. {
  225. pjxpidf_pres *xpidf;
  226. xpidf = pjxpidf_parse(pool, body, body_len);
  227. if (xpidf == NULL)
  228. return PJSIP_SIMPLE_EBADXPIDF;
  229. pres_status->info_cnt = 1;
  230. pj_strdup(pool,
  231. &pres_status->info[0].contact,
  232. pjxpidf_get_uri(xpidf));
  233. pres_status->info[0].basic_open = pjxpidf_get_status(xpidf);
  234. pres_status->info[0].id.slen = 0;
  235. pres_status->info[0].tuple_node = NULL;
  236. return PJ_SUCCESS;
  237. }