blob: 46eb0e83869e4787c912940b00e10d036ccc4e54 [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,
380 rdata);
381 }
Benny Prijonob0808372006-03-02 21:18:58 +0000382 }
383}
384
385
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000386/* Outgoing typing indication callback.
387 * (used to reauthenticate request)
Benny Prijonob0808372006-03-02 21:18:58 +0000388 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000389static void typing_callback(void *token, pjsip_event *e)
390{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000391 pjsua_im_data *im_data = (pjsua_im_data*) token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392
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 */
445PJ_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 Prijonob0808372006-03-02 21:18:58 +0000451{
452 pjsip_tx_data *tdata;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000453 const pj_str_t mime_text_plain = pj_str("text/plain");
Benny Prijonob0808372006-03-02 21:18:58 +0000454 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000455 pjsip_media_type media_type;
456 pjsua_im_data *im_data;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000457 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000458 pj_status_t status;
459
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000460 /* To and message body must be specified. */
461 PJ_ASSERT_RETURN(to && content, PJ_EINVAL);
462
Benny Prijonob0808372006-03-02 21:18:58 +0000463 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000464 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 Prijonob0808372006-03-02 21:18:58 +0000468 if (status != PJ_SUCCESS) {
469 pjsua_perror(THIS_FILE, "Unable to create request", status);
470 return status;
471 }
472
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000473 /* 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 Prijonob0808372006-03-02 21:18:58 +0000483 /* Add accept header. */
484 pjsip_msg_add_hdr( tdata->msg,
485 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
486
487 /* Add contact. */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000488 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 Prijonob0808372006-03-02 21:18:58 +0000495 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000496 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000497 &STR_CONTACT, &contact));
Benny Prijonob0808372006-03-02 21:18:58 +0000498
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000499 /* Create IM data to keep message details and give it back to
500 * application on the callback
Benny Prijonob0808372006-03-02 21:18:58 +0000501 */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000502 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000503 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 Prijonob0808372006-03-02 21:18:58 +0000517
518 /* Add message body */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 tdata->msg->body = pjsip_msg_body_create( tdata->pool, &media_type.type,
520 &media_type.subtype,
521 &im_data->body);
Benny Prijonob0808372006-03-02 21:18:58 +0000522 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 Prijonoeebe9af2006-06-13 22:57:13 +0000528 /* 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 Prijonob0808372006-03-02 21:18:58 +0000534 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000535 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
536 im_data, &im_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000537 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 Prijonoeebe9af2006-06-13 22:57:13 +0000546/*
Benny Prijonob0808372006-03-02 21:18:58 +0000547 * Send typing indication outside dialog.
548 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549PJ_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 Prijonob0808372006-03-02 21:18:58 +0000553{
Benny Prijono8b1889b2006-06-06 18:40:40 +0000554 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 pjsua_im_data *im_data;
Benny Prijonob0808372006-03-02 21:18:58 +0000556 pjsip_tx_data *tdata;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000557 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000558 pj_status_t status;
559
560 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000561 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 Prijonob0808372006-03-02 21:18:58 +0000564 if (status != PJ_SUCCESS) {
565 pjsua_perror(THIS_FILE, "Unable to create request", status);
566 return status;
567 }
568
569
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000570 /* 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 Prijono8b1889b2006-06-06 18:40:40 +0000580 /* 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 Prijonoc570f2d2006-07-18 00:33:02 +0000586 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 Prijono8b1889b2006-06-06 18:40:40 +0000593 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
594 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000595 &STR_CONTACT, &contact));
Benny Prijono8b1889b2006-06-06 18:40:40 +0000596
597
Benny Prijonob0808372006-03-02 21:18:58 +0000598 /* 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 Prijonoeebe9af2006-06-13 22:57:13 +0000602 /* Add additional headers etc. */
603 pjsua_process_msg_data(tdata, msg_data);
604
Benny Prijonoe1a10cb2007-04-04 10:45:44 +0000605 /* Add route set */
606 pjsua_set_msg_route_set(tdata, &pjsua_var.acc[acc_id].route_set);
607
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000608 /* Create data to reauthenticate */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000609 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000610 im_data->acc_id = acc_id;
611
Benny Prijonob0808372006-03-02 21:18:58 +0000612 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000613 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
614 im_data, &typing_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000615 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 */
627pj_status_t pjsua_im_init(void)
628{
629 const pj_str_t msg_tag = { "MESSAGE", 7 };
Benny Prijonoc8141a82006-08-20 09:12:19 +0000630 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 Prijonob0808372006-03-02 21:18:58 +0000633 pj_status_t status;
634
635 /* Register module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000636 status = pjsip_endpt_register_module(pjsua_var.endpt, &mod_pjsua_im);
Benny Prijonob0808372006-03-02 21:18:58 +0000637 if (status != PJ_SUCCESS)
638 return status;
639
640 /* Register support for MESSAGE method. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000641 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ALLOW,
Benny Prijonob0808372006-03-02 21:18:58 +0000642 NULL, 1, &msg_tag);
643
Benny Prijonoc8141a82006-08-20 09:12:19 +0000644 /* 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 Prijonob0808372006-03-02 21:18:58 +0000652 return PJ_SUCCESS;
653}
654