blob: a727d5daa2737cbdf03a7fd54bd84fd5189b6e8b [file] [log] [blame]
Benny Prijonob0808372006-03-02 21:18:58 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 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);
172 if (contact_hdr) {
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
383 /* Re-send request */
384 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
385 im_data2, &im_callback);
386 if (status == PJ_SUCCESS) {
387 /* Done */
388 return;
389 }
390 }
391 }
392
Benny Prijonob0808372006-03-02 21:18:58 +0000393 if (tsx->status_code/100 == 2) {
394 PJ_LOG(4,(THIS_FILE,
395 "Message \'%s\' delivered successfully",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000396 im_data->body.ptr));
Benny Prijonob0808372006-03-02 21:18:58 +0000397 } else {
398 PJ_LOG(3,(THIS_FILE,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000399 "Failed to deliver message \'%s\': %d/%.*s",
400 im_data->body.ptr,
401 tsx->status_code,
402 (int)tsx->status_text.slen,
403 tsx->status_text.ptr));
Benny Prijonob0808372006-03-02 21:18:58 +0000404 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000405
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000406 if (pjsua_var.ua_cfg.cb.on_pager_status) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000407 pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id,
408 &im_data->to,
409 &im_data->body,
410 im_data->user_data,
Benny Prijonoba5926a2007-05-02 11:29:37 +0000411 (pjsip_status_code)
412 tsx->status_code,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 &tsx->status_text);
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000414 }
415
416 if (pjsua_var.ua_cfg.cb.on_pager_status2) {
417 pjsip_rx_data *rdata;
418
419 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
420 rdata = e->body.tsx_state.src.rdata;
421 else
422 rdata = NULL;
423
424 pjsua_var.ua_cfg.cb.on_pager_status2(im_data->call_id,
425 &im_data->to,
426 &im_data->body,
427 im_data->user_data,
428 (pjsip_status_code)
429 tsx->status_code,
430 &tsx->status_text,
Benny Prijono0a982002007-06-12 16:22:09 +0000431 tsx->last_tx,
Benny Prijonoba736c42008-07-10 20:45:03 +0000432 rdata, im_data->acc_id);
Benny Prijono4da0b1d2007-06-11 18:22:54 +0000433 }
Benny Prijonob0808372006-03-02 21:18:58 +0000434 }
435}
436
437
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000438/* Outgoing typing indication callback.
439 * (used to reauthenticate request)
Benny Prijonob0808372006-03-02 21:18:58 +0000440 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000441static void typing_callback(void *token, pjsip_event *e)
442{
Benny Prijonoa1e69682007-05-11 15:14:34 +0000443 pjsua_im_data *im_data = (pjsua_im_data*) token;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444
445 if (e->type == PJSIP_EVENT_TSX_STATE) {
446
447 pjsip_transaction *tsx = e->body.tsx_state.tsx;
448
449 /* Ignore provisional response, if any */
450 if (tsx->status_code < 200)
451 return;
452
453 /* Handle authentication challenges */
454 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG &&
455 (tsx->status_code == 401 || tsx->status_code == 407))
456 {
457 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
458 pjsip_tx_data *tdata;
459 pjsip_auth_clt_sess auth;
460 pj_status_t status;
461
462 PJ_LOG(4,(THIS_FILE, "Resending IM with authentication"));
463
464 /* Create temporary authentication session */
465 pjsip_auth_clt_init(&auth,pjsua_var.endpt,rdata->tp_info.pool, 0);
466
467 pjsip_auth_clt_set_credentials(&auth,
468 pjsua_var.acc[im_data->acc_id].cred_cnt,
469 pjsua_var.acc[im_data->acc_id].cred);
470
Benny Prijono48ab2b72007-11-08 09:24:30 +0000471 pjsip_auth_clt_set_prefs(&auth,
472 &pjsua_var.acc[im_data->acc_id].cfg.auth_pref);
473
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000474 status = pjsip_auth_clt_reinit_req(&auth, rdata, tsx->last_tx,
475 &tdata);
476 if (status == PJ_SUCCESS) {
477 pjsua_im_data *im_data2;
478
479 /* Must duplicate im_data */
480 im_data2 = pjsua_im_data_dup(tdata->pool, im_data);
481
482 /* Re-send request */
483 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
484 im_data2, &typing_callback);
485 if (status == PJ_SUCCESS) {
486 /* Done */
487 return;
488 }
489 }
490 }
491
492 }
493}
494
495
496/*
497 * Send instant messaging outside dialog, using the specified account for
498 * route set and authentication.
499 */
500PJ_DEF(pj_status_t) pjsua_im_send( pjsua_acc_id acc_id,
501 const pj_str_t *to,
502 const pj_str_t *mime_type,
503 const pj_str_t *content,
504 const pjsua_msg_data *msg_data,
505 void *user_data)
Benny Prijonob0808372006-03-02 21:18:58 +0000506{
507 pjsip_tx_data *tdata;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 const pj_str_t mime_text_plain = pj_str("text/plain");
Benny Prijonob0808372006-03-02 21:18:58 +0000509 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510 pjsip_media_type media_type;
511 pjsua_im_data *im_data;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000512 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000513 pj_status_t status;
514
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000515 /* To and message body must be specified. */
516 PJ_ASSERT_RETURN(to && content, PJ_EINVAL);
517
Benny Prijonob0808372006-03-02 21:18:58 +0000518 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 status = pjsip_endpt_create_request(pjsua_var.endpt,
520 &pjsip_message_method, to,
521 &pjsua_var.acc[acc_id].cfg.id,
522 to, NULL, NULL, -1, NULL, &tdata);
Benny Prijonob0808372006-03-02 21:18:58 +0000523 if (status != PJ_SUCCESS) {
524 pjsua_perror(THIS_FILE, "Unable to create request", status);
525 return status;
526 }
527
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000528 /* If account is locked to specific transport, then set transport to
529 * the request.
530 */
531 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
532 pjsip_tpselector tp_sel;
533
534 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
535 pjsip_tx_data_set_transport(tdata, &tp_sel);
536 }
537
Benny Prijonob0808372006-03-02 21:18:58 +0000538 /* Add accept header. */
539 pjsip_msg_add_hdr( tdata->msg,
540 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
541
542 /* Add contact. */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000543 status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to);
544 if (status != PJ_SUCCESS) {
545 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
546 pjsip_tx_data_dec_ref(tdata);
547 return status;
548 }
549
Benny Prijonob0808372006-03-02 21:18:58 +0000550 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000551 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000552 &STR_CONTACT, &contact));
Benny Prijonob0808372006-03-02 21:18:58 +0000553
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000554 /* Create IM data to keep message details and give it back to
555 * application on the callback
Benny Prijonob0808372006-03-02 21:18:58 +0000556 */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000557 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000558 im_data->acc_id = acc_id;
559 im_data->call_id = PJSUA_INVALID_ID;
560 pj_strdup_with_null(tdata->pool, &im_data->to, to);
561 pj_strdup_with_null(tdata->pool, &im_data->body, content);
562 im_data->user_data = user_data;
563
564
565 /* Set default media type if none is specified */
566 if (mime_type == NULL) {
567 mime_type = &mime_text_plain;
568 }
569
570 /* Parse MIME type */
571 pjsua_parse_media_type(tdata->pool, mime_type, &media_type);
Benny Prijonob0808372006-03-02 21:18:58 +0000572
573 /* Add message body */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000574 tdata->msg->body = pjsip_msg_body_create( tdata->pool, &media_type.type,
575 &media_type.subtype,
576 &im_data->body);
Benny Prijonob0808372006-03-02 21:18:58 +0000577 if (tdata->msg->body == NULL) {
578 pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM);
579 pjsip_tx_data_dec_ref(tdata);
580 return PJ_ENOMEM;
581 }
582
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000583 /* Add additional headers etc. */
584 pjsua_process_msg_data(tdata, msg_data);
585
586 /* Add route set */
587 pjsua_set_msg_route_set(tdata, &pjsua_var.acc[acc_id].route_set);
588
Benny Prijonob0808372006-03-02 21:18:58 +0000589 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000590 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
591 im_data, &im_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000592 if (status != PJ_SUCCESS) {
593 pjsua_perror(THIS_FILE, "Unable to send request", status);
594 return status;
595 }
596
597 return PJ_SUCCESS;
598}
599
600
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000601/*
Benny Prijonob0808372006-03-02 21:18:58 +0000602 * Send typing indication outside dialog.
603 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000604PJ_DEF(pj_status_t) pjsua_im_typing( pjsua_acc_id acc_id,
605 const pj_str_t *to,
606 pj_bool_t is_typing,
607 const pjsua_msg_data *msg_data)
Benny Prijonob0808372006-03-02 21:18:58 +0000608{
Benny Prijono8b1889b2006-06-06 18:40:40 +0000609 const pj_str_t STR_CONTACT = { "Contact", 7 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000610 pjsua_im_data *im_data;
Benny Prijonob0808372006-03-02 21:18:58 +0000611 pjsip_tx_data *tdata;
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000612 pj_str_t contact;
Benny Prijonob0808372006-03-02 21:18:58 +0000613 pj_status_t status;
614
615 /* Create request. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000616 status = pjsip_endpt_create_request( pjsua_var.endpt, &pjsip_message_method,
617 to, &pjsua_var.acc[acc_id].cfg.id,
618 to, NULL, NULL, -1, NULL, &tdata);
Benny Prijonob0808372006-03-02 21:18:58 +0000619 if (status != PJ_SUCCESS) {
620 pjsua_perror(THIS_FILE, "Unable to create request", status);
621 return status;
622 }
623
624
Benny Prijono62c5c5b2007-01-13 23:22:40 +0000625 /* If account is locked to specific transport, then set transport to
626 * the request.
627 */
628 if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) {
629 pjsip_tpselector tp_sel;
630
631 pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel);
632 pjsip_tx_data_set_transport(tdata, &tp_sel);
633 }
634
Benny Prijono8b1889b2006-06-06 18:40:40 +0000635 /* Add accept header. */
636 pjsip_msg_add_hdr( tdata->msg,
637 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
638
639
640 /* Add contact. */
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000641 status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to);
642 if (status != PJ_SUCCESS) {
643 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
644 pjsip_tx_data_dec_ref(tdata);
645 return status;
646 }
647
Benny Prijono8b1889b2006-06-06 18:40:40 +0000648 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
649 pjsip_generic_string_hdr_create(tdata->pool,
Benny Prijonoc570f2d2006-07-18 00:33:02 +0000650 &STR_CONTACT, &contact));
Benny Prijono8b1889b2006-06-06 18:40:40 +0000651
652
Benny Prijonob0808372006-03-02 21:18:58 +0000653 /* Create "application/im-iscomposing+xml" msg body. */
654 tdata->msg->body = pjsip_iscomposing_create_body( tdata->pool, is_typing,
655 NULL, NULL, -1);
656
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000657 /* Add additional headers etc. */
658 pjsua_process_msg_data(tdata, msg_data);
659
Benny Prijonoe1a10cb2007-04-04 10:45:44 +0000660 /* Add route set */
661 pjsua_set_msg_route_set(tdata, &pjsua_var.acc[acc_id].route_set);
662
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000663 /* Create data to reauthenticate */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000664 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000665 im_data->acc_id = acc_id;
666
Benny Prijonob0808372006-03-02 21:18:58 +0000667 /* Send request (statefully) */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000668 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
669 im_data, &typing_callback);
Benny Prijonob0808372006-03-02 21:18:58 +0000670 if (status != PJ_SUCCESS) {
671 pjsua_perror(THIS_FILE, "Unable to send request", status);
672 return status;
673 }
674
675 return PJ_SUCCESS;
676}
677
678
679/*
680 * Init pjsua IM module.
681 */
682pj_status_t pjsua_im_init(void)
683{
684 const pj_str_t msg_tag = { "MESSAGE", 7 };
Benny Prijonoc8141a82006-08-20 09:12:19 +0000685 const pj_str_t STR_MIME_TEXT_PLAIN = { "text/plain", 10 };
686 const pj_str_t STR_MIME_APP_ISCOMPOSING =
687 { "application/im-iscomposing+xml", 30 };
Benny Prijonob0808372006-03-02 21:18:58 +0000688 pj_status_t status;
689
690 /* Register module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000691 status = pjsip_endpt_register_module(pjsua_var.endpt, &mod_pjsua_im);
Benny Prijonob0808372006-03-02 21:18:58 +0000692 if (status != PJ_SUCCESS)
693 return status;
694
695 /* Register support for MESSAGE method. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000696 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ALLOW,
Benny Prijonob0808372006-03-02 21:18:58 +0000697 NULL, 1, &msg_tag);
698
Benny Prijonoc8141a82006-08-20 09:12:19 +0000699 /* Register support for "application/im-iscomposing+xml" content */
700 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT,
701 NULL, 1, &STR_MIME_APP_ISCOMPOSING);
702
703 /* Register support for "text/plain" content */
704 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT,
705 NULL, 1, &STR_MIME_TEXT_PLAIN);
706
Benny Prijonob0808372006-03-02 21:18:58 +0000707 return PJ_SUCCESS;
708}
709