cel_manager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008 - 2009, Digium, Inc.
  5. *
  6. * Steve Murphy <murf@digium.com>
  7. * who freely borrowed code from the cdr equivalents
  8. * (see cdr/cdr_manager.c)
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief Asterisk Channel Event records.
  23. *
  24. * See also
  25. * \arg \ref AstCDR
  26. * \arg \ref AstAMI
  27. * \arg \ref Config_ami
  28. * \ingroup cel_drivers
  29. */
  30. /*** MODULEINFO
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include "asterisk/channel.h"
  36. #include "asterisk/cel.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/logger.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/manager.h"
  41. #include "asterisk/config.h"
  42. static const char DATE_FORMAT[] = "%Y-%m-%d %T";
  43. static const char CONF_FILE[] = "cel.conf";
  44. static int enablecel;
  45. static struct ast_event_sub *event_sub;
  46. static void manager_log(const struct ast_event *event, void *userdata)
  47. {
  48. struct ast_tm timeresult;
  49. char start_time[80] = "";
  50. struct ast_cel_event_record record = {
  51. .version = AST_CEL_EVENT_RECORD_VERSION,
  52. };
  53. if (!enablecel) {
  54. return;
  55. }
  56. if (ast_cel_fill_record(event, &record)) {
  57. return;
  58. }
  59. ast_localtime(&record.event_time, &timeresult, NULL);
  60. ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
  61. manager_event(EVENT_FLAG_CALL, "CEL",
  62. "EventName: %s\r\n"
  63. "AccountCode: %s\r\n"
  64. "CallerIDnum: %s\r\n"
  65. "CallerIDname: %s\r\n"
  66. "CallerIDani: %s\r\n"
  67. "CallerIDrdnis: %s\r\n"
  68. "CallerIDdnid: %s\r\n"
  69. "Exten: %s\r\n"
  70. "Context: %s\r\n"
  71. "Channel: %s\r\n"
  72. "Application: %s\r\n"
  73. "AppData: %s\r\n"
  74. "EventTime: %s\r\n"
  75. "AMAFlags: %s\r\n"
  76. "UniqueID: %s\r\n"
  77. "LinkedID: %s\r\n"
  78. "Userfield: %s\r\n"
  79. "Peer: %s\r\n"
  80. "PeerAccount: %s\r\n"
  81. "Extra: %s\r\n",
  82. record.event_name,
  83. record.account_code,
  84. record.caller_id_num,
  85. record.caller_id_name,
  86. record.caller_id_ani,
  87. record.caller_id_rdnis,
  88. record.caller_id_dnid,
  89. record.extension,
  90. record.context,
  91. record.channel_name,
  92. record.application_name,
  93. record.application_data,
  94. start_time,
  95. ast_cel_get_ama_flag_name(record.amaflag),
  96. record.unique_id,
  97. record.linked_id,
  98. record.user_field,
  99. record.peer,
  100. record.peer_account,
  101. record.extra);
  102. }
  103. static int load_config(int reload)
  104. {
  105. const char *cat = NULL;
  106. struct ast_config *cfg;
  107. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  108. struct ast_variable *v;
  109. int newenablecel = 0;
  110. cfg = ast_config_load(CONF_FILE, config_flags);
  111. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  112. return 0;
  113. }
  114. if (!cfg) {
  115. ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
  116. enablecel = 0;
  117. return -1;
  118. }
  119. while ((cat = ast_category_browse(cfg, cat))) {
  120. if (strcasecmp(cat, "manager")) {
  121. continue;
  122. }
  123. for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
  124. if (!strcasecmp(v->name, "enabled")) {
  125. newenablecel = ast_true(v->value);
  126. } else {
  127. ast_log(LOG_NOTICE, "Unknown option '%s' specified "
  128. "for cel_manager.\n", v->name);
  129. }
  130. }
  131. }
  132. ast_config_destroy(cfg);
  133. if (enablecel && !newenablecel) {
  134. if (event_sub) {
  135. event_sub = ast_event_unsubscribe(event_sub);
  136. }
  137. } else if (!enablecel && newenablecel) {
  138. event_sub = ast_event_subscribe(AST_EVENT_CEL, manager_log, "Manager Event Logging", NULL, AST_EVENT_IE_END);
  139. if (!event_sub) {
  140. ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
  141. }
  142. }
  143. enablecel = newenablecel;
  144. return 0;
  145. }
  146. static int unload_module(void)
  147. {
  148. if (event_sub) {
  149. event_sub = ast_event_unsubscribe(event_sub);
  150. }
  151. return 0;
  152. }
  153. static int load_module(void)
  154. {
  155. if (load_config(0)) {
  156. return AST_MODULE_LOAD_DECLINE;
  157. }
  158. return AST_MODULE_LOAD_SUCCESS;
  159. }
  160. static int reload(void)
  161. {
  162. return load_config(1);
  163. }
  164. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
  165. .load = load_module,
  166. .unload = unload_module,
  167. .reload = reload,
  168. .load_pri = AST_MODPRI_CDR_DRIVER,
  169. );