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