res_calendar.c 55 KB

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