presencestate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. if (check_cache) {
  133. res = presence_state_cached(presence_provider, subtype, message);
  134. if (res != AST_PRESENCE_INVALID) {
  135. return res;
  136. }
  137. }
  138. if ((address = strchr(label, ':'))) {
  139. *address = '\0';
  140. address++;
  141. } else {
  142. ast_log(LOG_WARNING, "No label found for presence state provider: %s\n", presence_provider);
  143. return res;
  144. }
  145. AST_RWLIST_RDLOCK(&presence_state_providers);
  146. AST_RWLIST_TRAVERSE(&presence_state_providers, provider, list) {
  147. ast_debug(5, "Checking provider %s with %s\n", provider->label, label);
  148. if (!strcasecmp(provider->label, label)) {
  149. res = provider->callback(address, subtype, message);
  150. break;
  151. }
  152. }
  153. AST_RWLIST_UNLOCK(&presence_state_providers);
  154. if (!provider) {
  155. ast_log(LOG_WARNING, "No provider found for label %s\n", label);
  156. }
  157. return res;
  158. }
  159. enum ast_presence_state ast_presence_state(const char *presence_provider, char **subtype, char **message)
  160. {
  161. return ast_presence_state_helper(presence_provider, subtype, message, 1);
  162. }
  163. enum ast_presence_state ast_presence_state_nocache(const char *presence_provider, char **subtype, char **message)
  164. {
  165. return ast_presence_state_helper(presence_provider, subtype, message, 0);
  166. }
  167. int ast_presence_state_prov_add(const char *label, ast_presence_state_prov_cb_type callback)
  168. {
  169. struct presence_state_provider *provider;
  170. if (!callback || !(provider = ast_calloc(1, sizeof(*provider)))) {
  171. return -1;
  172. }
  173. provider->callback = callback;
  174. ast_copy_string(provider->label, label, sizeof(provider->label));
  175. AST_RWLIST_WRLOCK(&presence_state_providers);
  176. AST_RWLIST_INSERT_HEAD(&presence_state_providers, provider, list);
  177. AST_RWLIST_UNLOCK(&presence_state_providers);
  178. return 0;
  179. }
  180. int ast_presence_state_prov_del(const char *label)
  181. {
  182. struct presence_state_provider *provider;
  183. int res = -1;
  184. AST_RWLIST_WRLOCK(&presence_state_providers);
  185. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&presence_state_providers, provider, list) {
  186. if (!strcasecmp(provider->label, label)) {
  187. AST_RWLIST_REMOVE_CURRENT(list);
  188. ast_free(provider);
  189. res = 0;
  190. break;
  191. }
  192. }
  193. AST_RWLIST_TRAVERSE_SAFE_END;
  194. AST_RWLIST_UNLOCK(&presence_state_providers);
  195. return res;
  196. }
  197. static void presence_state_dtor(void *obj)
  198. {
  199. struct ast_presence_state_message *presence_state = obj;
  200. ast_string_field_free_memory(presence_state);
  201. }
  202. static struct ast_presence_state_message *presence_state_alloc(const char *provider,
  203. enum ast_presence_state state,
  204. const char *subtype,
  205. const char *message)
  206. {
  207. RAII_VAR(struct ast_presence_state_message *, presence_state, ao2_alloc(sizeof(*presence_state), presence_state_dtor), ao2_cleanup);
  208. if (!presence_state || ast_string_field_init(presence_state, 256)) {
  209. return NULL;
  210. }
  211. presence_state->state = state;
  212. ast_string_field_set(presence_state, provider, provider);
  213. ast_string_field_set(presence_state, subtype, S_OR(subtype, ""));
  214. ast_string_field_set(presence_state, message, S_OR(message, ""));
  215. ao2_ref(presence_state, +1);
  216. return presence_state;
  217. }
  218. static void presence_state_event(const char *provider,
  219. enum ast_presence_state state,
  220. const char *subtype,
  221. const char *message)
  222. {
  223. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  224. RAII_VAR(struct ast_presence_state_message *, presence_state, NULL, ao2_cleanup);
  225. if (!ast_presence_state_message_type()) {
  226. return;
  227. }
  228. presence_state = presence_state_alloc(provider, state, subtype, message);
  229. if (!presence_state) {
  230. return;
  231. }
  232. msg = stasis_message_create(ast_presence_state_message_type(), presence_state);
  233. if (!msg) {
  234. return;
  235. }
  236. stasis_publish(ast_presence_state_topic_all(), msg);
  237. }
  238. static void do_presence_state_change(const char *provider)
  239. {
  240. char *subtype = NULL;
  241. char *message = NULL;
  242. enum ast_presence_state state;
  243. state = ast_presence_state_helper(provider, &subtype, &message, 0);
  244. if (state == AST_PRESENCE_INVALID) {
  245. return;
  246. }
  247. presence_state_event(provider, state, subtype, message);
  248. ast_free(subtype);
  249. ast_free(message);
  250. }
  251. int ast_presence_state_changed_literal(enum ast_presence_state state,
  252. const char *subtype,
  253. const char *message,
  254. const char *presence_provider)
  255. {
  256. if (state == AST_PRESENCE_NOT_SET) {
  257. do_presence_state_change(presence_provider);
  258. } else {
  259. presence_state_event(presence_provider, state, subtype, message);
  260. }
  261. return 0;
  262. }
  263. int ast_presence_state_changed(enum ast_presence_state state,
  264. const char *subtype,
  265. const char *message,
  266. const char *fmt, ...)
  267. {
  268. char buf[AST_MAX_EXTENSION];
  269. va_list ap;
  270. va_start(ap, fmt);
  271. vsnprintf(buf, sizeof(buf), fmt, ap);
  272. va_end(ap);
  273. return ast_presence_state_changed_literal(state, subtype, message, buf);
  274. }
  275. struct stasis_topic *ast_presence_state_topic_all(void)
  276. {
  277. return presence_state_topic_all;
  278. }
  279. struct stasis_cache *ast_presence_state_cache(void)
  280. {
  281. return presence_state_cache;
  282. }
  283. struct stasis_topic *ast_presence_state_topic_cached(void)
  284. {
  285. return stasis_caching_get_topic(presence_state_topic_cached);
  286. }
  287. static const char *presence_state_get_id(struct stasis_message *msg)
  288. {
  289. struct ast_presence_state_message *presence_state = stasis_message_data(msg);
  290. if (stasis_message_type(msg) != ast_presence_state_message_type()) {
  291. return NULL;
  292. }
  293. return presence_state->provider;
  294. }
  295. static void presence_state_engine_cleanup(void)
  296. {
  297. ao2_cleanup(presence_state_topic_all);
  298. presence_state_topic_all = NULL;
  299. ao2_cleanup(presence_state_cache);
  300. presence_state_cache = NULL;
  301. presence_state_topic_cached = stasis_caching_unsubscribe_and_join(presence_state_topic_cached);
  302. STASIS_MESSAGE_TYPE_CLEANUP(ast_presence_state_message_type);
  303. }
  304. int ast_presence_state_engine_init(void)
  305. {
  306. ast_register_cleanup(presence_state_engine_cleanup);
  307. if (STASIS_MESSAGE_TYPE_INIT(ast_presence_state_message_type) != 0) {
  308. return -1;
  309. }
  310. presence_state_topic_all = stasis_topic_create("ast_presence_state_topic_all");
  311. if (!presence_state_topic_all) {
  312. return -1;
  313. }
  314. presence_state_cache = stasis_cache_create(presence_state_get_id);
  315. if (!presence_state_cache) {
  316. return -1;
  317. }
  318. presence_state_topic_cached = stasis_caching_topic_create(presence_state_topic_all, presence_state_cache);
  319. if (!presence_state_topic_cached) {
  320. return -1;
  321. }
  322. return 0;
  323. }
  324. static struct ast_manager_event_blob *presence_state_to_ami(struct stasis_message *msg)
  325. {
  326. struct ast_presence_state_message *presence_state = stasis_message_data(msg);
  327. return ast_manager_event_blob_create(EVENT_FLAG_CALL, "PresenceStateChange",
  328. "Presentity: %s\r\n"
  329. "Status: %s\r\n"
  330. "Subtype: %s\r\n"
  331. "Message: %s\r\n",
  332. presence_state->provider,
  333. ast_presence_state2str(presence_state->state),
  334. presence_state->subtype,
  335. presence_state->message);
  336. }