app_forkcdr.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Anthony Minessale anthmct@yahoo.com
  5. * Development of this app Sponsered/Funded by TAAN Softworks Corp
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*! \file
  18. *
  19. * \brief Fork CDR application
  20. *
  21. * \author Anthony Minessale anthmct@yahoo.com
  22. *
  23. * \note Development of this app Sponsored/Funded by TAAN Softworks Corp
  24. *
  25. * \ingroup applications
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/file.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/cdr.h"
  36. #include "asterisk/app.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/stasis.h"
  39. #include "asterisk/stasis_message_router.h"
  40. /*** DOCUMENTATION
  41. <application name="ForkCDR" language="en_US">
  42. <synopsis>
  43. Forks the current Call Data Record for this channel.
  44. </synopsis>
  45. <syntax>
  46. <parameter name="options">
  47. <optionlist>
  48. <option name="a">
  49. <para>If the channel is answered, set the answer time on
  50. the forked CDR to the current time. If this option is
  51. not used, the answer time on the forked CDR will be the
  52. answer time on the original CDR. If the channel is not
  53. answered, this option has no effect.</para>
  54. <para>Note that this option is implicitly assumed if the
  55. <literal>r</literal> option is used.</para>
  56. </option>
  57. <option name="e">
  58. <para>End (finalize) the original CDR.</para>
  59. </option>
  60. <option name="r">
  61. <para>Reset the start and answer times on the forked CDR.
  62. This will set the start and answer times (if the channel
  63. is answered) to be set to the current time.</para>
  64. <para>Note that this option implicitly assumes the
  65. <literal>a</literal> option.</para>
  66. </option>
  67. <option name="v">
  68. <para>Do not copy CDR variables and attributes from the
  69. original CDR to the forked CDR.</para>
  70. <warning><para>This option has changed. Previously, the
  71. variables were removed from the original CDR. This no
  72. longer occurs - this option now controls whether or not
  73. a forked CDR inherits the variables from the original
  74. CDR.</para></warning>
  75. </option>
  76. </optionlist>
  77. </parameter>
  78. </syntax>
  79. <description>
  80. <para>Causes the Call Data Record engine to fork a new CDR starting
  81. from the time the application is executed. The forked CDR will be
  82. linked to the end of the CDRs associated with the channel.</para>
  83. </description>
  84. <see-also>
  85. <ref type="function">CDR</ref>
  86. <ref type="application">NoCDR</ref>
  87. <ref type="application">ResetCDR</ref>
  88. </see-also>
  89. </application>
  90. ***/
  91. static char *app = "ForkCDR";
  92. AST_APP_OPTIONS(forkcdr_exec_options, {
  93. AST_APP_OPTION('a', AST_CDR_FLAG_SET_ANSWER),
  94. AST_APP_OPTION('e', AST_CDR_FLAG_FINALIZE),
  95. AST_APP_OPTION('r', AST_CDR_FLAG_RESET),
  96. AST_APP_OPTION('v', AST_CDR_FLAG_KEEP_VARS),
  97. });
  98. STASIS_MESSAGE_TYPE_DEFN_LOCAL(forkcdr_message_type);
  99. /*! \internal \brief Message payload for the Stasis message sent to fork the CDR */
  100. struct fork_cdr_message_payload {
  101. /*! The name of the channel whose CDR will be forked */
  102. const char *channel_name;
  103. /*! Option flags that control how the CDR will be forked */
  104. struct ast_flags *flags;
  105. };
  106. static void forkcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  107. {
  108. struct fork_cdr_message_payload *payload;
  109. if (stasis_message_type(message) != forkcdr_message_type()) {
  110. return;
  111. }
  112. payload = stasis_message_data(message);
  113. if (!payload) {
  114. return;
  115. }
  116. if (ast_cdr_fork(payload->channel_name, payload->flags)) {
  117. ast_log(AST_LOG_WARNING, "Failed to fork CDR for channel %s\n",
  118. payload->channel_name);
  119. }
  120. }
  121. static int forkcdr_exec(struct ast_channel *chan, const char *data)
  122. {
  123. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  124. RAII_VAR(struct fork_cdr_message_payload *, payload, NULL, ao2_cleanup);
  125. RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
  126. char *parse;
  127. struct ast_flags flags = { 0, };
  128. AST_DECLARE_APP_ARGS(args,
  129. AST_APP_ARG(options);
  130. );
  131. parse = ast_strdupa(data);
  132. AST_STANDARD_APP_ARGS(args, parse);
  133. if (!ast_strlen_zero(args.options)) {
  134. ast_app_parse_options(forkcdr_exec_options, &flags, NULL, args.options);
  135. }
  136. if (!forkcdr_message_type()) {
  137. ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message type\n",
  138. ast_channel_name(chan));
  139. return -1;
  140. }
  141. payload = ao2_alloc(sizeof(*payload), NULL);
  142. if (!payload) {
  143. return -1;
  144. }
  145. if (!router) {
  146. ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
  147. ast_channel_name(chan));
  148. return -1;
  149. }
  150. payload->channel_name = ast_channel_name(chan);
  151. payload->flags = &flags;
  152. message = stasis_message_create(forkcdr_message_type(), payload);
  153. if (!message) {
  154. ast_log(AST_LOG_WARNING, "Failed to fork CDR for channel %s: unable to create message\n",
  155. ast_channel_name(chan));
  156. return -1;
  157. }
  158. stasis_message_router_publish_sync(router, message);
  159. return 0;
  160. }
  161. static int unload_module(void)
  162. {
  163. RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
  164. if (router) {
  165. stasis_message_router_remove(router, forkcdr_message_type());
  166. }
  167. STASIS_MESSAGE_TYPE_CLEANUP(forkcdr_message_type);
  168. ast_unregister_application(app);
  169. return 0;
  170. }
  171. static int load_module(void)
  172. {
  173. RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup);
  174. int res = 0;
  175. if (!router) {
  176. return AST_MODULE_LOAD_FAILURE;
  177. }
  178. res |= STASIS_MESSAGE_TYPE_INIT(forkcdr_message_type);
  179. res |= ast_register_application_xml(app, forkcdr_exec);
  180. res |= stasis_message_router_add(router, forkcdr_message_type(),
  181. forkcdr_callback, NULL);
  182. if (res) {
  183. return AST_MODULE_LOAD_FAILURE;
  184. }
  185. return AST_MODULE_LOAD_SUCCESS;
  186. }
  187. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Fork The CDR into 2 separate entities");