Sat Aug 6 00:39:19 2011

Asterisk developer's documentation


app_channelredirect.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2006, Sergey Basmanov
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief ChannelRedirect application
00020  *
00021  * \author Sergey Basmanov <sergey_basmanov@mail.ru>
00022  *
00023  * \ingroup applications
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 172030 $")
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <unistd.h>
00033 #include <string.h>
00034 
00035 #include "asterisk/file.h"
00036 #include "asterisk/logger.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/lock.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/features.h"
00043 #include "asterisk/options.h"
00044 
00045 static char *app = "ChannelRedirect";
00046 static char *synopsis = "Redirects given channel to a dialplan target.";
00047 static char *descrip = 
00048 "ChannelRedirect(channel|[[context|]extension|]priority):\n"
00049 "  Sends the specified channel to the specified extension priority\n";
00050 
00051 
00052 static int asyncgoto_exec(struct ast_channel *chan, void *data)
00053 {
00054    int res = -1;
00055    struct ast_module_user *u;
00056    char *info, *context, *exten, *priority;
00057    int prio = 1;
00058    struct ast_channel *chan2 = NULL;
00059 
00060    AST_DECLARE_APP_ARGS(args,
00061       AST_APP_ARG(channel);
00062       AST_APP_ARG(label);
00063    );
00064 
00065    if (ast_strlen_zero(data)) {
00066       ast_log(LOG_WARNING, "%s requires an argument (channel|[[context|]exten|]priority)\n", app);
00067       return -1;
00068    }
00069 
00070    u = ast_module_user_add(chan);
00071 
00072    info = ast_strdupa(data);
00073    AST_STANDARD_APP_ARGS(args, info);
00074 
00075    if (ast_strlen_zero(args.channel) || ast_strlen_zero(args.label)) {
00076       ast_log(LOG_WARNING, "%s requires an argument (channel|[[context|]exten|]priority)\n", app);
00077       goto quit;
00078    }
00079 
00080    chan2 = ast_get_channel_by_name_locked(args.channel);
00081    if (!chan2) {
00082       ast_log(LOG_WARNING, "No such channel: %s\n", args.channel);
00083       goto quit;
00084    }
00085 
00086    /* Parsed right to left, so standard parsing won't work */
00087    context = strsep(&args.label, "|");
00088    exten = strsep(&args.label, "|");
00089    if (exten) {
00090       priority = strsep(&args.label, "|");
00091       if (!priority) {
00092          priority = exten;
00093          exten = context;
00094          context = NULL;
00095       }
00096    } else {
00097       priority = context;
00098       context = NULL;
00099    }
00100 
00101    /* ast_findlabel_extension does not convert numeric priorities; it only does a lookup */
00102    if (!(prio = atoi(priority)) && !(prio = ast_findlabel_extension(chan2, S_OR(context, chan2->context),
00103                            S_OR(exten, chan2->exten), priority, chan2->cid.cid_num))) {
00104       ast_log(LOG_WARNING, "'%s' is not a known priority or label\n", priority);
00105       goto chanquit;
00106    }
00107 
00108    if (option_debug > 1)
00109       ast_log(LOG_DEBUG, "Attempting async goto (%s) to %s|%s|%d\n", args.channel, S_OR(context, chan2->context), S_OR(exten, chan2->exten), prio);
00110 
00111    if (chan2->pbx) {
00112       ast_channel_lock(chan2);
00113       ast_set_flag(chan2, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
00114       ast_channel_unlock(chan2);
00115    }
00116    if (ast_async_goto_if_exists(chan2, S_OR(context, chan2->context), S_OR(exten, chan2->exten), prio))
00117       ast_log(LOG_WARNING, "%s failed for %s\n", app, args.channel);
00118    else
00119       res = 0;
00120 
00121  chanquit:
00122    ast_mutex_unlock(&chan2->lock);
00123  quit:
00124    ast_module_user_remove(u);
00125 
00126    return res;
00127 }
00128 
00129 static int unload_module(void)
00130 {
00131    int res;
00132 
00133    res = ast_unregister_application(app);
00134 
00135    ast_module_user_hangup_all();
00136 
00137    return res; 
00138 }
00139 
00140 static int load_module(void)
00141 {
00142    return ast_register_application(app, asyncgoto_exec, synopsis, descrip);
00143 }
00144 
00145 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel Redirect");

Generated on Sat Aug 6 00:39:19 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7