5d9e44fc1fc19c72b08bced44a52fb853f7927df
[asterisk/asterisk.git] / res / res_pjsip_messaging.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Kevin Harwell <kharwell@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_session</depend>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include "pjsua-lib/pjsua.h"
29
30 #include "asterisk/message.h"
31 #include "asterisk/module.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/res_pjsip.h"
34 #include "asterisk/res_pjsip_session.h"
35
36 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
37
38 #define MAX_HDR_SIZE 512
39 #define MAX_BODY_SIZE 1024
40 #define MAX_EXTEN_SIZE 256
41 #define MAX_USER_SIZE 128
42
43 /*!
44  * \internal
45  * \brief Determine where in the dialplan a call should go
46  *
47  * \details This uses the username in the request URI to try to match
48  * an extension in an endpoint's context in order to route the call.
49  *
50  * \param rdata The SIP request
51  * \param context The context to use
52  * \param exten The extension to use
53  */
54 static enum pjsip_status_code get_destination(const pjsip_rx_data *rdata, const char *context, char *exten)
55 {
56         pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
57         pjsip_sip_uri *sip_ruri;
58
59         if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
60                 return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
61         }
62
63         sip_ruri = pjsip_uri_get_uri(ruri);
64         ast_copy_pj_str(exten, &sip_ruri->user, MAX_EXTEN_SIZE);
65
66         if (ast_exists_extension(NULL, context, exten, 1, NULL)) {
67                 return PJSIP_SC_OK;
68         }
69         return PJSIP_SC_NOT_FOUND;
70 }
71
72 /*!
73  * \internal
74  * \brief Checks to make sure the request has the correct content type.
75  *
76  * \details This module supports the following media types: "text/plain".
77  * Return unsupported otherwise.
78  *
79  * \param rdata The SIP request
80  */
81 static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
82 {
83         if (ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
84                                     "text",
85                                     "plain")) {
86                 return PJSIP_SC_OK;
87         } else {
88                 return PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
89         }
90 }
91
92 /*!
93  * \internal
94  * \brief Puts pointer past 'sip[s]:' string that should be at the
95  * front of the given 'fromto' parameter
96  *
97  * \param fromto 'From' or 'To' field containing 'sip:'
98  */
99 static const char* skip_sip(const char *fromto)
100 {
101         const char *p;
102
103         /* need to be one past 'sip:' or 'sips:' */
104         if (!(p = strstr(fromto, "sip"))) {
105                 return fromto;
106         }
107
108         p += 3;
109         if (*p == 's') {
110                 ++p;
111         }
112         return ++p;
113 }
114
115 /*!
116  * \internal
117  * \brief Retrieves an endpoint if specified in the given 'fromto'
118  *
119  * Expects the given 'fromto' to be in one of the following formats:
120  *      sip[s]:endpoint[/aor]
121  *      sip[s]:endpoint[/uri]
122  *
123  * If an optional aor is given it will try to find an associated uri
124  * to return.  If an optional uri is given then that will be returned,
125  * otherwise uri will be NULL.
126  *
127  * \param fromto 'From' or 'To' field with possible endpoint
128  * \param uri Optional uri to return
129  */
130 static struct ast_sip_endpoint* get_endpoint(const char *fromto, char **uri)
131 {
132         const char *name = skip_sip(fromto);
133         struct ast_sip_endpoint* endpoint;
134         struct ast_sip_aor *aor;
135
136         if ((*uri = strchr(name, '/'))) {
137                 *(*uri)++ = '\0';
138         } else if ((*uri = strchr(name, '@'))) {
139                 *(*uri) = '\0';
140         }
141
142         /* endpoint is required */
143         if (ast_strlen_zero(name)) {
144                 return NULL;
145         }
146
147         if (!(endpoint = ast_sorcery_retrieve_by_id(
148                       ast_sip_get_sorcery(), "endpoint", name))) {
149                 return NULL;
150         }
151
152         if (*uri && (aor = ast_sip_location_retrieve_aor(*uri))) {
153                 *uri = (char*)ast_sip_location_retrieve_first_aor_contact(aor)->uri;
154         }
155
156         return endpoint;
157 }
158
159 /*!
160  * \internal
161  * \brief Updates fields in an outgoing 'From' header.
162  *
163  * \param tdata The outgoing message data structure
164  * \param from Info to potentially copy into the 'From' header
165  */
166 static void update_from(pjsip_tx_data *tdata, const char *from)
167 {
168         pjsip_name_addr *from_name_addr;
169         pjsip_sip_uri *from_uri;
170         pjsip_uri *parsed;
171         char *uri;
172
173         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
174
175         if (ast_strlen_zero(from)) {
176                 return;
177         }
178
179         if (!(endpoint = get_endpoint(from, &uri))) {
180                 return;
181         }
182
183         if (ast_strlen_zero(uri)) {
184                 /* if no aor/uri was specified get one from the endpoint */
185                 uri = (char*)ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors)->uri;
186         }
187
188         /* get current 'from' hdr & uri - going to overwrite some fields */
189         from_name_addr = (pjsip_name_addr *)PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
190         from_uri = pjsip_uri_get_uri(from_name_addr);
191
192         /* check to see if uri is in 'name <sip:user@domain>' format */
193         if ((parsed = pjsip_parse_uri(tdata->pool, uri, strlen(uri), PJSIP_PARSE_URI_AS_NAMEADDR))) {
194                 pjsip_name_addr *name_addr = (pjsip_name_addr *)parsed;
195                 pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(name_addr->uri);
196
197                 pj_strdup(tdata->pool, &from_name_addr->display, &name_addr->display);
198                 pj_strdup(tdata->pool, &from_uri->user, &sip_uri->user);
199                 pj_strdup(tdata->pool, &from_uri->host, &sip_uri->host);
200                 from_uri->port = sip_uri->port;
201         } else {
202                 /* assume it is 'user[@domain]' format */
203                 char *domain = strchr(uri, '@');
204                 if (domain) {
205                         *domain++ = '\0';
206                         pj_strdup2(tdata->pool, &from_uri->host, domain);
207                 }
208                 pj_strdup2(tdata->pool, &from_uri->user, uri);
209         }
210 }
211
212 static char *scheme_sip_to_pjsip(pjsip_rx_data *rdata, char *buf, unsigned int size)
213 {
214         char *res = buf;
215         pjsip_name_addr *name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
216         pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(name_addr->uri);
217
218         const pj_str_t *scheme = pjsip_uri_get_scheme(rdata->msg_info.to->uri);
219         size_t size_scheme = pj_strlen(scheme);
220         size_t size_user = pj_strlen(&sip_uri->user);
221         size_t size_host = pj_strlen(&sip_uri->host);
222
223         /* 5 = count of 'p' 'j' ':' '@' '\0' */
224         if (size < size_scheme + size_user + size_host + 5) {
225                 /* won't fit */
226                 ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri "
227                         "too large for given buffer\n");
228                 return NULL;
229         }
230
231         *buf++ = 'p';
232         *buf++ = 'j';
233
234         memcpy(buf, pj_strbuf(scheme), size_scheme);
235         buf += size_scheme;
236         *buf++ = ':';
237
238         memcpy(buf, pj_strbuf(&sip_uri->user), size_user);
239         buf += size_user;
240         *buf++ = '@';
241
242         memcpy(buf, pj_strbuf(&sip_uri->host), size_host);
243         buf += size_host;
244         *buf = '\0';
245
246         return res;
247 }
248
249 static char *scheme_pjsip_to_sip(const char *uri)
250 {
251         ast_assert(!strncmp(uri, "pjsip", 5));
252         return ast_strdup(uri + 2);
253 }
254
255 /*!
256  * \internal
257  * \brief Checks if the given msg var name should be blocked.
258  *
259  * \details Some headers are not allowed to be overriden by the user.
260  *  Determine if the given var header name from the user is blocked for
261  *  an outgoing MESSAGE.
262  *
263  * \param name name of header to see if it is blocked.
264  *
265  * \retval TRUE if the given header is blocked.
266  */
267 static int is_msg_var_blocked(const char *name)
268 {
269         int i;
270
271         /*
272          * Don't block Content-Type or Max-Forwards headers because the
273          * user can override them.
274          */
275         static const char *hdr[] = {
276                 "To",
277                 "From",
278                 "Via",
279                 "Route",
280                 "Contact",
281                 "Call-ID",
282                 "CSeq",
283                 "Allow",
284                 "Content-Length",
285                 "Request-URI",
286         };
287
288         for (i = 0; i < ARRAY_LEN(hdr); ++i) {
289                 if (!strcasecmp(name, hdr[i])) {
290                         /* Block addition of this header. */
291                         return 1;
292                 }
293         }
294         return 0;
295 }
296
297 /*!
298  * \internal
299  * \brief Copies any other msg vars over to the request headers.
300  *
301  * \param msg The msg structure to copy headers from
302  * \param tdata The SIP transmission data
303  */
304 static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
305 {
306         const char *name;
307         const char *value;
308         int max_forwards;
309
310         RAII_VAR(struct ast_msg_var_iterator *, i, ast_msg_var_iterator_init(msg), ast_msg_var_iterator_destroy);
311         while (ast_msg_var_iterator_next(msg, i, &name, &value)) {
312                 if (!strcasecmp(name, "Max-Forwards")) {
313                         /* Decrement Max-Forwards for SIP loop prevention. */
314                         if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
315                                 ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero.  MESSAGE not sent.\n");
316                                 return -1;
317                         }
318                         sprintf((char*)value, "%d", max_forwards);
319                         ast_sip_add_header(tdata, name, value);
320                 } else if (!strcasecmp(name, "To")) {
321                         char *to = scheme_pjsip_to_sip(value);
322                         if (to) {
323                                 ast_sip_add_header(tdata, name, to);
324                                 ast_free(to);
325                         }
326                 } else if (!is_msg_var_blocked(name)) {
327                         ast_sip_add_header(tdata, name, value);
328                 }
329                 ast_msg_var_unref_current(i);
330         }
331         return PJSIP_SC_OK;
332 }
333
334 /*!
335  * \internal
336  * \brief Copies any other request header data over to ast_msg structure.
337  *
338  * \param rdata The SIP request
339  * \param msg The msg structure to copy headers into
340  */
341 static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
342 {
343         char *c;
344         char buf[MAX_HDR_SIZE];
345         int res = 0;
346         pjsip_hdr *h = rdata->msg_info.msg->hdr.next;
347         pjsip_hdr *end= &rdata->msg_info.msg->hdr;
348
349         while (h != end) {
350                 if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) {
351                         buf[res] = '\0';
352                         if ((c = strchr(buf, ':'))) {
353                                 ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf)-(c-buf));
354                         }
355
356                         if ((res = ast_msg_set_var(msg, pj_strbuf(&h->name), buf)) != 0) {
357                                 break;
358                         }
359                 }
360                 h = h->next;
361         }
362         return 0;
363 }
364
365 /*!
366  * \internal
367  * \brief Prints the message body into the given char buffer.
368  *
369  * \details Copies body content from the received data into the given
370  * character buffer removing any extra carriage return/line feeds.
371  *
372  * \param rdata The SIP request
373  * \param buf Buffer to fill
374  * \param len The length of the buffer
375  */
376 static int print_body(pjsip_rx_data *rdata, char *buf, int len)
377 {
378         int res = rdata->msg_info.msg->body->print_body(
379                 rdata->msg_info.msg->body, buf, len);
380
381         if (res < 0) {
382                 return res;
383         }
384
385         /* remove any trailing carriage return/line feeds */
386         while (res > 0 && ((buf[--res] == '\r') || (buf[res] == '\n')));
387
388         buf[++res] = '\0';
389
390         return res;
391 }
392
393 /*!
394  * \internal
395  * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
396  *
397  * \details Attempts to fill in as much information as possible into the given
398  * msg structure copied from the given request data.
399  *
400  * \param rdata The SIP request
401  * \param msg The asterisk message structure to fill in.
402  */
403 static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
404 {
405
406 #define CHECK_RES(z_) do { if (z_) { ast_msg_destroy(msg); \
407                 return PJSIP_SC_INTERNAL_SERVER_ERROR; } } while (0)
408
409         int size;
410         char buf[MAX_BODY_SIZE];
411         pjsip_name_addr *name_addr;
412         const char *field;
413         pjsip_status_code code;
414         struct ast_sip_endpoint *endpt = ast_pjsip_rdata_get_endpoint(rdata);
415
416         /* make sure there is an appropriate context and extension*/
417         if ((code = get_destination(rdata, endpt->context, buf)) != PJSIP_SC_OK) {
418                 return code;
419         }
420
421         CHECK_RES(ast_msg_set_context(msg, "%s", endpt->context));
422         CHECK_RES(ast_msg_set_exten(msg, "%s", buf));
423
424         /* to header */
425         CHECK_RES(ast_msg_set_to(msg, "%s", scheme_sip_to_pjsip(rdata, buf, sizeof(buf))));
426
427         /* from header */
428         name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
429         if ((size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf)-1)) > 0) {
430                 buf[size] = '\0';
431                 CHECK_RES(ast_msg_set_from(msg, "%s", buf));
432         }
433
434         /* contact header */
435         if ((size = pjsip_hdr_print_on(pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL), buf, sizeof(buf)-1)) > 0) {
436                 buf[size] = '\0';
437                 CHECK_RES(ast_msg_set_var(msg, "SIP_FULLCONTACT", buf));
438         }
439
440         /* receive address */
441         field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf)-1, 1);
442         CHECK_RES(ast_msg_set_var(msg, "SIP_RECVADDR", field));
443
444         /* body */
445         if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
446                 CHECK_RES(ast_msg_set_body(msg, "%s", buf));
447         }
448
449         /* endpoint name */
450         if (endpt->id.self.name.valid) {
451                 CHECK_RES(ast_msg_set_var(msg, "SIP_PEERNAME", endpt->id.self.name.str));
452         }
453
454         CHECK_RES(headers_to_vars(rdata, msg));
455
456         return PJSIP_SC_OK;
457 }
458
459 struct msg_data {
460         struct ast_msg *msg;
461         char *to;
462         char *from;
463 };
464
465 static void msg_data_destroy(void *obj)
466 {
467         struct msg_data *mdata = obj;
468
469         ast_free(mdata->from);
470         ast_free(mdata->to);
471
472         ast_msg_destroy(mdata->msg);
473 }
474
475 static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
476 {
477         char *tag;
478         struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
479
480         if (!mdata) {
481                 return NULL;
482         }
483
484         /* typecast to suppress const warning */
485         mdata->msg = ast_msg_ref((struct ast_msg*)msg);
486
487         mdata->to = scheme_pjsip_to_sip(to);
488         mdata->from = ast_strdup(from);
489
490         /* sometimes from can still contain the tag at this point, so remove it */
491         if ((tag = strchr(mdata->from, ';'))) {
492                 *tag = '\0';
493         }
494
495         return mdata;
496 }
497
498 static int msg_send(void *data)
499 {
500         RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);
501
502         const struct ast_sip_body body = {
503                 .type = "text",
504                 .subtype = "plain",
505                 .body_text = ast_msg_get_body(mdata->msg)
506         };
507
508         pjsip_tx_data *tdata;
509         char *uri;
510
511         RAII_VAR(struct ast_sip_endpoint *, endpoint, get_endpoint(
512                          mdata->to, &uri), ao2_cleanup);
513         if (!endpoint) {
514                 ast_log(LOG_ERROR, "SIP MESSAGE - Endpoint not found in %s\n", mdata->to);
515                 return -1;
516         }
517
518         if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, &tdata)) {
519                 ast_log(LOG_ERROR, "SIP MESSAGE - Could not create request\n");
520                 return -1;
521         }
522
523         if (ast_sip_add_body(tdata, &body)) {
524                 pjsip_tx_data_dec_ref(tdata);
525                 ast_log(LOG_ERROR, "SIP MESSAGE - Could not add body to request\n");
526                 return -1;
527         }
528
529         update_from(tdata, mdata->from);
530         vars_to_headers(mdata->msg, tdata);
531         if (ast_sip_send_request(tdata, NULL, endpoint)) {
532                 ast_log(LOG_ERROR, "SIP MESSAGE - Could not send request\n");
533                 return -1;
534         }
535
536         return PJ_SUCCESS;
537 }
538
539 static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *from)
540 {
541         struct msg_data *mdata;
542
543         if (ast_strlen_zero(to)) {
544                 ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI  must be specified\n");
545                 return -1;
546         }
547
548         if (!(mdata = msg_data_create(msg, to, from)) ||
549             ast_sip_push_task(NULL, msg_send, mdata)) {
550                 ao2_ref(mdata, -1);
551                 return -1;
552         }
553         return 0;
554 }
555
556 static const struct ast_msg_tech msg_tech = {
557         .name = "pjsip",
558         .msg_send = sip_msg_send,
559 };
560
561 static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
562                                  pjsip_dialog *dlg, pjsip_transaction *tsx)
563 {
564         pjsip_tx_data *tdata;
565         pj_status_t status;
566         pjsip_response_addr res_addr;
567
568         pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
569
570         status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata);
571         if (status != PJ_SUCCESS) {
572                 ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
573                 return status;
574         }
575
576         if (dlg && tsx) {
577                 status = pjsip_dlg_send_response(dlg, tsx, tdata);
578         } else {
579                 /* Get where to send request. */
580                 status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
581                 if (status != PJ_SUCCESS) {
582                         ast_log(LOG_ERROR, "Unable to get response address (%d)\n", status);
583                         return status;
584                 }
585                 status = pjsip_endpt_send_response(endpt, &res_addr, tdata, NULL, NULL);
586         }
587
588         if (status != PJ_SUCCESS) {
589                 ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
590         }
591
592         return status;
593 }
594
595 static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
596 {
597         enum pjsip_status_code code;
598         struct ast_msg *msg;
599
600         /* if not a MESSAGE, don't handle */
601         if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
602                 return PJ_FALSE;
603         }
604
605         msg = ast_msg_alloc();
606         if (!msg) {
607                 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
608                 return PJ_TRUE;
609         }
610
611         if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
612                 send_response(rdata, code, NULL, NULL);
613                 return PJ_TRUE;
614         }
615
616         if ((code = rx_data_to_ast_msg(rdata, msg)) == PJSIP_SC_OK) {
617                 /* send it to the dialplan */
618                 ast_msg_queue(msg);
619                 code = PJSIP_SC_ACCEPTED;
620         }
621
622         send_response(rdata, code, NULL, NULL);
623         return PJ_TRUE;
624 }
625
626 static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
627 {
628         char buf[MAX_BODY_SIZE];
629         enum pjsip_status_code code;
630         struct ast_frame f;
631
632         pjsip_dialog *dlg = session->inv_session->dlg;
633         pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
634
635         if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
636                 send_response(rdata, code, dlg, tsx);
637                 return 0;
638         }
639
640         if (print_body(rdata, buf, sizeof(buf)-1) < 1) {
641                 /* invalid body size */
642                 return 0;
643         }
644
645         memset(&f, 0, sizeof(f));
646         f.frametype = AST_FRAME_TEXT;
647         f.subclass.integer = 0;
648         f.offset = 0;
649         f.data.ptr = buf;
650         f.datalen = strlen(buf) + 1;
651         ast_queue_frame(session->channel, &f);
652
653         send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
654         return 0;
655 }
656
657 static struct ast_sip_session_supplement messaging_supplement = {
658         .method = "MESSAGE",
659         .incoming_request = incoming_in_dialog_request
660 };
661
662 static pjsip_module messaging_module = {
663         .name = {"Messaging Module", 16},
664         .id = -1,
665         .priority = PJSIP_MOD_PRIORITY_APPLICATION,
666         .on_rx_request = module_on_rx_request,
667 };
668
669 static int load_module(void)
670 {
671         if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
672                 return AST_MODULE_LOAD_DECLINE;
673         }
674
675         if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
676                                        NULL, PJSIP_H_ALLOW, NULL, 1,
677                                        &pjsip_message_method.name) != PJ_SUCCESS) {
678
679                 ast_sip_unregister_service(&messaging_module);
680                 return AST_MODULE_LOAD_DECLINE;
681         }
682
683         if (ast_msg_tech_register(&msg_tech)) {
684                 ast_sip_unregister_service(&messaging_module);
685                 return AST_MODULE_LOAD_DECLINE;
686         }
687
688         ast_sip_session_register_supplement(&messaging_supplement);
689         return AST_MODULE_LOAD_SUCCESS;
690 }
691
692 static int unload_module(void)
693 {
694         ast_sip_session_unregister_supplement(&messaging_supplement);
695         ast_msg_tech_unregister(&msg_tech);
696         ast_sip_unregister_service(&messaging_module);
697         return 0;
698 }
699
700 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
701                 .load = load_module,
702                 .unload = unload_module,
703                 .load_pri = AST_MODPRI_APP_DEPEND,
704                );