Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 21 | #include <pjsua-lib/pjsua_internal.h> |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 22 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 23 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 24 | #define THIS_FILE "pjsua_im.h" |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | /* Declare MESSAGE method */ |
| 28 | /* We put PJSIP_MESSAGE_METHOD as the enum here, so that when |
| 29 | * somebody add that method into pjsip_method_e in sip_msg.h we |
| 30 | * will get an error here. |
| 31 | */ |
| 32 | enum |
| 33 | { |
| 34 | PJSIP_MESSAGE_METHOD = PJSIP_OTHER_METHOD |
| 35 | }; |
| 36 | |
| 37 | const pjsip_method pjsip_message_method = |
| 38 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 39 | (pjsip_method_e) PJSIP_MESSAGE_METHOD, |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 40 | { "MESSAGE", 7 } |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | /* Proto */ |
| 45 | static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata); |
| 46 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 47 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 48 | /* The module instance. */ |
| 49 | static pjsip_module mod_pjsua_im = |
| 50 | { |
| 51 | NULL, NULL, /* prev, next. */ |
| 52 | { "mod-pjsua-im", 12 }, /* Name. */ |
| 53 | -1, /* Id */ |
| 54 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 55 | NULL, /* load() */ |
| 56 | NULL, /* start() */ |
| 57 | NULL, /* stop() */ |
| 58 | NULL, /* unload() */ |
| 59 | &im_on_rx_request, /* on_rx_request() */ |
| 60 | NULL, /* on_rx_response() */ |
| 61 | NULL, /* on_tx_request. */ |
| 62 | NULL, /* on_tx_response() */ |
| 63 | NULL, /* on_tsx_state() */ |
| 64 | |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | /* MIME constants. */ |
| 69 | static const pj_str_t STR_MIME_APP = { "application", 11 }; |
| 70 | static const pj_str_t STR_MIME_ISCOMPOSING = { "im-iscomposing+xml", 18 }; |
| 71 | static const pj_str_t STR_MIME_TEXT = { "text", 4 }; |
| 72 | static const pj_str_t STR_MIME_PLAIN = { "plain", 5 }; |
| 73 | |
| 74 | |
| 75 | /* Check if content type is acceptable */ |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 76 | #if 0 |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 77 | static pj_bool_t acceptable_message(const pjsip_media_type *mime) |
| 78 | { |
| 79 | return (pj_stricmp(&mime->type, &STR_MIME_TEXT)==0 && |
| 80 | pj_stricmp(&mime->subtype, &STR_MIME_PLAIN)==0) |
| 81 | || |
| 82 | (pj_stricmp(&mime->type, &STR_MIME_APP)==0 && |
| 83 | pj_stricmp(&mime->subtype, &STR_MIME_ISCOMPOSING)==0); |
| 84 | } |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 85 | #endif |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Create Accept header for MESSAGE. |
| 89 | */ |
| 90 | pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool) |
| 91 | { |
| 92 | /* Create Accept header. */ |
| 93 | pjsip_accept_hdr *accept; |
| 94 | |
| 95 | accept = pjsip_accept_hdr_create(pool); |
| 96 | accept->values[0] = pj_str("text/plain"); |
| 97 | accept->values[1] = pj_str("application/im-iscomposing+xml"); |
| 98 | accept->count = 2; |
| 99 | |
| 100 | return accept; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Private: check if we can accept the message. |
| 105 | */ |
| 106 | pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata, |
Benny Prijono | f9c668f | 2006-03-06 15:14:59 +0000 | [diff] [blame] | 107 | pjsip_accept_hdr **p_accept_hdr) |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 108 | { |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 109 | /* Some UA sends text/html, so this check will break */ |
| 110 | #if 0 |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 111 | pjsip_ctype_hdr *ctype; |
| 112 | pjsip_msg *msg; |
| 113 | |
| 114 | msg = rdata->msg_info.msg; |
| 115 | |
| 116 | /* Request MUST have message body, with Content-Type equal to |
| 117 | * "text/plain". |
| 118 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 119 | ctype = (pjsip_ctype_hdr*) |
| 120 | pjsip_msg_find_hdr(msg, PJSIP_H_CONTENT_TYPE, NULL); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 121 | if (msg->body == NULL || ctype == NULL || |
| 122 | !acceptable_message(&ctype->media)) |
| 123 | { |
| 124 | /* Create Accept header. */ |
| 125 | if (p_accept_hdr) |
| 126 | *p_accept_hdr = pjsua_im_create_accept(rdata->tp_info.pool); |
| 127 | |
| 128 | return PJ_FALSE; |
| 129 | } |
Benny Prijono | 81a99c3 | 2009-04-27 14:30:10 +0000 | [diff] [blame] | 130 | #elif 0 |
Benny Prijono | 0fe5acff | 2008-06-28 00:39:58 +0000 | [diff] [blame] | 131 | pjsip_msg *msg; |
| 132 | |
| 133 | msg = rdata->msg_info.msg; |
| 134 | if (msg->body == NULL) { |
| 135 | /* Create Accept header. */ |
| 136 | if (p_accept_hdr) |
| 137 | *p_accept_hdr = pjsua_im_create_accept(rdata->tp_info.pool); |
| 138 | |
| 139 | return PJ_FALSE; |
| 140 | } |
Benny Prijono | 81a99c3 | 2009-04-27 14:30:10 +0000 | [diff] [blame] | 141 | #else |
| 142 | /* Ticket #693: allow incoming MESSAGE without message body */ |
| 143 | PJ_UNUSED_ARG(rdata); |
| 144 | PJ_UNUSED_ARG(p_accept_hdr); |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 145 | #endif |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 146 | |
| 147 | return PJ_TRUE; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Private: process pager message. |
| 152 | * This may trigger pjsua_ui_on_pager() or pjsua_ui_on_typing(). |
| 153 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 154 | void pjsua_im_process_pager(int call_id, const pj_str_t *from, |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 155 | const pj_str_t *to, pjsip_rx_data *rdata) |
| 156 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 157 | pjsip_contact_hdr *contact_hdr; |
| 158 | pj_str_t contact; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 159 | pjsip_msg_body *body = rdata->msg_info.msg->body; |
| 160 | |
Benny Prijono | 81a99c3 | 2009-04-27 14:30:10 +0000 | [diff] [blame] | 161 | #if 0 |
| 162 | /* Ticket #693: allow incoming MESSAGE without message body */ |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 163 | /* Body MUST have been checked before */ |
| 164 | pj_assert(body != NULL); |
Benny Prijono | 81a99c3 | 2009-04-27 14:30:10 +0000 | [diff] [blame] | 165 | #endif |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 166 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 167 | |
| 168 | /* Build remote contact */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 169 | contact_hdr = (pjsip_contact_hdr*) |
| 170 | pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 171 | NULL); |
Benny Prijono | 8b33bba | 2010-06-02 03:03:43 +0000 | [diff] [blame] | 172 | if (contact_hdr && contact_hdr->uri) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 173 | contact.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool, |
| 174 | PJSIP_MAX_URL_SIZE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 175 | contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 176 | contact_hdr->uri, contact.ptr, |
| 177 | PJSIP_MAX_URL_SIZE); |
| 178 | } else { |
| 179 | contact.slen = 0; |
| 180 | } |
| 181 | |
Benny Prijono | 81a99c3 | 2009-04-27 14:30:10 +0000 | [diff] [blame] | 182 | if (body && pj_stricmp(&body->content_type.type, &STR_MIME_APP)==0 && |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 183 | pj_stricmp(&body->content_type.subtype, &STR_MIME_ISCOMPOSING)==0) |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 184 | { |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 185 | /* Expecting typing indication */ |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 186 | pj_status_t status; |
| 187 | pj_bool_t is_typing; |
| 188 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 189 | status = pjsip_iscomposing_parse(rdata->tp_info.pool, (char*)body->data, |
| 190 | body->len, &is_typing, NULL, NULL, |
| 191 | NULL ); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 192 | if (status != PJ_SUCCESS) { |
| 193 | pjsua_perror(THIS_FILE, "Invalid MESSAGE body", status); |
| 194 | return; |
| 195 | } |
| 196 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 197 | if (pjsua_var.ua_cfg.cb.on_typing) { |
| 198 | (*pjsua_var.ua_cfg.cb.on_typing)(call_id, from, to, &contact, |
| 199 | is_typing); |
| 200 | } |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 201 | |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 202 | if (pjsua_var.ua_cfg.cb.on_typing2) { |
| 203 | pjsua_acc_id acc_id; |
| 204 | |
| 205 | if (call_id == PJSUA_INVALID_ID) { |
| 206 | acc_id = pjsua_acc_find_for_incoming(rdata); |
| 207 | } else { |
| 208 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 209 | acc_id = call->acc_id; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | (*pjsua_var.ua_cfg.cb.on_typing2)(call_id, from, to, &contact, |
| 214 | is_typing, rdata, acc_id); |
| 215 | } |
| 216 | |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 217 | } else { |
| 218 | pj_str_t mime_type; |
| 219 | char buf[256]; |
| 220 | pjsip_media_type *m; |
| 221 | pj_str_t text_body; |
| 222 | |
| 223 | /* Save text body */ |
Benny Prijono | 81a99c3 | 2009-04-27 14:30:10 +0000 | [diff] [blame] | 224 | if (body) { |
| 225 | text_body.ptr = (char*)rdata->msg_info.msg->body->data; |
| 226 | text_body.slen = rdata->msg_info.msg->body->len; |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 227 | |
Benny Prijono | 81a99c3 | 2009-04-27 14:30:10 +0000 | [diff] [blame] | 228 | /* Get mime type */ |
| 229 | m = &rdata->msg_info.msg->body->content_type; |
| 230 | mime_type.ptr = buf; |
| 231 | mime_type.slen = pj_ansi_snprintf(buf, sizeof(buf), |
| 232 | "%.*s/%.*s", |
| 233 | (int)m->type.slen, |
| 234 | m->type.ptr, |
| 235 | (int)m->subtype.slen, |
| 236 | m->subtype.ptr); |
| 237 | if (mime_type.slen < 1) |
| 238 | mime_type.slen = 0; |
| 239 | |
| 240 | |
| 241 | } else { |
| 242 | text_body.ptr = mime_type.ptr = ""; |
| 243 | text_body.slen = mime_type.slen = 0; |
| 244 | } |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 245 | |
| 246 | if (pjsua_var.ua_cfg.cb.on_pager) { |
| 247 | (*pjsua_var.ua_cfg.cb.on_pager)(call_id, from, to, &contact, |
| 248 | &mime_type, &text_body); |
| 249 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 250 | |
Benny Prijono | bbeb399 | 2007-05-21 13:48:35 +0000 | [diff] [blame] | 251 | if (pjsua_var.ua_cfg.cb.on_pager2) { |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 252 | pjsua_acc_id acc_id; |
| 253 | |
| 254 | if (call_id == PJSUA_INVALID_ID) { |
| 255 | acc_id = pjsua_acc_find_for_incoming(rdata); |
| 256 | } else { |
| 257 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 258 | acc_id = call->acc_id; |
| 259 | } |
| 260 | |
Benny Prijono | bbeb399 | 2007-05-21 13:48:35 +0000 | [diff] [blame] | 261 | (*pjsua_var.ua_cfg.cb.on_pager2)(call_id, from, to, &contact, |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 262 | &mime_type, &text_body, rdata, |
| 263 | acc_id); |
Benny Prijono | bbeb399 | 2007-05-21 13:48:35 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | /* |
| 270 | * Handler to receive incoming MESSAGE |
| 271 | */ |
| 272 | static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata) |
| 273 | { |
| 274 | pj_str_t from, to; |
| 275 | pjsip_accept_hdr *accept_hdr; |
| 276 | pjsip_msg *msg; |
| 277 | pj_status_t status; |
| 278 | |
| 279 | msg = rdata->msg_info.msg; |
| 280 | |
| 281 | /* Only want to handle MESSAGE requests. */ |
| 282 | if (pjsip_method_cmp(&msg->line.req.method, &pjsip_message_method) != 0) { |
| 283 | return PJ_FALSE; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /* Should not have any transaction attached to rdata. */ |
| 288 | PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata)==NULL, PJ_FALSE); |
| 289 | |
| 290 | /* Should not have any dialog attached to rdata. */ |
| 291 | PJ_ASSERT_RETURN(pjsip_rdata_get_dlg(rdata)==NULL, PJ_FALSE); |
| 292 | |
| 293 | /* Check if we can accept the message. */ |
| 294 | if (!pjsua_im_accept_pager(rdata, &accept_hdr)) { |
| 295 | pjsip_hdr hdr_list; |
| 296 | |
| 297 | pj_list_init(&hdr_list); |
| 298 | pj_list_push_back(&hdr_list, accept_hdr); |
| 299 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 300 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 301 | PJSIP_SC_NOT_ACCEPTABLE_HERE, NULL, |
| 302 | &hdr_list, NULL); |
| 303 | return PJ_TRUE; |
| 304 | } |
| 305 | |
| 306 | /* Respond with 200 first, so that remote doesn't retransmit in case |
| 307 | * the UI takes too long to process the message. |
| 308 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 309 | status = pjsip_endpt_respond( pjsua_var.endpt, NULL, rdata, 200, NULL, |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 310 | NULL, NULL, NULL); |
| 311 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 312 | /* For the source URI, we use Contact header if present, since |
| 313 | * Contact header contains the port number information. If this is |
| 314 | * not available, then use From header. |
| 315 | */ |
Benny Prijono | 3850740 | 2007-05-15 11:15:58 +0000 | [diff] [blame] | 316 | from.ptr = (char*)pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | f4b538d | 2007-05-14 16:45:20 +0000 | [diff] [blame] | 317 | from.slen = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, |
| 318 | rdata->msg_info.from->uri, |
| 319 | from.ptr, PJSIP_MAX_URL_SIZE); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 320 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 321 | if (from.slen < 1) |
| 322 | from = pj_str("<--URI is too long-->"); |
| 323 | |
| 324 | /* Build the To text. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 325 | to.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 326 | to.slen = pjsip_uri_print( PJSIP_URI_IN_FROMTO_HDR, |
| 327 | rdata->msg_info.to->uri, |
| 328 | to.ptr, PJSIP_MAX_URL_SIZE); |
| 329 | if (to.slen < 1) |
| 330 | to = pj_str("<--URI is too long-->"); |
| 331 | |
| 332 | /* Process pager. */ |
| 333 | pjsua_im_process_pager(-1, &from, &to, rdata); |
| 334 | |
| 335 | /* Done. */ |
| 336 | return PJ_TRUE; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /* Outgoing IM callback. */ |
| 341 | static void im_callback(void *token, pjsip_event *e) |
| 342 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 343 | pjsua_im_data *im_data = (pjsua_im_data*) token; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 344 | |
| 345 | if (e->type == PJSIP_EVENT_TSX_STATE) { |
| 346 | |
| 347 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 348 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 349 | /* Ignore provisional response, if any */ |
| 350 | if (tsx->status_code < 200) |
| 351 | return; |
| 352 | |
| 353 | |
| 354 | /* Handle authentication challenges */ |
| 355 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG && |
| 356 | (tsx->status_code == 401 || tsx->status_code == 407)) |
| 357 | { |
| 358 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 359 | pjsip_tx_data *tdata; |
| 360 | pjsip_auth_clt_sess auth; |
| 361 | pj_status_t status; |
| 362 | |
| 363 | PJ_LOG(4,(THIS_FILE, "Resending IM with authentication")); |
| 364 | |
| 365 | /* Create temporary authentication session */ |
| 366 | pjsip_auth_clt_init(&auth,pjsua_var.endpt,rdata->tp_info.pool, 0); |
| 367 | |
| 368 | pjsip_auth_clt_set_credentials(&auth, |
| 369 | pjsua_var.acc[im_data->acc_id].cred_cnt, |
| 370 | pjsua_var.acc[im_data->acc_id].cred); |
| 371 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 372 | pjsip_auth_clt_set_prefs(&auth, |
| 373 | &pjsua_var.acc[im_data->acc_id].cfg.auth_pref); |
| 374 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 375 | status = pjsip_auth_clt_reinit_req(&auth, rdata, tsx->last_tx, |
| 376 | &tdata); |
| 377 | if (status == PJ_SUCCESS) { |
| 378 | pjsua_im_data *im_data2; |
| 379 | |
| 380 | /* Must duplicate im_data */ |
| 381 | im_data2 = pjsua_im_data_dup(tdata->pool, im_data); |
| 382 | |
Benny Prijono | ea4b439 | 2009-10-01 14:17:49 +0000 | [diff] [blame] | 383 | /* Increment CSeq */ |
| 384 | PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++; |
| 385 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 386 | /* Re-send request */ |
| 387 | status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1, |
| 388 | im_data2, &im_callback); |
| 389 | if (status == PJ_SUCCESS) { |
| 390 | /* Done */ |
| 391 | return; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 396 | if (tsx->status_code/100 == 2) { |
| 397 | PJ_LOG(4,(THIS_FILE, |
| 398 | "Message \'%s\' delivered successfully", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 399 | im_data->body.ptr)); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 400 | } else { |
| 401 | PJ_LOG(3,(THIS_FILE, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 402 | "Failed to deliver message \'%s\': %d/%.*s", |
| 403 | im_data->body.ptr, |
| 404 | tsx->status_code, |
| 405 | (int)tsx->status_text.slen, |
| 406 | tsx->status_text.ptr)); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 407 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 408 | |
Benny Prijono | 4da0b1d | 2007-06-11 18:22:54 +0000 | [diff] [blame] | 409 | if (pjsua_var.ua_cfg.cb.on_pager_status) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 410 | pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id, |
| 411 | &im_data->to, |
| 412 | &im_data->body, |
| 413 | im_data->user_data, |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 414 | (pjsip_status_code) |
| 415 | tsx->status_code, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 416 | &tsx->status_text); |
Benny Prijono | 4da0b1d | 2007-06-11 18:22:54 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (pjsua_var.ua_cfg.cb.on_pager_status2) { |
| 420 | pjsip_rx_data *rdata; |
| 421 | |
| 422 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 423 | rdata = e->body.tsx_state.src.rdata; |
| 424 | else |
| 425 | rdata = NULL; |
| 426 | |
| 427 | pjsua_var.ua_cfg.cb.on_pager_status2(im_data->call_id, |
| 428 | &im_data->to, |
| 429 | &im_data->body, |
| 430 | im_data->user_data, |
| 431 | (pjsip_status_code) |
| 432 | tsx->status_code, |
| 433 | &tsx->status_text, |
Benny Prijono | 0a98200 | 2007-06-12 16:22:09 +0000 | [diff] [blame] | 434 | tsx->last_tx, |
Benny Prijono | ba736c4 | 2008-07-10 20:45:03 +0000 | [diff] [blame] | 435 | rdata, im_data->acc_id); |
Benny Prijono | 4da0b1d | 2007-06-11 18:22:54 +0000 | [diff] [blame] | 436 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 441 | /* Outgoing typing indication callback. |
| 442 | * (used to reauthenticate request) |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 443 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 444 | static void typing_callback(void *token, pjsip_event *e) |
| 445 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 446 | pjsua_im_data *im_data = (pjsua_im_data*) token; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 447 | |
| 448 | if (e->type == PJSIP_EVENT_TSX_STATE) { |
| 449 | |
| 450 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 451 | |
| 452 | /* Ignore provisional response, if any */ |
| 453 | if (tsx->status_code < 200) |
| 454 | return; |
| 455 | |
| 456 | /* Handle authentication challenges */ |
| 457 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG && |
| 458 | (tsx->status_code == 401 || tsx->status_code == 407)) |
| 459 | { |
| 460 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 461 | pjsip_tx_data *tdata; |
| 462 | pjsip_auth_clt_sess auth; |
| 463 | pj_status_t status; |
| 464 | |
| 465 | PJ_LOG(4,(THIS_FILE, "Resending IM with authentication")); |
| 466 | |
| 467 | /* Create temporary authentication session */ |
| 468 | pjsip_auth_clt_init(&auth,pjsua_var.endpt,rdata->tp_info.pool, 0); |
| 469 | |
| 470 | pjsip_auth_clt_set_credentials(&auth, |
| 471 | pjsua_var.acc[im_data->acc_id].cred_cnt, |
| 472 | pjsua_var.acc[im_data->acc_id].cred); |
| 473 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 474 | pjsip_auth_clt_set_prefs(&auth, |
| 475 | &pjsua_var.acc[im_data->acc_id].cfg.auth_pref); |
| 476 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 477 | status = pjsip_auth_clt_reinit_req(&auth, rdata, tsx->last_tx, |
| 478 | &tdata); |
| 479 | if (status == PJ_SUCCESS) { |
| 480 | pjsua_im_data *im_data2; |
| 481 | |
| 482 | /* Must duplicate im_data */ |
| 483 | im_data2 = pjsua_im_data_dup(tdata->pool, im_data); |
| 484 | |
Benny Prijono | ea4b439 | 2009-10-01 14:17:49 +0000 | [diff] [blame] | 485 | /* Increment CSeq */ |
| 486 | PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++; |
| 487 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 488 | /* Re-send request */ |
| 489 | status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1, |
| 490 | im_data2, &typing_callback); |
| 491 | if (status == PJ_SUCCESS) { |
| 492 | /* Done */ |
| 493 | return; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | |
| 502 | /* |
| 503 | * Send instant messaging outside dialog, using the specified account for |
| 504 | * route set and authentication. |
| 505 | */ |
| 506 | PJ_DEF(pj_status_t) pjsua_im_send( pjsua_acc_id acc_id, |
| 507 | const pj_str_t *to, |
| 508 | const pj_str_t *mime_type, |
| 509 | const pj_str_t *content, |
| 510 | const pjsua_msg_data *msg_data, |
| 511 | void *user_data) |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 512 | { |
| 513 | pjsip_tx_data *tdata; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 514 | const pj_str_t mime_text_plain = pj_str("text/plain"); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 515 | const pj_str_t STR_CONTACT = { "Contact", 7 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 516 | pjsip_media_type media_type; |
| 517 | pjsua_im_data *im_data; |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 518 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 519 | pj_str_t contact; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 520 | pj_status_t status; |
| 521 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 522 | /* To and message body must be specified. */ |
| 523 | PJ_ASSERT_RETURN(to && content, PJ_EINVAL); |
| 524 | |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 525 | acc = &pjsua_var.acc[acc_id]; |
| 526 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 527 | /* Create request. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 528 | status = pjsip_endpt_create_request(pjsua_var.endpt, |
| 529 | &pjsip_message_method, to, |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 530 | &acc->cfg.id, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 531 | to, NULL, NULL, -1, NULL, &tdata); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 532 | if (status != PJ_SUCCESS) { |
| 533 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 534 | return status; |
| 535 | } |
| 536 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 537 | /* If account is locked to specific transport, then set transport to |
| 538 | * the request. |
| 539 | */ |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 540 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 541 | pjsip_tpselector tp_sel; |
| 542 | |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 543 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 544 | pjsip_tx_data_set_transport(tdata, &tp_sel); |
| 545 | } |
| 546 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 547 | /* Add accept header. */ |
| 548 | pjsip_msg_add_hdr( tdata->msg, |
| 549 | (pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 550 | |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 551 | /* Create suitable Contact header unless a Contact header has been |
| 552 | * set in the account. |
| 553 | */ |
| 554 | if (acc->contact.slen) { |
| 555 | contact = acc->contact; |
| 556 | } else { |
| 557 | status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to); |
| 558 | if (status != PJ_SUCCESS) { |
| 559 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 560 | pjsip_tx_data_dec_ref(tdata); |
| 561 | return status; |
| 562 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 565 | pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 566 | pjsip_generic_string_hdr_create(tdata->pool, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 567 | &STR_CONTACT, &contact)); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 568 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 569 | /* Create IM data to keep message details and give it back to |
| 570 | * application on the callback |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 571 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 572 | im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 573 | im_data->acc_id = acc_id; |
| 574 | im_data->call_id = PJSUA_INVALID_ID; |
| 575 | pj_strdup_with_null(tdata->pool, &im_data->to, to); |
| 576 | pj_strdup_with_null(tdata->pool, &im_data->body, content); |
| 577 | im_data->user_data = user_data; |
| 578 | |
| 579 | |
| 580 | /* Set default media type if none is specified */ |
| 581 | if (mime_type == NULL) { |
| 582 | mime_type = &mime_text_plain; |
| 583 | } |
| 584 | |
| 585 | /* Parse MIME type */ |
| 586 | pjsua_parse_media_type(tdata->pool, mime_type, &media_type); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 587 | |
| 588 | /* Add message body */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 589 | tdata->msg->body = pjsip_msg_body_create( tdata->pool, &media_type.type, |
| 590 | &media_type.subtype, |
| 591 | &im_data->body); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 592 | if (tdata->msg->body == NULL) { |
| 593 | pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM); |
| 594 | pjsip_tx_data_dec_ref(tdata); |
| 595 | return PJ_ENOMEM; |
| 596 | } |
| 597 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 598 | /* Add additional headers etc. */ |
| 599 | pjsua_process_msg_data(tdata, msg_data); |
| 600 | |
| 601 | /* Add route set */ |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 602 | pjsua_set_msg_route_set(tdata, &acc->route_set); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 603 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 604 | /* Send request (statefully) */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 605 | status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1, |
| 606 | im_data, &im_callback); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 607 | if (status != PJ_SUCCESS) { |
| 608 | pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 609 | return status; |
| 610 | } |
| 611 | |
| 612 | return PJ_SUCCESS; |
| 613 | } |
| 614 | |
| 615 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 616 | /* |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 617 | * Send typing indication outside dialog. |
| 618 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 619 | PJ_DEF(pj_status_t) pjsua_im_typing( pjsua_acc_id acc_id, |
| 620 | const pj_str_t *to, |
| 621 | pj_bool_t is_typing, |
| 622 | const pjsua_msg_data *msg_data) |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 623 | { |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 624 | const pj_str_t STR_CONTACT = { "Contact", 7 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 625 | pjsua_im_data *im_data; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 626 | pjsip_tx_data *tdata; |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 627 | pjsua_acc *acc; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 628 | pj_str_t contact; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 629 | pj_status_t status; |
| 630 | |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 631 | acc = &pjsua_var.acc[acc_id]; |
| 632 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 633 | /* Create request. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 634 | status = pjsip_endpt_create_request( pjsua_var.endpt, &pjsip_message_method, |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 635 | to, &acc->cfg.id, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 636 | to, NULL, NULL, -1, NULL, &tdata); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 637 | if (status != PJ_SUCCESS) { |
| 638 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 639 | return status; |
| 640 | } |
| 641 | |
| 642 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 643 | /* If account is locked to specific transport, then set transport to |
| 644 | * the request. |
| 645 | */ |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 646 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 647 | pjsip_tpselector tp_sel; |
| 648 | |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 649 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 650 | pjsip_tx_data_set_transport(tdata, &tp_sel); |
| 651 | } |
| 652 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 653 | /* Add accept header. */ |
| 654 | pjsip_msg_add_hdr( tdata->msg, |
| 655 | (pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 656 | |
| 657 | |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 658 | /* Create suitable Contact header unless a Contact header has been |
| 659 | * set in the account. |
| 660 | */ |
| 661 | if (acc->contact.slen) { |
| 662 | contact = acc->contact; |
| 663 | } else { |
| 664 | status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to); |
| 665 | if (status != PJ_SUCCESS) { |
| 666 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 667 | pjsip_tx_data_dec_ref(tdata); |
| 668 | return status; |
| 669 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 672 | pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 673 | pjsip_generic_string_hdr_create(tdata->pool, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 674 | &STR_CONTACT, &contact)); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 675 | |
| 676 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 677 | /* Create "application/im-iscomposing+xml" msg body. */ |
| 678 | tdata->msg->body = pjsip_iscomposing_create_body( tdata->pool, is_typing, |
| 679 | NULL, NULL, -1); |
| 680 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 681 | /* Add additional headers etc. */ |
| 682 | pjsua_process_msg_data(tdata, msg_data); |
| 683 | |
Benny Prijono | e1a10cb | 2007-04-04 10:45:44 +0000 | [diff] [blame] | 684 | /* Add route set */ |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 685 | pjsua_set_msg_route_set(tdata, &acc->route_set); |
Benny Prijono | e1a10cb | 2007-04-04 10:45:44 +0000 | [diff] [blame] | 686 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 687 | /* Create data to reauthenticate */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 688 | im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 689 | im_data->acc_id = acc_id; |
| 690 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 691 | /* Send request (statefully) */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 692 | status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1, |
| 693 | im_data, &typing_callback); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 694 | if (status != PJ_SUCCESS) { |
| 695 | pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 696 | return status; |
| 697 | } |
| 698 | |
| 699 | return PJ_SUCCESS; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | /* |
| 704 | * Init pjsua IM module. |
| 705 | */ |
| 706 | pj_status_t pjsua_im_init(void) |
| 707 | { |
| 708 | const pj_str_t msg_tag = { "MESSAGE", 7 }; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 709 | const pj_str_t STR_MIME_TEXT_PLAIN = { "text/plain", 10 }; |
| 710 | const pj_str_t STR_MIME_APP_ISCOMPOSING = |
| 711 | { "application/im-iscomposing+xml", 30 }; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 712 | pj_status_t status; |
| 713 | |
| 714 | /* Register module */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 715 | status = pjsip_endpt_register_module(pjsua_var.endpt, &mod_pjsua_im); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 716 | if (status != PJ_SUCCESS) |
| 717 | return status; |
| 718 | |
| 719 | /* Register support for MESSAGE method. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 720 | pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ALLOW, |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 721 | NULL, 1, &msg_tag); |
| 722 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 723 | /* Register support for "application/im-iscomposing+xml" content */ |
| 724 | pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT, |
| 725 | NULL, 1, &STR_MIME_APP_ISCOMPOSING); |
| 726 | |
| 727 | /* Register support for "text/plain" content */ |
| 728 | pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT, |
| 729 | NULL, 1, &STR_MIME_TEXT_PLAIN); |
| 730 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 731 | return PJ_SUCCESS; |
| 732 | } |
| 733 | |