blob: 48cb802a92c96cbb256577b241d9c9499794f476 [file] [log] [blame]
Benny Prijonob0808372006-03-02 21:18:58 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonob0808372006-03-02 21:18:58 +00004 *
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 Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
Benny Prijonob0808372006-03-02 21:18:58 +000021
Benny Prijonob0808372006-03-02 21:18:58 +000022
Benny Prijonoeebe9af2006-06-13 22:57:13 +000023#define THIS_FILE "pjsua_im.h"
Benny Prijonob0808372006-03-02 21:18:58 +000024
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 */
31enum
32{
33 PJSIP_MESSAGE_METHOD = PJSIP_OTHER_METHOD
34};
35
36const pjsip_method pjsip_message_method =
37{
Benny Prijonoba5926a2007-05-02 11:29:37 +000038 (pjsip_method_e) PJSIP_MESSAGE_METHOD,
Benny Prijonob0808372006-03-02 21:18:58 +000039 { "MESSAGE", 7 }
40};
41
42
43/* Proto */
44static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata);
45
Benny Prijonoeebe9af2006-06-13 22:57:13 +000046
Benny Prijonob0808372006-03-02 21:18:58 +000047/* The module instance. */
48static 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. */
68static const pj_str_t STR_MIME_APP = { "application", 11 };
69static const pj_str_t STR_MIME_ISCOMPOSING = { "im-iscomposing+xml", 18 };
70static const pj_str_t STR_MIME_TEXT = { "text", 4 };
71static const pj_str_t STR_MIME_PLAIN = { "plain", 5 };
72
73
74/* Check if content type is acceptable */
Benny Prijonof4b538d2007-05-14 16:45:20 +000075#if 0
Benny Prijonob0808372006-03-02 21:18:58 +000076static 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 Prijonof4b538d2007-05-14 16:45:20 +000084#endif
Benny Prijonob0808372006-03-02 21:18:58 +000085
86/**
87 * Create Accept header for MESSAGE.
88 */
89pjsip_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 */
105pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
Benny Prijonof9c668f2006-03-06 15:14:59 +0000106 pjsip_accept_hdr **p_accept_hdr)
Benny Prijonob0808372006-03-02 21:18:58 +0000107{
Benny Prijonof4b538d2007-05-14 16:45:20 +0000108 /* Some UA sends text/html, so this check will break */
109#if 0
Benny Prijonob0808372006-03-02 21:18:58 +0000110 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 Prijonoa1e69682007-05-11 15:14:34 +0000118 ctype = (pjsip_ctype_hdr*)
119 pjsip_msg_find_hdr(msg, PJSIP_H_CONTENT_TYPE, NULL);
Benny Prijonob0808372006-03-02 21:18:58 +0000120 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 Prijonof4b538d2007-05-14 16:45:20 +0000129#else
130 PJ_UNUSED_ARG(rdata);
131 PJ_UNUSED_ARG(p_accept_hdr);
132#endif
Benny Prijonob0808372006-03-02 21:18:58 +0000133
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 Prijonoeebe9af2006-06-13 22:57:13 +0000141void pjsua_im_process_pager(int call_id, const pj_str_t *from,
Benny Prijonob0808372006-03-02 21:18:58 +0000142 const pj_str_t *to, pjsip_rx_data *rdata)
143{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000144 pjsip_contact_hdr *contact_hdr;
145 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000146 pjsip_msg_body *body = rdata->msg_info.msg->body;
147
148 /* Body MUST have been checked before */
149 pj_assert(body != NULL);
150
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000151
152 /* Build remote contact */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000153 contact_hdr = (pjsip_contact_hdr*)
154 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000155 NULL);
156 if (contact_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000157 contact.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool,
158 PJSIP_MAX_URL_SIZE);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000159 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 Prijonof4b538d2007-05-14 16:45:20 +0000166 if (pj_stricmp(&body->content_type.type, &STR_MIME_APP)==0 &&
167 pj_stricmp(&body->content_type.subtype, &STR_MIME_ISCOMPOSING)==0)
Benny Prijonob0808372006-03-02 21:18:58 +0000168 {
Benny Prijonob0808372006-03-02 21:18:58 +0000169 /* Expecting typing indication */
Benny Prijonob0808372006-03-02 21:18:58 +0000170 pj_status_t status;
171 pj_bool_t is_typing;
172
Benny Prijonoa1e69682007-05-11 15:14:34 +0000173 status = pjsip_iscomposing_parse(rdata->tp_info.pool, (char*)body->data,
174 body->len, &is_typing, NULL, NULL,
175 NULL );
Benny Prijonob0808372006-03-02 21:18:58 +0000176 if (status != PJ_SUCCESS) {
177 pjsua_perror(THIS_FILE, "Invalid MESSAGE body", status);
178 return;
179 }
180
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181 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 Prijonof4b538d2007-05-14 16:45:20 +0000185
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 Prijono38507402007-05-15 11:15:58 +0000193 text_body.ptr = (char*)rdata->msg_info.msg->body->data;
Benny Prijonof4b538d2007-05-14 16:45:20 +0000194 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 Prijonob0808372006-03-02 21:18:58 +0000212
Benny Prijonobbeb3992007-05-21 13:48:35 +0000213 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 Prijonob0808372006-03-02 21:18:58 +0000218}
219
220
221/*
222 * Handler to receive incoming MESSAGE
223 */
224static 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 Prijonoeebe9af2006-06-13 22:57:13 +0000252 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
Benny Prijonob0808372006-03-02 21:18:58 +0000253 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 Prijonoeebe9af2006-06-13 22:57:13 +0000261 status = pjsip_endpt_respond( pjsua_var.endpt, NULL, rdata, 200, NULL,
Benny Prijonob0808372006-03-02 21:18:58 +0000262 NULL, NULL, NULL);
263
Benny Prijono8b1889b2006-06-06 18:40:40 +0000264 /* 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 Prijono38507402007-05-15 11:15:58 +0000268 from.ptr = (char*)pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE);
Benny Prijonof4b538d2007-05-14 16:45:20 +0000269 from.slen = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR,
270 rdata->msg_info.from->uri,
271 from.ptr, PJSIP_MAX_URL_SIZE);
Benny Prijono8b1889b2006-06-06 18:40:40 +0000272
Benny Prijonob0808372006-03-02 21:18:58 +0000273 if (from.slen < 1)
274 from = pj_str("<--URI is too long-->");
275
276 /* Build the To text. */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000277 to.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE);
Benny Prijonob0808372006-03-02 21:18:58 +0000278 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. */
293static void im_callback(void *token, pjsip_event *e)
294{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000295 pjsua_im_data *im_data = (pjsua_im_data*) token;
Benny Prijonob0808372006-03-02 21:18:58 +0000296
297 if (e->type == PJSIP_EVENT_TSX_STATE) {
298
299 pjsip_transaction *tsx = e->body.tsx_state.tsx;
300
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000301 /* 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 Prijonob0808372006-03-02 21:18:58 +0000342 if (tsx->status_code/100 == 2) {
343 PJ_LOG(4,(THIS_FILE,
344 "Message \'%s\' delivered successfully",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000345 im_data->body.ptr));
Benny Prijonob0808372006-03-02 21:18:58 +0000346 } else {
347 PJ_LOG(3,(THIS_FILE,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000348 "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 Prijonob0808372006-03-02 21:18:58 +0000353 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000354
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000355 if (pjsua_var.ua_cfg.cb.on_pager_status) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000356 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 Prijonoba5926a2007-05-02 11:29:37 +0000360 (pjsip_status_code)
361 tsx->status_code,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000362 &tsx->status_text);
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000363 }
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,
Benny Prijono0a982002007-06-12 16:22:09 +0000380 tsx->last_tx,
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000381 rdata);
382 }
Benny Prijonob0808372006-03-02 21:18:58 +0000383 }
384}
385
386
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387/* Outgoing typing indication callback.
388 * (used to reauthenticate request)
Benny Prijonob0808372006-03-02 21:18:58 +0000389 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390static void typing_callback(void *token, pjsip_event *e)
391{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000392 pjsua_im_data *im_data = (pjsua_im_data*) token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000393
394 if (e->type == PJSIP_EVENT_TSX_STATE) {
395
396 pjsip_transaction *tsx = e->body.tsx_state.tsx;
397
398 /* Ignore provisional response, if any */
399 if (tsx->status_code < 200)
400 return;
401
402 /* Handle authentication challenges */
403 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG &&
404 (tsx->status_code == 401 || tsx->status_code == 407))
405 {
406 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
407 pjsip_tx_data *tdata;
408 pjsip_auth_clt_sess auth;
409 pj_status_t status;
410
411 PJ_LOG(4,(THIS_FILE, "Resending IM with authentication"));
412
413 /* Create temporary authentication session */
414 pjsip_auth_clt_init(&auth,pjsua_var.endpt,rdata->tp_info.pool, 0);
415
416 pjsip_auth_clt_set_credentials(&auth,
417 pjsua_var.acc[im_data->acc_id].cred_cnt,
418 pjsua_var.acc[im_data->acc_id].cred);
419
420 status = pjsip_auth_clt_reinit_req(&auth, rdata, tsx->last_tx,
421 &tdata);
422 if (status == PJ_SUCCESS) {
423 pjsua_im_data *im_data2;
424
425 /* Must duplicate im_data */
426 im_data2 = pjsua_im_data_dup(tdata->pool, im_data);
427
428 /* Re-send request */
429 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
430 im_data2, &typing_callback);
431 if (status == PJ_SUCCESS) {
432 /* Done */
433 return;
434 }
435 }
436 }
437
438 }
439}
440
441
442/*
443 * Send instant messaging outside dialog, using the specified account for
444 * route set and authentication.
445 */
446PJ_DEF(pj_status_t) pjsua_im_send( pjsua_acc_id acc_id,
447 const pj_str_t *to,
448 const pj_str_t *mime_type,
449 const pj_str_t *content,
450 const pjsua_msg_data *msg_data,
451 void *user_data)
Benny Prijonob0808372006-03-02 21:18:58 +0000452{
453 pjsip_tx_data *tdata;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000454 const pj_str_t mime_text_plain = pj_str("text/plain");
Benny Prijonob0808372006-03-02 21:18:58 +0000455 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000456 pjsip_media_type media_type;
457 pjsua_im_data *im_data;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000458 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000459 pj_status_t status;
460
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000461 /* To and message body must be specified. */
462 PJ_ASSERT_RETURN(to && content, PJ_EINVAL);
463
Benny Prijonob0808372006-03-02 21:18:58 +0000464 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000465 status = pjsip_endpt_create_request(pjsua_var.endpt,
466 &pjsip_message_method, to,
467 &pjsua_var.acc[acc_id].cfg.id,
468 to, NULL, NULL, -1, NULL, &tdata);
Benny Prijonob0808372006-03-02 21:18:58 +0000469 if (status != PJ_SUCCESS) {
470 pjsua_perror(THIS_FILE, "Unable to create request", status);
471 return status;
472 }
473
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000474 /* If account is locked to specific transport, then set transport to
475 * the request.
476 */
477 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
478 pjsip_tpselector tp_sel;
479
480 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
481 pjsip_tx_data_set_transport(tdata, &tp_sel);
482 }
483
Benny Prijonob0808372006-03-02 21:18:58 +0000484 /* Add accept header. */
485 pjsip_msg_add_hdr( tdata->msg,
486 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
487
488 /* Add contact. */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000489 status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to);
490 if (status != PJ_SUCCESS) {
491 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
492 pjsip_tx_data_dec_ref(tdata);
493 return status;
494 }
495
Benny Prijonob0808372006-03-02 21:18:58 +0000496 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000497 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000498 &STR_CONTACT, &contact));
Benny Prijonob0808372006-03-02 21:18:58 +0000499
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000500 /* Create IM data to keep message details and give it back to
501 * application on the callback
Benny Prijonob0808372006-03-02 21:18:58 +0000502 */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000503 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504 im_data->acc_id = acc_id;
505 im_data->call_id = PJSUA_INVALID_ID;
506 pj_strdup_with_null(tdata->pool, &im_data->to, to);
507 pj_strdup_with_null(tdata->pool, &im_data->body, content);
508 im_data->user_data = user_data;
509
510
511 /* Set default media type if none is specified */
512 if (mime_type == NULL) {
513 mime_type = &mime_text_plain;
514 }
515
516 /* Parse MIME type */
517 pjsua_parse_media_type(tdata->pool, mime_type, &media_type);
Benny Prijonob0808372006-03-02 21:18:58 +0000518
519 /* Add message body */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000520 tdata->msg->body = pjsip_msg_body_create( tdata->pool, &media_type.type,
521 &media_type.subtype,
522 &im_data->body);
Benny Prijonob0808372006-03-02 21:18:58 +0000523 if (tdata->msg->body == NULL) {
524 pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM);
525 pjsip_tx_data_dec_ref(tdata);
526 return PJ_ENOMEM;
527 }
528
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 /* Add additional headers etc. */
530 pjsua_process_msg_data(tdata, msg_data);
531
532 /* Add route set */
533 pjsua_set_msg_route_set(tdata, &pjsua_var.acc[acc_id].route_set);
534
Benny Prijonob0808372006-03-02 21:18:58 +0000535 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000536 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
537 im_data, &im_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000538 if (status != PJ_SUCCESS) {
539 pjsua_perror(THIS_FILE, "Unable to send request", status);
540 return status;
541 }
542
543 return PJ_SUCCESS;
544}
545
546
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547/*
Benny Prijonob0808372006-03-02 21:18:58 +0000548 * Send typing indication outside dialog.
549 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000550PJ_DEF(pj_status_t) pjsua_im_typing( pjsua_acc_id acc_id,
551 const pj_str_t *to,
552 pj_bool_t is_typing,
553 const pjsua_msg_data *msg_data)
Benny Prijonob0808372006-03-02 21:18:58 +0000554{
Benny Prijono8b1889b2006-06-06 18:40:40 +0000555 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000556 pjsua_im_data *im_data;
Benny Prijonob0808372006-03-02 21:18:58 +0000557 pjsip_tx_data *tdata;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000558 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000559 pj_status_t status;
560
561 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000562 status = pjsip_endpt_create_request( pjsua_var.endpt, &pjsip_message_method,
563 to, &pjsua_var.acc[acc_id].cfg.id,
564 to, NULL, NULL, -1, NULL, &tdata);
Benny Prijonob0808372006-03-02 21:18:58 +0000565 if (status != PJ_SUCCESS) {
566 pjsua_perror(THIS_FILE, "Unable to create request", status);
567 return status;
568 }
569
570
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000571 /* If account is locked to specific transport, then set transport to
572 * the request.
573 */
574 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
575 pjsip_tpselector tp_sel;
576
577 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
578 pjsip_tx_data_set_transport(tdata, &tp_sel);
579 }
580
Benny Prijono8b1889b2006-06-06 18:40:40 +0000581 /* Add accept header. */
582 pjsip_msg_add_hdr( tdata->msg,
583 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
584
585
586 /* Add contact. */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000587 status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to);
588 if (status != PJ_SUCCESS) {
589 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
590 pjsip_tx_data_dec_ref(tdata);
591 return status;
592 }
593
Benny Prijono8b1889b2006-06-06 18:40:40 +0000594 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
595 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000596 &STR_CONTACT, &contact));
Benny Prijono8b1889b2006-06-06 18:40:40 +0000597
598
Benny Prijonob0808372006-03-02 21:18:58 +0000599 /* Create "application/im-iscomposing+xml" msg body. */
600 tdata->msg->body = pjsip_iscomposing_create_body( tdata->pool, is_typing,
601 NULL, NULL, -1);
602
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000603 /* Add additional headers etc. */
604 pjsua_process_msg_data(tdata, msg_data);
605
Benny Prijonoe1a10cb2007-04-04 10:45:44 +0000606 /* Add route set */
607 pjsua_set_msg_route_set(tdata, &pjsua_var.acc[acc_id].route_set);
608
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000609 /* Create data to reauthenticate */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000610 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611 im_data->acc_id = acc_id;
612
Benny Prijonob0808372006-03-02 21:18:58 +0000613 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000614 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
615 im_data, &typing_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000616 if (status != PJ_SUCCESS) {
617 pjsua_perror(THIS_FILE, "Unable to send request", status);
618 return status;
619 }
620
621 return PJ_SUCCESS;
622}
623
624
625/*
626 * Init pjsua IM module.
627 */
628pj_status_t pjsua_im_init(void)
629{
630 const pj_str_t msg_tag = { "MESSAGE", 7 };
Benny Prijonoc8141a82006-08-20 09:12:19 +0000631 const pj_str_t STR_MIME_TEXT_PLAIN = { "text/plain", 10 };
632 const pj_str_t STR_MIME_APP_ISCOMPOSING =
633 { "application/im-iscomposing+xml", 30 };
Benny Prijonob0808372006-03-02 21:18:58 +0000634 pj_status_t status;
635
636 /* Register module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000637 status = pjsip_endpt_register_module(pjsua_var.endpt, &mod_pjsua_im);
Benny Prijonob0808372006-03-02 21:18:58 +0000638 if (status != PJ_SUCCESS)
639 return status;
640
641 /* Register support for MESSAGE method. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000642 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ALLOW,
Benny Prijonob0808372006-03-02 21:18:58 +0000643 NULL, 1, &msg_tag);
644
Benny Prijonoc8141a82006-08-20 09:12:19 +0000645 /* Register support for "application/im-iscomposing+xml" content */
646 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT,
647 NULL, 1, &STR_MIME_APP_ISCOMPOSING);
648
649 /* Register support for "text/plain" content */
650 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT,
651 NULL, 1, &STR_MIME_TEXT_PLAIN);
652
Benny Prijonob0808372006-03-02 21:18:58 +0000653 return PJ_SUCCESS;
654}
655