res_resolver_unbound: Fix config documentation.
[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, "Extension state subscription failed: Extension %s does not exist in context '%s' or has no associated hint\n",
419                         resource, context);
420                 return 404;
421         }
422
423         return 200;
424 }
425
426 static int subscription_established(struct ast_sip_subscription *sip_sub)
427 {
428         struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
429         const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
430         struct exten_state_subscription *exten_state_sub;
431
432         if (!(exten_state_sub = exten_state_subscription_alloc(sip_sub, endpoint))) {
433                 ao2_cleanup(endpoint);
434                 return -1;
435         }
436
437         ast_copy_string(exten_state_sub->context,
438                 S_OR(endpoint->subscription.context, endpoint->context),
439                 sizeof(exten_state_sub->context));
440         ast_copy_string(exten_state_sub->exten, resource, sizeof(exten_state_sub->exten));
441
442         if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
443                      exten_state_sub->context, exten_state_sub->exten,
444                      state_changed, state_changed_destroy, exten_state_sub)) < 0) {
445                 ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n",
446                         ast_sorcery_object_get_id(endpoint), exten_state_sub->exten,
447                         exten_state_sub->context);
448                 ao2_cleanup(endpoint);
449                 ao2_cleanup(exten_state_sub);
450                 return -1;
451         }
452
453         /* Go ahead and cleanup the endpoint since we don't need it anymore */
454         ao2_cleanup(endpoint);
455
456         /* bump the ref since ast_extension_state_add holds a reference */
457         ao2_ref(exten_state_sub, +1);
458
459         if (add_datastore(exten_state_sub)) {
460                 ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
461                 ao2_cleanup(exten_state_sub);
462                 return -1;
463         }
464
465         ao2_cleanup(exten_state_sub);
466         return 0;
467 }
468
469 static void exten_state_data_destructor(void *obj)
470 {
471         struct ast_sip_exten_state_data *exten_state_data = obj;
472
473         ao2_cleanup(exten_state_data->device_state_info);
474         ast_free(exten_state_data->presence_subtype);
475         ast_free(exten_state_data->presence_message);
476         if (exten_state_data->pool) {
477                 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data->pool);
478         }
479 }
480
481 static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_subscription *sip_sub,
482                 struct exten_state_subscription *exten_state_sub)
483 {
484         struct ast_sip_exten_state_data *exten_state_data;
485         char *subtype = NULL;
486         char *message = NULL;
487         int presence_state;
488
489         exten_state_data = ao2_alloc(sizeof(*exten_state_data), exten_state_data_destructor);
490         if (!exten_state_data) {
491                 return NULL;
492         }
493
494         exten_state_data->exten = exten_state_sub->exten;
495         presence_state = ast_hint_presence_state(NULL, exten_state_sub->context, exten_state_sub->exten, &subtype, &message);
496         if (presence_state  == -1 || presence_state == AST_PRESENCE_INVALID) {
497                 ao2_cleanup(exten_state_data);
498                 return NULL;
499         }
500         exten_state_data->presence_state = presence_state;
501         exten_state_data->presence_subtype = subtype;
502         exten_state_data->presence_message = message;
503         exten_state_data->user_agent = exten_state_sub->user_agent;
504         ast_sip_subscription_get_local_uri(sip_sub, exten_state_data->local,
505                         sizeof(exten_state_data->local));
506         ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
507                         sizeof(exten_state_data->remote));
508         exten_state_data->sub = sip_sub;
509         exten_state_data->datastores = ast_sip_subscription_get_datastores(sip_sub);
510
511         exten_state_data->exten_state = ast_extension_state_extended(
512                         NULL, exten_state_sub->context, exten_state_sub->exten,
513                         &exten_state_data->device_state_info);
514         if (exten_state_data->exten_state < 0) {
515                 ao2_cleanup(exten_state_data);
516                 return NULL;
517         }
518
519         exten_state_data->pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
520                         "exten_state", 1024, 1024);
521         if (!exten_state_data->pool) {
522                 ao2_cleanup(exten_state_data);
523                 return NULL;
524         }
525
526         return exten_state_data;
527 }
528
529 static void *get_notify_data(struct ast_sip_subscription *sub)
530 {
531         struct exten_state_subscription *exten_state_sub;
532
533         exten_state_sub = get_exten_state_sub(sub);
534         if (!exten_state_sub) {
535                 return NULL;
536         }
537
538         return exten_state_data_alloc(sub, exten_state_sub);
539 }
540
541 static void to_ami(struct ast_sip_subscription *sub,
542                    struct ast_str **buf)
543 {
544         struct exten_state_subscription *exten_state_sub =
545                 get_exten_state_sub(sub);
546
547         if (!exten_state_sub) {
548                 return;
549         }
550
551         ast_str_append(buf, 0, "SubscriptionType: extension_state\r\n"
552                        "Extension: %s\r\nExtensionStates: %s\r\n",
553                        exten_state_sub->exten, ast_extension_state2str(
554                                exten_state_sub->last_exten_state));
555 }
556
557 struct exten_state_pub_data {
558         /*! Publishers needing state update */
559         AST_VECTOR(name, struct exten_state_publisher *) pubs;
560         /*! Body generator state data */
561         struct ast_sip_exten_state_data exten_state_data;
562 };
563
564 static void exten_state_pub_data_destroy(struct exten_state_pub_data *doomed)
565 {
566         if (!doomed) {
567                 return;
568         }
569
570         ast_free((void *) doomed->exten_state_data.exten);
571         ast_free(doomed->exten_state_data.presence_subtype);
572         ast_free(doomed->exten_state_data.presence_message);
573         ao2_cleanup(doomed->exten_state_data.device_state_info);
574
575         AST_VECTOR_CALLBACK_VOID(&doomed->pubs, ao2_ref, -1);
576         AST_VECTOR_FREE(&doomed->pubs);
577
578         ast_free(doomed);
579 }
580
581 static struct exten_state_pub_data *exten_state_pub_data_alloc(const char *exten, struct ast_state_cb_info *info)
582 {
583         struct exten_state_pub_data *pub_data;
584
585         pub_data = ast_calloc(1, sizeof(*pub_data));
586         if (!pub_data) {
587                 return NULL;
588         }
589
590         if (AST_VECTOR_INIT(&pub_data->pubs, ao2_container_count(publishers))) {
591                 exten_state_pub_data_destroy(pub_data);
592                 return NULL;
593         }
594
595         /* Save off currently known information for the body generators. */
596         pub_data->exten_state_data.exten = ast_strdup(exten);
597         pub_data->exten_state_data.exten_state = info->exten_state;
598         pub_data->exten_state_data.presence_state = info->presence_state;
599         pub_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
600         pub_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
601         pub_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
602         if (!pub_data->exten_state_data.exten
603                 || !pub_data->exten_state_data.presence_subtype
604                 || !pub_data->exten_state_data.presence_message) {
605                 exten_state_pub_data_destroy(pub_data);
606                 return NULL;
607         }
608         return pub_data;
609 }
610
611 /*!
612  * \internal
613  * \brief Create exten state PUBLISH messages under PJSIP thread.
614  * \since 14.0.0
615  *
616  * \return 0
617  */
618 static int exten_state_publisher_cb(void *data)
619 {
620         struct exten_state_pub_data *pub_data = data;
621         struct exten_state_publisher *publisher;
622         size_t idx;
623         struct ast_str *body_text;
624         pj_pool_t *pool;
625         struct ast_sip_body_data gen_data = {
626                 .body_type = AST_SIP_EXTEN_STATE_DATA,
627                 .body_data = &pub_data->exten_state_data,
628         };
629         struct ast_sip_body body;
630
631         body_text = ast_str_create(64);
632         if (!body_text) {
633                 exten_state_pub_data_destroy(pub_data);
634                 return 0;
635         }
636
637         /* Need a PJSIP memory pool to generate the bodies. */
638         pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "pub_state_body",
639                 1024, 1024);
640         if (!pool) {
641                 ast_log(LOG_WARNING, "Exten state publishing unable to create memory pool\n");
642                 exten_state_pub_data_destroy(pub_data);
643                 ast_free(body_text);
644                 return 0;
645         }
646         pub_data->exten_state_data.pool = pool;
647
648         for (idx = 0; idx < AST_VECTOR_SIZE(&pub_data->pubs); ++idx) {
649                 const char *uri;
650                 int res;
651
652                 publisher = AST_VECTOR_GET(&pub_data->pubs, idx);
653
654                 uri = ast_sip_publish_client_get_user_from_uri(publisher->client, pub_data->exten_state_data.exten,
655                         pub_data->exten_state_data.local, sizeof(pub_data->exten_state_data.local));
656                 if (ast_strlen_zero(uri)) {
657                         ast_log(LOG_WARNING, "PUBLISH client '%s' has no from_uri or server_uri defined.\n",
658                                 publisher->name);
659                         continue;
660                 }
661
662                 uri = ast_sip_publish_client_get_user_to_uri(publisher->client, pub_data->exten_state_data.exten,
663                         pub_data->exten_state_data.remote, sizeof(pub_data->exten_state_data.remote));
664                 if (ast_strlen_zero(uri)) {
665                         ast_log(LOG_WARNING, "PUBLISH client '%s' has no to_uri or server_uri defined.\n",
666                                 publisher->name);
667                         continue;
668                 }
669
670                 pub_data->exten_state_data.datastores = publisher->datastores;
671
672                 res = ast_sip_pubsub_generate_body_content(publisher->body_type,
673                         publisher->body_subtype, &gen_data, &body_text);
674                 pj_pool_reset(pool);
675                 if (res) {
676                         ast_log(LOG_WARNING,
677                                 "PUBLISH client '%s' unable to generate %s/%s PUBLISH body.\n",
678                                 publisher->name, publisher->body_type, publisher->body_subtype);
679                         continue;
680                 }
681
682                 body.type = publisher->body_type;
683                 body.subtype = publisher->body_subtype;
684                 body.body_text = ast_str_buffer(body_text);
685                 ast_sip_publish_client_user_send(publisher->client, pub_data->exten_state_data.exten, &body);
686         }
687
688         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
689
690         ast_free(body_text);
691         exten_state_pub_data_destroy(pub_data);
692         return 0;
693 }
694
695 /*!
696  * \brief Global extension state callback function
697  */
698 static int exten_state_publisher_state_cb(const char *context, const char *exten, struct ast_state_cb_info *info, void *data)
699 {
700         struct ao2_iterator publisher_iter;
701         struct exten_state_publisher *publisher;
702         struct exten_state_pub_data *pub_data = NULL;
703
704         ast_debug(5, "Exten state publisher: %s@%s Reason:%s State:%s Presence:%s Subtype:'%s' Message:'%s'\n",
705                 exten, context,
706                 info->reason == AST_HINT_UPDATE_DEVICE
707                         ? "Device"
708                         : info->reason == AST_HINT_UPDATE_PRESENCE
709                                 ? "Presence"
710                                 : "Unknown",
711                 ast_extension_state2str(info->exten_state),
712                 ast_presence_state2str(info->presence_state),
713                 S_OR(info->presence_subtype, ""),
714                 S_OR(info->presence_message, ""));
715         publisher_iter = ao2_iterator_init(publishers, 0);
716         for (; (publisher = ao2_iterator_next(&publisher_iter)); ao2_ref(publisher, -1)) {
717                 if ((publisher->context_filter && regexec(&publisher->context_regex, context, 0, NULL, 0)) ||
718                     (publisher->exten_filter && regexec(&publisher->exten_regex, exten, 0, NULL, 0))) {
719                         continue;
720                 }
721
722                 if (!pub_data) {
723                         pub_data = exten_state_pub_data_alloc(exten, info);
724                         if (!pub_data) {
725                                 ao2_ref(publisher, -1);
726                                 break;
727                         }
728                 }
729
730                 ao2_ref(publisher, +1);
731                 AST_VECTOR_APPEND(&pub_data->pubs, publisher);
732                 ast_debug(5, "'%s' will publish exten state\n", publisher->name);
733         }
734         ao2_iterator_destroy(&publisher_iter);
735
736         if (pub_data
737                 && ast_sip_push_task(publish_exten_state_serializer, exten_state_publisher_cb,
738                         pub_data)) {
739                 exten_state_pub_data_destroy(pub_data);
740         }
741
742         return 0;
743 }
744
745 /*!
746  * \brief Hashing function for extension state publisher
747  */
748 static int exten_state_publisher_hash(const void *obj, const int flags)
749 {
750         const struct exten_state_publisher *object;
751         const char *key;
752
753         switch (flags & OBJ_SEARCH_MASK) {
754         case OBJ_SEARCH_KEY:
755                 key = obj;
756                 break;
757         case OBJ_SEARCH_OBJECT:
758                 object = obj;
759                 key = object->name;
760                 break;
761         default:
762                 ast_assert(0);
763                 return 0;
764         }
765         return ast_str_hash(key);
766 }
767
768 /*!
769  * \brief Comparator function for extension state publisher
770  */
771 static int exten_state_publisher_cmp(void *obj, void *arg, int flags)
772 {
773         const struct exten_state_publisher *object_left = obj;
774         const struct exten_state_publisher *object_right = arg;
775         const char *right_key = arg;
776         int cmp;
777
778         switch (flags & OBJ_SEARCH_MASK) {
779         case OBJ_SEARCH_OBJECT:
780                 right_key = object_right->name;
781                 /* Fall through */
782         case OBJ_SEARCH_KEY:
783                 cmp = strcmp(object_left->name, right_key);
784                 break;
785         case OBJ_SEARCH_PARTIAL_KEY:
786                 /* Not supported by container. */
787                 ast_assert(0);
788                 return 0;
789         default:
790                 cmp = 0;
791                 break;
792         }
793         if (cmp) {
794                 return 0;
795         }
796         return CMP_MATCH;
797 }
798
799 /*!
800  * \brief Destructor for extension state publisher
801  */
802 static void exten_state_publisher_destroy(void *obj)
803 {
804         struct exten_state_publisher *publisher = obj;
805
806         if (publisher->context_filter) {
807                 regfree(&publisher->context_regex);
808         }
809
810         if (publisher->exten_filter) {
811                 regfree(&publisher->exten_regex);
812         }
813
814         ao2_cleanup(publisher->client);
815         ao2_cleanup(publisher->datastores);
816 }
817
818 static int build_regex(regex_t *regex, const char *text)
819 {
820         int res;
821
822         if ((res = regcomp(regex, text, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
823                 size_t len = regerror(res, regex, NULL, 0);
824                 char buf[len];
825                 regerror(res, regex, buf, len);
826                 ast_log(LOG_ERROR, "Could not compile regex '%s': %s\n", text, buf);
827                 return -1;
828         }
829
830         return 0;
831 }
832
833 static int publisher_start(struct ast_sip_outbound_publish *configuration, struct ast_sip_outbound_publish_client *client)
834 {
835         struct exten_state_publisher *publisher;
836         size_t name_size;
837         size_t body_type_size;
838         size_t body_subtype_size;
839         char *body_subtype;
840         const char *body_full;
841         const char *body_type;
842         const char *name;
843         const char *context;
844         const char *exten;
845
846         name = ast_sorcery_object_get_id(configuration);
847
848         body_full = ast_sorcery_object_get_extended(configuration, "body");
849         if (ast_strlen_zero(body_full)) {
850                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Body not set\n",
851                         name);
852                 return -1;
853         }
854
855         body_subtype = ast_strdupa(body_full);
856         body_type = strsep(&body_subtype, "/");
857         if (ast_strlen_zero(body_type) || ast_strlen_zero(body_subtype)) {
858                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Body '%s' missing type or subtype\n",
859                         name, body_full);
860                 return -1;
861         }
862
863         if (!ast_sip_pubsub_is_body_generator_registered(body_type, body_subtype)) {
864                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': '%s' body generator not registered\n",
865                         name, body_full);
866                 return -1;
867         }
868
869         name_size = strlen(name) + 1;
870         body_type_size = strlen(body_type) + 1;
871         body_subtype_size = strlen(body_subtype) + 1;
872
873         publisher = ao2_alloc_options(
874                 sizeof(*publisher) + name_size + body_type_size + body_subtype_size,
875                 exten_state_publisher_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
876         if (!publisher) {
877                 return -1;
878         }
879
880         ast_copy_string(publisher->name, name, name_size);
881         publisher->body_type = publisher->name + name_size;
882         ast_copy_string(publisher->body_type, body_type, body_type_size);
883         publisher->body_subtype = publisher->body_type + body_type_size;
884         ast_copy_string(publisher->body_subtype, body_subtype, body_subtype_size);
885
886         context = ast_sorcery_object_get_extended(configuration, "context");
887         if (!ast_strlen_zero(context)) {
888                 if (build_regex(&publisher->context_regex, context)) {
889                         ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not build context filter '%s'\n",
890                                 name, context);
891                         ao2_ref(publisher, -1);
892                         return -1;
893                 }
894
895                 publisher->context_filter = 1;
896         }
897
898         exten = ast_sorcery_object_get_extended(configuration, "exten");
899         if (!ast_strlen_zero(exten)) {
900                 if (build_regex(&publisher->exten_regex, exten)) {
901                         ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not build exten filter '%s'\n",
902                                 name, exten);
903                         ao2_ref(publisher, -1);
904                         return -1;
905                 }
906
907                 publisher->exten_filter = 1;
908         }
909
910         publisher->datastores = ast_datastores_alloc();
911         if (!publisher->datastores) {
912                 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not create datastores container\n",
913                         name);
914                 ao2_ref(publisher, -1);
915                 return -1;
916         }
917
918         publisher->client = ao2_bump(client);
919
920         ao2_lock(publishers);
921         if (!ao2_container_count(publishers)) {
922                 ast_extension_state_add(NULL, NULL, exten_state_publisher_state_cb, NULL);
923         }
924         ao2_link_flags(publishers, publisher, OBJ_NOLOCK);
925         ao2_unlock(publishers);
926
927         ao2_ref(publisher, -1);
928
929         return 0;
930 }
931
932 static int publisher_stop(struct ast_sip_outbound_publish_client *client)
933 {
934         ao2_find(publishers, ast_sorcery_object_get_id(client), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
935         return 0;
936 }
937
938 static int unload_module(void)
939 {
940         ast_sip_unregister_event_publisher_handler(&dialog_publisher);
941         ast_sip_unregister_subscription_handler(&dialog_handler);
942         ast_sip_unregister_event_publisher_handler(&presence_publisher);
943         ast_sip_unregister_subscription_handler(&presence_handler);
944
945         ast_extension_state_del(0, exten_state_publisher_state_cb);
946
947         ast_taskprocessor_unreference(publish_exten_state_serializer);
948         publish_exten_state_serializer = NULL;
949
950         ao2_cleanup(publishers);
951         publishers = NULL;
952
953         return 0;
954 }
955
956 static int load_module(void)
957 {
958         CHECK_PJSIP_PUBSUB_MODULE_LOADED();
959
960         if (!ast_module_check("res_pjsip_outbound_publish.so")) {
961                 ast_log(LOG_WARNING, "This module requires the 'res_pjsip_outbound_publish.so' module to be loaded\n");
962                 return AST_MODULE_LOAD_DECLINE;
963         }
964
965         publishers = ao2_container_alloc(PUBLISHER_BUCKETS, exten_state_publisher_hash,
966                 exten_state_publisher_cmp);
967         if (!publishers) {
968                 ast_log(LOG_WARNING, "Unable to create container to store extension state publishers\n");
969                 return AST_MODULE_LOAD_DECLINE;
970         }
971
972         publish_exten_state_serializer = ast_sip_create_serializer("pjsip/exten_state");
973         if (!publish_exten_state_serializer) {
974                 unload_module();
975                 return AST_MODULE_LOAD_DECLINE;
976         }
977
978         if (ast_sip_register_subscription_handler(&presence_handler)) {
979                 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
980                         presence_handler.event_name);
981                 unload_module();
982                 return AST_MODULE_LOAD_DECLINE;
983         }
984
985         if (ast_sip_register_event_publisher_handler(&presence_publisher)) {
986                 ast_log(LOG_WARNING, "Unable to register presence publisher %s\n",
987                         presence_publisher.event_name);
988                 unload_module();
989                 return AST_MODULE_LOAD_DECLINE;
990         }
991
992         if (ast_sip_register_subscription_handler(&dialog_handler)) {
993                 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
994                         dialog_handler.event_name);
995                 unload_module();
996                 return AST_MODULE_LOAD_DECLINE;
997         }
998
999         if (ast_sip_register_event_publisher_handler(&dialog_publisher)) {
1000                 ast_log(LOG_WARNING, "Unable to register presence publisher %s\n",
1001                         dialog_publisher.event_name);
1002                 unload_module();
1003                 return AST_MODULE_LOAD_DECLINE;
1004         }
1005
1006         return AST_MODULE_LOAD_SUCCESS;
1007 }
1008
1009 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Notifications",
1010         .support_level = AST_MODULE_SUPPORT_CORE,
1011         .load = load_module,
1012         .unload = unload_module,
1013         .load_pri = AST_MODPRI_CHANNEL_DEPEND + 5,
1014 );