| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "asterisk/file.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/pbx.h" |
| 38 |
#include "asterisk/cdr.h" |
| 39 |
#include "asterisk/app.h" |
| 40 |
#include "asterisk/module.h" |
| 41 |
#include "asterisk/stasis.h" |
| 42 |
#include "asterisk/stasis_message_router.h" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static char *app = "ForkCDR"; |
| 100 |
|
| 101 |
AST_APP_OPTIONS(forkcdr_exec_options, { |
| 102 |
AST_APP_OPTION('a', AST_CDR_FLAG_SET_ANSWER), |
| 103 |
AST_APP_OPTION('e', AST_CDR_FLAG_FINALIZE), |
| 104 |
AST_APP_OPTION('r', AST_CDR_FLAG_RESET), |
| 105 |
AST_APP_OPTION('v', AST_CDR_FLAG_KEEP_VARS), |
| 106 |
}); |
| 107 |
|
| 108 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(forkcdr_message_type); |
| 109 |
|
| 110 |
|
| 111 |
struct fork_cdr_message_payload { |
| 112 |
|
| 113 |
const char *channel_name; |
| 114 |
|
| 115 |
struct ast_flags *flags; |
| 116 |
}; |
| 117 |
|
| 118 |
static void forkcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 119 |
{ |
| 120 |
struct fork_cdr_message_payload *payload; |
| 121 |
|
| 122 |
if (stasis_message_type(message) != forkcdr_message_type()) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
payload = stasis_message_data(message); |
| 127 |
if (!payload) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if (ast_cdr_fork(payload->channel_name, payload->flags)) { |
| 132 |
ast_log(AST_LOG_WARNING, "Failed to fork CDR for channel %s\n", |
| 133 |
payload->channel_name); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static int forkcdr_exec(struct ast_channel *chan, const char *data) |
| 138 |
{ |
| 139 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 140 |
RAII_VAR(struct fork_cdr_message_payload *, payload, NULL, ao2_cleanup); |
| 141 |
RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup); |
| 142 |
|
| 143 |
char *parse; |
| 144 |
struct ast_flags flags = { 0, }; |
| 145 |
AST_DECLARE_APP_ARGS(args, |
| 146 |
AST_APP_ARG(options); |
| 147 |
); |
| 148 |
|
| 149 |
parse = ast_strdupa(data); |
| 150 |
|
| 151 |
AST_STANDARD_APP_ARGS(args, parse); |
| 152 |
|
| 153 |
if (!ast_strlen_zero(args.options)) { |
| 154 |
ast_app_parse_options(forkcdr_exec_options, &flags, NULL, args.options); |
| 155 |
} |
| 156 |
|
| 157 |
if (!forkcdr_message_type()) { |
| 158 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message type\n", |
| 159 |
ast_channel_name(chan)); |
| 160 |
return -1; |
| 161 |
} |
| 162 |
|
| 163 |
payload = ao2_alloc(sizeof(*payload), NULL); |
| 164 |
if (!payload) { |
| 165 |
return -1; |
| 166 |
} |
| 167 |
|
| 168 |
if (!router) { |
| 169 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n", |
| 170 |
ast_channel_name(chan)); |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
payload->channel_name = ast_channel_name(chan); |
| 175 |
payload->flags = &flags; |
| 176 |
message = stasis_message_create(forkcdr_message_type(), payload); |
| 177 |
if (!message) { |
| 178 |
ast_log(AST_LOG_WARNING, "Failed to fork CDR for channel %s: unable to create message\n", |
| 179 |
ast_channel_name(chan)); |
| 180 |
return -1; |
| 181 |
} |
| 182 |
stasis_message_router_publish_sync(router, message); |
| 183 |
|
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static int unload_module(void) |
| 188 |
{ |
| 189 |
RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup); |
| 190 |
|
| 191 |
if (router) { |
| 192 |
stasis_message_router_remove(router, forkcdr_message_type()); |
| 193 |
} |
| 194 |
STASIS_MESSAGE_TYPE_CLEANUP(forkcdr_message_type); |
| 195 |
ast_unregister_application(app); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
static int load_module(void) |
| 200 |
{ |
| 201 |
RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup); |
| 202 |
int res = 0; |
| 203 |
|
| 204 |
if (!router) { |
| 205 |
return AST_MODULE_LOAD_DECLINE; |
| 206 |
} |
| 207 |
|
| 208 |
res |= STASIS_MESSAGE_TYPE_INIT(forkcdr_message_type); |
| 209 |
res |= ast_register_application_xml(app, forkcdr_exec); |
| 210 |
res |= stasis_message_router_add(router, forkcdr_message_type(), |
| 211 |
forkcdr_callback, NULL); |
| 212 |
|
| 213 |
if (res) { |
| 214 |
unload_module(); |
| 215 |
|
| 216 |
return AST_MODULE_LOAD_DECLINE; |
| 217 |
} |
| 218 |
return AST_MODULE_LOAD_SUCCESS; |
| 219 |
} |
| 220 |
|
| 221 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Fork The CDR into 2 separate entities", |
| 222 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 223 |
.load = load_module, |
| 224 |
.unload = unload_module, |
| 225 |
.requires = "cdr", |
| 226 |
); |