loader: Add dependency fields to module structures.
[asterisk/asterisk.git] / res / res_pjsip_exten_state.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_pubsub</depend>
23         <depend>res_pjsip_outbound_publish</depend>
24         <support_level>core</support_level>
25  ***/
26
27 #include "asterisk.h"
28
29 #include <regex.h>
30
31 #include <pjsip.h>
32 #include <pjsip_simple.h>
33 #include <pjlib.h>
34
35 #include "asterisk/res_pjsip.h"
36 #include "asterisk/res_pjsip_outbound_publish.h"
37 #include "asterisk/res_pjsip_pubsub.h"
38 #include "asterisk/res_pjsip_body_generator_types.h"
39 #include "asterisk/module.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/astobj2.h"
42 #include "asterisk/sorcery.h"
43 #include "asterisk/app.h"
44 #include "asterisk/taskprocessor.h"
45
46 #define BODY_SIZE 1024
47 #define EVENT_TYPE_SIZE 50
48
49 /*!
50  * \brief The number of buckets to use for storing publishers
51  */
52 #define PUBLISHER_BUCKETS 31
53
54 /*!
55  * \brief Container of active outbound extension state publishers
56  */
57 static struct ao2_container *publishers;
58
59 /*! Serializer for outbound extension state publishing. */
60 static struct ast_taskprocessor *publish_exten_state_serializer;
61
62 /*!
63  * \brief A subscription for extension state
64  *
65  * This structure acts as the owner for the underlying SIP subscription. It
66  * also keeps a pointer to an associated "provider" so when a state changes
67  * a notify data creator is quickly accessible.
68  */
69 struct exten_state_subscription {
70         /*! Watcher id when registering for extension state changes */
71         int id;
72         /*! The SIP subscription */
73         struct ast_sip_subscription *sip_sub;
74         /*! The serializer to use for notifications */
75         struct ast_taskprocessor *serializer;
76         /*! Context in which subscription looks for updates */
77         char context[AST_MAX_CONTEXT];
78         /*! Extension within the context to receive updates from */
79         char exten[AST_MAX_EXTENSION];
80         /*! The subscription's user agent */
81         char *user_agent;
82         /*! The last known extension state */
83         enum ast_extension_states last_exten_state;
84         /*! The last known presence state */
85         enum ast_presence_state last_presence_state;
86 };
87
88 /*!
89  * \brief An extension state publisher
90  *
91  */
92 struct exten_state_publisher {
93         /*! Regular expression for context filtering */
94         regex_t context_regex;
95         /*! Regular expression for extension filtering */
96         regex_t exten_regex;
97         /*! Publish client to use for sending publish messages */
98         struct ast_sip_outbound_publish_client *client;
99         /*! Datastores container to hold persistent information */
100         struct ao2_container *datastores;
101         /*! Whether context filtering is active */
102         unsigned int context_filter;
103         /*! Whether extension filtering is active */
104         unsigned int exten_filter;
105         /*! The body type to use for this publisher - stored after the name */
106         char *body_type;
107         /*! The body subtype to use for this publisher - stored after the body type */
108         char *body_subtype;
109         /*! The name of this publisher */
110         char name[0];
111 };
112
113 #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
114 #define DEFAULT_DIALOG_BODY "application/dialog-info+xml"
115
116 static void subscription_shutdown(struct ast_sip_subscription *sub);
117 static int new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource);
118 static int subscription_established(struct ast_sip_subscription *sub);
119 static void *get_notify_data(struct ast_sip_subscription *sub);
120 static void to_ami(struct ast_sip_subscription *sub,
121                    struct ast_str **buf);
122 static int publisher_start(struct ast_sip_outbound_publish *configuration,
123                            struct ast_sip_outbound_publish_client *client);
124 static int publisher_stop(struct ast_sip_outbound_publish_client *client);
125
126 struct ast_sip_notifier presence_notifier = {
127         .default_accept = DEFAULT_PRESENCE_BODY,
128         .new_subscribe = new_subscribe,
129         .subscription_established = subscription_established,
130         .get_notify_data = get_notify_data,
131 };
132
133 struct ast_sip_notifier dialog_notifier = {
134         .default_accept = DEFAULT_DIALOG_BODY,
135         .new_subscribe = new_subscribe,
136         .subscription_established = subscription_established,
137         .get_notify_data = get_notify_data,
138 };
139
140 struct ast_sip_subscription_handler presence_handler = {
141         .event_name = "presence",
142         .body_type = AST_SIP_EXTEN_STATE_DATA,
143         .accept = { DEFAULT_PRESENCE_BODY, },
144         .subscription_shutdown = subscription_shutdown,
145         .to_ami = to_ami,
146         .notifier = &presence_notifier,
147 };
148
149 struct ast_sip_event_publisher_handler presence_publisher = {
150         .event_name = "presence",
151         .start_publishing = publisher_start,
152         .stop_publishing = publisher_stop,
153 };
154
155 struct ast_sip_subscription_handler dialog_handler = {
156         .event_name = "dialog",
157         .body_type = AST_SIP_EXTEN_STATE_DATA,
158         .accept = { DEFAULT_DIALOG_BODY, },
159         .subscription_shutdown = subscription_shutdown,
160         .to_ami = to_ami,
161         .notifier = &dialog_notifier,
162 };
163
164 struct ast_sip_event_publisher_handler dialog_publisher = {
165         .event_name = "dialog",
166         .start_publishing = publisher_start,
167         .stop_publishing = publisher_stop,
168 };
169
170 static void exten_state_subscription_destructor(void *obj)
171 {
172         struct exten_state_subscription *sub = obj;
173
174         ast_free(sub->user_agent);
175         ast_sip_subscription_destroy(sub->sip_sub);
176         ast_taskprocessor_unreference(sub->serializer);
177 }
178
179 static char *get_user_agent(const struct ast_sip_subscription *sip_sub)
180 {
181         size_t size;
182         char *user_agent = NULL;
183         pjsip_user_agent_hdr *user_agent_hdr = ast_sip_subscription_get_header(
184                         sip_sub, "User-Agent");
185
186         if (!user_agent_hdr) {
187                 return NULL;
188         }
189
190         size = pj_strlen(&user_agent_hdr->hvalue) + 1;
191         user_agent = ast_malloc(size);
192         ast_copy_pj_str(user_agent, &user_agent_hdr->hvalue, size);
193         return ast_str_to_lower(user_agent);
194 }
195
196 /*!
197  * \internal
198  * \brief Initialize the last extension state to something outside
199  * its usual states.
200  */
201 #define INITIAL_LAST_EXTEN_STATE -3
202
203 /*!
204  * \internal
205  * \brief Allocates an exten_state_subscription object.
206  *
207  * Creates the underlying SIP subscription for the given request. First makes
208  * sure that there are registered handler and provider objects available.
209  */
210 static struct exten_state_subscription *exten_state_subscription_alloc(
211                 struct ast_sip_subscription *sip_sub, struct ast_sip_endpoint *endpoint)
212 {
213         struct exten_state_subscription * exten_state_sub;
214
215         exten_state_sub = ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor);
216         if (!exten_state_sub) {
217                 return NULL;
218         }
219
220         exten_state_sub->sip_sub = sip_sub;
221
222         /* We keep our own reference to the serializer as there is no guarantee in state_changed
223          * that the subscription tree is still valid when it is called. This can occur when
224          * the subscription is terminated at around the same time as the state_changed
225          * callback is invoked.
226          */
227         exten_state_sub->serializer = ao2_bump(ast_sip_subscription_get_serializer(sip_sub));
228         exten_state_sub->last_exten_state = INITIAL_LAST_EXTEN_STATE;
229         exten_state_sub->last_presence_state = AST_PRESENCE_NOT_SET;
230         exten_state_sub->user_agent = get_user_agent(sip_sub);
231         return exten_state_sub;
232 }
233
234 struct notify_task_data {
235         struct ast_sip_exten_state_data exten_state_data;
236         struct exten_state_subscription *exten_state_sub;
237         int terminate;
238 };
239
240 static void notify_task_data_destructor(void *obj)
241 {
242         struct notify_task_data *task_data = obj;
243
244         ao2_ref(task_data->exten_state_sub, -1);
245         ao2_cleanup(task_data->exten_state_data.device_state_info);
246         ast_free(task_data->exten_state_data.presence_subtype);
247         ast_free(task_data->exten_state_data.presence_message);
248         ast_free(task_data->exten_state_data.user_agent);
249 }
250
251 static struct notify_task_data *alloc_notify_task_data(const char *exten,
252         struct exten_state_subscription *exten_state_sub,
253         struct ast_state_cb_info *info)
254 {
255         struct notify_task_data *task_data =
256                 ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
257
258         if (!task_data) {
259                 ast_log(LOG_WARNING, "Unable to create notify task data\n");
260                 return NULL;
261         }
262
263         task_data->exten_state_sub = exten_state_sub;
264         task_data->exten_state_sub->last_exten_state = info->exten_state;
265         task_data->exten_state_sub->last_presence_state = info->presence_state;
266         ao2_ref(task_data->exten_state_sub, +1);
267
268         task_data->exten_state_data.exten = exten_state_sub->exten;
269         task_data->exten_state_data.exten_state = info->exten_state;
270         task_data->exten_state_data.presence_state = info->presence_state;
271         task_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
272         task_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
273         task_data->exten_state_data.user_agent = ast_strdup(exten_state_sub->user_agent);
274         task_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
275         task_data->exten_state_data.sub = exten_state_sub->sip_sub;
276         task_data->exten_state_data.datastores = ast_sip_subscription_get_datastores(exten_state_sub->sip_sub);
277
278         if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
279             (info->exten_state == AST_EXTENSION_REMOVED)) {
280                 ast_verb(2, "Watcher for hint %s %s\n", exten, info->exten_state
281                          == AST_EXTENSION_REMOVED ? "removed" : "deactivated");
282                 task_data->terminate = 1;
283         }
284
285         return task_data;
286 }
287
288 static int notify_task(void *obj)
289 {
290         RAII_VAR(struct notify_task_data *, task_data, obj, ao2_cleanup);
291         struct ast_sip_body_data data = {
292                 .body_type = AST_SIP_EXTEN_STATE_DATA,
293                 .body_data = &task_data->exten_state_data,
294         };
295
296         /* Terminated subscriptions are no longer associated with a valid tree, and sending
297          * NOTIFY messages on a subscription which has already been terminated won't work.
298          */
299         if (ast_sip_subscription_is_terminated(task_data->exten_state_sub->sip_sub)) {
300                 return 0;
301         }
302
303         /* All access to the subscription must occur within a task executed within its serializer */
304         ast_sip_subscription_get_local_uri(task_data->exten_state_sub->sip_sub,
305                         task_data->exten_state_data.local, sizeof(task_data->exten_state_data.local));
306         ast_sip_subscription_get_remote_uri(task_data->exten_state_sub->sip_sub,
307                         task_data->exten_state_data.remote, sizeof(task_data->exten_state_data.remote));
308
309         /* Pool allocation has to happen here so that we allocate within a PJLIB thread */
310         task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
311                         "exten_state", 1024, 1024);
312         if (!task_data->exten_state_data.pool) {
313                 return -1;
314         }
315
316         task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
317         task_data->exten_state_data.datastores = ast_sip_subscription_get_datastores(task_data->exten_state_sub->sip_sub);
318
319         ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &data,
320                         task_data->terminate);
321
322         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
323                         task_data->exten_state_data.pool);
324         return 0;
325 }
326
327 /*!
328  * \internal
329  * \brief Callback for exten/device state changes.
330  *
331  * Upon state change, send the appropriate notification to the subscriber.
332  */
333 static int state_changed(const char *context, const char *exten,
334         struct ast_state_cb_info *info, void *data)
335 {
336         struct notify_task_data *task_data;
337         struct exten_state_subscription *exten_state_sub = data;
338
339         if (!(task_data = alloc_notify_task_data(exten, exten_state_sub, info))) {
340                 return -1;
341         }
342
343         /* safe to push this async since we copy the data from info and
344            add a ref for the device state info */
345         if (ast_sip_push_task(task_data->exten_state_sub->serializer, notify_task,
346                 task_data)) {
347                 ao2_cleanup(task_data);
348                 return -1;
349         }
350         return 0;
351 }
352
353 static void state_changed_destroy(int id, void *data)
354 {
355         struct exten_state_subscription *exten_state_sub = data;
356         ao2_cleanup(exten_state_sub);
357 }
358
359 static struct ast_datastore_info ds_info = { };
360 static const char ds_name[] = "exten state datastore";
361
362 /*!
363  * \internal
364  * \brief Add a datastore for exten exten_state_subscription.
365  *
366  * Adds the exten_state_subscription wrapper object to a datastore so it can be retrieved
367  * later based upon its association with the ast_sip_subscription.
368  */
369 static int add_datastore(struct exten_state_subscription *exten_state_sub)
370 {
371         RAII_VAR(struct ast_datastore *, datastore,
372                  ast_sip_subscription_alloc_datastore(&ds_info, ds_name), ao2_cleanup);
373
374         if (!datastore) {
375                 return -1;
376         }
377
378         datastore->data = exten_state_sub;
379         ast_sip_subscription_add_datastore(exten_state_sub->sip_sub, datastore);
380         ao2_ref(exten_state_sub, +1);
381         return 0;
382 }
383
384 /*!
385  * \internal
386  * \brief Get the exten_state_subscription object associated with the given
387  * ast_sip_subscription in the datastore.
388  */
389 static struct exten_state_subscription *get_exten_state_sub(
390         struct ast_sip_subscription *sub)
391 {
392         RAII_VAR(struct ast_datastore *, datastore,
393                  ast_sip_subscription_get_datastore(sub, ds_name), ao2_cleanup);
394
395         return datastore ? datastore->data : NULL;
396 }
397
398 static void subscription_shutdown(struct ast_sip_subscription *sub)
399 {
400         struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
401
402         if (!exten_state_sub) {
403                 return;
404         }
405
406         ast_extension_state_del(exten_state_sub->id, state_changed);
407         ast_sip_subscription_remove_datastore(exten_state_sub->sip_sub, ds_name);
408         /* remove data store reference */
409         ao2_cleanup(exten_state_sub);
410 }
411
412 static int new_subscribe(struct ast_sip_endpoint *endpoint,
413                 const char *resource)
414 {
415         const char *context = S_OR(endpoint->subscription.context, endpoint->context);
416
417         if (!ast_exists_extension(NULL, context, resource, PRIORITY_HINT, NULL)) {
418                 ast_log(LOG_NOTICE, "Endpoint '%s' state subscription failed: "
419                         "Extension '%s' does not exist in context '%s' or has no associated hint\n",
420                         ast_sorcery_object_get_id(endpoint), resource, context);
421                 return 404;
422         }
423
424         return 200;
425 }
426
427 static int subscription_established(struct ast_sip_subscription *sip_sub)
428 {
429         struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
430         const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
431         struct exten_state_subscription *exten_state_sub;
432
433         if (!(exten_state_sub = exten_state_subscription_alloc(sip_sub, endpoint))) {
434                 ao2_cleanup(endpoint);
435                 return -1;
436         }
437
438         ast_copy_string(exten_state_sub->context,
439                 S_OR(endpoint->subscription.context, endpoint->context),
440                 sizeof(exten_state_sub->context));
441         ast_copy_string(exten_state_sub->exten, resource, sizeof(exten_state_sub->exten));
442
443         if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
444                      exten_state_sub->context, exten_state_sub->exten,
445                      state_changed, state_changed_destroy, exten_state_sub)) < 0) {
446                 ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n",
447                         ast_sorcery_object_get_id(endpoint), exten_state_sub->exten,
448                         exten_state_sub->context);
449                 ao2_cleanup(endpoint);
450                 ao2_cleanup(exten_state_sub);
451                 return -1;
452         }
453
454         /* Go ahead and cleanup the endpoint since we don't need it anymore */
455         ao2_cleanup(endpoint);
456
457         /* bump the ref since ast_extension_state_add holds a reference */
458         ao2_ref(exten_state_sub, +1);
459
460         if (add_datastore(exten_state_sub)) {
461                 ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
462                 ao2_cleanup(exten_state_sub);
463                 return -1;
464         }
465
466         ao2_cleanup(exten_state_sub);
467         return 0;
468 }
469
470 static void exten_state_data_destructor(void *obj)
471 {
472         struct ast_sip_exten_state_data *exten_state_data = obj;
473
474         ao2_cleanup(exten_state_data->device_state_info);
475         ast_free(exten_state_data->presence_subtype);
476         ast_free(exten_state_data->presence_message);
477         if (exten_state_data->pool) {
478                 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data->pool);
479         }
480 }
481
482 static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_subscription *sip_sub,
483                 struct exten_state_subscription *exten_state_sub)
484 {
485         struct ast_sip_exten_state_data *exten_state_data;
486         char *subtype = NULL;
487         char *message = NULL;
488         int presence_state;
489
490         exten_state_data = ao2_alloc(sizeof(*exten_state_data), exten_state_data_destructor);
491         if (!exten_state_data) {
492                 return NULL;
493         }
494
495         exten_state_data->exten = exten_state_sub->exten;
496         presence_state = ast_hint_presence_state(NULL, exten_state_sub->context, exten_state_sub->exten, &subtype, &message);
497         if (presence_state  == -1 || presence_state == AST_PRESENCE_INVALID) {
498                 ao2_cleanup(exten_state_data);
499                 return NULL;
500         }
501         exten_state_data->presence_state = presence_state;
502         exten_state_data->presence_subtype = subtype;
503         exten_state_data->presence_message = message;
504         exten_state_data->user_agent = exten_state_sub->user_agent;
505         ast_sip_subscription_get_local_uri(sip_sub, exten_state_data->local,
506                         sizeof(exten_state_data->local));
507         ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
508                         sizeof(exten_state_data->remote));
509         exten_state_data->sub = sip_sub;
510         exten_state_data->datastores = ast_sip_subscription_get_datastores(sip_sub);
511
512         exten_state_data->exten_state = ast_extension_state_extended(
513                         NULL, exten_state_sub->context, exten_state_sub->exten,
514                         &exten_state_data->device_state_info);
515         if (exten_state_data->exten_state < 0) {
516                 ao2_cleanup(exten_state_data);
517                 return NULL;
518         }
519
520         exten_state_data->pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
521                         "exten_state", 1024, 1024);
522         if (!exten_state_data->pool) {
523                 ao2_cleanup(exten_state_data);
524                 return NULL;
525         }
526
527         return exten_state_data;
528 }
529
530 static void *get_notify_data(struct ast_sip_subscription *sub)
531 {
532         struct exten_state_subscription *exten_state_sub;
533
534         exten_state_sub = get_exten_state_sub(sub);
535         if (!exten_state_sub) {
536                 return NULL;
537         }
538
539         return exten_state_data_alloc(sub, exten_state_sub);
540 }
541
542 static void to_ami(struct ast_sip_subscription *sub,
543                    struct ast_str **buf)
544 {
545         struct exten_state_subscription *exten_state_sub =
546                 get_exten_state_sub(sub);
547
548         if (!exten_state_sub) {
549                 return;
550         }
551
552         ast_str_append(buf, 0, "SubscriptionType: extension_state\r\n"
553                        "Extension: %s\r\nExtensionStates: %s\r\n",
554                        exten_state_sub->exten, ast_extension_state2str(
555                                exten_state_sub->last_exten_state));
556 }
557
558 struct exten_state_pub_data {
559         /*! Publishers needing state update */
560         AST_VECTOR(name, struct exten_state_publisher *) pubs;
561         /*! Body generator state data */
562         struct ast_sip_exten_state_data exten_state_data;
563 };
564
565 static void exten_state_pub_data_destroy(struct exten_state_pub_data *doomed)
566 {
567         if (!doomed) {
568                 return;
569         }
570
571         ast_free((void *) doomed->exten_state_data.exten);
572         ast_free(doomed->exten_state_data.presence_subtype);
573         ast_free(doomed->exten_state_data.presence_message);
574         ao2_cleanup(doomed->exten_state_data.device_state_info);
575
576         AST_VECTOR_CALLBACK_VOID(&doomed->pubs, ao2_ref, -1);
577         AST_VECTOR_FREE(&doomed->pubs);
578
579         ast_free(doomed);
580 }
581
582 static struct exten_state_pub_data *exten_state_pub_data_alloc(const char *exten, struct ast_state_cb_info *info)
583 {
584         struct exten_state_pub_data *pub_data;
585
586         pub_data = ast_calloc(1, sizeof(*pub_data));
587         if (!pub_data) {
588                 return NULL;
589         }
590
591         if (AST_VECTOR_INIT(&pub_data->pubs, ao2_container_count(publishers))) {
592                 exten_state_pub_data_destroy(pub_data);
593                 return NULL;
594         }
595
596         /* Save off currently known information for the body generators. */
597         pub_data->exten_state_data.exten = ast_strdup(exten);
598         pub_data->exten_state_data.exten_state = info->exten_state;
599         pub_data->exten_state_data.presence_state = info->presence_state;
600         pub_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
601         pub_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
602         pub_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
603         if (!pub_data->exten_state_data.exten
604                 || !pub_data->exten_state_data.presence_subtype
605                 || !pub_data->exten_state_data.presence_message) {
606                 exten_state_pub_data_destroy(pub_data);
607                 return NULL;
608         }
609         return pub_data;
610 }
611
612 /*!
613  * \internal
614  * \brief Create exten state PUBLISH messages under PJSIP thread.
615  * \since 14.0.0
616  *
617  * \return 0
618  */
619 static int exten_state_publisher_cb(void *data)
620 {
621         struct exten_state_pub_data *pub_data = data;
622         struct exten_state_publisher *publisher;
623         size_t idx;
624         struct ast_str *body_text;
625         pj_pool_t *pool;
626         struct ast_sip_body_data gen_data = {
627                 .body_type = AST_SIP_EXTEN_STATE_DATA,
628                 .body_data = &pub_data->exten_state_data,
629         };
630         struct ast_sip_body body;
631
632         body_text = ast_str_create(64);
633         if (!body_text) {
634                 exten_state_pub_data_destroy(pub_data);
635                 return 0;
636         }
637
638         /* Need a PJSIP memory pool to generate the bodies. */
639         pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "pub_state_body",
640                 1024, 1024);
641         if (!pool) {
642                 ast_log(LOG_WARNING, "Exten state publishing unable to create memory pool\n");
643                 exten_state_pub_data_destroy(pub_data);
644                 ast_free(body_text);
645                 return 0;
646         }
647         pub_data->exten_state_data.pool = pool;
648
649         for (idx = 0; idx < AST_VECTOR_SIZE(&pub_data->pubs); ++idx) {
650                 const char *uri;
651                 int res;
652
653                 publisher = AST_VECTOR_GET(&pub_data->pubs, idx);
654
655                 uri = ast_sip_publish_client_get_user_from_uri(publisher->client, pub_data->exten_state_data.exten,
656                         pub_data->exten_state_data.local, sizeof(pub_data->exten_state_data.local));
657                 if (ast_strlen_zero(uri)) {
658                         ast_log(LOG_WARNING, "PUBLISH client '%s' has no from_uri or server_uri defined.\n",
659                                 publisher->name);
660                         continue;
661                 }
662
663                 uri = ast_sip_publish_client_get_user_to_uri(publisher->client, pub_data->exten_state_data.exten,
664                         pub_data->exten_state_data.remote, sizeof(pub_data->exten_state_data.remote));
665                 if (ast_strlen_zero(uri)) {
666                         ast_log(LOG_WARNING, "PUBLISH client '%s' has no to_uri or server_uri defined.\n",
667                                 publisher->name);
668                         continue;
669                 }
670
671                 pub_data->exten_state_data.datastores = publisher->datastores;
672
673                 res = ast_sip_pubsub_generate_body_content(publisher->body_type,
674                         publisher->body_subtype, &gen_data, &body_text);
675                 pj_pool_reset(pool);
676                 if (res) {
677                         ast_log(LOG_WARNING,
678                                 "PUBLISH client '%s' unable to generate %s/%s PUBLISH body.\n",
679                                 publisher->name, publisher->body_type, publisher->body_subtype);
680                         continue;
681                 }
682
683                 body.type = publisher->body_type;
684                 body.subtype = publisher->body_subtype;
685                 body.body_text = ast_str_buffer(body_text);
686                 ast_sip_publish_client_user_send(publisher->client, pub_data->exten_state_data.exten, &body);
687         }
688
689         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
690
691         ast_free(body_text);
692         exten_state_pub_data_destroy(pub_data);
693         return 0;
694 }
695
696 /*!
697  * \brief Global extension state callback function
698  */
699 static int exten_state_publisher_state_cb(const char *context, const char *exten, struct ast_state_cb_info *info, void *data)
700 {
701         struct ao2_iterator publisher_iter;
702         struct exten_state_publisher *publisher;
703         struct exten_state_pub_data *pub_data = NULL;
704
705         ast_debug(5, "Exten state publisher: %s@%s Reason:%s State:%s Presence:%s Subtype:'%s' Message:'%s'\n",
706                 exten, context,
707                 info->reason == AST_HINT_UPDATE_DEVICE
708                         ? "Device"
709                         : info->reason == AST_HINT_UPDATE_PRESENCE
710                                 ? "Presence"
711                                 : "Unknown",
712                 ast_extension_state2str(info->exten_state),
713                 ast_presence_state2str(info->presence_state),
714                 S_OR(info->presence_subtype, ""),
715                 S_OR(info->presence_message, ""));
716         publisher_iter = ao2_iterator_init(publishers, 0);
717         for (; (publisher = ao2_iterator_next(&publisher_iter)); ao2_ref(publisher, -1)) {
718                 if ((publisher->context_filter && regexec(&publisher->context_regex, context, 0, NULL, 0)) ||
719                     (publisher->exten_filter && regexec(&publisher->exten_regex, exten, 0, NULL, 0))) {
720                         continue;
721                 }
722
723                 if (!pub_data) {
724                         pub_data = exten_state_pub_data_alloc(exten, info);
725                         if (!pub_data) {
726                                 ao2_ref(publisher, -1);
727                                 break;
728                         }
729                 }
730
731                 ao2_ref(publisher, +1);
732                 if (AST_VECTOR_APPEND(&pub_data->pubs, publisher)) {
733                         ao2_ref(publisher, -1);
734                 } else {
735                         ast_debug(5, "'%s' will publish exten state\n", publisher->name);
736                 }
737         }
738         ao2_iterator_destroy(&publisher_iter);
739
740         if (pub_data
741                 && ast_sip_push_task(publish_exten_state_serializer, exten_state_publisher_cb,
742                         pub_data)) {
743                 exten_state_pub_data_destroy(pub_data);
744         }
745
746         return 0;
747 }
748
749 /*!
750  * \brief Hashing function for extension state publisher
751  */
752 static int exten_state_publisher_hash(const void *obj, const int flags)
753 {
754         const struct exten_state_publisher *object;
755         const char *key;
756
757         switch (flags & OBJ_SEARCH_MASK) {
758         case OBJ_SEARCH_KEY:
759                 key = obj;
760                 break;
761         case OBJ_SEARCH_OBJECT:
762                 object = obj;
763                 key = object->name;
764                 break;
765         default:
766                 ast_assert(0);
767                 return 0;
768         }
769         return ast_str_hash(key);
770 }
771
772 /*!
773  * \brief Comparator function for extension state publisher
774  */
775 static int exten_state_publisher_cmp(void *obj, void *arg, int flags)
776 {
777         const struct exten_state_publisher *object_left = obj;
778         const struct exten_state_publisher *object_right = arg;
779         const char *right_key = arg;
780         int cmp;
781
782         switch (flags & OBJ_SEARCH_MASK) {
783         case OBJ_SEARCH_OBJECT:
784                 right_key = object_right->name;
785                 /* Fall through */
786         case OBJ_SEARCH_KEY:
787                 cmp = strcmp(object_left->name, right_key);
788                 break;
789         case OBJ_SEARCH_PARTIAL_KEY:
790                 /* Not supported by container. */
791                 ast_assert(0);
792                 return 0;
793         default:
794                 cmp = 0;
795                 break;
796         }
797         if (cmp) {
798                 return 0;
799         }
800         return CMP_MATCH;
801 }
802
803 /*!
804  * \brief Destructor for extension state publisher
805  */
806 static void exten_state_publisher_destroy(void *obj)
807 {
808         struct exten_state_publisher *publisher = obj;
809
810         if (publisher->context_filter) {
811                 regfree(&publisher->context_regex);
812         }
813
814         if (publisher->exten_filter) {
815                 regfree(&publisher->exten_regex);
816         }
817
818         ao2_cleanup(publisher->client);
819         ao2_cleanup(publisher->datastores);
820 }
821
822 static int build_regex(regex_t *regex, const char *text)
823 {
824         int res;
825
826         if ((res = regcomp(regex, text, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
827                 size_t len = regerror(res, regex, NULL, 0);
828                 char buf[len];
829                 regerror(res, regex, buf, len);
830                 ast_log(LOG_ERROR, "Could not compile regex '%s': %s\n", text, buf);
831                 return -1;
832         }
833
834         return 0;
835 }
836
837 static int publisher_start(struct ast_sip_outbound_publish *configuration, struct ast_sip_outbound_publish_client *client)
838 {
839         struct exten_state_publisher *publisher;
840         size_t name_size;
841         size_t body_type_size;
842         size_t body_subtype_size;
843         char *body_subtype;
844         const char *body_full;
845         const char *body_type;
846         const char *name;
847         const char *context;
848         const char *exten;
849
850         name = ast_sorcery_object_get_id(configuration);
851
852         body_full = ast_sorcery_object_get_extended(configuration, "body");
853         if (ast_strlen_zero(body_full)) {
854                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Body not set\n",
855                         name);
856                 return -1;
857         }
858
859         body_subtype = ast_strdupa(body_full);
860         body_type = strsep(&body_subtype, "/");
861         if (ast_strlen_zero(body_type) || ast_strlen_zero(body_subtype)) {
862                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Body '%s' missing type or subtype\n",
863                         name, body_full);
864                 return -1;
865         }
866
867         if (!ast_sip_pubsub_is_body_generator_registered(body_type, body_subtype)) {
868                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': '%s' body generator not registered\n",
869                         name, body_full);
870                 return -1;
871         }
872
873         name_size = strlen(name) + 1;
874         body_type_size = strlen(body_type) + 1;
875         body_subtype_size = strlen(body_subtype) + 1;
876
877         publisher = ao2_alloc_options(
878                 sizeof(*publisher) + name_size + body_type_size + body_subtype_size,
879                 exten_state_publisher_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
880         if (!publisher) {
881                 return -1;
882         }
883
884         ast_copy_string(publisher->name, name, name_size);
885         publisher->body_type = publisher->name + name_size;
886         ast_copy_string(publisher->body_type, body_type, body_type_size);
887         publisher->body_subtype = publisher->body_type + body_type_size;
888         ast_copy_string(publisher->body_subtype, body_subtype, body_subtype_size);
889
890         context = ast_sorcery_object_get_extended(configuration, "context");
891         if (!ast_strlen_zero(context)) {
892                 if (build_regex(&publisher->context_regex, context)) {
893                         ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not build context filter '%s'\n",
894                                 name, context);
895                         ao2_ref(publisher, -1);
896                         return -1;
897                 }
898
899                 publisher->context_filter = 1;
900         }
901
902         exten = ast_sorcery_object_get_extended(configuration, "exten");
903         if (!ast_strlen_zero(exten)) {
904                 if (build_regex(&publisher->exten_regex, exten)) {
905                         ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not build exten filter '%s'\n",
906                                 name, exten);
907                         ao2_ref(publisher, -1);
908                         return -1;
909                 }
910
911                 publisher->exten_filter = 1;
912         }
913
914         publisher->datastores = ast_datastores_alloc();
915         if (!publisher->datastores) {
916                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not create datastores container\n",
917                         name);
918                 ao2_ref(publisher, -1);
919                 return -1;
920         }
921
922         publisher->client = ao2_bump(client);
923
924         ao2_lock(publishers);
925         if (!ao2_container_count(publishers)) {
926                 ast_extension_state_add(NULL, NULL, exten_state_publisher_state_cb, NULL);
927         }
928         ao2_link_flags(publishers, publisher, OBJ_NOLOCK);
929         ao2_unlock(publishers);
930
931         ao2_ref(publisher, -1);
932
933         return 0;
934 }
935
936 static int publisher_stop(struct ast_sip_outbound_publish_client *client)
937 {
938         ao2_find(publishers, ast_sorcery_object_get_id(client), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
939         return 0;
940 }
941
942 static int unload_module(void)
943 {
944         ast_sip_unregister_event_publisher_handler(&dialog_publisher);
945         ast_sip_unregister_subscription_handler(&dialog_handler);
946         ast_sip_unregister_event_publisher_handler(&presence_publisher);
947         ast_sip_unregister_subscription_handler(&presence_handler);
948
949         ast_extension_state_del(0, exten_state_publisher_state_cb);
950
951         ast_taskprocessor_unreference(publish_exten_state_serializer);
952         publish_exten_state_serializer = NULL;
953
954         ao2_cleanup(publishers);
955         publishers = NULL;
956
957         return 0;
958 }
959
960 static int load_module(void)
961 {
962         CHECK_PJSIP_PUBSUB_MODULE_LOADED();
963
964         if (!ast_module_check("res_pjsip_outbound_publish.so")) {
965                 ast_log(LOG_WARNING, "This module requires the 'res_pjsip_outbound_publish.so' module to be loaded\n");
966                 return AST_MODULE_LOAD_DECLINE;
967         }
968
969         publishers = ao2_container_alloc(PUBLISHER_BUCKETS, exten_state_publisher_hash,
970                 exten_state_publisher_cmp);
971         if (!publishers) {
972                 ast_log(LOG_WARNING, "Unable to create container to store extension state publishers\n");
973                 return AST_MODULE_LOAD_DECLINE;
974         }
975
976         publish_exten_state_serializer = ast_sip_create_serializer("pjsip/exten_state");
977         if (!publish_exten_state_serializer) {
978                 unload_module();
979                 return AST_MODULE_LOAD_DECLINE;
980         }
981
982         if (ast_sip_register_subscription_handler(&presence_handler)) {
983                 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
984                         presence_handler.event_name);
985                 unload_module();
986                 return AST_MODULE_LOAD_DECLINE;
987         }
988
989         if (ast_sip_register_event_publisher_handler(&presence_publisher)) {
990                 ast_log(LOG_WARNING, "Unable to register presence publisher %s\n",
991                         presence_publisher.event_name);
992                 unload_module();
993                 return AST_MODULE_LOAD_DECLINE;
994         }
995
996         if (ast_sip_register_subscription_handler(&dialog_handler)) {
997                 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
998                         dialog_handler.event_name);
999                 unload_module();
1000                 return AST_MODULE_LOAD_DECLINE;
1001         }
1002
1003         if (ast_sip_register_event_publisher_handler(&dialog_publisher)) {
1004                 ast_log(LOG_WARNING, "Unable to register presence publisher %s\n",
1005                         dialog_publisher.event_name);
1006                 unload_module();
1007                 return AST_MODULE_LOAD_DECLINE;
1008         }
1009
1010         return AST_MODULE_LOAD_SUCCESS;
1011 }
1012
1013 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Notifications",
1014         .support_level = AST_MODULE_SUPPORT_CORE,
1015         .load = load_module,
1016         .unload = unload_module,
1017         .load_pri = AST_MODPRI_CHANNEL_DEPEND + 5,
1018         .requires = "res_pjsip,res_pjsip_pubsub,res_pjsip_outbound_publish",
1019 );