diff --git a/include/asterisk/agi.h b/include/asterisk/agi.h index ac85db1818..584ddfe036 100644 --- a/include/asterisk/agi.h +++ b/include/asterisk/agi.h @@ -30,7 +30,7 @@ extern "C" { typedef struct agi_state { int fd; /* FD for general output */ int audio; /* FD for audio output */ - int ctrl; /* FD for input control */ + int ctrl; /* FD for input control */ } AGI; typedef struct agi_command { @@ -45,12 +45,17 @@ typedef struct agi_command { char *usage; /* Does this application run dead */ int dead; + /* Pointer to module that registered the agi command */ + struct ast_module *mod; /* Linked list pointer */ AST_LIST_ENTRY(agi_command) list; } agi_command; -int ast_agi_register(agi_command *cmd); -int ast_agi_unregister(agi_command *cmd); +void ast_agi_fdprintf(int fd, char *fmt, ...); +int ast_agi_register(struct ast_module *mod, agi_command *cmd); +int ast_agi_unregister(struct ast_module *mod, agi_command *cmd); +void ast_agi_register_multiple(struct ast_module *mod, agi_command *cmd, int len); +void ast_agi_unregister_multiple(struct ast_module *mod, agi_command *cmd, int len); #if defined(__cplusplus) || defined(c_plusplus) } diff --git a/res/res_agi.c b/res/res_agi.c index f37c09d2d6..003cefac50 100644 --- a/res/res_agi.c +++ b/res/res_agi.c @@ -67,10 +67,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/agi.h" #define MAX_ARGS 128 -#define MAX_COMMANDS 128 - -/* Recycle some stuff from the CLI interface */ -#define fdprintf agi_debug_cli static char *app = "AGI"; @@ -119,7 +115,7 @@ enum agi_result { static agi_command *find_command(char *cmds[], int exact); -static void agi_debug_cli(int fd, char *fmt, ...) +void ast_agi_fdprintf(int fd, char *fmt, ...) { char *stuff; int res = 0; @@ -221,7 +217,7 @@ static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds, in /* If we have a script parameter, relay it to the fastagi server */ /* Script parameters take the form of: AGI(agi://my.example.com/?extension=${EXTEN}) */ if (!ast_strlen_zero(script)) - fdprintf(s, "agi_network_script: %s\n", script); + ast_agi_fdprintf(s, "agi_network_script: %s\n", script); ast_debug(4, "Wow, connected!\n"); fds[0] = s; @@ -368,38 +364,38 @@ static void setup_env(struct ast_channel *chan, char *request, int fd, int enhan /* Print initial environment, with agi_request always being the first thing */ - fdprintf(fd, "agi_request: %s\n", request); - fdprintf(fd, "agi_channel: %s\n", chan->name); - fdprintf(fd, "agi_language: %s\n", chan->language); - fdprintf(fd, "agi_type: %s\n", chan->tech->type); - fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid); + ast_agi_fdprintf(fd, "agi_request: %s\n", request); + ast_agi_fdprintf(fd, "agi_channel: %s\n", chan->name); + ast_agi_fdprintf(fd, "agi_language: %s\n", chan->language); + ast_agi_fdprintf(fd, "agi_type: %s\n", chan->tech->type); + ast_agi_fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid); /* ANI/DNIS */ - fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown")); - fdprintf(fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown")); - fdprintf(fd, "agi_callingpres: %d\n", chan->cid.cid_pres); - fdprintf(fd, "agi_callingani2: %d\n", chan->cid.cid_ani2); - fdprintf(fd, "agi_callington: %d\n", chan->cid.cid_ton); - fdprintf(fd, "agi_callingtns: %d\n", chan->cid.cid_tns); - fdprintf(fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown")); - fdprintf(fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown")); + ast_agi_fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown")); + ast_agi_fdprintf(fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown")); + ast_agi_fdprintf(fd, "agi_callingpres: %d\n", chan->cid.cid_pres); + ast_agi_fdprintf(fd, "agi_callingani2: %d\n", chan->cid.cid_ani2); + ast_agi_fdprintf(fd, "agi_callington: %d\n", chan->cid.cid_ton); + ast_agi_fdprintf(fd, "agi_callingtns: %d\n", chan->cid.cid_tns); + ast_agi_fdprintf(fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown")); + ast_agi_fdprintf(fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown")); /* Context information */ - fdprintf(fd, "agi_context: %s\n", chan->context); - fdprintf(fd, "agi_extension: %s\n", chan->exten); - fdprintf(fd, "agi_priority: %d\n", chan->priority); - fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0"); + ast_agi_fdprintf(fd, "agi_context: %s\n", chan->context); + ast_agi_fdprintf(fd, "agi_extension: %s\n", chan->exten); + ast_agi_fdprintf(fd, "agi_priority: %d\n", chan->priority); + ast_agi_fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0"); /* User information */ - fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : ""); + ast_agi_fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : ""); /* Send any parameters to the fastagi server that have been passed via the agi application */ /* Agi application paramaters take the form of: AGI(/path/to/example/script|${EXTEN}) */ for(count = 1; count < argc; count++) - fdprintf(fd, "agi_arg_%d: %s\n", count, argv[count]); + ast_agi_fdprintf(fd, "agi_arg_%d: %s\n", count, argv[count]); /* End with empty return */ - fdprintf(fd, "\n"); + ast_agi_fdprintf(fd, "\n"); } static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *argv[]) @@ -410,7 +406,7 @@ static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *arg if (chan->_state != AST_STATE_UP) res = ast_answer(chan); - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -423,7 +419,7 @@ static int handle_waitfordigit(struct ast_channel *chan, AGI *agi, int argc, cha if (sscanf(argv[3], "%d", &to) != 1) return RESULT_SHOWUSAGE; res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl); - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -442,7 +438,7 @@ static int handle_sendtext(struct ast_channel *chan, AGI *agi, int argc, char *a parsing, then here, add a newline at the end of the string before sending it to ast_sendtext --DUDE */ res = ast_sendtext(chan, argv[2]); - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -455,15 +451,15 @@ static int handle_recvchar(struct ast_channel *chan, AGI *agi, int argc, char *a res = ast_recvchar(chan,atoi(argv[2])); if (res == 0) { - fdprintf(agi->fd, "200 result=%d (timeout)\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d (timeout)\n", res); return RESULT_SUCCESS; } if (res > 0) { - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return RESULT_SUCCESS; } else { - fdprintf(agi->fd, "200 result=%d (hangup)\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d (hangup)\n", res); return RESULT_FAILURE; } } @@ -477,10 +473,10 @@ static int handle_recvtext(struct ast_channel *chan, AGI *agi, int argc, char *a buf = ast_recvtext(chan,atoi(argv[2])); if (buf) { - fdprintf(agi->fd, "200 result=1 (%s)\n", buf); + ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", buf); ast_free(buf); } else { - fdprintf(agi->fd, "200 result=-1\n"); + ast_agi_fdprintf(agi->fd, "200 result=-1\n"); } return RESULT_SUCCESS; } @@ -502,9 +498,9 @@ static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, char *ar x = 1; res = ast_channel_setoption(chan, AST_OPTION_TDD, &x, sizeof(char), 0); if (res != RESULT_SUCCESS) - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); else - fdprintf(agi->fd, "200 result=1\n"); + ast_agi_fdprintf(agi->fd, "200 result=1\n"); return RESULT_SUCCESS; } @@ -518,7 +514,7 @@ static int handle_sendimage(struct ast_channel *chan, AGI *agi, int argc, char * res = ast_send_image(chan, argv[2]); if (!ast_check_hangup(chan)) res = 0; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -555,7 +551,7 @@ static int handle_controlstreamfile(struct ast_channel *chan, AGI *agi, int argc res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, pause, NULL, skipms, NULL); - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -577,7 +573,7 @@ static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, char return RESULT_SHOWUSAGE; if (!(fs = ast_openstream(chan, argv[2], chan->language))) { - fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset); return RESULT_SUCCESS; } vfs = ast_openvstream(chan, argv[2], chan->language); @@ -604,7 +600,7 @@ static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, char /* Stop this command, don't print a result line, as there is a new command */ return RESULT_SUCCESS; } - fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -631,7 +627,7 @@ static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, char * } if (!(fs = ast_openstream(chan, argv[2], chan->language))) { - fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset); ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]); return RESULT_SUCCESS; } @@ -668,7 +664,7 @@ static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, char * res=0; } - fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -688,7 +684,7 @@ static int handle_saynumber(struct ast_channel *chan, AGI *agi, int argc, char * res = ast_say_number_full(chan, num, argv[3], chan->language, argc > 4 ? argv[4] : NULL, agi->audio, agi->ctrl); if (res == 1) return RESULT_SUCCESS; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -704,7 +700,7 @@ static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, char * res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl); if (res == 1) /* New command */ return RESULT_SUCCESS; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -718,7 +714,7 @@ static int handle_sayalpha(struct ast_channel *chan, AGI *agi, int argc, char *a res = ast_say_character_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl); if (res == 1) /* New command */ return RESULT_SUCCESS; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -733,7 +729,7 @@ static int handle_saydate(struct ast_channel *chan, AGI *agi, int argc, char *ar res = ast_say_date(chan, num, argv[3], chan->language); if (res == 1) return RESULT_SUCCESS; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -748,7 +744,7 @@ static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, char *ar res = ast_say_time(chan, num, argv[3], chan->language); if (res == 1) return RESULT_SUCCESS; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -782,7 +778,7 @@ static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char if (res == 1) return RESULT_SUCCESS; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -796,7 +792,7 @@ static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, char res = ast_say_phonetic_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl); if (res == 1) /* New command */ return RESULT_SUCCESS; - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } @@ -819,11 +815,11 @@ static int handle_getdata(struct ast_channel *chan, AGI *agi, int argc, char *ar if (res == 2) /* New command */ return RESULT_SUCCESS; else if (res == 1) - fdprintf(agi->fd, "200 result=%s (timeout)\n", data); + ast_agi_fdprintf(agi->fd, "200 result=%s (timeout)\n", data); else if (res < 0 ) - fdprintf(agi->fd, "200 result=-1\n"); + ast_agi_fdprintf(agi->fd, "200 result=-1\n"); else - fdprintf(agi->fd, "200 result=%s\n", data); + ast_agi_fdprintf(agi->fd, "200 result=%s\n", data); return RESULT_SUCCESS; } @@ -833,7 +829,7 @@ static int handle_setcontext(struct ast_channel *chan, AGI *agi, int argc, char if (argc != 3) return RESULT_SHOWUSAGE; ast_copy_string(chan->context, argv[2], sizeof(chan->context)); - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); return RESULT_SUCCESS; } @@ -842,7 +838,7 @@ static int handle_setextension(struct ast_channel *chan, AGI *agi, int argc, cha if (argc != 3) return RESULT_SHOWUSAGE; ast_copy_string(chan->exten, argv[2], sizeof(chan->exten)); - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); return RESULT_SUCCESS; } @@ -859,7 +855,7 @@ static int handle_setpriority(struct ast_channel *chan, AGI *agi, int argc, char } ast_explicit_goto(chan, NULL, NULL, pri); - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); return RESULT_SUCCESS; } @@ -935,12 +931,12 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char if (!res) res = ast_waitstream(chan, argv[4]); if (res) { - fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset); } else { fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, AST_FILE_MODE); if (!fs) { res = -1; - fdprintf(agi->fd, "200 result=%d (writefile)\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d (writefile)\n", res); if (sildet) ast_dsp_free(sildet); return RESULT_FAILURE; @@ -960,14 +956,14 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char res = ast_waitfor(chan, -1); if (res < 0) { ast_closestream(fs); - fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset); if (sildet) ast_dsp_free(sildet); return RESULT_FAILURE; } f = ast_read(chan); if (!f) { - fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset); ast_closestream(fs); if (sildet) ast_dsp_free(sildet); @@ -982,7 +978,7 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char ast_stream_rewind(fs, 200); ast_truncstream(fs); sample_offset = ast_tellstream(fs); - fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset); ast_closestream(fs); ast_frfree(f); if (sildet) @@ -1022,12 +1018,12 @@ static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, char break; } - if (gotsilence) { - ast_stream_rewind(fs, silence-1000); - ast_truncstream(fs); - sample_offset = ast_tellstream(fs); + if (gotsilence) { + ast_stream_rewind(fs, silence-1000); + ast_truncstream(fs); + sample_offset = ast_tellstream(fs); } - fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset); + ast_agi_fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset); ast_closestream(fs); } @@ -1054,7 +1050,7 @@ static int handle_autohangup(struct ast_channel *chan, AGI *agi, int argc, char chan->whentohangup = time(NULL) + timeout; else chan->whentohangup = 0; - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); return RESULT_SUCCESS; } @@ -1065,7 +1061,7 @@ static int handle_hangup(struct ast_channel *chan, AGI *agi, int argc, char **ar if (argc == 1) { /* no argument: hangup the current channel */ ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT); - fdprintf(agi->fd, "200 result=1\n"); + ast_agi_fdprintf(agi->fd, "200 result=1\n"); return RESULT_SUCCESS; } else if (argc == 2) { /* one argument: look for info on the specified channel */ @@ -1073,12 +1069,12 @@ static int handle_hangup(struct ast_channel *chan, AGI *agi, int argc, char **ar if (c) { /* we have a matching channel */ ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT); - fdprintf(agi->fd, "200 result=1\n"); + ast_agi_fdprintf(agi->fd, "200 result=1\n"); ast_channel_unlock(c); return RESULT_SUCCESS; } /* if we get this far no channel name matched the argument given */ - fdprintf(agi->fd, "200 result=-1\n"); + ast_agi_fdprintf(agi->fd, "200 result=-1\n"); return RESULT_SUCCESS; } else { return RESULT_SHOWUSAGE; @@ -1101,7 +1097,7 @@ static int handle_exec(struct ast_channel *chan, AGI *agi, int argc, char **argv ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]); res = -2; } - fdprintf(agi->fd, "200 result=%d\n", res); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", res); /* Even though this is wrong, users are depending upon this result. */ return res; @@ -1124,7 +1120,7 @@ static int handle_setcallerid(struct ast_channel *chan, AGI *agi, int argc, char ast_set_callerid(chan, l, n, NULL); } - fdprintf(agi->fd, "200 result=1\n"); + ast_agi_fdprintf(agi->fd, "200 result=1\n"); return RESULT_SUCCESS; } @@ -1133,18 +1129,18 @@ static int handle_channelstatus(struct ast_channel *chan, AGI *agi, int argc, ch struct ast_channel *c; if (argc == 2) { /* no argument: supply info on the current channel */ - fdprintf(agi->fd, "200 result=%d\n", chan->_state); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", chan->_state); return RESULT_SUCCESS; } else if (argc == 3) { /* one argument: look for info on the specified channel */ c = ast_get_channel_by_name_locked(argv[2]); if (c) { - fdprintf(agi->fd, "200 result=%d\n", c->_state); + ast_agi_fdprintf(agi->fd, "200 result=%d\n", c->_state); ast_channel_unlock(c); return RESULT_SUCCESS; } /* if we get this far no channel name matched the argument given */ - fdprintf(agi->fd, "200 result=-1\n"); + ast_agi_fdprintf(agi->fd, "200 result=-1\n"); return RESULT_SUCCESS; } else { return RESULT_SHOWUSAGE; @@ -1156,7 +1152,7 @@ static int handle_setvariable(struct ast_channel *chan, AGI *agi, int argc, char if (argv[3]) pbx_builtin_setvar_helper(chan, argv[2], argv[3]); - fdprintf(agi->fd, "200 result=1\n"); + ast_agi_fdprintf(agi->fd, "200 result=1\n"); return RESULT_SUCCESS; } @@ -1176,9 +1172,9 @@ static int handle_getvariable(struct ast_channel *chan, AGI *agi, int argc, char } if (ret) - fdprintf(agi->fd, "200 result=1 (%s)\n", ret); + ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", ret); else - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); return RESULT_SUCCESS; } @@ -1197,9 +1193,9 @@ static int handle_getvariablefull(struct ast_channel *chan, AGI *agi, int argc, } if (chan) { /* XXX isn't this chan2 ? */ pbx_substitute_variables_helper(chan2, argv[3], tmp, sizeof(tmp) - 1); - fdprintf(agi->fd, "200 result=1 (%s)\n", tmp); + ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", tmp); } else { - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); } if (chan2 && (chan2 != chan)) ast_channel_unlock(chan2); @@ -1236,7 +1232,7 @@ static int handle_verbose(struct ast_channel *chan, AGI *agi, int argc, char **a if (level <= option_verbose) ast_verbose("%s %s: %s\n", prefix, chan->data, argv[1]); - fdprintf(agi->fd, "200 result=1\n"); + ast_agi_fdprintf(agi->fd, "200 result=1\n"); return RESULT_SUCCESS; } @@ -1250,9 +1246,9 @@ static int handle_dbget(struct ast_channel *chan, AGI *agi, int argc, char **arg return RESULT_SHOWUSAGE; res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp)); if (res) - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); else - fdprintf(agi->fd, "200 result=1 (%s)\n", tmp); + ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", tmp); return RESULT_SUCCESS; } @@ -1264,7 +1260,7 @@ static int handle_dbput(struct ast_channel *chan, AGI *agi, int argc, char **arg if (argc != 5) return RESULT_SHOWUSAGE; res = ast_db_put(argv[2], argv[3], argv[4]); - fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1'); + ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1'); return RESULT_SUCCESS; } @@ -1275,7 +1271,7 @@ static int handle_dbdel(struct ast_channel *chan, AGI *agi, int argc, char **arg if (argc != 4) return RESULT_SHOWUSAGE; res = ast_db_del(argv[2], argv[3]); - fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1'); + ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1'); return RESULT_SUCCESS; } @@ -1290,7 +1286,7 @@ static int handle_dbdeltree(struct ast_channel *chan, AGI *agi, int argc, char * else res = ast_db_deltree(argv[2], NULL); - fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1'); + ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1'); return RESULT_SUCCESS; } @@ -1322,7 +1318,7 @@ static int agi_no_debug(int fd, int argc, char *argv[]) static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, char *argv[]) { - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); return RESULT_SUCCESS; } @@ -1332,7 +1328,7 @@ static int handle_setmusic(struct ast_channel *chan, AGI *agi, int argc, char *a ast_moh_start(chan, argc > 3 ? argv[3] : NULL, NULL); else if (!strncasecmp(argv[2], "off", 3)) ast_moh_stop(chan); - fdprintf(agi->fd, "200 result=0\n"); + ast_agi_fdprintf(agi->fd, "200 result=0\n"); return RESULT_SUCCESS; } @@ -1586,7 +1582,7 @@ static char usage_noop[] = /*! * \brief AGI commands list */ -static struct agi_command commands[MAX_COMMANDS] = { +static struct agi_command commands[] = { { { "answer", NULL }, handle_answer, "Answer channel", usage_answer , 0 }, { { "channel", "status", NULL }, handle_channelstatus, "Returns status of the connected channel", usage_channelstatus , 0 }, { { "database", "del", NULL }, handle_dbdel, "Removes database key/value", usage_dbdel , 1 }, @@ -1653,12 +1649,19 @@ static int help_workhorse(int fd, char *match[]) return 0; } -int ast_agi_register(agi_command *agi) +int ast_agi_register(struct ast_module *mod, agi_command *cmd) { - if (!find_command(agi->cmda,1)) { + char fullcmd[80]; + + ast_join(fullcmd, sizeof(fullcmd), cmd->cmda); + + if (!find_command(cmd->cmda,1)) { AST_RWLIST_WRLOCK(&agi_commands); - AST_LIST_INSERT_TAIL(&agi_commands, agi, list); + AST_LIST_INSERT_TAIL(&agi_commands, cmd, list); AST_RWLIST_UNLOCK(&agi_commands); + if (mod != ast_module_info->self) + ast_module_ref(ast_module_info->self); + ast_verb(2, "AGI Command '%s' registered\n",fullcmd); return 1; } else { ast_log(LOG_WARNING, "Command already registered!\n"); @@ -1666,26 +1669,50 @@ int ast_agi_register(agi_command *agi) } } -int ast_agi_unregister(agi_command *cmd) +int ast_agi_unregister(struct ast_module *mod, agi_command *cmd) { struct agi_command *e; int unregistered = 0; + char fullcmd[80]; + + ast_join(fullcmd, sizeof(fullcmd), cmd->cmda); AST_RWLIST_WRLOCK(&agi_commands); AST_RWLIST_TRAVERSE_SAFE_BEGIN(&agi_commands, e, list) { if (cmd == e) { AST_RWLIST_REMOVE_CURRENT(&agi_commands, list); + if (mod != ast_module_info->self) + ast_module_unref(ast_module_info->self); unregistered=1; break; } } AST_RWLIST_TRAVERSE_SAFE_END AST_RWLIST_UNLOCK(&agi_commands); - if (!unregistered) - ast_log(LOG_WARNING, "Unable to unregister command!\n"); + if (unregistered) + ast_verb(2, "AGI Command '%s' unregistered\n",fullcmd); + else + ast_log(LOG_WARNING, "Unable to unregister command: '%s'!\n",fullcmd); return unregistered; } +void ast_agi_register_multiple(struct ast_module *mod, agi_command *cmd, int len) +{ + int i; + + for (i = 0; i < len; i++) + ast_agi_register(mod, cmd + i); + +} + +void ast_agi_unregister_multiple(struct ast_module *mod, agi_command *cmd, int len) +{ + int i; + + for (i = 0; i < len; i++) + ast_agi_unregister(mod, cmd + i); +} + static agi_command *find_command(char *cmds[], int exact) { int y, match; @@ -1791,12 +1818,18 @@ static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int parse_args(buf, &argc, argv); if ((c = find_command(argv, 0)) && (!dead || (dead && c->dead))) { + /* if this command wasnt registered by res_agi, be sure to usecount + the module we are using */ + if (c->mod != ast_module_info->self) + ast_module_ref(c->mod); res = c->handler(chan, agi, argc, argv); + if (c->mod != ast_module_info->self) + ast_module_unref(c->mod); switch(res) { case RESULT_SHOWUSAGE: - fdprintf(agi->fd, "520-Invalid command syntax. Proper usage follows:\n"); - fdprintf(agi->fd, c->usage); - fdprintf(agi->fd, "520 End of proper usage.\n"); + ast_agi_fdprintf(agi->fd, "520-Invalid command syntax. Proper usage follows:\n"); + ast_agi_fdprintf(agi->fd, c->usage); + ast_agi_fdprintf(agi->fd, "520 End of proper usage.\n"); break; case AST_PBX_KEEPALIVE: /* We've been asked to keep alive, so do so */ @@ -1808,9 +1841,9 @@ static int agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int return -1; } } else if ((c = find_command(argv, 0))) { - fdprintf(agi->fd, "511 Command Not Permitted on a dead channel\n"); + ast_agi_fdprintf(agi->fd, "511 Command Not Permitted on a dead channel\n"); } else { - fdprintf(agi->fd, "510 Invalid or unknown command\n"); + ast_agi_fdprintf(agi->fd, "510 Invalid or unknown command\n"); } return 0; } @@ -2156,15 +2189,9 @@ static struct ast_cli_entry cli_agi[] = { static int unload_module(void) { - struct agi_command *e; ast_cli_unregister_multiple(cli_agi, sizeof(cli_agi) / sizeof(struct ast_cli_entry)); - AST_RWLIST_WRLOCK(&agi_commands); - AST_RWLIST_TRAVERSE_SAFE_BEGIN(&agi_commands, e, list) { - AST_RWLIST_REMOVE_CURRENT(&agi_commands, list); - } - AST_RWLIST_TRAVERSE_SAFE_END - AST_RWLIST_UNLOCK(&agi_commands); + ast_agi_unregister_multiple(ast_module_info->self, commands, sizeof(commands) / sizeof(struct agi_command)); ast_unregister_application(eapp); ast_unregister_application(deadapp); return ast_unregister_application(app); @@ -2172,12 +2199,8 @@ static int unload_module(void) static int load_module(void) { - int i; - ast_cli_register_multiple(cli_agi, sizeof(cli_agi) / sizeof(struct ast_cli_entry)); - for (i=0; i < (sizeof(commands) / sizeof(struct agi_command)); i++) { - ast_agi_register(&commands[i]); - } + ast_agi_register_multiple(ast_module_info->self, commands, sizeof(commands) / sizeof(struct agi_command)); ast_register_application(deadapp, deadagi_exec, deadsynopsis, descrip); ast_register_application(eapp, eagi_exec, esynopsis, descrip); return ast_register_application(app, agi_exec, synopsis, descrip);