2005-03-26 21:01:59 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
1999-12-04 20:45:45 +00:00
|
|
|
*
|
2006-01-03 22:16:23 +00:00
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
1999-12-04 20:45:45 +00:00
|
|
|
*
|
2004-07-17 02:25:53 +00:00
|
|
|
* Mark Spencer <markster@digium.com>
|
1999-12-04 20:45:45 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
1999-12-04 20:45:45 +00:00
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Core PBX routines.
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
2006-05-09 08:03:52 +00:00
|
|
|
* \author Mark Spencer <markster@digium.com>
|
1999-12-04 20:45:45 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2005-03-04 06:36:18 +00:00
|
|
|
#include <sys/types.h>
|
2005-04-22 13:11:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2006-08-14 02:14:14 +00:00
|
|
|
#include <limits.h>
|
2007-04-11 21:13:44 +00:00
|
|
|
#if defined(HAVE_SYSINFO)
|
2007-04-11 19:11:32 +00:00
|
|
|
#include <sys/sysinfo.h>
|
2007-04-11 20:59:08 +00:00
|
|
|
#endif
|
2005-04-22 13:11:34 +00:00
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/cli.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/options.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/callerid.h"
|
|
|
|
#include "asterisk/cdr.h"
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
#include "asterisk/term.h"
|
|
|
|
#include "asterisk/manager.h"
|
|
|
|
#include "asterisk/ast_expr.h"
|
|
|
|
#include "asterisk/linkedlists.h"
|
2006-03-23 14:28:16 +00:00
|
|
|
#define SAY_STUBS /* generate declarations and stubs for say methods */
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/say.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/causes.h"
|
|
|
|
#include "asterisk/musiconhold.h"
|
|
|
|
#include "asterisk/app.h"
|
2005-07-08 21:14:34 +00:00
|
|
|
#include "asterisk/devicestate.h"
|
2006-02-01 23:05:28 +00:00
|
|
|
#include "asterisk/stringfields.h"
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
#include "asterisk/event.h"
|
2007-07-16 14:39:29 +00:00
|
|
|
#include "asterisk/module.h"
|
1999-12-04 20:45:45 +00:00
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*!
|
|
|
|
* \note I M P O R T A N T :
|
1999-12-04 20:45:45 +00:00
|
|
|
*
|
|
|
|
* The speed of extension handling will likely be among the most important
|
|
|
|
* aspects of this PBX. The switching scheme as it exists right now isn't
|
|
|
|
* terribly bad (it's O(N+M), where N is the # of extensions and M is the avg #
|
2006-05-09 08:03:52 +00:00
|
|
|
* of priorities, but a constant search time here would be great ;-)
|
1999-12-04 20:45:45 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-06-12 14:34:15 +00:00
|
|
|
#ifdef LOW_MEMORY
|
|
|
|
#define EXT_DATA_SIZE 256
|
|
|
|
#else
|
|
|
|
#define EXT_DATA_SIZE 8192
|
|
|
|
#endif
|
1999-12-04 20:45:45 +00:00
|
|
|
|
2005-01-13 05:14:56 +00:00
|
|
|
#define SWITCH_DATA_LENGTH 256
|
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
#define VAR_BUF_SIZE 4096
|
2005-01-13 05:14:56 +00:00
|
|
|
|
2004-11-01 02:23:28 +00:00
|
|
|
#define VAR_NORMAL 1
|
|
|
|
#define VAR_SOFTTRAN 2
|
|
|
|
#define VAR_HARDTRAN 3
|
|
|
|
|
2005-04-15 05:47:40 +00:00
|
|
|
#define BACKGROUND_SKIP (1 << 0)
|
|
|
|
#define BACKGROUND_NOANSWER (1 << 1)
|
2005-07-12 01:34:06 +00:00
|
|
|
#define BACKGROUND_MATCHEXTEN (1 << 2)
|
|
|
|
#define BACKGROUND_PLAYBACK (1 << 3)
|
2005-04-15 05:47:40 +00:00
|
|
|
|
2005-11-03 21:19:11 +00:00
|
|
|
AST_APP_OPTIONS(background_opts, {
|
|
|
|
AST_APP_OPTION('s', BACKGROUND_SKIP),
|
|
|
|
AST_APP_OPTION('n', BACKGROUND_NOANSWER),
|
|
|
|
AST_APP_OPTION('m', BACKGROUND_MATCHEXTEN),
|
|
|
|
AST_APP_OPTION('p', BACKGROUND_PLAYBACK),
|
2005-04-15 05:47:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
#define WAITEXTEN_MOH (1 << 0)
|
|
|
|
|
2005-11-03 21:19:11 +00:00
|
|
|
AST_APP_OPTIONS(waitexten_opts, {
|
2006-09-03 20:44:14 +00:00
|
|
|
AST_APP_OPTION_ARG('m', WAITEXTEN_MOH, 0),
|
2005-04-15 05:47:40 +00:00
|
|
|
});
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
struct ast_context;
|
2006-09-28 22:49:55 +00:00
|
|
|
struct ast_app;
|
1999-12-04 20:45:45 +00:00
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
/*!
|
2006-05-09 08:03:52 +00:00
|
|
|
\brief ast_exten: An extension
|
2005-11-14 19:00:38 +00:00
|
|
|
The dialplan is saved as a linked list with each context
|
|
|
|
having it's own linked list of extensions - one item per
|
|
|
|
priority.
|
|
|
|
*/
|
1999-12-04 20:45:45 +00:00
|
|
|
struct ast_exten {
|
2005-12-24 06:23:36 +00:00
|
|
|
char *exten; /*!< Extension name */
|
|
|
|
int matchcid; /*!< Match caller id ? */
|
2006-01-21 23:05:19 +00:00
|
|
|
const char *cidmatch; /*!< Caller id to match for this extension */
|
2005-12-24 06:23:36 +00:00
|
|
|
int priority; /*!< Priority */
|
2006-01-21 23:05:19 +00:00
|
|
|
const char *label; /*!< Label */
|
2005-12-24 06:23:36 +00:00
|
|
|
struct ast_context *parent; /*!< The context this extension belongs to */
|
2006-01-21 23:05:19 +00:00
|
|
|
const char *app; /*!< Application to execute */
|
2006-09-28 22:49:55 +00:00
|
|
|
struct ast_app *cached_app; /*!< Cached location of application */
|
2005-12-24 06:23:36 +00:00
|
|
|
void *data; /*!< Data to use (arguments) */
|
|
|
|
void (*datad)(void *); /*!< Data destructor */
|
|
|
|
struct ast_exten *peer; /*!< Next higher priority with our extension */
|
|
|
|
const char *registrar; /*!< Registrar */
|
|
|
|
struct ast_exten *next; /*!< Extension with a greater ID */
|
2004-10-03 04:19:59 +00:00
|
|
|
char stuff[0];
|
1999-12-04 20:45:45 +00:00
|
|
|
};
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief ast_include: include= support in extensions.conf */
|
2001-03-30 18:47:35 +00:00
|
|
|
struct ast_include {
|
2006-05-09 08:03:52 +00:00
|
|
|
const char *name;
|
2006-01-21 23:05:19 +00:00
|
|
|
const char *rname; /*!< Context to include */
|
2005-12-24 06:23:36 +00:00
|
|
|
const char *registrar; /*!< Registrar */
|
|
|
|
int hastime; /*!< If time construct exists */
|
|
|
|
struct ast_timing timing; /*!< time construct */
|
|
|
|
struct ast_include *next; /*!< Link them together */
|
2004-10-03 04:19:59 +00:00
|
|
|
char stuff[0];
|
2001-03-30 18:47:35 +00:00
|
|
|
};
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief ast_sw: Switch statement in extensions.conf */
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_sw {
|
2004-10-03 04:19:59 +00:00
|
|
|
char *name;
|
2005-12-24 06:23:36 +00:00
|
|
|
const char *registrar; /*!< Registrar */
|
|
|
|
char *data; /*!< Data load */
|
2005-01-13 05:14:56 +00:00
|
|
|
int eval;
|
2006-05-08 11:20:19 +00:00
|
|
|
AST_LIST_ENTRY(ast_sw) list;
|
2005-01-13 05:14:56 +00:00
|
|
|
char *tmpdata;
|
2004-10-03 04:19:59 +00:00
|
|
|
char stuff[0];
|
2001-09-28 21:20:52 +00:00
|
|
|
};
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief ast_ignorepat: Ignore patterns in dial plan */
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_ignorepat {
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *registrar;
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_ignorepat *next;
|
2006-01-21 23:05:19 +00:00
|
|
|
const char pattern[0];
|
2001-09-28 21:20:52 +00:00
|
|
|
};
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief ast_context: An extension context */
|
1999-12-04 20:45:45 +00:00
|
|
|
struct ast_context {
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rwlock_t lock; /*!< A lock to prevent multiple threads from clobbering the context */
|
2005-11-14 19:00:38 +00:00
|
|
|
struct ast_exten *root; /*!< The root of the list of extensions */
|
|
|
|
struct ast_context *next; /*!< Link them together */
|
|
|
|
struct ast_include *includes; /*!< Include other contexts */
|
|
|
|
struct ast_ignorepat *ignorepats; /*!< Patterns for which to continue playing dialtone */
|
|
|
|
const char *registrar; /*!< Registrar */
|
2006-05-08 11:20:19 +00:00
|
|
|
AST_LIST_HEAD_NOLOCK(, ast_sw) alts; /*!< Alternative switches */
|
2006-08-14 03:24:06 +00:00
|
|
|
ast_mutex_t macrolock; /*!< A lock to implement "exclusive" macros - held whilst a call is executing in the macro */
|
2005-11-14 19:00:38 +00:00
|
|
|
char name[0]; /*!< Name of the context */
|
1999-12-04 20:45:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief ast_app: A registered application */
|
1999-12-04 20:45:45 +00:00
|
|
|
struct ast_app {
|
|
|
|
int (*execute)(struct ast_channel *chan, void *data);
|
2005-12-24 06:23:36 +00:00
|
|
|
const char *synopsis; /*!< Synopsis text for 'show applications' */
|
2006-05-09 16:24:07 +00:00
|
|
|
const char *description; /*!< Description (help text) for 'show application <name>' */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_ENTRY(ast_app) list; /*!< Next app in list */
|
2007-07-16 14:39:29 +00:00
|
|
|
struct ast_module *module; /*!< Module this app belongs to */
|
2005-12-24 06:23:36 +00:00
|
|
|
char name[0]; /*!< Name of the application */
|
1999-12-04 20:45:45 +00:00
|
|
|
};
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief ast_state_cb: An extension state notify register item */
|
2003-04-06 18:19:51 +00:00
|
|
|
struct ast_state_cb {
|
2005-03-26 21:01:59 +00:00
|
|
|
int id;
|
|
|
|
void *data;
|
|
|
|
ast_state_cb_type callback;
|
|
|
|
struct ast_state_cb *next;
|
2003-03-30 22:55:42 +00:00
|
|
|
};
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief Structure for dial plan hints
|
|
|
|
|
METERMAIDS:
-----------
- Adding devicestate providers, a new architecture to add non-channel related
device state information, like parking lots, queues, meetmes, vending machines
and Windows 98 reboots (lots of blinking on those lights)
- Adding provider for parking lots, so you can subscribe to the status of a
parking lot
- Adding provider for meetme, so you can have a blinking lamp for a meetme
( Example: exten => edvina,hint,meetme:1234 )
- Adding support for directed parking - set the PARKINGEXTEN before you manually
call Park() and you will be parked on that space. If it's occupied, dialplan
execution will continue.
This work was sponsored by Voop A/S - www.voop.com
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@36055 f38db490-d61c-443f-a65b-d21fe96a405b
2006-06-26 16:43:21 +00:00
|
|
|
\note Hints are pointers from an extension in the dialplan to one or
|
2006-11-30 20:34:23 +00:00
|
|
|
more devices (tech/name)
|
|
|
|
- See \ref AstExtState
|
|
|
|
*/
|
2003-04-06 18:19:51 +00:00
|
|
|
struct ast_hint {
|
2005-11-14 19:00:38 +00:00
|
|
|
struct ast_exten *exten; /*!< Extension */
|
|
|
|
int laststate; /*!< Last known state */
|
|
|
|
struct ast_state_cb *callbacks; /*!< Callback list for this extension */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_ENTRY(ast_hint) list;/*!< Pointer to next hint in list */
|
2003-03-30 22:55:42 +00:00
|
|
|
};
|
|
|
|
|
2006-03-17 21:39:36 +00:00
|
|
|
static const struct cfextension_states {
|
|
|
|
int extension_state;
|
|
|
|
const char * const text;
|
|
|
|
} extension_states[] = {
|
|
|
|
{ AST_EXTENSION_NOT_INUSE, "Idle" },
|
|
|
|
{ AST_EXTENSION_INUSE, "InUse" },
|
|
|
|
{ AST_EXTENSION_BUSY, "Busy" },
|
|
|
|
{ AST_EXTENSION_UNAVAILABLE, "Unavailable" },
|
|
|
|
{ AST_EXTENSION_RINGING, "Ringing" },
|
2006-07-08 02:24:07 +00:00
|
|
|
{ AST_EXTENSION_INUSE | AST_EXTENSION_RINGING, "InUse&Ringing" },
|
|
|
|
{ AST_EXTENSION_ONHOLD, "Hold" },
|
|
|
|
{ AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD, "InUse&Hold" }
|
2006-03-17 21:39:36 +00:00
|
|
|
};
|
|
|
|
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
struct statechange {
|
|
|
|
AST_LIST_ENTRY(statechange) entry;
|
|
|
|
char dev[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Data used by the device state thread
|
|
|
|
*/
|
|
|
|
static struct {
|
|
|
|
/*! Set to 1 to stop the thread */
|
|
|
|
unsigned int stop:1;
|
|
|
|
/*! The device state monitoring thread */
|
|
|
|
pthread_t thread;
|
|
|
|
/*! Lock for the state change queue */
|
|
|
|
ast_mutex_t lock;
|
|
|
|
/*! Condition for the state change queue */
|
|
|
|
ast_cond_t cond;
|
|
|
|
/*! Queue of state changes */
|
|
|
|
AST_LIST_HEAD_NOLOCK(, statechange) state_change_q;
|
|
|
|
} device_state = {
|
|
|
|
.thread = AST_PTHREADT_NULL,
|
|
|
|
};
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
static int pbx_builtin_answer(struct ast_channel *, void *);
|
|
|
|
static int pbx_builtin_goto(struct ast_channel *, void *);
|
|
|
|
static int pbx_builtin_hangup(struct ast_channel *, void *);
|
|
|
|
static int pbx_builtin_background(struct ast_channel *, void *);
|
|
|
|
static int pbx_builtin_wait(struct ast_channel *, void *);
|
2004-04-06 04:14:19 +00:00
|
|
|
static int pbx_builtin_waitexten(struct ast_channel *, void *);
|
2007-06-20 23:26:07 +00:00
|
|
|
static int pbx_builtin_keepalive(struct ast_channel *, void *);
|
2003-09-08 21:52:45 +00:00
|
|
|
static int pbx_builtin_resetcdr(struct ast_channel *, void *);
|
2004-06-28 03:48:53 +00:00
|
|
|
static int pbx_builtin_setamaflags(struct ast_channel *, void *);
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_ringing(struct ast_channel *, void *);
|
2004-06-22 03:51:34 +00:00
|
|
|
static int pbx_builtin_progress(struct ast_channel *, void *);
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_congestion(struct ast_channel *, void *);
|
|
|
|
static int pbx_builtin_busy(struct ast_channel *, void *);
|
2003-02-12 13:59:15 +00:00
|
|
|
static int pbx_builtin_noop(struct ast_channel *, void *);
|
2002-09-11 17:09:48 +00:00
|
|
|
static int pbx_builtin_gotoif(struct ast_channel *, void *);
|
2003-04-28 04:18:47 +00:00
|
|
|
static int pbx_builtin_gotoiftime(struct ast_channel *, void *);
|
2005-02-25 00:25:28 +00:00
|
|
|
static int pbx_builtin_execiftime(struct ast_channel *, void *);
|
2003-04-16 13:43:11 +00:00
|
|
|
static int pbx_builtin_saynumber(struct ast_channel *, void *);
|
|
|
|
static int pbx_builtin_saydigits(struct ast_channel *, void *);
|
2004-05-03 00:54:16 +00:00
|
|
|
static int pbx_builtin_saycharacters(struct ast_channel *, void *);
|
|
|
|
static int pbx_builtin_sayphonetic(struct ast_channel *, void *);
|
2003-09-22 15:27:09 +00:00
|
|
|
int pbx_builtin_setvar(struct ast_channel *, void *);
|
2004-11-17 04:48:58 +00:00
|
|
|
static int pbx_builtin_importvar(struct ast_channel *, void *);
|
2002-09-11 17:09:48 +00:00
|
|
|
|
2006-12-21 03:20:08 +00:00
|
|
|
AST_RWLOCK_DEFINE_STATIC(globalslock);
|
2006-08-21 20:04:59 +00:00
|
|
|
static struct varshead globals = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
|
1999-12-04 20:45:45 +00:00
|
|
|
|
2006-08-30 21:44:05 +00:00
|
|
|
static int autofallthrough = 1;
|
2004-10-16 19:46:02 +00:00
|
|
|
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
/*! \brief Subscription for device state change events */
|
|
|
|
static struct ast_event_sub *device_state_sub;
|
|
|
|
|
2005-05-18 01:49:13 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(maxcalllock);
|
2006-12-27 22:14:33 +00:00
|
|
|
static int countcalls;
|
2005-05-18 01:49:13 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
static AST_RWLIST_HEAD_STATIC(acf_root, ast_custom_function);
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief Declaration of builtin applications */
|
1999-12-04 20:45:45 +00:00
|
|
|
static struct pbx_builtin {
|
|
|
|
char name[AST_MAX_APP];
|
|
|
|
int (*execute)(struct ast_channel *chan, void *data);
|
2001-04-27 15:07:56 +00:00
|
|
|
char *synopsis;
|
|
|
|
char *description;
|
2006-05-09 08:03:52 +00:00
|
|
|
} builtins[] =
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
|
|
|
/* These applications are built into the PBX core and do not
|
2005-07-19 15:54:17 +00:00
|
|
|
need separate modules */
|
2003-04-27 19:35:06 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
{ "Answer", pbx_builtin_answer,
|
|
|
|
"Answer a channel if ringing",
|
2005-11-09 21:48:59 +00:00
|
|
|
" Answer([delay]): If the call has not been answered, this application will\n"
|
|
|
|
"answer it. Otherwise, it has no effect on the call. If a delay is specified,\n"
|
2007-01-12 14:45:20 +00:00
|
|
|
"Asterisk will wait this number of milliseconds before returning to\n"
|
|
|
|
"the dialplan after answering the call.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2003-04-27 19:35:06 +00:00
|
|
|
{ "BackGround", pbx_builtin_background,
|
2006-07-23 03:29:19 +00:00
|
|
|
"Play an audio file while waiting for digits of an extension to go to.",
|
2007-07-23 19:51:41 +00:00
|
|
|
" Background(filename1[&filename2...][,options[,langoverride][,context]]):\n"
|
2005-11-09 21:48:59 +00:00
|
|
|
"This application will play the given list of files while waiting for an\n"
|
|
|
|
"extension to be dialed by the calling channel. To continue waiting for digits\n"
|
|
|
|
"after this application has finished playing files, the WaitExten application\n"
|
2006-05-30 16:01:50 +00:00
|
|
|
"should be used. The 'langoverride' option explicitly specifies which language\n"
|
2005-11-09 21:48:59 +00:00
|
|
|
"to attempt to use for the requested sound files. If a 'context' is specified,\n"
|
|
|
|
"this is the dialplan context that this application will use when exiting to a\n"
|
|
|
|
"dialed extension."
|
|
|
|
" If one of the requested sound files does not exist, call processing will be\n"
|
|
|
|
"terminated.\n"
|
2005-04-15 05:47:40 +00:00
|
|
|
" Options:\n"
|
2006-05-30 16:01:50 +00:00
|
|
|
" s - Causes the playback of the message to be skipped\n"
|
2005-04-15 05:47:40 +00:00
|
|
|
" if the channel is not in the 'up' state (i.e. it\n"
|
2006-05-30 16:01:50 +00:00
|
|
|
" hasn't been answered yet). If this happens, the\n"
|
2005-04-15 05:47:40 +00:00
|
|
|
" application will return immediately.\n"
|
2006-05-30 16:01:50 +00:00
|
|
|
" n - Don't answer the channel before playing the files.\n"
|
|
|
|
" m - Only break if a digit hit matches a one digit\n"
|
|
|
|
" extension in the destination context.\n"
|
2006-09-27 20:03:23 +00:00
|
|
|
"This application sets the following channel variable upon completion:\n"
|
|
|
|
" BACKGROUNDSTATUS The status of the background attempt as a text string, one of\n"
|
|
|
|
" SUCCESS | FAILED\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2003-04-27 19:35:06 +00:00
|
|
|
{ "Busy", pbx_builtin_busy,
|
2005-11-09 21:48:59 +00:00
|
|
|
"Indicate the Busy condition",
|
|
|
|
" Busy([timeout]): This application will indicate the busy condition to\n"
|
|
|
|
"the calling channel. If the optional timeout is specified, the calling channel\n"
|
|
|
|
"will be hung up after the specified number of seconds. Otherwise, this\n"
|
|
|
|
"application will wait until the calling channel hangs up.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2003-04-27 19:35:06 +00:00
|
|
|
|
|
|
|
{ "Congestion", pbx_builtin_congestion,
|
2005-11-09 21:48:59 +00:00
|
|
|
"Indicate the Congestion condition",
|
2006-05-24 18:17:50 +00:00
|
|
|
" Congestion([timeout]): This application will indicate the congestion\n"
|
2005-11-09 21:48:59 +00:00
|
|
|
"condition to the calling channel. If the optional timeout is specified, the\n"
|
|
|
|
"calling channel will be hung up after the specified number of seconds.\n"
|
|
|
|
"Otherwise, this application will wait until the calling channel hangs up.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-09-21 20:01:54 +00:00
|
|
|
{ "ExecIfTime", pbx_builtin_execiftime,
|
|
|
|
"Conditional application execution based on the current time",
|
2007-07-23 19:51:41 +00:00
|
|
|
" ExecIfTime(<times>,<weekdays>,<mdays>,<months>?appname[(appargs)]):\n"
|
2006-09-21 20:01:54 +00:00
|
|
|
"This application will execute the specified dialplan application, with optional\n"
|
|
|
|
"arguments, if the current time matches the given time specification.\n"
|
|
|
|
},
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
{ "Goto", pbx_builtin_goto,
|
2005-11-09 21:48:59 +00:00
|
|
|
"Jump to a particular priority, extension, or context",
|
2007-07-23 19:51:41 +00:00
|
|
|
" Goto([[context,]extension,]priority): This application will set the current\n"
|
2007-03-02 17:03:47 +00:00
|
|
|
"context, extension, and priority in the channel structure. After it completes, the\n"
|
|
|
|
"pbx engine will continue dialplan execution at the specified location.\n"
|
2005-11-09 21:48:59 +00:00
|
|
|
"If no specific extension, or extension and context, are specified, then this\n"
|
2007-03-02 17:03:47 +00:00
|
|
|
"application will just set the specified priority of the current extension.\n"
|
2007-02-28 19:30:58 +00:00
|
|
|
" At least a priority is required as an argument, or the goto will return a -1,\n"
|
|
|
|
"and the channel and call will be terminated.\n"
|
2007-03-02 17:03:47 +00:00
|
|
|
" If the location that is put into the channel information is bogus, and asterisk cannot\n"
|
|
|
|
"find that location in the dialplan,\n"
|
2007-02-28 19:30:58 +00:00
|
|
|
"then the execution engine will try to find and execute the code in the 'i' (invalid)\n"
|
|
|
|
"extension in the current context. If that does not exist, it will try to execute the\n"
|
|
|
|
"'h' extension. If either or neither the 'h' or 'i' extensions have been defined, the\n"
|
|
|
|
"channel is hung up, and the execution of instructions on the channel is terminated.\n"
|
2007-03-02 17:03:47 +00:00
|
|
|
"What this means is that, for example, you specify a context that does not exist, then\n"
|
|
|
|
"it will not be possible to find the 'h' or 'i' extensions, and the call will terminate!\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-11-24 15:15:01 +00:00
|
|
|
|
2003-04-27 19:35:06 +00:00
|
|
|
{ "GotoIf", pbx_builtin_gotoif,
|
2004-07-17 02:25:53 +00:00
|
|
|
"Conditional goto",
|
2007-03-02 17:03:47 +00:00
|
|
|
" GotoIf(condition?[labeliftrue]:[labeliffalse]): This application will set the current\n"
|
|
|
|
"context, extension, and priority in the channel structure based on the evaluation of\n"
|
|
|
|
"the given condition. After this application completes, the\n"
|
|
|
|
"pbx engine will continue dialplan execution at the specified location in the dialplan.\n"
|
|
|
|
"The channel will continue at\n"
|
2006-03-29 14:24:30 +00:00
|
|
|
"'labeliftrue' if the condition is true, or 'labeliffalse' if the condition is\n"
|
|
|
|
"false. The labels are specified with the same syntax as used within the Goto\n"
|
|
|
|
"application. If the label chosen by the condition is omitted, no jump is\n"
|
2007-02-28 19:30:58 +00:00
|
|
|
"performed, and the execution passes to the next instruction.\n"
|
|
|
|
"If the target location is bogus, and does not exist, the execution engine will try \n"
|
|
|
|
"to find and execute the code in the 'i' (invalid)\n"
|
|
|
|
"extension in the current context. If that does not exist, it will try to execute the\n"
|
|
|
|
"'h' extension. If either or neither the 'h' or 'i' extensions have been defined, the\n"
|
|
|
|
"channel is hung up, and the execution of instructions on the channel is terminated.\n"
|
2007-03-02 17:03:47 +00:00
|
|
|
"Remember that this command can set the current context, and if the context specified\n"
|
|
|
|
"does not exist, then it will not be able to find any 'h' or 'i' extensions there, and\n"
|
2007-03-05 15:30:37 +00:00
|
|
|
"the channel and call will both be terminated!\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2003-04-28 04:18:47 +00:00
|
|
|
{ "GotoIfTime", pbx_builtin_gotoiftime,
|
2005-11-09 21:48:59 +00:00
|
|
|
"Conditional Goto based on the current time",
|
2007-07-23 19:51:41 +00:00
|
|
|
" GotoIfTime(<times>,<weekdays>,<mdays>,<months>?[[context,]exten,]priority):\n"
|
2007-03-02 17:03:47 +00:00
|
|
|
"This application will set the context, extension, and priority in the channel structure\n"
|
|
|
|
"if the current time matches the given time specification. Otherwise, nothing is done.\n"
|
|
|
|
"Further information on the time specification can be found in examples\n"
|
|
|
|
"illustrating how to do time-based context includes in the dialplan.\n"
|
2007-02-28 19:30:58 +00:00
|
|
|
"If the target jump location is bogus, the same actions would be taken as for Goto.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2005-02-25 00:25:28 +00:00
|
|
|
|
2006-09-21 20:01:54 +00:00
|
|
|
{ "ImportVar", pbx_builtin_importvar,
|
|
|
|
"Import a variable from a channel into a new variable",
|
2007-07-23 19:51:41 +00:00
|
|
|
" ImportVar(newvar=channelname,variable): This application imports a variable\n"
|
2006-09-21 20:01:54 +00:00
|
|
|
"from the specified channel (as opposed to the current one) and stores it as\n"
|
|
|
|
"a variable in the current channel (the channel that is calling this\n"
|
|
|
|
"application). Variables created by this application have the same inheritance\n"
|
|
|
|
"properties as those created with the Set application. See the documentation for\n"
|
|
|
|
"Set for more information.\n"
|
2005-02-25 00:25:28 +00:00
|
|
|
},
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-04-27 19:35:06 +00:00
|
|
|
{ "Hangup", pbx_builtin_hangup,
|
2005-11-09 21:48:59 +00:00
|
|
|
"Hang up the calling channel",
|
2006-05-25 20:51:27 +00:00
|
|
|
" Hangup([causecode]): This application will hang up the calling channel.\n"
|
|
|
|
"If a causecode is given the channel's hangup cause will be set to the given\n"
|
|
|
|
"value.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2003-04-27 19:35:06 +00:00
|
|
|
{ "NoOp", pbx_builtin_noop,
|
2005-11-09 21:48:59 +00:00
|
|
|
"Do Nothing",
|
|
|
|
" NoOp(): This applicatiion does nothing. However, it is useful for debugging\n"
|
|
|
|
"purposes. Any text that is provided as arguments to this application can be\n"
|
|
|
|
"viewed at the Asterisk CLI. This method can be used to see the evaluations of\n"
|
2006-05-09 08:03:52 +00:00
|
|
|
"variables or functions without having any effect."
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2004-06-22 03:51:34 +00:00
|
|
|
{ "Progress", pbx_builtin_progress,
|
2004-07-17 02:25:53 +00:00
|
|
|
"Indicate progress",
|
2005-11-09 21:48:59 +00:00
|
|
|
" Progress(): This application will request that in-band progress information\n"
|
|
|
|
"be provided to the calling channel.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2004-06-22 03:51:34 +00:00
|
|
|
|
2003-09-08 21:52:45 +00:00
|
|
|
{ "ResetCDR", pbx_builtin_resetcdr,
|
2004-07-17 02:25:53 +00:00
|
|
|
"Resets the Call Data Record",
|
2005-11-09 21:48:59 +00:00
|
|
|
" ResetCDR([options]): This application causes the Call Data Record to be\n"
|
|
|
|
"reset.\n"
|
|
|
|
" Options:\n"
|
|
|
|
" w -- Store the current CDR record before resetting it.\n"
|
|
|
|
" a -- Store any stacked records.\n"
|
|
|
|
" v -- Save CDR variables.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2003-09-08 21:52:45 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
{ "Ringing", pbx_builtin_ringing,
|
2004-07-17 02:25:53 +00:00
|
|
|
"Indicate ringing tone",
|
2005-11-09 21:48:59 +00:00
|
|
|
" Ringing(): This application will request that the channel indicate a ringing\n"
|
|
|
|
"tone to the user.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-09-21 20:01:54 +00:00
|
|
|
{ "SayAlpha", pbx_builtin_saycharacters,
|
|
|
|
"Say Alpha",
|
|
|
|
" SayAlpha(string): This application will play the sounds that correspond to\n"
|
|
|
|
"the letters of the given string.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2003-04-27 19:35:06 +00:00
|
|
|
{ "SayDigits", pbx_builtin_saydigits,
|
2004-07-17 02:25:53 +00:00
|
|
|
"Say Digits",
|
2005-11-09 21:48:59 +00:00
|
|
|
" SayDigits(digits): This application will play the sounds that correspond\n"
|
|
|
|
"to the digits of the given number. This will use the language that is currently\n"
|
|
|
|
"set for the channel. See the LANGUAGE function for more information on setting\n"
|
|
|
|
"the language for the channel.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-09-21 20:01:54 +00:00
|
|
|
{ "SayNumber", pbx_builtin_saynumber,
|
|
|
|
"Say Number",
|
|
|
|
" SayNumber(digits[,gender]): This application will play the sounds that\n"
|
|
|
|
"correspond to the given number. Optionally, a gender may be specified.\n"
|
|
|
|
"This will use the language that is currently set for the channel. See the\n"
|
|
|
|
"LANGUAGE function for more information on setting the language for the channel.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2004-05-03 00:54:16 +00:00
|
|
|
|
|
|
|
{ "SayPhonetic", pbx_builtin_sayphonetic,
|
2004-07-17 02:25:53 +00:00
|
|
|
"Say Phonetic",
|
2005-11-09 21:48:59 +00:00
|
|
|
" SayPhonetic(string): This application will play the sounds from the phonetic\n"
|
|
|
|
"alphabet that correspond to the letters in the given string.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2004-05-03 00:54:16 +00:00
|
|
|
|
2005-05-15 23:32:38 +00:00
|
|
|
{ "Set", pbx_builtin_setvar,
|
2005-11-09 21:48:59 +00:00
|
|
|
"Set channel variable(s) or function value(s)",
|
2007-07-23 19:51:41 +00:00
|
|
|
" Set(name1=value1)\n"
|
2005-11-09 21:48:59 +00:00
|
|
|
"This function can be used to set the value of channel variables or dialplan\n"
|
|
|
|
"functions. It will accept up to 24 name/value pairs. When setting variables,\n"
|
|
|
|
"if the variable name is prefixed with _, the variable will be inherited into\n"
|
|
|
|
"channels created from the current channel. If the variable name is prefixed\n"
|
|
|
|
"with __, the variable will be inherited into channels created from the current\n"
|
|
|
|
"channel and all children channels.\n"
|
2005-05-15 23:32:38 +00:00
|
|
|
},
|
|
|
|
|
2006-09-21 20:01:54 +00:00
|
|
|
{ "SetAMAFlags", pbx_builtin_setamaflags,
|
|
|
|
"Set the AMA Flags",
|
|
|
|
" SetAMAFlags([flag]): This application will set the channel's AMA Flags for\n"
|
|
|
|
" billing purposes.\n"
|
2005-05-14 00:36:55 +00:00
|
|
|
},
|
2004-11-17 04:48:58 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
{ "Wait", pbx_builtin_wait,
|
|
|
|
"Waits for some time",
|
2005-11-09 21:48:59 +00:00
|
|
|
" Wait(seconds): This application waits for a specified number of seconds.\n"
|
|
|
|
"Then, dialplan execution will continue at the next priority.\n"
|
|
|
|
" Note that the seconds can be passed with fractions of a second. For example,\n"
|
2006-05-09 08:03:52 +00:00
|
|
|
"'1.5' will ask the application to wait for 1.5 seconds.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2004-04-06 04:14:19 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
{ "WaitExten", pbx_builtin_waitexten,
|
|
|
|
"Waits for an extension to be entered",
|
2007-07-23 19:51:41 +00:00
|
|
|
" WaitExten([seconds][,options]): This application waits for the user to enter\n"
|
2005-11-09 21:48:59 +00:00
|
|
|
"a new extension for a specified number of seconds.\n"
|
|
|
|
" Note that the seconds can be passed with fractions of a second. For example,\n"
|
2006-05-09 08:03:52 +00:00
|
|
|
"'1.5' will ask the application to wait for 1.5 seconds.\n"
|
2005-04-15 05:47:40 +00:00
|
|
|
" Options:\n"
|
2005-11-09 21:48:59 +00:00
|
|
|
" m[(x)] - Provide music on hold to the caller while waiting for an extension.\n"
|
2005-04-15 05:47:40 +00:00
|
|
|
" Optionally, specify the class for music on hold within parenthesis.\n"
|
2004-07-17 02:25:53 +00:00
|
|
|
},
|
2004-04-06 04:14:19 +00:00
|
|
|
|
2007-06-20 23:26:07 +00:00
|
|
|
{ "KeepAlive", pbx_builtin_keepalive,
|
|
|
|
"returns AST_PBX_KEEPALIVE value",
|
|
|
|
" KeepAlive(): This application is chiefly meant for internal use with Gosubs.\n"
|
|
|
|
"Please do not run it alone from the dialplan!\n"
|
|
|
|
},
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
};
|
|
|
|
|
2006-12-27 22:14:33 +00:00
|
|
|
static struct ast_context *contexts;
|
2007-02-28 20:46:01 +00:00
|
|
|
AST_RWLOCK_DEFINE_STATIC(conlock); /*!< Lock for the ast_context list */
|
2006-05-01 03:14:21 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
static AST_RWLIST_HEAD_STATIC(apps, ast_app);
|
1999-12-04 20:45:45 +00:00
|
|
|
|
2006-12-21 01:48:35 +00:00
|
|
|
static AST_RWLIST_HEAD_STATIC(switches, ast_switch);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2003-04-06 18:19:51 +00:00
|
|
|
static int stateid = 1;
|
2006-03-31 18:28:52 +00:00
|
|
|
/* WARNING:
|
|
|
|
When holding this list's lock, do _not_ do anything that will cause conlock
|
|
|
|
to be taken, unless you _already_ hold it. The ast_merge_contexts_and_delete
|
|
|
|
function will take the locks in conlock/hints order, so any other
|
|
|
|
paths that require both locks must also take them in that order.
|
|
|
|
*/
|
2006-11-02 21:40:53 +00:00
|
|
|
static AST_RWLIST_HEAD_STATIC(hints, ast_hint);
|
2006-12-27 22:14:33 +00:00
|
|
|
struct ast_state_cb *statecbs;
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
/*
|
2005-11-14 19:00:38 +00:00
|
|
|
\note This function is special. It saves the stack so that no matter
|
|
|
|
how many times it is called, it returns to the same place */
|
|
|
|
int pbx_exec(struct ast_channel *c, /*!< Channel */
|
2006-12-27 22:14:33 +00:00
|
|
|
struct ast_app *app, /*!< Application */
|
|
|
|
void *data) /*!< Data for execution */
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
|
|
|
int res;
|
2007-07-16 14:39:29 +00:00
|
|
|
struct ast_module_user *u = NULL;
|
2006-03-30 21:29:39 +00:00
|
|
|
const char *saved_c_appl;
|
|
|
|
const char *saved_c_data;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2007-05-02 20:57:55 +00:00
|
|
|
if (c->cdr && !ast_check_hangup(c))
|
2006-03-30 21:29:39 +00:00
|
|
|
ast_cdr_setapp(c->cdr, app->name, data);
|
2004-04-04 21:30:24 +00:00
|
|
|
|
2006-03-30 21:29:39 +00:00
|
|
|
/* save channel values */
|
|
|
|
saved_c_appl= c->appl;
|
|
|
|
saved_c_data= c->data;
|
2004-04-04 21:30:24 +00:00
|
|
|
|
2006-03-30 21:29:39 +00:00
|
|
|
c->appl = app->name;
|
2006-04-06 16:44:31 +00:00
|
|
|
c->data = data;
|
2007-07-16 14:39:29 +00:00
|
|
|
if (app->module)
|
|
|
|
u = __ast_module_user_add(app->module, c);
|
2006-04-06 16:44:31 +00:00
|
|
|
res = app->execute(c, data);
|
2007-07-16 14:39:29 +00:00
|
|
|
if (app->module && u)
|
|
|
|
__ast_module_user_remove(app->module, u);
|
2006-03-30 21:29:39 +00:00
|
|
|
/* restore channel values */
|
2006-04-06 16:44:31 +00:00
|
|
|
c->appl = saved_c_appl;
|
|
|
|
c->data = saved_c_data;
|
2006-03-30 21:29:39 +00:00
|
|
|
return res;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! Go no deeper than this through includes (not counting loops) */
|
2004-12-31 00:37:55 +00:00
|
|
|
#define AST_PBX_MAX_STACK 128
|
2001-03-30 18:47:35 +00:00
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief Find application handle in linked list
|
|
|
|
*/
|
2006-05-09 08:03:52 +00:00
|
|
|
struct ast_app *pbx_findapp(const char *app)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
|
|
|
struct ast_app *tmp;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&apps);
|
|
|
|
AST_RWLIST_TRAVERSE(&apps, tmp, list) {
|
1999-12-04 20:45:45 +00:00
|
|
|
if (!strcasecmp(tmp->name, app))
|
|
|
|
break;
|
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
static struct ast_switch *pbx_findswitch(const char *sw)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
struct ast_switch *asw;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_RDLOCK(&switches);
|
|
|
|
AST_RWLIST_TRAVERSE(&switches, asw, list) {
|
2001-09-28 21:20:52 +00:00
|
|
|
if (!strcasecmp(asw->name, sw))
|
|
|
|
break;
|
|
|
|
}
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_UNLOCK(&switches);
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
return asw;
|
|
|
|
}
|
|
|
|
|
2002-06-21 01:40:13 +00:00
|
|
|
static inline int include_valid(struct ast_include *i)
|
|
|
|
{
|
|
|
|
if (!i->hastime)
|
|
|
|
return 1;
|
|
|
|
|
2004-11-11 21:23:45 +00:00
|
|
|
return ast_check_timing(&(i->timing));
|
2002-06-21 01:40:13 +00:00
|
|
|
}
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
static void pbx_destroy(struct ast_pbx *p)
|
|
|
|
{
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(p);
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 07:45:14 +00:00
|
|
|
/*
|
|
|
|
* Special characters used in patterns:
|
|
|
|
* '_' underscore is the leading character of a pattern.
|
|
|
|
* In other position it is treated as a regular char.
|
|
|
|
* ' ' '-' space and '-' are separator and ignored.
|
|
|
|
* . one or more of any character. Only allowed at the end of
|
|
|
|
* a pattern.
|
|
|
|
* ! zero or more of anything. Also impacts the result of CANMATCH
|
|
|
|
* and MATCHMORE. Only allowed at the end of a pattern.
|
2006-05-10 20:34:18 +00:00
|
|
|
* In the core routine, ! causes a match with a return code of 2.
|
|
|
|
* In turn, depending on the search mode: (XXX check if it is implemented)
|
|
|
|
* - E_MATCH retuns 1 (does match)
|
|
|
|
* - E_MATCHMORE returns 0 (no match)
|
|
|
|
* - E_CANMATCH returns 1 (does match)
|
|
|
|
*
|
2006-05-10 07:45:14 +00:00
|
|
|
* / should not appear as it is considered the separator of the CID info.
|
|
|
|
* XXX at the moment we may stop on this char.
|
|
|
|
*
|
|
|
|
* X Z N match ranges 0-9, 1-9, 2-9 respectively.
|
|
|
|
* [ denotes the start of a set of character. Everything inside
|
|
|
|
* is considered literally. We can have ranges a-d and individual
|
|
|
|
* characters. A '[' and '-' can be considered literally if they
|
|
|
|
* are just before ']'.
|
|
|
|
* XXX currently there is no way to specify ']' in a range, nor \ is
|
|
|
|
* considered specially.
|
|
|
|
*
|
|
|
|
* When we compare a pattern with a specific extension, all characters in the extension
|
|
|
|
* itself are considered literally with the only exception of '-' which is considered
|
|
|
|
* as a separator and thus ignored.
|
|
|
|
* XXX do we want to consider space as a separator as well ?
|
|
|
|
* XXX do we want to consider the separators in non-patterns as well ?
|
|
|
|
*/
|
|
|
|
|
2006-05-09 18:34:30 +00:00
|
|
|
/*!
|
|
|
|
* \brief helper functions to sort extensions and patterns in the desired way,
|
|
|
|
* so that more specific patterns appear first.
|
|
|
|
*
|
|
|
|
* ext_cmp1 compares individual characters (or sets of), returning
|
|
|
|
* an int where bits 0-7 are the ASCII code of the first char in the set,
|
|
|
|
* while bit 8-15 are the cardinality of the set minus 1.
|
|
|
|
* This way more specific patterns (smaller cardinality) appear first.
|
|
|
|
* Wildcards have a special value, so that we can directly compare them to
|
|
|
|
* sets by subtracting the two values. In particular:
|
|
|
|
* 0x000xx one character, xx
|
|
|
|
* 0x0yyxx yy character set starting with xx
|
|
|
|
* 0x10000 '.' (one or more of anything)
|
|
|
|
* 0x20000 '!' (zero or more of anything)
|
|
|
|
* 0x30000 NUL (end of string)
|
|
|
|
* 0x40000 error in set.
|
|
|
|
* The pointer to the string is advanced according to needs.
|
|
|
|
* NOTES:
|
|
|
|
* 1. the empty set is equivalent to NUL.
|
|
|
|
* 2. given that a full set has always 0 as the first element,
|
|
|
|
* we could encode the special cases as 0xffXX where XX
|
|
|
|
* is 1, 2, 3, 4 as used above.
|
|
|
|
*/
|
|
|
|
static int ext_cmp1(const char **p)
|
|
|
|
{
|
|
|
|
uint32_t chars[8];
|
|
|
|
int c, cmin = 0xff, count = 0;
|
|
|
|
const char *end;
|
|
|
|
|
|
|
|
/* load, sign extend and advance pointer until we find
|
|
|
|
* a valid character.
|
|
|
|
*/
|
|
|
|
while ( (c = *(*p)++) && (c == ' ' || c == '-') )
|
|
|
|
; /* ignore some characters */
|
|
|
|
|
|
|
|
/* always return unless we have a set of chars */
|
|
|
|
switch (c) {
|
|
|
|
default: /* ordinary character */
|
|
|
|
return 0x0000 | (c & 0xff);
|
|
|
|
|
|
|
|
case 'N': /* 2..9 */
|
|
|
|
return 0x0700 | '2' ;
|
|
|
|
|
|
|
|
case 'X': /* 0..9 */
|
|
|
|
return 0x0900 | '0';
|
|
|
|
|
|
|
|
case 'Z': /* 1..9 */
|
|
|
|
return 0x0800 | '1';
|
|
|
|
|
|
|
|
case '.': /* wildcard */
|
|
|
|
return 0x10000;
|
|
|
|
|
|
|
|
case '!': /* earlymatch */
|
|
|
|
return 0x20000; /* less specific than NULL */
|
|
|
|
|
|
|
|
case '\0': /* empty string */
|
|
|
|
*p = NULL;
|
|
|
|
return 0x30000;
|
|
|
|
|
|
|
|
case '[': /* pattern */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* locate end of set */
|
|
|
|
end = strchr(*p, ']');
|
|
|
|
|
|
|
|
if (end == NULL) {
|
|
|
|
ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");
|
|
|
|
return 0x40000; /* XXX make this entry go last... */
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero(chars, sizeof(chars)); /* clear all chars in the set */
|
|
|
|
for (; *p < end ; (*p)++) {
|
|
|
|
unsigned char c1, c2; /* first-last char in range */
|
|
|
|
c1 = (unsigned char)((*p)[0]);
|
|
|
|
if (*p + 2 < end && (*p)[1] == '-') { /* this is a range */
|
|
|
|
c2 = (unsigned char)((*p)[2]);
|
|
|
|
*p += 2; /* skip a total of 3 chars */
|
|
|
|
} else /* individual character */
|
|
|
|
c2 = c1;
|
|
|
|
if (c1 < cmin)
|
|
|
|
cmin = c1;
|
|
|
|
for (; c1 <= c2; c1++) {
|
|
|
|
uint32_t mask = 1 << (c1 % 32);
|
|
|
|
if ( (chars[ c1 / 32 ] & mask) == 0)
|
|
|
|
count += 0x100;
|
|
|
|
chars[ c1 / 32 ] |= mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*p)++;
|
|
|
|
return count == 0 ? 0x30000 : (count | cmin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief the full routine to compare extensions in rules.
|
|
|
|
*/
|
|
|
|
static int ext_cmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
/* make sure non-patterns come first.
|
|
|
|
* If a is not a pattern, it either comes first or
|
|
|
|
* we use strcmp to compare the strings.
|
|
|
|
*/
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (a[0] != '_')
|
|
|
|
return (b[0] == '_') ? -1 : strcmp(a, b);
|
|
|
|
|
|
|
|
/* Now we know a is a pattern; if b is not, a comes first */
|
|
|
|
if (b[0] != '_')
|
|
|
|
return 1;
|
|
|
|
#if 0 /* old mode for ext matching */
|
|
|
|
return strcmp(a, b);
|
|
|
|
#endif
|
|
|
|
/* ok we need full pattern sorting routine */
|
|
|
|
while (!ret && a && b)
|
|
|
|
ret = ext_cmp1(&a) - ext_cmp1(&b);
|
|
|
|
if (ret == 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return (ret > 0) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/*!
|
|
|
|
* When looking up extensions, we can have different requests
|
|
|
|
* identified by the 'action' argument, as follows.
|
|
|
|
* Note that the coding is such that the low 4 bits are the
|
|
|
|
* third argument to extension_match_core.
|
|
|
|
*/
|
|
|
|
enum ext_match_t {
|
|
|
|
E_MATCHMORE = 0x00, /* extension can match but only with more 'digits' */
|
|
|
|
E_CANMATCH = 0x01, /* extension can match with or without more 'digits' */
|
|
|
|
E_MATCH = 0x02, /* extension is an exact match */
|
|
|
|
E_MATCH_MASK = 0x03, /* mask for the argument to extension_match_core() */
|
|
|
|
E_SPAWN = 0x12, /* want to spawn an extension. Requires exact match */
|
|
|
|
E_FINDLABEL = 0x22 /* returns the priority for a given label. Requires exact match */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal function for ast_extension_{match|close}
|
|
|
|
* return 0 on no-match, 1 on match, 2 on early match.
|
|
|
|
* mode is as follows:
|
|
|
|
* E_MATCH success only on exact match
|
|
|
|
* E_MATCHMORE success only on partial match (i.e. leftover digits in pattern)
|
|
|
|
* E_CANMATCH either of the above.
|
|
|
|
*/
|
2006-09-01 15:57:05 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
static int _extension_match_core(const char *pattern, const char *data, enum ext_match_t mode)
|
|
|
|
{
|
|
|
|
mode &= E_MATCH_MASK; /* only consider the relevant bits */
|
2006-09-01 15:57:05 +00:00
|
|
|
|
2006-09-01 16:48:06 +00:00
|
|
|
if ( (mode == E_MATCH) && (pattern[0] == '_') && (strcasecmp(pattern,data)==0) ) /* note: if this test is left out, then _x. will not match _x. !!! */
|
This change fixes bug 7820. Way back in April this bug was reintroduced, it appears, when a bunch of restructuring was done. This code was basically left out during the restructuring. In the case of the failure in 7820, it is trying to match the extension _x. with _x., and failing, because the 'x' should only match 0 thru 9. I **could** upgrade the code so that that N,Z, and X match not only their intended number ranges, but also N,Z,and X respectively. And, moreover, X could also match N and Z, and Z could also match N. I have no idea why this bug took so long to turn up. I have no idea what a more thorough treatment of the code would do to working code, either. So I left it as it ***was***.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@41283 f38db490-d61c-443f-a65b-d21fe96a405b
2006-08-29 23:08:42 +00:00
|
|
|
return 1;
|
2006-05-09 08:31:42 +00:00
|
|
|
|
|
|
|
if (pattern[0] != '_') { /* not a pattern, try exact or partial match */
|
|
|
|
int ld = strlen(data), lp = strlen(pattern);
|
|
|
|
|
|
|
|
if (lp < ld) /* pattern too short, cannot match */
|
|
|
|
return 0;
|
|
|
|
/* depending on the mode, accept full or partial match or both */
|
|
|
|
if (mode == E_MATCH)
|
|
|
|
return !strcmp(pattern, data); /* 1 on match, 0 on fail */
|
|
|
|
if (ld == 0 || !strncasecmp(pattern, data, ld)) /* partial or full match */
|
|
|
|
return (mode == E_MATCHMORE) ? lp > ld : 1; /* XXX should consider '!' and '/' ? */
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pattern++; /* skip leading _ */
|
2006-05-10 07:45:14 +00:00
|
|
|
/*
|
|
|
|
* XXX below we stop at '/' which is a separator for the CID info. However we should
|
|
|
|
* not store '/' in the pattern at all. When we insure it, we can remove the checks.
|
|
|
|
*/
|
2006-05-09 08:31:42 +00:00
|
|
|
while (*data && *pattern && *pattern != '/') {
|
|
|
|
const char *end;
|
|
|
|
|
|
|
|
if (*data == '-') { /* skip '-' in data (just a separator) */
|
|
|
|
data++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (toupper(*pattern)) {
|
|
|
|
case '[': /* a range */
|
|
|
|
end = strchr(pattern+1, ']'); /* XXX should deal with escapes ? */
|
|
|
|
if (end == NULL) {
|
|
|
|
ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");
|
|
|
|
return 0; /* unconditional failure */
|
|
|
|
}
|
|
|
|
for (pattern++; pattern != end; pattern++) {
|
|
|
|
if (pattern+2 < end && pattern[1] == '-') { /* this is a range */
|
|
|
|
if (*data >= pattern[0] && *data <= pattern[2])
|
|
|
|
break; /* match found */
|
|
|
|
else {
|
|
|
|
pattern += 2; /* skip a total of 3 chars */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (*data == pattern[0])
|
|
|
|
break; /* match found */
|
|
|
|
}
|
|
|
|
if (pattern == end)
|
|
|
|
return 0;
|
|
|
|
pattern = end; /* skip and continue */
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
if (*data < '2' || *data > '9')
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
if (*data < '0' || *data > '9')
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
if (*data < '1' || *data > '9')
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case '.': /* Must match, even with more digits */
|
|
|
|
return 1;
|
|
|
|
case '!': /* Early match */
|
|
|
|
return 2;
|
|
|
|
case ' ':
|
|
|
|
case '-': /* Ignore these in patterns */
|
|
|
|
data--; /* compensate the final data++ */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (*data != *pattern)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
data++;
|
|
|
|
pattern++;
|
|
|
|
}
|
|
|
|
if (*data) /* data longer than pattern, no match */
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* match so far, but ran off the end of the data.
|
|
|
|
* Depending on what is next, determine match or not.
|
|
|
|
*/
|
|
|
|
if (*pattern == '\0' || *pattern == '/') /* exact match */
|
|
|
|
return (mode == E_MATCHMORE) ? 0 : 1; /* this is a failure for E_MATCHMORE */
|
|
|
|
else if (*pattern == '!') /* early match */
|
|
|
|
return 2;
|
|
|
|
else /* partial match */
|
|
|
|
return (mode == E_MATCH) ? 0 : 1; /* this is a failure for E_MATCH */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper around _extension_match_core() to do performance measurement
|
|
|
|
* using the profiling code.
|
|
|
|
*/
|
|
|
|
static int extension_match_core(const char *pattern, const char *data, enum ext_match_t mode)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static int prof_id = -2; /* marker for 'unallocated' id */
|
|
|
|
if (prof_id == -2)
|
|
|
|
prof_id = ast_add_profile("ext_match", 0);
|
|
|
|
ast_mark(prof_id, 1);
|
|
|
|
i = _extension_match_core(pattern, data, mode);
|
|
|
|
ast_mark(prof_id, 0);
|
|
|
|
return i;
|
2003-02-12 13:59:15 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_extension_match(const char *pattern, const char *data)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
return extension_match_core(pattern, data, E_MATCH);
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2004-11-24 03:07:08 +00:00
|
|
|
int ast_extension_close(const char *pattern, const char *data, int needmore)
|
2000-01-07 10:54:40 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
if (needmore != E_MATCHMORE && needmore != E_CANMATCH)
|
|
|
|
ast_log(LOG_WARNING, "invalid argument %d\n", needmore);
|
|
|
|
return extension_match_core(pattern, data, needmore);
|
2000-01-07 10:54:40 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
struct ast_context *ast_context_find(const char *name)
|
2000-01-24 07:14:18 +00:00
|
|
|
{
|
2006-05-09 13:52:01 +00:00
|
|
|
struct ast_context *tmp = NULL;
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2006-05-09 13:52:01 +00:00
|
|
|
while ( (tmp = ast_walk_contexts(tmp)) ) {
|
|
|
|
if (!name || !strcasecmp(name, tmp->name))
|
|
|
|
break;
|
|
|
|
}
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2000-01-24 07:14:18 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2005-07-19 15:54:17 +00:00
|
|
|
#define STATUS_NO_CONTEXT 1
|
|
|
|
#define STATUS_NO_EXTENSION 2
|
|
|
|
#define STATUS_NO_PRIORITY 3
|
2004-10-03 04:19:59 +00:00
|
|
|
#define STATUS_NO_LABEL 4
|
2005-07-19 15:54:17 +00:00
|
|
|
#define STATUS_SUCCESS 5
|
2001-03-30 18:47:35 +00:00
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
static int matchcid(const char *cidpattern, const char *callerid)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2001-09-28 21:20:52 +00:00
|
|
|
/* If the Caller*ID pattern is empty, then we're matching NO Caller*ID, so
|
|
|
|
failing to get a number should count as a match, otherwise not */
|
|
|
|
|
2006-05-09 06:17:42 +00:00
|
|
|
if (ast_strlen_zero(callerid))
|
|
|
|
return ast_strlen_zero(cidpattern) ? 1 : 0;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2004-10-02 00:58:31 +00:00
|
|
|
return ast_extension_match(cidpattern, callerid);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 06:17:42 +00:00
|
|
|
/* request and result for pbx_find_extension */
|
|
|
|
struct pbx_find_info {
|
|
|
|
#if 0
|
|
|
|
const char *context;
|
|
|
|
const char *exten;
|
|
|
|
int priority;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *incstack[AST_PBX_MAX_STACK]; /* filled during the search */
|
|
|
|
int stacklen; /* modified during the search */
|
|
|
|
int status; /* set on return */
|
|
|
|
struct ast_switch *swo; /* set on return */
|
|
|
|
const char *data; /* set on return */
|
|
|
|
const char *foundcontext; /* set on return */
|
|
|
|
};
|
|
|
|
|
2006-05-08 20:49:30 +00:00
|
|
|
static struct ast_exten *pbx_find_extension(struct ast_channel *chan,
|
2006-05-09 08:31:42 +00:00
|
|
|
struct ast_context *bypass, struct pbx_find_info *q,
|
2006-05-08 20:49:30 +00:00
|
|
|
const char *context, const char *exten, int priority,
|
2006-05-09 08:31:42 +00:00
|
|
|
const char *label, const char *callerid, enum ext_match_t action)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
int x, res;
|
1999-12-04 20:45:45 +00:00
|
|
|
struct ast_context *tmp;
|
2001-03-30 18:47:35 +00:00
|
|
|
struct ast_exten *e, *eroot;
|
|
|
|
struct ast_include *i;
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_sw *sw;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2001-03-30 18:47:35 +00:00
|
|
|
/* Initialize status if appropriate */
|
2006-05-09 08:31:42 +00:00
|
|
|
if (q->stacklen == 0) {
|
|
|
|
q->status = STATUS_NO_CONTEXT;
|
|
|
|
q->swo = NULL;
|
|
|
|
q->data = NULL;
|
|
|
|
q->foundcontext = NULL;
|
2007-03-01 00:08:18 +00:00
|
|
|
} else if (q->stacklen >= AST_PBX_MAX_STACK) {
|
2001-03-30 18:47:35 +00:00
|
|
|
ast_log(LOG_WARNING, "Maximum PBX stack exceeded\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-01 00:08:18 +00:00
|
|
|
|
2001-03-30 18:47:35 +00:00
|
|
|
/* Check first to see if we've already been checked */
|
2006-05-09 08:31:42 +00:00
|
|
|
for (x = 0; x < q->stacklen; x++) {
|
|
|
|
if (!strcasecmp(q->incstack[x], context))
|
2001-03-30 18:47:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-01 00:08:18 +00:00
|
|
|
|
2006-05-08 20:49:30 +00:00
|
|
|
if (bypass) /* bypass means we only look there */
|
2004-10-03 16:15:44 +00:00
|
|
|
tmp = bypass;
|
2006-05-08 20:49:30 +00:00
|
|
|
else { /* look in contexts */
|
2006-05-09 13:52:01 +00:00
|
|
|
tmp = NULL;
|
|
|
|
while ((tmp = ast_walk_contexts(tmp)) ) {
|
2006-05-08 20:49:30 +00:00
|
|
|
if (!strcmp(tmp->name, context))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-01 00:08:18 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
if (q->status < STATUS_NO_EXTENSION)
|
|
|
|
q->status = STATUS_NO_EXTENSION;
|
2006-05-10 20:34:18 +00:00
|
|
|
|
|
|
|
/* scan the list trying to match extension and CID */
|
2006-05-11 08:00:35 +00:00
|
|
|
eroot = NULL;
|
|
|
|
while ( (eroot = ast_walk_context_extensions(tmp, eroot)) ) {
|
2006-05-10 20:34:18 +00:00
|
|
|
int match = extension_match_core(eroot->exten, exten, action);
|
|
|
|
/* 0 on fail, 1 on match, 2 on earlymatch */
|
|
|
|
|
2006-05-11 20:03:52 +00:00
|
|
|
if (!match || (eroot->matchcid && !matchcid(eroot->cidmatch, callerid)))
|
2006-05-11 08:00:35 +00:00
|
|
|
continue; /* keep trying */
|
|
|
|
if (match == 2 && action == E_MATCHMORE) {
|
|
|
|
/* We match an extension ending in '!'.
|
|
|
|
* The decision in this case is final and is NULL (no match).
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* found entry, now look for the right priority */
|
|
|
|
if (q->status < STATUS_NO_PRIORITY)
|
|
|
|
q->status = STATUS_NO_PRIORITY;
|
|
|
|
e = NULL;
|
|
|
|
while ( (e = ast_walk_extension_priorities(eroot, e)) ) {
|
|
|
|
/* Match label or priority */
|
|
|
|
if (action == E_FINDLABEL) {
|
|
|
|
if (q->status < STATUS_NO_LABEL)
|
|
|
|
q->status = STATUS_NO_LABEL;
|
|
|
|
if (label && e->label && !strcmp(label, e->label))
|
|
|
|
break; /* found it */
|
|
|
|
} else if (e->priority == priority) {
|
|
|
|
break; /* found it */
|
|
|
|
} /* else keep searching */
|
|
|
|
}
|
|
|
|
if (e) { /* found a valid match */
|
|
|
|
q->status = STATUS_SUCCESS;
|
|
|
|
q->foundcontext = context;
|
|
|
|
return e;
|
2006-05-08 21:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check alternative switches */
|
|
|
|
AST_LIST_TRAVERSE(&tmp->alts, sw, list) {
|
2006-05-09 08:31:42 +00:00
|
|
|
struct ast_switch *asw = pbx_findswitch(sw->name);
|
|
|
|
ast_switch_f *aswf = NULL;
|
|
|
|
char *datap;
|
|
|
|
|
|
|
|
if (!asw) {
|
2006-05-08 21:09:03 +00:00
|
|
|
ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
|
2006-05-09 08:31:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Substitute variables now */
|
|
|
|
if (sw->eval)
|
|
|
|
pbx_substitute_variables_helper(chan, sw->data, sw->tmpdata, SWITCH_DATA_LENGTH - 1);
|
|
|
|
|
|
|
|
/* equivalent of extension_match_core() at the switch level */
|
|
|
|
if (action == E_CANMATCH)
|
|
|
|
aswf = asw->canmatch;
|
|
|
|
else if (action == E_MATCHMORE)
|
|
|
|
aswf = asw->matchmore;
|
2006-05-10 20:34:18 +00:00
|
|
|
else /* action == E_MATCH */
|
2006-05-09 08:31:42 +00:00
|
|
|
aswf = asw->exists;
|
|
|
|
datap = sw->eval ? sw->tmpdata : sw->data;
|
|
|
|
res = !aswf ? 0 : aswf(chan, context, exten, priority, callerid, datap);
|
|
|
|
if (res) { /* Got a match */
|
|
|
|
q->swo = asw;
|
|
|
|
q->data = datap;
|
|
|
|
q->foundcontext = context;
|
|
|
|
/* XXX keep status = STATUS_NO_CONTEXT ? */
|
|
|
|
return NULL;
|
2006-05-08 21:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 08:31:42 +00:00
|
|
|
q->incstack[q->stacklen++] = tmp->name; /* Setup the stack */
|
2006-05-08 21:09:03 +00:00
|
|
|
/* Now try any includes we have in this context */
|
|
|
|
for (i = tmp->includes; i; i = i->next) {
|
|
|
|
if (include_valid(i)) {
|
2006-05-09 08:31:42 +00:00
|
|
|
if ((e = pbx_find_extension(chan, bypass, q, i->rname, exten, priority, label, callerid, action)))
|
2006-05-08 21:09:03 +00:00
|
|
|
return e;
|
2006-05-09 08:31:42 +00:00
|
|
|
if (q->swo)
|
2006-05-08 21:09:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-03-30 18:47:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-09 21:22:36 +00:00
|
|
|
/*! \brief extract offset:length from variable name.
|
|
|
|
* Returns 1 if there is a offset:length part, which is
|
|
|
|
* trimmed off (values go into variables)
|
|
|
|
*/
|
2005-09-29 04:39:14 +00:00
|
|
|
static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
|
|
|
|
{
|
|
|
|
int parens=0;
|
|
|
|
|
|
|
|
*offset = 0;
|
2006-08-14 02:14:14 +00:00
|
|
|
*length = INT_MAX;
|
2005-09-29 04:39:14 +00:00
|
|
|
*isfunc = 0;
|
2006-01-09 21:22:36 +00:00
|
|
|
for (; *var; var++) {
|
|
|
|
if (*var == '(') {
|
2005-09-29 04:39:14 +00:00
|
|
|
(*isfunc)++;
|
|
|
|
parens++;
|
2006-01-09 21:22:36 +00:00
|
|
|
} else if (*var == ')') {
|
2005-09-29 04:39:14 +00:00
|
|
|
parens--;
|
2006-01-09 21:22:36 +00:00
|
|
|
} else if (*var == ':' && parens == 0) {
|
|
|
|
*var++ = '\0';
|
|
|
|
sscanf(var, "%d:%d", offset, length);
|
|
|
|
return 1; /* offset:length valid */
|
2005-09-29 04:39:14 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-09 21:22:36 +00:00
|
|
|
return 0;
|
2005-09-29 04:39:14 +00:00
|
|
|
}
|
|
|
|
|
2006-01-22 02:10:19 +00:00
|
|
|
/*! \brief takes a substring. It is ok to call with value == workspace.
|
|
|
|
*
|
|
|
|
* offset < 0 means start from the end of the string and set the beginning
|
|
|
|
* to be that many characters back.
|
2006-08-14 02:14:14 +00:00
|
|
|
* length is the length of the substring. A value less than 0 means to leave
|
|
|
|
* that many off the end.
|
2006-01-22 02:10:19 +00:00
|
|
|
* Always return a copy in workspace.
|
|
|
|
*/
|
|
|
|
static char *substring(const char *value, int offset, int length, char *workspace, size_t workspace_len)
|
2005-09-29 04:39:14 +00:00
|
|
|
{
|
|
|
|
char *ret = workspace;
|
2006-01-22 02:10:19 +00:00
|
|
|
int lr; /* length of the input string after the copy */
|
2005-09-29 04:39:14 +00:00
|
|
|
|
2006-01-22 02:10:19 +00:00
|
|
|
ast_copy_string(workspace, value, workspace_len); /* always make a copy */
|
2005-09-29 04:39:14 +00:00
|
|
|
|
2006-08-14 02:14:14 +00:00
|
|
|
lr = strlen(ret); /* compute length after copy, so we never go out of the workspace */
|
|
|
|
|
2006-05-08 20:49:30 +00:00
|
|
|
/* Quick check if no need to do anything */
|
2006-08-14 02:14:14 +00:00
|
|
|
if (offset == 0 && length >= lr) /* take the whole string */
|
2006-01-22 02:10:19 +00:00
|
|
|
return ret;
|
2005-09-29 04:39:14 +00:00
|
|
|
|
2006-01-22 02:10:19 +00:00
|
|
|
if (offset < 0) { /* translate negative offset into positive ones */
|
|
|
|
offset = lr + offset;
|
|
|
|
if (offset < 0) /* If the negative offset was greater than the length of the string, just start at the beginning */
|
|
|
|
offset = 0;
|
2005-09-29 04:39:14 +00:00
|
|
|
}
|
|
|
|
|
2006-01-22 02:10:19 +00:00
|
|
|
/* too large offset result in empty string so we know what to return */
|
|
|
|
if (offset >= lr)
|
|
|
|
return ret + lr; /* the final '\0' */
|
2005-09-29 04:39:14 +00:00
|
|
|
|
2006-01-22 02:10:19 +00:00
|
|
|
ret += offset; /* move to the start position */
|
|
|
|
if (length >= 0 && length < lr - offset) /* truncate if necessary */
|
2005-09-29 04:39:14 +00:00
|
|
|
ret[length] = '\0';
|
2006-08-14 02:14:14 +00:00
|
|
|
else if (length < 0) {
|
|
|
|
if (lr > offset - length) /* After we remove from the front and from the rear, is there anything left? */
|
|
|
|
ret[lr + length - offset] = '\0';
|
|
|
|
else
|
|
|
|
ret[0] = '\0';
|
|
|
|
}
|
2005-09-29 04:39:14 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-01 19:27:22 +00:00
|
|
|
/*! \brief Support for Asterisk built-in variables in the dialplan
|
2006-11-24 14:00:19 +00:00
|
|
|
|
|
|
|
\note See also
|
|
|
|
- \ref AstVar Channel variables
|
|
|
|
- \ref AstCauses The HANGUPCAUSE variable
|
|
|
|
*/
|
2004-12-23 02:47:01 +00:00
|
|
|
void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp)
|
2003-02-21 06:00:08 +00:00
|
|
|
{
|
2006-01-09 21:22:36 +00:00
|
|
|
const char not_found = '\0';
|
2006-02-14 23:42:36 +00:00
|
|
|
char *tmpvar;
|
2006-01-09 21:22:36 +00:00
|
|
|
const char *s; /* the result */
|
|
|
|
int offset, length;
|
|
|
|
int i, need_substring;
|
|
|
|
struct varshead *places[2] = { headp, &globals }; /* list of places where we may look */
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
places[0] = &c->varshead;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Make a copy of var because parse_variable_name() modifies the string.
|
|
|
|
* Then if called directly, we might need to run substring() on the result;
|
|
|
|
* remember this for later in 'need_substring', 'offset' and 'length'
|
|
|
|
*/
|
2006-02-14 23:42:36 +00:00
|
|
|
tmpvar = ast_strdupa(var); /* parse_variable_name modifies the string */
|
2006-01-09 21:22:36 +00:00
|
|
|
need_substring = parse_variable_name(tmpvar, &offset, &length, &i /* ignored */);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look first into predefined variables, then into variable lists.
|
2006-02-15 18:39:52 +00:00
|
|
|
* Variable 's' points to the result, according to the following rules:
|
2006-01-09 21:22:36 +00:00
|
|
|
* s == ¬_found (set at the beginning) means that we did not find a
|
2006-02-15 18:39:52 +00:00
|
|
|
* matching variable and need to look into more places.
|
2006-01-09 21:22:36 +00:00
|
|
|
* If s != ¬_found, s is a valid result string as follows:
|
|
|
|
* s = NULL if the variable does not have a value;
|
2006-02-15 18:39:52 +00:00
|
|
|
* you typically do this when looking for an unset predefined variable.
|
2006-01-09 21:22:36 +00:00
|
|
|
* s = workspace if the result has been assembled there;
|
2006-02-15 18:39:52 +00:00
|
|
|
* typically done when the result is built e.g. with an snprintf(),
|
|
|
|
* so we don't need to do an additional copy.
|
2006-01-09 21:22:36 +00:00
|
|
|
* s != workspace in case we have a string, that needs to be copied
|
|
|
|
* (the ast_copy_string is done once for all at the end).
|
2006-02-15 18:39:52 +00:00
|
|
|
* Typically done when the result is already available in some string.
|
2006-01-09 21:22:36 +00:00
|
|
|
*/
|
|
|
|
s = ¬_found; /* default value */
|
|
|
|
if (c) { /* This group requires a valid channel */
|
|
|
|
/* Names with common parts are looked up a piece at a time using strncmp. */
|
|
|
|
if (!strncmp(var, "CALL", 4)) {
|
2006-01-17 23:45:05 +00:00
|
|
|
if (!strncmp(var + 4, "ING", 3)) {
|
2006-01-09 21:22:36 +00:00
|
|
|
if (!strcmp(var + 7, "PRES")) { /* CALLINGPRES */
|
|
|
|
snprintf(workspace, workspacelen, "%d", c->cid.cid_pres);
|
|
|
|
s = workspace;
|
|
|
|
} else if (!strcmp(var + 7, "ANI2")) { /* CALLINGANI2 */
|
|
|
|
snprintf(workspace, workspacelen, "%d", c->cid.cid_ani2);
|
|
|
|
s = workspace;
|
|
|
|
} else if (!strcmp(var + 7, "TON")) { /* CALLINGTON */
|
|
|
|
snprintf(workspace, workspacelen, "%d", c->cid.cid_ton);
|
|
|
|
s = workspace;
|
|
|
|
} else if (!strcmp(var + 7, "TNS")) { /* CALLINGTNS */
|
|
|
|
snprintf(workspace, workspacelen, "%d", c->cid.cid_tns);
|
|
|
|
s = workspace;
|
2003-04-18 15:34:25 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-09 21:22:36 +00:00
|
|
|
} else if (!strcmp(var, "HINT")) {
|
|
|
|
s = ast_get_hint(workspace, workspacelen, NULL, 0, c, c->context, c->exten) ? workspace : NULL;
|
|
|
|
} else if (!strcmp(var, "HINTNAME")) {
|
|
|
|
s = ast_get_hint(NULL, 0, workspace, workspacelen, c, c->context, c->exten) ? workspace : NULL;
|
|
|
|
} else if (!strcmp(var, "EXTEN")) {
|
|
|
|
s = c->exten;
|
|
|
|
} else if (!strcmp(var, "CONTEXT")) {
|
|
|
|
s = c->context;
|
|
|
|
} else if (!strcmp(var, "PRIORITY")) {
|
|
|
|
snprintf(workspace, workspacelen, "%d", c->priority);
|
|
|
|
s = workspace;
|
|
|
|
} else if (!strcmp(var, "CHANNEL")) {
|
|
|
|
s = c->name;
|
|
|
|
} else if (!strcmp(var, "UNIQUEID")) {
|
|
|
|
s = c->uniqueid;
|
|
|
|
} else if (!strcmp(var, "HANGUPCAUSE")) {
|
|
|
|
snprintf(workspace, workspacelen, "%d", c->hangupcause);
|
|
|
|
s = workspace;
|
2003-03-18 06:00:18 +00:00
|
|
|
}
|
2006-01-09 21:22:36 +00:00
|
|
|
}
|
|
|
|
if (s == ¬_found) { /* look for more */
|
|
|
|
if (!strcmp(var, "EPOCH")) {
|
|
|
|
snprintf(workspace, workspacelen, "%u",(int)time(NULL));
|
|
|
|
s = workspace;
|
2006-02-14 23:42:36 +00:00
|
|
|
} else if (!strcmp(var, "SYSTEMNAME")) {
|
2006-02-15 18:39:52 +00:00
|
|
|
s = ast_config_AST_SYSTEM_NAME;
|
2006-01-09 21:22:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if not found, look into chanvars or global vars */
|
|
|
|
for (i = 0; s == ¬_found && i < (sizeof(places) / sizeof(places[0])); i++) {
|
|
|
|
struct ast_var_t *variables;
|
|
|
|
if (!places[i])
|
|
|
|
continue;
|
2006-03-12 19:28:57 +00:00
|
|
|
if (places[i] == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_rdlock(&globalslock);
|
2006-01-09 21:22:36 +00:00
|
|
|
AST_LIST_TRAVERSE(places[i], variables, entries) {
|
|
|
|
if (strcasecmp(ast_var_name(variables), var)==0) {
|
|
|
|
s = ast_var_value(variables);
|
|
|
|
break;
|
2003-03-18 06:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-12 19:28:57 +00:00
|
|
|
if (places[i] == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_unlock(&globalslock);
|
2003-03-18 06:00:18 +00:00
|
|
|
}
|
2006-01-09 21:22:36 +00:00
|
|
|
if (s == ¬_found || s == NULL)
|
|
|
|
*ret = NULL;
|
|
|
|
else {
|
|
|
|
if (s != workspace)
|
|
|
|
ast_copy_string(workspace, s, workspacelen);
|
|
|
|
*ret = workspace;
|
|
|
|
if (need_substring)
|
|
|
|
*ret = substring(*ret, offset, length, workspace, workspacelen);
|
2005-12-26 16:48:12 +00:00
|
|
|
}
|
2003-02-21 06:00:08 +00:00
|
|
|
}
|
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
static int handle_show_functions(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct ast_custom_function *acf;
|
|
|
|
int count_acf = 0;
|
|
|
|
int like = 0;
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
if (argc == 5 && (!strcmp(argv[3], "like")) ) {
|
|
|
|
like = 1;
|
|
|
|
} else if (argc != 3) {
|
|
|
|
return RESULT_SHOWUSAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_cli(fd, "%s Custom Functions:\n--------------------------------------------------------------------------------\n", like ? "Matching" : "Installed");
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&acf_root);
|
|
|
|
AST_RWLIST_TRAVERSE(&acf_root, acf, acflist) {
|
2006-09-18 19:54:18 +00:00
|
|
|
if (!like || strstr(acf->name, argv[4])) {
|
|
|
|
count_acf++;
|
|
|
|
ast_cli(fd, "%-20.20s %-35.35s %s\n", acf->name, acf->syntax, acf->synopsis);
|
|
|
|
}
|
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&acf_root);
|
2006-09-18 19:54:18 +00:00
|
|
|
|
|
|
|
ast_cli(fd, "%d %scustom functions installed.\n", count_acf, like ? "matching " : "");
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_show_function(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct ast_custom_function *acf;
|
|
|
|
/* Maximum number of characters added by terminal coloring is 22 */
|
|
|
|
char infotitle[64 + AST_MAX_APP + 22], syntitle[40], destitle[40];
|
|
|
|
char info[64 + AST_MAX_APP], *synopsis = NULL, *description = NULL;
|
|
|
|
char stxtitle[40], *syntax = NULL;
|
|
|
|
int synopsis_size, description_size, syntax_size;
|
|
|
|
|
|
|
|
if (argc < 4)
|
|
|
|
return RESULT_SHOWUSAGE;
|
|
|
|
|
|
|
|
if (!(acf = ast_custom_function_find(argv[3]))) {
|
|
|
|
ast_cli(fd, "No function by that name registered.\n");
|
|
|
|
return RESULT_FAILURE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acf->synopsis)
|
|
|
|
synopsis_size = strlen(acf->synopsis) + 23;
|
|
|
|
else
|
|
|
|
synopsis_size = strlen("Not available") + 23;
|
|
|
|
synopsis = alloca(synopsis_size);
|
|
|
|
|
|
|
|
if (acf->desc)
|
|
|
|
description_size = strlen(acf->desc) + 23;
|
|
|
|
else
|
|
|
|
description_size = strlen("Not available") + 23;
|
|
|
|
description = alloca(description_size);
|
|
|
|
|
|
|
|
if (acf->syntax)
|
|
|
|
syntax_size = strlen(acf->syntax) + 23;
|
|
|
|
else
|
|
|
|
syntax_size = strlen("Not available") + 23;
|
|
|
|
syntax = alloca(syntax_size);
|
|
|
|
|
|
|
|
snprintf(info, 64 + AST_MAX_APP, "\n -= Info about function '%s' =- \n\n", acf->name);
|
|
|
|
term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
|
|
|
|
term_color(stxtitle, "[Syntax]\n", COLOR_MAGENTA, 0, 40);
|
|
|
|
term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
|
|
|
|
term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
|
|
|
|
term_color(syntax,
|
|
|
|
acf->syntax ? acf->syntax : "Not available",
|
|
|
|
COLOR_CYAN, 0, syntax_size);
|
|
|
|
term_color(synopsis,
|
|
|
|
acf->synopsis ? acf->synopsis : "Not available",
|
|
|
|
COLOR_CYAN, 0, synopsis_size);
|
|
|
|
term_color(description,
|
|
|
|
acf->desc ? acf->desc : "Not available",
|
|
|
|
COLOR_CYAN, 0, description_size);
|
|
|
|
|
|
|
|
ast_cli(fd,"%s%s%s\n\n%s%s\n\n%s%s\n", infotitle, stxtitle, syntax, syntitle, synopsis, destitle, description);
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-01-18 22:17:31 +00:00
|
|
|
static char *complete_show_function(const char *line, const char *word, int pos, int state)
|
2005-05-05 05:39:33 +00:00
|
|
|
{
|
|
|
|
struct ast_custom_function *acf;
|
2005-12-27 20:03:07 +00:00
|
|
|
char *ret = NULL;
|
2005-05-05 05:39:33 +00:00
|
|
|
int which = 0;
|
2005-12-27 20:03:07 +00:00
|
|
|
int wordlen = strlen(word);
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-01-01 17:50:37 +00:00
|
|
|
/* case-insensitive for convenience in this 'complete' function */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&acf_root);
|
|
|
|
AST_RWLIST_TRAVERSE(&acf_root, acf, acflist) {
|
2006-05-01 15:09:51 +00:00
|
|
|
if (!strncasecmp(word, acf->name, wordlen) && ++which > state) {
|
2007-06-14 23:01:01 +00:00
|
|
|
ret = ast_strdup(acf->name);
|
2006-05-01 15:09:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&acf_root);
|
2005-12-27 20:03:07 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
return ret;
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
struct ast_custom_function *ast_custom_function_find(const char *name)
|
2005-05-05 05:39:33 +00:00
|
|
|
{
|
2006-05-01 15:09:51 +00:00
|
|
|
struct ast_custom_function *acf = NULL;
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&acf_root);
|
|
|
|
AST_RWLIST_TRAVERSE(&acf_root, acf, acflist) {
|
2006-05-01 15:09:51 +00:00
|
|
|
if (!strcmp(name, acf->name))
|
2005-03-04 06:36:18 +00:00
|
|
|
break;
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&acf_root);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-05-01 15:09:51 +00:00
|
|
|
return acf;
|
2005-03-04 06:36:18 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_custom_function_unregister(struct ast_custom_function *acf)
|
2005-03-04 06:36:18 +00:00
|
|
|
{
|
2006-05-01 15:09:51 +00:00
|
|
|
struct ast_custom_function *cur;
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2005-05-05 05:39:33 +00:00
|
|
|
if (!acf)
|
|
|
|
return -1;
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&acf_root);
|
|
|
|
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&acf_root, cur, acflist) {
|
2006-04-16 21:57:02 +00:00
|
|
|
if (cur == acf) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_REMOVE_CURRENT(&acf_root, acflist);
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Unregistered custom function %s\n", acf->name);
|
2005-05-05 05:39:33 +00:00
|
|
|
break;
|
2005-03-04 06:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_END
|
|
|
|
AST_RWLIST_UNLOCK(&acf_root);
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-05-01 15:09:51 +00:00
|
|
|
return acf ? 0 : -1;
|
2005-03-04 06:36:18 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 18:38:28 +00:00
|
|
|
int __ast_custom_function_register(struct ast_custom_function *acf, struct ast_module *mod)
|
2005-03-04 06:36:18 +00:00
|
|
|
{
|
2006-05-01 15:09:51 +00:00
|
|
|
struct ast_custom_function *cur;
|
2005-12-23 22:47:26 +00:00
|
|
|
|
2005-05-05 05:39:33 +00:00
|
|
|
if (!acf)
|
|
|
|
return -1;
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2007-07-16 18:24:29 +00:00
|
|
|
acf->mod = mod;
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&acf_root);
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&acf_root, cur, acflist) {
|
|
|
|
if (!strcmp(acf->name, cur->name)) {
|
|
|
|
ast_log(LOG_ERROR, "Function %s already registered.\n", acf->name);
|
|
|
|
AST_RWLIST_UNLOCK(&acf_root);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-05-05 05:39:33 +00:00
|
|
|
}
|
|
|
|
|
2006-05-01 15:09:51 +00:00
|
|
|
/* Store in alphabetical order */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&acf_root, cur, acflist) {
|
2006-05-01 15:09:51 +00:00
|
|
|
if (strcasecmp(acf->name, cur->name) < 0) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_INSERT_BEFORE_CURRENT(&acf_root, acf, acflist);
|
2005-12-23 22:47:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_END
|
2006-05-01 15:09:51 +00:00
|
|
|
if (!cur)
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_INSERT_TAIL(&acf_root, acf, acflist);
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&acf_root);
|
2005-05-05 05:39:33 +00:00
|
|
|
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Registered custom function %s\n", acf->name);
|
2005-05-05 05:39:33 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-03-04 06:36:18 +00:00
|
|
|
}
|
|
|
|
|
2006-05-08 22:15:31 +00:00
|
|
|
/*! \brief return a pointer to the arguments of the function,
|
2006-05-09 16:24:07 +00:00
|
|
|
* and terminates the function name with '\\0'
|
2006-05-08 22:15:31 +00:00
|
|
|
*/
|
|
|
|
static char *func_args(char *function)
|
2005-03-29 06:16:49 +00:00
|
|
|
{
|
2006-05-08 22:15:31 +00:00
|
|
|
char *args = strchr(function, '(');
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2006-05-08 22:15:31 +00:00
|
|
|
if (!args)
|
|
|
|
ast_log(LOG_WARNING, "Function doesn't contain parentheses. Assuming null argument.\n");
|
|
|
|
else {
|
|
|
|
char *p;
|
2006-02-12 04:28:58 +00:00
|
|
|
*args++ = '\0';
|
2006-05-08 22:15:31 +00:00
|
|
|
if ((p = strrchr(args, ')')) )
|
2005-07-19 15:54:17 +00:00
|
|
|
*p = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
else
|
2005-07-19 15:54:17 +00:00
|
|
|
ast_log(LOG_WARNING, "Can't find trailing parenthesis?\n");
|
|
|
|
}
|
2006-05-08 22:15:31 +00:00
|
|
|
return args;
|
|
|
|
}
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2007-01-05 23:32:42 +00:00
|
|
|
int ast_func_read(struct ast_channel *chan, const char *function, char *workspace, size_t len)
|
2006-05-08 22:15:31 +00:00
|
|
|
{
|
2007-01-05 23:32:42 +00:00
|
|
|
char *copy = ast_strdupa(function);
|
|
|
|
char *args = func_args(copy);
|
|
|
|
struct ast_custom_function *acfptr = ast_custom_function_find(copy);
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2006-05-08 22:15:31 +00:00
|
|
|
if (acfptr == NULL)
|
2007-01-21 03:25:57 +00:00
|
|
|
ast_log(LOG_ERROR, "Function %s not registered\n", copy);
|
2006-05-08 22:15:31 +00:00
|
|
|
else if (!acfptr->read)
|
2007-01-21 03:25:57 +00:00
|
|
|
ast_log(LOG_ERROR, "Function %s cannot be read\n", copy);
|
2007-07-16 18:24:29 +00:00
|
|
|
else {
|
|
|
|
int res;
|
|
|
|
struct ast_module_user *u = NULL;
|
|
|
|
if (acfptr->mod)
|
|
|
|
u = __ast_module_user_add(acfptr->mod, chan);
|
|
|
|
res = acfptr->read(chan, copy, args, workspace, len);
|
|
|
|
if (acfptr->mod && u)
|
|
|
|
__ast_module_user_remove(acfptr->mod, u);
|
|
|
|
return res;
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-03-29 06:16:49 +00:00
|
|
|
}
|
|
|
|
|
2007-01-05 23:32:42 +00:00
|
|
|
int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
|
2005-03-29 06:16:49 +00:00
|
|
|
{
|
2007-01-05 23:32:42 +00:00
|
|
|
char *copy = ast_strdupa(function);
|
|
|
|
char *args = func_args(copy);
|
|
|
|
struct ast_custom_function *acfptr = ast_custom_function_find(copy);
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2006-05-08 22:15:31 +00:00
|
|
|
if (acfptr == NULL)
|
2007-01-21 03:25:57 +00:00
|
|
|
ast_log(LOG_ERROR, "Function %s not registered\n", copy);
|
2006-05-08 22:15:31 +00:00
|
|
|
else if (!acfptr->write)
|
2007-01-21 03:25:57 +00:00
|
|
|
ast_log(LOG_ERROR, "Function %s cannot be written to\n", copy);
|
2007-07-16 18:24:29 +00:00
|
|
|
else {
|
|
|
|
int res;
|
|
|
|
struct ast_module_user *u = NULL;
|
|
|
|
if (acfptr->mod)
|
|
|
|
u = __ast_module_user_add(acfptr->mod, chan);
|
|
|
|
res = acfptr->write(chan, copy, args, value);
|
|
|
|
if (acfptr->mod && u)
|
|
|
|
__ast_module_user_remove(acfptr->mod, u);
|
|
|
|
return res;
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return -1;
|
2005-03-04 06:36:18 +00:00
|
|
|
}
|
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
static void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count)
|
2003-02-21 06:00:08 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
/* Substitutes variables into cp2, based on string cp1, and assuming cp2 to be
|
|
|
|
zero-filled */
|
2003-04-18 15:34:25 +00:00
|
|
|
char *cp4;
|
|
|
|
const char *tmp, *whereweare;
|
2005-09-29 04:39:14 +00:00
|
|
|
int length, offset, offset2, isfunction;
|
2005-09-26 19:31:03 +00:00
|
|
|
char *workspace = NULL;
|
|
|
|
char *ltmp = NULL, *var = NULL;
|
2005-03-29 06:16:49 +00:00
|
|
|
char *nextvar, *nextexp, *nextthing;
|
2003-04-18 15:34:25 +00:00
|
|
|
char *vars, *vare;
|
2005-03-29 06:16:49 +00:00
|
|
|
int pos, brackets, needsub, len;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-04-18 15:34:25 +00:00
|
|
|
whereweare=tmp=cp1;
|
2006-05-08 16:14:29 +00:00
|
|
|
while (!ast_strlen_zero(whereweare) && count) {
|
2003-04-18 15:34:25 +00:00
|
|
|
/* Assume we're copying the whole remaining string */
|
|
|
|
pos = strlen(whereweare);
|
2005-03-05 05:21:45 +00:00
|
|
|
nextvar = NULL;
|
|
|
|
nextexp = NULL;
|
|
|
|
nextthing = strchr(whereweare, '$');
|
|
|
|
if (nextthing) {
|
2007-01-23 00:11:32 +00:00
|
|
|
switch (nextthing[1]) {
|
2005-03-05 05:21:45 +00:00
|
|
|
case '{':
|
|
|
|
nextvar = nextthing;
|
2005-09-26 16:58:40 +00:00
|
|
|
pos = nextvar - whereweare;
|
2005-03-05 05:21:45 +00:00
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
nextexp = nextthing;
|
2005-09-26 16:58:40 +00:00
|
|
|
pos = nextexp - whereweare;
|
2005-03-05 05:21:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-04-18 16:57:48 +00:00
|
|
|
}
|
2005-09-26 16:58:40 +00:00
|
|
|
|
|
|
|
if (pos) {
|
|
|
|
/* Can't copy more than 'count' bytes */
|
|
|
|
if (pos > count)
|
|
|
|
pos = count;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
/* Copy that many bytes */
|
|
|
|
memcpy(cp2, whereweare, pos);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
count -= pos;
|
|
|
|
cp2 += pos;
|
|
|
|
whereweare += pos;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-04-18 16:57:48 +00:00
|
|
|
if (nextvar) {
|
2003-04-18 15:34:25 +00:00
|
|
|
/* We have a variable. Find the start and end, and determine
|
|
|
|
if we are going to have to recursively call ourselves on the
|
|
|
|
contents */
|
2003-04-18 16:57:48 +00:00
|
|
|
vars = vare = nextvar + 2;
|
2003-04-18 15:34:25 +00:00
|
|
|
brackets = 1;
|
|
|
|
needsub = 0;
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2003-04-18 15:34:25 +00:00
|
|
|
/* Find the end of it */
|
2006-04-18 23:24:02 +00:00
|
|
|
while (brackets && *vare) {
|
2003-04-18 15:34:25 +00:00
|
|
|
if ((vare[0] == '$') && (vare[1] == '{')) {
|
|
|
|
needsub++;
|
2006-04-18 23:24:02 +00:00
|
|
|
} else if (vare[0] == '{') {
|
|
|
|
brackets++;
|
2003-04-18 15:34:25 +00:00
|
|
|
} else if (vare[0] == '}') {
|
|
|
|
brackets--;
|
2003-04-18 16:57:48 +00:00
|
|
|
} else if ((vare[0] == '$') && (vare[1] == '['))
|
2003-04-18 15:34:25 +00:00
|
|
|
needsub++;
|
|
|
|
vare++;
|
2003-02-21 06:00:08 +00:00
|
|
|
}
|
2003-04-18 15:34:25 +00:00
|
|
|
if (brackets)
|
|
|
|
ast_log(LOG_NOTICE, "Error in extension logic (missing '}')\n");
|
|
|
|
len = vare - vars - 1;
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
/* Skip totally over variable string */
|
2005-07-19 15:54:17 +00:00
|
|
|
whereweare += (len + 3);
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
if (!var)
|
|
|
|
var = alloca(VAR_BUF_SIZE);
|
|
|
|
|
2003-04-18 15:34:25 +00:00
|
|
|
/* Store variable name (and truncate) */
|
2005-09-26 16:58:40 +00:00
|
|
|
ast_copy_string(var, vars, len + 1);
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2003-04-18 15:34:25 +00:00
|
|
|
/* Substitute if necessary */
|
|
|
|
if (needsub) {
|
2005-09-26 16:58:40 +00:00
|
|
|
if (!ltmp)
|
|
|
|
ltmp = alloca(VAR_BUF_SIZE);
|
|
|
|
|
|
|
|
memset(ltmp, 0, VAR_BUF_SIZE);
|
|
|
|
pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1);
|
2003-04-18 15:34:25 +00:00
|
|
|
vars = ltmp;
|
|
|
|
} else {
|
|
|
|
vars = var;
|
2003-02-23 06:00:11 +00:00
|
|
|
}
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
if (!workspace)
|
|
|
|
workspace = alloca(VAR_BUF_SIZE);
|
|
|
|
|
2004-07-14 07:44:19 +00:00
|
|
|
workspace[0] = '\0';
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2005-11-09 01:51:46 +00:00
|
|
|
parse_variable_name(vars, &offset, &offset2, &isfunction);
|
2005-09-29 04:39:14 +00:00
|
|
|
if (isfunction) {
|
2005-03-29 06:16:49 +00:00
|
|
|
/* Evaluate function */
|
2007-04-27 16:15:47 +00:00
|
|
|
if (c || !headp)
|
2007-04-23 18:19:42 +00:00
|
|
|
cp4 = ast_func_read(c, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace;
|
|
|
|
else {
|
|
|
|
struct varshead old;
|
|
|
|
struct ast_channel *c = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/%p", vars);
|
|
|
|
if (c) {
|
|
|
|
memcpy(&old, &c->varshead, sizeof(old));
|
|
|
|
memcpy(&c->varshead, headp, sizeof(c->varshead));
|
|
|
|
cp4 = ast_func_read(c, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace;
|
|
|
|
/* Don't deallocate the varshead that was passed in */
|
|
|
|
memcpy(&c->varshead, &old, sizeof(c->varshead));
|
|
|
|
ast_channel_free(c);
|
|
|
|
} else
|
|
|
|
ast_log(LOG_ERROR, "Unable to allocate bogus channel for variable substitution. Function results may be blank.\n");
|
|
|
|
}
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Function result is '%s'\n", cp4 ? cp4 : "(null)");
|
2005-03-29 06:16:49 +00:00
|
|
|
} else {
|
|
|
|
/* Retrieve variable value */
|
2005-09-26 16:58:40 +00:00
|
|
|
pbx_retrieve_variable(c, vars, &cp4, workspace, VAR_BUF_SIZE, headp);
|
2005-03-29 06:16:49 +00:00
|
|
|
}
|
2003-04-18 15:34:25 +00:00
|
|
|
if (cp4) {
|
2005-09-29 04:39:14 +00:00
|
|
|
cp4 = substring(cp4, offset, offset2, workspace, VAR_BUF_SIZE);
|
|
|
|
|
2003-04-18 15:34:25 +00:00
|
|
|
length = strlen(cp4);
|
|
|
|
if (length > count)
|
|
|
|
length = count;
|
|
|
|
memcpy(cp2, cp4, length);
|
|
|
|
count -= length;
|
|
|
|
cp2 += length;
|
2003-02-21 06:00:08 +00:00
|
|
|
}
|
2003-04-18 16:57:48 +00:00
|
|
|
} else if (nextexp) {
|
|
|
|
/* We have an expression. Find the start and end, and determine
|
|
|
|
if we are going to have to recursively call ourselves on the
|
|
|
|
contents */
|
|
|
|
vars = vare = nextexp + 2;
|
|
|
|
brackets = 1;
|
|
|
|
needsub = 0;
|
2005-03-04 06:36:18 +00:00
|
|
|
|
2003-04-18 16:57:48 +00:00
|
|
|
/* Find the end of it */
|
2007-01-23 00:11:32 +00:00
|
|
|
while (brackets && *vare) {
|
2003-04-18 16:57:48 +00:00
|
|
|
if ((vare[0] == '$') && (vare[1] == '[')) {
|
|
|
|
needsub++;
|
|
|
|
brackets++;
|
2004-06-25 18:23:26 +00:00
|
|
|
vare++;
|
|
|
|
} else if (vare[0] == '[') {
|
|
|
|
brackets++;
|
2003-04-18 16:57:48 +00:00
|
|
|
} else if (vare[0] == ']') {
|
|
|
|
brackets--;
|
2004-06-25 18:23:26 +00:00
|
|
|
} else if ((vare[0] == '$') && (vare[1] == '{')) {
|
2003-04-18 16:57:48 +00:00
|
|
|
needsub++;
|
2004-06-25 19:33:27 +00:00
|
|
|
vare++;
|
2004-06-25 18:23:26 +00:00
|
|
|
}
|
2003-04-18 16:57:48 +00:00
|
|
|
vare++;
|
|
|
|
}
|
|
|
|
if (brackets)
|
|
|
|
ast_log(LOG_NOTICE, "Error in extension logic (missing ']')\n");
|
|
|
|
len = vare - vars - 1;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
/* Skip totally over expression */
|
|
|
|
whereweare += (len + 3);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-09-26 16:58:40 +00:00
|
|
|
if (!var)
|
|
|
|
var = alloca(VAR_BUF_SIZE);
|
|
|
|
|
2003-04-18 16:57:48 +00:00
|
|
|
/* Store variable name (and truncate) */
|
2005-09-26 16:58:40 +00:00
|
|
|
ast_copy_string(var, vars, len + 1);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-04-18 16:57:48 +00:00
|
|
|
/* Substitute if necessary */
|
2005-03-29 06:16:49 +00:00
|
|
|
if (needsub) {
|
2005-09-26 16:58:40 +00:00
|
|
|
if (!ltmp)
|
|
|
|
ltmp = alloca(VAR_BUF_SIZE);
|
|
|
|
|
|
|
|
memset(ltmp, 0, VAR_BUF_SIZE);
|
|
|
|
pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1);
|
2003-04-18 16:57:48 +00:00
|
|
|
vars = ltmp;
|
|
|
|
} else {
|
|
|
|
vars = var;
|
|
|
|
}
|
|
|
|
|
In regards to changes for 9508, expr2 system choking on floating point numbers, I'm adding this update to round out (no pun intended) and make this FP-capable version of the Expr2 stuff interoperate better with previous integer-only usage, by providing Functions syntax, with 20 builtin functions for floating pt to integer conversions, and some general floating point math routines that might commonly be used also. Along with this, I made it so if a function was not a builtin, it will try and find it in the ast_custom_function list, and if found, execute it and collect the results. Thus, you can call system functions like CDR(), CHANNEL(), etc, from within $\[..\] exprs, without having to wrap them in $\{...\} (curly brace) notation. Did a valgrind on the standalone and made sure there's no mem leaks. Looks good. Updated the docs, too.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@73449 f38db490-d61c-443f-a65b-d21fe96a405b
2007-07-05 18:15:22 +00:00
|
|
|
length = ast_expr(vars, cp2, count, c);
|
2005-08-29 22:03:37 +00:00
|
|
|
|
|
|
|
if (length) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Expression result is '%s'\n", cp2);
|
2005-03-04 06:36:18 +00:00
|
|
|
count -= length;
|
|
|
|
cp2 += length;
|
|
|
|
}
|
2003-04-18 15:34:25 +00:00
|
|
|
} else
|
2003-02-21 06:00:08 +00:00
|
|
|
break;
|
2003-04-18 15:34:25 +00:00
|
|
|
}
|
2003-02-21 06:00:08 +00:00
|
|
|
}
|
|
|
|
|
2004-10-24 02:53:24 +00:00
|
|
|
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
|
|
|
|
{
|
2005-09-26 19:51:58 +00:00
|
|
|
pbx_substitute_variables_helper_full(c, (c) ? &c->varshead : NULL, cp1, cp2, count);
|
2004-10-24 02:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, char *cp2, int count)
|
|
|
|
{
|
2005-09-26 16:58:40 +00:00
|
|
|
pbx_substitute_variables_helper_full(NULL, headp, cp1, cp2, count);
|
2004-10-24 02:53:24 +00:00
|
|
|
}
|
|
|
|
|
2005-03-29 06:16:49 +00:00
|
|
|
static void pbx_substitute_variables(char *passdata, int datalen, struct ast_channel *c, struct ast_exten *e)
|
|
|
|
{
|
2003-04-18 16:57:48 +00:00
|
|
|
memset(passdata, 0, datalen);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-04-18 15:34:25 +00:00
|
|
|
/* No variables or expressions in e->data, so why scan it? */
|
2007-01-10 03:53:30 +00:00
|
|
|
if (e->data && !strchr(e->data, '$') && !strstr(e->data,"${") && !strstr(e->data,"$[") && !strstr(e->data,"$(")) {
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(passdata, e->data, datalen);
|
2003-04-18 15:34:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-07-17 02:25:53 +00:00
|
|
|
pbx_substitute_variables_helper(c, e->data, passdata, datalen - 1);
|
2006-05-09 08:03:52 +00:00
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/*! \brief The return value depends on the action:
|
|
|
|
*
|
|
|
|
* E_MATCH, E_CANMATCH, E_MATCHMORE require a real match,
|
|
|
|
* and return 0 on failure, -1 on match;
|
|
|
|
* E_FINDLABEL maps the label to a priority, and returns
|
|
|
|
* the priority on success, ... XXX
|
|
|
|
* E_SPAWN, spawn an application,
|
|
|
|
* and return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con,
|
|
|
|
const char *context, const char *exten, int priority,
|
|
|
|
const char *label, const char *callerid, enum ext_match_t action)
|
2001-03-30 18:47:35 +00:00
|
|
|
{
|
|
|
|
struct ast_exten *e;
|
1999-12-04 20:45:45 +00:00
|
|
|
struct ast_app *app;
|
1999-12-18 07:01:48 +00:00
|
|
|
int res;
|
2006-05-09 08:31:42 +00:00
|
|
|
struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
|
2004-06-12 14:34:15 +00:00
|
|
|
char passdata[EXT_DATA_SIZE];
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
int matching_action = (action == E_MATCH || action == E_CANMATCH || action == E_MATCHMORE);
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2006-05-09 08:31:42 +00:00
|
|
|
e = pbx_find_extension(c, con, &q, context, exten, priority, label, callerid, action);
|
2001-03-30 18:47:35 +00:00
|
|
|
if (e) {
|
2006-05-09 08:31:42 +00:00
|
|
|
if (matching_action) {
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-05-09 08:31:42 +00:00
|
|
|
return -1; /* success, we found it */
|
|
|
|
} else if (action == E_FINDLABEL) { /* map the label to a priority */
|
2004-10-03 04:19:59 +00:00
|
|
|
res = e->priority;
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-05-09 08:31:42 +00:00
|
|
|
return res; /* the priority we were looking for */
|
|
|
|
} else { /* spawn */
|
2006-09-28 22:49:55 +00:00
|
|
|
if (!e->cached_app)
|
|
|
|
e->cached_app = pbx_findapp(e->app);
|
|
|
|
app = e->cached_app;
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-05-09 08:31:42 +00:00
|
|
|
if (!app) {
|
|
|
|
ast_log(LOG_WARNING, "No application '%s' for extension (%s, %s, %d)\n", e->app, context, exten, priority);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (c->context != context)
|
|
|
|
ast_copy_string(c->context, context, sizeof(c->context));
|
|
|
|
if (c->exten != exten)
|
|
|
|
ast_copy_string(c->exten, exten, sizeof(c->exten));
|
|
|
|
c->priority = priority;
|
|
|
|
pbx_substitute_variables(passdata, sizeof(passdata), c, e);
|
|
|
|
if (option_debug) {
|
|
|
|
char atmp[80];
|
|
|
|
char atmp2[EXT_DATA_SIZE+100];
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Launching '%s'\n", app->name);
|
2006-05-09 08:31:42 +00:00
|
|
|
snprintf(atmp, sizeof(atmp), "STACK-%s-%s-%d", context, exten, priority);
|
|
|
|
snprintf(atmp2, sizeof(atmp2), "%s(\"%s\", \"%s\") %s",
|
|
|
|
app->name, c->name, passdata, "in new stack");
|
|
|
|
pbx_builtin_setvar_helper(c, atmp, atmp2);
|
|
|
|
}
|
|
|
|
if (option_verbose > 2) {
|
|
|
|
char tmp[80], tmp2[80], tmp3[EXT_DATA_SIZE];
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\") %s\n",
|
2006-06-08 05:24:50 +00:00
|
|
|
exten, context, priority,
|
2006-05-09 08:31:42 +00:00
|
|
|
term_color(tmp, app->name, COLOR_BRCYAN, 0, sizeof(tmp)),
|
|
|
|
term_color(tmp2, c->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
|
|
|
|
term_color(tmp3, passdata, COLOR_BRMAGENTA, 0, sizeof(tmp3)),
|
|
|
|
"in new stack");
|
|
|
|
}
|
|
|
|
manager_event(EVENT_FLAG_CALL, "Newexten",
|
2004-07-24 02:26:07 +00:00
|
|
|
"Channel: %s\r\n"
|
|
|
|
"Context: %s\r\n"
|
|
|
|
"Extension: %s\r\n"
|
|
|
|
"Priority: %d\r\n"
|
|
|
|
"Application: %s\r\n"
|
|
|
|
"AppData: %s\r\n"
|
|
|
|
"Uniqueid: %s\r\n",
|
2005-11-21 01:39:26 +00:00
|
|
|
c->name, c->context, c->exten, c->priority, app->name, passdata, c->uniqueid);
|
2006-05-09 08:31:42 +00:00
|
|
|
return pbx_exec(c, app, passdata); /* 0 on success, -1 on failure */
|
2001-03-30 18:47:35 +00:00
|
|
|
}
|
2006-05-09 08:31:42 +00:00
|
|
|
} else if (q.swo) { /* not found here, but in another switch */
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-05-09 08:31:42 +00:00
|
|
|
if (matching_action)
|
2004-10-03 04:19:59 +00:00
|
|
|
return -1;
|
2006-05-09 08:31:42 +00:00
|
|
|
else {
|
|
|
|
if (!q.swo->exec) {
|
|
|
|
ast_log(LOG_WARNING, "No execution engine for switch %s\n", q.swo->name);
|
2001-09-28 21:20:52 +00:00
|
|
|
res = -1;
|
|
|
|
}
|
2006-05-09 08:31:42 +00:00
|
|
|
return q.swo->exec(c, q.foundcontext ? q.foundcontext : context, exten, priority, callerid, q.data);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-05-09 08:31:42 +00:00
|
|
|
} else { /* not found anywhere, see what happened */
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-05-09 08:31:42 +00:00
|
|
|
switch (q.status) {
|
2001-03-30 18:47:35 +00:00
|
|
|
case STATUS_NO_CONTEXT:
|
2006-05-09 08:31:42 +00:00
|
|
|
if (!matching_action)
|
2001-03-30 18:47:35 +00:00
|
|
|
ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", context);
|
|
|
|
break;
|
|
|
|
case STATUS_NO_EXTENSION:
|
2006-05-09 08:31:42 +00:00
|
|
|
if (!matching_action)
|
2001-03-30 18:47:35 +00:00
|
|
|
ast_log(LOG_NOTICE, "Cannot find extension '%s' in context '%s'\n", exten, context);
|
|
|
|
break;
|
|
|
|
case STATUS_NO_PRIORITY:
|
2006-05-09 08:31:42 +00:00
|
|
|
if (!matching_action)
|
2001-03-30 18:47:35 +00:00
|
|
|
ast_log(LOG_NOTICE, "No such priority %d in extension '%s' in context '%s'\n", priority, exten, context);
|
|
|
|
break;
|
2004-10-03 04:19:59 +00:00
|
|
|
case STATUS_NO_LABEL:
|
2004-10-03 16:15:44 +00:00
|
|
|
if (context)
|
|
|
|
ast_log(LOG_NOTICE, "No such label '%s' in extension '%s' in context '%s'\n", label, exten, context);
|
2004-10-03 04:19:59 +00:00
|
|
|
break;
|
2001-03-30 18:47:35 +00:00
|
|
|
default:
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Shouldn't happen!\n");
|
2001-03-30 18:47:35 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
return (matching_action) ? 0 : -1;
|
2001-03-30 18:47:35 +00:00
|
|
|
}
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2001-03-30 18:47:35 +00:00
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_hint_extension: Find hint for given extension in context */
|
2004-10-03 04:19:59 +00:00
|
|
|
static struct ast_exten *ast_hint_extension(struct ast_channel *c, const char *context, const char *exten)
|
2003-03-30 22:55:42 +00:00
|
|
|
{
|
|
|
|
struct ast_exten *e;
|
2006-05-09 08:31:42 +00:00
|
|
|
struct pbx_find_info q = { .stacklen = 0 }; /* the rest is set in pbx_find_context */
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2006-05-09 08:31:42 +00:00
|
|
|
e = pbx_find_extension(c, NULL, &q, context, exten, PRIORITY_HINT, NULL, "", E_MATCH);
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2003-03-30 22:55:42 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_extensions_state2: Check state of extension by using hints */
|
2003-03-30 22:55:42 +00:00
|
|
|
static int ast_extension_state2(struct ast_exten *e)
|
|
|
|
{
|
2006-04-06 16:44:31 +00:00
|
|
|
char hint[AST_MAX_EXTENSION];
|
2004-07-17 02:25:53 +00:00
|
|
|
char *cur, *rest;
|
2006-07-08 02:24:07 +00:00
|
|
|
int allunavailable = 1, allbusy = 1, allfree = 1, allonhold = 1;
|
2005-08-29 23:53:29 +00:00
|
|
|
int busy = 0, inuse = 0, ring = 0;
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2005-03-17 21:05:10 +00:00
|
|
|
if (!e)
|
|
|
|
return -1;
|
|
|
|
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(hint, ast_get_extension_app(e), sizeof(hint));
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2006-04-06 16:44:31 +00:00
|
|
|
rest = hint; /* One or more devices separated with a & character */
|
|
|
|
while ( (cur = strsep(&rest, "&")) ) {
|
|
|
|
int res = ast_device_state(cur);
|
2004-07-17 02:25:53 +00:00
|
|
|
switch (res) {
|
2005-03-29 06:16:49 +00:00
|
|
|
case AST_DEVICE_NOT_INUSE:
|
2004-07-17 02:25:53 +00:00
|
|
|
allunavailable = 0;
|
|
|
|
allbusy = 0;
|
2006-07-08 02:24:07 +00:00
|
|
|
allonhold = 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
break;
|
2005-03-29 06:16:49 +00:00
|
|
|
case AST_DEVICE_INUSE:
|
2005-08-29 23:53:29 +00:00
|
|
|
inuse = 1;
|
|
|
|
allunavailable = 0;
|
|
|
|
allfree = 0;
|
2006-07-08 02:24:07 +00:00
|
|
|
allonhold = 0;
|
2005-08-29 23:53:29 +00:00
|
|
|
break;
|
|
|
|
case AST_DEVICE_RINGING:
|
|
|
|
ring = 1;
|
|
|
|
allunavailable = 0;
|
|
|
|
allfree = 0;
|
2006-07-08 02:24:07 +00:00
|
|
|
allonhold = 0;
|
2005-08-29 23:53:29 +00:00
|
|
|
break;
|
2006-05-23 04:49:31 +00:00
|
|
|
case AST_DEVICE_RINGINUSE:
|
|
|
|
inuse = 1;
|
|
|
|
ring = 1;
|
2006-07-08 02:24:07 +00:00
|
|
|
allunavailable = 0;
|
|
|
|
allfree = 0;
|
|
|
|
allonhold = 0;
|
|
|
|
break;
|
|
|
|
case AST_DEVICE_ONHOLD:
|
2006-05-23 04:49:31 +00:00
|
|
|
allunavailable = 0;
|
|
|
|
allfree = 0;
|
|
|
|
break;
|
2005-03-29 06:16:49 +00:00
|
|
|
case AST_DEVICE_BUSY:
|
2004-07-17 02:25:53 +00:00
|
|
|
allunavailable = 0;
|
|
|
|
allfree = 0;
|
2006-07-08 02:24:07 +00:00
|
|
|
allonhold = 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
busy = 1;
|
|
|
|
break;
|
2005-03-29 06:16:49 +00:00
|
|
|
case AST_DEVICE_UNAVAILABLE:
|
|
|
|
case AST_DEVICE_INVALID:
|
2004-07-17 02:25:53 +00:00
|
|
|
allbusy = 0;
|
|
|
|
allfree = 0;
|
2006-07-08 02:24:07 +00:00
|
|
|
allonhold = 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
break;
|
2005-03-29 06:16:49 +00:00
|
|
|
default:
|
2004-07-17 02:25:53 +00:00
|
|
|
allunavailable = 0;
|
|
|
|
allbusy = 0;
|
|
|
|
allfree = 0;
|
2006-07-08 02:24:07 +00:00
|
|
|
allonhold = 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
2006-04-06 16:44:31 +00:00
|
|
|
}
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2005-08-29 23:53:29 +00:00
|
|
|
if (!inuse && ring)
|
|
|
|
return AST_EXTENSION_RINGING;
|
|
|
|
if (inuse && ring)
|
|
|
|
return (AST_EXTENSION_INUSE | AST_EXTENSION_RINGING);
|
|
|
|
if (inuse)
|
|
|
|
return AST_EXTENSION_INUSE;
|
|
|
|
if (allfree)
|
2003-04-06 19:10:24 +00:00
|
|
|
return AST_EXTENSION_NOT_INUSE;
|
2006-07-08 02:24:07 +00:00
|
|
|
if (allonhold)
|
|
|
|
return AST_EXTENSION_ONHOLD;
|
2006-05-09 08:03:52 +00:00
|
|
|
if (allbusy)
|
2003-04-06 19:10:24 +00:00
|
|
|
return AST_EXTENSION_BUSY;
|
2004-07-17 02:25:53 +00:00
|
|
|
if (allunavailable)
|
2003-04-06 19:10:24 +00:00
|
|
|
return AST_EXTENSION_UNAVAILABLE;
|
2006-05-09 08:03:52 +00:00
|
|
|
if (busy)
|
2003-04-06 19:10:24 +00:00
|
|
|
return AST_EXTENSION_INUSE;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-07-17 02:25:53 +00:00
|
|
|
return AST_EXTENSION_NOT_INUSE;
|
2003-03-30 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_extension_state2str: Return extension_state as string */
|
2005-08-29 23:53:29 +00:00
|
|
|
const char *ast_extension_state2str(int extension_state)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; (i < (sizeof(extension_states) / sizeof(extension_states[0]))); i++) {
|
2006-04-06 16:44:31 +00:00
|
|
|
if (extension_states[i].extension_state == extension_state)
|
2005-08-29 23:53:29 +00:00
|
|
|
return extension_states[i].text;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
return "Unknown";
|
2005-08-29 23:53:29 +00:00
|
|
|
}
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_extension_state: Check extension state for an extension by using hint */
|
2006-01-04 21:54:09 +00:00
|
|
|
int ast_extension_state(struct ast_channel *c, const char *context, const char *exten)
|
2003-03-30 22:55:42 +00:00
|
|
|
{
|
2004-05-09 07:19:00 +00:00
|
|
|
struct ast_exten *e;
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
e = ast_hint_extension(c, context, exten); /* Do we have a hint for this extension ? */
|
|
|
|
if (!e)
|
2005-03-17 21:05:10 +00:00
|
|
|
return -1; /* No hint, return -1 */
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2005-03-17 21:05:10 +00:00
|
|
|
return ast_extension_state2(e); /* Check all devices in the hint */
|
2003-03-30 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
static void handle_statechange(const char *device)
|
2003-03-30 22:55:42 +00:00
|
|
|
{
|
2005-07-08 21:14:34 +00:00
|
|
|
struct ast_hint *hint;
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&hints);
|
2004-07-14 07:44:19 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2006-05-08 16:14:29 +00:00
|
|
|
struct ast_state_cb *cblist;
|
|
|
|
char buf[AST_MAX_EXTENSION];
|
2006-05-08 22:47:03 +00:00
|
|
|
char *parse = buf;
|
2006-05-08 16:14:29 +00:00
|
|
|
char *cur;
|
|
|
|
int state;
|
|
|
|
|
2005-07-08 21:14:34 +00:00
|
|
|
ast_copy_string(buf, ast_get_extension_app(hint->exten), sizeof(buf));
|
2006-05-08 22:47:03 +00:00
|
|
|
while ( (cur = strsep(&parse, "&")) ) {
|
|
|
|
if (!strcasecmp(cur, device))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!cur)
|
|
|
|
continue;
|
2004-07-14 07:44:19 +00:00
|
|
|
|
2006-05-08 22:47:03 +00:00
|
|
|
/* Get device state for this hint */
|
|
|
|
state = ast_extension_state2(hint->exten);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-05-08 22:47:03 +00:00
|
|
|
if ((state == -1) || (state == hint->laststate))
|
|
|
|
continue;
|
2004-07-14 07:44:19 +00:00
|
|
|
|
2006-05-08 22:47:03 +00:00
|
|
|
/* Device state changed since last check - notify the watchers */
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-05-08 22:47:03 +00:00
|
|
|
/* For general callbacks */
|
|
|
|
for (cblist = statecbs; cblist; cblist = cblist->next)
|
|
|
|
cblist->callback(hint->exten->parent->name, hint->exten->exten, state, cblist->data);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-05-08 22:47:03 +00:00
|
|
|
/* For extension callbacks */
|
|
|
|
for (cblist = hint->callbacks; cblist; cblist = cblist->next)
|
|
|
|
cblist->callback(hint->exten->parent->name, hint->exten->exten, state, cblist->data);
|
2006-05-09 07:49:17 +00:00
|
|
|
|
|
|
|
hint->laststate = state; /* record we saw the change */
|
2004-11-13 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-11-13 22:44:33 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
static int statechange_queue(const char *dev)
|
|
|
|
{
|
|
|
|
struct statechange *sc;
|
|
|
|
|
|
|
|
if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(dev) + 1)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
strcpy(sc->dev, dev);
|
|
|
|
|
|
|
|
ast_mutex_lock(&device_state.lock);
|
|
|
|
AST_LIST_INSERT_TAIL(&device_state.state_change_q, sc, entry);
|
|
|
|
ast_cond_signal(&device_state.cond);
|
|
|
|
ast_mutex_unlock(&device_state.lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *device_state_thread(void *data)
|
|
|
|
{
|
|
|
|
struct statechange *sc;
|
|
|
|
|
|
|
|
while (!device_state.stop) {
|
|
|
|
ast_mutex_lock(&device_state.lock);
|
|
|
|
while (!(sc = AST_LIST_REMOVE_HEAD(&device_state.state_change_q, entry))) {
|
|
|
|
ast_cond_wait(&device_state.cond, &device_state.lock);
|
|
|
|
/* Check to see if we were woken up to see the request to stop */
|
|
|
|
if (device_state.stop) {
|
|
|
|
ast_mutex_unlock(&device_state.lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_mutex_unlock(&device_state.lock);
|
|
|
|
|
|
|
|
handle_statechange(sc->dev);
|
|
|
|
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(sc);
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_extension_state_add: Add watcher for extension states */
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_extension_state_add(const char *context, const char *exten,
|
2003-04-06 18:19:51 +00:00
|
|
|
ast_state_cb_type callback, void *data)
|
2003-03-30 22:55:42 +00:00
|
|
|
{
|
2006-01-11 04:31:59 +00:00
|
|
|
struct ast_hint *hint;
|
2004-07-17 02:25:53 +00:00
|
|
|
struct ast_state_cb *cblist;
|
|
|
|
struct ast_exten *e;
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2005-03-17 21:05:10 +00:00
|
|
|
/* If there's no context and extension: add callback to statecbs list */
|
2004-07-17 02:25:53 +00:00
|
|
|
if (!context && !exten) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&hints);
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
for (cblist = statecbs; cblist; cblist = cblist->next) {
|
2004-07-17 02:25:53 +00:00
|
|
|
if (cblist->callback == callback) {
|
|
|
|
cblist->data = data;
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2005-05-08 16:54:53 +00:00
|
|
|
return 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-05-08 16:54:53 +00:00
|
|
|
/* Now insert the callback */
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(cblist = ast_calloc(1, sizeof(*cblist)))) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-07-17 02:25:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cblist->id = 0;
|
|
|
|
cblist->callback = callback;
|
2003-04-06 18:19:51 +00:00
|
|
|
cblist->data = data;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-03-29 06:16:49 +00:00
|
|
|
cblist->next = statecbs;
|
2004-07-17 02:25:53 +00:00
|
|
|
statecbs = cblist;
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-07-17 02:25:53 +00:00
|
|
|
return 0;
|
2005-03-29 06:16:49 +00:00
|
|
|
}
|
2004-07-17 02:25:53 +00:00
|
|
|
|
|
|
|
if (!context || !exten)
|
|
|
|
return -1;
|
|
|
|
|
2005-03-17 21:05:10 +00:00
|
|
|
/* This callback type is for only one hint, so get the hint */
|
2006-05-09 08:03:52 +00:00
|
|
|
e = ast_hint_extension(NULL, context, exten);
|
2004-07-17 02:25:53 +00:00
|
|
|
if (!e) {
|
|
|
|
return -1;
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2005-03-17 21:05:10 +00:00
|
|
|
/* Find the hint in the list of hints */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&hints);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2006-01-11 04:31:59 +00:00
|
|
|
if (hint->exten == e)
|
2006-05-09 08:03:52 +00:00
|
|
|
break;
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:31:59 +00:00
|
|
|
if (!hint) {
|
2005-03-17 21:05:10 +00:00
|
|
|
/* We have no hint, sorry */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-07-17 02:25:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-17 21:05:10 +00:00
|
|
|
/* Now insert the callback in the callback list */
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(cblist = ast_calloc(1, sizeof(*cblist)))) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-07-17 02:25:53 +00:00
|
|
|
return -1;
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
2005-03-17 21:05:10 +00:00
|
|
|
cblist->id = stateid++; /* Unique ID for this callback */
|
|
|
|
cblist->callback = callback; /* Pointer to callback routine */
|
|
|
|
cblist->data = data; /* Data for the callback */
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2006-01-11 04:31:59 +00:00
|
|
|
cblist->next = hint->callbacks;
|
|
|
|
hint->callbacks = cblist;
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-07-17 02:25:53 +00:00
|
|
|
return cblist->id;
|
2003-03-30 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_extension_state_del: Remove a watcher from the callback list */
|
2003-04-06 18:19:51 +00:00
|
|
|
int ast_extension_state_del(int id, ast_state_cb_type callback)
|
|
|
|
{
|
2006-05-08 22:53:57 +00:00
|
|
|
struct ast_state_cb **p_cur = NULL; /* address of pointer to us */
|
|
|
|
int ret = -1;
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2004-07-17 02:25:53 +00:00
|
|
|
if (!id && !callback)
|
|
|
|
return -1;
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&hints);
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2006-05-08 22:53:57 +00:00
|
|
|
if (!id) { /* id == 0 is a callback without extension */
|
|
|
|
for (p_cur = &statecbs; *p_cur; p_cur = &(*p_cur)->next) {
|
|
|
|
if ((*p_cur)->callback == callback)
|
|
|
|
break;
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
2006-05-08 22:53:57 +00:00
|
|
|
} else { /* callback with extension, find the callback based on ID */
|
|
|
|
struct ast_hint *hint;
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2006-05-08 22:53:57 +00:00
|
|
|
for (p_cur = &hint->callbacks; *p_cur; p_cur = &(*p_cur)->next) {
|
2006-05-09 07:49:17 +00:00
|
|
|
if ((*p_cur)->id == id)
|
2006-05-08 22:53:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*p_cur) /* found in the inner loop */
|
|
|
|
break;
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
2006-05-08 22:53:57 +00:00
|
|
|
if (p_cur && *p_cur) {
|
|
|
|
struct ast_state_cb *cur = *p_cur;
|
|
|
|
*p_cur = cur->next;
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(cur);
|
2006-05-08 22:53:57 +00:00
|
|
|
ret = 0;
|
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2006-05-08 22:53:57 +00:00
|
|
|
return ret;
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_add_hint: Add hint to hint list, check initial extension state */
|
2003-04-06 18:19:51 +00:00
|
|
|
static int ast_add_hint(struct ast_exten *e)
|
|
|
|
{
|
2006-01-11 04:31:59 +00:00
|
|
|
struct ast_hint *hint;
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
if (!e)
|
2004-07-17 02:25:53 +00:00
|
|
|
return -1;
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&hints);
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2004-07-17 02:25:53 +00:00
|
|
|
/* Search if hint exists, do nothing */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2006-01-11 04:31:59 +00:00
|
|
|
if (hint->exten == e) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(2, "HINTS: Not re-adding existing hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
|
2004-07-17 02:25:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2005-03-29 06:16:49 +00:00
|
|
|
}
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(2, "HINTS: Adding hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
|
2005-02-21 06:31:01 +00:00
|
|
|
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(hint = ast_calloc(1, sizeof(*hint)))) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-07-17 02:25:53 +00:00
|
|
|
return -1;
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
2005-02-21 06:31:01 +00:00
|
|
|
/* Initialize and insert new item at the top */
|
2006-01-11 04:31:59 +00:00
|
|
|
hint->exten = e;
|
|
|
|
hint->laststate = ast_extension_state2(e);
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_INSERT_HEAD(&hints, hint, list);
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2004-07-17 02:25:53 +00:00
|
|
|
return 0;
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_change_hint: Change hint for an extension */
|
2003-04-06 18:19:51 +00:00
|
|
|
static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne)
|
2006-05-09 08:03:52 +00:00
|
|
|
{
|
2006-01-11 04:31:59 +00:00
|
|
|
struct ast_hint *hint;
|
|
|
|
int res = -1;
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&hints);
|
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2006-01-11 04:31:59 +00:00
|
|
|
if (hint->exten == oe) {
|
|
|
|
hint->exten = ne;
|
|
|
|
res = 0;
|
|
|
|
break;
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2005-12-24 06:23:36 +00:00
|
|
|
|
2006-01-11 04:31:59 +00:00
|
|
|
return res;
|
2003-04-06 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_remove_hint: Remove hint from extension */
|
2003-04-06 18:19:51 +00:00
|
|
|
static int ast_remove_hint(struct ast_exten *e)
|
2003-03-30 22:55:42 +00:00
|
|
|
{
|
2004-07-17 02:25:53 +00:00
|
|
|
/* Cleanup the Notifys if hint is removed */
|
2006-01-11 04:31:59 +00:00
|
|
|
struct ast_hint *hint;
|
2004-07-17 02:25:53 +00:00
|
|
|
struct ast_state_cb *cblist, *cbprev;
|
2006-01-11 04:31:59 +00:00
|
|
|
int res = -1;
|
2003-04-06 18:19:51 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
if (!e)
|
2004-07-17 02:25:53 +00:00
|
|
|
return -1;
|
2003-03-30 22:55:42 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&hints, hint, list) {
|
2006-01-11 04:31:59 +00:00
|
|
|
if (hint->exten == e) {
|
2004-07-17 02:25:53 +00:00
|
|
|
cbprev = NULL;
|
2006-01-11 04:31:59 +00:00
|
|
|
cblist = hint->callbacks;
|
2004-07-17 02:25:53 +00:00
|
|
|
while (cblist) {
|
|
|
|
/* Notify with -1 and remove all callbacks */
|
2006-05-09 08:03:52 +00:00
|
|
|
cbprev = cblist;
|
2004-07-17 02:25:53 +00:00
|
|
|
cblist = cblist->next;
|
2006-01-11 04:31:59 +00:00
|
|
|
cbprev->callback(hint->exten->parent->name, hint->exten->exten, AST_EXTENSION_DEACTIVATED, cbprev->data);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(cbprev);
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
2006-01-11 04:31:59 +00:00
|
|
|
hint->callbacks = NULL;
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_REMOVE_CURRENT(&hints, list);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(hint);
|
2006-01-11 04:31:59 +00:00
|
|
|
res = 0;
|
2006-05-09 08:03:52 +00:00
|
|
|
break;
|
2004-07-17 02:25:53 +00:00
|
|
|
}
|
2005-02-01 01:53:25 +00:00
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_END
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-01-11 04:31:59 +00:00
|
|
|
return res;
|
2003-03-30 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \brief ast_get_hint: Get hint for channel */
|
2005-02-01 01:53:25 +00:00
|
|
|
int ast_get_hint(char *hint, int hintsize, char *name, int namesize, struct ast_channel *c, const char *context, const char *exten)
|
2003-03-30 22:55:42 +00:00
|
|
|
{
|
2006-04-06 16:44:31 +00:00
|
|
|
struct ast_exten *e = ast_hint_extension(c, context, exten);
|
2005-06-05 16:32:16 +00:00
|
|
|
|
2005-02-01 01:53:25 +00:00
|
|
|
if (e) {
|
2006-05-09 08:03:52 +00:00
|
|
|
if (hint)
|
2006-04-06 16:44:31 +00:00
|
|
|
ast_copy_string(hint, ast_get_extension_app(e), hintsize);
|
2005-02-01 01:53:25 +00:00
|
|
|
if (name) {
|
2006-04-06 16:44:31 +00:00
|
|
|
const char *tmp = ast_get_extension_app_data(e);
|
2005-02-01 01:53:25 +00:00
|
|
|
if (tmp)
|
2006-04-06 16:44:31 +00:00
|
|
|
ast_copy_string(name, tmp, namesize);
|
2005-02-01 01:53:25 +00:00
|
|
|
}
|
2005-12-24 06:23:36 +00:00
|
|
|
return -1;
|
2003-03-30 22:55:42 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
return 0;
|
2003-03-30 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_MATCH);
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_findlabel_extension(struct ast_channel *c, const char *context, const char *exten, const char *label, const char *callerid)
|
2000-01-07 10:54:40 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
return pbx_extension_helper(c, NULL, context, exten, 0, label, callerid, E_FINDLABEL);
|
2004-10-03 16:15:44 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_findlabel_extension2(struct ast_channel *c, struct ast_context *con, const char *exten, const char *label, const char *callerid)
|
2004-10-03 16:15:44 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
return pbx_extension_helper(c, con, NULL, exten, 0, label, callerid, E_FINDLABEL);
|
2000-01-07 10:54:40 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_canmatch_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
|
2003-02-02 19:04:14 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_CANMATCH);
|
2003-02-02 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_MATCHMORE);
|
2004-10-03 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_spawn_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
|
2004-10-03 04:19:59 +00:00
|
|
|
{
|
2006-05-09 08:31:42 +00:00
|
|
|
return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, E_SPAWN);
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2006-04-19 09:18:30 +00:00
|
|
|
/* helper function to set extension and priority */
|
|
|
|
static void set_ext_pri(struct ast_channel *c, const char *exten, int pri)
|
|
|
|
{
|
|
|
|
ast_copy_string(c->exten, exten, sizeof(c->exten));
|
|
|
|
c->priority = pri;
|
|
|
|
}
|
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/*!
|
|
|
|
* \brief collect digits from the channel into the buffer,
|
|
|
|
* return -1 on error, 0 on timeout or done.
|
|
|
|
*/
|
|
|
|
static int collect_digits(struct ast_channel *c, int waittime, char *buf, int buflen, int pos)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2005-06-05 15:04:43 +00:00
|
|
|
int digit;
|
2006-05-09 08:31:42 +00:00
|
|
|
|
|
|
|
buf[pos] = '\0'; /* make sure it is properly terminated */
|
|
|
|
while (ast_matchmore_extension(c, c->context, buf, 1, c->cid.cid_num)) {
|
|
|
|
/* As long as we're willing to wait, and as long as it's not defined,
|
|
|
|
keep reading digits until we can't possibly get a right answer anymore. */
|
|
|
|
digit = ast_waitfordigit(c, waittime * 1000);
|
|
|
|
if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
|
|
|
|
c->_softhangup = 0;
|
|
|
|
} else {
|
|
|
|
if (!digit) /* No entry */
|
|
|
|
break;
|
|
|
|
if (digit < 0) /* Error, maybe a hangup */
|
|
|
|
return -1;
|
|
|
|
if (pos < buflen - 1) { /* XXX maybe error otherwise ? */
|
|
|
|
buf[pos++] = digit;
|
|
|
|
buf[pos] = '\0';
|
|
|
|
}
|
|
|
|
waittime = c->pbx->dtimeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __ast_pbx_run(struct ast_channel *c)
|
|
|
|
{
|
|
|
|
int found = 0; /* set if we find at least one match */
|
|
|
|
int res = 0;
|
2005-04-29 15:04:26 +00:00
|
|
|
int autoloopflag;
|
2006-05-09 08:31:42 +00:00
|
|
|
int error = 0; /* set an error conditions */
|
2001-03-30 18:47:35 +00:00
|
|
|
|
|
|
|
/* A little initial setup here */
|
2006-05-08 22:53:57 +00:00
|
|
|
if (c->pbx) {
|
2002-06-21 01:40:13 +00:00
|
|
|
ast_log(LOG_WARNING, "%s already has PBX structure??\n", c->name);
|
2006-05-08 22:53:57 +00:00
|
|
|
/* XXX and now what ? */
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(c->pbx);
|
2006-05-08 22:53:57 +00:00
|
|
|
}
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(c->pbx = ast_calloc(1, sizeof(*c->pbx))))
|
2001-03-30 18:47:35 +00:00
|
|
|
return -1;
|
2001-11-24 15:15:01 +00:00
|
|
|
if (c->amaflags) {
|
2004-10-15 17:51:37 +00:00
|
|
|
if (!c->cdr) {
|
2001-11-24 15:15:01 +00:00
|
|
|
c->cdr = ast_cdr_alloc();
|
|
|
|
if (!c->cdr) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(c->pbx);
|
2001-11-24 15:15:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ast_cdr_init(c->cdr, c);
|
|
|
|
}
|
|
|
|
}
|
2001-03-30 18:47:35 +00:00
|
|
|
/* Set reasonable defaults */
|
|
|
|
c->pbx->rtimeout = 10;
|
|
|
|
c->pbx->dtimeout = 5;
|
|
|
|
|
2006-05-08 22:53:57 +00:00
|
|
|
autoloopflag = ast_test_flag(c, AST_FLAG_IN_AUTOLOOP); /* save value to restore at the end */
|
2005-04-29 15:04:26 +00:00
|
|
|
ast_set_flag(c, AST_FLAG_IN_AUTOLOOP);
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
/* Start by trying whatever the channel is set to */
|
2004-10-02 00:58:31 +00:00
|
|
|
if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
|
2005-04-29 15:04:26 +00:00
|
|
|
/* If not successful fall back to 's' */
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", c->name, c->context, c->exten, c->priority);
|
2006-04-19 09:18:30 +00:00
|
|
|
/* XXX the original code used the existing priority in the call to
|
|
|
|
* ast_exists_extension(), and reset it to 1 afterwards.
|
|
|
|
* I believe the correct thing is to set it to 1 immediately.
|
|
|
|
*/
|
|
|
|
set_ext_pri(c, "s", 1);
|
2004-10-02 00:58:31 +00:00
|
|
|
if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
|
2003-02-02 19:04:14 +00:00
|
|
|
/* JK02: And finally back to default if everything else failed */
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Starting %s at %s,%s,%d still failed so falling back to context 'default'\n", c->name, c->context, c->exten, c->priority);
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(c->context, "default", sizeof(c->context));
|
2003-02-02 19:04:14 +00:00
|
|
|
}
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2006-04-19 09:18:30 +00:00
|
|
|
if (c->cdr && ast_tvzero(c->cdr->start))
|
2001-11-24 15:15:01 +00:00
|
|
|
ast_cdr_start(c->cdr);
|
2005-12-24 06:23:36 +00:00
|
|
|
for (;;) {
|
2006-05-09 08:31:42 +00:00
|
|
|
char dst_exten[256]; /* buffer to accumulate digits */
|
|
|
|
int pos = 0; /* XXX should check bounds */
|
|
|
|
int digit = 0;
|
|
|
|
|
|
|
|
/* loop on priorities in this context/exten */
|
2005-12-24 06:23:36 +00:00
|
|
|
while (ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
|
2006-05-09 08:31:42 +00:00
|
|
|
found = 1;
|
2004-10-02 00:58:31 +00:00
|
|
|
if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->cid.cid_num))) {
|
1999-12-04 20:45:45 +00:00
|
|
|
/* Something bad happened, or a hangup has been requested. */
|
2006-05-09 08:31:42 +00:00
|
|
|
if (strchr("0123456789ABCDEF*#", res)) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Oooh, got something to jump out with ('%c')!\n", res);
|
2003-03-27 02:57:17 +00:00
|
|
|
pos = 0;
|
2006-05-09 08:31:42 +00:00
|
|
|
dst_exten[pos++] = digit = res;
|
|
|
|
dst_exten[pos] = '\0';
|
2003-02-02 19:04:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-05-09 08:31:42 +00:00
|
|
|
if (res == AST_PBX_KEEPALIVE) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Spawn extension (%s,%s,%d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Spawn extension (%s, %s, %d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
|
2006-05-09 08:31:42 +00:00
|
|
|
error = 1;
|
2002-09-11 17:09:48 +00:00
|
|
|
break;
|
2006-05-09 08:31:42 +00:00
|
|
|
}
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
|
2006-05-09 08:31:42 +00:00
|
|
|
if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
|
|
|
|
c->_softhangup =0;
|
|
|
|
} else if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
|
|
|
|
/* atimeout, nothing bad */
|
|
|
|
} else {
|
|
|
|
if (c->cdr)
|
2004-02-02 01:13:00 +00:00
|
|
|
ast_cdr_update(c);
|
2006-05-09 08:31:42 +00:00
|
|
|
error = 1;
|
|
|
|
break;
|
2000-01-24 07:14:18 +00:00
|
|
|
}
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2006-04-19 09:18:30 +00:00
|
|
|
if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT && ast_exists_extension(c,c->context,"T",1,c->cid.cid_num)) {
|
|
|
|
set_ext_pri(c, "T", 0); /* 0 will become 1 with the c->priority++; at the end */
|
2003-03-07 06:00:13 +00:00
|
|
|
/* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
|
|
|
|
c->whentohangup = 0;
|
2003-05-22 04:38:46 +00:00
|
|
|
c->_softhangup &= ~AST_SOFTHANGUP_TIMEOUT;
|
2003-03-07 06:00:13 +00:00
|
|
|
} else if (c->_softhangup) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Extension %s, priority %d returned normally even though call was hung up\n",
|
|
|
|
c->exten, c->priority);
|
2006-05-09 08:31:42 +00:00
|
|
|
error = 1;
|
|
|
|
break;
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
1999-12-04 20:45:45 +00:00
|
|
|
c->priority++;
|
2006-05-09 08:31:42 +00:00
|
|
|
} /* end while - from here on we can use 'break' to go out */
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* XXX we get here on non-existing extension or a keypress or hangup ? */
|
|
|
|
|
2004-10-02 00:58:31 +00:00
|
|
|
if (!ast_exists_extension(c, c->context, c->exten, 1, c->cid.cid_num)) {
|
2006-04-19 09:18:30 +00:00
|
|
|
/* If there is no match at priority 1, it is not a valid extension anymore.
|
|
|
|
* Try to continue at "i", 1 or exit if the latter does not exist.
|
|
|
|
*/
|
2004-10-02 00:58:31 +00:00
|
|
|
if (ast_exists_extension(c, c->context, "i", 1, c->cid.cid_num)) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
|
2003-06-24 22:25:01 +00:00
|
|
|
pbx_builtin_setvar_helper(c, "INVALID_EXTEN", c->exten);
|
2006-04-19 09:18:30 +00:00
|
|
|
set_ext_pri(c, "i", 1);
|
2001-04-27 15:07:56 +00:00
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Channel '%s' sent into invalid extension '%s' in context '%s', but no invalid handler\n",
|
|
|
|
c->name, c->exten, c->context);
|
2006-05-09 08:31:42 +00:00
|
|
|
error = 1; /* we know what to do with it */
|
|
|
|
break;
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
2003-03-07 06:00:13 +00:00
|
|
|
} else if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
|
|
|
|
/* If we get this far with AST_SOFTHANGUP_TIMEOUT, then we know that the "T" extension is next. */
|
|
|
|
c->_softhangup = 0;
|
2006-05-09 08:31:42 +00:00
|
|
|
} else { /* keypress received, get more digits for a full extension */
|
|
|
|
int waittime = 0;
|
2001-04-27 15:07:56 +00:00
|
|
|
if (digit)
|
|
|
|
waittime = c->pbx->dtimeout;
|
2004-10-16 19:46:02 +00:00
|
|
|
else if (!autofallthrough)
|
2001-04-27 15:07:56 +00:00
|
|
|
waittime = c->pbx->rtimeout;
|
2006-05-09 08:31:42 +00:00
|
|
|
if (!waittime) {
|
|
|
|
const char *status = pbx_builtin_getvar_helper(c, "DIALSTATUS");
|
2005-03-14 05:51:20 +00:00
|
|
|
if (!status)
|
|
|
|
status = "UNKNOWN";
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Auto fallthrough, channel '%s' status is '%s'\n", c->name, status);
|
2005-03-14 05:51:20 +00:00
|
|
|
if (!strcasecmp(status, "CONGESTION"))
|
|
|
|
res = pbx_builtin_congestion(c, "10");
|
|
|
|
else if (!strcasecmp(status, "CHANUNAVAIL"))
|
|
|
|
res = pbx_builtin_congestion(c, "10");
|
|
|
|
else if (!strcasecmp(status, "BUSY"))
|
|
|
|
res = pbx_builtin_busy(c, "10");
|
2006-05-09 08:31:42 +00:00
|
|
|
error = 1; /* XXX disable message */
|
|
|
|
break; /* exit from the 'for' loop */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collect_digits(c, waittime, dst_exten, sizeof(dst_exten), pos))
|
|
|
|
break;
|
|
|
|
if (ast_exists_extension(c, c->context, dst_exten, 1, c->cid.cid_num)) /* Prepare the next cycle */
|
|
|
|
set_ext_pri(c, dst_exten, 1);
|
|
|
|
else {
|
|
|
|
/* No such extension */
|
|
|
|
if (!ast_strlen_zero(dst_exten)) {
|
|
|
|
/* An invalid extension */
|
|
|
|
if (ast_exists_extension(c, c->context, "i", 1, c->cid.cid_num)) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Invalid extension '%s' in context '%s' on %s\n", dst_exten, c->context, c->name);
|
2006-05-09 08:31:42 +00:00
|
|
|
pbx_builtin_setvar_helper(c, "INVALID_EXTEN", dst_exten);
|
|
|
|
set_ext_pri(c, "i", 1);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Invalid extension '%s', but no rule 'i' in context '%s'\n", dst_exten, c->context);
|
|
|
|
found = 1; /* XXX disable message */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* A simple timeout */
|
|
|
|
if (ast_exists_extension(c, c->context, "t", 1, c->cid.cid_num)) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Timeout on %s\n", c->name);
|
2006-05-09 08:31:42 +00:00
|
|
|
set_ext_pri(c, "t", 1);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Timeout, but no rule 't' in context '%s'\n", c->context);
|
|
|
|
found = 1; /* XXX disable message */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c->cdr) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "CDR updated on %s\n",c->name);
|
2006-05-09 08:31:42 +00:00
|
|
|
ast_cdr_update(c);
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 08:31:42 +00:00
|
|
|
if (!found && !error)
|
1999-12-04 20:45:45 +00:00
|
|
|
ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", c->name);
|
2007-07-17 20:01:54 +00:00
|
|
|
if (res != AST_PBX_KEEPALIVE)
|
|
|
|
ast_softhangup(c, c->hangupcause ? c->hangupcause : AST_CAUSE_NORMAL_CLEARING);
|
2004-10-02 00:58:31 +00:00
|
|
|
if ((res != AST_PBX_KEEPALIVE) && ast_exists_extension(c, c->context, "h", 1, c->cid.cid_num)) {
|
2006-03-14 16:49:34 +00:00
|
|
|
if (c->cdr && ast_opt_end_cdr_before_h_exten)
|
|
|
|
ast_cdr_end(c->cdr);
|
2006-04-19 09:18:30 +00:00
|
|
|
set_ext_pri(c, "h", 1);
|
2007-01-23 00:11:32 +00:00
|
|
|
while (ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
|
2004-10-02 00:58:31 +00:00
|
|
|
if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->cid.cid_num))) {
|
2002-06-21 01:40:13 +00:00
|
|
|
/* Something bad happened, or a hangup has been requested. */
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
|
2002-06-21 01:40:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
c->priority++;
|
|
|
|
}
|
|
|
|
}
|
2005-04-29 15:04:26 +00:00
|
|
|
ast_set2_flag(c, autoloopflag, AST_FLAG_IN_AUTOLOOP);
|
2002-06-21 01:40:13 +00:00
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
pbx_destroy(c->pbx);
|
|
|
|
c->pbx = NULL;
|
2000-01-24 07:14:18 +00:00
|
|
|
if (res != AST_PBX_KEEPALIVE)
|
|
|
|
ast_hangup(c);
|
2001-03-30 18:47:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-11 19:11:32 +00:00
|
|
|
/* Returns 0 on success, non-zero if a configured limit (maxcalls, maxload, minmemfree) was reached */
|
2005-09-13 21:59:45 +00:00
|
|
|
static int increase_call_count(const struct ast_channel *c)
|
|
|
|
{
|
|
|
|
int failed = 0;
|
2005-10-26 03:58:32 +00:00
|
|
|
double curloadavg;
|
2007-04-11 21:13:44 +00:00
|
|
|
#if defined(HAVE_SYSINFO)
|
2007-04-11 19:11:32 +00:00
|
|
|
long curfreemem;
|
|
|
|
struct sysinfo sys_info;
|
2007-04-11 20:59:08 +00:00
|
|
|
#endif
|
2007-04-11 19:11:32 +00:00
|
|
|
|
2005-09-13 21:59:45 +00:00
|
|
|
ast_mutex_lock(&maxcalllock);
|
|
|
|
if (option_maxcalls) {
|
|
|
|
if (countcalls >= option_maxcalls) {
|
|
|
|
ast_log(LOG_NOTICE, "Maximum call limit of %d calls exceeded by '%s'!\n", option_maxcalls, c->name);
|
|
|
|
failed = -1;
|
|
|
|
}
|
|
|
|
}
|
2005-10-26 03:58:32 +00:00
|
|
|
if (option_maxload) {
|
|
|
|
getloadavg(&curloadavg, 1);
|
|
|
|
if (curloadavg >= option_maxload) {
|
2005-11-21 01:39:26 +00:00
|
|
|
ast_log(LOG_NOTICE, "Maximum loadavg limit of %f load exceeded by '%s' (currently %f)!\n", option_maxload, c->name, curloadavg);
|
2005-10-26 03:58:32 +00:00
|
|
|
failed = -1;
|
|
|
|
}
|
|
|
|
}
|
2007-04-11 21:13:44 +00:00
|
|
|
#if defined(HAVE_SYSINFO)
|
2007-04-11 19:11:32 +00:00
|
|
|
if (option_minmemfree) {
|
|
|
|
if (!sysinfo(&sys_info)) {
|
|
|
|
/* make sure that the free system memory is above the configured low watermark
|
|
|
|
* convert the amount of freeram from mem_units to MB */
|
|
|
|
curfreemem = sys_info.freeram / sys_info.mem_unit;
|
|
|
|
curfreemem /= 1024*1024;
|
|
|
|
if (curfreemem < option_minmemfree) {
|
|
|
|
ast_log(LOG_WARNING, "Available system memory (~%ldMB) is below the configured low watermark (%ldMB)\n", curfreemem, option_minmemfree);
|
|
|
|
failed = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-11 20:59:08 +00:00
|
|
|
#endif
|
2007-04-11 19:11:32 +00:00
|
|
|
|
2005-09-13 21:59:45 +00:00
|
|
|
if (!failed)
|
2006-05-09 08:03:52 +00:00
|
|
|
countcalls++;
|
2005-09-13 21:59:45 +00:00
|
|
|
ast_mutex_unlock(&maxcalllock);
|
|
|
|
|
|
|
|
return failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void decrease_call_count(void)
|
|
|
|
{
|
|
|
|
ast_mutex_lock(&maxcalllock);
|
|
|
|
if (countcalls > 0)
|
|
|
|
countcalls--;
|
|
|
|
ast_mutex_unlock(&maxcalllock);
|
|
|
|
}
|
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
static void destroy_exten(struct ast_exten *e)
|
|
|
|
{
|
|
|
|
if (e->priority == PRIORITY_HINT)
|
|
|
|
ast_remove_hint(e);
|
|
|
|
|
|
|
|
if (e->datad)
|
|
|
|
e->datad(e->data);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(e);
|
2006-05-09 08:31:42 +00:00
|
|
|
}
|
|
|
|
|
2001-03-30 18:47:35 +00:00
|
|
|
static void *pbx_thread(void *data)
|
|
|
|
{
|
|
|
|
/* Oh joyeous kernel, we're a new thread, with nothing to do but
|
2005-09-02 18:07:26 +00:00
|
|
|
answer this channel and get it going.
|
|
|
|
*/
|
2005-09-13 21:59:45 +00:00
|
|
|
/* NOTE:
|
|
|
|
The launcher of this function _MUST_ increment 'countcalls'
|
|
|
|
before invoking the function; it will be decremented when the
|
|
|
|
PBX has finished running on the channel
|
|
|
|
*/
|
2001-03-30 18:47:35 +00:00
|
|
|
struct ast_channel *c = data;
|
2005-09-13 21:59:45 +00:00
|
|
|
|
|
|
|
__ast_pbx_run(c);
|
|
|
|
decrease_call_count();
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
pthread_exit(NULL);
|
2005-09-13 21:59:45 +00:00
|
|
|
|
2001-03-30 18:47:35 +00:00
|
|
|
return NULL;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2005-09-13 21:59:45 +00:00
|
|
|
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
|
|
|
pthread_t t;
|
2005-09-13 21:59:45 +00:00
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
if (!c) {
|
|
|
|
ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n");
|
2005-09-13 21:59:45 +00:00
|
|
|
return AST_PBX_FAILED;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-09-13 21:59:45 +00:00
|
|
|
if (increase_call_count(c))
|
|
|
|
return AST_PBX_CALL_LIMIT;
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
/* Start a new thread, and get something handling this channel. */
|
2007-05-24 18:30:19 +00:00
|
|
|
if (ast_pthread_create_detached(&t, NULL, pbx_thread, c)) {
|
1999-12-04 20:45:45 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to create new channel thread\n");
|
2005-09-13 21:59:45 +00:00
|
|
|
return AST_PBX_FAILED;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2005-09-13 21:59:45 +00:00
|
|
|
|
|
|
|
return AST_PBX_SUCCESS;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2005-09-13 21:59:45 +00:00
|
|
|
enum ast_pbx_result ast_pbx_run(struct ast_channel *c)
|
2005-05-18 01:49:13 +00:00
|
|
|
{
|
2005-09-13 21:59:45 +00:00
|
|
|
enum ast_pbx_result res = AST_PBX_SUCCESS;
|
|
|
|
|
|
|
|
if (increase_call_count(c))
|
|
|
|
return AST_PBX_CALL_LIMIT;
|
|
|
|
|
|
|
|
res = __ast_pbx_run(c);
|
|
|
|
decrease_call_count();
|
|
|
|
|
2005-05-18 01:49:13 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_active_calls(void)
|
|
|
|
{
|
|
|
|
return countcalls;
|
|
|
|
}
|
|
|
|
|
2004-10-16 19:46:02 +00:00
|
|
|
int pbx_set_autofallthrough(int newval)
|
|
|
|
{
|
2006-04-06 16:44:31 +00:00
|
|
|
int oldval = autofallthrough;
|
|
|
|
autofallthrough = newval;
|
2004-10-16 19:46:02 +00:00
|
|
|
return oldval;
|
|
|
|
}
|
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
/* lookup for a context with a given name,
|
|
|
|
* return with conlock held if found, NULL if not found
|
|
|
|
*/
|
|
|
|
static struct ast_context *find_context_locked(const char *context)
|
|
|
|
{
|
|
|
|
struct ast_context *c = NULL;
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2006-04-19 08:52:15 +00:00
|
|
|
while ( (c = ast_walk_contexts(c)) ) {
|
|
|
|
if (!strcmp(ast_get_context_name(c), context))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
ast_unlock_contexts();
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
2004-07-17 02:25:53 +00:00
|
|
|
* This function locks contexts list by &conlist, search for the right context
|
2001-09-28 21:20:52 +00:00
|
|
|
* structure, leave context list locked and call ast_context_remove_include2
|
|
|
|
* which removes include, unlock contexts list and return ...
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_include(const char *context, const char *include, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-04-19 09:31:41 +00:00
|
|
|
int ret = -1;
|
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-19 09:31:41 +00:00
|
|
|
if (c) {
|
|
|
|
/* found, remove include from this context ... */
|
|
|
|
ret = ast_context_remove_include2(c, include, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-04-19 09:31:41 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we call this function, &conlock lock must be locked, because when
|
|
|
|
* we giving *con argument, some process can remove/change this context
|
|
|
|
* and after that there can be segfault.
|
|
|
|
*
|
|
|
|
* This function locks given context, removes include, unlock context and
|
|
|
|
* return.
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_include2(struct ast_context *con, const char *include, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
struct ast_include *i, *pi = NULL;
|
2006-05-08 21:49:22 +00:00
|
|
|
int ret = -1;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
/* find our include */
|
2006-04-14 22:48:05 +00:00
|
|
|
for (i = con->includes; i; pi = i, i = i->next) {
|
2006-05-09 08:03:52 +00:00
|
|
|
if (!strcmp(i->name, include) &&
|
2006-05-08 21:49:22 +00:00
|
|
|
(!registrar || !strcmp(i->registrar, registrar))) {
|
2001-09-28 21:20:52 +00:00
|
|
|
/* remove from list */
|
|
|
|
if (pi)
|
|
|
|
pi->next = i->next;
|
|
|
|
else
|
|
|
|
con->includes = i->next;
|
|
|
|
/* free include and return */
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(i);
|
2006-05-08 21:49:22 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \note This function locks contexts list by &conlist, search for the rigt context
|
2001-09-28 21:20:52 +00:00
|
|
|
* structure, leave context list locked and call ast_context_remove_switch2
|
|
|
|
* which removes switch, unlock contexts list and return ...
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_switch(const char *context, const char *sw, const char *data, const char *registrar)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2005-12-26 21:38:50 +00:00
|
|
|
int ret = -1; /* default error return */
|
2006-04-19 08:52:15 +00:00
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
if (c) {
|
2006-05-09 08:03:52 +00:00
|
|
|
/* remove switch from this context ... */
|
2006-04-19 08:52:15 +00:00
|
|
|
ret = ast_context_remove_switch2(c, sw, data, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2005-12-26 21:38:50 +00:00
|
|
|
return ret;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \brief This function locks given context, removes switch, unlock context and
|
|
|
|
* return.
|
|
|
|
* \note When we call this function, &conlock lock must be locked, because when
|
2001-09-28 21:20:52 +00:00
|
|
|
* we giving *con argument, some process can remove/change this context
|
|
|
|
* and after that there can be segfault.
|
|
|
|
*
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_switch2(struct ast_context *con, const char *sw, const char *data, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-05-08 11:20:19 +00:00
|
|
|
struct ast_sw *i;
|
2006-04-19 08:52:15 +00:00
|
|
|
int ret = -1;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 11:20:19 +00:00
|
|
|
/* walk switches */
|
|
|
|
AST_LIST_TRAVERSE_SAFE_BEGIN(&con->alts, i, list) {
|
2006-05-09 08:03:52 +00:00
|
|
|
if (!strcmp(i->name, sw) && !strcmp(i->data, data) &&
|
2004-08-03 06:31:20 +00:00
|
|
|
(!registrar || !strcmp(i->registrar, registrar))) {
|
2006-04-19 08:52:15 +00:00
|
|
|
/* found, remove from list */
|
2006-05-08 11:20:19 +00:00
|
|
|
AST_LIST_REMOVE_CURRENT(&con->alts, list);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(i); /* free switch and return */
|
2006-04-19 08:52:15 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-08 11:20:19 +00:00
|
|
|
AST_LIST_TRAVERSE_SAFE_END
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-11-14 19:00:38 +00:00
|
|
|
* \note This functions lock contexts list, search for the right context,
|
2001-09-28 21:20:52 +00:00
|
|
|
* call ast_context_remove_extension2, unlock contexts list and return.
|
|
|
|
* In this function we are using
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2005-12-26 21:38:50 +00:00
|
|
|
int ret = -1; /* default error return */
|
2006-04-19 08:52:15 +00:00
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
if (c) { /* ... remove extension ... */
|
|
|
|
ret = ast_context_remove_extension2(c, extension, priority, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2005-12-26 21:38:50 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \brief This functionc locks given context, search for the right extension and
|
2001-09-28 21:20:52 +00:00
|
|
|
* fires out all peer in this extensions with given priority. If priority
|
|
|
|
* is set to 0, all peers are removed. After that, unlock context and
|
|
|
|
* return.
|
2005-11-14 19:00:38 +00:00
|
|
|
* \note When do you want to call this function, make sure that &conlock is locked,
|
|
|
|
* because some process can handle with your *con context before you lock
|
|
|
|
* it.
|
|
|
|
*
|
2001-09-28 21:20:52 +00:00
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_extension2(struct ast_context *con, const char *extension, int priority, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
struct ast_exten *exten, *prev_exten = NULL;
|
2006-05-09 08:31:42 +00:00
|
|
|
struct ast_exten *peer;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/* scan the extension list to find matching extension-registrar */
|
|
|
|
for (exten = con->root; exten; prev_exten = exten, exten = exten->next) {
|
2001-09-28 21:20:52 +00:00
|
|
|
if (!strcmp(exten->exten, extension) &&
|
2006-05-09 08:31:42 +00:00
|
|
|
(!registrar || !strcmp(exten->registrar, registrar)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!exten) {
|
|
|
|
/* we can't find right extension */
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2006-05-09 08:31:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/* should we free all peers in this extension? (priority == 0)? */
|
|
|
|
if (priority == 0) {
|
|
|
|
/* remove this extension from context list */
|
|
|
|
if (prev_exten)
|
|
|
|
prev_exten->next = exten->next;
|
|
|
|
else
|
|
|
|
con->root = exten->next;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/* fire out all peers */
|
|
|
|
while ( (peer = exten) ) {
|
|
|
|
exten = peer->peer; /* prepare for next entry */
|
|
|
|
destroy_exten(peer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* scan the priority list to remove extension with exten->priority == priority */
|
|
|
|
struct ast_exten *previous_peer = NULL;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
for (peer = exten; peer; previous_peer = peer, peer = peer->peer) {
|
|
|
|
if (peer->priority == priority &&
|
|
|
|
(!registrar || !strcmp(peer->registrar, registrar) ))
|
|
|
|
break; /* found our priority */
|
|
|
|
}
|
|
|
|
if (!peer) { /* not found */
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2006-05-09 08:31:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* we are first priority extension? */
|
|
|
|
if (!previous_peer) {
|
|
|
|
/*
|
|
|
|
* We are first in the priority chain, so must update the extension chain.
|
|
|
|
* The next node is either the next priority or the next extension
|
|
|
|
*/
|
|
|
|
struct ast_exten *next_node = peer->peer ? peer->peer : peer->next;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
if (!prev_exten) /* change the root... */
|
|
|
|
con->root = next_node;
|
|
|
|
else
|
|
|
|
prev_exten->next = next_node; /* unlink */
|
|
|
|
if (peer->peer) /* XXX update the new head of the pri list */
|
|
|
|
peer->peer->next = peer->next;
|
|
|
|
} else { /* easy, we are not first priority in extension */
|
|
|
|
previous_peer->peer = peer->peer;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/* now, free whole priority extension */
|
|
|
|
destroy_exten(peer);
|
|
|
|
/* XXX should we return -1 ? */
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2006-05-09 08:31:42 +00:00
|
|
|
return 0;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-14 03:24:06 +00:00
|
|
|
/*!
|
|
|
|
* \note This function locks contexts list by &conlist, searches for the right context
|
|
|
|
* structure, and locks the macrolock mutex in that context.
|
|
|
|
* macrolock is used to limit a macro to be executed by one call at a time.
|
|
|
|
*/
|
|
|
|
int ast_context_lockmacro(const char *context)
|
|
|
|
{
|
|
|
|
struct ast_context *c = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2006-08-14 03:24:06 +00:00
|
|
|
|
|
|
|
while ((c = ast_walk_contexts(c))) {
|
|
|
|
if (!strcmp(ast_get_context_name(c), context)) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_unlock_contexts();
|
|
|
|
|
|
|
|
/* if we found context, lock macrolock */
|
|
|
|
if (ret == 0)
|
|
|
|
ret = ast_mutex_lock(&c->macrolock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \note This function locks contexts list by &conlist, searches for the right context
|
|
|
|
* structure, and unlocks the macrolock mutex in that context.
|
|
|
|
* macrolock is used to limit a macro to be executed by one call at a time.
|
|
|
|
*/
|
|
|
|
int ast_context_unlockmacro(const char *context)
|
|
|
|
{
|
|
|
|
struct ast_context *c = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2006-08-14 03:24:06 +00:00
|
|
|
|
|
|
|
while ((c = ast_walk_contexts(c))) {
|
|
|
|
if (!strcmp(ast_get_context_name(c), context)) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_unlock_contexts();
|
|
|
|
|
|
|
|
/* if we found context, unlock macrolock */
|
|
|
|
if (ret == 0)
|
|
|
|
ret = ast_mutex_unlock(&c->macrolock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! \brief Dynamically register a new dial plan application */
|
2007-07-16 14:39:29 +00:00
|
|
|
int ast_register_application2(const char *app, int (*execute)(struct ast_channel *, void *), const char *synopsis, const char *description, void *mod)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2006-05-01 15:09:51 +00:00
|
|
|
struct ast_app *tmp, *cur = NULL;
|
2002-06-21 01:40:13 +00:00
|
|
|
char tmps[80];
|
2007-06-20 23:31:08 +00:00
|
|
|
int length, res;
|
2006-05-01 03:14:21 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&apps);
|
|
|
|
AST_RWLIST_TRAVERSE(&apps, tmp, list) {
|
2007-06-20 23:31:08 +00:00
|
|
|
if (!(res = strcasecmp(app, tmp->name))) {
|
1999-12-04 20:45:45 +00:00
|
|
|
ast_log(LOG_WARNING, "Already have an application '%s'\n", app);
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
1999-12-04 20:45:45 +00:00
|
|
|
return -1;
|
2007-06-20 23:31:08 +00:00
|
|
|
} else if (res < 0)
|
|
|
|
break;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-05-01 03:14:21 +00:00
|
|
|
length = sizeof(*tmp) + strlen(app) + 1;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(tmp = ast_calloc(1, length))) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
1999-12-04 20:45:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2005-12-24 06:23:36 +00:00
|
|
|
|
|
|
|
strcpy(tmp->name, app);
|
|
|
|
tmp->execute = execute;
|
|
|
|
tmp->synopsis = synopsis;
|
|
|
|
tmp->description = description;
|
2007-07-16 14:39:29 +00:00
|
|
|
tmp->module = mod;
|
2006-05-01 03:14:21 +00:00
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
/* Store in alphabetical order */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, cur, list) {
|
2006-05-01 15:09:51 +00:00
|
|
|
if (strcasecmp(tmp->name, cur->name) < 0) {
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_INSERT_BEFORE_CURRENT(&apps, tmp, list);
|
2005-12-24 06:23:36 +00:00
|
|
|
break;
|
2006-05-01 03:14:21 +00:00
|
|
|
}
|
2005-12-24 06:23:36 +00:00
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_END
|
2006-05-01 15:09:51 +00:00
|
|
|
if (!cur)
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_INSERT_TAIL(&apps, tmp, list);
|
2006-05-01 03:14:21 +00:00
|
|
|
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Registered application '%s'\n", term_color(tmps, tmp->name, COLOR_BRCYAN, 0, sizeof(tmps)));
|
2006-05-01 03:14:21 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
2006-05-01 03:14:21 +00:00
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
/*
|
|
|
|
* Append to the list. We don't have a tail pointer because we need
|
|
|
|
* to scan the list anyways to check for duplicates during insertion.
|
|
|
|
*/
|
2001-09-28 21:20:52 +00:00
|
|
|
int ast_register_switch(struct ast_switch *sw)
|
2001-04-27 15:07:56 +00:00
|
|
|
{
|
2006-05-08 11:20:19 +00:00
|
|
|
struct ast_switch *tmp;
|
|
|
|
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_WRLOCK(&switches);
|
|
|
|
AST_RWLIST_TRAVERSE(&switches, tmp, list) {
|
2006-05-08 11:20:19 +00:00
|
|
|
if (!strcasecmp(tmp->name, sw->name)) {
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_UNLOCK(&switches);
|
2006-05-08 11:20:19 +00:00
|
|
|
ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_INSERT_TAIL(&switches, sw, list);
|
|
|
|
AST_RWLIST_UNLOCK(&switches);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
return 0;
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
void ast_unregister_switch(struct ast_switch *sw)
|
2001-04-27 15:07:56 +00:00
|
|
|
{
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_WRLOCK(&switches);
|
|
|
|
AST_RWLIST_REMOVE(&switches, sw, list);
|
|
|
|
AST_RWLIST_UNLOCK(&switches);
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
|
|
|
* Help for CLI commands ...
|
|
|
|
*/
|
|
|
|
static char show_applications_help[] =
|
2006-11-02 23:16:09 +00:00
|
|
|
"Usage: core show applications [{like|describing} <text>]\n"
|
2004-08-27 04:21:09 +00:00
|
|
|
" List applications which are currently available.\n"
|
|
|
|
" If 'like', <text> will be a substring of the app name\n"
|
|
|
|
" If 'describing', <text> will be a substring of the description\n";
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
static char show_functions_help[] =
|
2006-11-02 23:16:09 +00:00
|
|
|
"Usage: core show functions [like <text>]\n"
|
2006-09-18 19:54:18 +00:00
|
|
|
" List builtin functions, optionally only those matching a given string\n";
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
static char show_switches_help[] =
|
2006-11-02 23:16:09 +00:00
|
|
|
"Usage: core show switches\n"
|
2006-09-18 19:54:18 +00:00
|
|
|
" List registered switches\n";
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
static char show_hints_help[] =
|
2006-11-02 23:16:09 +00:00
|
|
|
"Usage: core show hints\n"
|
2006-09-18 19:54:18 +00:00
|
|
|
" List registered hints\n";
|
2005-02-21 06:31:01 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
static char show_globals_help[] =
|
2006-11-02 23:16:09 +00:00
|
|
|
"Usage: core show globals\n"
|
2006-09-18 19:54:18 +00:00
|
|
|
" List current global dialplan variables and their values\n";
|
|
|
|
|
|
|
|
static char show_application_help[] =
|
|
|
|
"Usage: core show application <application> [<application> [<application> [...]]]\n"
|
|
|
|
" Describes a particular application.\n";
|
|
|
|
|
|
|
|
static char show_function_help[] =
|
|
|
|
"Usage: core show function <function>\n"
|
|
|
|
" Describe a particular dialplan function.\n";
|
|
|
|
|
|
|
|
static char show_dialplan_help[] =
|
2006-11-02 23:16:09 +00:00
|
|
|
"Usage: core show dialplan [exten@][context]\n"
|
2006-09-18 19:54:18 +00:00
|
|
|
" Show dialplan\n";
|
2006-03-07 22:57:52 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
static char set_global_help[] =
|
2006-09-18 19:54:18 +00:00
|
|
|
"Usage: core set global <name> <value>\n"
|
2006-03-07 22:57:52 +00:00
|
|
|
" Set global dialplan variable <name> to <value>\n";
|
|
|
|
|
2005-02-21 06:31:01 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
2005-11-14 19:00:38 +00:00
|
|
|
* \brief 'show application' CLI command implementation functions ...
|
2001-09-28 21:20:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is a possibility to show informations about more than one
|
|
|
|
* application at one time. You can type 'show application Dial Echo' and
|
|
|
|
* you will see informations about these two applications ...
|
|
|
|
*/
|
2006-01-18 22:17:31 +00:00
|
|
|
static char *complete_show_application(const char *line, const char *word, int pos, int state)
|
2001-04-27 15:07:56 +00:00
|
|
|
{
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_app *a;
|
2005-12-27 19:59:09 +00:00
|
|
|
char *ret = NULL;
|
2001-04-27 15:07:56 +00:00
|
|
|
int which = 0;
|
2005-12-27 19:59:09 +00:00
|
|
|
int wordlen = strlen(word);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-01-01 17:50:37 +00:00
|
|
|
/* return the n-th [partial] matching entry */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&apps);
|
|
|
|
AST_RWLIST_TRAVERSE(&apps, a, list) {
|
2006-05-19 19:35:16 +00:00
|
|
|
if (!strncasecmp(word, a->name, wordlen) && ++which > state) {
|
2007-06-14 23:01:01 +00:00
|
|
|
ret = ast_strdup(a->name);
|
2006-05-19 19:35:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
2005-12-27 19:59:09 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
return ret;
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
static int handle_show_application(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct ast_app *a;
|
|
|
|
int app, no_registered_app = 1;
|
|
|
|
|
|
|
|
if (argc < 4)
|
|
|
|
return RESULT_SHOWUSAGE;
|
|
|
|
|
|
|
|
/* ... go through all applications ... */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&apps);
|
|
|
|
AST_RWLIST_TRAVERSE(&apps, a, list) {
|
2006-09-18 19:54:18 +00:00
|
|
|
/* ... compare this application name with all arguments given
|
|
|
|
* to 'show application' command ... */
|
|
|
|
for (app = 3; app < argc; app++) {
|
|
|
|
if (!strcasecmp(a->name, argv[app])) {
|
|
|
|
/* Maximum number of characters added by terminal coloring is 22 */
|
|
|
|
char infotitle[64 + AST_MAX_APP + 22], syntitle[40], destitle[40];
|
|
|
|
char info[64 + AST_MAX_APP], *synopsis = NULL, *description = NULL;
|
|
|
|
int synopsis_size, description_size;
|
|
|
|
|
|
|
|
no_registered_app = 0;
|
|
|
|
|
|
|
|
if (a->synopsis)
|
|
|
|
synopsis_size = strlen(a->synopsis) + 23;
|
|
|
|
else
|
|
|
|
synopsis_size = strlen("Not available") + 23;
|
|
|
|
synopsis = alloca(synopsis_size);
|
|
|
|
|
|
|
|
if (a->description)
|
|
|
|
description_size = strlen(a->description) + 23;
|
|
|
|
else
|
|
|
|
description_size = strlen("Not available") + 23;
|
|
|
|
description = alloca(description_size);
|
|
|
|
|
|
|
|
if (synopsis && description) {
|
|
|
|
snprintf(info, 64 + AST_MAX_APP, "\n -= Info about application '%s' =- \n\n", a->name);
|
|
|
|
term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
|
|
|
|
term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
|
|
|
|
term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
|
|
|
|
term_color(synopsis,
|
|
|
|
a->synopsis ? a->synopsis : "Not available",
|
|
|
|
COLOR_CYAN, 0, synopsis_size);
|
|
|
|
term_color(description,
|
|
|
|
a->description ? a->description : "Not available",
|
|
|
|
COLOR_CYAN, 0, description_size);
|
|
|
|
|
|
|
|
ast_cli(fd,"%s%s%s\n\n%s%s\n", infotitle, syntitle, synopsis, destitle, description);
|
|
|
|
} else {
|
|
|
|
/* ... one of our applications, show info ...*/
|
|
|
|
ast_cli(fd,"\n -= Info about application '%s' =- \n\n"
|
|
|
|
"[Synopsis]\n %s\n\n"
|
|
|
|
"[Description]\n%s\n",
|
|
|
|
a->name,
|
|
|
|
a->synopsis ? a->synopsis : "Not available",
|
|
|
|
a->description ? a->description : "Not available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
2006-09-18 19:54:18 +00:00
|
|
|
|
|
|
|
/* we found at least one app? no? */
|
|
|
|
if (no_registered_app) {
|
|
|
|
ast_cli(fd, "Your application(s) is (are) not registered\n");
|
|
|
|
return RESULT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-10-26 17:52:15 +00:00
|
|
|
/*! \brief handle_show_hints: CLI support for listing registered dial plan hints */
|
2005-02-21 06:31:01 +00:00
|
|
|
static int handle_show_hints(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct ast_hint *hint;
|
|
|
|
int num = 0;
|
2005-08-27 23:55:14 +00:00
|
|
|
int watchers;
|
|
|
|
struct ast_state_cb *watcher;
|
2005-02-21 06:31:01 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&hints);
|
|
|
|
if (AST_RWLIST_EMPTY(&hints)) {
|
2005-02-21 06:31:01 +00:00
|
|
|
ast_cli(fd, "There are no registered dialplan hints\n");
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2005-02-21 06:31:01 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
/* ... we have hints ... */
|
|
|
|
ast_cli(fd, "\n -= Registered Asterisk Dial Plan Hints =-\n");
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2005-08-27 23:55:14 +00:00
|
|
|
watchers = 0;
|
|
|
|
for (watcher = hint->callbacks; watcher; watcher = watcher->next)
|
|
|
|
watchers++;
|
2006-03-28 22:14:50 +00:00
|
|
|
ast_cli(fd, " %20s@%-20.20s: %-20.20s State:%-15.15s Watchers %2d\n",
|
2006-03-28 22:09:38 +00:00
|
|
|
ast_get_extension_name(hint->exten),
|
|
|
|
ast_get_context_name(ast_get_extension_context(hint->exten)),
|
|
|
|
ast_get_extension_app(hint->exten),
|
2005-11-08 01:50:35 +00:00
|
|
|
ast_extension_state2str(hint->laststate), watchers);
|
2005-02-21 06:31:01 +00:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
ast_cli(fd, "----------------\n");
|
2005-10-13 23:35:57 +00:00
|
|
|
ast_cli(fd, "- %d hints registered\n", num);
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2005-02-21 06:31:01 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-10-26 17:52:15 +00:00
|
|
|
/*! \brief handle_show_switches: CLI support for listing registered dial plan switches */
|
2001-09-28 21:20:52 +00:00
|
|
|
static int handle_show_switches(int fd, int argc, char *argv[])
|
2001-04-27 15:07:56 +00:00
|
|
|
{
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_switch *sw;
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_RDLOCK(&switches);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-12-21 01:48:35 +00:00
|
|
|
if (AST_RWLIST_EMPTY(&switches)) {
|
|
|
|
AST_RWLIST_UNLOCK(&switches);
|
2001-09-28 21:20:52 +00:00
|
|
|
ast_cli(fd, "There are no registered alternative switches\n");
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
ast_cli(fd, "\n -= Registered Asterisk Alternative Switches =-\n");
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&switches, sw, list)
|
2001-09-28 21:20:52 +00:00
|
|
|
ast_cli(fd, "%s: %s\n", sw->name, sw->description);
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2006-12-21 01:48:35 +00:00
|
|
|
AST_RWLIST_UNLOCK(&switches);
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
static int handle_show_applications(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct ast_app *a;
|
|
|
|
int like = 0, describing = 0;
|
|
|
|
int total_match = 0; /* Number of matches in like clause */
|
|
|
|
int total_apps = 0; /* Number of apps registered */
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_RDLOCK(&apps);
|
2006-09-18 19:54:18 +00:00
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
if (AST_RWLIST_EMPTY(&apps)) {
|
2006-09-18 19:54:18 +00:00
|
|
|
ast_cli(fd, "There are no registered applications\n");
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
2006-09-18 19:54:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* core list applications like <keyword> */
|
|
|
|
if ((argc == 5) && (!strcmp(argv[3], "like"))) {
|
|
|
|
like = 1;
|
|
|
|
} else if ((argc > 4) && (!strcmp(argv[3], "describing"))) {
|
|
|
|
describing = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* core list applications describing <keyword1> [<keyword2>] [...] */
|
|
|
|
if ((!like) && (!describing)) {
|
|
|
|
ast_cli(fd, " -= Registered Asterisk Applications =-\n");
|
|
|
|
} else {
|
|
|
|
ast_cli(fd, " -= Matching Asterisk Applications =-\n");
|
|
|
|
}
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&apps, a, list) {
|
2006-09-18 19:54:18 +00:00
|
|
|
int printapp = 0;
|
|
|
|
total_apps++;
|
|
|
|
if (like) {
|
|
|
|
if (strcasestr(a->name, argv[4])) {
|
|
|
|
printapp = 1;
|
|
|
|
total_match++;
|
|
|
|
}
|
|
|
|
} else if (describing) {
|
|
|
|
if (a->description) {
|
|
|
|
/* Match all words on command line */
|
|
|
|
int i;
|
|
|
|
printapp = 1;
|
|
|
|
for (i = 4; i < argc; i++) {
|
|
|
|
if (!strcasestr(a->description, argv[i])) {
|
|
|
|
printapp = 0;
|
|
|
|
} else {
|
|
|
|
total_match++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printapp = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (printapp) {
|
|
|
|
ast_cli(fd," %20s: %s\n", a->name, a->synopsis ? a->synopsis : "<Synopsis not available>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((!like) && (!describing)) {
|
|
|
|
ast_cli(fd, " -= %d Applications Registered =-\n",total_apps);
|
|
|
|
} else {
|
|
|
|
ast_cli(fd, " -= %d Applications Matching =-\n",total_match);
|
|
|
|
}
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
2006-09-18 19:54:18 +00:00
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *complete_show_applications(const char *line, const char *word, int pos, int state)
|
|
|
|
{
|
|
|
|
static char* choices[] = { "like", "describing", NULL };
|
|
|
|
|
|
|
|
return (pos != 3) ? NULL : ast_cli_complete(word, choices, state);
|
|
|
|
}
|
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
|
|
|
* 'show dialplan' CLI command implementation functions ...
|
|
|
|
*/
|
2006-01-18 22:17:31 +00:00
|
|
|
static char *complete_show_dialplan_context(const char *line, const char *word, int pos,
|
2001-09-28 21:20:52 +00:00
|
|
|
int state)
|
|
|
|
{
|
2005-12-26 21:38:50 +00:00
|
|
|
struct ast_context *c = NULL;
|
|
|
|
char *ret = NULL;
|
2001-09-28 21:20:52 +00:00
|
|
|
int which = 0;
|
2005-12-24 06:23:36 +00:00
|
|
|
int wordlen;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2003-04-28 04:18:47 +00:00
|
|
|
/* we are do completion of [exten@]context on second position only */
|
2005-12-24 06:23:36 +00:00
|
|
|
if (pos != 2)
|
|
|
|
return NULL;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
wordlen = strlen(word);
|
|
|
|
|
2006-03-29 16:51:51 +00:00
|
|
|
/* walk through all contexts and return the n-th match */
|
2005-12-26 21:38:50 +00:00
|
|
|
while ( (c = ast_walk_contexts(c)) ) {
|
2006-03-29 16:51:51 +00:00
|
|
|
if (!strncasecmp(word, ast_get_context_name(c), wordlen) && ++which > state) {
|
|
|
|
ret = ast_strdup(ast_get_context_name(c));
|
|
|
|
break;
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
ast_unlock_contexts();
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2005-12-26 21:38:50 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2006-10-30 21:18:39 +00:00
|
|
|
/*! \brief Counters for the show dialplan manager command */
|
2005-01-09 20:28:47 +00:00
|
|
|
struct dialplan_counters {
|
2006-10-30 21:18:39 +00:00
|
|
|
int total_items;
|
2005-01-09 20:28:47 +00:00
|
|
|
int total_context;
|
|
|
|
int total_exten;
|
|
|
|
int total_prio;
|
|
|
|
int context_existence;
|
|
|
|
int extension_existence;
|
|
|
|
};
|
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
/*! \brief helper function to print an extension */
|
|
|
|
static void print_ext(struct ast_exten *e, char * buf, int buflen)
|
|
|
|
{
|
|
|
|
int prio = ast_get_extension_priority(e);
|
|
|
|
if (prio == PRIORITY_HINT) {
|
|
|
|
snprintf(buf, buflen, "hint: %s",
|
|
|
|
ast_get_extension_app(e));
|
|
|
|
} else {
|
|
|
|
snprintf(buf, buflen, "%d. %s(%s)",
|
|
|
|
prio, ast_get_extension_app(e),
|
|
|
|
(char *)ast_get_extension_app_data(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX not verified */
|
2006-05-09 08:16:07 +00:00
|
|
|
static int show_dialplan_helper(int fd, const char *context, const char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, const char *includes[])
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2005-12-26 21:38:50 +00:00
|
|
|
struct ast_context *c = NULL;
|
2005-12-24 06:23:36 +00:00
|
|
|
int res = 0, old_total_exten = dpc->total_exten;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
/* walk all contexts ... */
|
2005-12-26 21:38:50 +00:00
|
|
|
while ( (c = ast_walk_contexts(c)) ) {
|
2006-05-08 21:49:22 +00:00
|
|
|
struct ast_exten *e;
|
|
|
|
struct ast_include *i;
|
|
|
|
struct ast_ignorepat *ip;
|
|
|
|
char buf[256], buf2[256];
|
|
|
|
int context_info_printed = 0;
|
|
|
|
|
|
|
|
if (context && strcmp(ast_get_context_name(c), context))
|
|
|
|
continue; /* skip this one, name doesn't match */
|
|
|
|
|
|
|
|
dpc->context_existence = 1;
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_context(c);
|
2006-05-08 21:49:22 +00:00
|
|
|
|
|
|
|
/* are we looking for exten too? if yes, we print context
|
|
|
|
* only if we find our extension.
|
|
|
|
* Otherwise print context even if empty ?
|
|
|
|
* XXX i am not sure how the rinclude is handled.
|
|
|
|
* I think it ought to go inside.
|
|
|
|
*/
|
|
|
|
if (!exten) {
|
|
|
|
dpc->total_context++;
|
|
|
|
ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
|
|
|
|
ast_get_context_name(c), ast_get_context_registrar(c));
|
|
|
|
context_info_printed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* walk extensions ... */
|
|
|
|
e = NULL;
|
|
|
|
while ( (e = ast_walk_context_extensions(c, e)) ) {
|
|
|
|
struct ast_exten *p;
|
|
|
|
|
|
|
|
if (exten && !ast_extension_match(ast_get_extension_name(e), exten))
|
|
|
|
continue; /* skip, extension match failed */
|
|
|
|
|
|
|
|
dpc->extension_existence = 1;
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
/* may we print context info? */
|
2006-05-08 21:49:22 +00:00
|
|
|
if (!context_info_printed) {
|
|
|
|
dpc->total_context++;
|
|
|
|
if (rinclude) { /* TODO Print more info about rinclude */
|
|
|
|
ast_cli(fd, "[ Included context '%s' created by '%s' ]\n",
|
|
|
|
ast_get_context_name(c), ast_get_context_registrar(c));
|
|
|
|
} else {
|
2001-09-28 21:20:52 +00:00
|
|
|
ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
|
|
|
|
ast_get_context_name(c), ast_get_context_registrar(c));
|
|
|
|
}
|
2006-05-08 21:49:22 +00:00
|
|
|
context_info_printed = 1;
|
|
|
|
}
|
|
|
|
dpc->total_prio++;
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
/* write extension name and first peer */
|
2006-11-08 18:26:52 +00:00
|
|
|
if (e->matchcid)
|
|
|
|
snprintf(buf, sizeof(buf), "'%s' (CID match '%s') => ", ast_get_extension_name(e), e->cidmatch);
|
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "'%s' =>", ast_get_extension_name(e));
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
print_ext(e, buf2, sizeof(buf2));
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
ast_cli(fd, " %-17s %-45s [%s]\n", buf, buf2,
|
|
|
|
ast_get_extension_registrar(e));
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
dpc->total_exten++;
|
|
|
|
/* walk next extension peers */
|
|
|
|
p = e; /* skip the first one, we already got it */
|
|
|
|
while ( (p = ast_walk_extension_priorities(e, p)) ) {
|
|
|
|
const char *el = ast_get_extension_label(p);
|
|
|
|
dpc->total_prio++;
|
|
|
|
if (el)
|
|
|
|
snprintf(buf, sizeof(buf), " [%s]", el);
|
|
|
|
else
|
|
|
|
buf[0] = '\0';
|
|
|
|
print_ext(p, buf2, sizeof(buf2));
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
ast_cli(fd," %-17s %-45s [%s]\n", buf, buf2,
|
|
|
|
ast_get_extension_registrar(p));
|
|
|
|
}
|
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
/* walk included and write info ... */
|
|
|
|
i = NULL;
|
|
|
|
while ( (i = ast_walk_context_includes(c, i)) ) {
|
|
|
|
snprintf(buf, sizeof(buf), "'%s'", ast_get_include_name(i));
|
|
|
|
if (exten) {
|
|
|
|
/* Check all includes for the requested extension */
|
|
|
|
if (includecount >= AST_PBX_MAX_STACK) {
|
|
|
|
ast_log(LOG_NOTICE, "Maximum include depth exceeded!\n");
|
|
|
|
} else {
|
|
|
|
int dupe=0;
|
|
|
|
int x;
|
|
|
|
for (x=0;x<includecount;x++) {
|
|
|
|
if (!strcasecmp(includes[x], ast_get_include_name(i))) {
|
|
|
|
dupe++;
|
|
|
|
break;
|
2005-09-24 14:19:00 +00:00
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-05-08 21:49:22 +00:00
|
|
|
if (!dupe) {
|
2006-05-09 08:16:07 +00:00
|
|
|
includes[includecount] = ast_get_include_name(i);
|
2006-05-08 21:49:22 +00:00
|
|
|
show_dialplan_helper(fd, ast_get_include_name(i), exten, dpc, i, includecount + 1, includes);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Avoiding circular include of %s within %s\n", ast_get_include_name(i), context);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-08 21:49:22 +00:00
|
|
|
} else {
|
|
|
|
ast_cli(fd, " Include => %-45s [%s]\n",
|
|
|
|
buf, ast_get_include_registrar(i));
|
|
|
|
}
|
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-05-08 21:49:22 +00:00
|
|
|
/* walk ignore patterns and write info ... */
|
|
|
|
ip = NULL;
|
|
|
|
while ( (ip = ast_walk_context_ignorepats(c, ip)) ) {
|
|
|
|
const char *ipname = ast_get_ignorepat_name(ip);
|
|
|
|
char ignorepat[AST_MAX_EXTENSION];
|
|
|
|
snprintf(buf, sizeof(buf), "'%s'", ipname);
|
|
|
|
snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname);
|
|
|
|
if (!exten || ast_extension_match(ignorepat, exten)) {
|
|
|
|
ast_cli(fd, " Ignore pattern => %-45s [%s]\n",
|
|
|
|
buf, ast_get_ignorepat_registrar(ip));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!rinclude) {
|
|
|
|
struct ast_sw *sw = NULL;
|
|
|
|
while ( (sw = ast_walk_context_switches(c, sw)) ) {
|
|
|
|
snprintf(buf, sizeof(buf), "'%s/%s'",
|
|
|
|
ast_get_switch_name(sw),
|
|
|
|
ast_get_switch_data(sw));
|
|
|
|
ast_cli(fd, " Alt. Switch => %-45s [%s]\n",
|
2006-05-09 08:03:52 +00:00
|
|
|
buf, ast_get_switch_registrar(sw));
|
2006-05-08 21:49:22 +00:00
|
|
|
}
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
2006-05-08 21:49:22 +00:00
|
|
|
|
|
|
|
ast_unlock_context(c);
|
|
|
|
|
|
|
|
/* if we print something in context, make an empty line */
|
|
|
|
if (context_info_printed)
|
|
|
|
ast_cli(fd, "\r\n");
|
2001-04-27 15:07:56 +00:00
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
ast_unlock_contexts();
|
|
|
|
|
2006-04-14 22:48:05 +00:00
|
|
|
return (dpc->total_exten == old_total_exten) ? -1 : res;
|
2005-01-09 20:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_show_dialplan(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *exten = NULL, *context = NULL;
|
|
|
|
/* Variables used for different counters */
|
|
|
|
struct dialplan_counters counters;
|
2006-05-08 21:49:22 +00:00
|
|
|
|
2006-05-09 08:16:07 +00:00
|
|
|
const char *incstack[AST_PBX_MAX_STACK];
|
2005-01-09 20:28:47 +00:00
|
|
|
memset(&counters, 0, sizeof(counters));
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
if (argc != 2 && argc != 3)
|
2005-04-01 19:56:35 +00:00
|
|
|
return RESULT_SHOWUSAGE;
|
2005-01-09 20:28:47 +00:00
|
|
|
|
|
|
|
/* we obtain [exten@]context? if yes, split them ... */
|
|
|
|
if (argc == 3) {
|
2006-05-09 07:49:17 +00:00
|
|
|
if (strchr(argv[2], '@')) { /* split into exten & context */
|
|
|
|
context = ast_strdupa(argv[2]);
|
|
|
|
exten = strsep(&context, "@");
|
|
|
|
/* change empty strings to NULL */
|
2005-04-01 19:56:35 +00:00
|
|
|
if (ast_strlen_zero(exten))
|
|
|
|
exten = NULL;
|
2006-05-09 07:49:17 +00:00
|
|
|
} else { /* no '@' char, only context given */
|
2005-01-09 20:28:47 +00:00
|
|
|
context = argv[2];
|
|
|
|
}
|
2006-05-09 07:49:17 +00:00
|
|
|
if (ast_strlen_zero(context))
|
|
|
|
context = NULL;
|
2005-01-09 20:28:47 +00:00
|
|
|
}
|
2006-05-09 07:49:17 +00:00
|
|
|
/* else Show complete dial plan, context and exten are NULL */
|
|
|
|
show_dialplan_helper(fd, context, exten, &counters, NULL, 0, incstack);
|
2005-01-09 20:28:47 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/* check for input failure and throw some error messages */
|
2005-01-09 20:28:47 +00:00
|
|
|
if (context && !counters.context_existence) {
|
|
|
|
ast_cli(fd, "There is no existence of '%s' context\n", context);
|
2001-09-28 21:20:52 +00:00
|
|
|
return RESULT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2005-01-09 20:28:47 +00:00
|
|
|
if (exten && !counters.extension_existence) {
|
2001-09-28 21:20:52 +00:00
|
|
|
if (context)
|
|
|
|
ast_cli(fd, "There is no existence of %s@%s extension\n",
|
|
|
|
exten, context);
|
|
|
|
else
|
|
|
|
ast_cli(fd,
|
|
|
|
"There is no existence of '%s' extension in all contexts\n",
|
|
|
|
exten);
|
|
|
|
return RESULT_FAILURE;
|
|
|
|
}
|
2005-01-09 20:28:47 +00:00
|
|
|
|
2005-09-16 18:11:00 +00:00
|
|
|
ast_cli(fd,"-= %d %s (%d %s) in %d %s. =-\n",
|
|
|
|
counters.total_exten, counters.total_exten == 1 ? "extension" : "extensions",
|
|
|
|
counters.total_prio, counters.total_prio == 1 ? "priority" : "priorities",
|
|
|
|
counters.total_context, counters.total_context == 1 ? "context" : "contexts");
|
2005-01-09 20:28:47 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/* everything ok */
|
2001-04-27 15:07:56 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-10-30 21:18:39 +00:00
|
|
|
/*! \brief Send ack once */
|
2007-01-05 22:43:18 +00:00
|
|
|
static void manager_dpsendack(struct mansession *s, const struct message *m)
|
2006-10-30 21:18:39 +00:00
|
|
|
{
|
|
|
|
astman_send_listack(s, m, "DialPlan list will follow", "start");
|
|
|
|
}
|
|
|
|
|
2006-12-13 22:54:41 +00:00
|
|
|
/*! \brief Show dialplan extensions
|
|
|
|
* XXX this function is similar but not exactly the same as the CLI's
|
|
|
|
* show dialplan. Must check whether the difference is intentional or not.
|
|
|
|
*/
|
2007-01-05 22:43:18 +00:00
|
|
|
static int manager_show_dialplan_helper(struct mansession *s, const struct message *m,
|
|
|
|
const char *actionidtext, const char *context,
|
|
|
|
const char *exten, struct dialplan_counters *dpc,
|
|
|
|
struct ast_include *rinclude)
|
2006-10-30 21:18:39 +00:00
|
|
|
{
|
|
|
|
struct ast_context *c;
|
|
|
|
int res=0, old_total_exten = dpc->total_exten;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(exten))
|
|
|
|
exten = NULL;
|
|
|
|
if (ast_strlen_zero(context))
|
|
|
|
context = NULL;
|
|
|
|
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Context: -%s- Extension: -%s-\n", context, exten);
|
2006-10-30 21:18:39 +00:00
|
|
|
|
|
|
|
/* try to lock contexts */
|
2007-02-28 20:46:01 +00:00
|
|
|
if (ast_rdlock_contexts()) {
|
2006-10-30 21:18:39 +00:00
|
|
|
astman_send_error(s, m, "Failed to lock contexts\r\n");
|
|
|
|
ast_log(LOG_WARNING, "Failed to lock contexts list for manager: listdialplan\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-13 22:54:41 +00:00
|
|
|
c = NULL; /* walk all contexts ... */
|
|
|
|
while ( (c = ast_walk_contexts(c)) ) {
|
|
|
|
struct ast_exten *e;
|
|
|
|
struct ast_include *i;
|
|
|
|
struct ast_ignorepat *ip;
|
|
|
|
|
|
|
|
if (context && strcmp(ast_get_context_name(c), context) != 0)
|
|
|
|
continue; /* not the name we want */
|
|
|
|
|
|
|
|
dpc->context_existence = 1;
|
2006-10-30 21:18:39 +00:00
|
|
|
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Found Context: %s \n", ast_get_context_name(c));
|
2006-12-13 22:54:41 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
if (ast_rdlock_context(c)) { /* failed to lock */
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Failed to lock context\n");
|
2006-12-13 22:54:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-10-30 21:18:39 +00:00
|
|
|
|
2006-12-13 22:54:41 +00:00
|
|
|
/* XXX note- an empty context is not printed */
|
|
|
|
e = NULL; /* walk extensions in context */
|
|
|
|
while ( (e = ast_walk_context_extensions(c, e)) ) {
|
|
|
|
struct ast_exten *p;
|
2006-10-30 21:18:39 +00:00
|
|
|
|
2006-12-13 22:54:41 +00:00
|
|
|
/* looking for extension? is this our extension? */
|
|
|
|
if (exten && !ast_extension_match(ast_get_extension_name(e), exten)) {
|
|
|
|
/* not the one we are looking for, continue */
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Skipping extension %s\n", ast_get_extension_name(e));
|
2006-12-13 22:54:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Found Extension: %s \n", ast_get_extension_name(e));
|
2006-12-13 22:54:41 +00:00
|
|
|
|
|
|
|
dpc->extension_existence = 1;
|
|
|
|
|
|
|
|
/* may we print context info? */
|
|
|
|
dpc->total_context++;
|
|
|
|
dpc->total_exten++;
|
|
|
|
|
|
|
|
p = NULL; /* walk next extension peers */
|
|
|
|
while ( (p = ast_walk_extension_priorities(e, p)) ) {
|
|
|
|
int prio = ast_get_extension_priority(p);
|
|
|
|
|
|
|
|
dpc->total_prio++;
|
2006-12-13 23:08:02 +00:00
|
|
|
if (!dpc->total_items++)
|
|
|
|
manager_dpsendack(s, m);
|
2006-12-13 22:54:41 +00:00
|
|
|
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
|
|
|
|
astman_append(s, "Context: %s\r\nExtension: %s\r\n", ast_get_context_name(c), ast_get_extension_name(e) );
|
|
|
|
|
|
|
|
/* XXX maybe make this conditional, if p != e ? */
|
|
|
|
if (ast_get_extension_label(p))
|
|
|
|
astman_append(s, "ExtensionLabel: %s\r\n", ast_get_extension_label(p));
|
|
|
|
|
|
|
|
if (prio == PRIORITY_HINT) {
|
|
|
|
astman_append(s, "Priority: hint\r\nApplication: %s\r\n", ast_get_extension_app(p));
|
|
|
|
} else {
|
|
|
|
astman_append(s, "Priority: %d\r\nApplication: %s\r\nAppData: %s\r\n", prio, ast_get_extension_app(p), (char *) ast_get_extension_app_data(p));
|
2006-10-30 21:18:39 +00:00
|
|
|
}
|
2006-12-13 22:54:41 +00:00
|
|
|
astman_append(s, "Registrar: %s\r\n\r\n", ast_get_extension_registrar(e));
|
|
|
|
}
|
|
|
|
}
|
2006-10-30 21:18:39 +00:00
|
|
|
|
2006-12-13 22:54:41 +00:00
|
|
|
i = NULL; /* walk included and write info ... */
|
|
|
|
while ( (i = ast_walk_context_includes(c, i)) ) {
|
|
|
|
if (exten) {
|
|
|
|
/* Check all includes for the requested extension */
|
|
|
|
manager_show_dialplan_helper(s, m, actionidtext, ast_get_include_name(i), exten, dpc, i);
|
|
|
|
} else {
|
2006-12-13 23:08:02 +00:00
|
|
|
if (!dpc->total_items++)
|
|
|
|
manager_dpsendack(s, m);
|
2006-12-13 22:54:41 +00:00
|
|
|
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
|
|
|
|
astman_append(s, "Context: %s\r\nIncludeContext: %s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ast_get_include_name(i), ast_get_include_registrar(i));
|
|
|
|
astman_append(s, "\r\n");
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Found Included context: %s \n", ast_get_include_name(i));
|
2006-10-30 21:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
2006-12-13 22:54:41 +00:00
|
|
|
|
|
|
|
ip = NULL; /* walk ignore patterns and write info ... */
|
|
|
|
while ( (ip = ast_walk_context_ignorepats(c, ip)) ) {
|
|
|
|
const char *ipname = ast_get_ignorepat_name(ip);
|
|
|
|
char ignorepat[AST_MAX_EXTENSION];
|
|
|
|
|
|
|
|
snprintf(ignorepat, sizeof(ignorepat), "_%s.", ipname);
|
|
|
|
if (!exten || ast_extension_match(ignorepat, exten)) {
|
2006-12-13 23:08:02 +00:00
|
|
|
if (!dpc->total_items++)
|
|
|
|
manager_dpsendack(s, m);
|
2006-12-13 22:54:41 +00:00
|
|
|
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
|
|
|
|
astman_append(s, "Context: %s\r\nIgnorePattern: %s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ipname, ast_get_ignorepat_registrar(ip));
|
|
|
|
astman_append(s, "\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!rinclude) {
|
|
|
|
struct ast_sw *sw = NULL;
|
|
|
|
while ( (sw = ast_walk_context_switches(c, sw)) ) {
|
2006-12-13 23:08:02 +00:00
|
|
|
if (!dpc->total_items++)
|
|
|
|
manager_dpsendack(s, m);
|
2006-12-13 22:54:41 +00:00
|
|
|
astman_append(s, "Event: ListDialplan\r\n%s", actionidtext);
|
|
|
|
astman_append(s, "Context: %s\r\nSwitch: %s/%s\r\nRegistrar: %s\r\n", ast_get_context_name(c), ast_get_switch_name(sw), ast_get_switch_data(sw), ast_get_switch_registrar(sw));
|
|
|
|
astman_append(s, "\r\n");
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Found Switch : %s \n", ast_get_switch_name(sw));
|
2006-12-13 22:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_unlock_context(c);
|
|
|
|
}
|
2006-10-30 21:18:39 +00:00
|
|
|
ast_unlock_contexts();
|
|
|
|
|
|
|
|
if (dpc->total_exten == old_total_exten) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(3, "manager_show_dialplan: Found nothing new\n");
|
2006-10-30 21:18:39 +00:00
|
|
|
/* Nothing new under the sun */
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Manager listing of dial plan */
|
2007-01-05 22:43:18 +00:00
|
|
|
static int manager_show_dialplan(struct mansession *s, const struct message *m)
|
2006-10-30 21:18:39 +00:00
|
|
|
{
|
2007-01-05 22:43:18 +00:00
|
|
|
const char *exten, *context;
|
|
|
|
const char *id = astman_get_header(m, "ActionID");
|
2006-10-30 21:18:39 +00:00
|
|
|
char idtext[256];
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* Variables used for different counters */
|
|
|
|
struct dialplan_counters counters;
|
|
|
|
|
2007-06-13 17:06:53 +00:00
|
|
|
if (!ast_strlen_zero(id))
|
2006-10-30 21:18:39 +00:00
|
|
|
snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
|
2006-12-13 22:54:41 +00:00
|
|
|
else
|
|
|
|
idtext[0] = '\0';
|
2006-10-30 21:18:39 +00:00
|
|
|
|
|
|
|
memset(&counters, 0, sizeof(counters));
|
|
|
|
|
|
|
|
exten = astman_get_header(m, "Extension");
|
|
|
|
context = astman_get_header(m, "Context");
|
|
|
|
|
|
|
|
res = manager_show_dialplan_helper(s, m, idtext, context, exten, &counters, NULL);
|
|
|
|
|
|
|
|
if (context && !counters.context_existence) {
|
|
|
|
char errorbuf[BUFSIZ];
|
|
|
|
|
|
|
|
snprintf(errorbuf, sizeof(errorbuf), "Did not find context %s\r\n", context);
|
|
|
|
astman_send_error(s, m, errorbuf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (exten && !counters.extension_existence) {
|
|
|
|
char errorbuf[BUFSIZ];
|
|
|
|
|
|
|
|
if (context)
|
|
|
|
snprintf(errorbuf, sizeof(errorbuf), "Did not find extension %s@%s\r\n", exten, context);
|
|
|
|
else
|
|
|
|
snprintf(errorbuf, sizeof(errorbuf), "Did not find extension %s in any context\r\n", exten);
|
|
|
|
astman_send_error(s, m, errorbuf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
manager_event(EVENT_FLAG_CONFIG, "ShowDialPlanComplete",
|
|
|
|
"EventList: Complete\r\n"
|
|
|
|
"ListItems: %d\r\n"
|
|
|
|
"ListExtensions: %d\r\n"
|
|
|
|
"ListPriorities: %d\r\n"
|
|
|
|
"ListContexts: %d\r\n"
|
|
|
|
"%s"
|
|
|
|
"\r\n", counters.total_items, counters.total_exten, counters.total_prio, counters.total_context, idtext);
|
|
|
|
|
|
|
|
/* everything ok */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char mandescr_show_dialplan[] =
|
|
|
|
"Description: Show dialplan contexts and extensions.\n"
|
|
|
|
"Be aware that showing the full dialplan may take a lot of capacity\n"
|
|
|
|
"Variables: \n"
|
|
|
|
" ActionID: <id> Action ID for this AMI transaction (optional)\n"
|
|
|
|
" Extension: <extension> Extension (Optional)\n"
|
|
|
|
" Context: <context> Context (Optional)\n"
|
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
2006-03-07 22:57:52 +00:00
|
|
|
/*! \brief CLI support for listing global variables in a parseable way */
|
|
|
|
static int handle_show_globals(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
struct ast_var_t *newvariable;
|
|
|
|
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_rdlock(&globalslock);
|
2006-03-07 22:57:52 +00:00
|
|
|
AST_LIST_TRAVERSE (&globals, newvariable, entries) {
|
|
|
|
i++;
|
|
|
|
ast_cli(fd, " %s=%s\n", ast_var_name(newvariable), ast_var_value(newvariable));
|
|
|
|
}
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_unlock(&globalslock);
|
2006-03-07 22:57:52 +00:00
|
|
|
ast_cli(fd, "\n -- %d variables\n", i);
|
2006-03-23 21:16:46 +00:00
|
|
|
|
2006-03-07 22:57:52 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
static int handle_set_global(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc != 5)
|
|
|
|
return RESULT_SHOWUSAGE;
|
|
|
|
|
|
|
|
pbx_builtin_setvar_helper(NULL, argv[3], argv[4]);
|
|
|
|
ast_cli(fd, "\n -- Global variable %s set to %s\n", argv[3], argv[4]);
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-07 22:57:52 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
|
|
|
* CLI entries for upper commands ...
|
|
|
|
*/
|
2005-06-03 03:09:20 +00:00
|
|
|
static struct ast_cli_entry pbx_cli[] = {
|
2006-11-02 23:16:09 +00:00
|
|
|
{ { "core", "show", "applications", NULL },
|
2006-09-18 19:54:18 +00:00
|
|
|
handle_show_applications, "Shows registered dialplan applications",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_applications_help, complete_show_applications },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
2006-11-02 23:16:09 +00:00
|
|
|
{ { "core", "show", "functions", NULL },
|
2006-09-18 19:54:18 +00:00
|
|
|
handle_show_functions, "Shows registered dialplan functions",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_functions_help },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
2006-11-02 23:16:09 +00:00
|
|
|
{ { "core", "show", "switches", NULL },
|
2006-09-18 19:54:18 +00:00
|
|
|
handle_show_switches, "Show alternative switches",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_switches_help },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
2006-11-02 23:16:09 +00:00
|
|
|
{ { "core", "show", "hints", NULL },
|
2006-09-18 19:54:18 +00:00
|
|
|
handle_show_hints, "Show dialplan hints",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_hints_help },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
2006-11-02 23:16:09 +00:00
|
|
|
{ { "core", "show", "globals", NULL },
|
2006-09-18 19:54:18 +00:00
|
|
|
handle_show_globals, "Show global dialplan variables",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_globals_help },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
|
|
|
{ { "core", "show" , "function", NULL },
|
|
|
|
handle_show_function, "Describe a specific dialplan function",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_function_help, complete_show_function },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
|
|
|
{ { "core", "show", "application", NULL },
|
|
|
|
handle_show_application, "Describe a specific dialplan application",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_application_help, complete_show_application },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
|
|
|
{ { "core", "set", "global", NULL },
|
|
|
|
handle_set_global, "Set global dialplan variable",
|
2006-09-21 21:17:39 +00:00
|
|
|
set_global_help },
|
2006-09-18 19:54:18 +00:00
|
|
|
|
|
|
|
{ { "dialplan", "show", NULL },
|
|
|
|
handle_show_dialplan, "Show dialplan",
|
2006-09-21 21:17:39 +00:00
|
|
|
show_dialplan_help, complete_show_dialplan_context },
|
2005-05-05 05:39:33 +00:00
|
|
|
};
|
|
|
|
|
2006-09-28 22:49:55 +00:00
|
|
|
static void unreference_cached_app(struct ast_app *app)
|
|
|
|
{
|
|
|
|
struct ast_context *context = NULL;
|
|
|
|
struct ast_exten *eroot = NULL, *e = NULL;
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rdlock_contexts();
|
2006-09-28 22:49:55 +00:00
|
|
|
while ((context = ast_walk_contexts(context))) {
|
|
|
|
while ((eroot = ast_walk_context_extensions(context, eroot))) {
|
|
|
|
while ((e = ast_walk_extension_priorities(eroot, e))) {
|
|
|
|
if (e->cached_app == app)
|
|
|
|
e->cached_app = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_unlock_contexts();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_unregister_application(const char *app)
|
2005-09-24 14:19:00 +00:00
|
|
|
{
|
2006-05-01 03:14:21 +00:00
|
|
|
struct ast_app *tmp;
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&apps);
|
|
|
|
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, tmp, list) {
|
1999-12-04 20:45:45 +00:00
|
|
|
if (!strcasecmp(app, tmp->name)) {
|
2006-09-28 22:49:55 +00:00
|
|
|
unreference_cached_app(tmp);
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_REMOVE_CURRENT(&apps, list);
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(2, "Unregistered application '%s'\n", tmp->name);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(tmp);
|
2006-05-01 03:14:21 +00:00
|
|
|
break;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE_SAFE_END
|
|
|
|
AST_RWLIST_UNLOCK(&apps);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-05-01 03:14:21 +00:00
|
|
|
return tmp ? 0 : -1;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2006-09-16 23:53:58 +00:00
|
|
|
static struct ast_context *__ast_context_create(struct ast_context **extcontexts, const char *name, const char *registrar, int existsokay)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2003-07-14 15:35:21 +00:00
|
|
|
struct ast_context *tmp, **local_contexts;
|
2006-05-10 20:44:28 +00:00
|
|
|
int length = sizeof(struct ast_context) + strlen(name) + 1;
|
|
|
|
|
2003-07-14 15:35:21 +00:00
|
|
|
if (!extcontexts) {
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_contexts();
|
2006-05-10 20:44:28 +00:00
|
|
|
local_contexts = &contexts;
|
2003-07-14 15:35:21 +00:00
|
|
|
} else
|
|
|
|
local_contexts = extcontexts;
|
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
for (tmp = *local_contexts; tmp; tmp = tmp->next) {
|
1999-12-04 20:45:45 +00:00
|
|
|
if (!strcasecmp(tmp->name, name)) {
|
2006-09-16 23:53:58 +00:00
|
|
|
if (!existsokay) {
|
|
|
|
ast_log(LOG_WARNING, "Tried to register context '%s', already in use\n", name);
|
|
|
|
tmp = NULL;
|
|
|
|
}
|
2003-07-14 15:35:21 +00:00
|
|
|
if (!extcontexts)
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-09-16 23:53:58 +00:00
|
|
|
return tmp;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-29 19:54:41 +00:00
|
|
|
if ((tmp = ast_calloc(1, length))) {
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rwlock_init(&tmp->lock);
|
2006-08-14 03:24:06 +00:00
|
|
|
ast_mutex_init(&tmp->macrolock);
|
2004-10-03 04:19:59 +00:00
|
|
|
strcpy(tmp->name, name);
|
1999-12-04 20:45:45 +00:00
|
|
|
tmp->root = NULL;
|
2001-04-27 15:07:56 +00:00
|
|
|
tmp->registrar = registrar;
|
2003-07-14 15:35:21 +00:00
|
|
|
tmp->next = *local_contexts;
|
2001-03-30 18:47:35 +00:00
|
|
|
tmp->includes = NULL;
|
2001-09-28 21:20:52 +00:00
|
|
|
tmp->ignorepats = NULL;
|
2003-07-14 15:35:21 +00:00
|
|
|
*local_contexts = tmp;
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Registered context '%s'\n", tmp->name);
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Registered extension context '%s'\n", tmp->name);
|
2006-03-29 19:54:41 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-07-14 15:35:21 +00:00
|
|
|
if (!extcontexts)
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
1999-12-04 20:45:45 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2006-09-16 23:53:58 +00:00
|
|
|
struct ast_context *ast_context_create(struct ast_context **extcontexts, const char *name, const char *registrar)
|
|
|
|
{
|
|
|
|
return __ast_context_create(extcontexts, name, registrar, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_context *ast_context_find_or_create(struct ast_context **extcontexts, const char *name, const char *registrar)
|
|
|
|
{
|
|
|
|
return __ast_context_create(extcontexts, name, registrar, 1);
|
|
|
|
}
|
2004-10-03 04:19:59 +00:00
|
|
|
void __ast_context_destroy(struct ast_context *con, const char *registrar);
|
2003-07-14 15:35:21 +00:00
|
|
|
|
2005-08-27 23:55:14 +00:00
|
|
|
struct store_hint {
|
|
|
|
char *context;
|
|
|
|
char *exten;
|
|
|
|
struct ast_state_cb *callbacks;
|
|
|
|
int laststate;
|
|
|
|
AST_LIST_ENTRY(store_hint) list;
|
|
|
|
char data[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
AST_LIST_HEAD(store_hints, store_hint);
|
|
|
|
|
2006-05-09 07:49:17 +00:00
|
|
|
/* XXX this does not check that multiple contexts are merged */
|
2005-08-27 23:55:14 +00:00
|
|
|
void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char *registrar)
|
|
|
|
{
|
2003-07-14 15:35:21 +00:00
|
|
|
struct ast_context *tmp, *lasttmp = NULL;
|
2006-05-11 20:07:44 +00:00
|
|
|
struct store_hints store = AST_LIST_HEAD_INIT_VALUE;
|
2005-08-27 23:55:14 +00:00
|
|
|
struct store_hint *this;
|
|
|
|
struct ast_hint *hint;
|
|
|
|
struct ast_exten *exten;
|
|
|
|
int length;
|
|
|
|
struct ast_state_cb *thiscb, *prevcb;
|
|
|
|
|
2006-03-31 18:28:52 +00:00
|
|
|
/* it is very important that this function hold the hint list lock _and_ the conlock
|
|
|
|
during its operation; not only do we need to ensure that the list of contexts
|
|
|
|
and extensions does not change, but also that no hint callbacks (watchers) are
|
|
|
|
added or removed during the merge/delete process
|
|
|
|
|
|
|
|
in addition, the locks _must_ be taken in this order, because there are already
|
|
|
|
other code paths that use this order
|
|
|
|
*/
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_contexts();
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_WRLOCK(&hints);
|
2006-03-31 18:28:52 +00:00
|
|
|
|
|
|
|
/* preserve all watchers for hints associated with this registrar */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2005-08-27 23:55:14 +00:00
|
|
|
if (hint->callbacks && !strcmp(registrar, hint->exten->parent->registrar)) {
|
|
|
|
length = strlen(hint->exten->exten) + strlen(hint->exten->parent->name) + 2 + sizeof(*this);
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(this = ast_calloc(1, length)))
|
2005-08-27 23:55:14 +00:00
|
|
|
continue;
|
|
|
|
this->callbacks = hint->callbacks;
|
|
|
|
hint->callbacks = NULL;
|
|
|
|
this->laststate = hint->laststate;
|
|
|
|
this->context = this->data;
|
|
|
|
strcpy(this->data, hint->exten->parent->name);
|
|
|
|
this->exten = this->data + strlen(this->context) + 1;
|
|
|
|
strcpy(this->exten, hint->exten->exten);
|
|
|
|
AST_LIST_INSERT_HEAD(&store, this, list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-14 15:35:21 +00:00
|
|
|
tmp = *extcontexts;
|
2003-07-14 17:17:05 +00:00
|
|
|
if (registrar) {
|
2006-05-09 08:31:42 +00:00
|
|
|
/* XXX remove previous contexts from same registrar */
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "must remove any reg %s\n", registrar);
|
2004-04-06 22:17:32 +00:00
|
|
|
__ast_context_destroy(NULL,registrar);
|
2003-07-14 17:17:05 +00:00
|
|
|
while (tmp) {
|
|
|
|
lasttmp = tmp;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
} else {
|
2006-05-09 08:31:42 +00:00
|
|
|
/* XXX remove contexts with the same name */
|
2003-07-14 17:17:05 +00:00
|
|
|
while (tmp) {
|
2006-05-09 08:31:42 +00:00
|
|
|
ast_log(LOG_WARNING, "must remove %s reg %s\n", tmp->name, tmp->registrar);
|
2004-04-06 22:17:32 +00:00
|
|
|
__ast_context_destroy(tmp,tmp->registrar);
|
2003-07-14 17:17:05 +00:00
|
|
|
lasttmp = tmp;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
2003-07-14 15:35:21 +00:00
|
|
|
}
|
|
|
|
if (lasttmp) {
|
|
|
|
lasttmp->next = contexts;
|
|
|
|
contexts = *extcontexts;
|
|
|
|
*extcontexts = NULL;
|
2006-05-09 08:03:52 +00:00
|
|
|
} else
|
2003-07-14 15:35:21 +00:00
|
|
|
ast_log(LOG_WARNING, "Requested contexts didn't get merged\n");
|
2005-08-27 23:55:14 +00:00
|
|
|
|
|
|
|
/* restore the watchers for hints that can be found; notify those that
|
|
|
|
cannot be restored
|
|
|
|
*/
|
|
|
|
while ((this = AST_LIST_REMOVE_HEAD(&store, list))) {
|
|
|
|
exten = ast_hint_extension(NULL, this->context, this->exten);
|
|
|
|
/* Find the hint in the list of hints */
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_TRAVERSE(&hints, hint, list) {
|
2005-08-27 23:55:14 +00:00
|
|
|
if (hint->exten == exten)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!exten || !hint) {
|
|
|
|
/* this hint has been removed, notify the watchers */
|
|
|
|
prevcb = NULL;
|
|
|
|
thiscb = this->callbacks;
|
|
|
|
while (thiscb) {
|
2006-05-09 08:03:52 +00:00
|
|
|
prevcb = thiscb;
|
2005-08-27 23:55:14 +00:00
|
|
|
thiscb = thiscb->next;
|
|
|
|
prevcb->callback(this->context, this->exten, AST_EXTENSION_REMOVED, prevcb->data);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(prevcb);
|
2005-08-27 23:55:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
thiscb = this->callbacks;
|
|
|
|
while (thiscb->next)
|
|
|
|
thiscb = thiscb->next;
|
|
|
|
thiscb->next = hint->callbacks;
|
|
|
|
hint->callbacks = this->callbacks;
|
|
|
|
hint->laststate = this->laststate;
|
|
|
|
}
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(this);
|
2005-08-27 23:55:14 +00:00
|
|
|
}
|
|
|
|
|
2006-11-02 21:40:53 +00:00
|
|
|
AST_RWLIST_UNLOCK(&hints);
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_contexts();
|
2006-03-31 18:28:52 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
return;
|
2003-07-14 15:35:21 +00:00
|
|
|
}
|
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
|
|
|
* errno values
|
|
|
|
* EBUSY - can't lock
|
2003-04-23 19:13:35 +00:00
|
|
|
* ENOENT - no existence of context
|
2001-09-28 21:20:52 +00:00
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_add_include(const char *context, const char *include, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-04-19 08:52:15 +00:00
|
|
|
int ret = -1;
|
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
if (c) {
|
|
|
|
ret = ast_context_add_include2(c, include, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-04-19 08:52:15 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2006-01-12 17:27:38 +00:00
|
|
|
/*! \brief Helper for get_range.
|
|
|
|
* return the index of the matching entry, starting from 1.
|
|
|
|
* If names is not supplied, try numeric values.
|
|
|
|
*/
|
|
|
|
static int lookup_name(const char *s, char *const names[], int max)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (names) {
|
|
|
|
for (i = 0; names[i]; i++) {
|
|
|
|
if (!strcasecmp(s, names[i]))
|
|
|
|
return i+1;
|
|
|
|
}
|
|
|
|
} else if (sscanf(s, "%d", &i) == 1 && i >= 1 && i <= max) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return 0; /* error return */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief helper function to return a range up to max (7, 12, 31 respectively).
|
|
|
|
* names, if supplied, is an array of names that should be mapped to numbers.
|
|
|
|
*/
|
|
|
|
static unsigned get_range(char *src, int max, char *const names[], const char *msg)
|
|
|
|
{
|
|
|
|
int s, e; /* start and ending position */
|
|
|
|
unsigned int mask = 0;
|
2002-06-21 01:40:13 +00:00
|
|
|
|
2006-01-12 17:27:38 +00:00
|
|
|
/* Check for whole range */
|
|
|
|
if (ast_strlen_zero(src) || !strcmp(src, "*")) {
|
|
|
|
s = 0;
|
|
|
|
e = max - 1;
|
|
|
|
} else {
|
|
|
|
/* Get start and ending position */
|
|
|
|
char *c = strchr(src, '-');
|
|
|
|
if (c)
|
|
|
|
*c++ = '\0';
|
|
|
|
/* Find the start */
|
|
|
|
s = lookup_name(src, names, max);
|
|
|
|
if (!s) {
|
|
|
|
ast_log(LOG_WARNING, "Invalid %s '%s', assuming none\n", msg, src);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s--;
|
|
|
|
if (c) { /* find end of range */
|
|
|
|
e = lookup_name(c, names, max);
|
|
|
|
if (!e) {
|
|
|
|
ast_log(LOG_WARNING, "Invalid end %s '%s', assuming none\n", msg, c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
e--;
|
|
|
|
} else
|
|
|
|
e = s;
|
|
|
|
}
|
|
|
|
/* Fill the mask. Remember that ranges are cyclic */
|
2006-01-13 17:35:12 +00:00
|
|
|
mask = 1 << e; /* initialize with last element */
|
2006-03-21 05:48:17 +00:00
|
|
|
while (s != e) {
|
|
|
|
if (s >= max) {
|
|
|
|
s = 0;
|
|
|
|
mask |= (1 << s);
|
|
|
|
} else {
|
|
|
|
mask |= (1 << s);
|
|
|
|
s++;
|
|
|
|
}
|
2006-01-12 17:27:38 +00:00
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief store a bitmask of valid times, one bit each 2 minute */
|
2004-11-11 21:23:45 +00:00
|
|
|
static void get_timerange(struct ast_timing *i, char *times)
|
2002-06-21 01:40:13 +00:00
|
|
|
{
|
|
|
|
char *e;
|
|
|
|
int x;
|
|
|
|
int s1, s2;
|
|
|
|
int e1, e2;
|
2004-07-17 02:25:53 +00:00
|
|
|
/* int cth, ctm; */
|
2003-07-01 20:28:02 +00:00
|
|
|
|
2004-07-17 02:25:53 +00:00
|
|
|
/* start disabling all times, fill the fields with 0's, as they may contain garbage */
|
2003-08-07 17:35:08 +00:00
|
|
|
memset(i->minmask, 0, sizeof(i->minmask));
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-01-12 17:27:38 +00:00
|
|
|
/* 2-minutes per bit, since the mask has only 32 bits :( */
|
2002-06-21 01:40:13 +00:00
|
|
|
/* Star is all times */
|
2004-05-04 06:34:34 +00:00
|
|
|
if (ast_strlen_zero(times) || !strcmp(times, "*")) {
|
2005-04-01 19:56:35 +00:00
|
|
|
for (x=0; x<24; x++)
|
2006-01-12 17:27:38 +00:00
|
|
|
i->minmask[x] = 0x3fffffff; /* 30 bits */
|
2002-06-21 01:40:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Otherwise expect a range */
|
|
|
|
e = strchr(times, '-');
|
|
|
|
if (!e) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_WARNING, "Time range is not valid. Assuming no restrictions based on time.\n");
|
2002-06-21 01:40:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-01-12 17:27:38 +00:00
|
|
|
*e++ = '\0';
|
|
|
|
/* XXX why skip non digits ? */
|
2006-05-09 08:03:52 +00:00
|
|
|
while (*e && !isdigit(*e))
|
2005-04-01 19:56:35 +00:00
|
|
|
e++;
|
2002-06-21 01:40:13 +00:00
|
|
|
if (!*e) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_WARNING, "Invalid time range. Assuming no restrictions based on time.\n");
|
2002-06-21 01:40:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_WARNING, "%s isn't a time. Assuming no restrictions based on time.\n", times);
|
2002-06-21 01:40:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_WARNING, "%s isn't a time. Assuming no restrictions based on time.\n", e);
|
2002-06-21 01:40:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-01-12 17:27:38 +00:00
|
|
|
/* XXX this needs to be optimized */
|
2004-01-13 07:48:39 +00:00
|
|
|
#if 1
|
2002-06-21 01:40:13 +00:00
|
|
|
s1 = s1 * 30 + s2/2;
|
|
|
|
if ((s1 < 0) || (s1 >= 24*30)) {
|
2004-03-04 05:23:50 +00:00
|
|
|
ast_log(LOG_WARNING, "%s isn't a valid start time. Assuming no time.\n", times);
|
2002-06-21 01:40:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
e1 = e1 * 30 + e2/2;
|
2004-03-04 05:23:50 +00:00
|
|
|
if ((e1 < 0) || (e1 >= 24*30)) {
|
|
|
|
ast_log(LOG_WARNING, "%s isn't a valid end time. Assuming no time.\n", e);
|
2002-06-21 01:40:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Go through the time and enable each appropriate bit */
|
2003-08-07 19:21:56 +00:00
|
|
|
for (x=s1;x != e1;x = (x + 1) % (24 * 30)) {
|
2002-06-21 01:40:13 +00:00
|
|
|
i->minmask[x/30] |= (1 << (x % 30));
|
|
|
|
}
|
|
|
|
/* Do the last one */
|
|
|
|
i->minmask[x/30] |= (1 << (x % 30));
|
2004-01-11 09:19:16 +00:00
|
|
|
#else
|
2005-07-19 15:54:17 +00:00
|
|
|
for (cth=0; cth<24; cth++) {
|
2004-01-11 09:19:16 +00:00
|
|
|
/* Initialize masks to blank */
|
|
|
|
i->minmask[cth] = 0;
|
2005-07-19 15:54:17 +00:00
|
|
|
for (ctm=0; ctm<30; ctm++) {
|
2004-01-11 09:19:16 +00:00
|
|
|
if (
|
|
|
|
/* First hour with more than one hour */
|
|
|
|
(((cth == s1) && (ctm >= s2)) &&
|
|
|
|
((cth < e1)))
|
|
|
|
/* Only one hour */
|
|
|
|
|| (((cth == s1) && (ctm >= s2)) &&
|
|
|
|
((cth == e1) && (ctm <= e2)))
|
|
|
|
/* In between first and last hours (more than 2 hours) */
|
|
|
|
|| ((cth > s1) &&
|
|
|
|
(cth < e1))
|
|
|
|
/* Last hour with more than one hour */
|
|
|
|
|| ((cth > s1) &&
|
|
|
|
((cth == e1) && (ctm <= e2)))
|
|
|
|
)
|
|
|
|
i->minmask[cth] |= (1 << (ctm / 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-06-21 01:40:13 +00:00
|
|
|
/* All done */
|
2003-08-07 19:21:56 +00:00
|
|
|
return;
|
2002-06-21 01:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *days[] =
|
|
|
|
{
|
|
|
|
"sun",
|
|
|
|
"mon",
|
|
|
|
"tue",
|
|
|
|
"wed",
|
|
|
|
"thu",
|
|
|
|
"fri",
|
|
|
|
"sat",
|
2006-01-12 17:27:38 +00:00
|
|
|
NULL,
|
2002-06-21 01:40:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static char *months[] =
|
|
|
|
{
|
|
|
|
"jan",
|
|
|
|
"feb",
|
|
|
|
"mar",
|
|
|
|
"apr",
|
|
|
|
"may",
|
|
|
|
"jun",
|
|
|
|
"jul",
|
|
|
|
"aug",
|
|
|
|
"sep",
|
|
|
|
"oct",
|
|
|
|
"nov",
|
|
|
|
"dec",
|
2006-01-12 17:27:38 +00:00
|
|
|
NULL,
|
2002-06-21 01:40:13 +00:00
|
|
|
};
|
|
|
|
|
2006-04-14 21:00:59 +00:00
|
|
|
int ast_build_timing(struct ast_timing *i, const char *info_in)
|
2002-06-21 01:40:13 +00:00
|
|
|
{
|
2005-03-29 06:16:49 +00:00
|
|
|
char info_save[256];
|
|
|
|
char *info;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2002-06-21 01:40:13 +00:00
|
|
|
/* Check for empty just in case */
|
2004-11-11 21:23:45 +00:00
|
|
|
if (ast_strlen_zero(info_in))
|
|
|
|
return 0;
|
|
|
|
/* make a copy just in case we were passed a static string */
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(info_save, info_in, sizeof(info_save));
|
2004-11-11 21:23:45 +00:00
|
|
|
info = info_save;
|
2002-06-21 01:40:13 +00:00
|
|
|
/* Assume everything except time */
|
2006-01-12 17:27:38 +00:00
|
|
|
i->monthmask = 0xfff; /* 12 bits */
|
|
|
|
i->daymask = 0x7fffffffU; /* 31 bits */
|
|
|
|
i->dowmask = 0x7f; /* 7 bits */
|
|
|
|
/* on each call, use strsep() to move info to the next argument */
|
2007-07-23 19:51:41 +00:00
|
|
|
get_timerange(i, strsep(&info, "|,"));
|
2006-01-12 17:27:38 +00:00
|
|
|
if (info)
|
2007-07-23 19:51:41 +00:00
|
|
|
i->dowmask = get_range(strsep(&info, "|,"), 7, days, "day of week");
|
2006-01-12 17:27:38 +00:00
|
|
|
if (info)
|
2007-07-23 19:51:41 +00:00
|
|
|
i->daymask = get_range(strsep(&info, "|,"), 31, NULL, "day");
|
2006-01-12 17:27:38 +00:00
|
|
|
if (info)
|
2007-07-23 19:51:41 +00:00
|
|
|
i->monthmask = get_range(strsep(&info, "|,"), 12, months, "month");
|
2004-11-11 21:23:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-04-14 21:00:59 +00:00
|
|
|
int ast_check_timing(const struct ast_timing *i)
|
2004-11-11 21:23:45 +00:00
|
|
|
{
|
2007-07-18 19:47:20 +00:00
|
|
|
struct ast_tm tm;
|
|
|
|
struct timeval tv = ast_tvnow();
|
2004-11-11 21:23:45 +00:00
|
|
|
|
2007-07-18 19:47:20 +00:00
|
|
|
ast_localtime(&tv, &tm, NULL);
|
2004-11-11 21:23:45 +00:00
|
|
|
|
|
|
|
/* If it's not the right month, return */
|
2006-01-12 17:27:38 +00:00
|
|
|
if (!(i->monthmask & (1 << tm.tm_mon)))
|
2004-11-11 21:23:45 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If it's not that time of the month.... */
|
|
|
|
/* Warning, tm_mday has range 1..31! */
|
|
|
|
if (!(i->daymask & (1 << (tm.tm_mday-1))))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If it's not the right day of the week */
|
|
|
|
if (!(i->dowmask & (1 << tm.tm_wday)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Sanity check the hour just to be safe */
|
|
|
|
if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) {
|
|
|
|
ast_log(LOG_WARNING, "Insane time...\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now the tough part, we calculate if it fits
|
|
|
|
in the right time based on min/hour */
|
|
|
|
if (!(i->minmask[tm.tm_hour] & (1 << (tm.tm_min / 2))))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If we got this far, then we're good */
|
|
|
|
return 1;
|
2002-06-21 01:40:13 +00:00
|
|
|
}
|
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
|
|
|
* errno values
|
|
|
|
* ENOMEM - out of memory
|
|
|
|
* EBUSY - can't lock
|
|
|
|
* EEXIST - already included
|
|
|
|
* EINVAL - there is no existence of context for inclusion
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_add_include2(struct ast_context *con, const char *value,
|
|
|
|
const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
struct ast_include *new_include;
|
2002-06-21 01:40:13 +00:00
|
|
|
char *c;
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_include *i, *il = NULL; /* include, include_last */
|
2004-10-03 04:19:59 +00:00
|
|
|
int length;
|
|
|
|
char *p;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
length = sizeof(struct ast_include);
|
|
|
|
length += 2 * (strlen(value) + 1);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
/* allocate new include structure ... */
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(new_include = ast_calloc(1, length)))
|
2001-09-28 21:20:52 +00:00
|
|
|
return -1;
|
2006-01-21 23:05:19 +00:00
|
|
|
/* Fill in this structure. Use 'p' for assignments, as the fields
|
|
|
|
* in the structure are 'const char *'
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
p = new_include->stuff;
|
|
|
|
new_include->name = p;
|
2006-01-21 23:05:19 +00:00
|
|
|
strcpy(p, value);
|
2004-10-03 04:19:59 +00:00
|
|
|
p += strlen(value) + 1;
|
|
|
|
new_include->rname = p;
|
2006-01-21 23:05:19 +00:00
|
|
|
strcpy(p, value);
|
|
|
|
/* Strip off timing info, and process if it is there */
|
2007-07-23 19:51:41 +00:00
|
|
|
if ( (c = strchr(p, ',')) ) {
|
2006-01-21 23:05:19 +00:00
|
|
|
*c++ = '\0';
|
|
|
|
new_include->hastime = ast_build_timing(&(new_include->timing), c);
|
2002-06-21 01:40:13 +00:00
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
new_include->next = NULL;
|
|
|
|
new_include->registrar = registrar;
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
/* ... go to last include and check if context is already included too... */
|
2005-12-24 06:23:36 +00:00
|
|
|
for (i = con->includes; i; i = i->next) {
|
2001-09-28 21:20:52 +00:00
|
|
|
if (!strcasecmp(i->name, new_include->name)) {
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(new_include);
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
errno = EEXIST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
il = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ... include new context into context list, unlock, return */
|
|
|
|
if (il)
|
|
|
|
il->next = new_include;
|
|
|
|
else
|
|
|
|
con->includes = new_include;
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Including context '%s' in context '%s'\n", new_include->name, ast_get_context_name(con));
|
2007-02-28 20:46:01 +00:00
|
|
|
|
|
|
|
ast_unlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* errno values
|
|
|
|
* EBUSY - can't lock
|
2003-04-23 19:13:35 +00:00
|
|
|
* ENOENT - no existence of context
|
2001-09-28 21:20:52 +00:00
|
|
|
*/
|
2005-01-13 05:14:56 +00:00
|
|
|
int ast_context_add_switch(const char *context, const char *sw, const char *data, int eval, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-04-21 11:30:02 +00:00
|
|
|
int ret = -1;
|
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-21 11:30:02 +00:00
|
|
|
if (c) { /* found, add switch to this context */
|
|
|
|
ret = ast_context_add_switch2(c, sw, data, eval, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-04-21 11:30:02 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* errno values
|
|
|
|
* ENOMEM - out of memory
|
|
|
|
* EBUSY - can't lock
|
|
|
|
* EEXIST - already included
|
|
|
|
* EINVAL - there is no existence of context for inclusion
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_add_switch2(struct ast_context *con, const char *value,
|
2005-01-13 05:14:56 +00:00
|
|
|
const char *data, int eval, const char *registrar)
|
2001-03-30 18:47:35 +00:00
|
|
|
{
|
2001-09-28 21:20:52 +00:00
|
|
|
struct ast_sw *new_sw;
|
2006-05-08 11:20:19 +00:00
|
|
|
struct ast_sw *i;
|
2004-10-03 04:19:59 +00:00
|
|
|
int length;
|
|
|
|
char *p;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
length = sizeof(struct ast_sw);
|
|
|
|
length += strlen(value) + 1;
|
|
|
|
if (data)
|
|
|
|
length += strlen(data);
|
|
|
|
length++;
|
2005-01-13 05:14:56 +00:00
|
|
|
if (eval) {
|
|
|
|
/* Create buffer for evaluation of variables */
|
|
|
|
length += SWITCH_DATA_LENGTH;
|
|
|
|
length++;
|
|
|
|
}
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
/* allocate new sw structure ... */
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(new_sw = ast_calloc(1, length)))
|
2001-03-30 18:47:35 +00:00
|
|
|
return -1;
|
2001-09-28 21:20:52 +00:00
|
|
|
/* ... fill in this structure ... */
|
2004-10-03 04:19:59 +00:00
|
|
|
p = new_sw->stuff;
|
|
|
|
new_sw->name = p;
|
|
|
|
strcpy(new_sw->name, value);
|
|
|
|
p += strlen(value) + 1;
|
|
|
|
new_sw->data = p;
|
2005-01-13 05:14:56 +00:00
|
|
|
if (data) {
|
2004-10-03 04:19:59 +00:00
|
|
|
strcpy(new_sw->data, data);
|
2005-01-13 05:14:56 +00:00
|
|
|
p += strlen(data) + 1;
|
|
|
|
} else {
|
2004-10-03 04:19:59 +00:00
|
|
|
strcpy(new_sw->data, "");
|
2005-01-13 05:14:56 +00:00
|
|
|
p++;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
if (eval)
|
2005-01-13 05:14:56 +00:00
|
|
|
new_sw->tmpdata = p;
|
|
|
|
new_sw->eval = eval;
|
2001-09-28 21:20:52 +00:00
|
|
|
new_sw->registrar = registrar;
|
|
|
|
|
|
|
|
/* ... try to lock this context ... */
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
/* ... go to last sw and check if context is already swd too... */
|
2006-05-08 11:20:19 +00:00
|
|
|
AST_LIST_TRAVERSE(&con->alts, i, list) {
|
2004-03-06 20:15:08 +00:00
|
|
|
if (!strcasecmp(i->name, new_sw->name) && !strcasecmp(i->data, new_sw->data)) {
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(new_sw);
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
errno = EEXIST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ... sw new context into context list, unlock, return */
|
2006-05-08 11:20:19 +00:00
|
|
|
AST_LIST_INSERT_TAIL(&con->alts, new_sw, list);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Including switch '%s/%s' in context '%s'\n", new_sw->name, new_sw->data, ast_get_context_name(con));
|
2006-05-08 11:20:19 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EBUSY - can't lock
|
2003-04-23 19:13:35 +00:00
|
|
|
* ENOENT - there is not context existence
|
2001-09-28 21:20:52 +00:00
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_ignorepat(const char *context, const char *ignorepat, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-04-19 08:52:15 +00:00
|
|
|
int ret = -1;
|
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
if (c) {
|
|
|
|
ret = ast_context_remove_ignorepat2(c, ignorepat, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-04-19 08:52:15 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
struct ast_ignorepat *ip, *ipl = NULL;
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
for (ip = con->ignorepats; ip; ip = ip->next) {
|
2001-09-28 21:20:52 +00:00
|
|
|
if (!strcmp(ip->pattern, ignorepat) &&
|
2004-08-03 06:31:20 +00:00
|
|
|
(!registrar || (registrar == ip->registrar))) {
|
2001-09-28 21:20:52 +00:00
|
|
|
if (ipl) {
|
|
|
|
ipl->next = ip->next;
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(ip);
|
2001-09-28 21:20:52 +00:00
|
|
|
} else {
|
|
|
|
con->ignorepats = ip->next;
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(ip);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-12-24 06:23:36 +00:00
|
|
|
ipl = ip;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EBUSY - can't lock
|
2003-04-23 19:13:35 +00:00
|
|
|
* ENOENT - there is no existence of context
|
2001-09-28 21:20:52 +00:00
|
|
|
*/
|
2006-04-19 08:52:15 +00:00
|
|
|
int ast_context_add_ignorepat(const char *context, const char *value, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-04-19 08:52:15 +00:00
|
|
|
int ret = -1;
|
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
if (c) {
|
|
|
|
ret = ast_context_add_ignorepat2(c, value, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-04-19 08:52:15 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
struct ast_ignorepat *ignorepat, *ignorepatc, *ignorepatl = NULL;
|
2004-10-03 04:19:59 +00:00
|
|
|
int length;
|
|
|
|
length = sizeof(struct ast_ignorepat);
|
|
|
|
length += strlen(value) + 1;
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(ignorepat = ast_calloc(1, length)))
|
2001-09-28 21:20:52 +00:00
|
|
|
return -1;
|
2006-01-21 23:05:19 +00:00
|
|
|
/* The cast to char * is because we need to write the initial value.
|
|
|
|
* The field is not supposed to be modified otherwise
|
|
|
|
*/
|
|
|
|
strcpy((char *)ignorepat->pattern, value);
|
2001-09-28 21:20:52 +00:00
|
|
|
ignorepat->next = NULL;
|
|
|
|
ignorepat->registrar = registrar;
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2005-12-24 06:23:36 +00:00
|
|
|
for (ignorepatc = con->ignorepats; ignorepatc; ignorepatc = ignorepatc->next) {
|
2001-09-28 21:20:52 +00:00
|
|
|
ignorepatl = ignorepatc;
|
|
|
|
if (!strcasecmp(ignorepatc->pattern, value)) {
|
2001-03-30 18:47:35 +00:00
|
|
|
/* Already there */
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2001-09-28 21:20:52 +00:00
|
|
|
errno = EEXIST;
|
|
|
|
return -1;
|
2001-03-30 18:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
if (ignorepatl)
|
2001-09-28 21:20:52 +00:00
|
|
|
ignorepatl->next = ignorepat;
|
2001-03-30 18:47:35 +00:00
|
|
|
else
|
2001-09-28 21:20:52 +00:00
|
|
|
con->ignorepats = ignorepat;
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2001-03-30 18:47:35 +00:00
|
|
|
return 0;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2001-03-30 18:47:35 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_ignore_pattern(const char *context, const char *pattern)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-05-08 22:47:03 +00:00
|
|
|
struct ast_context *con = ast_context_find(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
if (con) {
|
2006-05-08 22:47:03 +00:00
|
|
|
struct ast_ignorepat *pat;
|
2005-12-24 06:23:36 +00:00
|
|
|
for (pat = con->ignorepats; pat; pat = pat->next) {
|
2001-09-28 21:20:52 +00:00
|
|
|
if (ast_extension_match(pat->pattern, pattern))
|
|
|
|
return 1;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
}
|
2006-05-08 22:47:03 +00:00
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EBUSY - can't lock
|
2003-04-23 19:13:35 +00:00
|
|
|
* ENOENT - no existence of context
|
2001-09-28 21:20:52 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-04-19 08:52:15 +00:00
|
|
|
int ast_add_extension(const char *context, int replace, const char *extension,
|
|
|
|
int priority, const char *label, const char *callerid,
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *application, void *data, void (*datad)(void *), const char *registrar)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2006-04-19 08:52:15 +00:00
|
|
|
int ret = -1;
|
|
|
|
struct ast_context *c = find_context_locked(context);
|
2001-09-28 21:20:52 +00:00
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
if (c) {
|
|
|
|
ret = ast_add_extension2(c, replace, extension, priority, label, callerid,
|
|
|
|
application, data, datad, registrar);
|
|
|
|
ast_unlock_contexts();
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
2006-04-19 08:52:15 +00:00
|
|
|
return ret;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2005-04-29 15:04:26 +00:00
|
|
|
int ast_explicit_goto(struct ast_channel *chan, const char *context, const char *exten, int priority)
|
|
|
|
{
|
|
|
|
if (!chan)
|
|
|
|
return -1;
|
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (!ast_strlen_zero(context))
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(chan->context, context, sizeof(chan->context));
|
2005-10-26 18:54:24 +00:00
|
|
|
if (!ast_strlen_zero(exten))
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(chan->exten, exten, sizeof(chan->exten));
|
2005-04-29 15:04:26 +00:00
|
|
|
if (priority > -1) {
|
|
|
|
chan->priority = priority;
|
|
|
|
/* see flag description in channel.h for explanation */
|
|
|
|
if (ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP))
|
|
|
|
chan->priority--;
|
2004-11-23 20:25:02 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-04-29 15:04:26 +00:00
|
|
|
return 0;
|
2004-11-23 20:25:02 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_async_goto(struct ast_channel *chan, const char *context, const char *exten, int priority)
|
2002-09-11 17:09:48 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
2005-04-29 15:04:26 +00:00
|
|
|
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_lock(chan);
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2006-05-08 22:47:03 +00:00
|
|
|
if (chan->pbx) { /* This channel is currently in the PBX */
|
2005-04-29 15:04:26 +00:00
|
|
|
ast_explicit_goto(chan, context, exten, priority);
|
2002-09-11 17:09:48 +00:00
|
|
|
ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO);
|
|
|
|
} else {
|
|
|
|
/* In order to do it when the channel doesn't really exist within
|
|
|
|
the PBX, we have to make a new channel, masquerade, and start the PBX
|
|
|
|
at the new location */
|
2007-04-10 05:41:34 +00:00
|
|
|
struct ast_channel *tmpchan = ast_channel_alloc(0, chan->_state, 0, 0, chan->accountcode, chan->exten, chan->context, chan->amaflags, "AsyncGoto/%s", chan->name);
|
2007-03-30 14:37:21 +00:00
|
|
|
if (chan->cdr) {
|
|
|
|
tmpchan->cdr = ast_cdr_dup(chan->cdr);
|
|
|
|
}
|
2006-04-21 12:12:42 +00:00
|
|
|
if (!tmpchan)
|
|
|
|
res = -1;
|
|
|
|
else {
|
2002-09-11 17:09:48 +00:00
|
|
|
/* Make formats okay */
|
|
|
|
tmpchan->readformat = chan->readformat;
|
|
|
|
tmpchan->writeformat = chan->writeformat;
|
|
|
|
/* Setup proper location */
|
2004-11-23 20:25:02 +00:00
|
|
|
ast_explicit_goto(tmpchan,
|
2006-04-21 12:12:42 +00:00
|
|
|
S_OR(context, chan->context), S_OR(exten, chan->exten), priority);
|
2004-11-23 20:25:02 +00:00
|
|
|
|
2002-09-11 17:09:48 +00:00
|
|
|
/* Masquerade into temp channel */
|
|
|
|
ast_channel_masquerade(tmpchan, chan);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-03-24 05:01:44 +00:00
|
|
|
/* Grab the locks and get going */
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_lock(tmpchan);
|
2004-04-06 22:17:32 +00:00
|
|
|
ast_do_masquerade(tmpchan);
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(tmpchan);
|
2002-09-11 17:09:48 +00:00
|
|
|
/* Start the PBX going on our stolen channel */
|
|
|
|
if (ast_pbx_start(tmpchan)) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmpchan->name);
|
|
|
|
ast_hangup(tmpchan);
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2002-09-11 17:09:48 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
int ast_async_goto_by_name(const char *channame, const char *context, const char *exten, int priority)
|
2002-09-11 17:09:48 +00:00
|
|
|
{
|
|
|
|
struct ast_channel *chan;
|
2004-05-20 16:30:10 +00:00
|
|
|
int res = -1;
|
|
|
|
|
2005-06-06 02:29:18 +00:00
|
|
|
chan = ast_get_channel_by_name_locked(channame);
|
2004-05-20 16:30:10 +00:00
|
|
|
if (chan) {
|
|
|
|
res = ast_async_goto(chan, context, exten, priority);
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2004-05-20 16:30:10 +00:00
|
|
|
}
|
|
|
|
return res;
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
|
2006-04-19 08:52:15 +00:00
|
|
|
/*! \brief copy a string skipping whitespace */
|
2004-12-19 17:16:39 +00:00
|
|
|
static int ext_strncpy(char *dst, const char *src, int len)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
int count=0;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
while (*src && (count < len - 1)) {
|
2007-01-23 00:11:32 +00:00
|
|
|
switch (*src) {
|
2001-09-28 21:20:52 +00:00
|
|
|
case ' ':
|
2004-07-17 02:25:53 +00:00
|
|
|
/* otherwise exten => [a-b],1,... doesn't work */
|
|
|
|
/* case '-': */
|
2001-09-28 21:20:52 +00:00
|
|
|
/* Ignore */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*dst = *src;
|
|
|
|
dst++;
|
|
|
|
}
|
|
|
|
src++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
2004-12-19 17:16:39 +00:00
|
|
|
|
|
|
|
return count;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 22:14:56 +00:00
|
|
|
/*! \brief add the extension in the priority chain.
|
|
|
|
* returns 0 on success, -1 on failure
|
|
|
|
*/
|
2006-05-09 08:31:42 +00:00
|
|
|
static int add_pri(struct ast_context *con, struct ast_exten *tmp,
|
|
|
|
struct ast_exten *el, struct ast_exten *e, int replace)
|
|
|
|
{
|
|
|
|
struct ast_exten *ep;
|
|
|
|
|
|
|
|
for (ep = NULL; e ; ep = e, e = e->peer) {
|
|
|
|
if (e->priority >= tmp->priority)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!e) { /* go at the end, and ep is surely set because the list is not empty */
|
|
|
|
ep->peer = tmp;
|
|
|
|
return 0; /* success */
|
|
|
|
}
|
|
|
|
if (e->priority == tmp->priority) {
|
|
|
|
/* Can't have something exactly the same. Is this a
|
|
|
|
replacement? If so, replace, otherwise, bonk. */
|
|
|
|
if (!replace) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to register extension '%s', priority %d in '%s', already in use\n", tmp->exten, tmp->priority, con->name);
|
2007-01-06 00:28:16 +00:00
|
|
|
if (tmp->datad)
|
|
|
|
tmp->datad(tmp->data);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(tmp);
|
2006-05-09 08:31:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* we are replacing e, so copy the link fields and then update
|
|
|
|
* whoever pointed to e to point to us
|
|
|
|
*/
|
|
|
|
tmp->next = e->next; /* not meaningful if we are not first in the peer list */
|
|
|
|
tmp->peer = e->peer; /* always meaningful */
|
|
|
|
if (ep) /* We're in the peer list, just insert ourselves */
|
|
|
|
ep->peer = tmp;
|
|
|
|
else if (el) /* We're the first extension. Take over e's functions */
|
|
|
|
el->next = tmp;
|
|
|
|
else /* We're the very first extension. */
|
|
|
|
con->root = tmp;
|
|
|
|
if (tmp->priority == PRIORITY_HINT)
|
|
|
|
ast_change_hint(e,tmp);
|
|
|
|
/* Destroy the old one */
|
2007-01-06 00:28:16 +00:00
|
|
|
if (e->datad)
|
|
|
|
e->datad(e->data);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(e);
|
2006-05-09 08:31:42 +00:00
|
|
|
} else { /* Slip ourselves in just before e */
|
|
|
|
tmp->peer = e;
|
|
|
|
tmp->next = e->next; /* extension chain, or NULL if e is not the first extension */
|
|
|
|
if (ep) /* Easy enough, we're just in the peer list */
|
|
|
|
ep->peer = tmp;
|
|
|
|
else { /* we are the first in some peer list, so link in the ext list */
|
|
|
|
if (el)
|
|
|
|
el->next = tmp; /* in the middle... */
|
|
|
|
else
|
|
|
|
con->root = tmp; /* ... or at the head */
|
|
|
|
e->next = NULL; /* e is no more at the head, so e->next must be reset */
|
|
|
|
}
|
|
|
|
/* And immediately return success. */
|
|
|
|
if (tmp->priority == PRIORITY_HINT)
|
|
|
|
ast_add_hint(tmp);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-05-08 15:32:53 +00:00
|
|
|
/*! \brief
|
|
|
|
* Main interface to add extensions to the list for out context.
|
|
|
|
*
|
|
|
|
* We sort extensions in order of matching preference, so that we can
|
|
|
|
* stop the search as soon as we find a suitable match.
|
|
|
|
* This ordering also takes care of wildcards such as '.' (meaning
|
|
|
|
* "one or more of any character") and '!' (which is 'earlymatch',
|
|
|
|
* meaning "zero or more of any character" but also impacts the
|
|
|
|
* return value from CANMATCH and EARLYMATCH.
|
2006-05-09 08:03:52 +00:00
|
|
|
*
|
2006-05-08 15:32:53 +00:00
|
|
|
* The extension match rules defined in the devmeeting 2006.05.05 are
|
|
|
|
* quite simple: WE SELECT THE LONGEST MATCH.
|
|
|
|
* In detail, "longest" means the number of matched characters in
|
|
|
|
* the extension. In case of ties (e.g. _XXX and 333) in the length
|
|
|
|
* of a pattern, we give priority to entries with the smallest cardinality
|
|
|
|
* (e.g, [5-9] comes before [2-8] before the former has only 5 elements,
|
|
|
|
* while the latter has 7, etc.
|
|
|
|
* In case of same cardinality, the first element in the range counts.
|
|
|
|
* If we still have a tie, any final '!' will make this as a possibly
|
|
|
|
* less specific pattern.
|
|
|
|
*
|
2001-09-28 21:20:52 +00:00
|
|
|
* EBUSY - can't lock
|
|
|
|
* EEXIST - extension with the same priority exist and no replace is set
|
|
|
|
*
|
|
|
|
*/
|
1999-12-04 20:45:45 +00:00
|
|
|
int ast_add_extension2(struct ast_context *con,
|
2006-05-08 15:32:53 +00:00
|
|
|
int replace, const char *extension, int priority, const char *label, const char *callerid,
|
|
|
|
const char *application, void *data, void (*datad)(void *),
|
|
|
|
const char *registrar)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
|
|
|
/*
|
2006-05-09 18:34:30 +00:00
|
|
|
* Sort extensions (or patterns) according to the rules indicated above.
|
|
|
|
* These are implemented by the function ext_cmp()).
|
|
|
|
* All priorities for the same ext/pattern/cid are kept in a list,
|
|
|
|
* using the 'peer' field as a link field..
|
1999-12-04 20:45:45 +00:00
|
|
|
*/
|
2006-05-09 08:31:42 +00:00
|
|
|
struct ast_exten *tmp, *e, *el = NULL;
|
1999-12-04 20:45:45 +00:00
|
|
|
int res;
|
2004-10-03 04:19:59 +00:00
|
|
|
int length;
|
|
|
|
char *p;
|
2005-12-08 01:06:09 +00:00
|
|
|
char expand_buf[VAR_BUF_SIZE] = { 0, };
|
2005-12-07 20:47:59 +00:00
|
|
|
|
|
|
|
/* if we are adding a hint, and there are global variables, and the hint
|
|
|
|
contains variable references, then expand them
|
|
|
|
*/
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_rdlock(&globalslock);
|
2006-05-10 22:14:56 +00:00
|
|
|
if (priority == PRIORITY_HINT && AST_LIST_FIRST(&globals) && strstr(application, "${")) {
|
2005-12-07 20:47:59 +00:00
|
|
|
pbx_substitute_variables_varshead(&globals, application, expand_buf, sizeof(expand_buf));
|
|
|
|
application = expand_buf;
|
|
|
|
}
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_unlock(&globalslock);
|
2005-12-07 20:47:59 +00:00
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
length = sizeof(struct ast_exten);
|
|
|
|
length += strlen(extension) + 1;
|
|
|
|
length += strlen(application) + 1;
|
|
|
|
if (label)
|
|
|
|
length += strlen(label) + 1;
|
|
|
|
if (callerid)
|
|
|
|
length += strlen(callerid) + 1;
|
|
|
|
else
|
2006-04-21 12:12:42 +00:00
|
|
|
length ++; /* just the '\0' */
|
2004-07-17 02:25:53 +00:00
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
/* Be optimistic: Build the extension structure first */
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(tmp = ast_calloc(1, length)))
|
1999-12-04 20:45:45 +00:00
|
|
|
return -1;
|
2006-05-09 07:49:17 +00:00
|
|
|
|
2006-03-29 19:54:41 +00:00
|
|
|
/* use p as dst in assignments, as the fields are const char * */
|
|
|
|
p = tmp->stuff;
|
|
|
|
if (label) {
|
|
|
|
tmp->label = p;
|
|
|
|
strcpy(p, label);
|
|
|
|
p += strlen(label) + 1;
|
|
|
|
}
|
|
|
|
tmp->exten = p;
|
|
|
|
p += ext_strncpy(p, extension, strlen(extension) + 1) + 1;
|
|
|
|
tmp->priority = priority;
|
|
|
|
tmp->cidmatch = p; /* but use p for assignments below */
|
|
|
|
if (callerid) {
|
|
|
|
p += ext_strncpy(p, callerid, strlen(callerid) + 1) + 1;
|
|
|
|
tmp->matchcid = 1;
|
|
|
|
} else {
|
|
|
|
*p++ = '\0';
|
|
|
|
tmp->matchcid = 0;
|
|
|
|
}
|
|
|
|
tmp->app = p;
|
|
|
|
strcpy(p, application);
|
|
|
|
tmp->parent = con;
|
|
|
|
tmp->data = data;
|
|
|
|
tmp->datad = datad;
|
|
|
|
tmp->registrar = registrar;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(con);
|
2006-05-10 07:13:23 +00:00
|
|
|
res = 0; /* some compilers will think it is uninitialized otherwise */
|
2006-05-09 06:04:45 +00:00
|
|
|
for (e = con->root; e; el = e, e = e->next) { /* scan the extension list */
|
2006-05-09 18:34:30 +00:00
|
|
|
res = ext_cmp(e->exten, extension);
|
2006-05-09 06:04:45 +00:00
|
|
|
if (res == 0) { /* extension match, now look at cidmatch */
|
2001-09-28 21:20:52 +00:00
|
|
|
if (!e->matchcid && !tmp->matchcid)
|
|
|
|
res = 0;
|
|
|
|
else if (tmp->matchcid && !e->matchcid)
|
|
|
|
res = 1;
|
|
|
|
else if (e->matchcid && !tmp->matchcid)
|
|
|
|
res = -1;
|
|
|
|
else
|
|
|
|
res = strcasecmp(e->cidmatch, tmp->cidmatch);
|
|
|
|
}
|
2006-05-09 08:31:42 +00:00
|
|
|
if (res >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (e && res == 0) { /* exact match, insert in the pri chain */
|
2006-05-10 22:14:56 +00:00
|
|
|
res = add_pri(con, tmp, el, e, replace);
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2006-05-10 22:14:56 +00:00
|
|
|
if (res < 0) {
|
|
|
|
errno = EEXIST; /* XXX do we care ? */
|
|
|
|
return 0; /* XXX should we return -1 maybe ? */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* not an exact match, this is the first entry with this pattern,
|
|
|
|
* so insert in the main list right before 'e' (if any)
|
|
|
|
*/
|
|
|
|
tmp->next = e;
|
|
|
|
if (el)
|
|
|
|
el->next = tmp;
|
|
|
|
else
|
|
|
|
con->root = tmp;
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(con);
|
2006-05-10 22:14:56 +00:00
|
|
|
if (tmp->priority == PRIORITY_HINT)
|
|
|
|
ast_add_hint(tmp);
|
|
|
|
}
|
|
|
|
if (option_debug) {
|
|
|
|
if (tmp->matchcid) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Added extension '%s' priority %d (CID match '%s') to %s\n",
|
|
|
|
tmp->exten, tmp->priority, tmp->cidmatch, con->name);
|
2006-05-10 22:14:56 +00:00
|
|
|
} else {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Added extension '%s' priority %d to %s\n",
|
|
|
|
tmp->exten, tmp->priority, con->name);
|
2006-05-10 22:14:56 +00:00
|
|
|
}
|
2006-09-19 16:23:45 +00:00
|
|
|
}
|
2007-07-23 14:21:41 +00:00
|
|
|
|
|
|
|
if (tmp->matchcid) {
|
|
|
|
ast_verb(3, "Added extension '%s' priority %d (CID match '%s')to %s\n",
|
|
|
|
tmp->exten, tmp->priority, tmp->cidmatch, con->name);
|
|
|
|
} else {
|
|
|
|
ast_verb(3, "Added extension '%s' priority %d to %s\n",
|
|
|
|
tmp->exten, tmp->priority, con->name);
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2007-07-23 14:21:41 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
return 0;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2002-09-11 17:09:48 +00:00
|
|
|
struct async_stat {
|
|
|
|
pthread_t p;
|
|
|
|
struct ast_channel *chan;
|
2005-07-10 23:49:57 +00:00
|
|
|
char context[AST_MAX_CONTEXT];
|
2002-09-11 17:09:48 +00:00
|
|
|
char exten[AST_MAX_EXTENSION];
|
|
|
|
int priority;
|
|
|
|
int timeout;
|
|
|
|
char app[AST_MAX_EXTENSION];
|
2003-02-02 19:04:14 +00:00
|
|
|
char appdata[1024];
|
2002-09-11 17:09:48 +00:00
|
|
|
};
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
static void *async_wait(void *data)
|
2002-09-11 17:09:48 +00:00
|
|
|
{
|
|
|
|
struct async_stat *as = data;
|
|
|
|
struct ast_channel *chan = as->chan;
|
|
|
|
int timeout = as->timeout;
|
|
|
|
int res;
|
|
|
|
struct ast_frame *f;
|
2003-02-02 19:04:14 +00:00
|
|
|
struct ast_app *app;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
while (timeout && (chan->_state != AST_STATE_UP)) {
|
2002-09-11 17:09:48 +00:00
|
|
|
res = ast_waitfor(chan, timeout);
|
2006-05-09 08:03:52 +00:00
|
|
|
if (res < 1)
|
2002-09-11 17:09:48 +00:00
|
|
|
break;
|
|
|
|
if (timeout > -1)
|
|
|
|
timeout = res;
|
|
|
|
f = ast_read(chan);
|
|
|
|
if (!f)
|
|
|
|
break;
|
|
|
|
if (f->frametype == AST_FRAME_CONTROL) {
|
|
|
|
if ((f->subclass == AST_CONTROL_BUSY) ||
|
2006-06-11 21:23:16 +00:00
|
|
|
(f->subclass == AST_CONTROL_CONGESTION) ) {
|
|
|
|
ast_frfree(f);
|
|
|
|
break;
|
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
ast_frfree(f);
|
|
|
|
}
|
|
|
|
if (chan->_state == AST_STATE_UP) {
|
2004-05-04 06:34:34 +00:00
|
|
|
if (!ast_strlen_zero(as->app)) {
|
2003-02-02 19:04:14 +00:00
|
|
|
app = pbx_findapp(as->app);
|
|
|
|
if (app) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Launching %s(%s) on %s\n", as->app, as->appdata, chan->name);
|
2006-03-30 21:29:39 +00:00
|
|
|
pbx_exec(chan, app, as->appdata);
|
2003-02-02 19:04:14 +00:00
|
|
|
} else
|
|
|
|
ast_log(LOG_WARNING, "No such application '%s'\n", as->app);
|
2002-09-11 17:09:48 +00:00
|
|
|
} else {
|
2004-05-04 06:34:34 +00:00
|
|
|
if (!ast_strlen_zero(as->context))
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(chan->context, as->context, sizeof(chan->context));
|
2004-05-04 06:34:34 +00:00
|
|
|
if (!ast_strlen_zero(as->exten))
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(chan->exten, as->exten, sizeof(chan->exten));
|
2003-02-02 19:04:14 +00:00
|
|
|
if (as->priority > 0)
|
|
|
|
chan->priority = as->priority;
|
|
|
|
/* Run the PBX */
|
|
|
|
if (ast_pbx_run(chan)) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_ERROR, "Failed to start PBX on %s\n", chan->name);
|
2003-02-02 19:04:14 +00:00
|
|
|
} else {
|
|
|
|
/* PBX will have taken care of this */
|
|
|
|
chan = NULL;
|
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(as);
|
2002-09-11 17:09:48 +00:00
|
|
|
if (chan)
|
|
|
|
ast_hangup(chan);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-22 21:05:24 +00:00
|
|
|
/*! Function to post an empty cdr after a spool call fails.
|
2004-10-15 17:51:37 +00:00
|
|
|
*
|
2006-03-22 21:05:24 +00:00
|
|
|
* This function posts an empty cdr for a failed spool call
|
2004-10-15 17:51:37 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-07-22 00:38:30 +00:00
|
|
|
static int ast_pbx_outgoing_cdr_failed(void)
|
2004-10-15 17:51:37 +00:00
|
|
|
{
|
|
|
|
/* allocate a channel */
|
2007-04-10 05:41:34 +00:00
|
|
|
struct ast_channel *chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, 0);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!chan)
|
2004-10-15 17:51:37 +00:00
|
|
|
return -1; /* failure */
|
|
|
|
|
2005-12-24 06:23:36 +00:00
|
|
|
if (!chan->cdr) {
|
2004-10-15 17:51:37 +00:00
|
|
|
/* allocation of the cdr failed */
|
|
|
|
ast_channel_free(chan); /* free the channel */
|
|
|
|
return -1; /* return failure */
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-10-15 17:51:37 +00:00
|
|
|
/* allocation of the cdr was successful */
|
2006-10-26 17:52:15 +00:00
|
|
|
ast_cdr_init(chan->cdr, chan); /* initialize our channel's cdr */
|
2004-10-15 17:51:37 +00:00
|
|
|
ast_cdr_start(chan->cdr); /* record the start and stop time */
|
|
|
|
ast_cdr_end(chan->cdr);
|
|
|
|
ast_cdr_failed(chan->cdr); /* set the status to failed */
|
2005-06-03 01:42:31 +00:00
|
|
|
ast_cdr_detach(chan->cdr); /* post and free the record */
|
2004-10-15 17:51:37 +00:00
|
|
|
ast_channel_free(chan); /* free the channel */
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-10-15 17:51:37 +00:00
|
|
|
return 0; /* success */
|
|
|
|
}
|
|
|
|
|
2006-02-11 18:31:59 +00:00
|
|
|
int ast_pbx_outgoing_exten(const char *type, int format, void *data, int timeout, const char *context, const char *exten, int priority, int *reason, int sync, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **channel)
|
2002-09-11 17:09:48 +00:00
|
|
|
{
|
|
|
|
struct ast_channel *chan;
|
|
|
|
struct async_stat *as;
|
2004-10-15 17:51:37 +00:00
|
|
|
int res = -1, cdr_res = -1;
|
2003-09-22 15:27:09 +00:00
|
|
|
struct outgoing_helper oh;
|
2005-07-15 23:24:51 +00:00
|
|
|
|
2002-09-11 17:09:48 +00:00
|
|
|
if (sync) {
|
2006-12-15 03:59:31 +00:00
|
|
|
oh.context = context;
|
|
|
|
oh.exten = exten;
|
|
|
|
oh.priority = priority;
|
|
|
|
oh.cid_num = cid_num;
|
|
|
|
oh.cid_name = cid_name;
|
|
|
|
oh.account = account;
|
|
|
|
oh.vars = vars;
|
|
|
|
oh.parent_channel = NULL;
|
|
|
|
|
2004-10-02 00:58:31 +00:00
|
|
|
chan = __ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name, &oh);
|
2005-02-02 02:54:15 +00:00
|
|
|
if (channel) {
|
|
|
|
*channel = chan;
|
|
|
|
if (chan)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_lock(chan);
|
2005-02-02 02:54:15 +00:00
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
if (chan) {
|
|
|
|
if (chan->_state == AST_STATE_UP) {
|
2003-09-22 15:27:09 +00:00
|
|
|
res = 0;
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(4, "Channel %s was answered.\n", chan->name);
|
2003-09-22 15:27:09 +00:00
|
|
|
|
2003-02-02 19:04:14 +00:00
|
|
|
if (sync > 1) {
|
2005-09-01 20:15:58 +00:00
|
|
|
if (channel)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2003-02-02 19:04:14 +00:00
|
|
|
if (ast_pbx_run(chan)) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_ERROR, "Unable to run PBX on %s\n", chan->name);
|
2005-10-04 17:41:30 +00:00
|
|
|
if (channel)
|
|
|
|
*channel = NULL;
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_hangup(chan);
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ast_pbx_start(chan)) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_ERROR, "Unable to start PBX on %s\n", chan->name);
|
2006-04-05 20:05:09 +00:00
|
|
|
if (channel) {
|
2005-10-04 17:41:30 +00:00
|
|
|
*channel = NULL;
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2006-04-05 20:05:09 +00:00
|
|
|
}
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_hangup(chan);
|
|
|
|
res = -1;
|
2006-05-09 08:03:52 +00:00
|
|
|
}
|
2003-02-02 19:04:14 +00:00
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
} else {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(4, "Channel %s was never answered.\n", chan->name);
|
2004-10-15 17:51:37 +00:00
|
|
|
|
2007-01-23 00:11:32 +00:00
|
|
|
if (chan->cdr) { /* update the cdr */
|
2004-10-15 17:51:37 +00:00
|
|
|
/* here we update the status of the call, which sould be busy.
|
|
|
|
* if that fails then we set the status to failed */
|
2005-07-15 23:24:51 +00:00
|
|
|
if (ast_cdr_disposition(chan->cdr, chan->hangupcause))
|
2004-10-15 17:51:37 +00:00
|
|
|
ast_cdr_failed(chan->cdr);
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-04-05 20:05:09 +00:00
|
|
|
if (channel) {
|
2005-10-04 17:41:30 +00:00
|
|
|
*channel = NULL;
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2006-04-05 20:05:09 +00:00
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
ast_hangup(chan);
|
|
|
|
}
|
2004-06-23 20:46:58 +00:00
|
|
|
}
|
|
|
|
|
2006-04-05 20:05:09 +00:00
|
|
|
if (res < 0) { /* the call failed for some reason */
|
2005-07-15 23:24:51 +00:00
|
|
|
if (*reason == 0) { /* if the call failed (not busy or no answer)
|
2004-10-15 17:51:37 +00:00
|
|
|
* update the cdr with the failed message */
|
|
|
|
cdr_res = ast_pbx_outgoing_cdr_failed();
|
2005-07-15 23:24:51 +00:00
|
|
|
if (cdr_res != 0) {
|
|
|
|
res = cdr_res;
|
|
|
|
goto outgoing_exten_cleanup;
|
|
|
|
}
|
2004-10-15 17:51:37 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-09-12 16:51:35 +00:00
|
|
|
/* create a fake channel and execute the "failed" extension (if it exists) within the requested context */
|
|
|
|
/* check if "failed" exists */
|
|
|
|
if (ast_exists_extension(chan, context, "failed", 1, NULL)) {
|
2007-04-10 05:41:34 +00:00
|
|
|
chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "OutgoingSpoolFailed");
|
2005-07-15 23:24:51 +00:00
|
|
|
if (chan) {
|
2005-10-26 18:54:24 +00:00
|
|
|
if (!ast_strlen_zero(context))
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(chan->context, context, sizeof(chan->context));
|
2006-04-19 09:18:30 +00:00
|
|
|
set_ext_pri(chan, "failed", 1);
|
2005-07-15 23:24:51 +00:00
|
|
|
ast_set_variables(chan, vars);
|
2006-02-11 18:31:59 +00:00
|
|
|
if (account)
|
|
|
|
ast_cdr_setaccount(chan, account);
|
2006-05-09 08:03:52 +00:00
|
|
|
ast_pbx_run(chan);
|
|
|
|
}
|
2003-09-12 16:51:35 +00:00
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(as = ast_calloc(1, sizeof(*as)))) {
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
|
|
|
goto outgoing_exten_cleanup;
|
2006-05-09 08:03:52 +00:00
|
|
|
}
|
2004-10-02 00:58:31 +00:00
|
|
|
chan = ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name);
|
2005-02-02 02:54:15 +00:00
|
|
|
if (channel) {
|
|
|
|
*channel = chan;
|
|
|
|
if (chan)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_lock(chan);
|
2005-02-02 02:54:15 +00:00
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
if (!chan) {
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(as);
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
|
|
|
goto outgoing_exten_cleanup;
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
as->chan = chan;
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(as->context, context, sizeof(as->context));
|
2006-04-19 09:18:30 +00:00
|
|
|
set_ext_pri(as->chan, exten, priority);
|
2002-09-11 17:09:48 +00:00
|
|
|
as->timeout = timeout;
|
2005-07-15 23:24:51 +00:00
|
|
|
ast_set_variables(chan, vars);
|
2006-02-11 18:31:59 +00:00
|
|
|
if (account)
|
|
|
|
ast_cdr_setaccount(chan, account);
|
2007-05-24 18:30:19 +00:00
|
|
|
if (ast_pthread_create_detached(&as->p, NULL, async_wait, as)) {
|
2002-09-11 17:09:48 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to start async wait\n");
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(as);
|
2006-04-05 20:05:09 +00:00
|
|
|
if (channel) {
|
2005-10-04 17:41:30 +00:00
|
|
|
*channel = NULL;
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2006-04-05 20:05:09 +00:00
|
|
|
}
|
2002-09-11 17:09:48 +00:00
|
|
|
ast_hangup(chan);
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
|
|
|
goto outgoing_exten_cleanup;
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
}
|
2005-07-15 23:24:51 +00:00
|
|
|
outgoing_exten_cleanup:
|
|
|
|
ast_variables_destroy(vars);
|
2002-09-11 17:09:48 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-02-02 19:04:14 +00:00
|
|
|
struct app_tmp {
|
|
|
|
char app[256];
|
|
|
|
char data[256];
|
|
|
|
struct ast_channel *chan;
|
|
|
|
pthread_t t;
|
|
|
|
};
|
|
|
|
|
2006-04-21 11:19:48 +00:00
|
|
|
/*! \brief run the application and free the descriptor once done */
|
2003-02-02 19:04:14 +00:00
|
|
|
static void *ast_pbx_run_app(void *data)
|
|
|
|
{
|
|
|
|
struct app_tmp *tmp = data;
|
|
|
|
struct ast_app *app;
|
|
|
|
app = pbx_findapp(tmp->app);
|
|
|
|
if (app) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(4, "Launching %s(%s) on %s\n", tmp->app, tmp->data, tmp->chan->name);
|
2006-03-30 21:29:39 +00:00
|
|
|
pbx_exec(tmp->chan, app, tmp->data);
|
2003-02-02 19:04:14 +00:00
|
|
|
} else
|
|
|
|
ast_log(LOG_WARNING, "No such application '%s'\n", tmp->app);
|
|
|
|
ast_hangup(tmp->chan);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(tmp);
|
2003-02-02 19:04:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-11 18:31:59 +00:00
|
|
|
int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout, const char *app, const char *appdata, int *reason, int sync, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel)
|
2003-02-02 19:04:14 +00:00
|
|
|
{
|
|
|
|
struct ast_channel *chan;
|
|
|
|
struct app_tmp *tmp;
|
2004-10-15 17:51:37 +00:00
|
|
|
int res = -1, cdr_res = -1;
|
2005-07-15 23:24:51 +00:00
|
|
|
struct outgoing_helper oh;
|
2006-02-11 18:31:59 +00:00
|
|
|
|
2005-07-15 23:24:51 +00:00
|
|
|
memset(&oh, 0, sizeof(oh));
|
2006-02-11 18:31:59 +00:00
|
|
|
oh.vars = vars;
|
2006-05-09 08:03:52 +00:00
|
|
|
oh.account = account;
|
2005-07-15 23:24:51 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
if (locked_channel)
|
2005-02-02 02:54:15 +00:00
|
|
|
*locked_channel = NULL;
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(app)) {
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
2006-05-09 08:03:52 +00:00
|
|
|
goto outgoing_app_cleanup;
|
2005-07-15 23:24:51 +00:00
|
|
|
}
|
2003-02-02 19:04:14 +00:00
|
|
|
if (sync) {
|
2005-07-15 23:24:51 +00:00
|
|
|
chan = __ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name, &oh);
|
2003-02-02 19:04:14 +00:00
|
|
|
if (chan) {
|
2005-07-15 23:24:51 +00:00
|
|
|
if (chan->cdr) { /* check if the channel already has a cdr record, if not give it one */
|
2004-10-15 17:51:37 +00:00
|
|
|
ast_log(LOG_WARNING, "%s already has a call record??\n", chan->name);
|
|
|
|
} else {
|
|
|
|
chan->cdr = ast_cdr_alloc(); /* allocate a cdr for the channel */
|
2007-01-23 00:11:32 +00:00
|
|
|
if (!chan->cdr) {
|
2004-10-15 17:51:37 +00:00
|
|
|
/* allocation of the cdr failed */
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(chan->pbx);
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
|
|
|
goto outgoing_app_cleanup;
|
2004-10-15 17:51:37 +00:00
|
|
|
}
|
|
|
|
/* allocation of the cdr was successful */
|
2006-10-26 17:52:15 +00:00
|
|
|
ast_cdr_init(chan->cdr, chan); /* initialize our channel's cdr */
|
2004-10-15 17:51:37 +00:00
|
|
|
ast_cdr_start(chan->cdr);
|
|
|
|
}
|
2005-07-15 23:24:51 +00:00
|
|
|
ast_set_variables(chan, vars);
|
2006-02-11 18:31:59 +00:00
|
|
|
if (account)
|
|
|
|
ast_cdr_setaccount(chan, account);
|
2003-02-02 19:04:14 +00:00
|
|
|
if (chan->_state == AST_STATE_UP) {
|
|
|
|
res = 0;
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(4, "Channel %s was answered.\n", chan->name);
|
2006-04-21 11:19:48 +00:00
|
|
|
tmp = ast_calloc(1, sizeof(*tmp));
|
|
|
|
if (!tmp)
|
|
|
|
res = -1;
|
|
|
|
else {
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(tmp->app, app, sizeof(tmp->app));
|
2005-06-05 16:08:44 +00:00
|
|
|
if (appdata)
|
|
|
|
ast_copy_string(tmp->data, appdata, sizeof(tmp->data));
|
2003-02-02 19:04:14 +00:00
|
|
|
tmp->chan = chan;
|
|
|
|
if (sync > 1) {
|
2005-04-17 03:44:19 +00:00
|
|
|
if (locked_channel)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_pbx_run_app(tmp);
|
|
|
|
} else {
|
2006-05-09 08:03:52 +00:00
|
|
|
if (locked_channel)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_lock(chan);
|
2007-05-24 18:30:19 +00:00
|
|
|
if (ast_pthread_create_detached(&tmp->t, NULL, ast_pbx_run_app, tmp)) {
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to spawn execute thread on %s: %s\n", chan->name, strerror(errno));
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(tmp);
|
2006-05-09 08:03:52 +00:00
|
|
|
if (locked_channel)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_hangup(chan);
|
|
|
|
res = -1;
|
2005-02-02 02:54:15 +00:00
|
|
|
} else {
|
2006-05-09 08:03:52 +00:00
|
|
|
if (locked_channel)
|
2005-02-02 02:54:15 +00:00
|
|
|
*locked_channel = chan;
|
2003-02-02 19:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(4, "Channel %s was never answered.\n", chan->name);
|
2005-07-15 23:24:51 +00:00
|
|
|
if (chan->cdr) { /* update the cdr */
|
2004-10-15 17:51:37 +00:00
|
|
|
/* here we update the status of the call, which sould be busy.
|
|
|
|
* if that fails then we set the status to failed */
|
2005-07-15 23:24:51 +00:00
|
|
|
if (ast_cdr_disposition(chan->cdr, chan->hangupcause))
|
2004-10-15 17:51:37 +00:00
|
|
|
ast_cdr_failed(chan->cdr);
|
|
|
|
}
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_hangup(chan);
|
|
|
|
}
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-07-15 23:24:51 +00:00
|
|
|
if (res < 0) { /* the call failed for some reason */
|
|
|
|
if (*reason == 0) { /* if the call failed (not busy or no answer)
|
2004-10-15 17:51:37 +00:00
|
|
|
* update the cdr with the failed message */
|
|
|
|
cdr_res = ast_pbx_outgoing_cdr_failed();
|
2005-07-15 23:24:51 +00:00
|
|
|
if (cdr_res != 0) {
|
|
|
|
res = cdr_res;
|
|
|
|
goto outgoing_app_cleanup;
|
|
|
|
}
|
2004-10-15 17:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-02 19:04:14 +00:00
|
|
|
} else {
|
2006-04-21 11:19:48 +00:00
|
|
|
struct async_stat *as;
|
2006-03-29 19:54:41 +00:00
|
|
|
if (!(as = ast_calloc(1, sizeof(*as)))) {
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
|
|
|
goto outgoing_app_cleanup;
|
|
|
|
}
|
Merged revisions 7265-7266,7268-7275 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.2
........
r7265 | oej | 2005-12-01 17:18:14 -0600 (Thu, 01 Dec 2005) | 2 lines
Changing bug report address to the Asterisk issue tracker
........
r7266 | kpfleming | 2005-12-01 17:18:29 -0600 (Thu, 01 Dec 2005) | 3 lines
Makefile 'update' target now supports updating from Subversion repositories (issue #5875)
remove support for 'patches' subdirectory, it's no longer useful
........
r7268 | kpfleming | 2005-12-01 17:34:58 -0600 (Thu, 01 Dec 2005) | 2 lines
ensure channel's scheduling context is freed (issue #5788)
........
r7269 | kpfleming | 2005-12-01 17:49:44 -0600 (Thu, 01 Dec 2005) | 2 lines
don't block waiting for the Festival server forever when it goes away (issue #5882)
........
r7270 | kpfleming | 2005-12-01 18:26:12 -0600 (Thu, 01 Dec 2005) | 2 lines
allow variables to exist on both 'halves' of the Local channel (issue #5810)
........
r7271 | kpfleming | 2005-12-01 18:28:48 -0600 (Thu, 01 Dec 2005) | 2 lines
protect agent_bridgedchannel() from segfaulting when there is no bridged channel (issue #5879)
........
r7272 | kpfleming | 2005-12-01 18:39:00 -0600 (Thu, 01 Dec 2005) | 3 lines
properly handle password changes when mailbox is last line of config file and not followed by a newline (issue #5870)
reformat password changing code to conform to coding guidelines (issue #5870)
........
r7273 | kpfleming | 2005-12-01 18:42:40 -0600 (Thu, 01 Dec 2005) | 2 lines
allow previous context-searching behavior to be used if desired (issue #5899)
........
r7274 | kpfleming | 2005-12-01 18:51:15 -0600 (Thu, 01 Dec 2005) | 2 lines
inherit channel variables into channels created by Page() application (issue #5888)
........
r7275 | oej | 2005-12-01 18:52:13 -0600 (Thu, 01 Dec 2005) | 2 lines
Bug #5907. Improve SIP INFO DTMF debugging output. (1.2 & Trunk)
........
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@7276 f38db490-d61c-443f-a65b-d21fe96a405b
2005-12-02 01:01:11 +00:00
|
|
|
chan = __ast_request_and_dial(type, format, data, timeout, reason, cid_num, cid_name, &oh);
|
2003-02-02 19:04:14 +00:00
|
|
|
if (!chan) {
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(as);
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
|
|
|
goto outgoing_app_cleanup;
|
2003-02-02 19:04:14 +00:00
|
|
|
}
|
|
|
|
as->chan = chan;
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(as->app, app, sizeof(as->app));
|
2003-02-02 19:04:14 +00:00
|
|
|
if (appdata)
|
2005-06-05 16:32:16 +00:00
|
|
|
ast_copy_string(as->appdata, appdata, sizeof(as->appdata));
|
2003-02-02 19:04:14 +00:00
|
|
|
as->timeout = timeout;
|
2005-07-15 23:24:51 +00:00
|
|
|
ast_set_variables(chan, vars);
|
2006-02-11 18:31:59 +00:00
|
|
|
if (account)
|
|
|
|
ast_cdr_setaccount(chan, account);
|
2004-04-02 07:24:33 +00:00
|
|
|
/* Start a new thread, and get something handling this channel. */
|
2006-05-09 08:03:52 +00:00
|
|
|
if (locked_channel)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_lock(chan);
|
2007-05-24 18:30:19 +00:00
|
|
|
if (ast_pthread_create_detached(&as->p, NULL, async_wait, as)) {
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to start async wait\n");
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(as);
|
2006-05-09 08:03:52 +00:00
|
|
|
if (locked_channel)
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan);
|
2003-02-02 19:04:14 +00:00
|
|
|
ast_hangup(chan);
|
2005-07-15 23:24:51 +00:00
|
|
|
res = -1;
|
|
|
|
goto outgoing_app_cleanup;
|
2005-02-02 02:54:15 +00:00
|
|
|
} else {
|
|
|
|
if (locked_channel)
|
|
|
|
*locked_channel = chan;
|
2003-02-02 19:04:14 +00:00
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
}
|
2005-07-15 23:24:51 +00:00
|
|
|
outgoing_app_cleanup:
|
|
|
|
ast_variables_destroy(vars);
|
2003-02-02 19:04:14 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
void __ast_context_destroy(struct ast_context *con, const char *registrar)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
|
|
|
struct ast_context *tmp, *tmpl=NULL;
|
2006-05-08 22:06:50 +00:00
|
|
|
struct ast_include *tmpi;
|
2006-05-08 11:20:19 +00:00
|
|
|
struct ast_sw *sw;
|
2003-04-06 18:19:51 +00:00
|
|
|
struct ast_exten *e, *el, *en;
|
2006-05-08 22:06:50 +00:00
|
|
|
struct ast_ignorepat *ipi;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2006-05-08 22:06:50 +00:00
|
|
|
for (tmp = contexts; tmp; ) {
|
|
|
|
struct ast_context *next; /* next starting point */
|
|
|
|
for (; tmp; tmpl = tmp, tmp = tmp->next) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "check ctx %s %s\n", tmp->name, tmp->registrar);
|
2006-05-08 22:06:50 +00:00
|
|
|
if ( (!registrar || !strcasecmp(registrar, tmp->registrar)) &&
|
|
|
|
(!con || !strcasecmp(tmp->name, con->name)) )
|
|
|
|
break; /* found it */
|
|
|
|
}
|
|
|
|
if (!tmp) /* not found, we are done */
|
|
|
|
break;
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_wrlock_context(tmp);
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "delete ctx %s %s\n", tmp->name, tmp->registrar);
|
2006-05-08 22:06:50 +00:00
|
|
|
next = tmp->next;
|
|
|
|
if (tmpl)
|
|
|
|
tmpl->next = next;
|
|
|
|
else
|
|
|
|
contexts = next;
|
|
|
|
/* Okay, now we're safe to let it go -- in a sense, we were
|
|
|
|
ready to let it go as soon as we locked it. */
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_unlock_context(tmp);
|
2006-05-08 22:06:50 +00:00
|
|
|
for (tmpi = tmp->includes; tmpi; ) { /* Free includes */
|
|
|
|
struct ast_include *tmpil = tmpi;
|
|
|
|
tmpi = tmpi->next;
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(tmpil);
|
2006-05-08 22:06:50 +00:00
|
|
|
}
|
|
|
|
for (ipi = tmp->ignorepats; ipi; ) { /* Free ignorepats */
|
|
|
|
struct ast_ignorepat *ipl = ipi;
|
|
|
|
ipi = ipi->next;
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(ipl);
|
2006-05-08 22:06:50 +00:00
|
|
|
}
|
|
|
|
while ((sw = AST_LIST_REMOVE_HEAD(&tmp->alts, list)))
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(sw);
|
2006-05-08 22:06:50 +00:00
|
|
|
for (e = tmp->root; e;) {
|
|
|
|
for (en = e->peer; en;) {
|
|
|
|
el = en;
|
|
|
|
en = en->peer;
|
2003-04-06 18:19:51 +00:00
|
|
|
destroy_exten(el);
|
|
|
|
}
|
2006-05-08 22:06:50 +00:00
|
|
|
el = e;
|
|
|
|
e = e->next;
|
|
|
|
destroy_exten(el);
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2007-02-28 20:46:01 +00:00
|
|
|
ast_rwlock_destroy(&tmp->lock);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(tmp);
|
2006-05-08 22:06:50 +00:00
|
|
|
/* if we have a specific match, we are done, otherwise continue */
|
|
|
|
tmp = con ? NULL : next;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2003-07-14 15:35:21 +00:00
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
void ast_context_destroy(struct ast_context *con, const char *registrar)
|
2003-07-14 15:35:21 +00:00
|
|
|
{
|
2007-03-02 18:05:29 +00:00
|
|
|
ast_wrlock_contexts();
|
2004-04-06 22:17:32 +00:00
|
|
|
__ast_context_destroy(con,registrar);
|
2007-03-02 18:05:29 +00:00
|
|
|
ast_unlock_contexts();
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2004-07-09 08:32:09 +00:00
|
|
|
static void wait_for_hangup(struct ast_channel *chan, void *data)
|
2001-04-27 15:07:56 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct ast_frame *f;
|
2006-10-05 01:40:06 +00:00
|
|
|
double waitsec;
|
2004-07-09 08:32:09 +00:00
|
|
|
int waittime;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-10-05 01:40:06 +00:00
|
|
|
if (ast_strlen_zero(data) || (sscanf(data, "%lg", &waitsec) != 1) || (waitsec < 0))
|
|
|
|
waitsec = -1;
|
|
|
|
if (waitsec > -1) {
|
|
|
|
waittime = waitsec * 1000.0;
|
|
|
|
ast_safe_sleep(chan, waittime);
|
2004-07-09 08:32:09 +00:00
|
|
|
} else do {
|
2001-04-27 15:07:56 +00:00
|
|
|
res = ast_waitfor(chan, -1);
|
|
|
|
if (res < 0)
|
|
|
|
return;
|
|
|
|
f = ast_read(chan);
|
|
|
|
if (f)
|
|
|
|
ast_frfree(f);
|
|
|
|
} while(f);
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2004-06-22 03:51:34 +00:00
|
|
|
static int pbx_builtin_progress(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
ast_indicate(chan, AST_CONTROL_PROGRESS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_ringing(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
ast_indicate(chan, AST_CONTROL_RINGING);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_busy(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2006-05-09 08:03:52 +00:00
|
|
|
ast_indicate(chan, AST_CONTROL_BUSY);
|
2006-06-16 06:25:26 +00:00
|
|
|
/* Don't change state of an UP channel, just indicate
|
|
|
|
busy in audio */
|
|
|
|
if (chan->_state != AST_STATE_UP)
|
|
|
|
ast_setstate(chan, AST_STATE_BUSY);
|
2004-07-09 08:32:09 +00:00
|
|
|
wait_for_hangup(chan, data);
|
2001-04-27 15:07:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_congestion(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
ast_indicate(chan, AST_CONTROL_CONGESTION);
|
2006-06-16 06:25:26 +00:00
|
|
|
/* Don't change state of an UP channel, just indicate
|
|
|
|
congestion in audio */
|
|
|
|
if (chan->_state != AST_STATE_UP)
|
|
|
|
ast_setstate(chan, AST_STATE_BUSY);
|
2004-07-09 08:32:09 +00:00
|
|
|
wait_for_hangup(chan, data);
|
2001-04-27 15:07:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_answer(struct ast_channel *chan, void *data)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2005-11-06 21:00:35 +00:00
|
|
|
int delay = 0;
|
|
|
|
|
2007-01-12 15:01:46 +00:00
|
|
|
if ((chan->_state != AST_STATE_UP) && !ast_strlen_zero(data))
|
2005-11-06 21:00:35 +00:00
|
|
|
delay = atoi(data);
|
|
|
|
|
2007-01-12 15:01:46 +00:00
|
|
|
return __ast_answer(chan, delay);
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 23:26:07 +00:00
|
|
|
static int pbx_builtin_keepalive(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
return AST_PBX_KEEPALIVE;
|
|
|
|
}
|
|
|
|
|
2005-11-06 21:00:35 +00:00
|
|
|
AST_APP_OPTIONS(resetcdr_opts, {
|
|
|
|
AST_APP_OPTION('w', AST_CDR_FLAG_POSTED),
|
|
|
|
AST_APP_OPTION('a', AST_CDR_FLAG_LOCKED),
|
|
|
|
AST_APP_OPTION('v', AST_CDR_FLAG_KEEP_VARS),
|
|
|
|
});
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2003-09-08 21:52:45 +00:00
|
|
|
static int pbx_builtin_resetcdr(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2005-11-06 21:00:35 +00:00
|
|
|
char *args;
|
|
|
|
struct ast_flags flags = { 0 };
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-11-06 21:00:35 +00:00
|
|
|
if (!ast_strlen_zero(data)) {
|
2006-05-10 13:22:15 +00:00
|
|
|
args = ast_strdupa(data);
|
2005-11-06 21:00:35 +00:00
|
|
|
ast_app_parse_options(resetcdr_opts, &flags, NULL, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_cdr_reset(chan->cdr, &flags);
|
|
|
|
|
2003-09-08 21:52:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2004-06-28 03:48:53 +00:00
|
|
|
static int pbx_builtin_setamaflags(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
/* Copy the AMA Flags as specified */
|
2006-04-19 09:31:41 +00:00
|
|
|
ast_cdr_setamaflags(chan, data ? data : "");
|
2004-06-28 03:48:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_hangup(struct ast_channel *chan, void *data)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2006-05-25 20:51:27 +00:00
|
|
|
if (!ast_strlen_zero(data)) {
|
|
|
|
int cause;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if ((cause = ast_str2cause(data)) > -1) {
|
|
|
|
chan->hangupcause = cause;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cause = strtol((const char *) data, &endptr, 10);
|
|
|
|
if (cause != 0 || (data != endptr)) {
|
|
|
|
chan->hangupcause = cause;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_log(LOG_NOTICE, "Invalid cause given to Hangup(): \"%s\"\n", (char *) data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chan->hangupcause) {
|
2005-12-13 06:00:17 +00:00
|
|
|
chan->hangupcause = AST_CAUSE_NORMAL_CLEARING;
|
2006-05-25 20:51:27 +00:00
|
|
|
}
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2003-04-28 04:18:47 +00:00
|
|
|
static int pbx_builtin_gotoiftime(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2006-01-21 20:20:06 +00:00
|
|
|
int res=0;
|
2003-04-28 04:18:47 +00:00
|
|
|
char *s, *ts;
|
2004-11-11 21:23:45 +00:00
|
|
|
struct ast_timing timing;
|
2003-04-28 04:18:47 +00:00
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_log(LOG_WARNING, "GotoIfTime requires an argument:\n <time range>,<days of week>,<days of month>,<months>?[[context,]extension,]priority\n");
|
2003-04-28 04:18:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-05-10 13:22:15 +00:00
|
|
|
ts = s = ast_strdupa(data);
|
2005-02-25 00:25:28 +00:00
|
|
|
|
2006-05-10 13:22:15 +00:00
|
|
|
/* Separate the Goto path */
|
2007-07-23 19:51:41 +00:00
|
|
|
strsep(&ts, "?");
|
2005-02-25 00:25:28 +00:00
|
|
|
|
2006-05-10 13:22:15 +00:00
|
|
|
/* struct ast_include include contained garbage here, fixed by zeroing it on get_timerange */
|
|
|
|
if (ast_build_timing(&timing, s) && ast_check_timing(&timing))
|
|
|
|
res = pbx_builtin_goto(chan, ts);
|
|
|
|
|
2005-02-25 00:25:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2005-02-25 00:25:28 +00:00
|
|
|
static int pbx_builtin_execiftime(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2006-04-21 13:44:15 +00:00
|
|
|
char *s, *appname;
|
2005-02-25 00:25:28 +00:00
|
|
|
struct ast_timing timing;
|
2005-08-19 07:12:53 +00:00
|
|
|
struct ast_app *app;
|
2007-07-23 19:51:41 +00:00
|
|
|
static const char *usage = "ExecIfTime requires an argument:\n <time range>,<days of week>,<days of month>,<months>?<appname>[(<appargs>)]";
|
2003-04-28 04:18:47 +00:00
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2006-05-09 08:03:52 +00:00
|
|
|
ast_log(LOG_WARNING, "%s\n", usage);
|
2005-02-25 00:25:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-04-28 04:18:47 +00:00
|
|
|
|
2006-05-10 13:22:15 +00:00
|
|
|
appname = ast_strdupa(data);
|
2005-08-19 07:12:53 +00:00
|
|
|
|
2007-07-23 19:51:41 +00:00
|
|
|
s = strsep(&appname, "?"); /* Separate the timerange and application name/data */
|
2006-04-21 13:44:15 +00:00
|
|
|
if (!appname) { /* missing application */
|
|
|
|
ast_log(LOG_WARNING, "%s\n", usage);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ast_build_timing(&timing, s)) {
|
|
|
|
ast_log(LOG_WARNING, "Invalid Time Spec: %s\nCorrect usage: %s\n", s, usage);
|
|
|
|
return -1;
|
2005-08-19 07:12:53 +00:00
|
|
|
}
|
2006-05-08 22:06:50 +00:00
|
|
|
|
2006-04-21 13:44:15 +00:00
|
|
|
if (!ast_check_timing(&timing)) /* outside the valid time window, just return */
|
|
|
|
return 0;
|
2005-08-19 07:12:53 +00:00
|
|
|
|
2007-07-23 19:51:41 +00:00
|
|
|
/* now split appname(appargs) */
|
|
|
|
if ((s = strchr(appname, '('))) {
|
|
|
|
char *e;
|
2006-04-21 13:44:15 +00:00
|
|
|
*s++ = '\0';
|
2007-07-23 19:51:41 +00:00
|
|
|
if ((e = strrchr(s, ')')))
|
|
|
|
*e = '\0';
|
|
|
|
else
|
|
|
|
ast_log(LOG_WARNING, "Failed to find closing parenthesis\n");
|
|
|
|
}
|
|
|
|
|
2006-05-08 22:06:50 +00:00
|
|
|
|
2006-04-21 13:44:15 +00:00
|
|
|
if ((app = pbx_findapp(appname))) {
|
|
|
|
return pbx_exec(chan, app, S_OR(s, ""));
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Cannot locate application %s\n", appname);
|
|
|
|
return -1;
|
2005-02-25 00:25:28 +00:00
|
|
|
}
|
2003-04-28 04:18:47 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_wait(struct ast_channel *chan, void *data)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2006-10-05 01:40:06 +00:00
|
|
|
double s;
|
2001-11-24 15:15:01 +00:00
|
|
|
int ms;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
/* Wait for "n" seconds */
|
2006-10-05 01:40:06 +00:00
|
|
|
if (data && (s = atof(data)) > 0.0) {
|
2007-07-23 19:51:41 +00:00
|
|
|
ms = s * 1000.0;
|
2002-06-21 01:40:13 +00:00
|
|
|
return ast_safe_sleep(chan, ms);
|
2001-11-24 15:15:01 +00:00
|
|
|
}
|
1999-12-04 20:45:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2004-04-06 04:14:19 +00:00
|
|
|
static int pbx_builtin_waitexten(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2006-01-25 20:02:12 +00:00
|
|
|
int ms, res;
|
2006-10-05 01:40:06 +00:00
|
|
|
double s;
|
2005-04-15 05:47:40 +00:00
|
|
|
struct ast_flags flags = {0};
|
2005-11-03 21:19:11 +00:00
|
|
|
char *opts[1] = { NULL };
|
2006-01-25 20:02:12 +00:00
|
|
|
char *parse;
|
|
|
|
AST_DECLARE_APP_ARGS(args,
|
|
|
|
AST_APP_ARG(timeout);
|
|
|
|
AST_APP_ARG(options);
|
|
|
|
);
|
2005-04-15 05:47:40 +00:00
|
|
|
|
2006-01-25 20:02:12 +00:00
|
|
|
if (!ast_strlen_zero(data)) {
|
2006-05-10 13:22:15 +00:00
|
|
|
parse = ast_strdupa(data);
|
2006-01-25 20:02:12 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, parse);
|
|
|
|
} else
|
|
|
|
memset(&args, 0, sizeof(args));
|
2005-04-15 05:47:40 +00:00
|
|
|
|
2006-01-25 20:02:12 +00:00
|
|
|
if (args.options)
|
|
|
|
ast_app_parse_options(waitexten_opts, &flags, opts, args.options);
|
2006-09-03 21:26:04 +00:00
|
|
|
|
|
|
|
if (ast_test_flag(&flags, WAITEXTEN_MOH) && !opts[0] ) {
|
|
|
|
ast_log(LOG_WARNING, "The 'm' option has been specified for WaitExten without a class.\n");
|
|
|
|
} else if (ast_test_flag(&flags, WAITEXTEN_MOH))
|
2006-07-19 20:44:39 +00:00
|
|
|
ast_indicate_data(chan, AST_CONTROL_HOLD, opts[0], strlen(opts[0]));
|
2005-04-15 05:47:40 +00:00
|
|
|
|
2004-04-06 04:14:19 +00:00
|
|
|
/* Wait for "n" seconds */
|
2006-10-05 01:40:06 +00:00
|
|
|
if (args.timeout && (s = atof(args.timeout)) > 0)
|
|
|
|
ms = s * 1000.0;
|
2004-10-16 19:46:02 +00:00
|
|
|
else if (chan->pbx)
|
|
|
|
ms = chan->pbx->rtimeout * 1000;
|
|
|
|
else
|
|
|
|
ms = 10000;
|
2006-10-05 01:40:06 +00:00
|
|
|
|
2004-10-16 19:46:02 +00:00
|
|
|
res = ast_waitfordigit(chan, ms);
|
|
|
|
if (!res) {
|
2004-11-22 22:40:06 +00:00
|
|
|
if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 1, chan->cid.cid_num)) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Timeout on %s, continuing...\n", chan->name);
|
2004-11-22 22:40:06 +00:00
|
|
|
} else if (ast_exists_extension(chan, chan->context, "t", 1, chan->cid.cid_num)) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(3, "Timeout on %s, going to 't'\n", chan->name);
|
2006-04-19 09:18:30 +00:00
|
|
|
set_ext_pri(chan, "t", 0); /* XXX is the 0 correct ? */
|
2004-10-16 19:46:02 +00:00
|
|
|
} else {
|
|
|
|
ast_log(LOG_WARNING, "Timeout but no rule 't' in context '%s'\n", chan->context);
|
|
|
|
res = -1;
|
|
|
|
}
|
2004-04-06 04:14:19 +00:00
|
|
|
}
|
2005-04-15 05:47:40 +00:00
|
|
|
|
|
|
|
if (ast_test_flag(&flags, WAITEXTEN_MOH))
|
2006-07-19 20:44:39 +00:00
|
|
|
ast_indicate(chan, AST_CONTROL_UNHOLD);
|
2005-04-15 05:47:40 +00:00
|
|
|
|
2004-10-16 19:46:02 +00:00
|
|
|
return res;
|
2004-04-06 04:14:19 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*!
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_background(struct ast_channel *chan, void *data)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2004-09-07 14:51:20 +00:00
|
|
|
int res = 0;
|
2006-09-27 20:03:23 +00:00
|
|
|
int mres = 0;
|
2005-04-15 05:47:40 +00:00
|
|
|
struct ast_flags flags = {0};
|
2006-01-25 20:02:12 +00:00
|
|
|
char *parse;
|
|
|
|
AST_DECLARE_APP_ARGS(args,
|
|
|
|
AST_APP_ARG(filename);
|
|
|
|
AST_APP_ARG(options);
|
|
|
|
AST_APP_ARG(lang);
|
|
|
|
AST_APP_ARG(context);
|
|
|
|
);
|
2004-09-07 14:51:20 +00:00
|
|
|
|
2006-09-27 19:45:24 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2006-01-25 20:02:12 +00:00
|
|
|
ast_log(LOG_WARNING, "Background requires an argument (filename)\n");
|
2006-09-27 19:45:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-09-07 14:51:20 +00:00
|
|
|
|
2006-05-10 13:22:15 +00:00
|
|
|
parse = ast_strdupa(data);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-01-25 20:02:12 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, parse);
|
2005-04-20 16:11:16 +00:00
|
|
|
|
2007-02-26 17:12:18 +00:00
|
|
|
if (ast_strlen_zero(args.lang))
|
2006-05-09 08:16:07 +00:00
|
|
|
args.lang = (char *)chan->language; /* XXX this is const */
|
2005-07-12 01:34:06 +00:00
|
|
|
|
2007-02-26 17:12:18 +00:00
|
|
|
if (ast_strlen_zero(args.context))
|
2006-01-25 20:02:12 +00:00
|
|
|
args.context = chan->context;
|
|
|
|
|
|
|
|
if (args.options) {
|
|
|
|
if (!strcasecmp(args.options, "skip"))
|
2005-04-15 05:47:40 +00:00
|
|
|
flags.flags = BACKGROUND_SKIP;
|
2006-01-25 20:02:12 +00:00
|
|
|
else if (!strcasecmp(args.options, "noanswer"))
|
2005-04-15 05:47:40 +00:00
|
|
|
flags.flags = BACKGROUND_NOANSWER;
|
|
|
|
else
|
2006-01-25 20:02:12 +00:00
|
|
|
ast_app_parse_options(background_opts, &flags, NULL, args.options);
|
2005-04-15 05:47:40 +00:00
|
|
|
}
|
2004-09-07 14:51:20 +00:00
|
|
|
|
|
|
|
/* Answer if need be */
|
|
|
|
if (chan->_state != AST_STATE_UP) {
|
2005-04-15 05:47:40 +00:00
|
|
|
if (ast_test_flag(&flags, BACKGROUND_SKIP)) {
|
2006-09-27 20:03:23 +00:00
|
|
|
goto done;
|
2005-04-15 05:47:40 +00:00
|
|
|
} else if (!ast_test_flag(&flags, BACKGROUND_NOANSWER)) {
|
2004-09-07 14:51:20 +00:00
|
|
|
res = ast_answer(chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res) {
|
2006-04-21 12:58:39 +00:00
|
|
|
char *back = args.filename;
|
|
|
|
char *front;
|
2006-09-27 20:03:23 +00:00
|
|
|
|
2006-04-21 12:58:39 +00:00
|
|
|
ast_stopstream(chan); /* Stop anything playing */
|
|
|
|
/* Stream the list of files */
|
|
|
|
while (!res && (front = strsep(&back, "&")) ) {
|
|
|
|
if ( (res = ast_streamfile(chan, front, args.lang)) ) {
|
2004-11-25 02:57:34 +00:00
|
|
|
ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char*)data);
|
|
|
|
res = 0;
|
2006-09-27 20:03:23 +00:00
|
|
|
mres = 1;
|
2004-11-25 02:57:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-04-21 12:58:39 +00:00
|
|
|
if (ast_test_flag(&flags, BACKGROUND_PLAYBACK)) {
|
|
|
|
res = ast_waitstream(chan, "");
|
|
|
|
} else if (ast_test_flag(&flags, BACKGROUND_MATCHEXTEN)) {
|
|
|
|
res = ast_waitstream_exten(chan, args.context);
|
|
|
|
} else {
|
|
|
|
res = ast_waitstream(chan, AST_DIGIT_ANY);
|
|
|
|
}
|
|
|
|
ast_stopstream(chan);
|
2004-09-07 14:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-25 20:02:12 +00:00
|
|
|
if (args.context != chan->context && res) {
|
2005-07-12 01:34:06 +00:00
|
|
|
snprintf(chan->exten, sizeof(chan->exten), "%c", res);
|
2006-01-25 20:02:12 +00:00
|
|
|
ast_copy_string(chan->context, args.context, sizeof(chan->context));
|
2005-07-12 01:34:06 +00:00
|
|
|
chan->priority = 0;
|
2006-04-21 12:58:39 +00:00
|
|
|
res = 0;
|
2005-07-12 01:34:06 +00:00
|
|
|
}
|
2006-09-27 20:03:23 +00:00
|
|
|
done:
|
|
|
|
pbx_builtin_setvar_helper(chan, "BACKGROUNDSTATUS", mres ? "FAILED" : "SUCCESS");
|
2006-04-21 12:58:39 +00:00
|
|
|
return res;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:38 +00:00
|
|
|
/*! Goto
|
|
|
|
* \ingroup applications
|
|
|
|
*/
|
2001-04-27 15:07:56 +00:00
|
|
|
static int pbx_builtin_goto(struct ast_channel *chan, void *data)
|
1999-12-04 20:45:45 +00:00
|
|
|
{
|
2006-04-19 09:31:41 +00:00
|
|
|
int res = ast_parseable_goto(chan, data);
|
2007-07-23 14:21:41 +00:00
|
|
|
if (!res)
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_verb(3, "Goto (%s,%s,%d)\n", chan->context, chan->exten, chan->priority + 1);
|
2004-11-22 22:11:10 +00:00
|
|
|
return res;
|
1999-12-04 20:45:45 +00:00
|
|
|
}
|
2001-04-27 15:07:56 +00:00
|
|
|
|
2004-11-22 22:11:10 +00:00
|
|
|
|
2006-12-18 15:38:50 +00:00
|
|
|
int pbx_builtin_serialize_variables(struct ast_channel *chan, struct ast_str **buf)
|
2004-10-22 20:59:38 +00:00
|
|
|
{
|
|
|
|
struct ast_var_t *variables;
|
2005-12-03 19:25:33 +00:00
|
|
|
const char *var, *val;
|
2004-10-22 20:59:38 +00:00
|
|
|
int total = 0;
|
|
|
|
|
2005-06-20 23:37:01 +00:00
|
|
|
if (!chan)
|
|
|
|
return 0;
|
|
|
|
|
2006-12-18 15:38:50 +00:00
|
|
|
(*buf)->used = 0;
|
|
|
|
(*buf)->str[0] = '\0';
|
2005-06-20 23:37:01 +00:00
|
|
|
|
|
|
|
AST_LIST_TRAVERSE(&chan->varshead, variables, entries) {
|
2007-07-23 19:51:41 +00:00
|
|
|
if ((var = ast_var_name(variables)) && (val = ast_var_value(variables))
|
2006-08-24 19:53:43 +00:00
|
|
|
/* && !ast_strlen_zero(var) && !ast_strlen_zero(val) */
|
|
|
|
) {
|
2006-12-18 15:38:50 +00:00
|
|
|
if (ast_str_append(buf, 0, "%s=%s\n", var, val) < 0) {
|
2005-06-20 23:37:01 +00:00
|
|
|
ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
|
2004-10-27 02:26:17 +00:00
|
|
|
break;
|
2005-06-20 23:37:01 +00:00
|
|
|
} else
|
|
|
|
total++;
|
2006-05-09 08:03:52 +00:00
|
|
|
} else
|
2005-06-20 23:37:01 +00:00
|
|
|
break;
|
2004-10-22 20:59:38 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-10-22 20:59:38 +00:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
const char *pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
|
2004-05-09 07:19:00 +00:00
|
|
|
{
|
2003-02-02 19:04:14 +00:00
|
|
|
struct ast_var_t *variables;
|
2006-03-12 19:28:57 +00:00
|
|
|
const char *ret = NULL;
|
2006-01-01 17:50:37 +00:00
|
|
|
int i;
|
|
|
|
struct varshead *places[2] = { NULL, &globals };
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-01-01 17:50:37 +00:00
|
|
|
if (!name)
|
|
|
|
return NULL;
|
2004-05-09 07:19:00 +00:00
|
|
|
if (chan)
|
2006-01-01 17:50:37 +00:00
|
|
|
places[0] = &chan->varshead;
|
2003-02-02 19:04:14 +00:00
|
|
|
|
2006-01-01 17:50:37 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2006-03-12 19:28:57 +00:00
|
|
|
if (!places[i])
|
|
|
|
continue;
|
|
|
|
if (places[i] == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_rdlock(&globalslock);
|
2006-03-12 19:28:57 +00:00
|
|
|
AST_LIST_TRAVERSE(places[i], variables, entries) {
|
|
|
|
if (!strcmp(name, ast_var_name(variables))) {
|
|
|
|
ret = ast_var_value(variables);
|
|
|
|
break;
|
2003-02-12 13:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-12 19:28:57 +00:00
|
|
|
if (places[i] == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_unlock(&globalslock);
|
2006-03-12 19:28:57 +00:00
|
|
|
if (ret)
|
|
|
|
break;
|
2003-02-12 13:59:15 +00:00
|
|
|
}
|
2006-03-12 19:28:57 +00:00
|
|
|
|
|
|
|
return ret;
|
2003-02-02 19:04:14 +00:00
|
|
|
}
|
|
|
|
|
2005-11-08 23:37:53 +00:00
|
|
|
void pbx_builtin_pushvar_helper(struct ast_channel *chan, const char *name, const char *value)
|
|
|
|
{
|
|
|
|
struct ast_var_t *newvariable;
|
|
|
|
struct varshead *headp;
|
|
|
|
|
|
|
|
if (name[strlen(name)-1] == ')') {
|
2006-02-12 04:28:58 +00:00
|
|
|
char *function = ast_strdupa(name);
|
|
|
|
|
2005-11-08 23:37:53 +00:00
|
|
|
ast_log(LOG_WARNING, "Cannot push a value onto a function\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
ast_func_write(chan, function, value);
|
|
|
|
return;
|
2005-11-08 23:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
headp = (chan) ? &chan->varshead : &globals;
|
|
|
|
|
|
|
|
if (value) {
|
2007-07-23 14:21:41 +00:00
|
|
|
if (headp == &globals)
|
|
|
|
ast_verb(2, "Setting global variable '%s' to '%s'\n", name, value);
|
2006-03-12 19:28:57 +00:00
|
|
|
newvariable = ast_var_assign(name, value);
|
|
|
|
if (headp == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_wrlock(&globalslock);
|
2005-11-08 23:37:53 +00:00
|
|
|
AST_LIST_INSERT_HEAD(headp, newvariable, entries);
|
2006-03-12 19:28:57 +00:00
|
|
|
if (headp == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_unlock(&globalslock);
|
2005-11-08 23:37:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-29 06:16:49 +00:00
|
|
|
void pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
|
2004-05-09 07:19:00 +00:00
|
|
|
{
|
2002-09-11 17:09:48 +00:00
|
|
|
struct ast_var_t *newvariable;
|
2004-05-09 07:19:00 +00:00
|
|
|
struct varshead *headp;
|
2005-10-18 19:16:13 +00:00
|
|
|
const char *nametail = name;
|
2005-02-23 22:48:47 +00:00
|
|
|
|
2006-05-09 08:31:42 +00:00
|
|
|
/* XXX may need locking on the channel ? */
|
2007-07-23 19:51:41 +00:00
|
|
|
if (name[strlen(name) - 1] == ')') {
|
2006-02-12 04:28:58 +00:00
|
|
|
char *function = ast_strdupa(name);
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
ast_func_write(chan, function, value);
|
|
|
|
return;
|
|
|
|
}
|
2005-03-29 06:16:49 +00:00
|
|
|
|
2005-05-04 15:56:44 +00:00
|
|
|
headp = (chan) ? &chan->varshead : &globals;
|
2004-11-01 02:23:28 +00:00
|
|
|
|
2005-10-18 19:16:13 +00:00
|
|
|
/* For comparison purposes, we have to strip leading underscores */
|
|
|
|
if (*nametail == '_') {
|
|
|
|
nametail++;
|
2006-05-09 08:03:52 +00:00
|
|
|
if (*nametail == '_')
|
2005-10-18 19:16:13 +00:00
|
|
|
nametail++;
|
|
|
|
}
|
|
|
|
|
2006-03-12 19:28:57 +00:00
|
|
|
if (headp == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_wrlock(&globalslock);
|
2004-11-01 02:23:28 +00:00
|
|
|
AST_LIST_TRAVERSE (headp, newvariable, entries) {
|
2005-10-18 19:16:13 +00:00
|
|
|
if (strcasecmp(ast_var_name(newvariable), nametail) == 0) {
|
2002-09-11 17:09:48 +00:00
|
|
|
/* there is already such a variable, delete it */
|
2005-01-01 21:26:37 +00:00
|
|
|
AST_LIST_REMOVE(headp, newvariable, entries);
|
2002-09-11 17:09:48 +00:00
|
|
|
ast_var_delete(newvariable);
|
|
|
|
break;
|
|
|
|
}
|
2006-03-12 19:28:57 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2003-02-12 13:59:15 +00:00
|
|
|
if (value) {
|
2007-07-23 14:21:41 +00:00
|
|
|
if (headp == &globals)
|
|
|
|
ast_verb(2, "Setting global variable '%s' to '%s'\n", name, value);
|
2006-05-09 08:03:52 +00:00
|
|
|
newvariable = ast_var_assign(name, value);
|
2004-11-01 02:23:28 +00:00
|
|
|
AST_LIST_INSERT_HEAD(headp, newvariable, entries);
|
2007-06-08 16:14:06 +00:00
|
|
|
manager_event(EVENT_FLAG_CALL, "VarSet",
|
|
|
|
"Channel: %s\r\n"
|
|
|
|
"Variable: %s\r\n"
|
|
|
|
"Value: %s\r\n"
|
|
|
|
"Uniqueid: %s\r\n",
|
|
|
|
chan ? chan->name : "none", name, value,
|
|
|
|
chan ? chan->uniqueid : "none");
|
2003-02-12 13:59:15 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-03-12 19:28:57 +00:00
|
|
|
if (headp == &globals)
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_unlock(&globalslock);
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
|
2003-09-22 15:27:09 +00:00
|
|
|
int pbx_builtin_setvar(struct ast_channel *chan, void *data)
|
2002-09-11 17:09:48 +00:00
|
|
|
{
|
2005-05-04 15:56:44 +00:00
|
|
|
char *name, *value, *mydata;
|
2005-02-23 22:48:47 +00:00
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_log(LOG_WARNING, "Set requires one variable name/value pair.\n");
|
2005-05-04 15:56:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-01 02:23:28 +00:00
|
|
|
|
2005-05-04 15:56:44 +00:00
|
|
|
mydata = ast_strdupa(data);
|
2007-07-23 19:51:41 +00:00
|
|
|
name = strsep(&mydata, "=");
|
|
|
|
value = mydata;
|
2005-05-04 15:56:44 +00:00
|
|
|
|
2007-07-23 19:51:41 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, name, value);
|
2004-11-01 02:23:28 +00:00
|
|
|
return(0);
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
|
2004-11-17 04:48:58 +00:00
|
|
|
int pbx_builtin_importvar(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
char *channel;
|
2005-09-26 16:58:40 +00:00
|
|
|
char tmp[VAR_BUF_SIZE]="";
|
2007-07-23 19:51:41 +00:00
|
|
|
static int deprecation_warning = 0;
|
2004-11-17 04:48:58 +00:00
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2004-11-17 04:48:58 +00:00
|
|
|
ast_log(LOG_WARNING, "Ignoring, since there is no variable to set\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-23 19:51:41 +00:00
|
|
|
if (!deprecation_warning) {
|
|
|
|
ast_log(LOG_WARNING, "ImportVar is deprecated. Please use Set(varname=${IMPORT(channel,variable)}) instead.\n");
|
|
|
|
deprecation_warning = 1;
|
|
|
|
}
|
|
|
|
|
2006-04-18 13:37:07 +00:00
|
|
|
value = ast_strdupa(data);
|
|
|
|
name = strsep(&value,"=");
|
2007-07-23 19:51:41 +00:00
|
|
|
channel = strsep(&value,",");
|
2006-04-18 13:37:07 +00:00
|
|
|
if (channel && value && name) { /*! \todo XXX should do !ast_strlen_zero(..) of the args ? */
|
|
|
|
struct ast_channel *chan2 = ast_get_channel_by_name_locked(channel);
|
2004-11-17 04:48:58 +00:00
|
|
|
if (chan2) {
|
2006-04-18 13:37:07 +00:00
|
|
|
char *s = alloca(strlen(value) + 4);
|
2004-11-17 04:48:58 +00:00
|
|
|
if (s) {
|
|
|
|
sprintf(s, "${%s}", value);
|
|
|
|
pbx_substitute_variables_helper(chan2, s, tmp, sizeof(tmp) - 1);
|
|
|
|
}
|
2006-04-29 14:50:18 +00:00
|
|
|
ast_channel_unlock(chan2);
|
2004-11-17 04:48:58 +00:00
|
|
|
}
|
|
|
|
pbx_builtin_setvar_helper(chan, name, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2003-02-12 13:59:15 +00:00
|
|
|
static int pbx_builtin_noop(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pbx_builtin_clear_globals(void)
|
|
|
|
{
|
|
|
|
struct ast_var_t *vardata;
|
2006-03-12 19:28:57 +00:00
|
|
|
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_wrlock(&globalslock);
|
2006-03-12 19:28:57 +00:00
|
|
|
while ((vardata = AST_LIST_REMOVE_HEAD(&globals, entries)))
|
2003-02-12 13:59:15 +00:00
|
|
|
ast_var_delete(vardata);
|
2006-12-21 03:20:08 +00:00
|
|
|
ast_rwlock_unlock(&globalslock);
|
2003-02-12 13:59:15 +00:00
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int pbx_checkcondition(const char *condition)
|
2004-05-09 07:19:00 +00:00
|
|
|
{
|
2006-04-18 13:37:07 +00:00
|
|
|
if (ast_strlen_zero(condition)) /* NULL or empty strings are false */
|
2005-10-18 16:51:28 +00:00
|
|
|
return 0;
|
2006-04-18 13:37:07 +00:00
|
|
|
else if (*condition >= '0' && *condition <= '9') /* Numbers are evaluated for truth */
|
|
|
|
return atoi(condition);
|
2006-05-08 10:08:20 +00:00
|
|
|
else /* Strings are true */
|
2006-04-18 13:37:07 +00:00
|
|
|
return 1;
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pbx_builtin_gotoif(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2005-11-09 21:48:59 +00:00
|
|
|
char *condition, *branch1, *branch2, *branch;
|
2006-04-19 09:31:41 +00:00
|
|
|
char *stringp;
|
2002-09-11 17:09:48 +00:00
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2004-01-11 09:19:16 +00:00
|
|
|
ast_log(LOG_WARNING, "Ignoring, since there is no variable to check\n");
|
2002-09-11 17:09:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2006-04-18 13:37:07 +00:00
|
|
|
stringp = ast_strdupa(data);
|
2005-11-09 21:48:59 +00:00
|
|
|
condition = strsep(&stringp,"?");
|
|
|
|
branch1 = strsep(&stringp,":");
|
|
|
|
branch2 = strsep(&stringp,"");
|
2004-07-01 20:42:49 +00:00
|
|
|
branch = pbx_checkcondition(condition) ? branch1 : branch2;
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(branch)) {
|
2007-06-12 16:13:45 +00:00
|
|
|
ast_debug(1, "Not taking any branch\n");
|
2005-11-09 21:48:59 +00:00
|
|
|
return 0;
|
2002-09-11 17:09:48 +00:00
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2007-07-23 19:51:41 +00:00
|
|
|
return pbx_builtin_goto(chan, branch);
|
2006-05-09 08:03:52 +00:00
|
|
|
}
|
2003-04-16 13:43:11 +00:00
|
|
|
|
|
|
|
static int pbx_builtin_saynumber(struct ast_channel *chan, void *data)
|
|
|
|
{
|
2004-04-29 02:30:14 +00:00
|
|
|
char tmp[256];
|
2006-04-18 13:37:07 +00:00
|
|
|
char *number = tmp;
|
|
|
|
char *options;
|
2004-04-29 02:30:14 +00:00
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2005-03-29 06:16:49 +00:00
|
|
|
ast_log(LOG_WARNING, "SayNumber requires an argument (number)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-04-18 13:37:07 +00:00
|
|
|
ast_copy_string(tmp, data, sizeof(tmp));
|
2007-07-23 19:51:41 +00:00
|
|
|
strsep(&number, ",");
|
|
|
|
options = strsep(&number, ",");
|
2006-05-09 08:03:52 +00:00
|
|
|
if (options) {
|
2007-07-23 19:51:41 +00:00
|
|
|
if ( strcasecmp(options, "f") && strcasecmp(options, "m") &&
|
2004-04-29 02:30:14 +00:00
|
|
|
strcasecmp(options, "c") && strcasecmp(options, "n") ) {
|
2005-03-29 06:16:49 +00:00
|
|
|
ast_log(LOG_WARNING, "SayNumber gender option is either 'f', 'm', 'c' or 'n'\n");
|
|
|
|
return -1;
|
2004-04-29 02:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-18 13:37:07 +00:00
|
|
|
return ast_say_number(chan, atoi(tmp), "", chan->language, options);
|
2003-04-16 13:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pbx_builtin_saydigits(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2003-04-29 20:30:08 +00:00
|
|
|
if (data)
|
2006-05-09 13:52:01 +00:00
|
|
|
res = ast_say_digit_str(chan, data, "", chan->language);
|
2003-04-16 13:43:11 +00:00
|
|
|
return res;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-05-03 00:54:16 +00:00
|
|
|
static int pbx_builtin_saycharacters(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2004-05-03 00:54:16 +00:00
|
|
|
if (data)
|
2006-05-09 13:52:01 +00:00
|
|
|
res = ast_say_character_str(chan, data, "", chan->language);
|
2004-05-03 00:54:16 +00:00
|
|
|
return res;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
2004-05-03 00:54:16 +00:00
|
|
|
static int pbx_builtin_sayphonetic(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
2004-05-03 00:54:16 +00:00
|
|
|
if (data)
|
2006-05-09 13:52:01 +00:00
|
|
|
res = ast_say_phonetic_str(chan, data, "", chan->language);
|
2004-05-03 00:54:16 +00:00
|
|
|
return res;
|
|
|
|
}
|
2006-05-09 08:03:52 +00:00
|
|
|
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
static void device_state_cb(const struct ast_event *event, void *unused)
|
|
|
|
{
|
|
|
|
const char *device;
|
|
|
|
|
|
|
|
device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
|
|
|
|
if (ast_strlen_zero(device)) {
|
|
|
|
ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
statechange_queue(device);
|
|
|
|
}
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
int load_pbx(void)
|
|
|
|
{
|
|
|
|
int x;
|
2004-07-17 02:25:53 +00:00
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
/* Initialize the PBX */
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(1, "Asterisk PBX Core Initializing\n");
|
|
|
|
ast_verb(1, "Registering builtin applications:\n");
|
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
ast_cli_register_multiple(pbx_cli, sizeof(pbx_cli) / sizeof(struct ast_cli_entry));
|
2004-07-17 02:25:53 +00:00
|
|
|
|
|
|
|
/* Register builtin applications */
|
|
|
|
for (x=0; x<sizeof(builtins) / sizeof(struct pbx_builtin); x++) {
|
2007-07-23 14:21:41 +00:00
|
|
|
ast_verb(1, "[%s]\n", builtins[x].name);
|
2007-07-16 14:39:29 +00:00
|
|
|
if (ast_register_application2(builtins[x].name, builtins[x].execute, builtins[x].synopsis, builtins[x].description, NULL)) {
|
1999-12-04 20:45:45 +00:00
|
|
|
ast_log(LOG_ERROR, "Unable to register builtin application '%s'\n", builtins[x].name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2006-10-30 21:18:39 +00:00
|
|
|
|
|
|
|
/* Register manager application */
|
|
|
|
ast_manager_register2("ShowDialPlan", EVENT_FLAG_CONFIG, manager_show_dialplan, "List dialplan", mandescr_show_dialplan);
|
Merge major changes to the way device state is passed around Asterisk. The two
places that cared about device states were app_queue and the hint code in pbx.c.
The changes include converting it to use the Asterisk event system, as well as
other efficiency improvements.
* app_queue: This module used to register a callback into devicestate.c to
monitor device state changes. Now, it is just a subscriber to Asterisk
events with the type, device state.
* pbx.c hints: Previously, the device state processing thread in devicestate.c
would call ast_hint_state_changed() each time the state of a device changed.
Then, that code would go looking for all the hints that monitor that device,
and call their callbacks. All of this blocked the device state processing
thread. Now, the hint code is a subscriber of Asterisk events with the
type, device state. Furthermore, when this code receives a device state
change event, it queues it up to be processed by another thread so that it
doesn't block one of the event processing threads.
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@66958 f38db490-d61c-443f-a65b-d21fe96a405b
2007-06-01 23:34:43 +00:00
|
|
|
|
|
|
|
ast_mutex_init(&device_state.lock);
|
|
|
|
ast_cond_init(&device_state.cond, NULL);
|
|
|
|
ast_pthread_create(&device_state.thread, NULL, device_state_thread, NULL);
|
|
|
|
|
|
|
|
if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL,
|
|
|
|
AST_EVENT_IE_END))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-12-04 20:45:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-28 21:20:52 +00:00
|
|
|
/*
|
|
|
|
* Lock context list functions ...
|
|
|
|
*/
|
2007-02-28 20:46:01 +00:00
|
|
|
int ast_wrlock_contexts()
|
|
|
|
{
|
|
|
|
return ast_rwlock_wrlock(&conlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_rdlock_contexts()
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2007-02-28 20:46:01 +00:00
|
|
|
return ast_rwlock_rdlock(&conlock);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_unlock_contexts()
|
|
|
|
{
|
2007-02-28 20:46:01 +00:00
|
|
|
return ast_rwlock_unlock(&conlock);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock context ...
|
|
|
|
*/
|
2007-02-28 20:46:01 +00:00
|
|
|
int ast_wrlock_context(struct ast_context *con)
|
|
|
|
{
|
|
|
|
return ast_rwlock_wrlock(&con->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_rdlock_context(struct ast_context *con)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
2007-02-28 20:46:01 +00:00
|
|
|
return ast_rwlock_rdlock(&con->lock);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_unlock_context(struct ast_context *con)
|
|
|
|
{
|
2007-02-28 20:46:01 +00:00
|
|
|
return ast_rwlock_unlock(&con->lock);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Name functions ...
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_context_name(struct ast_context *con)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return con ? con->name : NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-28 22:09:38 +00:00
|
|
|
struct ast_context *ast_get_extension_context(struct ast_exten *exten)
|
|
|
|
{
|
|
|
|
return exten ? exten->parent : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_extension_name(struct ast_exten *exten)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return exten ? exten->exten : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_extension_label(struct ast_exten *exten)
|
|
|
|
{
|
|
|
|
return exten ? exten->label : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ast_get_include_name(struct ast_include *inc)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return inc ? inc->name : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_ignorepat_name(struct ast_ignorepat *ip)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return ip ? ip->pattern : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_get_extension_priority(struct ast_exten *exten)
|
|
|
|
{
|
|
|
|
return exten ? exten->priority : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Registrar info functions ...
|
|
|
|
*/
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_context_registrar(struct ast_context *c)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return c ? c->registrar : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_extension_registrar(struct ast_exten *e)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return e ? e->registrar : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_include_registrar(struct ast_include *i)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return i ? i->registrar : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_ignorepat_registrar(struct ast_ignorepat *ip)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return ip ? ip->registrar : NULL;
|
|
|
|
}
|
|
|
|
|
2004-08-21 18:55:39 +00:00
|
|
|
int ast_get_extension_matchcid(struct ast_exten *e)
|
|
|
|
{
|
|
|
|
return e ? e->matchcid : 0;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_extension_cidmatch(struct ast_exten *e)
|
2004-08-21 18:55:39 +00:00
|
|
|
{
|
|
|
|
return e ? e->cidmatch : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_extension_app(struct ast_exten *e)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return e ? e->app : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ast_get_extension_app_data(struct ast_exten *e)
|
|
|
|
{
|
|
|
|
return e ? e->data : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_switch_name(struct ast_sw *sw)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return sw ? sw->name : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_switch_data(struct ast_sw *sw)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return sw ? sw->data : NULL;
|
|
|
|
}
|
|
|
|
|
2004-10-03 04:19:59 +00:00
|
|
|
const char *ast_get_switch_registrar(struct ast_sw *sw)
|
2001-09-28 21:20:52 +00:00
|
|
|
{
|
|
|
|
return sw ? sw->registrar : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walking functions ...
|
|
|
|
*/
|
|
|
|
struct ast_context *ast_walk_contexts(struct ast_context *con)
|
|
|
|
{
|
2005-12-26 21:38:50 +00:00
|
|
|
return con ? con->next : contexts;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
|
|
|
|
struct ast_exten *exten)
|
|
|
|
{
|
|
|
|
if (!exten)
|
|
|
|
return con ? con->root : NULL;
|
|
|
|
else
|
|
|
|
return exten->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_sw *ast_walk_context_switches(struct ast_context *con,
|
|
|
|
struct ast_sw *sw)
|
|
|
|
{
|
|
|
|
if (!sw)
|
2006-05-08 11:20:19 +00:00
|
|
|
return con ? AST_LIST_FIRST(&con->alts) : NULL;
|
2001-09-28 21:20:52 +00:00
|
|
|
else
|
2006-05-08 11:20:19 +00:00
|
|
|
return AST_LIST_NEXT(sw, list);
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
|
|
|
|
struct ast_exten *priority)
|
|
|
|
{
|
2005-12-26 21:38:50 +00:00
|
|
|
return priority ? priority->peer : exten;
|
2001-09-28 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_include *ast_walk_context_includes(struct ast_context *con,
|
|
|
|
struct ast_include *inc)
|
|
|
|
{
|
|
|
|
if (!inc)
|
|
|
|
return con ? con->includes : NULL;
|
|
|
|
else
|
|
|
|
return inc->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con,
|
|
|
|
struct ast_ignorepat *ip)
|
|
|
|
{
|
|
|
|
if (!ip)
|
|
|
|
return con ? con->ignorepats : NULL;
|
|
|
|
else
|
|
|
|
return ip->next;
|
|
|
|
}
|
2003-10-25 17:50:06 +00:00
|
|
|
|
|
|
|
int ast_context_verify_includes(struct ast_context *con)
|
|
|
|
{
|
2005-12-26 21:38:50 +00:00
|
|
|
struct ast_include *inc = NULL;
|
2003-10-25 17:50:06 +00:00
|
|
|
int res = 0;
|
|
|
|
|
2005-12-26 21:38:50 +00:00
|
|
|
while ( (inc = ast_walk_context_includes(con, inc)) )
|
2003-10-25 17:50:06 +00:00
|
|
|
if (!ast_context_find(inc->rname)) {
|
|
|
|
res = -1;
|
2005-04-13 23:33:47 +00:00
|
|
|
ast_log(LOG_WARNING, "Context '%s' tries includes nonexistent context '%s'\n",
|
2003-10-25 17:50:06 +00:00
|
|
|
ast_get_context_name(con), inc->rname);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2004-10-24 02:53:24 +00:00
|
|
|
|
2004-11-22 22:11:10 +00:00
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
static int __ast_goto_if_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, int async)
|
2004-11-22 22:11:10 +00:00
|
|
|
{
|
2005-04-29 15:04:26 +00:00
|
|
|
int (*goto_func)(struct ast_channel *chan, const char *context, const char *exten, int priority);
|
2004-12-08 18:24:04 +00:00
|
|
|
|
2005-04-29 15:04:26 +00:00
|
|
|
if (!chan)
|
|
|
|
return -2;
|
2004-12-08 18:24:04 +00:00
|
|
|
|
2006-04-19 09:31:41 +00:00
|
|
|
if (context == NULL)
|
|
|
|
context = chan->context;
|
|
|
|
if (exten == NULL)
|
|
|
|
exten = chan->exten;
|
|
|
|
|
2005-04-29 15:04:26 +00:00
|
|
|
goto_func = (async) ? ast_async_goto : ast_explicit_goto;
|
2006-04-19 09:31:41 +00:00
|
|
|
if (ast_exists_extension(chan, context, exten, priority, chan->cid.cid_num))
|
|
|
|
return goto_func(chan, context, exten, priority);
|
2006-05-09 08:03:52 +00:00
|
|
|
else
|
2005-04-29 15:04:26 +00:00
|
|
|
return -3;
|
2004-11-22 22:11:10 +00:00
|
|
|
}
|
|
|
|
|
2006-04-06 16:44:31 +00:00
|
|
|
int ast_goto_if_exists(struct ast_channel *chan, const char* context, const char *exten, int priority)
|
|
|
|
{
|
2004-11-23 20:25:02 +00:00
|
|
|
return __ast_goto_if_exists(chan, context, exten, priority, 0);
|
|
|
|
}
|
|
|
|
|
2006-04-06 16:44:31 +00:00
|
|
|
int ast_async_goto_if_exists(struct ast_channel *chan, const char * context, const char *exten, int priority)
|
|
|
|
{
|
2004-11-23 20:25:02 +00:00
|
|
|
return __ast_goto_if_exists(chan, context, exten, priority, 1);
|
|
|
|
}
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
int ast_parseable_goto(struct ast_channel *chan, const char *goto_string)
|
2004-11-22 22:11:10 +00:00
|
|
|
{
|
|
|
|
char *exten, *pri, *context;
|
2006-04-18 13:37:07 +00:00
|
|
|
char *stringp;
|
2004-11-22 22:11:10 +00:00
|
|
|
int ipri;
|
|
|
|
int mode = 0;
|
|
|
|
|
2005-10-26 18:54:24 +00:00
|
|
|
if (ast_strlen_zero(goto_string)) {
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_log(LOG_WARNING, "Goto requires an argument ([[context,]extension,]priority)\n");
|
2004-11-22 22:11:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-04-18 13:37:07 +00:00
|
|
|
stringp = ast_strdupa(goto_string);
|
2007-07-23 19:51:41 +00:00
|
|
|
context = strsep(&stringp, ","); /* guaranteed non-null */
|
|
|
|
exten = strsep(&stringp, ",");
|
|
|
|
pri = strsep(&stringp, ",");
|
2006-04-18 13:37:07 +00:00
|
|
|
if (!exten) { /* Only a priority in this one */
|
2004-11-22 22:11:10 +00:00
|
|
|
pri = context;
|
|
|
|
exten = NULL;
|
|
|
|
context = NULL;
|
2006-04-18 13:37:07 +00:00
|
|
|
} else if (!pri) { /* Only an extension and priority in this one */
|
|
|
|
pri = exten;
|
|
|
|
exten = context;
|
|
|
|
context = NULL;
|
2004-11-22 22:11:10 +00:00
|
|
|
}
|
|
|
|
if (*pri == '+') {
|
|
|
|
mode = 1;
|
|
|
|
pri++;
|
|
|
|
} else if (*pri == '-') {
|
|
|
|
mode = -1;
|
|
|
|
pri++;
|
|
|
|
}
|
2005-04-29 15:04:26 +00:00
|
|
|
if (sscanf(pri, "%d", &ipri) != 1) {
|
2006-05-09 08:03:52 +00:00
|
|
|
if ((ipri = ast_findlabel_extension(chan, context ? context : chan->context, exten ? exten : chan->exten,
|
2004-11-22 22:11:10 +00:00
|
|
|
pri, chan->cid.cid_num)) < 1) {
|
|
|
|
ast_log(LOG_WARNING, "Priority '%s' must be a number > 0, or valid label\n", pri);
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
mode = 0;
|
2006-05-09 08:03:52 +00:00
|
|
|
}
|
2004-11-22 22:11:10 +00:00
|
|
|
/* At this point we have a priority and maybe an extension and a context */
|
|
|
|
|
2006-05-09 08:03:52 +00:00
|
|
|
if (mode)
|
2004-11-22 22:11:10 +00:00
|
|
|
ipri = chan->priority + (ipri * mode);
|
|
|
|
|
2005-04-29 15:04:26 +00:00
|
|
|
ast_explicit_goto(chan, context, exten, ipri);
|
2004-11-22 22:11:10 +00:00
|
|
|
ast_cdr_update(chan);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|