res_calendar.c 56 KB

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