2017-01-06 02:10:15 -05:00
|
|
|
/*
|
2008-04-25 22:12:01 +00:00
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2014-02-05 15:02:28 -06:00
|
|
|
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
|
2008-04-25 22:12:01 +00:00
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2008-04-25 22:12:01 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2017-01-06 02:10:15 -05:00
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2008-04-25 22:12:01 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* mod_perl.c -- Perl
|
|
|
|
*
|
|
|
|
*/
|
2008-04-26 00:45:43 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
#ifdef __ICC
|
|
|
|
#pragma warning (disable:1419)
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <perlibs.h>
|
|
|
|
#pragma comment(lib, PERL_LIB)
|
|
|
|
#endif
|
|
|
|
|
2009-12-02 07:56:18 +00:00
|
|
|
#if defined (__SVR4) && defined (__sun)
|
|
|
|
#include <uconfig.h>
|
|
|
|
#endif
|
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
#include <EXTERN.h>
|
|
|
|
#include <perl.h>
|
|
|
|
#include <switch.h>
|
2017-03-22 12:23:36 -07:00
|
|
|
static char *embedding[] = { "", "-e", "0" };
|
2008-04-25 22:12:01 +00:00
|
|
|
EXTERN_C void xs_init(pTHX);
|
|
|
|
|
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_perl_load);
|
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_perl_shutdown);
|
2008-11-12 11:29:33 +00:00
|
|
|
SWITCH_MODULE_DEFINITION_EX(mod_perl, mod_perl_load, mod_perl_shutdown, NULL, SMODF_GLOBAL_SYMBOLS);
|
2008-04-25 22:12:01 +00:00
|
|
|
|
|
|
|
static STRLEN n_a;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
PerlInterpreter *my_perl;
|
|
|
|
switch_memory_pool_t *pool;
|
|
|
|
char *xml_handler;
|
|
|
|
} globals;
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static int Perl_safe_eval(PerlInterpreter * my_perl, const char *string)
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
|
|
|
char *err = NULL;
|
2008-04-26 15:57:05 +00:00
|
|
|
|
|
|
|
Perl_eval_pv(my_perl, string, FALSE);
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if ((err = SvPV(get_sv("@", TRUE), n_a)) && !zstr(err)) {
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[%s]\n%s\n", string, err);
|
2008-04-26 15:57:05 +00:00
|
|
|
return -1;
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
2008-04-26 15:57:05 +00:00
|
|
|
return 0;
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static int perl_parse_and_execute(PerlInterpreter * my_perl, char *input_code, char *setup_code)
|
2008-05-01 22:56:14 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(input_code)) {
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No code to execute!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setup_code) {
|
|
|
|
error = Perl_safe_eval(my_perl, setup_code);
|
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*input_code == '~') {
|
|
|
|
char *buff = input_code + 1;
|
|
|
|
error = Perl_safe_eval(my_perl, buff);
|
|
|
|
} else {
|
|
|
|
char *args = strchr(input_code, ' ');
|
|
|
|
if (args) {
|
|
|
|
char *code = NULL;
|
|
|
|
int x, argc;
|
|
|
|
char *argv[128] = { 0 };
|
|
|
|
*args++ = '\0';
|
|
|
|
|
|
|
|
if ((argc = switch_separate_string(args, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
|
|
|
|
switch_stream_handle_t stream = { 0 };
|
|
|
|
SWITCH_STANDARD_STREAM(stream);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
stream.write_function(&stream, " @ARGV = ( ");
|
|
|
|
for (x = 0; x < argc; x++) {
|
2008-05-27 04:54:52 +00:00
|
|
|
stream.write_function(&stream, "'%s'%s", argv[x], x == argc - 1 ? "" : ", ");
|
2008-05-01 22:56:14 +00:00
|
|
|
}
|
|
|
|
stream.write_function(&stream, " );");
|
|
|
|
code = stream.data;
|
|
|
|
} else {
|
|
|
|
code = switch_mprintf("ARGV = ();");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
error = Perl_safe_eval(my_perl, code);
|
|
|
|
switch_safe_free(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!error) {
|
|
|
|
char *file = input_code;
|
2008-05-02 16:43:54 +00:00
|
|
|
char *err;
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
if (!switch_is_file_path(file)) {
|
2008-05-02 16:43:54 +00:00
|
|
|
file = switch_mprintf("require '%s/%s';", SWITCH_GLOBAL_dirs.script_dir, file);
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_assert(file);
|
|
|
|
} else {
|
2008-05-02 16:43:54 +00:00
|
|
|
file = switch_mprintf("require '%s';", file);
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_assert(file);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
error = Perl_safe_eval(my_perl, file);
|
|
|
|
switch_safe_free(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-02-21 22:50:35 +00:00
|
|
|
#define HACK_CLEAN_CODE "eval{foreach my $kl(keys %main::) {eval{undef($$kl);} if (defined($$kl) && ($kl =~ /^\\w+[\\w\\d_]+$/))}}"
|
2008-07-14 20:37:36 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
static void destroy_perl(PerlInterpreter ** to_destroy)
|
|
|
|
{
|
2008-07-14 20:37:36 +00:00
|
|
|
Perl_safe_eval(*to_destroy, HACK_CLEAN_CODE);
|
2008-04-25 22:12:01 +00:00
|
|
|
perl_destruct(*to_destroy);
|
|
|
|
perl_free(*to_destroy);
|
|
|
|
*to_destroy = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PerlInterpreter *clone_perl(void)
|
|
|
|
{
|
|
|
|
PerlInterpreter *my_perl = perl_clone(globals.my_perl, CLONEf_COPY_STACKS | CLONEf_KEEP_PTR_TABLE);
|
|
|
|
PERL_SET_CONTEXT(my_perl);
|
|
|
|
return my_perl;
|
|
|
|
}
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
#if 0
|
2008-05-27 04:54:52 +00:00
|
|
|
static perl_parse_and_execute(PerlInterpreter * my_perl, char *input_code, char *setup_code)
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
if (*input_code == '~') {
|
|
|
|
char *buff = input_code + 1;
|
|
|
|
perl_parse(my_perl, xs_init, 3, embedding, NULL);
|
2008-05-27 04:54:52 +00:00
|
|
|
if (setup_code)
|
|
|
|
Perl_safe_eval(my_perl, setup_code);
|
2008-05-01 22:56:14 +00:00
|
|
|
Perl_safe_eval(my_perl, buff);
|
2008-04-25 22:12:01 +00:00
|
|
|
} else {
|
|
|
|
int argc = 0;
|
|
|
|
char *argv[128] = { 0 };
|
2008-04-26 15:57:05 +00:00
|
|
|
char *err;
|
2008-04-25 22:12:01 +00:00
|
|
|
argv[0] = "FreeSWITCH";
|
|
|
|
argc++;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
argc += switch_separate_string(input_code, ' ', &argv[1], (sizeof(argv) / sizeof(argv[0])) - 1);
|
2008-05-27 04:54:52 +00:00
|
|
|
if (!perl_parse(my_perl, xs_init, argc, argv, (char **) NULL)) {
|
2008-04-26 15:57:05 +00:00
|
|
|
if (setup_code) {
|
2008-05-01 22:56:14 +00:00
|
|
|
if (!Perl_safe_eval(my_perl, setup_code)) {
|
2008-04-26 15:57:05 +00:00
|
|
|
perl_run(my_perl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if ((err = SvPV(get_sv("@", TRUE), n_a)) && !zstr(err)) {
|
2008-04-26 15:57:05 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s\n", err);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-26 15:57:05 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
#endif
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2008-07-14 20:37:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
static void perl_function(switch_core_session_t *session, char *data)
|
|
|
|
{
|
|
|
|
char *uuid = switch_core_session_get_uuid(session);
|
|
|
|
PerlInterpreter *my_perl = clone_perl();
|
2008-07-14 20:37:36 +00:00
|
|
|
char code[1024] = "";
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
perl_parse(my_perl, xs_init, 3, embedding, NULL);
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_snprintf(code, sizeof(code),
|
|
|
|
"use lib '%s/perl';\n"
|
|
|
|
"use freeswitch;\n"
|
|
|
|
"$SWITCH_ENV{UUID} = \"%s\";\n" "$session = new freeswitch::Session(\"%s\")", SWITCH_GLOBAL_dirs.base_dir, uuid, uuid);
|
2008-04-25 22:12:01 +00:00
|
|
|
|
|
|
|
perl_parse_and_execute(my_perl, data, code);
|
|
|
|
destroy_perl(&my_perl);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_perl_shutdown)
|
|
|
|
{
|
|
|
|
if (globals.my_perl) {
|
|
|
|
perl_free(globals.my_perl);
|
|
|
|
globals.my_perl = NULL;
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Unallocated perl interpreter.\n");
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
struct perl_o {
|
|
|
|
switch_stream_handle_t *stream;
|
|
|
|
switch_core_session_t *session;
|
|
|
|
char *cmd;
|
2011-10-19 08:25:06 -05:00
|
|
|
switch_event_t *message;
|
2008-05-01 22:56:14 +00:00
|
|
|
int d;
|
|
|
|
};
|
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
static void *SWITCH_THREAD_FUNC perl_thread_run(switch_thread_t *thread, void *obj)
|
|
|
|
{
|
|
|
|
PerlInterpreter *my_perl = clone_perl();
|
|
|
|
char code[1024];
|
2008-05-01 22:56:14 +00:00
|
|
|
SV *sv = NULL;
|
|
|
|
char *uuid = NULL;
|
|
|
|
struct perl_o *po = (struct perl_o *) obj;
|
|
|
|
char *cmd = po->cmd;
|
|
|
|
switch_stream_handle_t *stream = po->stream;
|
|
|
|
switch_core_session_t *session = po->session;
|
2011-10-19 08:25:06 -05:00
|
|
|
switch_event_t *message = po->message;
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
if (session) {
|
|
|
|
uuid = switch_core_session_get_uuid(session);
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_snprintf(code, sizeof(code),
|
|
|
|
"use lib '%s/perl';\n" "use freeswitch;\n" "$SWITCH_ENV{UUID} = \"%s\";\n", SWITCH_GLOBAL_dirs.base_dir, switch_str_nil(uuid)
|
|
|
|
);
|
2008-05-01 22:56:14 +00:00
|
|
|
|
|
|
|
perl_parse(my_perl, xs_init, 3, embedding, NULL);
|
|
|
|
Perl_safe_eval(my_perl, code);
|
|
|
|
|
|
|
|
if (uuid) {
|
|
|
|
switch_snprintf(code, sizeof(code), "$session = new freeswitch::Session(\"%s\")", uuid);
|
|
|
|
Perl_safe_eval(my_perl, code);
|
|
|
|
}
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
if (cmd) {
|
|
|
|
if (stream) {
|
|
|
|
mod_perl_conjure_stream(my_perl, stream, "stream");
|
2008-05-24 03:46:19 +00:00
|
|
|
if (stream->param_event) {
|
|
|
|
mod_perl_conjure_event(my_perl, stream->param_event, "env");
|
2008-05-01 22:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-19 08:25:06 -05:00
|
|
|
|
|
|
|
if (message) {
|
|
|
|
mod_perl_conjure_event(my_perl, message, "message");
|
|
|
|
}
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
//Perl_safe_eval(my_perl, cmd);
|
|
|
|
perl_parse_and_execute(my_perl, cmd, NULL);
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
destroy_perl(&my_perl);
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_safe_free(cmd);
|
|
|
|
|
|
|
|
if (po->d) {
|
|
|
|
free(po);
|
|
|
|
}
|
2010-02-06 03:38:24 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int perl_thread(const char *text)
|
|
|
|
{
|
|
|
|
switch_thread_t *thread;
|
|
|
|
switch_threadattr_t *thd_attr = NULL;
|
2008-05-01 22:56:14 +00:00
|
|
|
struct perl_o *po;
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
po = malloc(sizeof(*po));
|
|
|
|
memset(po, 0, sizeof(*po));
|
|
|
|
po->cmd = strdup(text);
|
|
|
|
po->d = 1;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
switch_threadattr_create(&thd_attr, globals.pool);
|
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
|
|
|
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
|
2008-05-01 22:56:14 +00:00
|
|
|
switch_thread_create(&thread, thd_attr, perl_thread_run, po, globals.pool);
|
2008-04-25 22:12:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
SWITCH_STANDARD_API(perlrun_api_function)
|
|
|
|
{
|
2008-05-07 14:52:34 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(cmd)) {
|
2008-05-07 14:52:34 +00:00
|
|
|
stream->write_function(stream, "-ERR Missing args.\n");
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
perl_thread(cmd);
|
|
|
|
stream->write_function(stream, "+OK\n");
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
SWITCH_STANDARD_API(perl_api_function)
|
|
|
|
{
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
struct perl_o po = { 0 };
|
2008-05-07 14:52:34 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(cmd)) {
|
2008-05-07 14:52:34 +00:00
|
|
|
stream->write_function(stream, "-ERR Missing args.\n");
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-01 22:56:14 +00:00
|
|
|
po.cmd = strdup(cmd);
|
|
|
|
po.stream = stream;
|
|
|
|
po.session = session;
|
|
|
|
perl_thread_run(NULL, &po);
|
2013-06-13 10:07:56 -07:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static switch_xml_t perl_fetch(const char *section,
|
|
|
|
const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params, void *user_data)
|
2008-04-25 22:12:01 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
char *argv[128] = { 0 };
|
|
|
|
int argc = 0;
|
|
|
|
switch_xml_t xml = NULL;
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(globals.xml_handler)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
PerlInterpreter *my_perl = clone_perl();
|
|
|
|
HV *hash;
|
|
|
|
char *str;
|
|
|
|
switch_event_header_t *hp;
|
|
|
|
SV *this;
|
|
|
|
char code[1024] = "";
|
|
|
|
|
|
|
|
argv[argc++] = "FreeSWITCH";
|
|
|
|
argv[argc++] = globals.xml_handler;
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
PERL_SET_CONTEXT(my_perl);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
|
|
|
if (perl_parse(my_perl, xs_init, argc, argv, (char **) NULL)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(hash = get_hv("XML_REQUEST", TRUE))) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(section)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
section = "";
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
this = newSV(strlen(section) + 1);
|
2008-04-25 22:12:01 +00:00
|
|
|
sv_setpv(this, section);
|
|
|
|
hv_store(hash, "section", 7, this, 0);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(tag_name)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
tag_name = "";
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
this = newSV(strlen(tag_name) + 1);
|
2008-04-25 22:12:01 +00:00
|
|
|
sv_setpv(this, tag_name);
|
|
|
|
hv_store(hash, "tag_name", 8, this, 0);
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(key_name)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
key_name = "";
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
this = newSV(strlen(key_name) + 1);
|
2008-04-25 22:12:01 +00:00
|
|
|
sv_setpv(this, key_name);
|
|
|
|
hv_store(hash, "key_name", 8, this, 0);
|
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (zstr(key_value)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
key_value = "";
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
this = newSV(strlen(key_value) + 1);
|
2008-04-25 22:12:01 +00:00
|
|
|
sv_setpv(this, key_value);
|
|
|
|
hv_store(hash, "key_value", 9, this, 0);
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
if (!(hash = get_hv("XML_DATA", TRUE))) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
for (hp = params->headers; hp; hp = hp->next) {
|
2008-05-27 04:54:52 +00:00
|
|
|
this = newSV(strlen(hp->value) + 1);
|
2008-04-25 22:12:01 +00:00
|
|
|
sv_setpv(this, hp->value);
|
|
|
|
hv_store(hash, hp->name, strlen(hp->name), this, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_snprintf(code, sizeof(code), "use lib '%s/perl';\n" "use freeswitch;\n", SWITCH_GLOBAL_dirs.base_dir);
|
2008-05-01 22:56:14 +00:00
|
|
|
Perl_safe_eval(my_perl, code);
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2008-05-07 15:28:07 +00:00
|
|
|
if (params) {
|
|
|
|
mod_perl_conjure_event(my_perl, params, "params");
|
|
|
|
}
|
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
perl_run(my_perl);
|
2008-05-07 15:28:07 +00:00
|
|
|
str = SvPV(get_sv("XML_STRING", TRUE), n_a);
|
2008-04-25 22:12:01 +00:00
|
|
|
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(str)) {
|
|
|
|
if (zstr(str)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Result\n");
|
|
|
|
} else if (!(xml = switch_xml_parse_str(str, strlen(str)))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing XML Result!\n");
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
destroy_perl(&my_perl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
2011-10-19 08:25:06 -05:00
|
|
|
|
|
|
|
SWITCH_STANDARD_CHAT_APP(perl_chat_function)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct perl_o po = { 0 };
|
|
|
|
|
|
|
|
if (zstr(data)) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
po.cmd = strdup(data);
|
|
|
|
po.stream = NULL;
|
|
|
|
po.session = NULL;
|
|
|
|
po.message = message;
|
|
|
|
perl_thread_run(NULL, &po);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
static switch_status_t do_config(void)
|
|
|
|
{
|
|
|
|
char *cf = "perl.conf";
|
|
|
|
switch_xml_t cfg, xml, settings, param;
|
|
|
|
|
|
|
|
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
|
2008-10-11 06:19:56 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
|
2008-04-25 22:12:01 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((settings = switch_xml_child(cfg, "settings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
|
|
|
|
|
|
|
if (!strcmp(var, "xml-handler-script")) {
|
|
|
|
globals.xml_handler = switch_core_strdup(globals.pool, val);
|
|
|
|
} else if (!strcmp(var, "xml-handler-bindings")) {
|
2009-10-23 16:03:42 +00:00
|
|
|
if (!zstr(globals.xml_handler)) {
|
2008-04-25 22:12:01 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "binding '%s' to '%s'\n", globals.xml_handler, var);
|
|
|
|
switch_xml_bind_search_function(perl_fetch, switch_xml_parse_section_string(val), NULL);
|
|
|
|
}
|
2008-07-17 20:01:53 +00:00
|
|
|
} else if (!strcmp(var, "startup-script")) {
|
2008-07-17 20:33:50 +00:00
|
|
|
if (val) {
|
2008-07-17 20:01:53 +00:00
|
|
|
perl_thread(val);
|
2008-07-17 20:33:50 +00:00
|
|
|
}
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_xml_free(xml);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_perl_load)
|
|
|
|
{
|
|
|
|
switch_application_interface_t *app_interface;
|
|
|
|
PerlInterpreter *my_perl;
|
|
|
|
char code[1024];
|
|
|
|
switch_api_interface_t *api_interface;
|
2013-05-14 07:38:00 -05:00
|
|
|
switch_chat_application_interface_t *chat_app_interface;
|
2008-04-25 22:12:01 +00:00
|
|
|
|
|
|
|
globals.pool = pool;
|
|
|
|
|
|
|
|
if (!(my_perl = perl_alloc())) {
|
2008-07-17 20:01:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate perl interpreter\n");
|
2008-04-25 22:12:01 +00:00
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
2008-07-17 20:48:14 +00:00
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Allocated perl intrepreter.\n");
|
|
|
|
|
|
|
|
perl_construct(my_perl);
|
|
|
|
perl_parse(my_perl, xs_init, 3, embedding, NULL);
|
|
|
|
perl_run(my_perl);
|
|
|
|
globals.my_perl = my_perl;
|
|
|
|
|
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
|
|
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
2008-05-26 15:45:40 +00:00
|
|
|
SWITCH_ADD_APP(app_interface, "perl", NULL, NULL, perl_function, NULL, SAF_SUPPORT_NOMEDIA);
|
2008-04-25 22:12:01 +00:00
|
|
|
SWITCH_ADD_API(api_interface, "perlrun", "run a script", perlrun_api_function, "<script>");
|
|
|
|
SWITCH_ADD_API(api_interface, "perl", "run a script", perl_api_function, "<script>");
|
2013-05-14 07:38:00 -05:00
|
|
|
SWITCH_ADD_CHAT_APP(chat_app_interface, "perl", "execute a perl script", "execute a perl script", perl_chat_function, "<script>", SCAF_NONE);
|
|
|
|
|
2008-04-25 22:12:01 +00:00
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
|
|
|
|
do_config();
|
|
|
|
|
2009-08-11 16:03:31 +00:00
|
|
|
return SWITCH_STATUS_NOUNLOAD;
|
2008-04-25 22:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2013-06-25 11:50:17 -05:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
2008-04-25 22:12:01 +00:00
|
|
|
*/
|