blob: 742d739c804752aa5dff55406cee7e25d1a866ee [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJSIP_SIMPLE_MESSAGING_H__
20#define __PJSIP_SIMPLE_MESSAGING_H__
21
22/**
23 * @file messaging.h
24 * @brief Instant Messaging Extension (RFC 3428)
25 */
26
27#include <pjsip/sip_msg.h>
28
29PJ_BEGIN_DECL
30
31/**
32 * @defgroup PJSIP_MESSAGING SIP Instant Messaging (RFC 3428) Module
33 * @ingroup PJSIP_SIMPLE
34 * @{
35 *
36 * This module provides the implementation of SIP Extension for Instant
37 * Messaging (RFC 3428). It extends PJSIP by supporting MESSAGE method.
38 *
39 * The RFC 3428 doesn't provide any means of dialog for the purpose of sending/
40 * receiving instant messaging. IM with SIP is basicly sessionless, which means
41 * that there is absolutely no correlation between IM messages sent or received
42 * by a host. Any correlation between IM messages is only perceivable by
43 * user, phsychologically.
44 *
45 * However, the RFC doesn't prohibit sending IM within a dialog (presumably
46 * using the same Call-ID and CSeq namespace), although it prohibits creating
47 * a dialog specificly for creating IM session.
48 *
49 * The implementation here is modeled to support both ways of sending IM msgs,
50 * i.e. sending IM message individually and sending IM message within a dialog.
51 * Although IM message can be associated with a dialog, this implementation of
52 * IM module is completely independent of the User Agent library in PJSIP. Yes,
53 * that's what is called modularity, and it demonstrates the clearness
54 * of PJSIP design (the last sentence is of course marketing crap :)).
55 *
56 * To send an IM message as part of dialog, application would first create the
57 * message using #pjsip_messaging_create_msg, using dialog's Call-ID, CSeq,
58 * From, and To header, then send the message using #pjsip_dlg_send_msg instead
59 * of #pjsip_messaging_send_msg.
60 *
61 * To send IM messages individually, application has two options. The first is
62 * to create the request with #pjsip_messaging_create_msg then send it with
63 * #pjsip_messaging_send_msg. But this way, application has to pre-construct
64 * From and To header first, which is not too convenient.
65 *
66 * The second option (to send IM messages not associated with a dialog) is to
67 * first create an 'IM session'. An 'IM session' here is not a SIP dialog, as
68 * it doesn't have Contact header etc. An 'IM session' here is just a local
69 * state to cache most of IM headers, for convenience and optimization. Appl
70 * creates an IM session with #pjsip_messaging_create_session, and destroy
71 * the session with #pjsip_messaging_destroy_session. To create an outgoing
72 * IM message, application would call #pjsip_messaging_session_create_msg,
73 * and to send the message it would use #pjsip_messaging_send_msg.
74 *
75 * Message authorization is handled by application, as usual, by inserting a
76 * proper WWW-Authenticate or Proxy-Authenticate header before sending the
77 * message.
78 *
79 * And the last bit, to handle incoing IM messages.
80 *
81 * To handle incoming IM messages, application would register a global callback
82 * to be called when incoming messages arrive, by registering with function
83 * #pjsip_messaging_set_incoming_callback. This will be the global callback
84 * for all incoming IM messages. Although the message was sent as part of
85 * a dialog, it would still come here. And as long as the request has proper
86 * identification (Call-ID, From/To tag), the dialog will be aware of the
87 * request and update it's state (remote CSeq) accordingly.
88 */
89
90
91
92/**
93 * Typedef for callback to be called when outgoing message has been sent
94 * and a final response has been received.
95 */
96typedef void (*pjsip_messaging_cb)(void *token, int status_code);
97
98/**
99 * Typedef for callback to receive incoming message.
100 *
101 * @param rdata Incoming message data.
102 *
103 * @return The status code to be returned back to original sender.
104 * Application must return a final status code upon returning
105 * from this function, or otherwise the stack will respond
106 * with error.
107 */
108typedef int (*pjsip_on_new_msg_cb)(pjsip_rx_data *rdata);
109
110/**
111 * Opaque data type for instant messaging session.
112 */
113typedef struct pjsip_messaging_session pjsip_messaging_session;
114
115/**
116 * Get the messaging module.
117 *
118 * @return SIP module.
119 */
120PJ_DECL(pjsip_module*) pjsip_messaging_get_module();
121
122/**
123 * Set the global callback to be called when incoming message is received.
124 *
125 * @param cb The callback to be called when incoming message is received.
126 *
127 * @return The previous callback.
128 */
129PJ_DECL(pjsip_on_new_msg_cb)
130pjsip_messaging_set_incoming_callback(pjsip_on_new_msg_cb cb);
131
132
133/**
134 * Create an instant message transmit data buffer using the specified arguments.
135 * The returned transmit data buffers will have it's reference counter set
136 * to 1, and when application send the buffer, the send function will decrement
137 * the reference counter. When the reference counter reach zero, the buffer
138 * will be deleted. As long as the function does not increment the buffer's
139 * reference counter between creating and sending the request, the buffer
140 * will always be deleted and no memory leak will occur.
141 *
142 * @param endpt Endpoint instance.
143 * @param target Target URL.
144 * @param from The "From" header, which content will be copied to request.
145 * If the "From" header doesn't have a tag parameter, the
146 * function will generate one.
147 * @param to The "To" header, which content will be copied to request.
148 * @param call_id Optionally specify Call-ID, or put NULL to make this
149 * function generate a unique Call-ID automatically.
150 * @param cseq Optionally specify CSeq, or put -1 to make this function
151 * generate a random CSeq.
152 * @param text Optionally specify "text/plain" message body, or put NULL
153 * if application wants to put body other than "text/plain"
154 * manually.
155 *
156 * @return SIP transmit data buffer, which reference count has been
157 * set to 1.
158 */
159PJ_DECL(pjsip_tx_data*)
160pjsip_messaging_create_msg_from_hdr(pjsip_endpoint *endpt,
161 const pjsip_uri *target,
162 const pjsip_from_hdr *from,
163 const pjsip_to_hdr *to,
164 const pjsip_cid_hdr *call_id,
165 int cseq,
166 const pj_str_t *text);
167
168/**
169 * Create instant message, by specifying URL string for both From and To header.
170 *
171 * @param endpt Endpoint instance.
172 * @param target Target URL.
173 * @param from URL of the sender.
174 * @param to URL of the recipient.
175 * @param call_id Optionally specify Call-ID, or put NULL to make this
176 * function generate a unique Call-ID automatically.
177 * @param cseq Optionally specify CSeq, or put -1 to make this function
178 * generate a random CSeq.
179 * @param text Optionally specify "text/plain" message body, or put NULL
180 * if application wants to put body other than "text/plain"
181 * manually.
182 *
183 * @return SIP transmit data buffer, which reference count has been
184 * set to 1.
185 */
186PJ_DECL(pjsip_tx_data*) pjsip_messaging_create_msg( pjsip_endpoint *endpt,
187 const pj_str_t *target,
188 const pj_str_t *from,
189 const pj_str_t *to,
190 const pj_str_t *call_id,
191 int cseq,
192 const pj_str_t *text);
193
194/**
195 * Send the instant message transmit buffer and attach a callback to be called
196 * when the request has received a final response. This function will decrement
197 * the transmit buffer's reference counter, and when the reference counter
198 * reach zero, the buffer will be deleted. As long as the function does not
199 * increment the buffer's reference counter between creating the request and
200 * calling this function, the buffer will always be deleted regardless whether
201 * the sending was failed or succeeded.
202 *
203 * @param endpt Endpoint instance.
204 * @param tdata Transmit data buffer.
205 * @param token Token to be associated with the SIP transaction which sends
206 * this request.
207 * @param cb The callback to be called when the SIP request has received
208 * a final response from destination.
209 *
210 * @return Zero if the transaction was started successfully. Note that
211 * this doesn't mean the request has been received successfully
212 * by remote recipient.
213 */
214PJ_DECL(pj_status_t) pjsip_messaging_send_msg( pjsip_endpoint *endpt,
215 pjsip_tx_data *tdata,
216 void *token,
217 pjsip_messaging_cb cb );
218
219/**
220 * Create an instant messaging session, which can conveniently be used to send
221 * multiple instant messages to the same recipient.
222 *
223 * @param endpt Endpoint instance.
224 * @param from URI of sender. The function will add a unique tag parameter
225 * to this URI in the From header.
226 * @param to URI of recipient.
227 *
228 * @return Messaging session.
229 */
230PJ_DECL(pjsip_messaging_session*)
231pjsip_messaging_create_session( pjsip_endpoint *endpt,
232 const pj_str_t *from,
233 const pj_str_t *to );
234
235/**
236 * Create an instant message using instant messaging session, and optionally
237 * attach a text message.
238 *
239 * @param ses The instant messaging session.
240 * @param text Optional "text/plain" message to be attached as the
241 * message body. If this parameter is NULL, then no message
242 * body will be created, and application can attach any
243 * type of message body before the request is sent.
244 *
245 * @return SIP transmit data buffer, which reference counter has been
246 * set to 1.
247 */
248PJ_DECL(pjsip_tx_data*)
249pjsip_messaging_session_create_msg( pjsip_messaging_session *ses,
250 const pj_str_t *text );
251
252/**
253 * Destroy an instant messaging session.
254 *
255 * @param ses The instant messaging session.
256 *
257 * @return Zero on successfull.
258 */
259PJ_DECL(pj_status_t)
260pjsip_messaging_destroy_session( pjsip_messaging_session *ses );
261
262/**
263 * @}
264 */
265
266PJ_END_DECL
267
268#endif