res_calendar.c 53 KB

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