f279e73a0d0a25c0171e8e8578d213348809602f
[asterisk/asterisk.git] / res / res_calendar.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008 - 2009, Digium, Inc.
5  *
6  * Terry Wilson <twilson@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 /*! \file
20  * \brief Calendaring API
21  * 
22  * \todo Support responding to a meeting invite
23  * \todo Support writing attendees
24  */
25
26 /*! \li \ref res_calendar.c uses the configuration file \ref calendar.conf
27  * \addtogroup configuration_file Configuration Files
28  */
29
30 /*! 
31  * \page calendar.conf calendar.conf
32  * \verbinclude calendar.conf.sample
33  */
34
35 /*** MODULEINFO
36         <support_level>core</support_level>
37  ***/
38
39 #include "asterisk.h"
40
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42
43 #include "asterisk/_private.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/calendar.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/astobj2.h"
48 #include "asterisk/module.h"
49 #include "asterisk/config.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/devicestate.h"
52 #include "asterisk/linkedlists.h"
53 #include "asterisk/sched.h"
54 #include "asterisk/dial.h"
55 #include "asterisk/cli.h"
56 #include "asterisk/pbx.h"
57 #include "asterisk/app.h"
58
59 /*** DOCUMENTATION
60         <function name="CALENDAR_BUSY" language="en_US">
61                 <synopsis>
62                         Determine if the calendar is marked busy at this time.
63                 </synopsis>
64                 <syntax>
65                         <parameter name="calendar" required="true" />
66                 </syntax>
67                 <description>
68                         <para>Check the specified calendar's current busy status.</para>
69                 </description>
70                 <see-also>
71                         <ref type="function">CALENDAR_EVENT</ref>
72                         <ref type="function">CALENDAR_QUERY</ref>
73                         <ref type="function">CALENDAR_QUERY_RESULT</ref>
74                         <ref type="function">CALENDAR_WRITE</ref>
75                 </see-also>
76         </function>
77         <function name="CALENDAR_EVENT" language="en_US">
78                 <synopsis>
79                         Get calendar event notification data from a notification call.
80                 </synopsis>
81                 <syntax>
82                         <parameter name="field" required="true">
83                                 <enumlist>
84                                         <enum name="summary"><para>The VEVENT SUMMARY property or Exchange event 'subject'</para></enum>
85                                         <enum name="description"><para>The text description of the event</para></enum>
86                                         <enum name="organizer"><para>The organizer of the event</para></enum>
87                                         <enum name="location"><para>The location of the eventt</para></enum>
88                                         <enum name="categories"><para>The categories of the event</para></enum>
89                                         <enum name="priority"><para>The priority of the event</para></enum>
90                                         <enum name="calendar"><para>The name of the calendar associated with the event</para></enum>
91                                         <enum name="uid"><para>The unique identifier for this event</para></enum>
92                                         <enum name="start"><para>The start time of the event</para></enum>
93                                         <enum name="end"><para>The end time of the event</para></enum>
94                                         <enum name="busystate"><para>The busy state of the event 0=FREE, 1=TENTATIVE, 2=BUSY</para></enum>
95                                 </enumlist>
96                         </parameter>
97                 </syntax>
98                 <description>
99                         <para>Whenever a calendar event notification call is made, the event data
100                         may be accessed with this function.</para>
101                 </description>
102                 <see-also>
103                         <ref type="function">CALENDAR_BUSY</ref>
104                         <ref type="function">CALENDAR_QUERY</ref>
105                         <ref type="function">CALENDAR_QUERY_RESULT</ref>
106                         <ref type="function">CALENDAR_WRITE</ref>
107                 </see-also>
108         </function>
109         <function name="CALENDAR_QUERY" language="en_US">
110                 <synopsis>Query a calendar server and store the data on a channel
111                 </synopsis>
112                 <syntax>
113                         <parameter name="calendar" required="true">
114                                 <para>The calendar that should be queried</para>
115                         </parameter>
116                         <parameter name="start" required="false">
117                                 <para>The start time of the query (in seconds since epoch)</para>
118                         </parameter>
119                         <parameter name="end" required="false">
120                                 <para>The end time of the query (in seconds since epoch)</para>
121                         </parameter>
122                 </syntax>
123                 <description>
124                         <para>Get a list of events in the currently accessible timeframe of the <replaceable>calendar</replaceable>
125                         The function returns the id for accessing the result with CALENDAR_QUERY_RESULT()</para>
126                 </description>
127                 <see-also>
128                         <ref type="function">CALENDAR_BUSY</ref>
129                         <ref type="function">CALENDAR_EVENT</ref>
130                         <ref type="function">CALENDAR_QUERY_RESULT</ref>
131                         <ref type="function">CALENDAR_WRITE</ref>
132                 </see-also>
133         </function>
134         <function name="CALENDAR_QUERY_RESULT" language="en_US">
135                 <synopsis>
136                         Retrieve data from a previously run CALENDAR_QUERY() call
137                 </synopsis>
138                 <syntax>
139                         <parameter name="id" required="true">
140                                 <para>The query ID returned by <literal>CALENDAR_QUERY</literal></para>
141                         </parameter>
142                         <parameter name="field" required="true">
143                                 <enumlist>
144                                         <enum name="getnum"><para>number of events occurring during time range</para></enum>
145                                         <enum name="summary"><para>A summary of the event</para></enum>
146                                         <enum name="description"><para>The full event description</para></enum>
147                                         <enum name="organizer"><para>The event organizer</para></enum>
148                                         <enum name="location"><para>The event location</para></enum>
149                                         <enum name="categories"><para>The categories of the event</para></enum>
150                                         <enum name="priority"><para>The priority of the event</para></enum>
151                                         <enum name="calendar"><para>The name of the calendar associted with the event</para></enum>
152                                         <enum name="uid"><para>The unique identifier for the event</para></enum>
153                                         <enum name="start"><para>The start time of the event (in seconds since epoch)</para></enum>
154                                         <enum name="end"><para>The end time of the event (in seconds since epoch)</para></enum>
155                                         <enum name="busystate"><para>The busy status of the event 0=FREE, 1=TENTATIVE, 2=BUSY</para></enum>
156                                 </enumlist>
157                         </parameter>
158                         <parameter name="entry" required="false" default="1">
159                                 <para>Return data from a specific event returned by the query</para>
160                         </parameter>
161                 </syntax>
162                 <description>
163                         <para>After running CALENDAR_QUERY and getting a result <replaceable>id</replaceable>, calling
164                         <literal>CALENDAR_QUERY</literal> with that <replaceable>id</replaceable> and a <replaceable>field</replaceable>
165                         will return the data for that field. If multiple events matched the query, and <replaceable>entry</replaceable>
166                         is provided, information from that event will be returned.</para>
167                 </description>
168                 <see-also>
169                         <ref type="function">CALENDAR_BUSY</ref>
170                         <ref type="function">CALENDAR_EVENT</ref>
171                         <ref type="function">CALENDAR_QUERY</ref>
172                         <ref type="function">CALENDAR_WRITE</ref>
173                 </see-also>
174         </function>
175         <function name="CALENDAR_WRITE" language="en_US">
176                 <synopsis>Write an event to a calendar</synopsis>
177                 <syntax>
178                         <parameter name="calendar" required="true">
179                                 <para>The calendar to write to</para>
180                         </parameter>
181                         <parameter name="field" multiple="true" required="true">
182                                 <enumlist>
183                                         <enum name="summary"><para>A summary of the event</para></enum>
184                                         <enum name="description"><para>The full event description</para></enum>
185                                         <enum name="organizer"><para>The event organizer</para></enum>
186                                         <enum name="location"><para>The event location</para></enum>
187                                         <enum name="categories"><para>The categories of the event</para></enum>
188                                         <enum name="priority"><para>The priority of the event</para></enum>
189                                         <enum name="uid"><para>The unique identifier for the event</para></enum>
190                                         <enum name="start"><para>The start time of the event (in seconds since epoch)</para></enum>
191                                         <enum name="end"><para>The end time of the event (in seconds since epoch)</para></enum>
192                                         <enum name="busystate"><para>The busy status of the event 0=FREE, 1=TENTATIVE, 2=BUSY</para></enum>
193                                 </enumlist>
194                         </parameter>
195                 </syntax>
196                 <description>
197                         <para>Example: CALENDAR_WRITE(calendar,field1,field2,field3)=val1,val2,val3</para>
198                         <para>The field and value arguments can easily be set/passed using the HASHKEYS() and HASH() functions</para>
199                         <variablelist>
200                                 <variable name="CALENDAR_SUCCESS">
201                                         <para>The status of the write operation to the calendar</para>
202                                         <value name="1" >
203                                                 The event was successfully written to the calendar.
204                                         </value>
205                                         <value name="0" >
206                                                 The event was not written to the calendar due to network issues, permissions, etc.
207                                         </value>
208                                 </variable>
209                         </variablelist>
210
211                 </description>
212                 <see-also>
213                         <ref type="function">CALENDAR_BUSY</ref>
214                         <ref type="function">CALENDAR_EVENT</ref>
215                         <ref type="function">CALENDAR_QUERY</ref>
216                         <ref type="function">CALENDAR_QUERY_RESULT</ref>
217                 </see-also>
218         </function>
219
220 ***/
221 #define CALENDAR_BUCKETS 19
222
223 static struct ao2_container *calendars;
224 static struct ast_sched_context *sched;
225 static pthread_t refresh_thread = AST_PTHREADT_NULL;
226 static ast_mutex_t refreshlock;
227 static ast_cond_t refresh_condition;
228 static ast_mutex_t reloadlock;
229 static int module_unloading;
230
231 static void event_notification_destroy(void *data);
232 static void *event_notification_duplicate(void *data);
233 static void eventlist_destroy(void *data);
234 static void *eventlist_duplicate(void *data);
235
236 static const struct ast_datastore_info event_notification_datastore = {
237         .type = "EventNotification",
238         .destroy = event_notification_destroy,
239         .duplicate = event_notification_duplicate,
240 };
241
242 static const struct ast_datastore_info eventlist_datastore_info = {
243         .type = "CalendarEventList",
244         .destroy = eventlist_destroy,
245         .duplicate = eventlist_duplicate,
246 };
247
248 struct evententry {
249         struct ast_calendar_event *event;
250         AST_LIST_ENTRY(evententry) list;
251 };
252
253 static AST_LIST_HEAD_STATIC(techs, ast_calendar_tech);
254 AST_LIST_HEAD_NOLOCK(eventlist, evententry); /* define the type */
255
256 static struct ast_config *calendar_config;
257 AST_RWLOCK_DEFINE_STATIC(config_lock);
258
259 const struct ast_config *ast_calendar_config_acquire(void)
260 {
261         ast_rwlock_rdlock(&config_lock);
262
263         if (!calendar_config) {
264                 ast_rwlock_unlock(&config_lock);
265                 return NULL;
266         }
267
268         return calendar_config;
269 }
270
271 void ast_calendar_config_release(void)
272 {
273         ast_rwlock_unlock(&config_lock);
274 }
275
276 static struct ast_calendar *unref_calendar(struct ast_calendar *cal)
277 {
278         ao2_ref(cal, -1);
279         return NULL;
280 }
281
282 static int calendar_hash_fn(const void *obj, const int flags)
283 {
284         const struct ast_calendar *cal = obj;
285         return ast_str_case_hash(cal->name);
286 }
287
288 static int calendar_cmp_fn(void *obj, void *arg, int flags)
289 {
290         const struct ast_calendar *one = obj, *two = arg;
291         return !strcasecmp(one->name, two->name) ? CMP_MATCH | CMP_STOP: 0;
292 }
293
294 static struct ast_calendar *find_calendar(const char *name)
295 {
296         struct ast_calendar tmp = {
297                 .name = name,
298         };
299         return ao2_find(calendars, &tmp, OBJ_POINTER);
300 }
301
302 static int event_hash_fn(const void *obj, const int flags)
303 {
304         const struct ast_calendar_event *event = obj;
305         return ast_str_hash(event->uid);
306 }
307
308 static int event_cmp_fn(void *obj, void *arg, int flags)
309 {
310         const struct ast_calendar_event *one = obj, *two = arg;
311         return !strcmp(one->uid, two->uid) ? CMP_MATCH | CMP_STOP : 0;
312 }
313
314 static struct ast_calendar_event *find_event(struct ao2_container *events, const char *uid)
315 {
316         struct ast_calendar_event tmp = {
317                 .uid = uid,
318         };
319         return ao2_find(events, &tmp, OBJ_POINTER);
320 }
321
322 struct ast_calendar_event *ast_calendar_unref_event(struct ast_calendar_event *event)
323 {
324         ao2_ref(event, -1);
325         return NULL;
326 }
327
328 static void calendar_destructor(void *obj)
329 {
330         struct ast_calendar *cal = obj;
331
332         ast_debug(3, "Destroying calendar %s\n", cal->name);
333
334         ao2_lock(cal);
335         cal->unloading = 1;
336         ast_cond_signal(&cal->unload);
337         pthread_join(cal->thread, NULL);
338         if (cal->tech_pvt) {
339                 cal->tech_pvt = cal->tech->unref_calendar(cal->tech_pvt);
340         }
341         ast_calendar_clear_events(cal);
342         ast_string_field_free_memory(cal);
343         if (cal->vars) {
344                 ast_variables_destroy(cal->vars);
345                 cal->vars = NULL;
346         }
347         ao2_ref(cal->events, -1);
348         ao2_unlock(cal);
349 }
350
351 static void eventlist_destructor(void *obj)
352 {
353         struct eventlist *events = obj;
354         struct evententry *entry;
355
356         while ((entry = AST_LIST_REMOVE_HEAD(events, list))) {
357                 ao2_ref(entry->event, -1);
358                 ast_free(entry);
359         }
360 }
361
362 static int calendar_busy_callback(void *obj, void *arg, int flags)
363 {
364         struct ast_calendar_event *event = obj;
365         int *is_busy = arg;
366         struct timeval tv = ast_tvnow();
367
368         if (tv.tv_sec >= event->start && tv.tv_sec <= event->end && event->busy_state > AST_CALENDAR_BS_FREE) {
369                 *is_busy = 1;
370                 return CMP_STOP;
371         }
372
373         return 0;
374 }
375
376 static int calendar_is_busy(struct ast_calendar *cal)
377 {
378         int is_busy = 0;
379
380         ao2_callback(cal->events, OBJ_NODATA, calendar_busy_callback, &is_busy);
381
382         return is_busy;
383 }
384
385 static enum ast_device_state calendarstate(const char *data)
386 {
387         enum ast_device_state state;
388         struct ast_calendar *cal;
389
390         if (ast_strlen_zero(data) || (!(cal = find_calendar(data)))) {
391                 return AST_DEVICE_INVALID;
392         }
393
394         if (cal->tech->is_busy) {
395                 state = cal->tech->is_busy(cal) ? AST_DEVICE_INUSE : AST_DEVICE_NOT_INUSE;
396         } else {
397                 state = calendar_is_busy(cal) ? AST_DEVICE_INUSE : AST_DEVICE_NOT_INUSE;
398         }
399
400         cal = unref_calendar(cal);
401         return state;
402 }
403
404 static struct ast_calendar *build_calendar(struct ast_config *cfg, const char *cat, const struct ast_calendar_tech *tech)
405 {
406         struct ast_calendar *cal;
407         struct ast_variable *v, *last = NULL;
408         int new_calendar = 0;
409
410         if (!(cal = find_calendar(cat))) {
411                 new_calendar = 1;
412                 if (!(cal = ao2_alloc(sizeof(*cal), calendar_destructor))) {
413                         ast_log(LOG_ERROR, "Could not allocate calendar structure. Stopping.\n");
414                         return NULL;
415                 }
416
417                 if (!(cal->events = ao2_container_alloc(CALENDAR_BUCKETS, event_hash_fn, event_cmp_fn))) {
418                         ast_log(LOG_ERROR, "Could not allocate events container for %s\n", cat);
419                         cal = unref_calendar(cal);
420                         return NULL;
421                 }
422
423                 if (ast_string_field_init(cal, 32)) {
424                         ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", cat);
425                         cal = unref_calendar(cal);
426                         return NULL;
427                 }
428         } else {
429                 cal->pending_deletion = 0;
430         }
431
432         ast_string_field_set(cal, name, cat);
433         cal->tech = tech;
434
435         cal->refresh = 3600;
436         cal->timeframe = 60;
437         cal->notify_waittime = 30000;
438
439         for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
440                 if (!strcasecmp(v->name, "autoreminder")) {
441                         cal->autoreminder = atoi(v->value);
442                 } else if (!strcasecmp(v->name, "channel")) {
443                         ast_string_field_set(cal, notify_channel, v->value);
444                 } else if (!strcasecmp(v->name, "context")) {
445                         ast_string_field_set(cal, notify_context, v->value);
446                 } else if (!strcasecmp(v->name, "extension")) {
447                         ast_string_field_set(cal, notify_extension, v->value);
448                 } else if (!strcasecmp(v->name, "waittime")) {
449                         int i = atoi(v->value);
450                         if (i > 0) {
451                                 cal->notify_waittime = 1000 * i;
452                         }
453                 } else if (!strcasecmp(v->name, "app")) {
454                         ast_string_field_set(cal, notify_app, v->value);
455                 } else if (!strcasecmp(v->name, "appdata")) {
456                         ast_string_field_set(cal, notify_appdata, v->value);
457                 } else if (!strcasecmp(v->name, "refresh")) {
458                         cal->refresh = atoi(v->value);
459                 } else if (!strcasecmp(v->name, "timeframe")) {
460                         cal->timeframe = atoi(v->value);
461                 } else if (!strcasecmp(v->name, "setvar")) {
462                         char *name, *value;
463                         struct ast_variable *var;
464
465                         if ((name = (value = ast_strdup(v->value)))) {
466                                 strsep(&value, "=");
467                                 if (value) {
468                                         if ((var = ast_variable_new(ast_strip(name), ast_strip(value), ""))) {
469                                                 if (last) {
470                                                         last->next = var;
471                                                 } else {
472                                                         cal->vars = var;
473                                                 }
474                                                 last = var;
475                                         }
476                                 } else {
477                                         ast_log(LOG_WARNING, "Malformed argument. Should be '%s: variable=value'\n", v->name);
478                                 }
479                                 ast_free(name);
480                         }
481                 }
482         }
483
484         if (new_calendar) {
485                 cal->thread = AST_PTHREADT_NULL;
486                 ast_cond_init(&cal->unload, NULL);
487                 ao2_link(calendars, cal);
488                 if (ast_pthread_create(&cal->thread, NULL, cal->tech->load_calendar, cal)) {
489                         /* If we start failing to create threads, go ahead and return NULL
490                          * and the tech module will be unregistered
491                          */ 
492                         ao2_unlink(calendars, cal);
493                         cal = unref_calendar(cal);
494                 }
495         }
496
497         return cal;
498 }
499
500 static int load_tech_calendars(struct ast_calendar_tech *tech)
501 {
502         struct ast_calendar *cal;
503         const char *cat = NULL;
504         const char *val;
505
506         if (!calendar_config) {
507                 ast_log(LOG_WARNING, "Calendar support disabled, not loading %s calendar module\n", tech->type);
508                 return -1;
509         }
510
511         ast_rwlock_wrlock(&config_lock);
512         while ((cat = ast_category_browse(calendar_config, cat))) {
513                 if (!strcasecmp(cat, "general")) {
514                         continue;
515                 }
516
517                 if (!(val = ast_variable_retrieve(calendar_config, cat, "type")) || strcasecmp(val, tech->type)) {
518                         continue;
519                 }
520
521                 /* A serious error occurred loading calendars from this tech and it should be disabled */
522                 if (!(cal = build_calendar(calendar_config, cat, tech))) {
523                         ast_calendar_unregister(tech);
524                         ast_rwlock_unlock(&config_lock);
525                         return -1;
526                 }
527
528                 cal = unref_calendar(cal);
529         }
530
531         ast_rwlock_unlock(&config_lock);
532
533         return 0;
534 }
535
536 int ast_calendar_register(struct ast_calendar_tech *tech)
537 {
538         struct ast_calendar_tech *iter;
539
540         AST_LIST_LOCK(&techs);
541         AST_LIST_TRAVERSE(&techs, iter, list) {
542                 if(!strcasecmp(tech->type, iter->type)) {
543                         ast_log(LOG_WARNING, "Already have a handler for calendar type '%s'\n", tech->type);
544                         AST_LIST_UNLOCK(&techs);
545                         return -1;
546                 }
547         }
548         AST_LIST_INSERT_HEAD(&techs, tech, list);
549         tech->user = ast_module_user_add(NULL);
550         AST_LIST_UNLOCK(&techs);
551
552         ast_verb(2, "Registered calendar type '%s' (%s)\n", tech->type, tech->description);
553
554         return load_tech_calendars(tech);
555 }
556
557 static int match_caltech_cb(void *user_data, void *arg, int flags)
558 {
559         struct ast_calendar *cal = user_data;
560         struct ast_calendar_tech *tech = arg;
561
562         if (cal->tech == tech) {
563                 return CMP_MATCH;
564         }
565
566         return 0;
567 }
568
569 void ast_calendar_unregister(struct ast_calendar_tech *tech)
570 {
571         struct ast_calendar_tech *iter;
572
573         AST_LIST_LOCK(&techs);
574         AST_LIST_TRAVERSE_SAFE_BEGIN(&techs, iter, list) {
575                 if (iter != tech) {
576                         continue;
577                 }
578
579                 ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, match_caltech_cb, tech);
580
581                 AST_LIST_REMOVE_CURRENT(list);
582                 ast_module_user_remove(iter->user);
583                 ast_verb(2, "Unregistered calendar type '%s'\n", tech->type);
584                 break;
585         }
586         AST_LIST_TRAVERSE_SAFE_END;
587         AST_LIST_UNLOCK(&techs);
588
589 }
590
591 static void calendar_event_destructor(void *obj)
592 {
593         struct ast_calendar_event *event = obj;
594         struct ast_calendar_attendee *attendee;
595
596         ast_debug(3, "Destroying event for calendar '%s'\n", event->owner->name);
597         ast_string_field_free_memory(event);
598         while ((attendee = AST_LIST_REMOVE_HEAD(&event->attendees, next))) {
599                 if (attendee->data) {
600                         ast_free(attendee->data);
601                 }
602                 ast_free(attendee);
603         }
604 }
605
606 /* This is only called from ao2_callbacks that are going to unref the event for us,
607  * so we don't unref the event here.  */
608 static struct ast_calendar_event *destroy_event(struct ast_calendar_event *event)
609 {
610         if (event->notify_sched > -1 && ast_sched_del(sched, event->notify_sched)) {
611                 ast_debug(3, "Notification running, can't delete sched entry\n");
612         }
613         if (event->bs_start_sched > -1 && ast_sched_del(sched, event->bs_start_sched)) {
614                 ast_debug(3, "Devicestate update (start) running, can't delete sched entry\n");
615         }
616         if (event->bs_end_sched > -1 && ast_sched_del(sched, event->bs_end_sched)) {
617                 ast_debug(3, "Devicestate update (end) running, can't delete sched entry\n");
618         }
619
620         /* If an event is being deleted and we've fired an event changing the status at the beginning,
621          * but haven't hit the end event yet, go ahead and set the devicestate to the current busy status */
622         if (event->bs_start_sched < 0 && event->bs_end_sched >= 0) {
623                 if (!calendar_is_busy(event->owner)) {
624                         ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
625                 } else {
626                         ast_devstate_changed(AST_DEVICE_BUSY, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
627                 }
628         }
629
630         return NULL;
631 }
632
633 static int clear_events_cb(void *user_data, void *arg, int flags)
634 {
635         struct ast_calendar_event *event = user_data;
636
637         event = destroy_event(event);
638
639         return CMP_MATCH;
640 }
641
642 void ast_calendar_clear_events(struct ast_calendar *cal)
643 {
644         ast_debug(3, "Clearing all events for calendar %s\n", cal->name);
645
646         ao2_callback(cal->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, clear_events_cb, NULL);
647 }
648
649 struct ast_calendar_event *ast_calendar_event_alloc(struct ast_calendar *cal)
650 {
651         struct ast_calendar_event *event;
652         if (!(event = ao2_alloc(sizeof(*event), calendar_event_destructor))) {
653                 return NULL;
654         }
655
656         if (ast_string_field_init(event, 32)) {
657                 event = ast_calendar_unref_event(event);
658                 return NULL;
659         }
660
661         event->owner = cal;
662         event->notify_sched = -1;
663         event->bs_start_sched = -1;
664         event->bs_end_sched = -1;
665
666         AST_LIST_HEAD_INIT_NOLOCK(&event->attendees);
667
668         return event;
669 }
670
671 struct ao2_container *ast_calendar_event_container_alloc(void)
672 {
673         return ao2_container_alloc(CALENDAR_BUCKETS, event_hash_fn, event_cmp_fn);
674 }
675
676 static void event_notification_destroy(void *data)
677 {
678         struct ast_calendar_event *event = data;
679
680         event = ast_calendar_unref_event(event);
681
682 }
683
684 static void *event_notification_duplicate(void *data)
685 {
686         struct ast_calendar_event *event = data;
687
688         if (!event) {
689                 return NULL;
690         }
691
692         ao2_ref(event, +1);
693
694         return event;
695 }
696
697 /*! \brief Generate 32 byte random string (stolen from chan_sip.c)*/
698 static char *generate_random_string(char *buf, size_t size)
699 {
700         long val[4];
701         int x;
702
703         for (x = 0; x < 4; x++) {
704                 val[x] = ast_random();
705         }
706         snprintf(buf, size, "%08lx%08lx%08lx%08lx", val[0], val[1], val[2], val[3]);
707
708         return buf;
709 }
710
711 static int null_chan_write(struct ast_channel *chan, struct ast_frame *frame)
712 {
713         return 0;
714 }
715
716 static const struct ast_channel_tech null_tech = {
717         .type = "NULL",
718         .description = "Null channel (should not see this)",
719                 .write = null_chan_write,
720 };
721
722 static void *do_notify(void *data)
723 {
724         struct ast_calendar_event *event = data;
725         struct ast_dial *dial = NULL;
726         struct ast_str *apptext = NULL, *tmpstr = NULL;
727         struct ast_datastore *datastore;
728         enum ast_dial_result res;
729         struct ast_channel *chan = NULL;
730         struct ast_variable *itervar;
731         char *tech, *dest;
732         char buf[8];
733
734         tech = ast_strdupa(event->owner->notify_channel);
735
736         if ((dest = strchr(tech, '/'))) {
737                 *dest = '\0';
738                 dest++;
739         } else {
740                 ast_log(LOG_WARNING, "Channel should be in form Tech/Dest (was '%s')\n", tech);
741                 goto notify_cleanup;
742         }
743
744         if (!(dial = ast_dial_create())) {
745                 ast_log(LOG_ERROR, "Could not create dial structure\n");
746                 goto notify_cleanup;
747         }
748
749         if (ast_dial_append(dial, tech, dest, NULL) < 0) {
750                 ast_log(LOG_ERROR, "Could not append channel\n");
751                 goto notify_cleanup;
752         }
753
754         ast_dial_set_global_timeout(dial, event->owner->notify_waittime);
755         generate_random_string(buf, sizeof(buf));
756
757         if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, 0, 0, 0, 0, 0, NULL, NULL, 0, "Calendar/%s-%s", event->owner->name, buf))) {
758                 ast_log(LOG_ERROR, "Could not allocate notification channel\n");
759                 goto notify_cleanup;
760         }
761
762         ast_channel_tech_set(chan, &null_tech);
763         ast_format_set(ast_channel_writeformat(chan), AST_FORMAT_SLINEAR, 0);
764         ast_format_set(ast_channel_readformat(chan), AST_FORMAT_SLINEAR, 0);
765         ast_format_set(ast_channel_rawwriteformat(chan), AST_FORMAT_SLINEAR, 0);
766         ast_format_set(ast_channel_rawreadformat(chan), AST_FORMAT_SLINEAR, 0);
767         /* clear native formats and set to slinear. write format is signlear so just use that to set it */
768         ast_format_cap_set(ast_channel_nativeformats(chan), ast_channel_writeformat(chan));
769
770         ast_channel_unlock(chan);
771
772         if (!(datastore = ast_datastore_alloc(&event_notification_datastore, NULL))) {
773                 ast_log(LOG_ERROR, "Could not allocate datastore, notification not being sent!\n");
774                 goto notify_cleanup;
775         }
776
777         datastore->data = event;
778         datastore->inheritance = DATASTORE_INHERIT_FOREVER;
779
780         ao2_ref(event, +1);
781
782         ast_channel_lock(chan);
783         res = ast_channel_datastore_add(chan, datastore);
784         ast_channel_unlock(chan);
785
786         if (!(tmpstr = ast_str_create(32))) {
787                 goto notify_cleanup;
788         }
789
790         for (itervar = event->owner->vars; itervar; itervar = itervar->next) {
791                 ast_str_substitute_variables(&tmpstr, 0, chan, itervar->value);
792                 pbx_builtin_setvar_helper(chan, itervar->name, tmpstr->str);
793         }
794
795         if (!(apptext = ast_str_create(32))) {
796                 goto notify_cleanup;
797         }
798
799         if (!ast_strlen_zero(event->owner->notify_app)) {
800                 ast_str_set(&apptext, 0, "%s,%s", event->owner->notify_app, event->owner->notify_appdata);
801                 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, ast_str_buffer(apptext));
802         } else {
803         }
804
805         ast_verb(3, "Dialing %s for notification on calendar %s\n", event->owner->notify_channel, event->owner->name);
806         res = ast_dial_run(dial, chan, 0);
807
808         if (res != AST_DIAL_RESULT_ANSWERED) {
809                 ast_verb(3, "Notification call for %s was not completed\n", event->owner->name);
810         } else {
811                 struct ast_channel *answered;
812
813                 answered = ast_dial_answered_steal(dial);
814                 if (ast_strlen_zero(event->owner->notify_app)) {
815                         ast_channel_context_set(answered, event->owner->notify_context);
816                         ast_channel_exten_set(answered, event->owner->notify_extension);
817                         ast_channel_priority_set(answered, 1);
818                         ast_pbx_run(answered);
819                 }
820         }
821
822 notify_cleanup:
823         if (apptext) {
824                 ast_free(apptext);
825         }
826         if (tmpstr) {
827                 ast_free(tmpstr);
828         }
829         if (dial) {
830                 ast_dial_destroy(dial);
831         }
832         if (chan) {
833                 ast_channel_release(chan);
834         }
835
836         event = ast_calendar_unref_event(event);
837
838         return NULL;
839 }
840
841 static int calendar_event_notify(const void *data)
842 {
843         struct ast_calendar_event *event = (void *)data;
844         int res = -1;
845         pthread_t notify_thread = AST_PTHREADT_NULL;
846
847         if (!(event && event->owner)) {
848                 ast_log(LOG_ERROR, "Extremely low-cal...in fact cal is NULL!\n");
849                 return res;
850         }
851
852         ao2_ref(event, +1);
853         event->notify_sched = -1;
854
855         if (ast_pthread_create_background(&notify_thread, NULL, do_notify, event) < 0) {
856                 ast_log(LOG_ERROR, "Could not create notification thread\n");
857                 return res;
858         }
859
860         res = 0;
861
862         return res;
863 }
864
865 static int calendar_devstate_change(const void *data)
866 {
867         struct ast_calendar_event *event = (struct ast_calendar_event *)data;
868         struct timeval now = ast_tvnow();
869         int is_end_event;
870
871         if (!event) {
872                 ast_log(LOG_WARNING, "Event was NULL!\n");
873                 return 0;
874         }
875
876         ao2_ref(event, +1);
877
878         is_end_event = event->end <= now.tv_sec;
879
880         if (is_end_event) {
881                 event->bs_end_sched = -1;
882         } else {
883                 event->bs_start_sched = -1;
884         }
885
886         /* We can have overlapping events, so ignore the event->busy_state and check busy state
887          * based on all events in the calendar */
888         if (!calendar_is_busy(event->owner)) {
889                 ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
890         } else {
891                 ast_devstate_changed(AST_DEVICE_BUSY, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name);
892         }
893
894         event = ast_calendar_unref_event(event);
895
896         return 0;
897 }
898
899 static void copy_event_data(struct ast_calendar_event *dst, struct ast_calendar_event *src)
900 {
901         struct ast_calendar_attendee *attendee;
902
903         ast_string_field_set(dst, summary, src->summary);
904         ast_string_field_set(dst, description, src->description);
905         ast_string_field_set(dst, organizer, src->organizer);
906         ast_string_field_set(dst, location, src->location);
907         ast_string_field_set(dst, uid, src->uid);
908         ast_string_field_set(dst, categories, src->categories);
909         dst->priority = src->priority;
910         dst->owner = src->owner;
911         dst->start = src->start;
912         dst->end = src->end;
913         dst->alarm = src->alarm;
914         dst->busy_state = src->busy_state;
915
916         /* Delete any existing attendees */
917         while ((attendee = AST_LIST_REMOVE_HEAD(&dst->attendees, next))) {
918                 ast_free(attendee);
919         }
920
921         /* Copy over the new attendees */
922         while ((attendee = AST_LIST_REMOVE_HEAD(&src->attendees, next))) {
923                 AST_LIST_INSERT_TAIL(&dst->attendees, attendee, next);
924         }
925 }
926
927 static int schedule_calendar_event(struct ast_calendar *cal, struct ast_calendar_event *old_event, struct ast_calendar_event *cmp_event)
928 {
929         struct timeval now = ast_tvnow();
930         struct ast_calendar_event *event;
931         time_t alarm_notify_sched = 0, devstate_sched_start, devstate_sched_end;
932         int changed = 0;
933
934         event = cmp_event ? cmp_event : old_event;
935
936         ao2_lock(event);
937         if (!cmp_event || old_event->alarm != event->alarm) {
938                 changed = 1;
939                 if (cal->autoreminder) {
940                         alarm_notify_sched = (event->start - (60 * cal->autoreminder) - now.tv_sec) * 1000;
941                 } else if (event->alarm) {
942                         alarm_notify_sched = (event->alarm - now.tv_sec) * 1000;
943                 }
944
945                 /* For now, send the notification if we missed it, but the meeting hasn't happened yet */
946                 if (event->start >=  now.tv_sec) {
947                         if (alarm_notify_sched <= 0) {
948                                 alarm_notify_sched = 1;
949                         }
950                         ast_mutex_lock(&refreshlock);
951                         AST_SCHED_REPLACE(old_event->notify_sched, sched, alarm_notify_sched, calendar_event_notify, old_event);
952                         ast_mutex_unlock(&refreshlock);
953                         ast_debug(3, "Calendar alarm event notification scheduled to happen in %ld ms\n", (long) alarm_notify_sched);
954                 }
955         }
956
957         if (!cmp_event || old_event->start != event->start) {
958                 changed = 1;
959                 devstate_sched_start = (event->start - now.tv_sec) * 1000;
960
961                 if (devstate_sched_start < 1) {
962                         devstate_sched_start = 1;
963                 }
964
965                 ast_mutex_lock(&refreshlock);
966                 AST_SCHED_REPLACE(old_event->bs_start_sched, sched, devstate_sched_start, calendar_devstate_change, old_event);
967                 ast_mutex_unlock(&refreshlock);
968                 ast_debug(3, "Calendar bs_start event notification scheduled to happen in %ld ms\n", (long) devstate_sched_start);
969         }
970
971         if (!cmp_event || old_event->end != event->end) {
972                 changed = 1;
973                 devstate_sched_end = (event->end - now.tv_sec) * 1000;
974                 ast_mutex_lock(&refreshlock);
975                 AST_SCHED_REPLACE(old_event->bs_end_sched, sched, devstate_sched_end, calendar_devstate_change, old_event);
976                 ast_mutex_unlock(&refreshlock);
977                 ast_debug(3, "Calendar bs_end event notification scheduled to happen in %ld ms\n", (long) devstate_sched_end);
978         }
979
980         if (changed) {
981                 ast_cond_signal(&refresh_condition);
982         }
983
984         ao2_unlock(event);
985
986         return 0;
987 }
988
989 static int merge_events_cb(void *obj, void *arg, int flags)
990 {
991         struct ast_calendar_event *old_event = obj, *new_event;
992         struct ao2_container *new_events = arg;
993
994         /* If we don't find the old_event in new_events, then we can safely delete the old_event */
995         if (!(new_event = find_event(new_events, old_event->uid))) {
996                 old_event = destroy_event(old_event);
997                 return CMP_MATCH;
998         }
999
1000         /* We have events to merge.  If any data that will affect a scheduler event has changed,
1001          * then we need to replace the scheduler event */
1002         schedule_calendar_event(old_event->owner, old_event, new_event);
1003
1004         /* Since we don't want to mess with cancelling sched events and adding new ones, just
1005          * copy the internals of the new_event to the old_event */
1006         copy_event_data(old_event, new_event);
1007
1008         /* Now we can go ahead and unlink the new_event from new_events and unref it so that only completely
1009          * new events remain in the container */
1010         ao2_unlink(new_events, new_event);
1011         new_event = ast_calendar_unref_event(new_event);
1012
1013         return 0;
1014 }
1015
1016 static int add_new_event_cb(void *obj, void *arg, int flags)
1017 {
1018         struct ast_calendar_event *new_event = obj;
1019         struct ao2_container *events = arg;
1020
1021         ao2_link(events, new_event);
1022         schedule_calendar_event(new_event->owner, new_event, NULL);
1023         return CMP_MATCH;
1024 }
1025
1026 void ast_calendar_merge_events(struct ast_calendar *cal, struct ao2_container *new_events)
1027 {
1028         /* Loop through all events attached to the calendar.  If there is a matching new event
1029          * merge its data over and handle any schedule changes that need to be made.  Then remove
1030          * the new_event from new_events so that we are left with only new_events that we can add later. */
1031         ao2_callback(cal->events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, merge_events_cb, new_events);
1032
1033         /* Now, we should only have completely new events in new_events.  Loop through and add them */
1034         ao2_callback(new_events, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, add_new_event_cb, cal->events);
1035 }
1036
1037
1038 static int load_config(int reload)
1039 {
1040         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1041         struct ast_config *tmpcfg;
1042
1043         if (!(tmpcfg = ast_config_load2("calendar.conf", "calendar", config_flags)) ||
1044                 tmpcfg == CONFIG_STATUS_FILEINVALID) {
1045                 ast_log(LOG_ERROR, "Unable to load config calendar.conf\n");
1046                 return -1;
1047         }
1048
1049         if (tmpcfg == CONFIG_STATUS_FILEUNCHANGED) {
1050                 return 0;
1051         }
1052
1053         ast_rwlock_wrlock(&config_lock);
1054         if (calendar_config) {
1055                 ast_config_destroy(calendar_config);
1056         }
1057
1058         calendar_config = tmpcfg;
1059         ast_rwlock_unlock(&config_lock);
1060
1061         return 0;
1062 }
1063
1064 /*! \brief A dialplan function that can be used to determine the busy status of a calendar */
1065 static int calendar_busy_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1066 {
1067         struct ast_calendar *cal;
1068
1069         if (ast_strlen_zero(data)) {
1070                 ast_log(LOG_WARNING, "CALENDAR_BUSY requires an argument: CALENDAR_BUSY(<calendar_name>)\n");
1071                 return -1;
1072         }
1073
1074         cal = find_calendar(data);
1075
1076         if (!cal) {
1077                 ast_log(LOG_WARNING, "Could not find calendar '%s'\n", data);
1078                 return -1;
1079         }
1080
1081         strcpy(buf, calendar_is_busy(cal) ? "1" : "0");
1082         cal = unref_calendar(cal);
1083
1084         return 0;
1085 }
1086
1087 static struct ast_custom_function calendar_busy_function = {
1088     .name = "CALENDAR_BUSY",
1089     .read = calendar_busy_exec,
1090 };
1091
1092 static int add_event_to_list(struct eventlist *events, struct ast_calendar_event *event, time_t start, time_t end)
1093 {
1094         struct evententry *entry, *iter;
1095         int event_startdiff = abs(start - event->start);
1096         int event_enddiff = abs(end - event->end);
1097         int i = 0;
1098
1099         if (!(entry = ast_calloc(1, sizeof(*entry)))) {
1100                 ast_log(LOG_ERROR, "Unable to allocate memory for event list\n");
1101                 return -1;
1102         }
1103
1104         entry->event = event;
1105         ao2_ref(event, +1);
1106
1107         if (start == end) {
1108                 AST_LIST_TRAVERSE_SAFE_BEGIN(events, iter, list) {
1109                         int startdiff = abs(iter->event->start - start);
1110
1111                         ast_debug(10, "Comparing %s with startdiff %d to %s with startdiff %d\n", event->summary, event_startdiff, iter->event->summary, startdiff);
1112                         ++i;
1113                         if (startdiff > event_startdiff) {
1114                                 AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
1115                                 return i;
1116                         }
1117                         if (startdiff == event_startdiff) {
1118                                 int enddiff = abs(iter->event->end - end);
1119
1120                                 if (enddiff > event_enddiff) {
1121                                         AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
1122                                         return i;
1123                                 }
1124                                 if (event_startdiff == enddiff) {
1125                                         if (strcmp(event->uid, iter->event->uid) < 0) {
1126                                                 AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
1127                                                 return i;
1128                                         }
1129                                 }
1130                         }
1131                 }
1132                 AST_LIST_TRAVERSE_SAFE_END;
1133
1134                 AST_LIST_INSERT_TAIL(events, entry, list);
1135
1136                 return i;
1137         }
1138
1139         AST_LIST_TRAVERSE_SAFE_BEGIN(events, iter, list) {
1140                 ++i;
1141                 if (iter->event->start > event->start) {
1142                         AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
1143                         return i;
1144                 }
1145
1146                 if (iter->event->start == event->start) {
1147                         if ((iter->event->end - iter->event->start) == (event->end - event->start)) {
1148                                 if (strcmp(event->uid, iter->event->uid) < 0) {
1149                                         AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
1150                                         return i;
1151                                 }
1152                         }
1153                         if ((iter->event->end - iter->event->start) < (event->end - event->start)) {
1154                                 AST_LIST_INSERT_BEFORE_CURRENT(entry, list);
1155                                 return i;
1156                         }
1157                 }
1158         }
1159         AST_LIST_TRAVERSE_SAFE_END;
1160
1161         AST_LIST_INSERT_TAIL(events, entry, list);
1162
1163         return i;
1164 }
1165
1166 static void eventlist_destroy(void *data)
1167 {
1168         struct eventlist *events = data;
1169
1170         ao2_ref(events, -1);
1171 }
1172
1173 static void *eventlist_duplicate(void *data)
1174 {
1175         struct eventlist *events = data;
1176
1177         if (!events) {
1178                 return NULL;
1179         }
1180
1181         ao2_ref(events, +1);
1182
1183         return events;
1184 }
1185
1186 static int calendar_query_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1187 {
1188         struct ast_calendar *cal;
1189         struct ao2_iterator i;
1190         struct ast_calendar_event *event;
1191         struct eventlist *events;
1192         time_t start = INT_MIN, end = INT_MAX;
1193         struct ast_datastore *eventlist_datastore;
1194         AST_DECLARE_APP_ARGS(args,
1195                 AST_APP_ARG(calendar);
1196                 AST_APP_ARG(start);
1197                 AST_APP_ARG(end);
1198         );
1199
1200         if (!chan) {
1201                 ast_log(LOG_WARNING, "%s requires a channel to store the data on\n", cmd);
1202                 return -1;
1203         }
1204
1205         AST_STANDARD_APP_ARGS(args, data);
1206
1207         if (ast_strlen_zero(args.calendar)) {
1208                 ast_log(LOG_WARNING, "%s requires a calendar argument\n", cmd);
1209                 return -1;
1210         }
1211
1212         if (!(cal = find_calendar(args.calendar))) {
1213                 ast_log(LOG_WARNING, "Unknown calendar '%s'\n", args.calendar);
1214                 return -1;
1215         }
1216
1217         if (!(events = ao2_alloc(sizeof(*events), eventlist_destructor))) {
1218                 ast_log(LOG_ERROR, "Unable to allocate memory for event list\n");
1219                 cal = unref_calendar(cal);
1220                 return -1;
1221         }
1222
1223         if (!ast_strlen_zero(args.start)) {
1224                 start = atoi(args.start);
1225         }
1226
1227         if (!ast_strlen_zero(args.end)) {
1228                 end = atoi(args.end);
1229         }
1230
1231         i = ao2_iterator_init(cal->events, 0);
1232         while ((event = ao2_iterator_next(&i))) {
1233                 if (!(start > event->end || end < event->start)) {
1234                         ast_debug(10, "%s (%ld - %ld) overlapped with (%ld - %ld)\n", event->summary, (long) event->start, (long) event->end, (long) start, (long) end);
1235                         if (add_event_to_list(events, event, start, end) < 0) {
1236                                 event = ast_calendar_unref_event(event);
1237                                 cal = unref_calendar(cal);
1238                                 ao2_ref(events, -1);
1239                                 ao2_iterator_destroy(&i);
1240                                 return -1;
1241                         }
1242                 }
1243
1244                 event = ast_calendar_unref_event(event);
1245         }
1246         ao2_iterator_destroy(&i);
1247
1248         ast_channel_lock(chan);
1249         do {
1250                 generate_random_string(buf, len);
1251         } while (ast_channel_datastore_find(chan, &eventlist_datastore_info, buf));
1252         ast_channel_unlock(chan);
1253
1254         if (!(eventlist_datastore = ast_datastore_alloc(&eventlist_datastore_info, buf))) {
1255                 ast_log(LOG_ERROR, "Could not allocate datastore!\n");
1256                 cal = unref_calendar(cal);
1257                 ao2_ref(events, -1);
1258                 return -1;
1259         }
1260
1261         eventlist_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
1262         eventlist_datastore->data = events;
1263
1264         ast_channel_lock(chan);
1265         ast_channel_datastore_add(chan, eventlist_datastore);
1266         ast_channel_unlock(chan);
1267
1268         cal = unref_calendar(cal);
1269         return 0;
1270 }
1271
1272 static struct ast_custom_function calendar_query_function = {
1273     .name = "CALENDAR_QUERY",
1274     .read = calendar_query_exec,
1275 };
1276
1277 static void calendar_join_attendees(struct ast_calendar_event *event, char *buf, size_t len)
1278 {
1279         struct ast_str *tmp;
1280         struct ast_calendar_attendee *attendee;
1281
1282         if (!(tmp = ast_str_create(32))) {
1283                 ast_log(LOG_ERROR, "Could not allocate memory for attendees!\n");
1284                 return;
1285         }
1286
1287         AST_LIST_TRAVERSE(&event->attendees, attendee, next) {
1288                 ast_str_append(&tmp, 0, "%s%s", attendee == AST_LIST_FIRST(&event->attendees) ? "" : ",", attendee->data);
1289         }
1290
1291         ast_copy_string(buf, ast_str_buffer(tmp), len);
1292         ast_free(tmp);
1293 }
1294
1295 static int calendar_query_result_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1296 {
1297         struct ast_datastore *datastore;
1298         struct eventlist *events;
1299         struct evententry *entry;
1300         int row = 1;
1301         size_t listlen = 0;
1302         AST_DECLARE_APP_ARGS(args,
1303                 AST_APP_ARG(id);
1304                 AST_APP_ARG(field);
1305                 AST_APP_ARG(row);
1306         );
1307
1308         if (!chan) {
1309                 ast_log(LOG_WARNING, "%s requires a channel\n", cmd);
1310                 return -1;
1311         }
1312
1313         AST_STANDARD_APP_ARGS(args, data);
1314
1315         if (ast_strlen_zero(args.id) || ast_strlen_zero(args.field)) {
1316                 ast_log(LOG_WARNING, "%s requires an id and a field", cmd);
1317                 return -1;
1318         }
1319
1320         ast_channel_lock(chan);
1321         if (!(datastore = ast_channel_datastore_find(chan, &eventlist_datastore_info, args.id))) {
1322                 ast_log(LOG_WARNING, "There is no event notification datastore with id '%s' on '%s'!\n", args.id, ast_channel_name(chan));
1323                 ast_channel_unlock(chan);
1324                 return -1;
1325         }
1326         ast_channel_unlock(chan);
1327
1328         if (!(events = datastore->data)) {
1329                 ast_log(LOG_WARNING, "The datastore contains no data!\n");
1330                 return -1;
1331         }
1332
1333         if (!ast_strlen_zero(args.row)) {
1334                 row = atoi(args.row);
1335         }
1336
1337         AST_LIST_TRAVERSE(events, entry, list) {
1338                 listlen++;
1339         }
1340
1341         if (!strcasecmp(args.field, "getnum")) {
1342                 snprintf(buf, len, "%zu", listlen);
1343                 return 0;
1344         }
1345
1346         AST_LIST_TRAVERSE(events, entry, list) {
1347                 if (--row) {
1348                         continue;
1349                 }
1350                 if (!strcasecmp(args.field, "summary")) {
1351                         ast_copy_string(buf, entry->event->summary, len);
1352                 } else if (!strcasecmp(args.field, "description")) {
1353                         ast_copy_string(buf, entry->event->description, len);
1354                 } else if (!strcasecmp(args.field, "organizer")) {
1355                         ast_copy_string(buf, entry->event->organizer, len);
1356                 } else if (!strcasecmp(args.field, "location")) {
1357                         ast_copy_string(buf, entry->event->location, len);
1358                 } else if (!strcasecmp(args.field, "categories")) {
1359                         ast_copy_string(buf, entry->event->categories, len);
1360                 } else if (!strcasecmp(args.field, "priority")) {
1361                         snprintf(buf, len, "%d", entry->event->priority);
1362                 } else if (!strcasecmp(args.field, "calendar")) {
1363                         ast_copy_string(buf, entry->event->owner->name, len);
1364                 } else if (!strcasecmp(args.field, "uid")) {
1365                         ast_copy_string(buf, entry->event->uid, len);
1366                 } else if (!strcasecmp(args.field, "start")) {
1367                         snprintf(buf, len, "%ld", (long) entry->event->start);
1368                 } else if (!strcasecmp(args.field, "end")) {
1369                         snprintf(buf, len, "%ld", (long) entry->event->end);
1370                 } else if (!strcasecmp(args.field, "busystate")) {
1371                         snprintf(buf, len, "%d", entry->event->busy_state);
1372                 } else if (!strcasecmp(args.field, "attendees")) {
1373                         calendar_join_attendees(entry->event, buf, len);
1374                 } else {
1375                         ast_log(LOG_WARNING, "Unknown field '%s'\n", args.field);
1376                 }
1377                 break;
1378         }
1379
1380         return 0;
1381 }
1382
1383 static struct ast_custom_function calendar_query_result_function = {
1384         .name = "CALENDAR_QUERY_RESULT",
1385         .read = calendar_query_result_exec,
1386 };
1387
1388 static int calendar_write_exec(struct ast_channel *chan, const char *cmd, char *data, const char *value)
1389 {
1390         int i, j, ret = -1;
1391         char *val_dup = NULL;
1392         struct ast_calendar *cal = NULL;
1393         struct ast_calendar_event *event = NULL;
1394         struct timeval tv = ast_tvnow();
1395         AST_DECLARE_APP_ARGS(fields,
1396                 AST_APP_ARG(field)[10];
1397         );
1398         AST_DECLARE_APP_ARGS(values,
1399                 AST_APP_ARG(value)[10];
1400         );
1401
1402         if (!(val_dup = ast_strdup(value))) {
1403                 ast_log(LOG_ERROR, "Could not allocate memory for values\n");
1404                 goto write_cleanup;
1405         }
1406
1407         AST_STANDARD_APP_ARGS(fields, data);
1408         AST_STANDARD_APP_ARGS(values, val_dup);
1409
1410         /* XXX Eventually we will support unnamed calendars, so if we don't find one, we parse
1411          * for a calendar type and create it */
1412         if (!(cal = find_calendar(fields.field[0]))) {
1413                 ast_log(LOG_WARNING, "Couldn't find calendar '%s'\n", fields.field[0]);
1414                 goto write_cleanup;
1415         }
1416
1417         if (!(cal->tech->write_event)) {
1418                 ast_log(LOG_WARNING, "Calendar '%s' has no write function!\n", cal->name);
1419                 goto write_cleanup;
1420         }
1421
1422         if (!(event = ast_calendar_event_alloc(cal))) {
1423                 goto write_cleanup;
1424         }
1425
1426         if (ast_strlen_zero(fields.field[0])) {
1427                 ast_log(LOG_WARNING, "CALENDAR_WRITE requires a calendar name!\n");
1428                 goto write_cleanup;
1429         }
1430
1431         if (fields.argc - 1 != values.argc) {
1432                 ast_log(LOG_WARNING, "CALENDAR_WRITE should have the same number of fields (%d) and values (%d)!\n", fields.argc - 1, values.argc);
1433                 goto write_cleanup;
1434         }
1435
1436         event->owner = cal;
1437
1438         for (i = 1, j = 0; i < fields.argc; i++, j++) {
1439                 if (!strcasecmp(fields.field[i], "summary")) {
1440                         ast_string_field_set(event, summary, values.value[j]);
1441                 } else if (!strcasecmp(fields.field[i], "description")) {
1442                         ast_string_field_set(event, description, values.value[j]);
1443                 } else if (!strcasecmp(fields.field[i], "organizer")) {
1444                         ast_string_field_set(event, organizer, values.value[j]);
1445                 } else if (!strcasecmp(fields.field[i], "location")) {
1446                         ast_string_field_set(event, location, values.value[j]);
1447                 } else if (!strcasecmp(fields.field[i], "categories")) {
1448                         ast_string_field_set(event, categories, values.value[j]);
1449                 } else if (!strcasecmp(fields.field[i], "priority")) {
1450                         event->priority = atoi(values.value[j]);
1451                 } else if (!strcasecmp(fields.field[i], "uid")) {
1452                         ast_string_field_set(event, uid, values.value[j]);
1453                 } else if (!strcasecmp(fields.field[i], "start")) {
1454                         event->start = atoi(values.value[j]);
1455                 } else if (!strcasecmp(fields.field[i], "end")) {
1456                         event->end = atoi(values.value[j]);
1457                 } else if (!strcasecmp(fields.field[i], "busystate")) {
1458                         event->busy_state = atoi(values.value[j]);
1459                 } else {
1460                         ast_log(LOG_WARNING, "Unknown calendar event field '%s'\n", fields.field[i]);
1461                 }
1462         }
1463
1464         if (!event->start) {
1465                 event->start = tv.tv_sec;
1466         }
1467
1468         if (!event->end) {
1469                 event->end = tv.tv_sec;
1470         }
1471
1472         if((ret = cal->tech->write_event(event))) {
1473                 ast_log(LOG_WARNING, "Writing event to calendar '%s' failed!\n", cal->name);
1474         }
1475
1476 write_cleanup:
1477         if (ret) {
1478                 pbx_builtin_setvar_helper(chan, "CALENDAR_SUCCESS", "0");
1479         } else {
1480                 pbx_builtin_setvar_helper(chan, "CALENDAR_SUCCESS", "1");
1481         }
1482         if (cal) {
1483                 cal = unref_calendar(cal);
1484         }
1485         if (event) {
1486                 event = ast_calendar_unref_event(event);
1487         }
1488         if (val_dup) {
1489                 ast_free(val_dup);
1490         }
1491
1492         return ret;
1493 }
1494
1495 static struct ast_custom_function calendar_write_function = {
1496         .name = "CALENDAR_WRITE",
1497         .write = calendar_write_exec,
1498 };
1499
1500 /*! \brief CLI command to list available calendars */
1501 static char *handle_show_calendars(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1502 {
1503 #define FORMAT "%-20.20s %-10.10s %-6.6s\n"
1504         struct ao2_iterator i;
1505         struct ast_calendar *cal;
1506
1507         switch(cmd) {
1508         case CLI_INIT:
1509                 e->command = "calendar show calendars";
1510                 e->usage =
1511                         "Usage: calendar show calendars\n"
1512                         "       Lists all registered calendars.\n";
1513                 return NULL;
1514         case CLI_GENERATE:
1515                 return NULL;
1516         }
1517
1518         ast_cli(a->fd, FORMAT, "Calendar", "Type", "Status");
1519         ast_cli(a->fd, FORMAT, "--------", "----", "------");
1520         i = ao2_iterator_init(calendars, 0);
1521         while ((cal = ao2_iterator_next(&i))) {
1522                 ast_cli(a->fd, FORMAT, cal->name, cal->tech->type, calendar_is_busy(cal) ? "busy" : "free");
1523                 cal = unref_calendar(cal);
1524         }
1525         ao2_iterator_destroy(&i);
1526
1527         return CLI_SUCCESS;
1528 #undef FORMAT
1529 }
1530
1531 /*! \brief CLI command to list of all calendars types currently loaded on the backend */
1532 static char *handle_show_calendars_types(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1533 {
1534 #define FORMAT "%-10.10s %-30.30s\n"
1535         struct ast_calendar_tech *iter;
1536
1537
1538         switch(cmd) {
1539         case CLI_INIT:
1540                 e->command = "calendar show types";
1541                 e->usage =
1542                         "Usage: calendar show types\n"
1543                         "       Lists all registered calendars types.\n";
1544                 return NULL;
1545         case CLI_GENERATE:
1546                 return NULL;
1547         }
1548
1549         ast_cli(a->fd, FORMAT, "Type", "Description");
1550         AST_LIST_LOCK(&techs);
1551         AST_LIST_TRAVERSE(&techs, iter, list) {
1552                 ast_cli(a->fd, FORMAT, iter->type, iter->description);
1553         }
1554         AST_LIST_UNLOCK(&techs);
1555
1556         return CLI_SUCCESS;
1557 #undef FORMAT
1558 }
1559
1560 static char *epoch_to_string(char *buf, size_t buflen, time_t epoch)
1561 {
1562         struct ast_tm tm;
1563         struct timeval tv = {
1564                 .tv_sec = epoch,
1565         };
1566
1567         if (!epoch) {
1568                 *buf = '\0';
1569                 return buf;
1570         }
1571         ast_localtime(&tv, &tm, NULL);
1572         ast_strftime(buf, buflen, "%F %r %z", &tm);
1573
1574         return buf;
1575 }
1576
1577 static char *handle_show_calendar(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1578 {
1579 #define FORMAT "%-17.17s : %-20.20s\n"
1580 #define FORMAT2 "%-12.12s: %-40.60s\n"
1581         struct ao2_iterator i;
1582         struct ast_calendar *cal;
1583         struct ast_calendar_event *event;
1584         int which = 0;
1585         char *ret = NULL;
1586
1587         switch(cmd) {
1588         case CLI_INIT:
1589                 e->command = "calendar show calendar";
1590                 e->usage =
1591                         "Usage: calendar show calendar <calendar name>\n"
1592                         "       Displays information about a calendar\n";
1593                 return NULL;
1594
1595         case CLI_GENERATE:
1596                 if (a->pos != 3) {
1597                         return NULL;
1598                 }
1599                 i = ao2_iterator_init(calendars, 0);
1600                 while ((cal = ao2_iterator_next(&i))) {
1601                         if (!strncasecmp(a->word, cal->name, strlen(a->word)) && ++which > a->n) {
1602                                 ret = ast_strdup(cal->name);
1603                                 cal = unref_calendar(cal);
1604                                 break;
1605                         }
1606                         cal = unref_calendar(cal);
1607                 }
1608                 ao2_iterator_destroy(&i);
1609                 return ret;
1610         }
1611
1612         if (a->argc != 4) {
1613                 return CLI_SHOWUSAGE;
1614         }
1615
1616         if (!(cal = find_calendar(a->argv[3]))) {
1617                 return NULL;
1618         }
1619
1620         ast_cli(a->fd, FORMAT, "Name", cal->name);
1621         ast_cli(a->fd, FORMAT, "Notify channel", cal->notify_channel);
1622         ast_cli(a->fd, FORMAT, "Notify context", cal->notify_context);
1623         ast_cli(a->fd, FORMAT, "Notify extension", cal->notify_extension);
1624         ast_cli(a->fd, FORMAT, "Notify application", cal->notify_app);
1625         ast_cli(a->fd, FORMAT, "Notify appdata", cal->notify_appdata);
1626         ast_cli(a->fd, "%-17.17s : %d\n", "Refresh time", cal->refresh);
1627         ast_cli(a->fd, "%-17.17s : %d\n", "Timeframe", cal->timeframe);
1628         ast_cli(a->fd, "%-17.17s : %d\n", "Autoreminder", cal->autoreminder);
1629         ast_cli(a->fd, "%s\n", "Events");
1630         ast_cli(a->fd, "%s\n", "------");
1631
1632         i = ao2_iterator_init(cal->events, 0);
1633         while ((event = ao2_iterator_next(&i))) {
1634                 char buf[100];
1635
1636                 ast_cli(a->fd, FORMAT2, "Summary", event->summary);
1637                 ast_cli(a->fd, FORMAT2, "Description", event->description);
1638                 ast_cli(a->fd, FORMAT2, "Organizer", event->organizer);
1639                 ast_cli(a->fd, FORMAT2, "Location", event->location);
1640                 ast_cli(a->fd, FORMAT2, "Categories", event->categories);
1641                 ast_cli(a->fd, "%-12.12s: %d\n", "Priority", event->priority);
1642                 ast_cli(a->fd, FORMAT2, "UID", event->uid);
1643                 ast_cli(a->fd, FORMAT2, "Start", epoch_to_string(buf, sizeof(buf), event->start));
1644                 ast_cli(a->fd, FORMAT2, "End", epoch_to_string(buf, sizeof(buf), event->end));
1645                 ast_cli(a->fd, FORMAT2, "Alarm", epoch_to_string(buf, sizeof(buf), event->alarm));
1646                 ast_cli(a->fd, "\n");
1647
1648                 event = ast_calendar_unref_event(event);
1649         }
1650         ao2_iterator_destroy(&i);
1651         cal = unref_calendar(cal);
1652         return CLI_SUCCESS;
1653 #undef FORMAT
1654 #undef FORMAT2
1655 }
1656
1657 static char *handle_dump_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1658 {
1659         switch(cmd) {
1660         case CLI_INIT:
1661                 e->command = "calendar dump sched";
1662                 e->usage =
1663                         "Usage: calendar dump sched\n"
1664                         "       Dump the calendar sched context";
1665                 return NULL;
1666
1667         case CLI_GENERATE:
1668                 return NULL;
1669         }
1670
1671         ast_sched_dump(sched);
1672
1673         return CLI_SUCCESS;
1674 }
1675
1676 static struct ast_cli_entry calendar_cli[] = {
1677         AST_CLI_DEFINE(handle_show_calendar, "Display information about a calendar"),
1678         AST_CLI_DEFINE(handle_show_calendars, "Show registered calendars"),
1679         AST_CLI_DEFINE(handle_dump_sched, "Dump calendar sched context"),
1680         AST_CLI_DEFINE(handle_show_calendars_types, "Show all calendar types loaded"),
1681 };
1682
1683 static int calendar_event_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1684 {
1685         struct ast_datastore *datastore;
1686         struct ast_calendar_event *event;
1687
1688         if (ast_strlen_zero(data)) {
1689                 ast_log(LOG_WARNING, "%s requires an argument\n", cmd);
1690                 return -1;
1691         }
1692
1693         ast_channel_lock(chan);
1694         if (!(datastore = ast_channel_datastore_find(chan, &event_notification_datastore, NULL))) {
1695                 ast_log(LOG_WARNING, "There is no event notification datastore on '%s'!\n", ast_channel_name(chan));
1696                 ast_channel_unlock(chan);
1697                 return -1;
1698         }
1699         ast_channel_unlock(chan);
1700
1701         if (!(event = datastore->data)) {
1702                 ast_log(LOG_WARNING, "The datastore contains no data!\n");
1703                 return -1;
1704         }
1705
1706         if (!strcasecmp(data, "summary")) {
1707                 ast_copy_string(buf, event->summary, len);
1708         } else if (!strcasecmp(data, "description")) {
1709                 ast_copy_string(buf, event->description, len);
1710         } else if (!strcasecmp(data, "organizer")) {
1711                 ast_copy_string(buf, event->organizer, len);
1712         } else if (!strcasecmp(data, "location")) {
1713                 ast_copy_string(buf, event->location, len);
1714         } else if (!strcasecmp(data, "categories")) {
1715                 ast_copy_string(buf, event->categories, len);
1716         } else if (!strcasecmp(data, "priority")) {
1717                 snprintf(buf, len, "%d", event->priority);
1718         } else if (!strcasecmp(data, "calendar")) {
1719                 ast_copy_string(buf, event->owner->name, len);
1720         } else if (!strcasecmp(data, "uid")) {
1721                 ast_copy_string(buf, event->uid, len);
1722         } else if (!strcasecmp(data, "start")) {
1723                 snprintf(buf, len, "%ld", (long)event->start);
1724         } else if (!strcasecmp(data, "end")) {
1725                 snprintf(buf, len, "%ld", (long)event->end);
1726         } else if (!strcasecmp(data, "busystate")) {
1727                 snprintf(buf, len, "%d", event->busy_state);
1728         } else if (!strcasecmp(data, "attendees")) {
1729                 calendar_join_attendees(event, buf, len);
1730         }
1731
1732
1733         return 0;
1734 }
1735
1736 static struct ast_custom_function calendar_event_function = {
1737         .name = "CALENDAR_EVENT",
1738         .read = calendar_event_read,
1739 };
1740
1741 static int cb_pending_deletion(void *user_data, void *arg, int flags)
1742 {
1743         struct ast_calendar *cal = user_data;
1744
1745         cal->pending_deletion = 1;
1746
1747         return CMP_MATCH;
1748 }
1749
1750 static int cb_rm_pending_deletion(void *user_data, void *arg, int flags)
1751 {
1752         struct ast_calendar *cal = user_data;
1753
1754         return cal->pending_deletion ? CMP_MATCH : 0;
1755 }
1756
1757 static int reload(void)
1758 {
1759         struct ast_calendar_tech *iter;
1760
1761         ast_mutex_lock(&reloadlock);
1762
1763         /* Mark existing calendars for deletion */
1764         ao2_callback(calendars, OBJ_NODATA | OBJ_MULTIPLE, cb_pending_deletion, NULL);
1765         load_config(1);
1766
1767         AST_LIST_LOCK(&techs);
1768         AST_LIST_TRAVERSE(&techs, iter, list) {
1769                 if (load_tech_calendars(iter)) {
1770                         ast_log(LOG_WARNING, "Failed to reload %s calendars, module disabled\n", iter->type);
1771                 }
1772         }
1773         AST_LIST_UNLOCK(&techs);
1774
1775         /* Delete calendars that no longer show up in the config */
1776         ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, cb_rm_pending_deletion, NULL);
1777
1778         ast_mutex_unlock(&reloadlock);
1779
1780         return 0;
1781 }
1782
1783 static void *do_refresh(void *data)
1784 {
1785         for (;;) {
1786                 struct timeval now = ast_tvnow();
1787                 struct timespec ts = {0,};
1788                 int wait;
1789
1790                 ast_mutex_lock(&refreshlock);
1791
1792                 while (!module_unloading) {
1793                         if ((wait = ast_sched_wait(sched)) < 0) {
1794                                 wait = 1000;
1795                         }
1796
1797                         ts.tv_sec = (now.tv_sec + wait / 1000) + 1;
1798                         if (ast_cond_timedwait(&refresh_condition, &refreshlock, &ts) == ETIMEDOUT) {
1799                                 break;
1800                         }
1801                 }
1802                 ast_mutex_unlock(&refreshlock);
1803
1804                 if (module_unloading) {
1805                         break;
1806                 }
1807                 ast_sched_runq(sched);
1808         }
1809
1810         return NULL;
1811 }
1812
1813 /* If I were to allow unloading it would look something like this */
1814 static int unload_module(void)
1815 {
1816         struct ast_calendar_tech *tech;
1817
1818         ast_devstate_prov_del("calendar");
1819         ast_custom_function_unregister(&calendar_busy_function);
1820         ast_custom_function_unregister(&calendar_event_function);
1821         ast_custom_function_unregister(&calendar_query_function);
1822         ast_custom_function_unregister(&calendar_query_result_function);
1823         ast_custom_function_unregister(&calendar_write_function);
1824         ast_cli_unregister_multiple(calendar_cli, ARRAY_LEN(calendar_cli));
1825
1826         /* Remove all calendars */
1827         ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1828         ao2_cleanup(calendars);
1829         calendars = NULL;
1830
1831         ast_mutex_lock(&refreshlock);
1832         module_unloading = 1;
1833         ast_cond_signal(&refresh_condition);
1834         ast_mutex_unlock(&refreshlock);
1835         pthread_join(refresh_thread, NULL);
1836
1837         AST_LIST_LOCK(&techs);
1838         AST_LIST_TRAVERSE_SAFE_BEGIN(&techs, tech, list) {
1839                 ast_unload_resource(tech->module, 0);
1840         }
1841         AST_LIST_TRAVERSE_SAFE_END;
1842         AST_LIST_UNLOCK(&techs);
1843
1844         ast_config_destroy(calendar_config);
1845         calendar_config = NULL;
1846
1847         return 0;
1848 }
1849
1850 /*!
1851  * \brief Load the module
1852  *
1853  * Module loading including tests for configuration or dependencies.
1854  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
1855  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
1856  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the 
1857  * configuration file or other non-critical problem return 
1858  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
1859  */
1860 static int load_module(void)
1861 {
1862         if (!(calendars = ao2_container_alloc(CALENDAR_BUCKETS, calendar_hash_fn, calendar_cmp_fn))) {
1863                 ast_log(LOG_ERROR, "Unable to allocate calendars container!\n");
1864                 return AST_MODULE_LOAD_FAILURE;
1865         }
1866
1867         if (load_config(0)) {
1868                 /* We don't have calendar support enabled */
1869                 return AST_MODULE_LOAD_DECLINE;
1870         }
1871
1872         ast_mutex_init(&refreshlock);
1873         ast_cond_init(&refresh_condition, NULL);
1874         ast_mutex_init(&reloadlock);
1875
1876         if (!(sched = ast_sched_context_create())) {
1877                 ast_log(LOG_ERROR, "Unable to create sched context\n");
1878                 return AST_MODULE_LOAD_FAILURE;
1879         }
1880
1881         if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
1882                 ast_log(LOG_ERROR, "Unable to start refresh thread--notifications disabled!\n");
1883         }
1884
1885         ast_custom_function_register(&calendar_busy_function);
1886         ast_custom_function_register(&calendar_event_function);
1887         ast_custom_function_register(&calendar_query_function);
1888         ast_custom_function_register(&calendar_query_result_function);
1889         ast_custom_function_register(&calendar_write_function);
1890         ast_cli_register_multiple(calendar_cli, ARRAY_LEN(calendar_cli));
1891
1892         ast_devstate_prov_add("Calendar", calendarstate);
1893
1894         return AST_MODULE_LOAD_SUCCESS;
1895 }
1896 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk Calendar integration",
1897                 .load = load_module,
1898                 .unload = unload_module,
1899                 .reload = reload,
1900                 .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
1901         );