res_calendar.c 55 KB

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