res_calendar.c 52 KB

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