app_cdr.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Martin Pycko <martinp@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 Applications connected with CDR engine
  21. *
  22. * \author Martin Pycko <martinp@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/channel.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/stasis.h"
  35. #include "asterisk/stasis_message_router.h"
  36. /*** DOCUMENTATION
  37. <application name="NoCDR" language="en_US">
  38. <synopsis>
  39. Tell Asterisk to not maintain a CDR for this channel.
  40. </synopsis>
  41. <syntax />
  42. <description>
  43. <para>This application will tell Asterisk not to maintain a CDR for
  44. the current channel. This does <emphasis>NOT</emphasis> mean that
  45. information is not tracked; rather, if the channel is hung up no
  46. CDRs will be created for that channel.</para>
  47. <para>If a subsequent call to ResetCDR occurs, all non-finalized
  48. CDRs created for the channel will be enabled.</para>
  49. <note><para>This application is deprecated. Please use the CDR_PROP
  50. function to disable CDRs on a channel.</para></note>
  51. </description>
  52. <see-also>
  53. <ref type="application">ResetCDR</ref>
  54. <ref type="function">CDR_PROP</ref>
  55. </see-also>
  56. </application>
  57. <application name="ResetCDR" language="en_US">
  58. <synopsis>
  59. Resets the Call Data Record.
  60. </synopsis>
  61. <syntax>
  62. <parameter name="options">
  63. <optionlist>
  64. <option name="v">
  65. <para>Save the CDR variables during the reset.</para>
  66. </option>
  67. <option name="e">
  68. <para>Enable the CDRs for this channel only (negate
  69. effects of NoCDR).</para>
  70. </option>
  71. </optionlist>
  72. </parameter>
  73. </syntax>
  74. <description>
  75. <para>This application causes the Call Data Record to be reset.
  76. Depending on the flags passed in, this can have several effects.
  77. With no options, a reset does the following:</para>
  78. <para>1. The <literal>start</literal> time is set to the current time.</para>
  79. <para>2. If the channel is answered, the <literal>answer</literal> time is set to the
  80. current time.</para>
  81. <para>3. All variables are wiped from the CDR. Note that this step
  82. can be prevented with the <literal>v</literal> option.</para>
  83. <para>On the other hand, if the <literal>e</literal> option is
  84. specified, the effects of the NoCDR application will be lifted. CDRs
  85. will be re-enabled for this channel.</para>
  86. <note><para>The <literal>e</literal> option is deprecated. Please
  87. use the CDR_PROP function instead.</para></note>
  88. </description>
  89. <see-also>
  90. <ref type="application">ForkCDR</ref>
  91. <ref type="application">NoCDR</ref>
  92. <ref type="function">CDR_PROP</ref>
  93. </see-also>
  94. </application>
  95. ***/
  96. static const char nocdr_app[] = "NoCDR";
  97. static const char resetcdr_app[] = "ResetCDR";
  98. enum reset_cdr_options {
  99. OPT_DISABLE_DISPATCH = (1 << 0),
  100. OPT_KEEP_VARS = (1 << 1),
  101. OPT_ENABLE = (1 << 2),
  102. };
  103. AST_APP_OPTIONS(resetcdr_opts, {
  104. AST_APP_OPTION('v', AST_CDR_FLAG_KEEP_VARS),
  105. AST_APP_OPTION('e', AST_CDR_FLAG_DISABLE_ALL),
  106. });
  107. STASIS_MESSAGE_TYPE_DEFN_LOCAL(appcdr_message_type);
  108. /*! \internal \brief Payload for the Stasis message sent to manipulate a CDR */
  109. struct app_cdr_message_payload {
  110. /*! The name of the channel to be manipulated */
  111. const char *channel_name;
  112. /*! Disable the CDR for this channel */
  113. int disable:1;
  114. /*! Re-enable the CDR for this channel */
  115. int reenable:1;
  116. /*! Reset the CDR */
  117. int reset:1;
  118. /*! If reseting the CDR, keep the variables */
  119. int keep_variables:1;
  120. };
  121. static void appcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  122. {
  123. struct app_cdr_message_payload *payload;
  124. if (stasis_message_type(message) != appcdr_message_type()) {
  125. return;
  126. }
  127. payload = stasis_message_data(message);
  128. if (!payload) {
  129. return;
  130. }
  131. if (payload->disable) {
  132. if (ast_cdr_set_property(payload->channel_name, AST_CDR_FLAG_DISABLE_ALL)) {
  133. ast_log(AST_LOG_WARNING, "Failed to disable CDRs on channel %s\n",
  134. payload->channel_name);
  135. }
  136. }
  137. if (payload->reenable) {
  138. if (ast_cdr_clear_property(payload->channel_name, AST_CDR_FLAG_DISABLE_ALL)) {
  139. ast_log(AST_LOG_WARNING, "Failed to enable CDRs on channel %s\n",
  140. payload->channel_name);
  141. }
  142. }
  143. if (payload->reset) {
  144. if (ast_cdr_reset(payload->channel_name, payload->keep_variables)) {
  145. ast_log(AST_LOG_WARNING, "Failed to reset CDRs on channel %s\n",
  146. payload->channel_name);
  147. }
  148. }
  149. }
  150. static int publish_app_cdr_message(struct ast_channel *chan, struct app_cdr_message_payload *payload)
  151. {
  152. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  153. RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
  154. if (!router) {
  155. ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
  156. ast_channel_name(chan));
  157. return -1;
  158. }
  159. message = stasis_message_create(appcdr_message_type(), payload);
  160. if (!message) {
  161. ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
  162. payload->channel_name);
  163. return -1;
  164. }
  165. stasis_message_router_publish_sync(router, message);
  166. return 0;
  167. }
  168. static int resetcdr_exec(struct ast_channel *chan, const char *data)
  169. {
  170. RAII_VAR(struct app_cdr_message_payload *, payload,
  171. ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
  172. char *args;
  173. struct ast_flags flags = { 0 };
  174. if (!payload) {
  175. return -1;
  176. }
  177. if (!ast_strlen_zero(data)) {
  178. args = ast_strdupa(data);
  179. ast_app_parse_options(resetcdr_opts, &flags, NULL, args);
  180. }
  181. payload->channel_name = ast_channel_name(chan);
  182. payload->reset = 1;
  183. if (ast_test_flag(&flags, AST_CDR_FLAG_DISABLE_ALL)) {
  184. payload->reenable = 1;
  185. }
  186. if (ast_test_flag(&flags, AST_CDR_FLAG_KEEP_VARS)) {
  187. payload->keep_variables = 1;
  188. }
  189. return publish_app_cdr_message(chan, payload);
  190. }
  191. static int nocdr_exec(struct ast_channel *chan, const char *data)
  192. {
  193. RAII_VAR(struct app_cdr_message_payload *, payload,
  194. ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
  195. if (!payload) {
  196. return -1;
  197. }
  198. payload->channel_name = ast_channel_name(chan);
  199. payload->disable = 1;
  200. return publish_app_cdr_message(chan, payload);
  201. }
  202. static int unload_module(void)
  203. {
  204. RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
  205. if (router) {
  206. stasis_message_router_remove(router, appcdr_message_type());
  207. }
  208. STASIS_MESSAGE_TYPE_CLEANUP(appcdr_message_type);
  209. ast_unregister_application(nocdr_app);
  210. ast_unregister_application(resetcdr_app);
  211. return 0;
  212. }
  213. static int load_module(void)
  214. {
  215. RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
  216. int res = 0;
  217. if (!router) {
  218. return AST_MODULE_LOAD_FAILURE;
  219. }
  220. res |= STASIS_MESSAGE_TYPE_INIT(appcdr_message_type);
  221. res |= ast_register_application_xml(nocdr_app, nocdr_exec);
  222. res |= ast_register_application_xml(resetcdr_app, resetcdr_exec);
  223. res |= stasis_message_router_add(router, appcdr_message_type(),
  224. appcdr_callback, NULL);
  225. if (res) {
  226. return AST_MODULE_LOAD_FAILURE;
  227. }
  228. return AST_MODULE_LOAD_SUCCESS;
  229. }
  230. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Tell Asterisk to not maintain a CDR for the current call");