407e6389062a4b1ded1940a20a9e4c55e7b00f09
[asterisk/asterisk.git] / pbx.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Core PBX routines.
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  */
25
26 #include <sys/types.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <time.h>
34 #include <sys/time.h>
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/lock.h"
41 #include "asterisk/cli.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/options.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/file.h"
47 #include "asterisk/callerid.h"
48 #include "asterisk/cdr.h"
49 #include "asterisk/config.h"
50 #include "asterisk/term.h"
51 #include "asterisk/manager.h"
52 #include "asterisk/ast_expr.h"
53 #include "asterisk/linkedlists.h"
54 #define SAY_STUBS       /* generate declarations and stubs for say methods */
55 #include "asterisk/say.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/causes.h"
58 #include "asterisk/musiconhold.h"
59 #include "asterisk/app.h"
60 #include "asterisk/devicestate.h"
61 #include "asterisk/compat.h"
62 #include "asterisk/stringfields.h"
63
64 /*!
65  * \note I M P O R T A N T :
66  *
67  *              The speed of extension handling will likely be among the most important
68  * aspects of this PBX.  The switching scheme as it exists right now isn't
69  * terribly bad (it's O(N+M), where N is the # of extensions and M is the avg #
70  * of priorities, but a constant search time here would be great ;-) 
71  *
72  */
73
74 #ifdef LOW_MEMORY
75 #define EXT_DATA_SIZE 256
76 #else
77 #define EXT_DATA_SIZE 8192
78 #endif
79
80 #define SWITCH_DATA_LENGTH 256
81
82 #define VAR_BUF_SIZE 4096
83
84 #define VAR_NORMAL              1
85 #define VAR_SOFTTRAN    2
86 #define VAR_HARDTRAN    3
87
88 #define BACKGROUND_SKIP         (1 << 0)
89 #define BACKGROUND_NOANSWER     (1 << 1)
90 #define BACKGROUND_MATCHEXTEN   (1 << 2)
91 #define BACKGROUND_PLAYBACK     (1 << 3)
92
93 AST_APP_OPTIONS(background_opts, {
94         AST_APP_OPTION('s', BACKGROUND_SKIP),
95         AST_APP_OPTION('n', BACKGROUND_NOANSWER),
96         AST_APP_OPTION('m', BACKGROUND_MATCHEXTEN),
97         AST_APP_OPTION('p', BACKGROUND_PLAYBACK),
98 });
99
100 #define WAITEXTEN_MOH           (1 << 0)
101
102 AST_APP_OPTIONS(waitexten_opts, {
103         AST_APP_OPTION_ARG('m', WAITEXTEN_MOH, 1),
104 });
105
106 struct ast_context;
107
108 /*!
109    \brief ast_exten: An extension 
110         The dialplan is saved as a linked list with each context
111         having it's own linked list of extensions - one item per
112         priority.
113 */
114 struct ast_exten {
115         char *exten;                    /*!< Extension name */
116         int matchcid;                   /*!< Match caller id ? */
117         const char *cidmatch;           /*!< Caller id to match for this extension */
118         int priority;                   /*!< Priority */
119         const char *label;              /*!< Label */
120         struct ast_context *parent;     /*!< The context this extension belongs to  */
121         const char *app;                /*!< Application to execute */
122         void *data;                     /*!< Data to use (arguments) */
123         void (*datad)(void *);          /*!< Data destructor */
124         struct ast_exten *peer;         /*!< Next higher priority with our extension */
125         const char *registrar;          /*!< Registrar */
126         struct ast_exten *next;         /*!< Extension with a greater ID */
127         char stuff[0];
128 };
129
130 /*! \brief ast_include: include= support in extensions.conf */
131 struct ast_include {
132         const char *name;               
133         const char *rname;                      /*!< Context to include */
134         const char *registrar;                  /*!< Registrar */
135         int hastime;                            /*!< If time construct exists */
136         struct ast_timing timing;               /*!< time construct */
137         struct ast_include *next;               /*!< Link them together */
138         char stuff[0];
139 };
140
141 /*! \brief ast_sw: Switch statement in extensions.conf */
142 struct ast_sw {
143         char *name;
144         const char *registrar;                  /*!< Registrar */
145         char *data;                             /*!< Data load */
146         int eval;
147         struct ast_sw *next;                    /*!< Link them together */
148         char *tmpdata;
149         char stuff[0];
150 };
151
152 /*! \brief ast_ignorepat: Ignore patterns in dial plan */
153 struct ast_ignorepat {
154         const char *registrar;
155         struct ast_ignorepat *next;
156         const char pattern[0];
157 };
158
159 /*! \brief ast_context: An extension context */
160 struct ast_context {
161         ast_mutex_t lock;                       /*!< A lock to prevent multiple threads from clobbering the context */
162         struct ast_exten *root;                 /*!< The root of the list of extensions */
163         struct ast_context *next;               /*!< Link them together */
164         struct ast_include *includes;           /*!< Include other contexts */
165         struct ast_ignorepat *ignorepats;       /*!< Patterns for which to continue playing dialtone */
166         const char *registrar;                  /*!< Registrar */
167         struct ast_sw *alts;                    /*!< Alternative switches */
168         char name[0];                           /*!< Name of the context */
169 };
170
171
172 /*! \brief ast_app: A registered application */
173 struct ast_app {
174         int (*execute)(struct ast_channel *chan, void *data);
175         const char *synopsis;                   /*!< Synopsis text for 'show applications' */
176         const char *description;                /*!< Description (help text) for 'show application <name>' */
177         struct ast_app *next;                   /*!< Next app in list */
178         struct module *module;                  /*!< Module this app belongs to */
179         char name[0];                           /*!< Name of the application */
180 };
181
182 /*! \brief ast_state_cb: An extension state notify register item */
183 struct ast_state_cb {
184         int id;
185         void *data;
186         ast_state_cb_type callback;
187         struct ast_state_cb *next;
188 };
189             
190 /*! \brief Structure for dial plan hints
191
192   Hints are pointers from an extension in the dialplan to one or
193   more devices (tech/name) */
194 struct ast_hint {
195         struct ast_exten *exten;        /*!< Extension */
196         int laststate;                  /*!< Last known state */
197         struct ast_state_cb *callbacks; /*!< Callback list for this extension */
198         AST_LIST_ENTRY(ast_hint) list;  /*!< Pointer to next hint in list */
199 };
200
201 static const struct cfextension_states {
202         int extension_state;
203         const char * const text;
204 } extension_states[] = {
205         { AST_EXTENSION_NOT_INUSE,                     "Idle" },
206         { AST_EXTENSION_INUSE,                         "InUse" },
207         { AST_EXTENSION_BUSY,                          "Busy" },
208         { AST_EXTENSION_UNAVAILABLE,                   "Unavailable" },
209         { AST_EXTENSION_RINGING,                       "Ringing" },
210         { AST_EXTENSION_INUSE | AST_EXTENSION_RINGING, "InUse&Ringing" }
211 };
212
213 int ast_pbx_outgoing_cdr_failed(void);
214
215 static int pbx_builtin_answer(struct ast_channel *, void *);
216 static int pbx_builtin_goto(struct ast_channel *, void *);
217 static int pbx_builtin_hangup(struct ast_channel *, void *);
218 static int pbx_builtin_background(struct ast_channel *, void *);
219 static int pbx_builtin_wait(struct ast_channel *, void *);
220 static int pbx_builtin_waitexten(struct ast_channel *, void *);
221 static int pbx_builtin_resetcdr(struct ast_channel *, void *);
222 static int pbx_builtin_setamaflags(struct ast_channel *, void *);
223 static int pbx_builtin_ringing(struct ast_channel *, void *);
224 static int pbx_builtin_progress(struct ast_channel *, void *);
225 static int pbx_builtin_congestion(struct ast_channel *, void *);
226 static int pbx_builtin_busy(struct ast_channel *, void *);
227 static int pbx_builtin_setglobalvar(struct ast_channel *, void *);
228 static int pbx_builtin_noop(struct ast_channel *, void *);
229 static int pbx_builtin_gotoif(struct ast_channel *, void *);
230 static int pbx_builtin_gotoiftime(struct ast_channel *, void *);
231 static int pbx_builtin_execiftime(struct ast_channel *, void *);
232 static int pbx_builtin_saynumber(struct ast_channel *, void *);
233 static int pbx_builtin_saydigits(struct ast_channel *, void *);
234 static int pbx_builtin_saycharacters(struct ast_channel *, void *);
235 static int pbx_builtin_sayphonetic(struct ast_channel *, void *);
236 int pbx_builtin_setvar(struct ast_channel *, void *);
237 static int pbx_builtin_importvar(struct ast_channel *, void *);
238
239 AST_MUTEX_DEFINE_STATIC(globalslock);
240 static struct varshead globals;
241
242 static int autofallthrough = 0;
243
244 AST_MUTEX_DEFINE_STATIC(maxcalllock);
245 static int countcalls = 0;
246
247 AST_MUTEX_DEFINE_STATIC(acflock);               /*!< Lock for the custom function list */
248 static struct ast_custom_function *acf_root = NULL;
249
250 /*! \brief Declaration of builtin applications */
251 static struct pbx_builtin {
252         char name[AST_MAX_APP];
253         int (*execute)(struct ast_channel *chan, void *data);
254         char *synopsis;
255         char *description;
256 } builtins[] = 
257 {
258         /* These applications are built into the PBX core and do not
259            need separate modules */
260
261         { "Answer", pbx_builtin_answer, 
262         "Answer a channel if ringing", 
263         "  Answer([delay]): If the call has not been answered, this application will\n"
264         "answer it. Otherwise, it has no effect on the call. If a delay is specified,\n"
265         "Asterisk will wait this number of milliseconds before answering the call.\n"
266         },
267
268         { "BackGround", pbx_builtin_background,
269         "Play a file while awaiting extension",
270         "  Background(filename1[&filename2...][|options[|langoverride][|context]]):\n"
271         "This application will play the given list of files while waiting for an\n"
272         "extension to be dialed by the calling channel. To continue waiting for digits\n"
273         "after this application has finished playing files, the WaitExten application\n"
274         "should be used. The 'langoverride' option explicity specifies which language\n"
275         "to attempt to use for the requested sound files. If a 'context' is specified,\n"
276         "this is the dialplan context that this application will use when exiting to a\n"
277         "dialed extension."
278         "  If one of the requested sound files does not exist, call processing will be\n"
279         "terminated.\n"
280         "  Options:\n"
281         "    s - causes the playback of the message to be skipped\n"
282         "          if the channel is not in the 'up' state (i.e. it\n"
283         "          hasn't been answered yet.) If this happens, the\n"
284         "          application will return immediately.\n"
285         "    n - don't answer the channel before playing the files\n"
286         "    m - only break if a digit hit matches a one digit\n"
287         "          extension in the destination context\n"
288         },
289
290         { "Busy", pbx_builtin_busy,
291         "Indicate the Busy condition",
292         "  Busy([timeout]): This application will indicate the busy condition to\n"
293         "the calling channel. If the optional timeout is specified, the calling channel\n"
294         "will be hung up after the specified number of seconds. Otherwise, this\n"
295         "application will wait until the calling channel hangs up.\n"
296         },
297
298         { "Congestion", pbx_builtin_congestion,
299         "Indicate the Congestion condition",
300         "  Congestion([timeout]): This application will indicate the congenstion\n"
301         "condition to the calling channel. If the optional timeout is specified, the\n"
302         "calling channel will be hung up after the specified number of seconds.\n"
303         "Otherwise, this application will wait until the calling channel hangs up.\n"
304         },
305
306         { "Goto", pbx_builtin_goto, 
307         "Jump to a particular priority, extension, or context",
308         "  Goto([[context|]extension|]priority): This application will cause the\n"
309         "calling channel to continue dialplan execution at the specified priority.\n"
310         "If no specific extension, or extension and context, are specified, then this\n"
311         "application will jump to the specified priority of the current extension.\n"
312         "  If the attempt to jump to another location in the dialplan is not successful,\n"
313         "then the channel will continue at the next priority of the current extension.\n"
314         },
315
316         { "GotoIf", pbx_builtin_gotoif,
317         "Conditional goto",
318         "  GotoIf(condition?[labeliftrue]:[labeliffalse]): This application will cause\n"
319         "the calling channel to jump to the specified location in the dialplan based on\n"
320         "the evaluation of the given condition. The channel will continue at\n"
321         "'labeliftrue' if the condition is true, or 'labeliffalse' if the condition is\n"
322         "false. The labels are specified with the same syntax as used within the Goto\n"
323         "application.  If the label chosen by the condition is omitted, no jump is\n"
324         "performed, but execution continues with the next priority in the dialplan.\n"
325         },
326
327         { "GotoIfTime", pbx_builtin_gotoiftime,
328         "Conditional Goto based on the current time",
329         "  GotoIfTime(<times>|<weekdays>|<mdays>|<months>?[[context|]exten|]priority):\n"
330         "This application will have the calling channel jump to the speicified location\n"
331         "int the dialplan if the current time matches the given time specification.\n"
332         "Further information on the time specification can be found in examples\n"
333         "illustrating how to do time-based context includes in the dialplan.\n" 
334         },
335
336         { "ExecIfTime", pbx_builtin_execiftime,
337         "Conditional application execution based on the current time",
338         "  ExecIfTime(<times>|<weekdays>|<mdays>|<months>?appname[|appargs]):\n"
339         "This application will execute the specified dialplan application, with optional\n"
340         "arguments, if the current time matches the given time specification. Further\n"
341         "information on the time speicification can be found in examples illustrating\n"
342         "how to do time-based context includes in the dialplan.\n"
343         },
344         
345         { "Hangup", pbx_builtin_hangup,
346         "Hang up the calling channel",
347         "  Hangup(): This application will hang up the calling channel.\n"
348         },
349
350         { "NoOp", pbx_builtin_noop,
351         "Do Nothing",
352         "  NoOp(): This applicatiion does nothing. However, it is useful for debugging\n"
353         "purposes. Any text that is provided as arguments to this application can be\n"
354         "viewed at the Asterisk CLI. This method can be used to see the evaluations of\n"
355         "variables or functions without having any effect." 
356         },
357
358         { "Progress", pbx_builtin_progress,
359         "Indicate progress",
360         "  Progress(): This application will request that in-band progress information\n"
361         "be provided to the calling channel.\n"
362         },
363
364         { "ResetCDR", pbx_builtin_resetcdr,
365         "Resets the Call Data Record",
366         "  ResetCDR([options]):  This application causes the Call Data Record to be\n"
367         "reset.\n"
368         "  Options:\n"
369         "    w -- Store the current CDR record before resetting it.\n"
370         "    a -- Store any stacked records.\n"
371         "    v -- Save CDR variables.\n"
372         },
373
374         { "Ringing", pbx_builtin_ringing,
375         "Indicate ringing tone",
376         "  Ringing(): This application will request that the channel indicate a ringing\n"
377         "tone to the user.\n"
378         },
379
380         { "SayNumber", pbx_builtin_saynumber,
381         "Say Number",
382         "  SayNumber(digits[,gender]): This application will play the sounds that\n"
383         "correspond to the given number. Optionally, a gender may be specified.\n"
384         "This will use the language that is currently set for the channel. See the\n"
385         "LANGUAGE function for more information on setting the language for the channel.\n"     
386         },
387
388         { "SayDigits", pbx_builtin_saydigits,
389         "Say Digits",
390         "  SayDigits(digits): This application will play the sounds that correspond\n"
391         "to the digits of the given number. This will use the language that is currently\n"
392         "set for the channel. See the LANGUAGE function for more information on setting\n"
393         "the language for the channel.\n"
394         },
395
396         { "SayAlpha", pbx_builtin_saycharacters,
397         "Say Alpha",
398         "  SayAlpha(string): This application will play the sounds that correspond to\n"
399         "the letters of the given string.\n" 
400         },
401
402         { "SayPhonetic", pbx_builtin_sayphonetic,
403         "Say Phonetic",
404         "  SayPhonetic(string): This application will play the sounds from the phonetic\n"
405         "alphabet that correspond to the letters in the given string.\n"
406         },
407
408         { "SetAMAFlags", pbx_builtin_setamaflags,
409         "Set the AMA Flags",
410         "  SetAMAFlags([flag]): This channel will set the channel's AMA Flags for billing\n"
411         "purposes.\n"
412         },
413
414         { "SetGlobalVar", pbx_builtin_setglobalvar,
415         "Set a global variable to a given value",
416         "  SetGlobalVar(variable=value): This application sets a given global variable to\n"
417         "the specified value.\n"
418         },
419
420         { "Set", pbx_builtin_setvar,
421         "Set channel variable(s) or function value(s)",
422         "  Set(name1=value1|name2=value2|..[|options])\n"
423         "This function can be used to set the value of channel variables or dialplan\n"
424         "functions. It will accept up to 24 name/value pairs. When setting variables,\n"
425         "if the variable name is prefixed with _, the variable will be inherited into\n"
426         "channels created from the current channel. If the variable name is prefixed\n"
427         "with __, the variable will be inherited into channels created from the current\n"
428         "channel and all children channels.\n"
429         "  Options:\n" 
430         "    g - Set variable globally instead of on the channel\n"
431         "        (applies only to variables, not functions)\n"
432         },
433
434         { "ImportVar", pbx_builtin_importvar,
435         "Import a variable from a channel into a new variable",
436         "  ImportVar(newvar=channelname|variable): This application imports a variable\n"
437         "from the specified channel (as opposed to the current one) and stores it as\n"
438         "a variable in the current channel (the channel that is calling this\n"
439         "application). Variables created by this application have the same inheritance\n"
440         "properties as those created with the Set application. See the documentation for\n"
441         "Set for more information.\n"
442         },
443
444         { "Wait", pbx_builtin_wait, 
445         "Waits for some time", 
446         "  Wait(seconds): This application waits for a specified number of seconds.\n"
447         "Then, dialplan execution will continue at the next priority.\n"
448         "  Note that the seconds can be passed with fractions of a second. For example,\n"
449         "'1.5' will ask the application to wait for 1.5 seconds.\n" 
450         },
451
452         { "WaitExten", pbx_builtin_waitexten, 
453         "Waits for an extension to be entered", 
454         "  WaitExten([seconds][|options]): This application waits for the user to enter\n"
455         "a new extension for a specified number of seconds.\n"
456         "  Note that the seconds can be passed with fractions of a second. For example,\n"
457         "'1.5' will ask the application to wait for 1.5 seconds.\n" 
458         "  Options:\n"
459         "    m[(x)] - Provide music on hold to the caller while waiting for an extension.\n"
460         "               Optionally, specify the class for music on hold within parenthesis.\n"
461         },
462
463 };
464
465 static struct ast_context *contexts = NULL;
466 AST_MUTEX_DEFINE_STATIC(conlock);               /*!< Lock for the ast_context list */
467 static struct ast_app *apps = NULL;
468 AST_MUTEX_DEFINE_STATIC(applock);               /*!< Lock for the application list */
469
470 struct ast_switch *switches = NULL;
471 AST_MUTEX_DEFINE_STATIC(switchlock);            /*!< Lock for switches */
472
473 static int stateid = 1;
474 /* WARNING:
475    When holding this list's lock, do _not_ do anything that will cause conlock
476    to be taken, unless you _already_ hold it. The ast_merge_contexts_and_delete
477    function will take the locks in conlock/hints order, so any other
478    paths that require both locks must also take them in that order.
479 */
480 static AST_LIST_HEAD_STATIC(hints, ast_hint);
481 struct ast_state_cb *statecbs = NULL;
482
483 /* 
484    \note This function is special. It saves the stack so that no matter
485    how many times it is called, it returns to the same place */
486 int pbx_exec(struct ast_channel *c,             /*!< Channel */
487                 struct ast_app *app,            /*!< Application */
488                 void *data)                     /*!< Data for execution */
489 {
490         int res;
491         
492         const char *saved_c_appl;
493         const char *saved_c_data;
494         
495         if (c->cdr)
496                 ast_cdr_setapp(c->cdr, app->name, data);
497
498         /* save channel values */
499         saved_c_appl= c->appl;
500         saved_c_data= c->data;
501
502         c->appl = app->name;
503         c->data = data;
504         /* XXX remember what to to when we have linked apps to modules */
505         if (app->module) {
506                 /* XXX LOCAL_USER_ADD(app->module) */
507         }
508         res = app->execute(c, data);
509         if (app->module) {
510                 /* XXX LOCAL_USER_REMOVE(app->module) */
511         }
512         /* restore channel values */
513         c->appl = saved_c_appl;
514         c->data = saved_c_data;
515         return res;
516 }
517
518
519 /*! Go no deeper than this through includes (not counting loops) */
520 #define AST_PBX_MAX_STACK       128
521
522 #define HELPER_EXISTS 0
523 #define HELPER_SPAWN 1
524 #define HELPER_CANMATCH 3
525 #define HELPER_MATCHMORE 4
526 #define HELPER_FINDLABEL 5
527
528 /*! \brief Find application handle in linked list
529  */
530 struct ast_app *pbx_findapp(const char *app) 
531 {
532         struct ast_app *tmp;
533
534         if (ast_mutex_lock(&applock)) {
535                 ast_log(LOG_WARNING, "Unable to obtain application lock\n");
536                 return NULL;
537         }
538         for (tmp = apps; tmp; tmp = tmp->next) {
539                 if (!strcasecmp(tmp->name, app))
540                         break;
541         }
542         ast_mutex_unlock(&applock);
543         return tmp;
544 }
545
546 static struct ast_switch *pbx_findswitch(const char *sw)
547 {
548         struct ast_switch *asw;
549
550         if (ast_mutex_lock(&switchlock)) {
551                 ast_log(LOG_WARNING, "Unable to obtain application lock\n");
552                 return NULL;
553         }
554         for (asw = switches; asw; asw = asw->next) {
555                 if (!strcasecmp(asw->name, sw))
556                         break;
557         }
558         ast_mutex_unlock(&switchlock);
559         return asw;
560 }
561
562 static inline int include_valid(struct ast_include *i)
563 {
564         if (!i->hastime)
565                 return 1;
566
567         return ast_check_timing(&(i->timing));
568 }
569
570 static void pbx_destroy(struct ast_pbx *p)
571 {
572         free(p);
573 }
574
575 #define EXTENSION_MATCH_CORE(data,pattern,match) {\
576         /* All patterns begin with _ */\
577         if (pattern[0] != '_') \
578                 return 0;\
579         /* Start optimistic */\
580         match=1;\
581         pattern++;\
582         while(match && *data && *pattern && (*pattern != '/')) {\
583                 while (*data == '-' && (*(data+1) != '\0')) data++;\
584                 switch(toupper(*pattern)) {\
585                 case '[': \
586                 {\
587                         int i,border=0;\
588                         char *where;\
589                         match=0;\
590                         pattern++;\
591                         where=strchr(pattern,']');\
592                         if (where)\
593                                 border=(int)(where-pattern);\
594                         if (!where || border > strlen(pattern)) {\
595                                 ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");\
596                                 return match;\
597                         }\
598                         for (i=0; i<border; i++) {\
599                                 int res=0;\
600                                 if (i+2<border)\
601                                         if (pattern[i+1]=='-') {\
602                                                 if (*data >= pattern[i] && *data <= pattern[i+2]) {\
603                                                         res=1;\
604                                                 } else {\
605                                                         i+=2;\
606                                                         continue;\
607                                                 }\
608                                         }\
609                                 if (res==1 || *data==pattern[i]) {\
610                                         match = 1;\
611                                         break;\
612                                 }\
613                         }\
614                         pattern+=border;\
615                         break;\
616                 }\
617                 case 'N':\
618                         if ((*data < '2') || (*data > '9'))\
619                                 match=0;\
620                         break;\
621                 case 'X':\
622                         if ((*data < '0') || (*data > '9'))\
623                                 match = 0;\
624                         break;\
625                 case 'Z':\
626                         if ((*data < '1') || (*data > '9'))\
627                                 match = 0;\
628                         break;\
629                 case '.':\
630                         /* Must match */\
631                         return 1;\
632                 case '!':\
633                         /* Early match */\
634                         return 2;\
635                 case ' ':\
636                 case '-':\
637                         /* Ignore these characters */\
638                         data--;\
639                         break;\
640                 default:\
641                         if (*data != *pattern)\
642                                 match =0;\
643                 }\
644                 data++;\
645                 pattern++;\
646         }\
647         /* If we ran off the end of the data and the pattern ends in '!', match */\
648         if (match && !*data && (*pattern == '!'))\
649                 return 2;\
650 }
651
652 int ast_extension_match(const char *pattern, const char *data)
653 {
654         int match;
655         /* If they're the same return */
656         if (!strcmp(pattern, data))
657                 return 1;
658         EXTENSION_MATCH_CORE(data,pattern,match);
659         /* Must be at the end of both */
660         if (*data || (*pattern && (*pattern != '/')))
661                 match = 0;
662         return match;
663 }
664
665 int ast_extension_close(const char *pattern, const char *data, int needmore)
666 {
667         int match;
668         /* If "data" is longer, it can'be a subset of pattern unless
669            pattern is a pattern match */
670         if ((strlen(pattern) < strlen(data)) && (pattern[0] != '_'))
671                 return 0;
672         
673         if ((ast_strlen_zero((char *)data) || !strncasecmp(pattern, data, strlen(data))) && 
674                 (!needmore || (strlen(pattern) > strlen(data)))) {
675                 return 1;
676         }
677         EXTENSION_MATCH_CORE(data,pattern,match);
678         /* If there's more or we don't care about more, or if it's a possible early match, 
679            return non-zero; otherwise it's a miss */
680         if (!needmore || *pattern || match == 2) {
681                 return match;
682         } else
683                 return 0;
684 }
685
686 struct ast_context *ast_context_find(const char *name)
687 {
688         struct ast_context *tmp;
689         ast_mutex_lock(&conlock);
690         if (name) {
691                 for (tmp = contexts; tmp; tmp = tmp->next) {
692                         if (!strcasecmp(name, tmp->name))
693                                 break;
694                 }
695         } else
696                 tmp = contexts;
697         ast_mutex_unlock(&conlock);
698         return tmp;
699 }
700
701 #define STATUS_NO_CONTEXT       1
702 #define STATUS_NO_EXTENSION     2
703 #define STATUS_NO_PRIORITY      3
704 #define STATUS_NO_LABEL         4
705 #define STATUS_SUCCESS          5
706
707 static int matchcid(const char *cidpattern, const char *callerid)
708 {
709         int failresult;
710         
711         /* If the Caller*ID pattern is empty, then we're matching NO Caller*ID, so
712            failing to get a number should count as a match, otherwise not */
713
714         if (!ast_strlen_zero(cidpattern))
715                 failresult = 0;
716         else
717                 failresult = 1;
718
719         if (!callerid)
720                 return failresult;
721
722         return ast_extension_match(cidpattern, callerid);
723 }
724
725 static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast_context *bypass, const char *context, const char *exten, int priority, const char *label, const char *callerid, int action, char *incstack[], int *stacklen, int *status, struct ast_switch **swo, char **data, const char **foundcontext)
726 {
727         int x, res;
728         struct ast_context *tmp;
729         struct ast_exten *e, *eroot;
730         struct ast_include *i;
731         struct ast_sw *sw;
732         struct ast_switch *asw;
733
734         /* Initialize status if appropriate */
735         if (!*stacklen) {
736                 *status = STATUS_NO_CONTEXT;
737                 *swo = NULL;
738                 *data = NULL;
739         }
740         /* Check for stack overflow */
741         if (*stacklen >= AST_PBX_MAX_STACK) {
742                 ast_log(LOG_WARNING, "Maximum PBX stack exceeded\n");
743                 return NULL;
744         }
745         /* Check first to see if we've already been checked */
746         for (x = 0; x < *stacklen; x++) {
747                 if (!strcasecmp(incstack[x], context))
748                         return NULL;
749         }
750         if (bypass)
751                 tmp = bypass;
752         else
753                 tmp = contexts;
754         for (; tmp; tmp = tmp->next) {
755                 /* Match context */
756                 if (bypass || !strcmp(tmp->name, context)) {
757                         struct ast_exten *earlymatch = NULL;
758
759                         if (*status < STATUS_NO_EXTENSION)
760                                 *status = STATUS_NO_EXTENSION;
761                         for (eroot = tmp->root; eroot; eroot = eroot->next) {
762                                 int match = 0;
763                                 /* Match extension */
764                                 if ((((action != HELPER_MATCHMORE) && ast_extension_match(eroot->exten, exten)) ||
765                                      ((action == HELPER_CANMATCH) && (ast_extension_close(eroot->exten, exten, 0))) ||
766                                      ((action == HELPER_MATCHMORE) && (match = ast_extension_close(eroot->exten, exten, 1)))) &&
767                                      (!eroot->matchcid || matchcid(eroot->cidmatch, callerid))) {
768
769                                         if (action == HELPER_MATCHMORE && match == 2 && !earlymatch) {
770                                                 /* It matched an extension ending in a '!' wildcard
771                                                    So ignore it for now, unless there's a better match */
772                                                 earlymatch = eroot;
773                                         } else {
774                                                 if (*status < STATUS_NO_PRIORITY)
775                                                         *status = STATUS_NO_PRIORITY;
776                                                 for (e = eroot; e; e = e->peer) {
777                                                         /* Match priority */
778                                                         if (action == HELPER_FINDLABEL) {
779                                                                 if (*status < STATUS_NO_LABEL)
780                                                                         *status = STATUS_NO_LABEL;
781                                                                 if (label && e->label && !strcmp(label, e->label)) {
782                                                                         *status = STATUS_SUCCESS;
783                                                                         *foundcontext = context;
784                                                                         return e;
785                                                                 }
786                                                         } else if (e->priority == priority) {
787                                                                 *status = STATUS_SUCCESS;
788                                                                 *foundcontext = context;
789                                                                 return e;
790                                                         }
791                                                 }
792                                         }
793                                 }
794                         }
795                         if (earlymatch) {
796                                 /* Bizarre logic for HELPER_MATCHMORE. We return zero to break out 
797                                    of the loop waiting for more digits, and _then_ match (normally)
798                                    the extension we ended up with. We got an early-matching wildcard
799                                    pattern, so return NULL to break out of the loop. */
800                                 return NULL;
801                         }
802                         /* Check alternative switches */
803                         for (sw = tmp->alts; sw; sw = sw->next) {
804                                 if ((asw = pbx_findswitch(sw->name))) {
805                                         /* Substitute variables now */
806                                         if (sw->eval) 
807                                                 pbx_substitute_variables_helper(chan, sw->data, sw->tmpdata, SWITCH_DATA_LENGTH - 1);
808                                         if (action == HELPER_CANMATCH)
809                                                 res = asw->canmatch ? asw->canmatch(chan, context, exten, priority, callerid, sw->eval ? sw->tmpdata : sw->data) : 0;
810                                         else if (action == HELPER_MATCHMORE)
811                                                 res = asw->matchmore ? asw->matchmore(chan, context, exten, priority, callerid, sw->eval ? sw->tmpdata : sw->data) : 0;
812                                         else
813                                                 res = asw->exists ? asw->exists(chan, context, exten, priority, callerid, sw->eval ? sw->tmpdata : sw->data) : 0;
814                                         if (res) {
815                                                 /* Got a match */
816                                                 *swo = asw;
817                                                 *data = sw->eval ? sw->tmpdata : sw->data;
818                                                 *foundcontext = context;
819                                                 return NULL;
820                                         }
821                                 } else {
822                                         ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
823                                 }
824                         }
825                         /* Setup the stack */
826                         incstack[*stacklen] = tmp->name;
827                         (*stacklen)++;
828                         /* Now try any includes we have in this context */
829                         for (i = tmp->includes; i; i = i->next) {
830                                 if (include_valid(i)) {
831                                         if ((e = pbx_find_extension(chan, bypass, i->rname, exten, priority, label, callerid, action, incstack, stacklen, status, swo, data, foundcontext))) 
832                                                 return e;
833                                         if (*swo) 
834                                                 return NULL;
835                                 }
836                         }
837                         break;
838                 }
839         }
840         return NULL;
841 }
842
843 /* Note that it's negative -- that's important later. */
844 #define DONT_HAVE_LENGTH        0x80000000
845
846 /*! \brief extract offset:length from variable name.
847  * Returns 1 if there is a offset:length part, which is
848  * trimmed off (values go into variables)
849  */
850 static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
851 {
852         int parens=0;
853
854         *offset = 0;
855         *length = DONT_HAVE_LENGTH;
856         *isfunc = 0;
857         for (; *var; var++) {
858                 if (*var == '(') {
859                         (*isfunc)++;
860                         parens++;
861                 } else if (*var == ')') {
862                         parens--;
863                 } else if (*var == ':' && parens == 0) {
864                         *var++ = '\0';
865                         sscanf(var, "%d:%d", offset, length);
866                         return 1; /* offset:length valid */
867                 }
868         }
869         return 0;
870 }
871
872 /*! \brief takes a substring. It is ok to call with value == workspace.
873  *
874  * offset < 0 means start from the end of the string and set the beginning
875  *   to be that many characters back.
876  * length is the length of the substring, -1 means unlimited
877  * (we take any negative value).
878  * Always return a copy in workspace.
879  */
880 static char *substring(const char *value, int offset, int length, char *workspace, size_t workspace_len)
881 {
882         char *ret = workspace;
883         int lr; /* length of the input string after the copy */
884
885         ast_copy_string(workspace, value, workspace_len); /* always make a copy */
886
887         if (offset == 0 && length < 0)  /* take the whole string */
888                 return ret;
889
890         lr = strlen(ret); /* compute length after copy, so we never go out of the workspace */
891
892         if (offset < 0) {       /* translate negative offset into positive ones */
893                 offset = lr + offset;
894                 if (offset < 0) /* If the negative offset was greater than the length of the string, just start at the beginning */
895                         offset = 0;
896         }
897
898         /* too large offset result in empty string so we know what to return */
899         if (offset >= lr)
900                 return ret + lr;        /* the final '\0' */
901
902         ret += offset;          /* move to the start position */
903         if (length >= 0 && length < lr - offset)        /* truncate if necessary */
904                 ret[length] = '\0';
905
906         return ret;
907 }
908
909 /*! \brief  pbx_retrieve_variable: Support for Asterisk built-in variables and
910       functions in the dialplan
911   ---*/
912 void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp)
913 {
914         const char not_found = '\0';
915         char *tmpvar;
916         const char *s;  /* the result */
917         int offset, length;
918         int i, need_substring;
919         struct varshead *places[2] = { headp, &globals };       /* list of places where we may look */
920
921         if (c) {
922                 places[0] = &c->varshead;
923         }
924         /*
925          * Make a copy of var because parse_variable_name() modifies the string.
926          * Then if called directly, we might need to run substring() on the result;
927          * remember this for later in 'need_substring', 'offset' and 'length'
928          */
929         tmpvar = ast_strdupa(var);      /* parse_variable_name modifies the string */
930         need_substring = parse_variable_name(tmpvar, &offset, &length, &i /* ignored */);
931
932         /*
933          * Look first into predefined variables, then into variable lists.
934          * Variable 's' points to the result, according to the following rules:
935          * s == &not_found (set at the beginning) means that we did not find a
936          *      matching variable and need to look into more places.
937          * If s != &not_found, s is a valid result string as follows:
938          * s = NULL if the variable does not have a value;
939          *      you typically do this when looking for an unset predefined variable.
940          * s = workspace if the result has been assembled there;
941          *      typically done when the result is built e.g. with an snprintf(),
942          *      so we don't need to do an additional copy.
943          * s != workspace in case we have a string, that needs to be copied
944          *      (the ast_copy_string is done once for all at the end).
945          *      Typically done when the result is already available in some string.
946          */
947         s = &not_found; /* default value */
948         if (c) {        /* This group requires a valid channel */
949                 /* Names with common parts are looked up a piece at a time using strncmp. */
950                 if (!strncmp(var, "CALL", 4)) {
951                         if (!strncmp(var + 4, "ING", 3)) {
952                                 if (!strcmp(var + 7, "PRES")) {                 /* CALLINGPRES */
953                                         snprintf(workspace, workspacelen, "%d", c->cid.cid_pres);
954                                         s = workspace;
955                                 } else if (!strcmp(var + 7, "ANI2")) {          /* CALLINGANI2 */
956                                         snprintf(workspace, workspacelen, "%d", c->cid.cid_ani2);
957                                         s = workspace;
958                                 } else if (!strcmp(var + 7, "TON")) {           /* CALLINGTON */
959                                         snprintf(workspace, workspacelen, "%d", c->cid.cid_ton);
960                                         s = workspace;
961                                 } else if (!strcmp(var + 7, "TNS")) {           /* CALLINGTNS */
962                                         snprintf(workspace, workspacelen, "%d", c->cid.cid_tns);
963                                         s = workspace;
964                                 }
965                         }
966                 } else if (!strcmp(var, "HINT")) {
967                         s = ast_get_hint(workspace, workspacelen, NULL, 0, c, c->context, c->exten) ? workspace : NULL;
968                 } else if (!strcmp(var, "HINTNAME")) {
969                         s = ast_get_hint(NULL, 0, workspace, workspacelen, c, c->context, c->exten) ? workspace : NULL;
970                 } else if (!strcmp(var, "EXTEN")) {
971                         s = c->exten;
972                 } else if (!strcmp(var, "CONTEXT")) {
973                         s = c->context;
974                 } else if (!strcmp(var, "PRIORITY")) {
975                         snprintf(workspace, workspacelen, "%d", c->priority);
976                         s = workspace;
977                 } else if (!strcmp(var, "CHANNEL")) {
978                         s = c->name;
979                 } else if (!strcmp(var, "UNIQUEID")) {
980                         s = c->uniqueid;
981                 } else if (!strcmp(var, "HANGUPCAUSE")) {
982                         snprintf(workspace, workspacelen, "%d", c->hangupcause);
983                         s = workspace;
984                 }
985         }
986         if (s == &not_found) { /* look for more */
987                 if (!strcmp(var, "EPOCH")) {
988                         snprintf(workspace, workspacelen, "%u",(int)time(NULL));
989                         s = workspace;
990                 } else if (!strcmp(var, "SYSTEMNAME")) {
991                         s = ast_config_AST_SYSTEM_NAME;
992                 }
993         }
994         /* if not found, look into chanvars or global vars */
995         for (i = 0; s == &not_found && i < (sizeof(places) / sizeof(places[0])); i++) {
996                 struct ast_var_t *variables;
997                 if (!places[i])
998                         continue;
999                 if (places[i] == &globals)
1000                         ast_mutex_lock(&globalslock);
1001                 AST_LIST_TRAVERSE(places[i], variables, entries) {
1002                         if (strcasecmp(ast_var_name(variables), var)==0) {
1003                                 s = ast_var_value(variables);
1004                                 break;
1005                         }
1006                 }
1007                 if (places[i] == &globals)
1008                         ast_mutex_unlock(&globalslock);
1009         }
1010         if (s == &not_found || s == NULL)
1011                 *ret = NULL;
1012         else {
1013                 if (s != workspace)
1014                         ast_copy_string(workspace, s, workspacelen);
1015                 *ret = workspace;
1016                 if (need_substring)
1017                         *ret = substring(*ret, offset, length, workspace, workspacelen);
1018         }
1019 }
1020
1021 /*! \brief CLI function to show installed custom functions 
1022     \addtogroup CLI_functions
1023  */
1024 static int handle_show_functions(int fd, int argc, char *argv[])
1025 {
1026         struct ast_custom_function *acf;
1027         int count_acf = 0;
1028         int like = 0;
1029
1030         if (argc == 4 && (!strcmp(argv[2], "like")) ) {
1031                 like = 1;
1032         } else if (argc != 2) {
1033                 return RESULT_SHOWUSAGE;
1034         }
1035
1036         ast_cli(fd, "%s Custom Functions:\n--------------------------------------------------------------------------------\n", like ? "Matching" : "Installed");
1037         
1038         for (acf = acf_root ; acf; acf = acf->next) {
1039                 if (!like || strstr(acf->name, argv[3])) {
1040                         count_acf++;
1041                         ast_cli(fd, "%-20.20s  %-35.35s  %s\n", acf->name, acf->syntax, acf->synopsis);
1042                 }
1043         }
1044
1045         ast_cli(fd, "%d %scustom functions installed.\n", count_acf, like ? "matching " : "");
1046
1047         return 0;
1048 }
1049
1050 static int handle_show_function(int fd, int argc, char *argv[])
1051 {
1052         struct ast_custom_function *acf;
1053         /* Maximum number of characters added by terminal coloring is 22 */
1054         char infotitle[64 + AST_MAX_APP + 22], syntitle[40], destitle[40];
1055         char info[64 + AST_MAX_APP], *synopsis = NULL, *description = NULL;
1056         char stxtitle[40], *syntax = NULL;
1057         int synopsis_size, description_size, syntax_size;
1058
1059         if (argc < 3)
1060                 return RESULT_SHOWUSAGE;
1061
1062         if (!(acf = ast_custom_function_find(argv[2]))) {
1063                 ast_cli(fd, "No function by that name registered.\n");
1064                 return RESULT_FAILURE;
1065
1066         }
1067
1068         if (acf->synopsis)
1069                 synopsis_size = strlen(acf->synopsis) + 23;
1070         else
1071                 synopsis_size = strlen("Not available") + 23;
1072         synopsis = alloca(synopsis_size);
1073         
1074         if (acf->desc)
1075                 description_size = strlen(acf->desc) + 23;
1076         else
1077                 description_size = strlen("Not available") + 23;
1078         description = alloca(description_size);
1079
1080         if (acf->syntax)
1081                 syntax_size = strlen(acf->syntax) + 23;
1082         else
1083                 syntax_size = strlen("Not available") + 23;
1084         syntax = alloca(syntax_size);
1085
1086         snprintf(info, 64 + AST_MAX_APP, "\n  -= Info about function '%s' =- \n\n", acf->name);
1087         term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
1088         term_color(stxtitle, "[Syntax]\n", COLOR_MAGENTA, 0, 40);
1089         term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
1090         term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
1091         term_color(syntax,
1092                    acf->syntax ? acf->syntax : "Not available",
1093                    COLOR_CYAN, 0, syntax_size);
1094         term_color(synopsis,
1095                    acf->synopsis ? acf->synopsis : "Not available",
1096                    COLOR_CYAN, 0, synopsis_size);
1097         term_color(description,
1098                    acf->desc ? acf->desc : "Not available",
1099                    COLOR_CYAN, 0, description_size);
1100         
1101         ast_cli(fd,"%s%s%s\n\n%s%s\n\n%s%s\n", infotitle, stxtitle, syntax, syntitle, synopsis, destitle, description);
1102
1103         return RESULT_SUCCESS;
1104 }
1105
1106 static char *complete_show_function(const char *line, const char *word, int pos, int state)
1107 {
1108         struct ast_custom_function *acf;
1109         char *ret = NULL;
1110         int which = 0;
1111         int wordlen = strlen(word);
1112
1113         /* try to lock functions list ... */
1114         if (ast_mutex_lock(&acflock)) {
1115                 ast_log(LOG_ERROR, "Unable to lock function list\n");
1116                 return NULL;
1117         }
1118
1119         /* case-insensitive for convenience in this 'complete' function */
1120         for (acf = acf_root; acf && !ret; acf = acf->next) {
1121                 if (!strncasecmp(word, acf->name, wordlen) && ++which > state)
1122                         ret = strdup(acf->name);
1123         }
1124
1125         ast_mutex_unlock(&acflock);
1126
1127         return ret; 
1128 }
1129
1130 struct ast_custom_function* ast_custom_function_find(const char *name) 
1131 {
1132         struct ast_custom_function *acfptr;
1133
1134         /* try to lock functions list ... */
1135         if (ast_mutex_lock(&acflock)) {
1136                 ast_log(LOG_ERROR, "Unable to lock function list\n");
1137                 return NULL;
1138         }
1139
1140         for (acfptr = acf_root; acfptr; acfptr = acfptr->next) {
1141                 if (!strcmp(name, acfptr->name))
1142                         break;
1143         }
1144
1145         ast_mutex_unlock(&acflock);
1146         
1147         return acfptr;
1148 }
1149
1150 int ast_custom_function_unregister(struct ast_custom_function *acf) 
1151 {
1152         struct ast_custom_function *cur, *prev = NULL;
1153         int res = -1;
1154
1155         if (!acf)
1156                 return -1;
1157
1158         /* try to lock functions list ... */
1159         if (ast_mutex_lock(&acflock)) {
1160                 ast_log(LOG_ERROR, "Unable to lock function list\n");
1161                 return -1;
1162         }
1163
1164         for (cur = acf_root; cur; prev = cur, cur = cur->next) {
1165                 if (cur == acf) {
1166                         if (prev)
1167                                 prev->next = acf->next;
1168                         else
1169                                 acf_root = acf->next;
1170                         res = 0;
1171                         break;
1172                 }
1173         }
1174
1175         ast_mutex_unlock(&acflock);
1176
1177         if (!res && option_verbose > 1)
1178                 ast_verbose(VERBOSE_PREFIX_2 "Unregistered custom function %s\n", acf->name);
1179
1180         return res;
1181 }
1182
1183 int ast_custom_function_register(struct ast_custom_function *acf) 
1184 {
1185         struct ast_custom_function *cur, *last = NULL;
1186         int found = 0;
1187
1188         if (!acf)
1189                 return -1;
1190
1191         /* try to lock functions list ... */
1192         if (ast_mutex_lock(&acflock)) {
1193                 ast_log(LOG_ERROR, "Unable to lock function list. Failed registering function %s\n", acf->name);
1194                 return -1;
1195         }
1196
1197         if (ast_custom_function_find(acf->name)) {
1198                 ast_log(LOG_ERROR, "Function %s already registered.\n", acf->name);
1199                 ast_mutex_unlock(&acflock);
1200                 return -1;
1201         }
1202
1203         for (cur = acf_root; cur; cur = cur->next) {
1204                 if (strcmp(acf->name, cur->name) < 0) {
1205                         found = 1;
1206                         if (last) {
1207                                 acf->next = cur;
1208                                 last->next = acf;
1209                         } else {
1210                                 acf->next = acf_root;
1211                                 acf_root = acf;
1212                         }
1213                         break;
1214                 }
1215                 last = cur;
1216         }
1217
1218         /* Wasn't before anything else, put it at the end */
1219         if (!found) {
1220                 if (last)
1221                         last->next = acf;
1222                 else
1223                         acf_root = acf;
1224                 acf->next = NULL;
1225         }
1226
1227         ast_mutex_unlock(&acflock);
1228
1229         if (option_verbose > 1)
1230                 ast_verbose(VERBOSE_PREFIX_2 "Registered custom function %s\n", acf->name);
1231
1232         return 0;
1233 }
1234
1235 int ast_func_read(struct ast_channel *chan, char *function, char *workspace, size_t len)
1236 {
1237         char *args = NULL, *p;
1238         struct ast_custom_function *acfptr;
1239
1240         if ((args = strchr(function, '('))) {
1241                 *args++ = '\0';
1242                 if ((p = strrchr(args, ')')))
1243                         *p = '\0';
1244                 else
1245                         ast_log(LOG_WARNING, "Can't find trailing parenthesis?\n");
1246         } else {
1247                 ast_log(LOG_WARNING, "Function doesn't contain parentheses.  Assuming null argument.\n");
1248         }
1249
1250         if ((acfptr = ast_custom_function_find(function))) {
1251                 /* run the custom function */
1252                 if (acfptr->read)
1253                         return acfptr->read(chan, function, args, workspace, len);
1254                 else
1255                         ast_log(LOG_ERROR, "Function %s cannot be read\n", function);
1256         } else {
1257                 ast_log(LOG_ERROR, "Function %s not registered\n", function);
1258         }
1259
1260         return -1;
1261 }
1262
1263 int ast_func_write(struct ast_channel *chan, char *function, const char *value)
1264 {
1265         char *args = NULL, *p;
1266         struct ast_custom_function *acfptr;
1267
1268         if ((args = strchr(function, '('))) {
1269                 *args++ = '\0';
1270                 if ((p = strrchr(args, ')')))
1271                         *p = '\0';
1272                 else
1273                         ast_log(LOG_WARNING, "Can't find trailing parenthesis?\n");
1274         } else {
1275                 ast_log(LOG_WARNING, "Function doesn't contain parentheses.  Assuming null argument.\n");
1276         }
1277
1278         if ((acfptr = ast_custom_function_find(function))) {
1279                 /* run the custom function */
1280                 if (acfptr->write)
1281                         return acfptr->write(chan, function, args, value);
1282                 else
1283                         ast_log(LOG_ERROR, "Function %s is read-only, it cannot be written to\n", function);
1284         } else {
1285                 ast_log(LOG_ERROR, "Function %s not registered\n", function);
1286         }
1287
1288         return -1;
1289 }
1290
1291 static void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count)
1292 {
1293         char *cp4;
1294         const char *tmp, *whereweare;
1295         int length, offset, offset2, isfunction;
1296         char *workspace = NULL;
1297         char *ltmp = NULL, *var = NULL;
1298         char *nextvar, *nextexp, *nextthing;
1299         char *vars, *vare;
1300         int pos, brackets, needsub, len;
1301         
1302         /* Substitutes variables into cp2, based on string cp1, and assuming cp2 to be
1303            zero-filled */
1304         whereweare=tmp=cp1;
1305         while(!ast_strlen_zero(whereweare) && count) {
1306                 /* Assume we're copying the whole remaining string */
1307                 pos = strlen(whereweare);
1308                 nextvar = NULL;
1309                 nextexp = NULL;
1310                 nextthing = strchr(whereweare, '$');
1311                 if (nextthing) {
1312                         switch(nextthing[1]) {
1313                         case '{':
1314                                 nextvar = nextthing;
1315                                 pos = nextvar - whereweare;
1316                                 break;
1317                         case '[':
1318                                 nextexp = nextthing;
1319                                 pos = nextexp - whereweare;
1320                                 break;
1321                         }
1322                 }
1323
1324                 if (pos) {
1325                         /* Can't copy more than 'count' bytes */
1326                         if (pos > count)
1327                                 pos = count;
1328                         
1329                         /* Copy that many bytes */
1330                         memcpy(cp2, whereweare, pos);
1331                         
1332                         count -= pos;
1333                         cp2 += pos;
1334                         whereweare += pos;
1335                 }
1336                 
1337                 if (nextvar) {
1338                         /* We have a variable.  Find the start and end, and determine
1339                            if we are going to have to recursively call ourselves on the
1340                            contents */
1341                         vars = vare = nextvar + 2;
1342                         brackets = 1;
1343                         needsub = 0;
1344
1345                         /* Find the end of it */
1346                         while(brackets && *vare) {
1347                                 if ((vare[0] == '$') && (vare[1] == '{')) {
1348                                         needsub++;
1349                                         brackets++;
1350                                 } else if (vare[0] == '}') {
1351                                         brackets--;
1352                                 } else if ((vare[0] == '$') && (vare[1] == '['))
1353                                         needsub++;
1354                                 vare++;
1355                         }
1356                         if (brackets)
1357                                 ast_log(LOG_NOTICE, "Error in extension logic (missing '}')\n");
1358                         len = vare - vars - 1;
1359
1360                         /* Skip totally over variable string */
1361                         whereweare += (len + 3);
1362
1363                         if (!var)
1364                                 var = alloca(VAR_BUF_SIZE);
1365
1366                         /* Store variable name (and truncate) */
1367                         ast_copy_string(var, vars, len + 1);
1368
1369                         /* Substitute if necessary */
1370                         if (needsub) {
1371                                 if (!ltmp)
1372                                         ltmp = alloca(VAR_BUF_SIZE);
1373
1374                                 memset(ltmp, 0, VAR_BUF_SIZE);
1375                                 pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1);
1376                                 vars = ltmp;
1377                         } else {
1378                                 vars = var;
1379                         }
1380
1381                         if (!workspace)
1382                                 workspace = alloca(VAR_BUF_SIZE);
1383
1384                         workspace[0] = '\0';
1385
1386                         parse_variable_name(vars, &offset, &offset2, &isfunction);
1387                         if (isfunction) {
1388                                 /* Evaluate function */
1389                                 cp4 = ast_func_read(c, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace;
1390
1391                                 ast_log(LOG_DEBUG, "Function result is '%s'\n", cp4 ? cp4 : "(null)");
1392                         } else {
1393                                 /* Retrieve variable value */
1394                                 pbx_retrieve_variable(c, vars, &cp4, workspace, VAR_BUF_SIZE, headp);
1395                         }
1396                         if (cp4) {
1397                                 cp4 = substring(cp4, offset, offset2, workspace, VAR_BUF_SIZE);
1398
1399                                 length = strlen(cp4);
1400                                 if (length > count)
1401                                         length = count;
1402                                 memcpy(cp2, cp4, length);
1403                                 count -= length;
1404                                 cp2 += length;
1405                         }
1406                 } else if (nextexp) {
1407                         /* We have an expression.  Find the start and end, and determine
1408                            if we are going to have to recursively call ourselves on the
1409                            contents */
1410                         vars = vare = nextexp + 2;
1411                         brackets = 1;
1412                         needsub = 0;
1413
1414                         /* Find the end of it */
1415                         while(brackets && *vare) {
1416                                 if ((vare[0] == '$') && (vare[1] == '[')) {
1417                                         needsub++;
1418                                         brackets++;
1419                                         vare++;
1420                                 } else if (vare[0] == '[') {
1421                                         brackets++;
1422                                 } else if (vare[0] == ']') {
1423                                         brackets--;
1424                                 } else if ((vare[0] == '$') && (vare[1] == '{')) {
1425                                         needsub++;
1426                                         vare++;
1427                                 }
1428                                 vare++;
1429                         }
1430                         if (brackets)
1431                                 ast_log(LOG_NOTICE, "Error in extension logic (missing ']')\n");
1432                         len = vare - vars - 1;
1433                         
1434                         /* Skip totally over expression */
1435                         whereweare += (len + 3);
1436                         
1437                         if (!var)
1438                                 var = alloca(VAR_BUF_SIZE);
1439
1440                         /* Store variable name (and truncate) */
1441                         ast_copy_string(var, vars, len + 1);
1442                         
1443                         /* Substitute if necessary */
1444                         if (needsub) {
1445                                 if (!ltmp)
1446                                         ltmp = alloca(VAR_BUF_SIZE);
1447
1448                                 memset(ltmp, 0, VAR_BUF_SIZE);
1449                                 pbx_substitute_variables_helper_full(c, headp, var, ltmp, VAR_BUF_SIZE - 1);
1450                                 vars = ltmp;
1451                         } else {
1452                                 vars = var;
1453                         }
1454
1455                         length = ast_expr(vars, cp2, count);
1456
1457                         if (length) {
1458                                 ast_log(LOG_DEBUG, "Expression result is '%s'\n", cp2);
1459                                 count -= length;
1460                                 cp2 += length;
1461                         }
1462                 } else
1463                         break;
1464         }
1465 }
1466
1467 void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
1468 {
1469         pbx_substitute_variables_helper_full(c, (c) ? &c->varshead : NULL, cp1, cp2, count);
1470 }
1471
1472 void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, char *cp2, int count)
1473 {
1474         pbx_substitute_variables_helper_full(NULL, headp, cp1, cp2, count);
1475 }
1476
1477 static void pbx_substitute_variables(char *passdata, int datalen, struct ast_channel *c, struct ast_exten *e)
1478 {
1479         memset(passdata, 0, datalen);
1480                 
1481         /* No variables or expressions in e->data, so why scan it? */
1482         if (!strchr(e->data, '$') && !strstr(e->data,"${") && !strstr(e->data,"$[") && !strstr(e->data,"$(")) {
1483                 ast_copy_string(passdata, e->data, datalen);
1484                 return;
1485         }
1486         
1487         pbx_substitute_variables_helper(c, e->data, passdata, datalen - 1);
1488 }                                                               
1489
1490 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, int action) 
1491 {
1492         struct ast_exten *e;
1493         struct ast_app *app;
1494         struct ast_switch *sw;
1495         char *data;
1496         const char *foundcontext=NULL;
1497         int res;
1498         int status = 0;
1499         char *incstack[AST_PBX_MAX_STACK];
1500         char passdata[EXT_DATA_SIZE];
1501         int stacklen = 0;
1502         char tmp[80];
1503         char tmp2[80];
1504         char tmp3[EXT_DATA_SIZE];
1505         char atmp[80];
1506         char atmp2[EXT_DATA_SIZE+100];
1507
1508         if (ast_mutex_lock(&conlock)) {
1509                 ast_log(LOG_WARNING, "Unable to obtain lock\n");
1510                 if ((action == HELPER_EXISTS) || (action == HELPER_CANMATCH) || (action == HELPER_MATCHMORE))
1511                         return 0;
1512                 else
1513                         return -1;
1514         }
1515         e = pbx_find_extension(c, con, context, exten, priority, label, callerid, action, incstack, &stacklen, &status, &sw, &data, &foundcontext);
1516         if (e) {
1517                 switch(action) {
1518                 case HELPER_CANMATCH:
1519                         ast_mutex_unlock(&conlock);
1520                         return -1;
1521                 case HELPER_EXISTS:
1522                         ast_mutex_unlock(&conlock);
1523                         return -1;
1524                 case HELPER_FINDLABEL:
1525                         res = e->priority;
1526                         ast_mutex_unlock(&conlock);
1527                         return res;
1528                 case HELPER_MATCHMORE:
1529                         ast_mutex_unlock(&conlock);
1530                         return -1;
1531                 case HELPER_SPAWN:
1532                         app = pbx_findapp(e->app);
1533                         ast_mutex_unlock(&conlock);
1534                         if (app) {
1535                                 if (c->context != context)
1536                                         ast_copy_string(c->context, context, sizeof(c->context));
1537                                 if (c->exten != exten)
1538                                         ast_copy_string(c->exten, exten, sizeof(c->exten));
1539                                 c->priority = priority;
1540                                 pbx_substitute_variables(passdata, sizeof(passdata), c, e);
1541                                 if (option_debug) {
1542                                                 ast_log(LOG_DEBUG, "Launching '%s'\n", app->name);
1543                                                 snprintf(atmp, 80, "STACK-%s-%s-%d", context, exten, priority);
1544                                                 snprintf(atmp2, EXT_DATA_SIZE+100, "%s(\"%s\", \"%s\") %s", app->name, c->name, passdata, "in new stack");
1545                                                 pbx_builtin_setvar_helper(c, atmp, atmp2);
1546                                 }
1547                                 if (option_verbose > 2)
1548                                                 ast_verbose( VERBOSE_PREFIX_3 "Executing %s(\"%s\", \"%s\") %s\n", 
1549                                                                 term_color(tmp, app->name, COLOR_BRCYAN, 0, sizeof(tmp)),
1550                                                                 term_color(tmp2, c->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
1551                                                                 term_color(tmp3, passdata, COLOR_BRMAGENTA, 0, sizeof(tmp3)),
1552                                                                 "in new stack");
1553                                 manager_event(EVENT_FLAG_CALL, "Newexten", 
1554                                         "Channel: %s\r\n"
1555                                         "Context: %s\r\n"
1556                                         "Extension: %s\r\n"
1557                                         "Priority: %d\r\n"
1558                                         "Application: %s\r\n"
1559                                         "AppData: %s\r\n"
1560                                         "Uniqueid: %s\r\n",
1561                                         c->name, c->context, c->exten, c->priority, app->name, passdata, c->uniqueid);
1562                                 res = pbx_exec(c, app, passdata);
1563                                 return res;
1564                         } else {
1565                                 ast_log(LOG_WARNING, "No application '%s' for extension (%s, %s, %d)\n", e->app, context, exten, priority);
1566                                 return -1;
1567                         }
1568                 default:
1569                         ast_log(LOG_WARNING, "Huh (%d)?\n", action);
1570                         return -1;
1571                 }
1572         } else if (sw) {
1573                 switch(action) {
1574                 case HELPER_CANMATCH:
1575                         ast_mutex_unlock(&conlock);
1576                         return -1;
1577                 case HELPER_EXISTS:
1578                         ast_mutex_unlock(&conlock);
1579                         return -1;
1580                 case HELPER_MATCHMORE:
1581                         ast_mutex_unlock(&conlock);
1582                         return -1;
1583                 case HELPER_FINDLABEL:
1584                         ast_mutex_unlock(&conlock);
1585                         return -1;
1586                 case HELPER_SPAWN:
1587                         ast_mutex_unlock(&conlock);
1588                         if (sw->exec)
1589                                 res = sw->exec(c, foundcontext ? foundcontext : context, exten, priority, callerid, data);
1590                         else {
1591                                 ast_log(LOG_WARNING, "No execution engine for switch %s\n", sw->name);
1592                                 res = -1;
1593                         }
1594                         return res;
1595                 default:
1596                         ast_log(LOG_WARNING, "Huh (%d)?\n", action);
1597                         return -1;
1598                 }
1599         } else {
1600                 ast_mutex_unlock(&conlock);
1601                 switch(status) {
1602                 case STATUS_NO_CONTEXT:
1603                         if ((action != HELPER_EXISTS) && (action != HELPER_MATCHMORE))
1604                                 ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", context);
1605                         break;
1606                 case STATUS_NO_EXTENSION:
1607                         if ((action != HELPER_EXISTS) && (action !=  HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
1608                                 ast_log(LOG_NOTICE, "Cannot find extension '%s' in context '%s'\n", exten, context);
1609                         break;
1610                 case STATUS_NO_PRIORITY:
1611                         if ((action != HELPER_EXISTS) && (action !=  HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
1612                                 ast_log(LOG_NOTICE, "No such priority %d in extension '%s' in context '%s'\n", priority, exten, context);
1613                         break;
1614                 case STATUS_NO_LABEL:
1615                         if (context)
1616                                 ast_log(LOG_NOTICE, "No such label '%s' in extension '%s' in context '%s'\n", label, exten, context);
1617                         break;
1618                 default:
1619                         ast_log(LOG_DEBUG, "Shouldn't happen!\n");
1620                 }
1621                 
1622                 if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
1623                         return -1;
1624                 else
1625                         return 0;
1626         }
1627
1628 }
1629
1630 /*! \brief  ast_hint_extension: Find hint for given extension in context */
1631 static struct ast_exten *ast_hint_extension(struct ast_channel *c, const char *context, const char *exten)
1632 {
1633         struct ast_exten *e;
1634         struct ast_switch *sw;
1635         char *data;
1636         const char *foundcontext = NULL;
1637         int status = 0;
1638         char *incstack[AST_PBX_MAX_STACK];
1639         int stacklen = 0;
1640
1641         if (ast_mutex_lock(&conlock)) {
1642                 ast_log(LOG_WARNING, "Unable to obtain lock\n");
1643                 return NULL;
1644         }
1645         e = pbx_find_extension(c, NULL, context, exten, PRIORITY_HINT, NULL, "", HELPER_EXISTS, incstack, &stacklen, &status, &sw, &data, &foundcontext);
1646         ast_mutex_unlock(&conlock);     
1647         return e;
1648 }
1649
1650 /*! \brief  ast_extensions_state2: Check state of extension by using hints */
1651 static int ast_extension_state2(struct ast_exten *e)
1652 {
1653         char hint[AST_MAX_EXTENSION];
1654         char *cur, *rest;
1655         int allunavailable = 1, allbusy = 1, allfree = 1;
1656         int busy = 0, inuse = 0, ring = 0;
1657
1658         if (!e)
1659                 return -1;
1660
1661         ast_copy_string(hint, ast_get_extension_app(e), sizeof(hint));
1662
1663         rest = hint;    /* One or more devices separated with a & character */
1664         while ( (cur = strsep(&rest, "&")) ) {
1665                 int res = ast_device_state(cur);
1666                 switch (res) {
1667                 case AST_DEVICE_NOT_INUSE:
1668                         allunavailable = 0;
1669                         allbusy = 0;
1670                         break;
1671                 case AST_DEVICE_INUSE:
1672                         inuse = 1;
1673                         allunavailable = 0;
1674                         allfree = 0;
1675                         break;
1676                 case AST_DEVICE_RINGING:
1677                         ring = 1;
1678                         allunavailable = 0;
1679                         allfree = 0;
1680                         break;
1681                 case AST_DEVICE_BUSY:
1682                         allunavailable = 0;
1683                         allfree = 0;
1684                         busy = 1;
1685                         break;
1686                 case AST_DEVICE_UNAVAILABLE:
1687                 case AST_DEVICE_INVALID:
1688                         allbusy = 0;
1689                         allfree = 0;
1690                         break;
1691                 default:
1692                         allunavailable = 0;
1693                         allbusy = 0;
1694                         allfree = 0;
1695                 }
1696         }
1697
1698         if (!inuse && ring)
1699                 return AST_EXTENSION_RINGING;
1700         if (inuse && ring)
1701                 return (AST_EXTENSION_INUSE | AST_EXTENSION_RINGING);
1702         if (inuse)
1703                 return AST_EXTENSION_INUSE;
1704         if (allfree)
1705                 return AST_EXTENSION_NOT_INUSE;
1706         if (allbusy)            
1707                 return AST_EXTENSION_BUSY;
1708         if (allunavailable)
1709                 return AST_EXTENSION_UNAVAILABLE;
1710         if (busy) 
1711                 return AST_EXTENSION_INUSE;
1712         
1713         return AST_EXTENSION_NOT_INUSE;
1714 }
1715
1716 /*! \brief  ast_extension_state2str: Return extension_state as string */
1717 const char *ast_extension_state2str(int extension_state)
1718 {
1719         int i;
1720
1721         for (i = 0; (i < (sizeof(extension_states) / sizeof(extension_states[0]))); i++) {
1722                 if (extension_states[i].extension_state == extension_state)
1723                         return extension_states[i].text;
1724         }
1725         return "Unknown";       
1726 }
1727
1728 /*! \brief  ast_extension_state: Check extension state for an extension by using hint */
1729 int ast_extension_state(struct ast_channel *c, const char *context, const char *exten)
1730 {
1731         struct ast_exten *e;
1732
1733         e = ast_hint_extension(c, context, exten);      /* Do we have a hint for this extension ? */ 
1734         if (!e) 
1735                 return -1;                              /* No hint, return -1 */
1736
1737         return ast_extension_state2(e);                 /* Check all devices in the hint */
1738 }
1739
1740 void ast_hint_state_changed(const char *device)
1741 {
1742         struct ast_hint *hint;
1743         struct ast_state_cb *cblist;
1744         char buf[AST_MAX_EXTENSION];
1745         char *parse;
1746         char *cur;
1747         int state;
1748
1749         AST_LIST_LOCK(&hints);
1750
1751         AST_LIST_TRAVERSE(&hints, hint, list) {
1752                 ast_copy_string(buf, ast_get_extension_app(hint->exten), sizeof(buf));
1753                 parse = buf;
1754                 for (cur = strsep(&parse, "&"); cur; cur = strsep(&parse, "&")) {
1755                         if (strcasecmp(cur, device))
1756                                 continue;
1757
1758                         /* Get device state for this hint */
1759                         state = ast_extension_state2(hint->exten);
1760                         
1761                         if ((state == -1) || (state == hint->laststate))
1762                                 continue;
1763
1764                         /* Device state changed since last check - notify the watchers */
1765                         
1766                         /* For general callbacks */
1767                         for (cblist = statecbs; cblist; cblist = cblist->next)
1768                                 cblist->callback(hint->exten->parent->name, hint->exten->exten, state, cblist->data);
1769                         
1770                         /* For extension callbacks */
1771                         for (cblist = hint->callbacks; cblist; cblist = cblist->next)
1772                                 cblist->callback(hint->exten->parent->name, hint->exten->exten, state, cblist->data);
1773                         
1774                         hint->laststate = state;
1775                         break;
1776                 }
1777         }
1778
1779         AST_LIST_UNLOCK(&hints);
1780 }
1781                         
1782 /*! \brief  ast_extension_state_add: Add watcher for extension states */
1783 int ast_extension_state_add(const char *context, const char *exten, 
1784                             ast_state_cb_type callback, void *data)
1785 {
1786         struct ast_hint *hint;
1787         struct ast_state_cb *cblist;
1788         struct ast_exten *e;
1789
1790         /* If there's no context and extension:  add callback to statecbs list */
1791         if (!context && !exten) {
1792                 AST_LIST_LOCK(&hints);
1793
1794                 for (cblist = statecbs; cblist; cblist = cblist->next) {
1795                         if (cblist->callback == callback) {
1796                                 cblist->data = data;
1797                                 AST_LIST_UNLOCK(&hints);
1798                                 return 0;
1799                         }
1800                 }
1801         
1802                 /* Now insert the callback */
1803                 if (!(cblist = ast_calloc(1, sizeof(*cblist)))) {
1804                         AST_LIST_UNLOCK(&hints);        
1805                         return -1;
1806                 }
1807                 cblist->id = 0;
1808                 cblist->callback = callback;
1809                 cblist->data = data;
1810         
1811                 cblist->next = statecbs;
1812                 statecbs = cblist;
1813
1814                 AST_LIST_UNLOCK(&hints);
1815                 return 0;
1816         }
1817
1818         if (!context || !exten)
1819                 return -1;
1820
1821         /* This callback type is for only one hint, so get the hint */
1822         e = ast_hint_extension(NULL, context, exten);    
1823         if (!e) {
1824                 return -1;
1825         }
1826
1827         /* Find the hint in the list of hints */
1828         AST_LIST_LOCK(&hints);
1829         
1830         AST_LIST_TRAVERSE(&hints, hint, list) {
1831                 if (hint->exten == e)
1832                         break;      
1833         }
1834
1835         if (!hint) {
1836                 /* We have no hint, sorry */
1837                 AST_LIST_UNLOCK(&hints);
1838                 return -1;
1839         }
1840
1841         /* Now insert the callback in the callback list  */
1842         if (!(cblist = ast_calloc(1, sizeof(*cblist)))) {
1843                 AST_LIST_UNLOCK(&hints);
1844                 return -1;
1845         }
1846         cblist->id = stateid++;         /* Unique ID for this callback */
1847         cblist->callback = callback;    /* Pointer to callback routine */
1848         cblist->data = data;            /* Data for the callback */
1849
1850         cblist->next = hint->callbacks;
1851         hint->callbacks = cblist;
1852
1853         AST_LIST_UNLOCK(&hints);
1854         return cblist->id;
1855 }
1856
1857 /*! \brief  ast_extension_state_del: Remove a watcher from the callback list */
1858 int ast_extension_state_del(int id, ast_state_cb_type callback)
1859 {
1860         struct ast_hint *hint;
1861         struct ast_state_cb *cblist, *cbprev;
1862
1863         if (!id && !callback)
1864                 return -1;
1865
1866         AST_LIST_LOCK(&hints);
1867
1868         /* id is zero is a callback without extension */
1869         if (!id) {
1870                 cbprev = NULL;
1871                 for (cblist = statecbs; cblist; cblist = cblist->next) {
1872                         if (cblist->callback == callback) {
1873                                 if (!cbprev)
1874                                         statecbs = cblist->next;
1875                                 else
1876                                         cbprev->next = cblist->next;
1877
1878                                 free(cblist);
1879
1880                                 AST_LIST_UNLOCK(&hints);
1881                                 return 0;
1882                         }
1883                         cbprev = cblist;
1884                 }
1885
1886                 AST_LIST_UNLOCK(&hints);
1887                 return -1;
1888         }
1889
1890         /* id greater than zero is a callback with extension */
1891         /* Find the callback based on ID */
1892         AST_LIST_TRAVERSE(&hints, hint, list) {
1893                 cbprev = NULL;
1894                 for (cblist = hint->callbacks; cblist; cblist = cblist->next) {
1895                         if (cblist->id==id) {
1896                                 if (!cbprev)
1897                                         hint->callbacks = cblist->next;         
1898                                 else
1899                                         cbprev->next = cblist->next;
1900                 
1901                                 free(cblist);
1902                 
1903                                 AST_LIST_UNLOCK(&hints);
1904                                 return 0;               
1905                         }               
1906                         cbprev = cblist;                                
1907                 }
1908         }
1909
1910         AST_LIST_UNLOCK(&hints);
1911         return -1;
1912 }
1913
1914 /*! \brief  ast_add_hint: Add hint to hint list, check initial extension state */
1915 static int ast_add_hint(struct ast_exten *e)
1916 {
1917         struct ast_hint *hint;
1918
1919         if (!e) 
1920                 return -1;
1921
1922         AST_LIST_LOCK(&hints);
1923
1924         /* Search if hint exists, do nothing */
1925         AST_LIST_TRAVERSE(&hints, hint, list) {
1926                 if (hint->exten == e) {
1927                         AST_LIST_UNLOCK(&hints);
1928                         if (option_debug > 1)
1929                                 ast_log(LOG_DEBUG, "HINTS: Not re-adding existing hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
1930                         return -1;
1931                 }
1932         }
1933
1934         if (option_debug > 1)
1935                 ast_log(LOG_DEBUG, "HINTS: Adding hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
1936
1937         if (!(hint = ast_calloc(1, sizeof(*hint)))) {
1938                 AST_LIST_UNLOCK(&hints);
1939                 return -1;
1940         }
1941         /* Initialize and insert new item at the top */
1942         hint->exten = e;
1943         hint->laststate = ast_extension_state2(e);
1944         AST_LIST_INSERT_HEAD(&hints, hint, list);
1945
1946         AST_LIST_UNLOCK(&hints);
1947         return 0;
1948 }
1949
1950 /*! \brief  ast_change_hint: Change hint for an extension */
1951 static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne)
1952
1953         struct ast_hint *hint;
1954         int res = -1;
1955
1956         AST_LIST_LOCK(&hints);
1957         AST_LIST_TRAVERSE(&hints, hint, list) {
1958                 if (hint->exten == oe) {
1959                         hint->exten = ne;
1960                         res = 0;
1961                         break;
1962                 }
1963         }
1964         AST_LIST_UNLOCK(&hints);
1965
1966         return res;
1967 }
1968
1969 /*! \brief  ast_remove_hint: Remove hint from extension */
1970 static int ast_remove_hint(struct ast_exten *e)
1971 {
1972         /* Cleanup the Notifys if hint is removed */
1973         struct ast_hint *hint;
1974         struct ast_state_cb *cblist, *cbprev;
1975         int res = -1;
1976
1977         if (!e) 
1978                 return -1;
1979
1980         AST_LIST_LOCK(&hints);
1981         AST_LIST_TRAVERSE_SAFE_BEGIN(&hints, hint, list) {
1982                 if (hint->exten == e) {
1983                         cbprev = NULL;
1984                         cblist = hint->callbacks;
1985                         while (cblist) {
1986                                 /* Notify with -1 and remove all callbacks */
1987                                 cbprev = cblist;            
1988                                 cblist = cblist->next;
1989                                 cbprev->callback(hint->exten->parent->name, hint->exten->exten, AST_EXTENSION_DEACTIVATED, cbprev->data);
1990                                 free(cbprev);
1991                         }
1992                         hint->callbacks = NULL;
1993                         AST_LIST_REMOVE_CURRENT(&hints, list);
1994                         free(hint);
1995                         res = 0;
1996                         break; 
1997                 }
1998         }
1999         AST_LIST_TRAVERSE_SAFE_END
2000         AST_LIST_UNLOCK(&hints);
2001         
2002         return res;
2003 }
2004
2005
2006 /*! \brief  ast_get_hint: Get hint for channel */
2007 int ast_get_hint(char *hint, int hintsize, char *name, int namesize, struct ast_channel *c, const char *context, const char *exten)
2008 {
2009         struct ast_exten *e = ast_hint_extension(c, context, exten);
2010
2011         if (e) {
2012                 if (hint) 
2013                         ast_copy_string(hint, ast_get_extension_app(e), hintsize);
2014                 if (name) {
2015                         const char *tmp = ast_get_extension_app_data(e);
2016                         if (tmp)
2017                                 ast_copy_string(name, tmp, namesize);
2018                 }
2019                 return -1;
2020         }
2021         return 0;       
2022 }
2023
2024 int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid) 
2025 {
2026         return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, HELPER_EXISTS);
2027 }
2028
2029 int ast_findlabel_extension(struct ast_channel *c, const char *context, const char *exten, const char *label, const char *callerid) 
2030 {
2031         return pbx_extension_helper(c, NULL, context, exten, 0, label, callerid, HELPER_FINDLABEL);
2032 }
2033
2034 int ast_findlabel_extension2(struct ast_channel *c, struct ast_context *con, const char *exten, const char *label, const char *callerid) 
2035 {
2036         return pbx_extension_helper(c, con, NULL, exten, 0, label, callerid, HELPER_FINDLABEL);
2037 }
2038
2039 int ast_canmatch_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
2040 {
2041         return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, HELPER_CANMATCH);
2042 }
2043
2044 int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
2045 {
2046         return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, HELPER_MATCHMORE);
2047 }
2048
2049 int ast_spawn_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid) 
2050 {
2051         return pbx_extension_helper(c, NULL, context, exten, priority, NULL, callerid, HELPER_SPAWN);
2052 }
2053
2054 static int __ast_pbx_run(struct ast_channel *c)
2055 {
2056         int firstpass = 1;
2057         int digit;
2058         char exten[256];
2059         int pos;
2060         int waittime;
2061         int res=0;
2062         int autoloopflag;
2063
2064         /* A little initial setup here */
2065         if (c->pbx)
2066                 ast_log(LOG_WARNING, "%s already has PBX structure??\n", c->name);
2067         if (!(c->pbx = ast_calloc(1, sizeof(*c->pbx))))
2068                 return -1;
2069         if (c->amaflags) {
2070                 if (!c->cdr) {
2071                         c->cdr = ast_cdr_alloc();
2072                         if (!c->cdr) {
2073                                 ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
2074                                 free(c->pbx);
2075                                 return -1;
2076                         }
2077                         ast_cdr_init(c->cdr, c);
2078                 }
2079         }
2080         /* Set reasonable defaults */
2081         c->pbx->rtimeout = 10;
2082         c->pbx->dtimeout = 5;
2083
2084         autoloopflag = ast_test_flag(c, AST_FLAG_IN_AUTOLOOP);
2085         ast_set_flag(c, AST_FLAG_IN_AUTOLOOP);
2086
2087         /* Start by trying whatever the channel is set to */
2088         if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
2089                 /* If not successful fall back to 's' */
2090                 if (option_verbose > 1)
2091                         ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", c->name, c->context, c->exten, c->priority);
2092                 ast_copy_string(c->exten, "s", sizeof(c->exten));
2093                 if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
2094                         /* JK02: And finally back to default if everything else failed */
2095                         if (option_verbose > 1)
2096                                 ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d still failed so falling back to context 'default'\n", c->name, c->context, c->exten, c->priority);
2097                         ast_copy_string(c->context, "default", sizeof(c->context));
2098                 }
2099                 c->priority = 1;
2100         }
2101         if (c->cdr && !c->cdr->start.tv_sec && !c->cdr->start.tv_usec)
2102                 ast_cdr_start(c->cdr);
2103         for (;;) {
2104                 pos = 0;
2105                 digit = 0;
2106                 while (ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
2107                         memset(exten, 0, sizeof(exten));
2108                         if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->cid.cid_num))) {
2109                                 /* Something bad happened, or a hangup has been requested. */
2110                                 if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
2111                                         (res == '*') || (res == '#')) {
2112                                         ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
2113                                         memset(exten, 0, sizeof(exten));
2114                                         pos = 0;
2115                                         exten[pos++] = digit = res;
2116                                         break;
2117                                 }
2118                                 switch(res) {
2119                                 case AST_PBX_KEEPALIVE:
2120                                         if (option_debug)
2121                                                 ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
2122                                         else if (option_verbose > 1)
2123                                                 ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
2124                                         goto out;
2125                                         break;
2126                                 default:
2127                                         if (option_debug)
2128                                                 ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
2129                                         else if (option_verbose > 1)
2130                                                 ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
2131                                         if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
2132                                                 c->_softhangup =0;
2133                                                 break;
2134                                         }
2135                                         /* atimeout */
2136                                         if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
2137                                                 break;
2138                                         }
2139
2140                                         if (c->cdr) {
2141                                                 ast_cdr_update(c);
2142                                         }
2143                                         goto out;
2144                                 }
2145                         }
2146                         if ((c->_softhangup == AST_SOFTHANGUP_TIMEOUT) && (ast_exists_extension(c,c->context,"T",1,c->cid.cid_num))) {
2147                                 ast_copy_string(c->exten, "T", sizeof(c->exten));
2148                                 /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
2149                                 c->whentohangup = 0;
2150                                 c->priority = 0;
2151                                 c->_softhangup &= ~AST_SOFTHANGUP_TIMEOUT;
2152                         } else if (c->_softhangup) {
2153                                 ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
2154                                         c->exten, c->priority);
2155                                 goto out;
2156                         }
2157                         firstpass = 0;
2158                         c->priority++;
2159                 }
2160                 if (!ast_exists_extension(c, c->context, c->exten, 1, c->cid.cid_num)) {
2161                         /* It's not a valid extension anymore */
2162                         if (ast_exists_extension(c, c->context, "i", 1, c->cid.cid_num)) {
2163                                 if (option_verbose > 2)
2164                                         ast_verbose(VERBOSE_PREFIX_3 "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
2165                                 pbx_builtin_setvar_helper(c, "INVALID_EXTEN", c->exten);
2166                                 ast_copy_string(c->exten, "i", sizeof(c->exten));
2167                                 c->priority = 1;
2168                         } else {
2169                                 ast_log(LOG_WARNING, "Channel '%s' sent into invalid extension '%s' in context '%s', but no invalid handler\n",
2170                                         c->name, c->exten, c->context);
2171                                 goto out;
2172                         }
2173                 } else if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
2174                         /* If we get this far with AST_SOFTHANGUP_TIMEOUT, then we know that the "T" extension is next. */
2175                         c->_softhangup = 0;
2176                 } else {
2177                         /* Done, wait for an extension */
2178                         waittime = 0;
2179                         if (digit)
2180                                 waittime = c->pbx->dtimeout;
2181                         else if (!autofallthrough)
2182                                 waittime = c->pbx->rtimeout;
2183                         if (waittime) {
2184                                 while (ast_matchmore_extension(c, c->context, exten, 1, c->cid.cid_num)) {
2185                                         /* As long as we're willing to wait, and as long as it's not defined, 
2186                                            keep reading digits until we can't possibly get a right answer anymore.  */
2187                                         digit = ast_waitfordigit(c, waittime * 1000);
2188                                         if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
2189                                                 c->_softhangup = 0;
2190                                         } else {
2191                                                 if (!digit)
2192                                                         /* No entry */
2193                                                         break;
2194                                                 if (digit < 0)
2195                                                         /* Error, maybe a  hangup */
2196                                                         goto out;
2197                                                 exten[pos++] = digit;
2198                                                 waittime = c->pbx->dtimeout;
2199                                         }
2200                                 }
2201                                 if (ast_exists_extension(c, c->context, exten, 1, c->cid.cid_num)) {
2202                                         /* Prepare the next cycle */
2203                                         ast_copy_string(c->exten, exten, sizeof(c->exten));
2204                                         c->priority = 1;
2205                                 } else {
2206                                         /* No such extension */
2207                                         if (!ast_strlen_zero(exten)) {
2208                                                 /* An invalid extension */
2209                                                 if (ast_exists_extension(c, c->context, "i", 1, c->cid.cid_num)) {
2210                                                         if (option_verbose > 2)
2211                                                                 ast_verbose( VERBOSE_PREFIX_3 "Invalid extension '%s' in context '%s' on %s\n", exten, c->context, c->name);
2212                                                         pbx_builtin_setvar_helper(c, "INVALID_EXTEN", exten);
2213                                                         ast_copy_string(c->exten, "i", sizeof(c->exten));
2214                                                         c->priority = 1;
2215                                                 } else {
2216                                                         ast_log(LOG_WARNING, "Invalid extension '%s', but no rule 'i' in context '%s'\n", exten, c->context);
2217                                                         goto out;
2218                                                 }
2219                                         } else {
2220                                                 /* A simple timeout */
2221                                                 if (ast_exists_extension(c, c->context, "t", 1, c->cid.cid_num)) {
2222                                                         if (option_verbose > 2)
2223                                                                 ast_verbose( VERBOSE_PREFIX_3 "Timeout on %s\n", c->name);
2224                                                         ast_copy_string(c->exten, "t", sizeof(c->exten));
2225                                                         c->priority = 1;
2226                                                 } else {
2227                                                         ast_log(LOG_WARNING, "Timeout, but no rule 't' in context '%s'\n", c->context);
2228                                                         goto out;
2229                                                 }
2230                                         }       
2231                                 }
2232                                 if (c->cdr) {
2233                                         if (option_verbose > 2)
2234                                                 ast_verbose(VERBOSE_PREFIX_2 "CDR updated on %s\n",c->name);    
2235                                         ast_cdr_update(c);
2236                             }
2237                         } else {
2238                                 const char *status;
2239
2240                                 status = pbx_builtin_getvar_helper(c, "DIALSTATUS");
2241                                 if (!status)
2242                                         status = "UNKNOWN";
2243                                 if (option_verbose > 2)
2244                                         ast_verbose(VERBOSE_PREFIX_2 "Auto fallthrough, channel '%s' status is '%s'\n", c->name, status);
2245                                 if (!strcasecmp(status, "CONGESTION"))
2246                                         res = pbx_builtin_congestion(c, "10");
2247                                 else if (!strcasecmp(status, "CHANUNAVAIL"))
2248                                         res = pbx_builtin_congestion(c, "10");
2249                                 else if (!strcasecmp(status, "BUSY"))
2250                                         res = pbx_builtin_busy(c, "10");
2251                                 goto out;
2252                         }
2253                 }
2254         }
2255         if (firstpass) 
2256                 ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", c->name);
2257 out:
2258         if ((res != AST_PBX_KEEPALIVE) && ast_exists_extension(c, c->context, "h", 1, c->cid.cid_num)) {
2259                 if (c->cdr && ast_opt_end_cdr_before_h_exten)
2260                         ast_cdr_end(c->cdr);
2261                 c->exten[0] = 'h';
2262                 c->exten[1] = '\0';
2263                 c->priority = 1;
2264                 while(ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
2265                         if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->cid.cid_num))) {
2266                                 /* Something bad happened, or a hangup has been requested. */
2267                                 if (option_debug)
2268                                         ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
2269                                 else if (option_verbose > 1)
2270                                         ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
2271                                 break;
2272                         }
2273                         c->priority++;
2274                 }
2275         }
2276         ast_set2_flag(c, autoloopflag, AST_FLAG_IN_AUTOLOOP);
2277
2278         pbx_destroy(c->pbx);
2279         c->pbx = NULL;
2280         if (res != AST_PBX_KEEPALIVE)
2281                 ast_hangup(c);
2282         return 0;
2283 }
2284
2285 /* Returns 0 on success, non-zero if call limit was reached */
2286 static int increase_call_count(const struct ast_channel *c)
2287 {
2288         int failed = 0;
2289         double curloadavg;
2290         ast_mutex_lock(&maxcalllock);
2291         if (option_maxcalls) {
2292                 if (countcalls >= option_maxcalls) {
2293                         ast_log(LOG_NOTICE, "Maximum call limit of %d calls exceeded by '%s'!\n", option_maxcalls, c->name);
2294                         failed = -1;
2295                 }
2296         }
2297         if (option_maxload) {
2298                 getloadavg(&curloadavg, 1);
2299                 if (curloadavg >= option_maxload) {
2300                         ast_log(LOG_NOTICE, "Maximum loadavg limit of %f load exceeded by '%s' (currently %f)!\n", option_maxload, c->name, curloadavg);
2301                         failed = -1;
2302                 }
2303         }
2304         if (!failed)
2305                 countcalls++;   
2306         ast_mutex_unlock(&maxcalllock);
2307
2308         return failed;
2309 }
2310
2311 static void decrease_call_count(void)
2312 {
2313         ast_mutex_lock(&maxcalllock);
2314         if (countcalls > 0)
2315                 countcalls--;
2316         ast_mutex_unlock(&maxcalllock);
2317 }
2318
2319 static void *pbx_thread(void *data)
2320 {
2321         /* Oh joyeous kernel, we're a new thread, with nothing to do but
2322            answer this channel and get it going.
2323         */
2324         /* NOTE:
2325            The launcher of this function _MUST_ increment 'countcalls'
2326            before invoking the function; it will be decremented when the
2327            PBX has finished running on the channel
2328          */
2329         struct ast_channel *c = data;
2330
2331         __ast_pbx_run(c);
2332         decrease_call_count();
2333
2334         pthread_exit(NULL);
2335
2336         return NULL;
2337 }
2338
2339 enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
2340 {
2341         pthread_t t;
2342         pthread_attr_t attr;
2343
2344         if (!c) {
2345                 ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n");
2346                 return AST_PBX_FAILED;
2347         }
2348            
2349         if (increase_call_count(c))
2350                 return AST_PBX_CALL_LIMIT;
2351
2352         /* Start a new thread, and get something handling this channel. */
2353         pthread_attr_init(&attr);
2354         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2355         if (ast_pthread_create(&t, &attr, pbx_thread, c)) {
2356                 ast_log(LOG_WARNING, "Failed to create new channel thread\n");
2357                 return AST_PBX_FAILED;
2358         }
2359
2360         return AST_PBX_SUCCESS;
2361 }
2362
2363 enum ast_pbx_result ast_pbx_run(struct ast_channel *c)
2364 {
2365         enum ast_pbx_result res = AST_PBX_SUCCESS;
2366
2367         if (increase_call_count(c))
2368                 return AST_PBX_CALL_LIMIT;
2369
2370         res = __ast_pbx_run(c);
2371         decrease_call_count();
2372
2373         return res;
2374 }
2375
2376 int ast_active_calls(void)
2377 {
2378         return countcalls;
2379 }
2380
2381 int pbx_set_autofallthrough(int newval)
2382 {
2383         int oldval = autofallthrough;
2384         autofallthrough = newval;
2385         return oldval;
2386 }
2387
2388 /*
2389  * This function locks contexts list by &conlist, search for the right context
2390  * structure, leave context list locked and call ast_context_remove_include2
2391  * which removes include, unlock contexts list and return ...
2392  */
2393 int ast_context_remove_include(const char *context, const char *include, const char *registrar)
2394 {
2395         struct ast_context *c = NULL;
2396
2397         if (ast_lock_contexts())
2398                 return -1;
2399
2400         /* walk contexts and search for the right one ...*/
2401         while ( (c = ast_walk_contexts(c)) ) {
2402                 /* we found one ... */
2403                 if (!strcmp(ast_get_context_name(c), context)) {
2404                         int ret;
2405                         /* remove include from this context ... */      
2406                         ret = ast_context_remove_include2(c, include, registrar);
2407
2408                         ast_unlock_contexts();
2409
2410                         /* ... return results */
2411                         return ret;
2412                 }
2413         }
2414
2415         /* we can't find the right one context */
2416         ast_unlock_contexts();
2417         return -1;
2418 }
2419
2420 /*
2421  * When we call this function, &conlock lock must be locked, because when
2422  * we giving *con argument, some process can remove/change this context
2423  * and after that there can be segfault.
2424  *
2425  * This function locks given context, removes include, unlock context and
2426  * return.
2427  */
2428 int ast_context_remove_include2(struct ast_context *con, const char *include, const char *registrar)
2429 {
2430         struct ast_include *i, *pi = NULL;
2431
2432         if (ast_mutex_lock(&con->lock))
2433                 return -1;
2434
2435         /* walk includes */
2436         for (i = con->includes; i; pi = i, i = i->next) {
2437                 /* find our include */
2438                 if (!strcmp(i->name, include) && 
2439                         (!registrar || !strcmp(i->registrar, registrar))) {
2440                         /* remove from list */
2441                         if (pi)
2442                                 pi->next = i->next;
2443                         else
2444                                 con->includes = i->next;
2445                         /* free include and return */
2446                         free(i);
2447                         ast_mutex_unlock(&con->lock);
2448                         return 0;
2449                 }
2450         }
2451
2452         /* we can't find the right include */
2453         ast_mutex_unlock(&con->lock);
2454         return -1;
2455 }
2456
2457 /*!
2458  * \note This function locks contexts list by &conlist, search for the rigt context
2459  * structure, leave context list locked and call ast_context_remove_switch2
2460  * which removes switch, unlock contexts list and return ...
2461  */
2462 int ast_context_remove_switch(const char *context, const char *sw, const char *data, const char *registrar)
2463 {
2464         struct ast_context *c = NULL;
2465         int ret = -1; /* default error return */
2466
2467         if (ast_lock_contexts())
2468                 return -1;
2469
2470         /* walk contexts and search for the right one ...*/
2471         while ( (c = ast_walk_contexts(c)) ) {
2472                 /* we found one ... */
2473                 if (!strcmp(ast_get_context_name(c), context)) {
2474                         /* remove switch from this context ... */       
2475                         ret = ast_context_remove_switch2(c, sw, data, registrar);
2476                         break;
2477                 }
2478         }
2479
2480         /* found or error */
2481         ast_unlock_contexts();
2482         return ret;
2483 }
2484
2485 /*!
2486  * \brief This function locks given context, removes switch, unlock context and
2487  * return.
2488  * \note When we call this function, &conlock lock must be locked, because when
2489  * we giving *con argument, some process can remove/change this context
2490  * and after that there can be segfault.
2491  *
2492  */
2493 int ast_context_remove_switch2(struct ast_context *con, const char *sw, const char *data, const char *registrar)
2494 {
2495         struct ast_sw *i, *pi = NULL;
2496
2497         if (ast_mutex_lock(&con->lock)) return -1;
2498
2499         /* walk switchs */
2500         for (i = con->alts; i; pi = i, i = i->next) {
2501                 /* find our switch */
2502                 if (!strcmp(i->name, sw) && !strcmp(i->data, data) && 
2503                         (!registrar || !strcmp(i->registrar, registrar))) {
2504                         /* remove from list */
2505                         if (pi)
2506                                 pi->next = i->next;
2507                         else
2508                                 con->alts = i->next;
2509                         /* free switch and return */
2510                         free(i);
2511                         ast_mutex_unlock(&con->lock);
2512                         return 0;
2513                 }
2514         }
2515
2516         /* we can't find the right switch */
2517         ast_mutex_unlock(&con->lock);
2518         return -1;
2519 }
2520
2521 /*
2522  * \note This functions lock contexts list, search for the right context,
2523  * call ast_context_remove_extension2, unlock contexts list and return.
2524  * In this function we are using
2525  */
2526 int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar)
2527 {
2528         struct ast_context *c = NULL;
2529         int ret = -1; /* default error return */
2530
2531         if (ast_lock_contexts())
2532                 return -1;
2533
2534         /* walk contexts ... */
2535         while ( (c = ast_walk_contexts(c)) ) {
2536                 /* ... search for the right one ... */
2537                 if (!strcmp(ast_get_context_name(c), context)) {
2538                         /* ... remove extension ... */
2539                         ret = ast_context_remove_extension2(c, extension, priority,
2540                                 registrar);
2541                         break;
2542                 }
2543         }
2544         /* found or error */
2545         ast_unlock_contexts();
2546         return ret;
2547 }
2548
2549 /*!
2550  * \brief This functionc locks given context, search for the right extension and
2551  * fires out all peer in this extensions with given priority. If priority
2552  * is set to 0, all peers are removed. After that, unlock context and
2553  * return.
2554  * \note When do you want to call this function, make sure that &conlock is locked,
2555  * because some process can handle with your *con context before you lock
2556  * it.
2557  *
2558  */
2559 int ast_context_remove_extension2(struct ast_context *con, const char *extension, int priority, const char *registrar)
2560 {
2561         struct ast_exten *exten, *prev_exten = NULL;
2562
2563         if (ast_mutex_lock(&con->lock))
2564                 return -1;
2565
2566         /* go through all extensions in context and search the right one ... */
2567         exten = con->root;
2568         while (exten) {
2569
2570                 /* look for right extension */
2571                 if (!strcmp(exten->exten, extension) &&
2572                         (!registrar || !strcmp(exten->registrar, registrar))) {
2573                         struct ast_exten *peer;
2574
2575                         /* should we free all peers in this extension? (priority == 0)? */
2576                         if (priority == 0) {
2577                                 /* remove this extension from context list */
2578                                 if (prev_exten)
2579                                         prev_exten->next = exten->next;
2580                                 else
2581                                         con->root = exten->next;
2582
2583                                 /* fire out all peers */
2584                                 peer = exten; 
2585                                 while (peer) {
2586                                         exten = peer->peer;
2587                                         
2588                                         if (!peer->priority==PRIORITY_HINT) 
2589                                             ast_remove_hint(peer);
2590
2591                                         peer->datad(peer->data);
2592                                         free(peer);
2593
2594                                         peer = exten;
2595                                 }
2596
2597                                 ast_mutex_unlock(&con->lock);
2598                                 return 0;
2599                         } else {
2600                                 /* remove only extension with exten->priority == priority */
2601                                 struct ast_exten *previous_peer = NULL;
2602
2603                                 peer = exten;
2604                                 while (peer) {
2605                                         /* is this our extension? */
2606                                         if (peer->priority == priority &&
2607                                                 (!registrar || !strcmp(peer->registrar, registrar) )) {
2608                                                 /* we are first priority extension? */
2609                                                 if (!previous_peer) {
2610                                                         /* exists previous extension here? */
2611                                                         if (prev_exten) {
2612                                                                 /* yes, so we must change next pointer in
2613                                                                  * previous connection to next peer
2614                                                                  */
2615                                                                 if (peer->peer) {
2616                                                                         prev_exten->next = peer->peer;
2617                                                                         peer->peer->next = exten->next;
2618                                                                 } else
2619                                                                         prev_exten->next = exten->next;
2620                                                         } else {
2621                                                                 /* no previous extension, we are first
2622                                                                  * extension, so change con->root ...
2623                                                                  */
2624                                                                 if (peer->peer)
2625                                                                         con->root = peer->peer;
2626                                                                 else
2627                                                                         con->root = exten->next; 
2628                                                         }
2629                                                 } else {
2630                                                         /* we are not first priority in extension */
2631                                                         previous_peer->peer = peer->peer;
2632                                                 }
2633
2634                                                 /* now, free whole priority extension */
2635                                                 if (peer->priority==PRIORITY_HINT)
2636                                                     ast_remove_hint(peer);
2637                                                 peer->datad(peer->data);
2638                                                 free(peer);
2639
2640                                                 ast_mutex_unlock(&con->lock);
2641                                                 return 0;
2642                                         } else {
2643                                                 /* this is not right extension, skip to next peer */
2644                                                 previous_peer = peer;
2645                                                 peer = peer->peer;
2646                                         }
2647                                 }
2648
2649                                 ast_mutex_unlock(&con->lock);
2650                                 return -1;
2651                         }
2652                 }
2653
2654                 prev_exten = exten;
2655                 exten = exten->next;
2656         }
2657
2658         /* we can't find right extension */
2659         ast_mutex_unlock(&con->lock);
2660         return -1;
2661 }
2662
2663
2664 /*! \brief Dynamically register a new dial plan application */
2665 int ast_register_application(const char *app, int (*execute)(struct ast_channel *, void *), const char *synopsis, const char *description)
2666 {
2667         struct ast_app *tmp, *prev, *cur;
2668         char tmps[80];
2669         int length;
2670         length = sizeof(struct ast_app);
2671         length += strlen(app) + 1;
2672         if (ast_mutex_lock(&applock)) {
2673                 ast_log(LOG_ERROR, "Unable to lock application list\n");
2674                 return -1;
2675         }
2676         for (tmp = apps; tmp; tmp = tmp->next) {
2677                 if (!strcasecmp(app, tmp->name)) {
2678                         ast_log(LOG_WARNING, "Already have an application '%s'\n", app);
2679                         ast_mutex_unlock(&applock);
2680                         return -1;
2681                 }
2682         }
2683         
2684         if (!(tmp = ast_calloc(1, length))) {
2685                 ast_mutex_unlock(&applock);
2686                 return -1;
2687         }
2688
2689         strcpy(tmp->name, app);
2690         tmp->execute = execute;
2691         tmp->synopsis = synopsis;
2692         tmp->description = description;
2693         /* Store in alphabetical order */
2694         prev = NULL;
2695         for (cur = apps; cur; cur = cur->next) {
2696                 if (strcasecmp(tmp->name, cur->name) < 0)
2697                         break;
2698                 prev = cur;
2699         }
2700         if (prev) {
2701                 tmp->next = prev->next;
2702                 prev->next = tmp;
2703         } else {
2704                 tmp->next = apps;
2705                 apps = tmp;
2706         }
2707         
2708         if (option_verbose > 1)
2709                 ast_verbose( VERBOSE_PREFIX_2 "Registered application '%s'\n", term_color(tmps, tmp->name, COLOR_BRCYAN, 0, sizeof(tmps)));
2710         ast_mutex_unlock(&applock);
2711         return 0;
2712 }
2713
2714 int ast_register_switch(struct ast_switch *sw)
2715 {
2716         struct ast_switch *tmp, *prev=NULL;
2717         if (ast_mutex_lock(&switchlock)) {
2718                 ast_log(LOG_ERROR, "Unable to lock switch lock\n");
2719                 return -1;
2720         }
2721         for (tmp = switches; tmp; prev = tmp, tmp = tmp->next) {
2722                 if (!strcasecmp(tmp->name, sw->name))
2723                         break;
2724         }
2725         if (tmp) {      
2726                 ast_mutex_unlock(&switchlock);
2727                 ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
2728                 return -1;
2729         }
2730         sw->next = NULL;
2731         if (prev) 
2732                 prev->next = sw;
2733         else
2734                 switches = sw;
2735         ast_mutex_unlock(&switchlock);
2736         return 0;
2737 }
2738
2739 void ast_unregister_switch(struct ast_switch *sw)
2740 {
2741         struct ast_switch *tmp, *prev=NULL;
2742         if (ast_mutex_lock(&switchlock)) {
2743                 ast_log(LOG_ERROR, "Unable to lock switch lock\n");
2744                 return;
2745         }
2746         for (tmp = switches; tmp; prev = tmp, tmp = tmp->next) {
2747                 if (tmp == sw) {
2748                         if (prev)
2749                                 prev->next = tmp->next;
2750                         else
2751                                 switches = tmp->next;
2752                         tmp->next = NULL;
2753                         break;                  
2754                 }
2755         }
2756         ast_mutex_unlock(&switchlock);
2757 }
2758
2759 /*
2760  * Help for CLI commands ...
2761  */
2762 static char show_application_help[] = 
2763 "Usage: show application <application> [<application> [<application> [...]]]\n"
2764 "       Describes a particular application.\n";
2765
2766 static char show_functions_help[] =
2767 "Usage: show functions [like <text>]\n"
2768 "       List builtin functions, optionally only those matching a given string\n";
2769
2770 static char show_function_help[] =
2771 "Usage: show function <function>\n"
2772 "       Describe a particular dialplan function.\n";
2773
2774 static char show_applications_help[] =
2775 "Usage: show applications [{like|describing} <text>]\n"
2776 "       List applications which are currently available.\n"
2777 "       If 'like', <text> will be a substring of the app name\n"
2778 "       If 'describing', <text> will be a substring of the description\n";
2779
2780 static char show_dialplan_help[] =
2781 "Usage: show dialplan [exten@][context]\n"
2782 "       Show dialplan\n";
2783
2784 static char show_switches_help[] = 
2785 "Usage: show switches\n"
2786 "       Show registered switches\n";
2787
2788 static char show_hints_help[] = 
2789 "Usage: show hints\n"
2790 "       Show registered hints\n";
2791
2792 static char show_globals_help[] = 
2793 "Usage: show globals\n"
2794 "       Show current global dialplan variables and their values\n";
2795
2796 static char set_global_help[] = 
2797 "Usage: set global <name> <value>\n"
2798 "       Set global dialplan variable <name> to <value>\n";
2799
2800
2801 /*
2802  * IMPLEMENTATION OF CLI FUNCTIONS IS IN THE SAME ORDER AS COMMANDS HELPS
2803  *
2804  */
2805
2806 /*
2807  * \brief 'show application' CLI command implementation functions ...
2808  */
2809
2810 /*
2811  * There is a possibility to show informations about more than one
2812  * application at one time. You can type 'show application Dial Echo' and
2813  * you will see informations about these two applications ...
2814  */
2815 static char *complete_show_application(const char *line, const char *word, int pos, int state)
2816 {
2817         struct ast_app *a;
2818         char *ret = NULL;
2819         int which = 0;
2820         int wordlen = strlen(word);
2821
2822         /* try to lock applications list ... */
2823         if (ast_mutex_lock(&applock)) {
2824                 ast_log(LOG_ERROR, "Unable to lock application list\n");
2825                 return NULL;
2826         }
2827
2828         /* return the n-th [partial] matching entry */
2829         for (a = apps; a && !ret; a = a->next) {
2830                 if (!strncasecmp(word, a->name, wordlen) && ++which > state)
2831                         ret = strdup(a->name);
2832         }
2833
2834         ast_mutex_unlock(&applock);
2835
2836         return ret; 
2837 }
2838
2839 static int handle_show_application(int fd, int argc, char *argv[])
2840 {
2841         struct ast_app *a;
2842         int app, no_registered_app = 1;
2843
2844         if (argc < 3) return RESULT_SHOWUSAGE;
2845
2846         /* try to lock applications list ... */
2847         if (ast_mutex_lock(&applock)) {
2848                 ast_log(LOG_ERROR, "Unable to lock application list\n");
2849                 return -1;
2850         }
2851
2852         /* ... go through all applications ... */
2853         for (a = apps; a; a = a->next) {
2854                 /* ... compare this application name with all arguments given
2855                  * to 'show application' command ... */
2856                 for (app = 2; app < argc; app++) {
2857                         if (!strcasecmp(a->name, argv[app])) {
2858                                 /* Maximum number of characters added by terminal coloring is 22 */
2859                                 char infotitle[64 + AST_MAX_APP + 22], syntitle[40], destitle[40];
2860                                 char info[64 + AST_MAX_APP], *synopsis = NULL, *description = NULL;
2861                                 int synopsis_size, description_size;
2862
2863                                 no_registered_app = 0;
2864
2865                                 if (a->synopsis)
2866                                         synopsis_size = strlen(a->synopsis) + 23;
2867                                 else
2868                                         synopsis_size = strlen("Not available") + 23;
2869                                 synopsis = alloca(synopsis_size);
2870
2871                                 if (a->description)
2872                                         description_size = strlen(a->description) + 23;
2873                                 else
2874                                         description_size = strlen("Not available") + 23;
2875                                 description = alloca(description_size);
2876
2877                                 if (synopsis && description) {
2878                                         snprintf(info, 64 + AST_MAX_APP, "\n  -= Info about application '%s' =- \n\n", a->name);
2879                                         term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
2880                                         term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
2881                                         term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
2882                                         term_color(synopsis,
2883                                                                         a->synopsis ? a->synopsis : "Not available",
2884                                                                         COLOR_CYAN, 0, synopsis_size);
2885                                         term_color(description,
2886                                                                         a->description ? a->description : "Not available",
2887                                                                         COLOR_CYAN, 0, description_size);
2888
2889                                         ast_cli(fd,"%s%s%s\n\n%s%s\n", infotitle, syntitle, synopsis, destitle, description);
2890                                 } else {
2891                                         /* ... one of our applications, show info ...*/
2892                                         ast_cli(fd,"\n  -= Info about application '%s' =- \n\n"
2893                                                 "[Synopsis]\n  %s\n\n"
2894                                                 "[Description]\n%s\n",
2895                                                 a->name,
2896                                                 a->synopsis ? a->synopsis : "Not available",
2897                                                 a->description ? a->description : "Not available");
2898                                 }
2899                         }
2900                 }
2901         }
2902
2903         ast_mutex_unlock(&applock);
2904
2905         /* we found at least one app? no? */
2906         if (no_registered_app) {
2907                 ast_cli(fd, "Your application(s) is (are) not registered\n");
2908                 return RESULT_FAILURE;
2909         }
2910
2911         return RESULT_SUCCESS;
2912 }
2913
2914 /*! \brief  handle_show_hints: CLI support for listing registred dial plan hints */
2915 static int handle_show_hints(int fd, int argc, char *argv[])
2916 {
2917         struct ast_hint *hint;
2918         int num = 0;
2919         int watchers;
2920         struct ast_state_cb *watcher;
2921
2922         if (AST_LIST_EMPTY(&hints)) {
2923                 ast_cli(fd, "There are no registered dialplan hints\n");
2924                 return RESULT_SUCCESS;
2925         }
2926         /* ... we have hints ... */
2927         ast_cli(fd, "\n    -= Registered Asterisk Dial Plan Hints =-\n");
2928         if (AST_LIST_LOCK(&hints)) {
2929                 ast_log(LOG_ERROR, "Unable to lock hints\n");
2930                 return -1;
2931         }
2932         AST_LIST_TRAVERSE(&hints, hint, list) {
2933                 watchers = 0;
2934                 for (watcher = hint->callbacks; watcher; watcher = watcher->next)
2935                         watchers++;
2936                 ast_cli(fd, "   %20s@%-20.20s: %-20.20s  State:%-15.15s Watchers %2d\n",
2937                         ast_get_extension_name(hint->exten),
2938                         ast_get_context_name(ast_get_extension_context(hint->exten)),
2939                         ast_get_extension_app(hint->exten),
2940                         ast_extension_state2str(hint->laststate), watchers);
2941                 num++;
2942         }
2943         ast_cli(fd, "----------------\n");
2944         ast_cli(fd, "- %d hints registered\n", num);
2945         AST_LIST_UNLOCK(&hints);
2946         return RESULT_SUCCESS;
2947 }
2948
2949 /*! \brief  handle_show_switches: CLI support for listing registred dial plan switches */
2950 static int handle_show_switches(int fd, int argc, char *argv[])
2951 {
2952         struct ast_switch *sw;
2953         if (!switches) {
2954                 ast_cli(fd, "There are no registered alternative switches\n");
2955                 return RESULT_SUCCESS;
2956         }
2957         /* ... we have applications ... */
2958         ast_cli(fd, "\n    -= Registered Asterisk Alternative Switches =-\n");
2959         if (ast_mutex_lock(&switchlock)) {
2960                 ast_log(LOG_ERROR, "Unable to lock switches\n");
2961                 return -1;
2962         }
2963         for (sw = switches; sw; sw = sw->next)
2964                 ast_cli(fd, "%s: %s\n", sw->name, sw->description);
2965         ast_mutex_unlock(&switchlock);
2966         return RESULT_SUCCESS;
2967 }
2968
2969 /*
2970  * 'show applications' CLI command implementation functions ...
2971  */
2972 static int handle_show_applications(int fd, int argc, char *argv[])
2973 {
2974         struct ast_app *a;
2975         int like = 0, describing = 0;
2976         int total_match = 0;    /* Number of matches in like clause */
2977         int total_apps = 0;     /* Number of apps registered */
2978         
2979         /* try to lock applications list ... */
2980         if (ast_mutex_lock(&applock)) {
2981                 ast_log(LOG_ERROR, "Unable to lock application list\n");
2982                 return -1;
2983         }
2984
2985         /* ... have we got at least one application (first)? no? */
2986         if (!apps) {
2987                 ast_cli(fd, "There are no registered applications\n");
2988                 ast_mutex_unlock(&applock);
2989                 return -1;
2990         }
2991
2992         /* show applications like <keyword> */
2993         if ((argc == 4) && (!strcmp(argv[2], "like"))) {
2994                 like = 1;
2995         } else if ((argc > 3) && (!strcmp(argv[2], "describing"))) {
2996                 describing = 1;
2997         }
2998
2999         /* show applications describing <keyword1> [<keyword2>] [...] */
3000         if ((!like) && (!describing)) {
3001                 ast_cli(fd, "    -= Registered Asterisk Applications =-\n");
3002         } else {
3003                 ast_cli(fd, "    -= Matching Asterisk Applications =-\n");
3004         }
3005
3006         /* ... go through all applications ... */
3007         for (a = apps; a; a = a->next) {
3008                 /* ... show informations about applications ... */
3009                 int printapp=0;
3010                 total_apps++;
3011                 if (like) {
3012                         if (strcasestr(a->name, argv[3])) {
3013                                 printapp = 1;
3014                                 total_match++;
3015                         }
3016                 } else if (describing) {
3017                         if (a->description) {
3018                                 /* Match all words on command line */
3019                                 int i;
3020                                 printapp = 1;
3021                                 for (i = 3; i < argc; i++) {
3022                                         if (!strcasestr(a->description, argv[i])) {
3023                                                 printapp = 0;
3024                                         } else {
3025                                                 total_match++;
3026                                         }
3027                                 }
3028                         }
3029                 } else {
3030                         printapp = 1;
3031                 }
3032
3033                 if (printapp) {
3034                         ast_cli(fd,"  %20s: %s\n", a->name, a->synopsis ? a->synopsis : "<Synopsis not available>");
3035                 }
3036         }
3037         if ((!like) && (!describing)) {
3038                 ast_cli(fd, "    -= %d Applications Registered =-\n",total_apps);
3039         } else {
3040                 ast_cli(fd, "    -= %d Applications Matching =-\n",total_match);
3041         }
3042         
3043         /* ... unlock and return */
3044         ast_mutex_unlock(&applock);
3045
3046         return RESULT_SUCCESS;
3047 }
3048
3049 static char *complete_show_applications(const char *line, const char *word, int pos, int state)
3050 {
3051         static char* choices[] = { "like", "describing", NULL };
3052
3053         return (pos != 2) ? NULL : ast_cli_complete(word, choices, state);
3054 }
3055
3056 /*
3057  * 'show dialplan' CLI command implementation functions ...
3058  */
3059 static char *complete_show_dialplan_context(const char *line, const char *word, int pos,
3060         int state)
3061 {
3062         struct ast_context *c = NULL;
3063         char *ret = NULL;
3064         int which = 0;
3065         int wordlen;
3066
3067         /* we are do completion of [exten@]context on second position only */
3068         if (pos != 2)
3069                 return NULL;
3070
3071         /* try to lock contexts list ... */
3072         if (ast_lock_contexts()) {
3073                 ast_log(LOG_ERROR, "Unable to lock context list\n");
3074                 return NULL;