presencestate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011-2012, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@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. *
  20. * \brief Presence state management
  21. */
  22. /*** MODULEINFO
  23. <support_level>core</support_level>
  24. ***/
  25. /*** DOCUMENTATION
  26. <managerEvent language="en_US" name="PresenceStateChange">
  27. <managerEventInstance class="EVENT_FLAG_CALL">
  28. <synopsis>Raised when a presence state changes</synopsis>
  29. <syntax>
  30. <parameter name="Presentity">
  31. <para>The entity whose presence state has changed</para>
  32. </parameter>
  33. <parameter name="Status">
  34. <para>The new status of the presentity</para>
  35. </parameter>
  36. <parameter name="Subtype">
  37. <para>The new subtype of the presentity</para>
  38. </parameter>
  39. <parameter name="Message">
  40. <para>The new message of the presentity</para>
  41. </parameter>
  42. </syntax>
  43. <description>
  44. <para>This differs from the <literal>PresenceStatus</literal>
  45. event because this event is raised for all presence state changes,
  46. not only for changes that affect dialplan hints.</para>
  47. </description>
  48. <see-also>
  49. <ref type="managerEvent">PresenceStatus</ref>
  50. </see-also>
  51. </managerEventInstance>
  52. </managerEvent>
  53. ***/
  54. #include "asterisk.h"
  55. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  56. #include "asterisk/_private.h"
  57. #include "asterisk/utils.h"
  58. #include "asterisk/lock.h"
  59. #include "asterisk/linkedlists.h"
  60. #include "asterisk/presencestate.h"
  61. #include "asterisk/pbx.h"
  62. #include "asterisk/app.h"
  63. /*! \brief Device state strings for printing */
  64. static const struct {
  65. const char *string;
  66. enum ast_presence_state state;
  67. } state2string[] = {
  68. { "not_set", AST_PRESENCE_NOT_SET},
  69. { "unavailable", AST_PRESENCE_UNAVAILABLE },
  70. { "available", AST_PRESENCE_AVAILABLE},
  71. { "away", AST_PRESENCE_AWAY},
  72. { "xa", AST_PRESENCE_XA},
  73. { "chat", AST_PRESENCE_CHAT},
  74. { "dnd", AST_PRESENCE_DND},
  75. };
  76. static struct ast_manager_event_blob *presence_state_to_ami(struct stasis_message *msg);
  77. STASIS_MESSAGE_TYPE_DEFN(ast_presence_state_message_type,
  78. .to_ami = presence_state_to_ami,
  79. );
  80. struct stasis_topic *presence_state_topic_all;
  81. struct stasis_cache *presence_state_cache;
  82. struct stasis_caching_topic *presence_state_topic_cached;
  83. /*! \brief A presence state provider */
  84. struct presence_state_provider {
  85. char label[40];
  86. ast_presence_state_prov_cb_type callback;
  87. AST_RWLIST_ENTRY(presence_state_provider) list;
  88. };
  89. /*! \brief A list of providers */
  90. static AST_RWLIST_HEAD_STATIC(presence_state_providers, presence_state_provider);
  91. const char *ast_presence_state2str(enum ast_presence_state state)
  92. {
  93. int i;
  94. for (i = 0; i < ARRAY_LEN(state2string); i++) {
  95. if (state == state2string[i].state) {
  96. return state2string[i].string;
  97. }
  98. }
  99. return "";
  100. }
  101. enum ast_presence_state ast_presence_state_val(const char *val)
  102. {
  103. int i;
  104. for (i = 0; i < ARRAY_LEN(state2string); i++) {
  105. if (!strcasecmp(val, state2string[i].string)) {
  106. return state2string[i].state;
  107. }
  108. }
  109. return AST_PRESENCE_INVALID;
  110. }
  111. static enum ast_presence_state presence_state_cached(const char *presence_provider, char **subtype, char **message)
  112. {
  113. enum ast_presence_state res = AST_PRESENCE_INVALID;
  114. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  115. struct ast_presence_state_message *presence_state;
  116. msg = stasis_cache_get(ast_presence_state_cache(), ast_presence_state_message_type(), presence_provider);
  117. if (!msg) {
  118. return res;
  119. }
  120. presence_state = stasis_message_data(msg);
  121. res = presence_state->state;
  122. *subtype = !ast_strlen_zero(presence_state->subtype) ? ast_strdup(presence_state->subtype) : NULL;
  123. *message = !ast_strlen_zero(presence_state->message) ? ast_strdup(presence_state->message) : NULL;
  124. return res;
  125. }
  126. static enum ast_presence_state ast_presence_state_helper(const char *presence_provider, char **subtype, char **message, int check_cache)
  127. {
  128. struct presence_state_provider *provider;
  129. char *address;
  130. char *label = ast_strdupa(presence_provider);
  131. int res = AST_PRESENCE_INVALID;
  132. *subtype = NULL;
  133. *message = NULL;
  134. if (check_cache) {
  135. res = presence_state_cached(presence_provider, subtype, message);
  136. if (res != AST_PRESENCE_INVALID) {
  137. return res;
  138. }
  139. }
  140. if ((address = strchr(label, ':'))) {
  141. *address = '\0';
  142. address++;
  143. } else {
  144. ast_log(LOG_WARNING, "No label found for presence state provider: %s\n", presence_provider);
  145. return res;
  146. }
  147. AST_RWLIST_RDLOCK(&presence_state_providers);
  148. AST_RWLIST_TRAVERSE(&presence_state_providers, provider, list) {
  149. ast_debug(5, "Checking provider %s with %s\n", provider->label, label);
  150. if (!strcasecmp(provider->label, label)) {
  151. res = provider->callback(address, subtype, message);
  152. break;
  153. }
  154. }
  155. AST_RWLIST_UNLOCK(&presence_state_providers);
  156. if (!provider) {
  157. ast_log(LOG_WARNING, "No provider found for label %s\n", label);
  158. }
  159. return res;
  160. }
  161. enum ast_presence_state ast_presence_state(const char *presence_provider, char **subtype, char **message)
  162. {
  163. return ast_presence_state_helper(presence_provider, subtype, message, 1);
  164. }
  165. enum ast_presence_state ast_presence_state_nocache(const char *presence_provider, char **subtype, char **message)
  166. {
  167. return ast_presence_state_helper(presence_provider, subtype, message, 0);
  168. }
  169. int ast_presence_state_prov_add(const char *label, ast_presence_state_prov_cb_type callback)
  170. {
  171. struct presence_state_provider *provider;
  172. if (!callback || !(provider = ast_calloc(1, sizeof(*provider)))) {
  173. return -1;
  174. }
  175. provider->callback = callback;
  176. ast_copy_string(provider->label, label, sizeof(provider->label));
  177. AST_RWLIST_WRLOCK(&presence_state_providers);
  178. AST_RWLIST_INSERT_HEAD(&presence_state_providers, provider, list);
  179. AST_RWLIST_UNLOCK(&presence_state_providers);
  180. return 0;
  181. }
  182. int ast_presence_state_prov_del(const char *label)
  183. {
  184. struct presence_state_provider *provider;
  185. int res = -1;
  186. AST_RWLIST_WRLOCK(&presence_state_providers);
  187. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&presence_state_providers, provider, list) {
  188. if (!strcasecmp(provider->label, label)) {
  189. AST_RWLIST_REMOVE_CURRENT(list);
  190. ast_free(provider);
  191. res = 0;
  192. break;
  193. }
  194. }
  195. AST_RWLIST_TRAVERSE_SAFE_END;
  196. AST_RWLIST_UNLOCK(&presence_state_providers);
  197. return res;
  198. }
  199. static void presence_state_dtor(void *obj)
  200. {
  201. struct ast_presence_state_message *presence_state = obj;
  202. ast_string_field_free_memory(presence_state);
  203. }
  204. static struct ast_presence_state_message *presence_state_alloc(const char *provider,
  205. enum ast_presence_state state,
  206. const char *subtype,
  207. const char *message)
  208. {
  209. RAII_VAR(struct ast_presence_state_message *, presence_state, ao2_alloc(sizeof(*presence_state), presence_state_dtor), ao2_cleanup);
  210. if (!presence_state || ast_string_field_init(presence_state, 256)) {
  211. return NULL;
  212. }
  213. presence_state->state = state;
  214. ast_string_field_set(presence_state, provider, provider);
  215. ast_string_field_set(presence_state, subtype, S_OR(subtype, ""));
  216. ast_string_field_set(presence_state, message, S_OR(message, ""));
  217. ao2_ref(presence_state, +1);
  218. return presence_state;
  219. }
  220. static void presence_state_event(const char *provider,
  221. enum ast_presence_state state,
  222. const char *subtype,
  223. const char *message)
  224. {
  225. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  226. RAII_VAR(struct ast_presence_state_message *, presence_state, NULL, ao2_cleanup);
  227. if (!ast_presence_state_message_type()) {
  228. return;
  229. }
  230. presence_state = presence_state_alloc(provider, state, subtype, message);
  231. if (!presence_state) {
  232. return;
  233. }
  234. msg = stasis_message_create(ast_presence_state_message_type(), presence_state);
  235. if (!msg) {
  236. return;
  237. }
  238. stasis_publish(ast_presence_state_topic_all(), msg);
  239. }
  240. static void do_presence_state_change(const char *provider)
  241. {
  242. char *subtype = NULL;
  243. char *message = NULL;
  244. enum ast_presence_state state;
  245. state = ast_presence_state_helper(provider, &subtype, &message, 0);
  246. if (state == AST_PRESENCE_INVALID) {
  247. return;
  248. }
  249. presence_state_event(provider, state, subtype, message);
  250. ast_free(subtype);
  251. ast_free(message);
  252. }
  253. int ast_presence_state_changed_literal(enum ast_presence_state state,
  254. const char *subtype,
  255. const char *message,
  256. const char *presence_provider)
  257. {
  258. if (state == AST_PRESENCE_NOT_SET) {
  259. do_presence_state_change(presence_provider);
  260. } else {
  261. presence_state_event(presence_provider, state, subtype, message);
  262. }
  263. return 0;
  264. }
  265. int ast_presence_state_changed(enum ast_presence_state state,
  266. const char *subtype,
  267. const char *message,
  268. const char *fmt, ...)
  269. {
  270. char buf[AST_MAX_EXTENSION];
  271. va_list ap;
  272. va_start(ap, fmt);
  273. vsnprintf(buf, sizeof(buf), fmt, ap);
  274. va_end(ap);
  275. return ast_presence_state_changed_literal(state, subtype, message, buf);
  276. }
  277. struct stasis_topic *ast_presence_state_topic_all(void)
  278. {
  279. return presence_state_topic_all;
  280. }
  281. struct stasis_cache *ast_presence_state_cache(void)
  282. {
  283. return presence_state_cache;
  284. }
  285. struct stasis_topic *ast_presence_state_topic_cached(void)
  286. {
  287. return stasis_caching_get_topic(presence_state_topic_cached);
  288. }
  289. static const char *presence_state_get_id(struct stasis_message *msg)
  290. {
  291. struct ast_presence_state_message *presence_state = stasis_message_data(msg);
  292. if (stasis_message_type(msg) != ast_presence_state_message_type()) {
  293. return NULL;
  294. }
  295. return presence_state->provider;
  296. }
  297. static void presence_state_engine_cleanup(void)
  298. {
  299. ao2_cleanup(presence_state_topic_all);
  300. presence_state_topic_all = NULL;
  301. ao2_cleanup(presence_state_cache);
  302. presence_state_cache = NULL;
  303. presence_state_topic_cached = stasis_caching_unsubscribe_and_join(presence_state_topic_cached);
  304. STASIS_MESSAGE_TYPE_CLEANUP(ast_presence_state_message_type);
  305. }
  306. int ast_presence_state_engine_init(void)
  307. {
  308. ast_register_cleanup(presence_state_engine_cleanup);
  309. if (STASIS_MESSAGE_TYPE_INIT(ast_presence_state_message_type) != 0) {
  310. return -1;
  311. }
  312. presence_state_topic_all = stasis_topic_create("ast_presence_state_topic_all");
  313. if (!presence_state_topic_all) {
  314. return -1;
  315. }
  316. presence_state_cache = stasis_cache_create(presence_state_get_id);
  317. if (!presence_state_cache) {
  318. return -1;
  319. }
  320. presence_state_topic_cached = stasis_caching_topic_create(presence_state_topic_all, presence_state_cache);
  321. if (!presence_state_topic_cached) {
  322. return -1;
  323. }
  324. stasis_caching_accept_message_type(presence_state_topic_cached, ast_presence_state_message_type());
  325. stasis_caching_set_filter(presence_state_topic_cached, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  326. return 0;
  327. }
  328. static struct ast_manager_event_blob *presence_state_to_ami(struct stasis_message *msg)
  329. {
  330. struct ast_presence_state_message *presence_state = stasis_message_data(msg);
  331. struct ast_manager_event_blob *res;
  332. char *subtype = ast_escape_c_alloc(presence_state->subtype);
  333. char *message = ast_escape_c_alloc(presence_state->message);
  334. res = ast_manager_event_blob_create(EVENT_FLAG_CALL, "PresenceStateChange",
  335. "Presentity: %s\r\n"
  336. "Status: %s\r\n"
  337. "Subtype: %s\r\n"
  338. "Message: %s\r\n",
  339. presence_state->provider,
  340. ast_presence_state2str(presence_state->state),
  341. subtype ?: "",
  342. message ?: "");
  343. ast_free(subtype);
  344. ast_free(message);
  345. return res;
  346. }