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