blob: a1062c6fe6044bd98966ab22431766dba7180880 [file] [log] [blame]
Benny Prijonob0808372006-03-02 21:18:58 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonob0808372006-03-02 21:18:58 +00005 *
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 Prijonoeebe9af2006-06-13 22:57:13 +000021#include <pjsua-lib/pjsua_internal.h>
Benny Prijonob0808372006-03-02 21:18:58 +000022
Benny Prijonob0808372006-03-02 21:18:58 +000023
Benny Prijonoeebe9af2006-06-13 22:57:13 +000024#define THIS_FILE "pjsua_im.h"
Benny Prijonob0808372006-03-02 21:18:58 +000025
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 */
32enum
33{
34 PJSIP_MESSAGE_METHOD = PJSIP_OTHER_METHOD
35};
36
37const pjsip_method pjsip_message_method =
38{
Benny Prijonoba5926a2007-05-02 11:29:37 +000039 (pjsip_method_e) PJSIP_MESSAGE_METHOD,
Benny Prijonob0808372006-03-02 21:18:58 +000040 { "MESSAGE", 7 }
41};
42
43
44/* Proto */
45static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata);
46
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047
Benny Prijonob0808372006-03-02 21:18:58 +000048/* The module instance. */
49static 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. */
69static const pj_str_t STR_MIME_APP = { "application", 11 };
70static const pj_str_t STR_MIME_ISCOMPOSING = { "im-iscomposing+xml", 18 };
71static const pj_str_t STR_MIME_TEXT = { "text", 4 };
72static const pj_str_t STR_MIME_PLAIN = { "plain", 5 };
73
74
75/* Check if content type is acceptable */
Benny Prijonof4b538d2007-05-14 16:45:20 +000076#if 0
Benny Prijonob0808372006-03-02 21:18:58 +000077static 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 Prijonof4b538d2007-05-14 16:45:20 +000085#endif
Benny Prijonob0808372006-03-02 21:18:58 +000086
87/**
88 * Create Accept header for MESSAGE.
89 */
90pjsip_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 */
106pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
Benny Prijonof9c668f2006-03-06 15:14:59 +0000107 pjsip_accept_hdr **p_accept_hdr)
Benny Prijonob0808372006-03-02 21:18:58 +0000108{
Benny Prijonof4b538d2007-05-14 16:45:20 +0000109 /* Some UA sends text/html, so this check will break */
110#if 0
Benny Prijonob0808372006-03-02 21:18:58 +0000111 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 Prijonoa1e69682007-05-11 15:14:34 +0000119 ctype = (pjsip_ctype_hdr*)
120 pjsip_msg_find_hdr(msg, PJSIP_H_CONTENT_TYPE, NULL);
Benny Prijonob0808372006-03-02 21:18:58 +0000121 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 Prijono81a99c32009-04-27 14:30:10 +0000130#elif 0
Benny Prijono0fe5acff2008-06-28 00:39:58 +0000131 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 Prijono81a99c32009-04-27 14:30:10 +0000141#else
142 /* Ticket #693: allow incoming MESSAGE without message body */
143 PJ_UNUSED_ARG(rdata);
144 PJ_UNUSED_ARG(p_accept_hdr);
Benny Prijonof4b538d2007-05-14 16:45:20 +0000145#endif
Benny Prijonob0808372006-03-02 21:18:58 +0000146
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 Prijonoeebe9af2006-06-13 22:57:13 +0000154void pjsua_im_process_pager(int call_id, const pj_str_t *from,
Benny Prijonob0808372006-03-02 21:18:58 +0000155 const pj_str_t *to, pjsip_rx_data *rdata)
156{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000157 pjsip_contact_hdr *contact_hdr;
158 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000159 pjsip_msg_body *body = rdata->msg_info.msg->body;
160
Benny Prijono81a99c32009-04-27 14:30:10 +0000161#if 0
162 /* Ticket #693: allow incoming MESSAGE without message body */
Benny Prijonob0808372006-03-02 21:18:58 +0000163 /* Body MUST have been checked before */
164 pj_assert(body != NULL);
Benny Prijono81a99c32009-04-27 14:30:10 +0000165#endif
Benny Prijonob0808372006-03-02 21:18:58 +0000166
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000167
168 /* Build remote contact */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000169 contact_hdr = (pjsip_contact_hdr*)
170 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000171 NULL);
Benny Prijono8b33bba2010-06-02 03:03:43 +0000172 if (contact_hdr && contact_hdr->uri) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000173 contact.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool,
174 PJSIP_MAX_URL_SIZE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000175 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 Prijono81a99c32009-04-27 14:30:10 +0000182 if (body && pj_stricmp(&body->content_type.type, &STR_MIME_APP)==0 &&
Benny Prijonof4b538d2007-05-14 16:45:20 +0000183 pj_stricmp(&body->content_type.subtype, &STR_MIME_ISCOMPOSING)==0)
Benny Prijonob0808372006-03-02 21:18:58 +0000184 {
Benny Prijonob0808372006-03-02 21:18:58 +0000185 /* Expecting typing indication */
Benny Prijonob0808372006-03-02 21:18:58 +0000186 pj_status_t status;
187 pj_bool_t is_typing;
188
Benny Prijonoa1e69682007-05-11 15:14:34 +0000189 status = pjsip_iscomposing_parse(rdata->tp_info.pool, (char*)body->data,
190 body->len, &is_typing, NULL, NULL,
191 NULL );
Benny Prijonob0808372006-03-02 21:18:58 +0000192 if (status != PJ_SUCCESS) {
193 pjsua_perror(THIS_FILE, "Invalid MESSAGE body", status);
194 return;
195 }
196
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000197 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 Prijonof4b538d2007-05-14 16:45:20 +0000201
Benny Prijonoba736c42008-07-10 20:45:03 +0000202 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 Prijonof4b538d2007-05-14 16:45:20 +0000217 } 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 Prijono81a99c32009-04-27 14:30:10 +0000224 if (body) {
225 text_body.ptr = (char*)rdata->msg_info.msg->body->data;
226 text_body.slen = rdata->msg_info.msg->body->len;
Benny Prijonof4b538d2007-05-14 16:45:20 +0000227
Benny Prijono81a99c32009-04-27 14:30:10 +0000228 /* 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 Prijonof4b538d2007-05-14 16:45:20 +0000245
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 Prijonob0808372006-03-02 21:18:58 +0000250
Benny Prijonobbeb3992007-05-21 13:48:35 +0000251 if (pjsua_var.ua_cfg.cb.on_pager2) {
Benny Prijonoba736c42008-07-10 20:45:03 +0000252 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 Prijonobbeb3992007-05-21 13:48:35 +0000261 (*pjsua_var.ua_cfg.cb.on_pager2)(call_id, from, to, &contact,
Benny Prijonoba736c42008-07-10 20:45:03 +0000262 &mime_type, &text_body, rdata,
263 acc_id);
Benny Prijonobbeb3992007-05-21 13:48:35 +0000264 }
265 }
Benny Prijonob0808372006-03-02 21:18:58 +0000266}
267
268
269/*
270 * Handler to receive incoming MESSAGE
271 */
272static 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 Prijonoeebe9af2006-06-13 22:57:13 +0000300 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
Benny Prijonob0808372006-03-02 21:18:58 +0000301 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 Prijonoeebe9af2006-06-13 22:57:13 +0000309 status = pjsip_endpt_respond( pjsua_var.endpt, NULL, rdata, 200, NULL,
Benny Prijonob0808372006-03-02 21:18:58 +0000310 NULL, NULL, NULL);
311
Benny Prijono8b1889b2006-06-06 18:40:40 +0000312 /* 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 Prijono38507402007-05-15 11:15:58 +0000316 from.ptr = (char*)pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE);
Benny Prijonof4b538d2007-05-14 16:45:20 +0000317 from.slen = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR,
318 rdata->msg_info.from->uri,
319 from.ptr, PJSIP_MAX_URL_SIZE);
Benny Prijono8b1889b2006-06-06 18:40:40 +0000320
Benny Prijonob0808372006-03-02 21:18:58 +0000321 if (from.slen < 1)
322 from = pj_str("<--URI is too long-->");
323
324 /* Build the To text. */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000325 to.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE);
Benny Prijonob0808372006-03-02 21:18:58 +0000326 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. */
341static void im_callback(void *token, pjsip_event *e)
342{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000343 pjsua_im_data *im_data = (pjsua_im_data*) token;
Benny Prijonob0808372006-03-02 21:18:58 +0000344
345 if (e->type == PJSIP_EVENT_TSX_STATE) {
346
347 pjsip_transaction *tsx = e->body.tsx_state.tsx;
348
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000349 /* 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 Prijono48ab2b72007-11-08 09:24:30 +0000372 pjsip_auth_clt_set_prefs(&auth,
373 &pjsua_var.acc[im_data->acc_id].cfg.auth_pref);
374
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000375 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 Prijonoea4b4392009-10-01 14:17:49 +0000383 /* Increment CSeq */
384 PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++;
385
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000386 /* 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 Prijonob0808372006-03-02 21:18:58 +0000396 if (tsx->status_code/100 == 2) {
397 PJ_LOG(4,(THIS_FILE,
398 "Message \'%s\' delivered successfully",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000399 im_data->body.ptr));
Benny Prijonob0808372006-03-02 21:18:58 +0000400 } else {
401 PJ_LOG(3,(THIS_FILE,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000402 "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 Prijonob0808372006-03-02 21:18:58 +0000407 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000408
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000409 if (pjsua_var.ua_cfg.cb.on_pager_status) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000410 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 Prijonoba5926a2007-05-02 11:29:37 +0000414 (pjsip_status_code)
415 tsx->status_code,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000416 &tsx->status_text);
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000417 }
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 Prijono0a982002007-06-12 16:22:09 +0000434 tsx->last_tx,
Benny Prijonoba736c42008-07-10 20:45:03 +0000435 rdata, im_data->acc_id);
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000436 }
Benny Prijonob0808372006-03-02 21:18:58 +0000437 }
438}
439
440
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000441/* Outgoing typing indication callback.
442 * (used to reauthenticate request)
Benny Prijonob0808372006-03-02 21:18:58 +0000443 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444static void typing_callback(void *token, pjsip_event *e)
445{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000446 pjsua_im_data *im_data = (pjsua_im_data*) token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000447
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 Prijono48ab2b72007-11-08 09:24:30 +0000474 pjsip_auth_clt_set_prefs(&auth,
475 &pjsua_var.acc[im_data->acc_id].cfg.auth_pref);
476
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000477 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 Prijonoea4b4392009-10-01 14:17:49 +0000485 /* Increment CSeq */
486 PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++;
487
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000488 /* 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 */
506PJ_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 Prijonob0808372006-03-02 21:18:58 +0000512{
513 pjsip_tx_data *tdata;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 const pj_str_t mime_text_plain = pj_str("text/plain");
Benny Prijonob0808372006-03-02 21:18:58 +0000515 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000516 pjsip_media_type media_type;
517 pjsua_im_data *im_data;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000518 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000519 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000520 pj_status_t status;
521
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000522 /* To and message body must be specified. */
523 PJ_ASSERT_RETURN(to && content, PJ_EINVAL);
524
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000525 acc = &pjsua_var.acc[acc_id];
526
Benny Prijonob0808372006-03-02 21:18:58 +0000527 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 status = pjsip_endpt_create_request(pjsua_var.endpt,
529 &pjsip_message_method, to,
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000530 &acc->cfg.id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000531 to, NULL, NULL, -1, NULL, &tdata);
Benny Prijonob0808372006-03-02 21:18:58 +0000532 if (status != PJ_SUCCESS) {
533 pjsua_perror(THIS_FILE, "Unable to create request", status);
534 return status;
535 }
536
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000537 /* If account is locked to specific transport, then set transport to
538 * the request.
539 */
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000540 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000541 pjsip_tpselector tp_sel;
542
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000543 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000544 pjsip_tx_data_set_transport(tdata, &tp_sel);
545 }
546
Benny Prijonob0808372006-03-02 21:18:58 +0000547 /* Add accept header. */
548 pjsip_msg_add_hdr( tdata->msg,
549 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
550
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000551 /* 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 Prijonoc570f2d2006-07-18 00:33:02 +0000563 }
564
Benny Prijonob0808372006-03-02 21:18:58 +0000565 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000566 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000567 &STR_CONTACT, &contact));
Benny Prijonob0808372006-03-02 21:18:58 +0000568
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000569 /* Create IM data to keep message details and give it back to
570 * application on the callback
Benny Prijonob0808372006-03-02 21:18:58 +0000571 */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000572 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000573 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 Prijonob0808372006-03-02 21:18:58 +0000587
588 /* Add message body */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000589 tdata->msg->body = pjsip_msg_body_create( tdata->pool, &media_type.type,
590 &media_type.subtype,
591 &im_data->body);
Benny Prijonob0808372006-03-02 21:18:58 +0000592 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 Prijonoeebe9af2006-06-13 22:57:13 +0000598 /* Add additional headers etc. */
599 pjsua_process_msg_data(tdata, msg_data);
600
601 /* Add route set */
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000602 pjsua_set_msg_route_set(tdata, &acc->route_set);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000603
Benny Prijonob0808372006-03-02 21:18:58 +0000604 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000605 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
606 im_data, &im_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000607 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 Prijonoeebe9af2006-06-13 22:57:13 +0000616/*
Benny Prijonob0808372006-03-02 21:18:58 +0000617 * Send typing indication outside dialog.
618 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000619PJ_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 Prijonob0808372006-03-02 21:18:58 +0000623{
Benny Prijono8b1889b2006-06-06 18:40:40 +0000624 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000625 pjsua_im_data *im_data;
Benny Prijonob0808372006-03-02 21:18:58 +0000626 pjsip_tx_data *tdata;
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000627 pjsua_acc *acc;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000628 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000629 pj_status_t status;
630
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000631 acc = &pjsua_var.acc[acc_id];
632
Benny Prijonob0808372006-03-02 21:18:58 +0000633 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000634 status = pjsip_endpt_create_request( pjsua_var.endpt, &pjsip_message_method,
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000635 to, &acc->cfg.id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000636 to, NULL, NULL, -1, NULL, &tdata);
Benny Prijonob0808372006-03-02 21:18:58 +0000637 if (status != PJ_SUCCESS) {
638 pjsua_perror(THIS_FILE, "Unable to create request", status);
639 return status;
640 }
641
642
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000643 /* If account is locked to specific transport, then set transport to
644 * the request.
645 */
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000646 if (acc->cfg.transport_id != PJSUA_INVALID_ID) {
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000647 pjsip_tpselector tp_sel;
648
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000649 pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel);
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000650 pjsip_tx_data_set_transport(tdata, &tp_sel);
651 }
652
Benny Prijono8b1889b2006-06-06 18:40:40 +0000653 /* Add accept header. */
654 pjsip_msg_add_hdr( tdata->msg,
655 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
656
657
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000658 /* 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 Prijonoc570f2d2006-07-18 00:33:02 +0000670 }
671
Benny Prijono8b1889b2006-06-06 18:40:40 +0000672 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
673 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000674 &STR_CONTACT, &contact));
Benny Prijono8b1889b2006-06-06 18:40:40 +0000675
676
Benny Prijonob0808372006-03-02 21:18:58 +0000677 /* 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 Prijonoeebe9af2006-06-13 22:57:13 +0000681 /* Add additional headers etc. */
682 pjsua_process_msg_data(tdata, msg_data);
683
Benny Prijonoe1a10cb2007-04-04 10:45:44 +0000684 /* Add route set */
Nanang Izzuddin5af37ff2009-08-05 18:41:23 +0000685 pjsua_set_msg_route_set(tdata, &acc->route_set);
Benny Prijonoe1a10cb2007-04-04 10:45:44 +0000686
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000687 /* Create data to reauthenticate */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000688 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000689 im_data->acc_id = acc_id;
690
Benny Prijonob0808372006-03-02 21:18:58 +0000691 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000692 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
693 im_data, &typing_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000694 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 */
706pj_status_t pjsua_im_init(void)
707{
708 const pj_str_t msg_tag = { "MESSAGE", 7 };
Benny Prijonoc8141a82006-08-20 09:12:19 +0000709 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 Prijonob0808372006-03-02 21:18:58 +0000712 pj_status_t status;
713
714 /* Register module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000715 status = pjsip_endpt_register_module(pjsua_var.endpt, &mod_pjsua_im);
Benny Prijonob0808372006-03-02 21:18:58 +0000716 if (status != PJ_SUCCESS)
717 return status;
718
719 /* Register support for MESSAGE method. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000720 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ALLOW,
Benny Prijonob0808372006-03-02 21:18:58 +0000721 NULL, 1, &msg_tag);
722
Benny Prijonoc8141a82006-08-20 09:12:19 +0000723 /* 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 Prijonob0808372006-03-02 21:18:58 +0000731 return PJ_SUCCESS;
732}
733