Added keypad forwarding, keypad parameter, chan_lcr keypad option 'k'.
[lcr.git] / route.c
1 /*****************************************************************************\ 
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** match processing of routing configuration                                 **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14
15 struct route_ruleset    *ruleset_first;         /* first entry */
16 struct route_ruleset    *ruleset_main;          /* pointer to main ruleset */
17
18 struct cond_defs cond_defs[] = {
19         { "extern",     MATCH_EXTERN,   COND_TYPE_NULL,
20           "extern", "Matches if call is from external port (no extension)."},
21         { "intern",     MATCH_INTERN,COND_TYPE_NULL,
22           "intern", "Matches if call is from an extension."},
23         { "port",       MATCH_PORT,     COND_TYPE_INTEGER,
24           "port=<number>[-<number>][,...]", "Matches if call is received from given port(s). NOT INTERFACE!"},
25         { "interface",  MATCH_INTERFACE,COND_TYPE_STRING,
26           "interface=<interface>[,...]", "Matches if call is received from given interface(s). NOT PORTS!"},
27         { "callerid",   MATCH_CALLERID, COND_TYPE_STRING,
28           "callerid=<digits>[-<digits>][,...]", "Matches if caller ID matches or begins with the given (range(s) of) prefixes(s)."},
29         { "callerid2",  MATCH_CALLERID2,COND_TYPE_STRING,
30           "callerid2=<digits>[-<digits>][,...]", "Matches the second caller ID (network provided)."},
31         { "extension",  MATCH_EXTENSION,COND_TYPE_STRING,
32           "extension=<digits>[-<digits>][,...]", "Matches if caller calls from given (range(s) of) extension(s)."},
33         { "dialing",    MATCH_DIALING,  COND_TYPE_STRING,
34           "dialing=<digits>[-<digits>][,...]", "Matches if caller has dialed the given (range(s) of) digits at least."},
35         { "enblock",    MATCH_ENBLOCK,  COND_TYPE_NULL,
36           "enblock", "Matches if caller dialed en block. (Dial the number before pick up.)"},
37         { "overlap",    MATCH_OVERLAP,  COND_TYPE_NULL,
38           "overlap", "Matches if caller dialed digit by digit. (Dial the number after pick up.)"},
39         { "anonymous",  MATCH_ANONYMOUS,COND_TYPE_NULL,
40           "anonymous", "Matches if caller uses restricted caller ID or if not available."},
41         { "visible",    MATCH_VISIBLE,  COND_TYPE_NULL,
42           "visible", "Matches if caller ID is presented and if available."},
43         { "unknown",    MATCH_UNKNOWN,  COND_TYPE_NULL,
44           "unknown", "Matches if no ID is available from caller."},
45         { "available",  MATCH_AVAILABLE,COND_TYPE_NULL,
46           "available", "Matches if ID is available from caller."},
47         { "fake",       MATCH_FAKE,     COND_TYPE_NULL,
48           "fake", "Matches if caller ID is not screened and may be faked by caller."},
49         { "real",       MATCH_REAL,     COND_TYPE_NULL,
50           "real", "Matches if caller ID is screend and so it is the real caller's ID."},
51         { "redirected", MATCH_REDIRECTED,COND_TYPE_NULL,
52           "redirected", "Matches if caller has been redirected."},
53         { "direct",     MATCH_DIRECT    ,COND_TYPE_NULL,
54           "direct", "Matches if caller did not come from an redirection."},
55         { "redirid",    MATCH_REDIRID   ,COND_TYPE_STRING,
56           "redirid=<digits>[-<digits>][,...]", "Matches if the caller has been redirected by the given (range(s) of) ID(s) or prefix(es))"},
57         { "time",       MATCH_TIME,     COND_TYPE_TIME,
58           "time=<minutes>[-<minutes>][,...]", "Matches if the caller calls within the given (range(s) of) time(s). (e.g. 0700-1900)"},
59         { "mday",       MATCH_MDAY,     COND_TYPE_MDAY,
60           "mday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) day(s) of the month. (1..31)"},
61         { "month",      MATCH_MONTH,    COND_TYPE_MONTH,
62           "month=<month>[-<month>][,...]", "Matches if the caller calls within the given (range(s) of) month(s). (1=January..12=December)"},
63         { "year",       MATCH_YEAR,     COND_TYPE_YEAR,
64           "year=<year>[-<year>][,...]", "Matches if the caller calls within the given (range(s) of) year(s). (1970..2106)"},
65         { "wday",       MATCH_WDAY,     COND_TYPE_WDAY,
66           "wday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) weekday(s). (1=Monday..7=Sunday)"},
67         { "capability", MATCH_CAPABILITY, COND_TYPE_CAPABILITY,
68           "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones[,...]", "Matches the given bearer capability(s)."},
69         { "infolayer1", MATCH_INFOLAYER1, COND_TYPE_INTEGER,
70           "infolayer1=<value>[,...]", "Matches the given information layer 1. (2=u-Law, 3=a-law, see info layer 1 in bearer capability.)"},
71         { "hlc",        MATCH_HLC,      COND_TYPE_INTEGER,
72           "hlc=<value>[,...]", "Matches the high layer capability(s)."},
73         { "file",       MATCH_FILE,     COND_TYPE_STRING,
74           "file=<path>[,...]", "Mathes is the given file exists and if the first character is '1'."},
75         { "execute",    MATCH_EXECUTE,  COND_TYPE_STRING,
76           "execute=<command>[,...]","Matches if the return value of the given command is greater 0."},
77         { "default",    MATCH_DEFAULT,  COND_TYPE_NULL,
78           "default","Matches if no further dialing could match."},
79         { "timeout",    MATCH_TIMEOUT,  COND_TYPE_INTEGER,
80           "timeout=<seconds>","Matches if the ruleset was entered AFTER given seconds."},
81         { "free",       MATCH_FREE,     COND_TYPE_IFATTR,
82           "free=<interface>:<channel>","Matches if the given minimum of channels are free."},
83         { "notfree",    MATCH_NOTFREE,  COND_TYPE_IFATTR,
84           "notfree=<interface>:<channel>","Matches if NOT the given minimum of channels are free."},
85         { "blocked",    MATCH_DOWN,     COND_TYPE_STRING,
86           "blocked=<interfaces>[,...]","Matches if all of the given interfaces are blocked."},
87         { "idle",       MATCH_UP,       COND_TYPE_STRING,
88           "idle=<interface>[,...]","Matches if any of the given interfaces is idle."},
89         { "busy",       MATCH_BUSY,     COND_TYPE_STRING,
90           "busy=<extension>[,...]","Matches if any of the given extension is busy."},
91         { "notbusy",    MATCH_IDLE,     COND_TYPE_STRING,
92           "notbusy=<extension>[,...]","Matches if any of the given extension is not busy."},
93         { "remote",     MATCH_REMOTE,   COND_TYPE_STRING,
94           "remote=<application name>","Matches if remote application is running."},
95         { "notremote",  MATCH_NOTREMOTE,COND_TYPE_STRING,
96           "notremote=<application name>","Matches if remote application is not running."},
97         { NULL, 0, 0, NULL}
98 };
99
100 struct param_defs param_defs[] = {
101         { PARAM_PROCEEDING,
102           "proceeding", PARAM_TYPE_NULL,
103           "proceeding", "Will set the call into 'proceeding' state to prevent dial timeout."},
104         { PARAM_ALERTING,
105           "alerting",   PARAM_TYPE_NULL,
106           "alerting", "Will set the call into 'altering' state."},
107         { PARAM_CONNECT,
108           "connect",    PARAM_TYPE_NULL,
109           "connect", "Will complete the call before processing the action. Audio path for external calls will be established."},
110         { PARAM_EXTENSION,
111           "extension",  PARAM_TYPE_STRING,
112           "extension=<digits>", "Give extension name (digits) to relate this action to."},
113         { PARAM_EXTENSIONS,
114           "extensions", PARAM_TYPE_STRING,
115           "extensions=<extension>[,<extension>[,...]]", "One or more extensions may be given."},
116         { PARAM_PREFIX,
117           "prefix",     PARAM_TYPE_STRING,
118           "prefix=<digits>", "Add prefix in front of the dialed number."},
119         { PARAM_CAPA,
120           "capability", PARAM_TYPE_CAPABILITY,
121           "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones", "Alter the service type of the call."},
122         { PARAM_BMODE,
123           "bmode",      PARAM_TYPE_BMODE,
124           "bmode=transparent|hdlc", "Alter the bchannel mode of the call. Use hdlc for data calls."},
125         { PARAM_INFO1,
126           "infolayer1", PARAM_TYPE_INTEGER,
127           "infolayer1=<value>", "Alter the layer 1 information of a call. Use 3 for ALAW or 2 for uLAW."},
128         { PARAM_HLC,
129           "hlc",        PARAM_TYPE_INTEGER,
130           "hlc=<value>", "Alter the HLC identification. Use 1 for telephony or omit."},
131         { PARAM_EXTHLC,
132           "exthlc",     PARAM_TYPE_INTEGER,
133           "exthlc=<value>", "Alter extended HLC value. (Mainenance only, don't use it.)"},
134         { PARAM_PRESENT,
135           "present",    PARAM_TYPE_YESNO,
136           "present=yes|no", "Allow or restrict caller ID regardless what the caller wants."},
137         { PARAM_DIVERSION,
138           "diversion",  PARAM_TYPE_DIVERSION,
139           "diversion=cfu|cfnr|cfb|cfp", "Set diversion type."},
140         { PARAM_DEST,
141           "dest",       PARAM_TYPE_DESTIN,
142           "dest=<string>", "Destination number to divert to. Use 'vbox' to divert to vbox. (cfu,cfnr,cfb only)"},
143         { PARAM_SELECT,
144           "select",     PARAM_TYPE_NULL,
145           "select", "Lets the caller select the history of calls using keys '1'/'3' or '*'/'#'."},
146         { PARAM_DELAY,
147           "delay",      PARAM_TYPE_INTEGER,
148           "delay=<seconds>", "Number of seconds to delay."},
149         { PARAM_LIMIT,
150           "limit",      PARAM_TYPE_INTEGER,
151           "limit=<retries>", "Number of maximum retries."},
152         { PARAM_HOST,
153           "host",       PARAM_TYPE_STRING,
154           "host=<string>", "Name of remote VoIP host."},
155         { PARAM_PORT,
156           "port",       PARAM_TYPE_STRING,
157           "port=<value>", "Alternate port to use if 'host' is given."},
158         { PARAM_INTERFACES,
159           "interfaces", PARAM_TYPE_STRING,
160           "interfaces=<interface>[,<interface>[,...]]", "Give one or a list of Interfaces to select a free channel from."},
161         { PARAM_ADDRESS,
162           "address",    PARAM_TYPE_STRING,
163           "address=<string>", "Complete VoIP address. ( [user@]host[:port] )"},
164         { PARAM_SAMPLE,
165           "sample",     PARAM_TYPE_STRING,
166           "sample=<file prefix>", "Filename of sample (current tone's dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
167         { PARAM_ANNOUNCEMENT,
168           "announcement",PARAM_TYPE_STRING,
169           "announcement=<file prefix>", "Filename of announcement (inside vbox recording dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
170         { PARAM_RULESET,
171           "ruleset",    PARAM_TYPE_STRING,
172           "ruleset=<name>", "Ruleset to go to."},
173         { PARAM_CAUSE,
174           "cause",      PARAM_TYPE_INTEGER,
175           "cause=<cause value>", "Cause value when disconnecting. (21=reject 1=unassigned 63=service not available)"},
176         { PARAM_LOCATION,
177           "location",   PARAM_TYPE_INTEGER,
178           "location=<location value>", "Location of cause value when disconnecting. (0=user 1=private network sering local user)"},
179         { PARAM_DISPLAY,
180           "display",    PARAM_TYPE_STRING,
181           "display=<text>", "Message to display on the caller's telephone. (internal only)"},
182         { PARAM_PORTS,
183           "ports",      PARAM_TYPE_INTEGER,
184           "ports=<port>[,<port>[,...]]", "ISDN port[s] to use."},
185         { PARAM_TPRESET,
186           "tpreset",    PARAM_TYPE_INTEGER,
187           "tpreset=<seconds>", "Preset of countdown timer."},
188         { PARAM_FILE,
189           "file",       PARAM_TYPE_STRING,
190           "file=<full path>", "Full path to file name."},
191         { PARAM_CONTENT,
192           "content",    PARAM_TYPE_STRING,
193           "content=<string>", "Content to write into file."},
194         { PARAM_APPEND,
195           "append",     PARAM_TYPE_NULL,
196           "append", "Will append to given file, rather than overwriting it."},
197         { PARAM_EXECUTE,
198           "execute",    PARAM_TYPE_STRING,
199           "execute=<full path>", "Full path to script/command name. (Dialed digits are the argument 1.)"},
200         { PARAM_PARAM,
201           "param",      PARAM_TYPE_STRING,
202           "param=<string>", "Optionally this parameter can be inserted as argument 1, others are shifted."},
203         { PARAM_TYPE,
204           "type",       PARAM_TYPE_TYPE,
205           "type=unknown|subscriber|national|international", "Type of number to dial, default is 'unknown'."},
206         { PARAM_COMPLETE,
207           "complete",   PARAM_TYPE_NULL,
208           "complete", "Indicates complete number as given by prefix. Proceeding of long distance calls may be faster."},
209         { PARAM_CALLERID,
210           "callerid",   PARAM_TYPE_STRING,
211           "callerid=<digits>", "Change caller ID to given string."},
212         { PARAM_CALLERIDTYPE,
213           "calleridtype",PARAM_TYPE_CALLERIDTYPE,
214           "calleridtype=[unknown|subscriber|national|international]", "Type of caller ID. For normal MSN use 'unknown'"},
215         { PARAM_CALLTO,
216           "callto",     PARAM_TYPE_STRING,
217           "callto=<digits>", "Where to call back. By default the caller ID is used."},
218         { PARAM_ROOM,
219           "room",       PARAM_TYPE_INTEGER,
220           "room=<digits>", "Conference room number, must be greater 0, as in real life."},
221         { PARAM_JINGLE,
222           "jingle",     PARAM_TYPE_NULL,
223           "jingle", "Conference members will hear a jingle if a member joins."},
224         { PARAM_TIMEOUT,
225           "timeout",    PARAM_TYPE_INTEGER,
226           "timeout=<seconds>", "Timeout before continue with next action."},
227         { PARAM_NOPASSWORD,
228           "nopassword", PARAM_TYPE_NULL,
229           "nopassword", "Don't ask for password. Be sure to authenticate right via real caller ID."},
230         { PARAM_STRIP,
231           "strip",      PARAM_TYPE_NULL,
232           "strip", "Remove digits that were required to match this rule."},
233         { PARAM_APPLICATION,
234           "application",PARAM_TYPE_STRING,
235           "application=<name>", "Name of remote application to make call to."},
236         { PARAM_CONTEXT,
237           "context",    PARAM_TYPE_STRING,
238           "context=<context>", "Give context parameter to the remote application."},
239         { PARAM_EXTEN,
240           "exten",      PARAM_TYPE_STRING,
241           "exten=<extension>", "Give exten parameter to the remote application. (overrides dialed number)"},
242         { PARAM_ON,
243           "on", PARAM_TYPE_STRING,
244           "on=[init|hangup]", "Defines if the action is executed on call init or on hangup."},
245         { PARAM_KEYPAD,
246           "keypad",     PARAM_TYPE_NULL,
247           "keypad", "Use 'keypad facility' for dialing, instead of 'called number'."},
248         { 0, NULL, 0, NULL, NULL}
249 };
250
251 struct action_defs action_defs[] = {
252         { ACTION_EXTERNAL,
253           "extern",     &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
254           PARAM_CONNECT | PARAM_PREFIX | PARAM_COMPLETE | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_INTERFACES | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_KEYPAD | PARAM_TIMEOUT,
255           "Call is routed to extern number as dialed."},
256         { ACTION_INTERNAL,
257           "intern",     &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_internal, &EndpointAppPBX::action_hangup_call,
258           PARAM_CONNECT | PARAM_EXTENSION | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_TIMEOUT,
259           "Call is routed to intern extension as given by the dialed number or specified by option."},
260         { ACTION_OUTDIAL,
261           "outdial",    &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
262           PARAM_CONNECT | PARAM_PREFIX | PARAM_COMPLETE | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_INTERFACES | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_KEYPAD | PARAM_TIMEOUT,
263           "Same as 'extern'"},
264         { ACTION_REMOTE,
265           "remote",     &EndpointAppPBX::action_init_remote, &EndpointAppPBX::action_dialing_remote, &EndpointAppPBX::action_hangup_call,
266           PARAM_CONNECT | PARAM_APPLICATION | PARAM_CONTEXT | PARAM_EXTEN | PARAM_TIMEOUT,
267           "Call is routed to Remote application, like Asterisk."},
268         { ACTION_VBOX_RECORD,
269           "vbox-record",&EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_vbox_record, &EndpointAppPBX::action_hangup_call,
270           PARAM_CONNECT | PARAM_EXTENSION | PARAM_ANNOUNCEMENT | PARAM_TIMEOUT,
271           "Caller is routed to the voice box of given extension."},
272         { ACTION_PARTYLINE,
273           "partyline",&EndpointAppPBX::action_init_partyline, NULL, &EndpointAppPBX::action_hangup_call,
274           PARAM_ROOM | PARAM_JINGLE,
275           "Caller is participating the conference with the given room number."},
276         { ACTION_LOGIN,
277           "login",      NULL, &EndpointAppPBX::action_dialing_login, NULL,
278           PARAM_CONNECT | PARAM_EXTENSION | PARAM_NOPASSWORD,
279           "Log into the given extension. Password required."},
280         { ACTION_CALLERID,
281           "callerid",   &EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_callerid, NULL,
282           PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
283           "Caller changes the caller ID for all calls."},
284         { ACTION_CALLERIDNEXT,
285           "calleridnext",&EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_calleridnext, NULL,
286           PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
287           "Caller changes the caller ID for the next call."},
288         { ACTION_FORWARD,
289           "forward",    &EndpointAppPBX::action_init_change_forward, &EndpointAppPBX::action_dialing_forward, NULL,
290           PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_DELAY,
291           "Caller changes the diversion of given type to the given destination or voice box."},
292         { ACTION_REDIAL,
293           "redial",     &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_redial, NULL,
294           PARAM_CONNECT | PARAM_SELECT,
295           "Caller redials. (last outgoing call(s))"},
296         { ACTION_REPLY,
297           "reply",      &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_reply, NULL,
298           PARAM_CONNECT | PARAM_SELECT,
299           "Caller replies. (last incoming call(s))"},
300         { ACTION_POWERDIAL,
301           "powerdial",  NULL, &EndpointAppPBX::action_dialing_powerdial, NULL,
302           PARAM_CONNECT | PARAM_DELAY | PARAM_LIMIT | PARAM_TIMEOUT,
303           "Caller redials using powerdialing."},
304         { ACTION_CALLBACK,
305           "callback",   NULL, &EndpointAppPBX::action_dialing_callback, &EndpointAppPBX::action_hangup_callback,
306           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_EXTENSION | PARAM_DELAY | PARAM_CALLTO | PARAM_PREFIX,
307           "Caller will use the callback service. After disconnecting, the callback is triggered."},
308         { ACTION_ABBREV,
309           "abbrev",     NULL, &EndpointAppPBX::action_dialing_abbrev, NULL,
310           PARAM_CONNECT,
311           "Caller dials abbreviation."},
312         { ACTION_TEST,
313           "test",       NULL, &EndpointAppPBX::action_dialing_test, NULL,
314           PARAM_CONNECT | PARAM_PREFIX | PARAM_TIMEOUT,
315           "Caller dials test mode."},
316         { ACTION_PLAY,
317           "play",       &EndpointAppPBX::action_init_play, NULL, NULL,
318           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_SAMPLE | PARAM_TIMEOUT,
319           "Plays the given sample."},
320         { ACTION_VBOX_PLAY,
321           "vbox-play",  &EndpointAppPBX::action_init_vbox_play, &EndpointAppPBX::action_dialing_vbox_play, NULL,
322           PARAM_EXTENSION,
323           "Caller listens to her voice box or to given extension."},
324         { ACTION_CALCULATOR,
325           "calculator", NULL, &EndpointAppPBX::action_dialing_calculator, NULL,
326           PARAM_CONNECT,
327           "Caller calls the calculator."},
328         { ACTION_TIMER,
329           "timer",      NULL, &EndpointAppPBX::action_dialing_timer, NULL,
330           PARAM_CONNECT | PARAM_TPRESET | PARAM_TIMEOUT,
331           NULL},
332 //        "Caller calls the timer."},
333         { ACTION_GOTO,
334           "goto",       NULL, &EndpointAppPBX::action_dialing_goto, NULL,
335           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_RULESET | PARAM_STRIP | PARAM_SAMPLE,
336           "Jump to given ruleset and optionally play sample. Dialed digits are not flushed."},
337         { ACTION_MENU,
338           "menu",       NULL, &EndpointAppPBX::action_dialing_menu, NULL,
339           PARAM_CONNECT | PARAM_RULESET | PARAM_SAMPLE,
340           "Same as 'goto', but flushes all digits dialed so far."},
341         { ACTION_DISCONNECT,
342           "disconnect", NULL, &EndpointAppPBX::action_dialing_disconnect, NULL,
343           PARAM_CONNECT | PARAM_CAUSE | PARAM_LOCATION | PARAM_SAMPLE | PARAM_DISPLAY,
344           "Caller gets disconnected optionally with given cause and given sample and given display text."},
345         { ACTION_DEFLECT,
346           "deflect",    NULL, &EndpointAppPBX::action_dialing_deflect, NULL,
347           PARAM_DEST,
348           NULL},
349 //        "External call is deflected to the given destination within the telephone network."},
350         { ACTION_SETFORWARD,
351           "setforward", NULL, &EndpointAppPBX::action_dialing_setforward, NULL,
352           PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_PORT,
353           NULL},
354 //        "The call forward is set within the telephone network of the external line."},
355         { ACTION_EXECUTE,
356           "execute",    &EndpointAppPBX::action_init_execute, NULL, &EndpointAppPBX::action_hangup_execute,
357           PARAM_CONNECT | PARAM_EXECUTE | PARAM_PARAM | PARAM_ON,
358           "Executes the given script file. The file must terminate quickly, because it will halt the PBX."},
359         { ACTION_FILE,
360           "file",       NULL, NULL, &EndpointAppPBX::action_hangup_file,
361           PARAM_CONNECT | PARAM_FILE | PARAM_CONTENT | PARAM_APPEND,
362           "Writes givent content to given file. If content is not given, the dialed digits are written."},
363         { ACTION_PICK,
364           "pick",       &EndpointAppPBX::action_init_pick, NULL, NULL,
365           PARAM_EXTENSIONS,
366           "Pick up a call that is ringing on any phone. Extensions may be given to limit the picking ability."},
367         { ACTION_PASSWORD,
368           "password",   NULL, &EndpointAppPBX::action_dialing_password, NULL,
369           0,
370           NULL},
371         { ACTION_PASSWORD_WRITE,
372           "password_wr",NULL, &EndpointAppPBX::action_dialing_password_wr, NULL,
373           0,
374           NULL},
375         { ACTION_NOTHING,
376           "nothing",    NULL, NULL, NULL,
377           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_TIMEOUT,
378           "does nothing. Usefull to wait for calls to be released completely, by giving timeout value."},
379         { ACTION_EFI,
380           "efi",        &EndpointAppPBX::action_init_efi, NULL, NULL,
381           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT,
382           "Elektronische Fernsprecher Identifikation - announces caller ID."},
383         { -1,
384           NULL, NULL, NULL, NULL, 0, NULL}
385 };
386
387
388 /* display documentation of rules */
389
390 void doc_rules(const char *name)
391 {
392         int i, j;
393
394         if (name) {
395                 i = 0;
396                 while(action_defs[i].name) {
397                         if (!strcasecmp(action_defs[i].name, name))
398                                 break;
399                         i++;
400                 }
401                 if (!action_defs[i].name) {
402                         fprintf(stderr, "Given action '%s' unknown.\n", name);
403                         return;
404                 }
405                 name = action_defs[i].name;
406         }
407
408         printf("Syntax overview:\n");
409         printf("----------------\n\n");
410         printf("[ruleset]\n");
411         printf("<condition> ...   : <action> [parameter ...]   [timeout=X : <action> ...]\n");
412         printf("...\n");
413         printf("Please refer to the documentation for description on rule format.\n\n");
414
415         if (!name) {
416                 printf("Available conditions to match:\n");
417                 printf("------------------------------\n\n");
418                 i = 0;
419                 while(cond_defs[i].name) {
420                         printf("Usage: %s\n", cond_defs[i].doc);
421                         printf("%s\n\n", cond_defs[i].help);
422                         i++;
423                 }
424
425                 printf("Available actions with their parameters:\n");
426                 printf("----------------------------------------\n\n");
427         } else {
428                 printf("Detailes parameter description of action:\n");
429                 printf("-----------------------------------------\n\n");
430         }
431         i = 0;
432         while(action_defs[i].name) {
433                 if (name && !!strcmp(action_defs[i].name,name)) { /* not selected */
434                         i++;
435                         continue;
436                 }
437                 if (!action_defs[i].help) { /* not internal actions */
438                         i++;
439                         continue;
440                 }
441                 printf("Usage: %s", action_defs[i].name);
442                 j = 0;
443                 while(j < 64) {
444                         if ((1LL<<j) & action_defs[i].params)
445                                 printf(" [%s]", param_defs[j].doc);
446                         j++;
447                 }
448                 printf("\n%s\n\n", action_defs[i].help);
449                 if (name) { /* only show parameter help for specific action */
450                         j = 0;
451                         while(j < 64) {
452                                 if ((1LL<<j) & action_defs[i].params)
453                                         printf("%s:\n\t%s\n", param_defs[j].doc, param_defs[j].help);
454                                 j++;
455                         }
456                         printf("\n");
457                 }
458                 i++;
459         }
460 }
461
462 void ruleset_free(struct route_ruleset *ruleset_start)
463 {
464         struct route_ruleset *ruleset;
465         struct route_rule *rule;
466         struct route_cond *cond;
467         struct route_action *action;
468         struct route_param *param;
469
470         while(ruleset_start) {
471                 ruleset = ruleset_start;
472                 ruleset_start = ruleset->next;
473                 while(ruleset->rule_first) {
474                         rule = ruleset->rule_first;
475                         ruleset->rule_first = rule->next;
476                         while(rule->cond_first) {
477                                 cond = rule->cond_first;
478                                 if (cond->string_value) {
479                                         FREE(cond->string_value, 0);
480                                         rmemuse--;
481                                 }
482                                 if (cond->string_value_to) {
483                                         FREE(cond->string_value_to, 0);
484                                         rmemuse--;
485                                 }
486                                 rule->cond_first = cond->next;
487                                 FREE(cond, sizeof(struct route_cond));
488                                 rmemuse--;
489                         }
490                         while(rule->action_first) {
491                                 action = rule->action_first;
492                                 rule->action_first = action->next;
493                                 while(action->param_first) {
494                                         param = action->param_first;
495                                         action->param_first = param->next;
496                                         if (param->string_value) {
497                                                 FREE(param->string_value, 0);
498                                                 rmemuse--;
499                                         }
500                                         FREE(param, sizeof(struct route_param));
501                                         rmemuse--;
502                                 }
503                                 FREE(action, sizeof(struct route_action));
504                                 rmemuse--;
505                         }
506                         FREE(rule, sizeof(struct route_rule));
507                         rmemuse--;
508                 }
509                 FREE(ruleset, sizeof(struct route_ruleset));
510                 rmemuse--;
511         }
512 }
513
514 void ruleset_debug(struct route_ruleset *ruleset_start)
515 {
516         struct route_ruleset    *ruleset;
517         struct route_rule       *rule;
518         struct route_cond       *cond;
519         struct route_action     *action;
520         struct route_param      *param;
521         int                     first;
522
523         ruleset = ruleset_start;
524         while(ruleset) {
525                 printf("Ruleset: '%s'\n", ruleset->name);
526                 rule = ruleset->rule_first;
527                 while(rule) {
528                         /* CONDITION */
529                         first = 1;
530                         cond = rule->cond_first;
531                         while(cond) {
532                                 if (first)
533                                         printf("    Condition:");
534                                 else
535                                         printf("    and       ");
536                                 first = 0;
537                                 printf(" %s", cond_defs[cond->index].name);
538                                 if (cond->value_type != VALUE_TYPE_NULL)
539                                         printf(" = ");
540                                 next_cond_value:
541                                 switch(cond->value_type) {
542                                         case VALUE_TYPE_NULL:
543                                         break;
544
545                                         case VALUE_TYPE_INTEGER:
546                                         printf("%d", cond->integer_value);
547                                         break;
548
549                                         case VALUE_TYPE_INTEGER_RANGE:
550                                         printf("%d-%d", cond->integer_value, cond->integer_value_to);
551                                         break;
552
553                                         case VALUE_TYPE_STRING:
554                                         printf("'%s'", cond->string_value);
555                                         break;
556
557                                         case VALUE_TYPE_STRING_RANGE:
558                                         printf("'%s'-'%s'", cond->string_value, cond->string_value_to);
559                                         break;
560
561                                         default:
562                                         printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", cond->value_type, __FUNCTION__, __LINE__);
563                                 }
564                                 if (cond->value_extension && cond->next) {
565                                         cond = cond->next;
566                                         printf(" or ");
567                                         goto next_cond_value;
568                                 }
569
570                                 cond = cond->next;
571                                 printf("\n");
572                         }
573
574                         /* ACTION */
575                         action = rule->action_first;
576                         while(action) {
577                                 printf("    Action: %s\n", action_defs[action->index].name);
578                                 /* PARAM */
579                                 first = 1;
580                                 param = action->param_first;
581                                 while(param) {
582                                         if (first)
583                                                 printf("    Param:");
584                                         else
585                                                 printf("          ");
586                                         first = 0;
587                                         printf(" %s", param_defs[param->index].name);
588                                         if (param->value_type != VALUE_TYPE_NULL)
589                                                 printf(" = ");
590                                         switch(param->value_type) {
591                                                 case VALUE_TYPE_NULL:
592                                                 break;
593
594                                                 case VALUE_TYPE_INTEGER:
595                                                 if (param_defs[param->index].type == PARAM_TYPE_CALLERIDTYPE) {
596                                                         switch(param->integer_value) {
597                                                                 case INFO_NTYPE_UNKNOWN:
598                                                                 printf("unknown");
599                                                                 break;
600                                                                 case INFO_NTYPE_SUBSCRIBER:
601                                                                 printf("subscriber");
602                                                                 break;
603                                                                 case INFO_NTYPE_NATIONAL:
604                                                                 printf("national");
605                                                                 break;
606                                                                 case INFO_NTYPE_INTERNATIONAL:
607                                                                 printf("international");
608                                                                 break;
609                                                                 default:
610                                                                 printf("unknown(%d)", param->integer_value);
611                                                         }
612                                                         break;
613                                                 }
614                                                 if (param_defs[param->index].type == PARAM_TYPE_CAPABILITY) {
615                                                         switch(param->integer_value) {
616                                                                 case INFO_BC_SPEECH:
617                                                                 printf("speech");
618                                                                 break;
619                                                                 case INFO_BC_AUDIO:
620                                                                 printf("audio");
621                                                                 break;
622                                                                 case INFO_BC_VIDEO:
623                                                                 printf("video");
624                                                                 break;
625                                                                 case INFO_BC_DATARESTRICTED:
626                                                                 printf("digital-restricted");
627                                                                 break;
628                                                                 case INFO_BC_DATAUNRESTRICTED:
629                                                                 printf("digital-unrestricted");
630                                                                 break;
631                                                                 case INFO_BC_DATAUNRESTRICTED_TONES:
632                                                                 printf("digital-unrestricted-tones");
633                                                                 break;
634                                                                 default:
635                                                                 printf("unknown(%d)", param->integer_value);
636                                                         }
637                                                         break;
638                                                 }
639                                                 if (param_defs[param->index].type == PARAM_TYPE_DIVERSION) {
640                                                         switch(param->integer_value) {
641                                                                 case INFO_DIVERSION_CFU:
642                                                                 printf("cfu");
643                                                                 break;
644                                                                 case INFO_DIVERSION_CFNR:
645                                                                 printf("cfnr");
646                                                                 break;
647                                                                 case INFO_DIVERSION_CFB:
648                                                                 printf("cfb");
649                                                                 break;
650                                                                 case INFO_DIVERSION_CFP:
651                                                                 printf("cfp");
652                                                                 break;
653                                                                 default:
654                                                                 printf("unknown(%d)", param->integer_value);
655                                                         }
656                                                         break;
657                                                 }
658                                                 if (param_defs[param->index].type == PARAM_TYPE_TYPE) {
659                                                         switch(param->integer_value) {
660                                                                 case INFO_NTYPE_UNKNOWN:
661                                                                 printf("unknown");
662                                                                 break;
663                                                                 case INFO_NTYPE_SUBSCRIBER:
664                                                                 printf("subscriber");
665                                                                 break;
666                                                                 case INFO_NTYPE_NATIONAL:
667                                                                 printf("national");
668                                                                 break;
669                                                                 case INFO_NTYPE_INTERNATIONAL:
670                                                                 printf("international");
671                                                                 break;
672                                                                 default:
673                                                                 printf("unknown(%d)", param->integer_value);
674                                                         }
675                                                         break;
676                                                 }
677                                                 if (param_defs[param->index].type == PARAM_TYPE_YESNO) {
678                                                         switch(param->integer_value) {
679                                                                 case 1:
680                                                                 printf("yes");
681                                                                 break;
682                                                                 case 0:
683                                                                 printf("no");
684                                                                 break;
685                                                                 default:
686                                                                 printf("unknown(%d)", param->integer_value);
687                                                         }
688                                                         break;
689                                                 }
690                                                 if (param_defs[param->index].type == PARAM_TYPE_NULL) {
691                                                         break;
692                                                 }
693                                                 printf("%d", param->integer_value);
694                                                 break;
695
696                                                 case VALUE_TYPE_STRING:
697                                                 printf("'%s'", param->string_value);
698                                                 break;
699
700                                                 default:
701                                                 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", param->value_type, __FUNCTION__, __LINE__);
702                                         }
703                                         param = param->next;
704                                         printf("\n");
705                                 }
706                                 /* TIMEOUT */
707                                 if (action->timeout)
708                                         printf("    Timeout: %d\n", action->timeout);
709                                 action = action->next;
710                         }
711                         printf("\n");
712                         rule = rule->next;
713                 }
714                 printf("\n");
715                 ruleset = ruleset->next;
716         }
717 }
718
719
720 /*
721  * parse ruleset
722  */
723 static char *read_string(char *p, char *key, int key_size, const char *special)
724 {
725         key[0] = 0;
726
727         if (*p == '\"') {
728                 p++;
729                 /* quote */
730                 while(*p) {
731                         if (*p == '\"') {
732                                 p++;
733                                 *key = '\0';
734                                 return(p);
735                         }
736                         if (*p == '\\') {
737                                 p++;
738                                 if (*p == '\0') {
739                                         break;
740                                 }
741                         }
742                         if (--key_size == 0) {
743                                 UPRINT(key, "\001String too long.");
744                                 return(p);
745                         }
746                         *key++ = *p++;
747                 }
748                 UPRINT(key, "\001Unexpected end of line inside quotes.");
749                 return(p);
750         }
751
752         /* no quote */
753         while(*p) {
754                 if (strchr(special, *p)) {
755                         *key = '\0';
756                         return(p);
757                 }
758                 if (*p == '\\') {
759                         p++;
760                         if (*p == '\0') {
761                                 UPRINT(key, "\001Unexpected end of line.");
762                                 return(p);
763                         }
764                 }
765                 if (--key_size == 0) {
766                         UPRINT(key, "\001String too long.");
767                         return(p);
768                 }
769                 *key++ = *p++;
770         }
771         *key = '\0';
772         return(p);
773 }
774 char ruleset_error[256];
775 struct route_ruleset *ruleset_parse(void)
776 {
777 //      char                    from[128];
778 //      char                    to[128];
779         int                     i;
780         unsigned long long      j;
781 //      int                     a,
782 //                              b;
783         #define                 MAXNESTING 8
784         FILE                    *fp[MAXNESTING];
785         char                    filename[MAXNESTING][256];
786         int                     line[MAXNESTING];
787         int                     nesting = -1;
788         char                    buffer[1024],
789                                 key[1024],
790                                 key_to[1024],
791                                 pointer[1024+1],
792                                 *p;
793         int                     expecting = 1; /* 1 = expecting ruleset */
794         int                     index,
795                                 value_type,
796                                 integer,
797                                 integer_to; /* condition index, .. */
798         struct route_ruleset    *ruleset_start = NULL, *ruleset;
799         struct route_ruleset    **ruleset_pointer = &ruleset_start;
800         struct route_rule       *rule;
801         struct route_rule       **rule_pointer = NULL;
802         struct route_cond       *cond;
803         struct route_cond       **cond_pointer = NULL;
804         struct route_action     *action;
805         struct route_action     **action_pointer = NULL;
806         struct route_param      *param;
807         struct route_param      **param_pointer = NULL;
808         char                    failure[256];
809         unsigned long long      allowed_params;
810
811         /* check the integrity of IDs for ACTION_* and PARAM_* */
812         i = 0;
813         while(action_defs[i].name) {
814                 if (action_defs[i].id != i) {
815                         PERROR("Software Error action '%s' must have id of %d, but has %d.\n",
816                                 action_defs[i].name, i, action_defs[i].id);
817                         goto openerror;
818                 }
819                 i++;
820         }
821         i = 0; j = 1;
822         while(param_defs[i].name) {
823                 if (param_defs[i].id != j) {
824                         PERROR("Software Error param '%s' must have id of 0x%llx, but has 0x%llx.\n",
825                                 param_defs[i].name, j, param_defs[i].id);
826                         goto openerror;
827                 }
828                 i++;
829                 j<<=1;
830         }
831
832         SPRINT(filename[0], "%s/routing.conf", CONFIG_DATA);
833
834         if (!(fp[0]=fopen(filename[0],"r")))
835         {
836                 PERROR("Cannot open %s\n",filename[0]);
837                 goto openerror;
838         }
839         nesting++;
840         fduse++;
841
842         go_leaf:
843         line[nesting]=0;
844         go_root:
845         while((GETLINE(buffer, fp[nesting])))
846         {
847                 line[nesting]++;
848                 p = buffer;
849
850                 /* remove tabs */
851                 while(*p) {
852                         if (*p < 32)
853                                 *p = 32;
854                         p++;
855                 } 
856                 p = buffer;
857
858                 /* skip spaces, if any */
859                 while(*p == 32)
860                 {
861                         if (*p == 0)
862                                 break;
863                         p++;
864                 }
865
866                 /* skip comments */
867                 if (*p == '#') {
868                         p++;
869                         /* don't skip "define" */
870                         if (!!strncmp(p, "define", 6))
871                                 continue;
872                         p+=6;
873                         if (*p != 32)
874                                 continue;
875                         /* skip spaces */
876                         while(*p == 32) {
877                                 if (*p == 0)
878                                         break;
879                                 p++;
880                         }
881                         p++;
882                         p = read_string(p, key, sizeof(key), " ");
883                         if (key[0] == 1) { /* error */
884                                 SPRINT(failure, "Parsing Filename failed: %s", key+1);
885                                 goto parse_error;
886                         }
887                         if (nesting == MAXNESTING-1) {
888                                 SPRINT(failure, "'include' is nesting too deep.\n");
889                                 goto parse_error;
890                         }
891                         if (key[0] == '/')
892                                 SCPY(filename[nesting+1], key);
893                         else
894                                 SPRINT(filename[nesting+1], "%s/%s", CONFIG_DATA, key);
895                         if (!(fp[nesting+1]=fopen(filename[nesting+1],"r"))) {
896                                 PERROR("Cannot open %s\n", filename[nesting+1]);
897                                 goto parse_error;
898                         }
899                         fduse++;
900                         nesting++;
901                         goto go_leaf;
902                 }
903                 if (*p == '/') if (p[1] == '/')
904                         continue;
905
906                 /* skip empty lines */
907                 if (*p == 0)
908                         continue;
909
910                 /* expecting ruleset */
911                 if (expecting) {
912                         new_ruleset:
913                         /* expecting [ */
914                         if (*p != '[') {
915                                 SPRINT(failure, "Expecting ruleset name starting with '['.");
916                                 goto parse_error;
917                         }
918                         p++;
919
920                         /* reading ruleset name text */
921                         i = 0;
922                         while(*p>' ' && *p<127 && *p!=']') {
923                                 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
924                                 key[i++] = *p++;
925                                 if (i == sizeof(key)) i--; /* limit */
926                         }
927                         key[i] = 0;
928                         if (key[0] == '\0') {
929                                 SPRINT(failure, "Missing ruleset name after '['.");
930                                 goto parse_error;
931                         }
932
933                         /* expecting ] and nothing more */
934                         if (*p != ']') {
935                                 SPRINT(failure, "Expecting ']' after ruleset name.");
936                                 goto parse_error;
937                         }
938                         p++;
939                         if (*p != 0) {
940                                 SPRINT(failure, "Unexpected character after ruleset name.");
941                                 goto parse_error;
942                         }
943
944                         /* check for duplicate rulesets */
945                         ruleset = ruleset_start;
946                         while(ruleset) {
947                                 if (!strcmp(ruleset->name, key)) {
948                                         SPRINT(failure, "Duplicate ruleset '%s', already defined in file '%s' line %d.", key, ruleset->file, ruleset->line);
949                                         goto parse_error;
950                                 }
951                                 ruleset = ruleset->next;
952                         }
953
954                         /* create ruleset */
955                         ruleset = (struct route_ruleset *)MALLOC(sizeof(struct route_ruleset));
956                         rmemuse++;
957                         *ruleset_pointer = ruleset;
958                         ruleset_pointer = &(ruleset->next);
959                         SCPY(ruleset->name, key);
960                         SCPY(ruleset->file, filename[nesting]);
961                         ruleset->line = line[nesting];
962                         rule_pointer = &(ruleset->rule_first);
963                         expecting = 0;
964                         continue;
965                 }
966
967                 /* for new ruleset [ */
968                 if (*p == '[') {
969                         goto new_ruleset;
970                 }
971
972                 /* Alloc memory for rule */
973                 rule = (struct route_rule *)MALLOC(sizeof(struct route_rule));
974                 rmemuse++;
975                 *rule_pointer = rule;
976                 rule_pointer = &(rule->next);
977                 cond_pointer = &(rule->cond_first);
978                 action_pointer = &(rule->action_first);
979                 SCPY(rule->file, filename[nesting]);
980                 rule->line = line[nesting];
981
982                 /* loop CONDITIONS */
983                 while(*p!=':' && *p!='\0') {
984                         /* read item text */
985                         i = 0;
986                         while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9')) {
987                                 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
988                                 key[i++] = *p++;
989                                 if (i == sizeof(key)) i--; /* limit */
990                         }
991                         key[i] = 0;
992                         if (key[0] == '\0') {
993                                 SPRINT(failure, "Expecting condition item name or ':' for end of condition list.");
994                                 goto parse_error;
995                         }
996                         if (*p!=' ' && *p!='=') {
997                                 SPRINT(failure, "Illegal character '%c' after condition name '%s'. Expecting '=' for equation or ' ' to seperate condition items.", *p, key);
998                                 goto parse_error;
999                         }
1000
1001                         /* check if condition exists */
1002                         index = 0;
1003                         while(cond_defs[index].name) {
1004                                 if (!strcmp(cond_defs[index].name, key))
1005                                         break;
1006                                 index++;
1007                         }
1008                         if (cond_defs[index].name == NULL) {
1009                                 SPRINT(failure, "Unknown condition item name '%s'.", key);
1010                                 goto parse_error;
1011                         }
1012
1013                         /* items without values must not have any parameter */
1014                         if (cond_defs[index].type == COND_TYPE_NULL) {
1015                                 if (*p == '=') {
1016                                         SPRINT(failure, "Condition item '%s' must not have any value. Don't use '=' for this type of condition.", key);
1017                                         goto parse_error;
1018                                 }
1019                                 if (*p != ' ') {
1020                                         SPRINT(failure, "Condition item '%s' must not have any value. Expecting ' ' or tab after item name.", key);
1021                                         goto parse_error;
1022                                 }
1023 //                              p++;
1024                         } else {
1025                                 if (*p == ' ') {
1026                                         SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected, and not a space.", key);
1027                                         goto parse_error;
1028                                 }
1029                                 if (*p != '=') {
1030                                         SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected.", key);
1031                                         goto parse_error;
1032                                 }
1033                                 p++;
1034                         }
1035
1036                         /* check for duplicate condition */
1037                         cond = rule->cond_first;
1038                         while(cond) {
1039                                 if (cond->index == index) {
1040                                         SPRINT(failure, "Duplicate condition '%s', use ',' to give multiple values.", key);
1041                                         goto parse_error;
1042                                 }
1043                                 cond = cond->next;
1044                         }
1045
1046                         nextcondvalue:
1047                         /* Alloc memory for item */
1048                         cond = (struct route_cond *)MALLOC(sizeof(struct route_cond));
1049                         rmemuse++;
1050                         *cond_pointer = cond;
1051                         cond_pointer = &(cond->next);
1052                         cond->index = index;
1053                         cond->match = cond_defs[index].match;
1054                         switch(cond_defs[index].type) {
1055                                 case COND_TYPE_NULL:
1056                                 if (*p=='=') {
1057                                         SPRINT(failure, "Expecting no value.");
1058                                         goto parse_error;
1059                                 }
1060                                 value_type = VALUE_TYPE_NULL;
1061                                 break;
1062
1063                                 /* parse all integer values/ranges */
1064                                 case COND_TYPE_INTEGER:
1065                                 case COND_TYPE_TIME:
1066                                 case COND_TYPE_MDAY:
1067                                 case COND_TYPE_MONTH:
1068                                 case COND_TYPE_WDAY:
1069                                 case COND_TYPE_YEAR:
1070                                 integer = integer_to = 0;
1071                                 if (*p==',' || *p==' ' || *p=='\0') {
1072                                         SPRINT(failure, "Missing integer value.");
1073                                         goto parse_error;
1074                                 }
1075                                 while(*p>='0' && *p<='9') {
1076                                         integer = integer*10 + *p-'0';
1077                                         p++;
1078                                 }
1079                                 value_type = VALUE_TYPE_INTEGER;
1080                                 if (*p == '-') {
1081                                         p++;
1082                                         if (*p==',' || *p==' ' || *p=='\0') {
1083                                                 SPRINT(failure, "Missing integer value.");
1084                                                 goto parse_error;
1085                                         }
1086                                         while(*p>='0' && *p<='9') {
1087                                                 integer_to = integer_to*10 + *p-'0';
1088                                                 p++;
1089                                         }
1090                                         value_type = VALUE_TYPE_INTEGER_RANGE;
1091                                 }
1092                                 if (cond_defs[index].type == COND_TYPE_TIME) {
1093                                         // Simon: i store the time as decimal, later i compare it correctly:
1094                                         // hours * 100 + minutes
1095                                         if (integer == 2400)
1096                                                 integer = 0;
1097                                         if (integer >= 2400) {
1098                                                 timeoutofrange1:
1099                                                 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer);
1100                                                 goto parse_error;
1101                                         }
1102                                         if (integer%100 >= 60)
1103                                                 goto timeoutofrange1;
1104                                         if (value_type == VALUE_TYPE_INTEGER)
1105                                                 goto integer_done;
1106                                         if (integer_to == 2400)
1107                                                 integer_to = 0;
1108                                         if (integer_to >= 2400) {
1109                                                 timeoutofrange2:
1110                                                 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer_to);
1111                                                 goto parse_error;
1112                                         }
1113                                         if (integer_to%100 >= 60)
1114                                                 goto timeoutofrange2;
1115                                 }
1116                                 if (cond_defs[index].type == COND_TYPE_MDAY) {
1117                                         if (integer<1 || integer>31) {
1118                                                 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer);
1119                                                 goto parse_error;
1120                                         } 
1121                                         if (value_type == VALUE_TYPE_INTEGER)
1122                                                 goto integer_done;
1123                                         if (integer_to<1 || integer_to>31) {
1124                                                 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer_to);
1125                                                 goto parse_error;
1126                                         } 
1127                                 }
1128                                 if (cond_defs[index].type == COND_TYPE_WDAY) {
1129                                         if (integer<1 || integer>7) {
1130                                                 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer);
1131                                                 goto parse_error;
1132                                         } 
1133                                         if (value_type == VALUE_TYPE_INTEGER)
1134                                                 goto integer_done;
1135                                         if (integer_to<1 || integer_to>7) {
1136                                                 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer_to);
1137                                                 goto parse_error;
1138                                         } 
1139                                 }
1140                                 if (cond_defs[index].type == COND_TYPE_MONTH) {
1141                                         if (integer<1 || integer>12) {
1142                                                 SPRINT(failure, "Given month '%d' not in range 1..12", integer);
1143                                                 goto parse_error;
1144                                         } 
1145                                         if (value_type == VALUE_TYPE_INTEGER)
1146                                                 goto integer_done;
1147                                         if (integer_to<1 || integer_to>12) {
1148                                                 SPRINT(failure, "Given month '%d' not in range 1..12", integer_to);
1149                                                 goto parse_error;
1150                                         } 
1151                                 }
1152                                 if (cond_defs[index].type == COND_TYPE_YEAR) {
1153                                         if (integer<1970 || integer>2106) {
1154                                                 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer);
1155                                                 goto parse_error;
1156                                         } 
1157                                         if (value_type == VALUE_TYPE_INTEGER)
1158                                                 goto integer_done;
1159                                         if (integer_to<1970 || integer_to>2106) {
1160                                                 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer_to);
1161                                                 goto parse_error;
1162                                         } 
1163                                 }
1164                                 integer_done:
1165                                 cond->integer_value = integer;
1166                                 cond->integer_value_to = integer_to;
1167                                 cond->value_type = value_type;
1168                                 break;
1169
1170                                 /* parse all string values/ranges */
1171                                 case COND_TYPE_STRING:
1172                                 key[0] = key_to[0] = '\0';
1173                                 if (*p==',' || *p==' ' || *p=='\0') {
1174                                         SPRINT(failure, "Missing string value, use \"\" for empty string.");
1175                                         goto parse_error;
1176                                 }
1177                                 p = read_string(p, key, sizeof(key), "-, ");
1178                                 if (key[0] == 1) { /* error */
1179                                         SPRINT(failure, "Parsing String failed: %s", key+1);
1180                                         goto parse_error;
1181                                 }
1182                                 value_type = VALUE_TYPE_STRING;
1183                                 if (*p == '-') {
1184                                         p++;
1185                                         if (*p==',' || *p==' ' || *p=='\0') {
1186                                                 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1187                                                 goto parse_error;
1188                                         }
1189                                         p = read_string(p, key_to, sizeof(key_to), "-, ");
1190                                         if (key_to[0] == 1) { /* error */
1191                                                 SPRINT(failure, "Parsing string failed: %s", key_to+1);
1192                                                 goto parse_error;
1193                                         }
1194                                         value_type = VALUE_TYPE_STRING_RANGE;
1195                                         if (strlen(key) != strlen(key_to)) {
1196                                                 SPRINT(failure, "Given range of strings \"%s\"-\"%s\" have unequal length.", key, key_to);
1197                                                 goto parse_error;
1198                                         }
1199                                         if (key[0] == '\0') {
1200                                                 SPRINT(failure, "Given range has no length.");
1201                                                 goto parse_error;
1202                                         }
1203                                 }
1204                                 alloc_string:
1205                                 cond->string_value = (char *)MALLOC(strlen(key)+1);
1206                                 rmemuse++;
1207                                 UCPY(cond->string_value, key);
1208                                 if (value_type == VALUE_TYPE_STRING_RANGE) {
1209                                         cond->string_value_to = (char *)MALLOC(strlen(key_to)+1);
1210                                         rmemuse++;
1211                                         UCPY(cond->string_value_to, key_to);
1212                                         cond->comp_string = strcmp(key, key_to);
1213                                 }
1214                                 cond->value_type = value_type;
1215                                 break;
1216
1217                                 /* parse service value */
1218                                 case COND_TYPE_CAPABILITY:
1219                                 if (!strncasecmp("speech", p, 6))
1220                                         cond->integer_value = INFO_BC_SPEECH;
1221                                 else if (!strncasecmp("audio", p, 5))
1222                                         cond->integer_value = INFO_BC_AUDIO;
1223                                 else if (!strncasecmp("video", p, 5))
1224                                         cond->integer_value = INFO_BC_VIDEO;
1225                                 else if (!strncasecmp("digital-restricted", p, 18))
1226                                         cond->integer_value = INFO_BC_DATARESTRICTED;
1227                                 else if (!strncasecmp("digital-unrestricted", p, 20))
1228                                         cond->integer_value = INFO_BC_DATAUNRESTRICTED;
1229                                 else if (!strncasecmp("digital-unrestricted-tones", p, 26))
1230                                         cond->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1231                                 else {
1232                                         SPRINT(failure, "Given service type is invalid or misspelled.");
1233                                         goto parse_error;
1234                                 }
1235                                 cond->value_type = VALUE_TYPE_INTEGER;
1236                                 break;
1237
1238                                 /* parse bmode value */
1239                                 case COND_TYPE_BMODE:
1240                                 if (!strncasecmp("transparent", p, 11))
1241                                         cond->integer_value = INFO_BMODE_CIRCUIT;
1242                                 else if (!strncasecmp("hdlc", p, 4))
1243                                         cond->integer_value = INFO_BMODE_PACKET;
1244                                 else {
1245                                         SPRINT(failure, "Given bchannel mode is invalid or misspelled.");
1246                                         goto parse_error;
1247                                 }
1248                                 cond->value_type = VALUE_TYPE_INTEGER;
1249                                 break;
1250
1251                                 /* parse interface attribute <if>:<value> */
1252                                 case COND_TYPE_IFATTR:
1253                                 key[0] = key_to[0] = '\0';
1254                                 if (*p==':' || *p==',' || *p==' ' || *p=='\0') {
1255                                         SPRINT(failure, "Missing interface name.");
1256                                         goto parse_error;
1257                                 }
1258                                 p = read_string(p, key, sizeof(key), ":-, ");
1259                                 if (key[0] == 1) { /* error */
1260                                         SPRINT(failure, "Parsing interface failed: %s", key+1);
1261                                         goto parse_error;
1262                                 }
1263                                 if (*p != ':') {
1264                                         SPRINT(failure, "Expeciting kolon to seperate value behind interface name.");
1265                                         goto parse_error;
1266                                 }
1267                                 SCCAT(key, *p++);
1268                                 while(*p>='0' && *p<='9') {
1269                                         SCCAT(key, *p++);
1270                                 }
1271                                 if (*p!=',' && *p!=' ' && *p!='\0') {
1272                                         SPRINT(failure, "Invalid characters behind value.");
1273                                         goto parse_error;
1274                                 }
1275                                 value_type = VALUE_TYPE_STRING;
1276                                 goto alloc_string;
1277                                 break;
1278
1279                                 default:
1280                                 SPRINT(failure, "Software error: COND_TYPE_* %d not parsed in function '%s'", cond_defs[index].type, __FUNCTION__);
1281                                 goto parse_error;
1282                         }
1283                         /* if we have another value for that item, we attach it */
1284                         if (*p == ',') {
1285                                 p++;
1286                                 /* next item */
1287                                 cond->value_extension = 1;
1288                                 goto nextcondvalue;
1289                         }
1290                         /* to seperate the items, a space is required */
1291                         if (*p != ' ') {
1292                                 SPRINT(failure, "Character '%c' not expected here. Use ',' to seperate multiple possible values.", *p);
1293                                 goto parse_error;
1294                         }
1295                         /* skip spaces */
1296                         while(*p == 32)
1297                         {
1298                                 if (*p == 0)
1299                                         break;
1300                                 p++;
1301                         }
1302                 }
1303
1304                 /* we are done with CONDITIONS, so we expect the ACTION */
1305                 if (*p != ':') {
1306                         SPRINT(failure, "Expecting ':' after condition item(s).");
1307                         goto parse_error;
1308                 }
1309                 p++;
1310
1311                 nextaction:
1312                 /* skip spaces, if any */
1313                 while(*p == 32)
1314                 {
1315                         if (*p == 0)
1316                                 break;
1317                         p++;
1318                 }
1319
1320                 /* read action name */
1321                 i = 0;
1322                 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9') || *p == '-') {
1323                         if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1324                         key[i++] = *p++;
1325                         if (i == sizeof(key)) i--; /* limit */
1326                 }
1327                 key[i] = 0;
1328                 if (key[0] == '\0') {
1329                         SPRINT(failure, "Expecting action name.");
1330                         goto parse_error;
1331                 }
1332
1333                 /* check if item exists */
1334                 index = 0;
1335                 while(action_defs[index].name) {
1336                         if (!action_defs[index].help) { /* not internal actions */
1337                                 index++;
1338                                 continue;
1339                         }
1340                         if (!strcmp(action_defs[index].name, key))
1341                                 break;
1342                         index++;
1343                 }
1344                 if (action_defs[index].name == NULL) {
1345                         SPRINT(failure, "Unknown action name '%s'.", key);
1346                         goto parse_error;
1347                 }
1348                 allowed_params = action_defs[index].params;
1349
1350                 /* alloc memory for action */
1351                 action = (struct route_action *)MALLOC(sizeof(struct route_action));
1352                 rmemuse++;
1353                 *action_pointer = action;
1354                 action_pointer = &(action->next);
1355                 param_pointer = &(action->param_first);
1356                 action->index = index;
1357                 action->line = line[nesting];
1358
1359                 /* skip spaces after param name */
1360                 while(*p == 32)
1361                 {
1362                         if (*p == 0)
1363                                 break;
1364                         p++;
1365                 }
1366
1367                 /* loop PARAMS */
1368                 while(*p != 0) {
1369                         /* read param text */
1370                         i = 0;
1371                         while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9')) {
1372                                 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1373                                 key[i++] = *p++;
1374                                 if (i == sizeof(key)) i--; /* limit */
1375                         }
1376                         key[i] = 0;
1377                         if (key[0] == '\0') {
1378                                 SPRINT(failure, "Expecting parameter name.");
1379                                 goto parse_error;
1380                         }
1381
1382                         /* check if item exists */
1383                         index = 0;
1384                         while(param_defs[index].name) {
1385                                 if (!strcmp(param_defs[index].name, key))
1386                                         break;
1387                                 index++;
1388                         }
1389                         if (param_defs[index].name == NULL) {
1390                                 SPRINT(failure, "Unknown param name '%s'.", key);
1391                                 goto parse_error;
1392                         }
1393
1394                         /* check if item is allowed for the action */
1395                         if (!(param_defs[index].id & allowed_params)) {
1396                                 SPRINT(failure, "Param name '%s' exists, but not for this action.", key);
1397                                 goto parse_error;
1398                         }
1399
1400                         /* params without values must not have any parameter */
1401                         if (param_defs[index].type == PARAM_TYPE_NULL) {
1402                                 if (*p!=' ' && *p!='\0') {
1403                                         SPRINT(failure, "Parameter '%s' must not have any value.", key);
1404                                         goto parse_error;
1405                                 }
1406                         } else {
1407                                 if (*p == ' ') {
1408                                         SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected and not a space.", key);
1409                                         goto parse_error;
1410                                 }
1411                                 if (*p != '=') {
1412                                         SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected.", key);
1413                                         goto parse_error;
1414                                 }
1415                                 p++;
1416                         }
1417
1418                         /* special timeout value */
1419                         if (!strcmp("timeout", key)) {
1420                                 if (action->timeout) {
1421                                         SPRINT(failure, "Duplicate timeout value.");
1422                                         goto parse_error;
1423                                 }
1424                                 if (*p==',' || *p==' ' || *p=='\0') {
1425                                         SPRINT(failure, "Missing integer value.");
1426                                         goto parse_error;
1427                                 }
1428                                 integer = 0;
1429                                 while(*p>='0' && *p<='9') {
1430                                         integer = integer*10 + *p-'0';
1431                                         p++;
1432                                 }
1433                                 if (integer < 1) {
1434                                         SPRINT(failure, "Expecting timeout value greater 0.");
1435                                         goto parse_error;
1436                                 }
1437                                 if (*p!=' ' && *p!='\0') {
1438                                         SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters.", *p);
1439                                         goto parse_error;
1440                                 }
1441                                 /* skip spaces */
1442                                 while(*p == 32) {
1443                                         if (*p == 0)
1444                                                 break;
1445                                         p++;
1446                                 }
1447                                 action->timeout = integer;
1448                                 /* check for next ACTION */
1449                                 if (*p == ':') {
1450                                         p++;
1451                                         goto nextaction;
1452                                 }
1453                                 continue;
1454                         }
1455
1456                         /* check for duplicate parameters */
1457                         param = action->param_first;
1458                         while(param) {
1459                                 if (param->index == index) {
1460                                         SPRINT(failure, "Duplicate parameter '%s', use ',' to give multiple values.", key);
1461                                         goto parse_error;
1462                                 }
1463                                 param = param->next;
1464                         }
1465
1466                         nextparamvalue:
1467                         /* Alloc memory for param */
1468                         param = (struct route_param *)MALLOC(sizeof(struct route_param));
1469                         rmemuse++;
1470                         *param_pointer = param;
1471                         param_pointer = &(param->next);
1472                         param->index = index;
1473                         param->id = param_defs[index].id;
1474
1475                         switch(param_defs[index].type) {
1476                                 /* parse null value */
1477                                 case PARAM_TYPE_NULL:
1478                                 param->value_type = VALUE_TYPE_NULL;
1479                                 break;
1480
1481                                 /* parse integer value */
1482                                 case PARAM_TYPE_INTEGER:
1483                                 integer = 0;
1484                                 if (*p==',' || *p==' ' || *p=='\0') {
1485                                         SPRINT(failure, "Missing integer value.");
1486                                         goto parse_error;
1487                                 }
1488                                 while(*p>='0' && *p<='9') {
1489                                         integer = integer*10 + *p-'0';
1490                                         p++;
1491                                 }
1492                                 param->integer_value = integer;
1493                                 param->value_type = VALUE_TYPE_INTEGER;
1494                                 break;
1495
1496 #if 0
1497                                 /* parse ports value */
1498                                 case PARAM_TYPE_PORTS:
1499                                 key[0] = '\0';
1500                                 if (*p==',' || *p==' ' || *p=='\0') {
1501                                         SPRINT(failure, "Missing port number, omit parameter or give port number.");
1502                                         goto parse_error;
1503                                 }
1504                                 i = 0;
1505                                 nextport:
1506                                 integer = 0;
1507                                 while(*p>='0' && *p<='9') {
1508                                         if (i < (int)sizeof(key)-1) {
1509                                                 key[i] = *p;
1510                                                 key[i++] = '\0';
1511                                         }
1512                                         integer = integer*10 + *p-'0';
1513                                         p++;
1514                                 }
1515                                 if (integer > 255) {
1516                                         SPRINT(failure, "Port number too high.");
1517                                         goto parse_error;
1518                                 }
1519                                 if (*p==',') {
1520                                         if (i < (int)sizeof(key)-1) {
1521                                                 key[i] = *p;
1522                                                 key[i++] = '\0';
1523                                         }
1524                                         p++;
1525                                         goto nextport;
1526                                 }
1527                                 goto mallocstring;
1528 #endif
1529
1530                                 /* parse string value */
1531                                 case PARAM_TYPE_STRING:
1532                                 case PARAM_TYPE_CALLERIDTYPE:
1533                                 case PARAM_TYPE_CAPABILITY:
1534                                 case PARAM_TYPE_BMODE:
1535                                 case PARAM_TYPE_DIVERSION:
1536                                 case PARAM_TYPE_DESTIN:
1537                                 case PARAM_TYPE_TYPE:
1538                                 case PARAM_TYPE_YESNO:
1539                                 case PARAM_TYPE_ON:
1540                                 key[0] = '\0';
1541                                 if (*p==',' || *p==' ' || *p=='\0') {
1542                                         SPRINT(failure, "Missing string value, use \"\" for empty string.");
1543                                         goto parse_error;
1544                                 }
1545                                 p = read_string(p, key, sizeof(key), " ");
1546                                 if (key[0] == 1) { /* error */
1547                                         SPRINT(failure, "Parsing string failed: %s", key+1);
1548                                         goto parse_error;
1549                                 }
1550                                 if (param_defs[index].type == PARAM_TYPE_CALLERIDTYPE) {
1551                                         param->value_type = VALUE_TYPE_INTEGER;
1552                                         if (!strcasecmp(key, "unknown")) {
1553                                                 param->integer_value = INFO_NTYPE_UNKNOWN;
1554                                                 break;
1555                                         }
1556                                         if (!strcasecmp(key, "subscriber")) {
1557                                                 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1558                                                 break;
1559                                         }
1560                                         if (!strcasecmp(key, "national")) {
1561                                                 param->integer_value = INFO_NTYPE_NATIONAL;
1562                                                 break;
1563                                         }
1564                                         if (!strcasecmp(key, "international")) {
1565                                                 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1566                                                 break;
1567                                         }
1568                                         SPRINT(failure, "Caller ID type '%s' unknown.", key);
1569                                         goto parse_error;
1570                                 }
1571                                 if (param_defs[index].type == PARAM_TYPE_ON) {
1572                                         param->value_type = VALUE_TYPE_INTEGER;
1573                                         if (!strcasecmp(key, "init")) {
1574                                                 param->integer_value = INFO_ON_INIT;
1575                                                 break;
1576                                         }
1577                                         if (!strcasecmp(key, "hangup")) {
1578                                                 param->integer_value = INFO_ON_HANGUP;
1579                                                 break;
1580                                         }
1581                                         SPRINT(failure, "Execute on '%s' unknown.", key);
1582                                         goto parse_error;
1583                                 }
1584                                 if (param_defs[index].type == PARAM_TYPE_CAPABILITY) {
1585                                         param->value_type = VALUE_TYPE_INTEGER;
1586                                         if (!strcasecmp(key, "speech")) {
1587                                                 param->integer_value = INFO_BC_SPEECH;
1588                                                 break;
1589                                         }
1590                                         if (!strcasecmp(key, "audio")) {
1591                                                 param->integer_value = INFO_BC_AUDIO;
1592                                                 break;
1593                                         }
1594                                         if (!strcasecmp(key, "video")) {
1595                                                 param->integer_value = INFO_BC_VIDEO;
1596                                                 break;
1597                                         }
1598                                         if (!strcasecmp(key, "digital-restricted")) {
1599                                                 param->integer_value = INFO_BC_DATARESTRICTED;
1600                                                 break;
1601                                         }
1602                                         if (!strcasecmp(key, "digital-unrestricted")) {
1603                                                 param->integer_value = INFO_BC_DATAUNRESTRICTED;
1604                                                 break;
1605                                         }
1606                                         if (!strcasecmp(key, "digital-unrestricted-tones")) {
1607                                                 param->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1608                                                 break;
1609                                         }
1610                                         SPRINT(failure, "Service type '%s' unknown.", key);
1611                                         goto parse_error;
1612                                 }
1613                                 if (param_defs[index].type == PARAM_TYPE_BMODE) {
1614                                         param->value_type = VALUE_TYPE_INTEGER;
1615                                         if (!strcasecmp(key, "transparent")) {
1616                                                 param->integer_value = INFO_BMODE_CIRCUIT;
1617                                                 break;
1618                                         }
1619                                         if (!strcasecmp(key, "hdlc")) {
1620                                                 param->integer_value = INFO_BMODE_PACKET;
1621                                                 break;
1622                                         }
1623                                         SPRINT(failure, "Bchannel mode '%s' unknown.", key);
1624                                         goto parse_error;
1625                                 }
1626                                 if (param_defs[index].type == PARAM_TYPE_DIVERSION) {
1627                                         param->value_type = VALUE_TYPE_INTEGER;
1628                                         if (!strcasecmp(key, "cfu")) {
1629                                                 param->integer_value = INFO_DIVERSION_CFU;
1630                                                 break;
1631                                         }
1632                                         if (!strcasecmp(key, "cfb")) {
1633                                                 param->integer_value = INFO_DIVERSION_CFB;
1634                                                 break;
1635                                         }
1636                                         if (!strcasecmp(key, "cfnr")) {
1637                                                 param->integer_value = INFO_DIVERSION_CFNR;
1638                                                 break;
1639                                         }
1640                                         if (!strcasecmp(key, "cfp")) {
1641                                                 param->integer_value = INFO_DIVERSION_CFP;
1642                                                 break;
1643                                         }
1644                                         SPRINT(failure, "Diversion type '%s' unknown.", key);
1645                                         goto parse_error;
1646                                 }
1647                                 if (param_defs[index].type == PARAM_TYPE_TYPE) {
1648                                         param->value_type = VALUE_TYPE_INTEGER;
1649                                         if (!strcasecmp(key, "unknown")) {
1650                                                 param->integer_value = INFO_NTYPE_UNKNOWN;
1651                                                 break;
1652                                         }
1653                                         if (!strcasecmp(key, "subscriber")) {
1654                                                 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1655                                                 break;
1656                                         }
1657                                         if (!strcasecmp(key, "national")) {
1658                                                 param->integer_value = INFO_NTYPE_NATIONAL;
1659                                                 break;
1660                                         }
1661                                         if (!strcasecmp(key, "international")) {
1662                                                 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1663                                                 break;
1664                                         }
1665                                         SPRINT(failure, "Number type '%s' unknown.", key);
1666                                         goto parse_error;
1667                                 }
1668                                 if (param_defs[index].type == PARAM_TYPE_YESNO) {
1669                                         param->value_type = VALUE_TYPE_INTEGER;
1670                                         if (!strcasecmp(key, "yes")) {
1671                                                 param->integer_value = 1;
1672                                                 break;
1673                                         }
1674                                         if (!strcasecmp(key, "no")) {
1675                                                 param->integer_value = 0;
1676                                                 break;
1677                                         }
1678                                         SPRINT(failure, "Value '%s' unknown. ('yes' or 'no')", key);
1679                                         goto parse_error;
1680                                 }
1681                                 param->string_value = (char *)MALLOC(strlen(key)+1);
1682                                 rmemuse++;
1683                                 UCPY(param->string_value, key);
1684                                 param->value_type = VALUE_TYPE_STRING;
1685                                 break;
1686
1687                                 default:
1688                                 SPRINT(failure, "Software error: PARAM_TYPE_* %d not parsed in function '%s'", param_defs[index].type, __FUNCTION__);
1689                                 goto parse_error;
1690                         }
1691
1692                         if (*p == ',') {
1693                                 p++;
1694                                 /* next item */
1695                                 param->value_extension = 1;
1696                                 goto nextparamvalue;
1697                         }
1698
1699                         /* end of line */
1700                         if (*p == '\0')
1701                                 break;
1702
1703                         /* to seperate the items, a space is required */
1704                         if (*p != ' ') {
1705                                 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters, or ',' for multiple values.", *p);
1706                                 goto parse_error;
1707                         }
1708                         /* skip spaces */
1709                         while(*p == 32)
1710                         {
1711                                 if (*p == 0)
1712                                         break;
1713                                 p++;
1714                         }
1715
1716                         /* check for next ACTION */
1717                         if (*p == ':') {
1718                                 p++;
1719                                 goto nextaction;
1720                         }
1721                 }
1722         }
1723
1724         fclose(fp[nesting--]);
1725         fduse--;
1726
1727         if (nesting >= 0)
1728                 goto go_root;
1729
1730         if (!ruleset_start) {
1731                 SPRINT(failure, "No ruleset defined.");
1732         }
1733         return(ruleset_start);
1734
1735         parse_error:
1736         printf("While parsing %s, an error occurred in line %d:\n", filename[nesting], line[nesting]);
1737         printf("-> %s\n", buffer);
1738         memset(pointer, ' ', sizeof(pointer));
1739         pointer[p-buffer] = '^';
1740         pointer[p-buffer+1] = '\0';
1741         printf("   %s\n", pointer);
1742         printf("%s\n", failure);
1743         SPRINT(ruleset_error, "Error in file %s, line %d: %s",  filename[nesting], line[nesting], failure);
1744
1745         openerror:
1746         while(nesting >= 0) {
1747                 fclose(fp[nesting--]);
1748                 fduse--;
1749         }
1750
1751         ruleset_free(ruleset_start);
1752         return(NULL);
1753 }
1754
1755 /*
1756  * return ruleset by name
1757  */
1758 struct route_ruleset *getrulesetbyname(const char *name)
1759 {
1760         struct route_ruleset *ruleset = ruleset_first;
1761
1762         while(ruleset) {
1763                 if (!strcasecmp(name, ruleset->name)) {
1764                         break;
1765                 }
1766                 ruleset = ruleset->next;
1767         }
1768         PDEBUG(DEBUG_ROUTE, "ruleset %s %s.\n", name, ruleset?"found":"not found");
1769         return(ruleset);
1770 }
1771
1772 /*
1773  * parses the current ruleset and returns action
1774  */
1775 struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
1776 {
1777         int                     match,
1778                                 couldmatch = 0, /* any rule could match */
1779                                 istrue,
1780                                 couldbetrue,
1781                                 condition,
1782                                 dialing_required,
1783                                 avail,
1784                                 any;
1785         struct route_rule       *rule = ruleset->rule_first;
1786         struct route_cond       *cond;
1787         struct route_action     *action = NULL;
1788         unsigned long           comp_len;
1789         int                     j, jj;
1790         char                    callerid[64], callerid2[64], redirid[64];
1791         int                     integer;
1792         char                    *string;
1793         FILE                    *tfp;
1794         double                  timeout;
1795         struct mISDNport        *mISDNport;
1796         struct admin_list       *admin;
1797
1798         /* reset timeout action */
1799         e_match_timeout = 0; /* no timeout */
1800         e_match_to_action = NULL;
1801
1802         SCPY(callerid, numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international));
1803         SCPY(callerid2, numberrize_callerinfo(e_callerinfo.id2, e_callerinfo.ntype2, options.national, options.international));
1804         SCPY(redirid, numberrize_callerinfo(e_redirinfo.id, e_redirinfo.ntype, options.national, options.international));
1805         
1806         PDEBUG(DEBUG_ROUTE, "parsing ruleset '%s'\n", ruleset->name);
1807         while(rule) {
1808                 PDEBUG(DEBUG_ROUTE, "checking rule in line %d\n", rule->line);
1809                 match = 1; /* this rule matches */
1810                 dialing_required = 0;
1811                 timeout = 0; /* timeout time */
1812                 cond = rule->cond_first;
1813                 while(cond) {
1814                         condition = 0; /* any condition element is true (1) or could be true (2) */
1815                         checkextension:
1816                         istrue = 0; /* this condition-element is true */
1817                         couldbetrue = 0; /* this conditions-element could be true */
1818                         switch(cond->match) {
1819                                 case MATCH_EXTERN:
1820                                 if (!e_ext.number[0])
1821                                         istrue = 1;      
1822                                 break;
1823
1824                                 case MATCH_INTERN:
1825                                 if (e_ext.number[0])
1826                                         istrue = 1;      
1827                                 break;
1828
1829                                 case MATCH_PORT:
1830                                 if (ea_endpoint->ep_portlist)
1831                                 if ((ea_endpoint->ep_portlist->port_type & PORT_CLASS_MASK) != PORT_CLASS_mISDN)
1832                                         break;
1833                                 integer = e_callerinfo.isdn_port;
1834                                 goto match_integer;
1835
1836                                 case MATCH_INTERFACE:
1837                                 if (!e_callerinfo.interface[0])
1838                                         break;
1839                                 string = e_callerinfo.interface;
1840                                 goto match_string_prefix;
1841
1842                                 case MATCH_CALLERID:
1843                                 string = callerid;
1844                                 goto match_string_prefix;
1845
1846                                 case MATCH_CALLERID2:
1847                                 string = callerid2;
1848                                 goto match_string_prefix;
1849
1850                                 case MATCH_EXTENSION:
1851                                 string = e_ext.name;
1852                                 goto match_string;
1853
1854                                 case MATCH_DIALING:
1855                                 string = e_dialinginfo.id;
1856                                 goto match_string_prefix;
1857
1858                                 case MATCH_ENBLOCK:
1859                                 if (!e_overlap)
1860                                         istrue = 1;
1861                                 break;
1862
1863                                 case MATCH_OVERLAP:
1864                                 if (e_overlap)
1865                                         istrue = 1;
1866                                 break;
1867
1868                                 case MATCH_ANONYMOUS:
1869                                 if (e_callerinfo.present != INFO_PRESENT_ALLOWED)
1870                                         istrue = 1;
1871                                 break;
1872
1873                                 case MATCH_VISIBLE:
1874                                 if (e_callerinfo.present == INFO_PRESENT_ALLOWED)
1875                                         istrue = 1;
1876                                 break;
1877
1878                                 case MATCH_UNKNOWN:
1879                                 if (e_callerinfo.present == INFO_PRESENT_NOTAVAIL)
1880                                         istrue = 1;
1881                                 break;
1882
1883                                 case MATCH_AVAILABLE:
1884                                 if (e_callerinfo.present != INFO_PRESENT_NOTAVAIL)
1885                                         istrue = 1;
1886                                 break;
1887
1888                                 case MATCH_FAKE:
1889                                 if (e_callerinfo.screen == INFO_SCREEN_USER)
1890                                         istrue = 1;
1891                                 break;
1892
1893                                 case MATCH_REAL:
1894                                 if (e_callerinfo.screen != INFO_SCREEN_USER)
1895                                         istrue = 1;
1896                                 break;
1897
1898                                 case MATCH_REDIRECTED:
1899                                 if (e_redirinfo.ntype != INFO_NTYPE_NOTPRESENT)
1900                                         istrue = 1;
1901                                 break;
1902
1903                                 case MATCH_DIRECT:
1904                                 if (e_redirinfo.ntype == INFO_NTYPE_NOTPRESENT)
1905                                         istrue = 1;
1906                                 break;
1907
1908                                 case MATCH_REDIRID:
1909                                 string = redirid;
1910                                 goto match_string_prefix;
1911
1912                                 case MATCH_TIME:
1913                                 integer = now_tm->tm_hour*100 + now_tm->tm_min;
1914                                 goto match_integer;
1915
1916                                 case MATCH_MDAY:
1917                                 integer = now_tm->tm_mday;
1918                                 goto match_integer;
1919
1920                                 case MATCH_MONTH:
1921                                 integer = now_tm->tm_mon+1;
1922                                 goto match_integer;
1923
1924                                 case MATCH_YEAR:
1925                                 integer = now_tm->tm_year + 1900;
1926                                 goto match_integer;
1927
1928                                 case MATCH_WDAY:
1929                                 integer = now_tm->tm_wday;
1930                                 integer = integer?integer:7; /* correct sunday */
1931                                 goto match_integer;
1932
1933                                 case MATCH_CAPABILITY:
1934                                 integer = e_capainfo.bearer_capa;
1935                                 goto match_integer;
1936         
1937                                 case MATCH_INFOLAYER1:
1938                                 integer = e_capainfo.bearer_info1;
1939                                 goto match_integer;
1940
1941                                 case MATCH_HLC:
1942                                 integer = e_capainfo.hlc;
1943                                 goto match_integer;
1944
1945                                 case MATCH_FILE:
1946                                 tfp = fopen(cond->string_value, "r");
1947                                 if (!tfp) {
1948                                         break;
1949                                 }
1950                                 if (fgetc(tfp) == '1')
1951                                         istrue = 1;
1952                                 fclose(tfp);
1953                                 break;
1954
1955                                 case MATCH_EXECUTE:
1956                                 if (system(cond->string_value) == 0)
1957                                         istrue = 1;
1958                                 break;
1959
1960                                 case MATCH_DEFAULT:
1961                                 if (!couldmatch)
1962                                         istrue = 1;
1963                                 break;
1964
1965                                 case MATCH_TIMEOUT:
1966                                 timeout = now_d + cond->integer_value;
1967                                 istrue = 1;
1968                                 break;
1969
1970                                 case MATCH_FREE:
1971                                 case MATCH_NOTFREE:
1972                                 if (!(comp_len = (unsigned long)strchr(cond->string_value, ':')))
1973                                         break;
1974                                 comp_len = comp_len-(unsigned long)cond->string_value;
1975                                 avail = 0;
1976                                 mISDNport = mISDNport_first;
1977                                 while(mISDNport) {
1978                                         if (mISDNport->ifport)
1979                                         if (strlen(mISDNport->ifport->interface->name) == comp_len)
1980                                         if (!strncasecmp(mISDNport->ifport->interface->name, cond->string_value, comp_len)) 
1981                                         if (!mISDNport->l2hold || mISDNport->l2link>0) {
1982                                                 j = 0;
1983                                                 jj = mISDNport->b_num;
1984                                                 avail += jj;
1985                                                 while(j < jj) {
1986                                                         if (mISDNport->b_state[j])
1987                                                                 avail--;
1988                                                         j++;
1989                                                 }
1990                                         }
1991                                         mISDNport = mISDNport->next;
1992                                 }
1993                                 if (cond->match == MATCH_FREE) {
1994                                         if (avail >= atoi(cond->string_value + comp_len + 1))
1995                                                 istrue = 1;
1996                                 } else {
1997                                         if (avail < atoi(cond->string_value + comp_len + 1))
1998                                                 istrue = 1;
1999                                 }
2000                                 break;
2001
2002
2003                                 case MATCH_DOWN:
2004                                 mISDNport = mISDNport_first;
2005                                 while(mISDNport) {
2006                                         if (mISDNport->ifport)
2007                                         if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value)) 
2008                                         if (!mISDNport->l2hold || mISDNport->l2link>0)
2009                                                 break;
2010                                         mISDNport = mISDNport->next;
2011                                 }
2012                                 if (!mISDNport) /* all down */
2013                                         istrue = 1;
2014                                 break;
2015
2016                                 case MATCH_UP:
2017                                 mISDNport = mISDNport_first;
2018                                 while(mISDNport) {
2019                                         if (mISDNport->ifport)
2020                                         if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value)) 
2021                                         if (!mISDNport->l2hold || mISDNport->l2link>0)
2022                                                 break;
2023                                         
2024                                         mISDNport = mISDNport->next;
2025                                 }
2026                                 if (mISDNport) /* one link at least */
2027                                         istrue = 1;
2028                                 break;
2029
2030                                 case MATCH_BUSY:
2031                                 case MATCH_IDLE:
2032                                 any = 0;
2033                                 mISDNport = mISDNport_first;
2034                                 while(mISDNport) {
2035                                         if (mISDNport->ifport)
2036                                         if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value)) 
2037                                         if (mISDNport->use) /* break if in use */
2038                                                 break;
2039                                         mISDNport = mISDNport->next;
2040                                 }
2041                                 if (mISDNport && cond->match==MATCH_BUSY)
2042                                         istrue = 1;
2043                                 if (!mISDNport && cond->match==MATCH_IDLE)
2044                                         istrue = 1;
2045                                 break;
2046
2047                                 case MATCH_REMOTE:
2048                                 case MATCH_NOTREMOTE:
2049                                 admin = admin_first;
2050                                 while(admin) {
2051                                         if (admin->remote_name[0] && !strcmp(cond->string_value, admin->remote_name))
2052                                                 break;
2053                                         admin = admin->next;
2054                                 }
2055                                 if (admin && cond->match==MATCH_REMOTE)
2056                                         istrue = 1;
2057                                 if (!admin && cond->match==MATCH_NOTREMOTE)
2058                                         istrue = 1;
2059                                 break;
2060
2061                                 default:
2062                                 PERROR("Software error: MATCH_* %d not parsed in function '%s'", cond->match, __FUNCTION__);
2063                                 break;
2064
2065                                 match_integer:
2066                                 if (cond->value_type == VALUE_TYPE_INTEGER) {
2067                                         if (integer != cond->integer_value)
2068                                                 break;
2069                                         istrue = 1;
2070                                         break;
2071                                 }
2072                                 if (cond->value_type == VALUE_TYPE_INTEGER_RANGE) {
2073                                         /* check if negative range (2100 - 700 o'clock) */
2074                                         if (cond->integer_value > cond->integer_value_to) {
2075                                                 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2076                                                         istrue = 1;
2077                                                 break;
2078                                         }
2079                                         /* range is positive */
2080                                         if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2081                                                 istrue = 1;
2082                                         break;
2083                                 }
2084                                 break;
2085
2086                                 match_string:
2087                                 if (strlen(cond->string_value) != strlen(string))
2088                                         break;
2089                                 /* fall through */
2090                                 match_string_prefix:
2091                                 comp_len = strlen(cond->string_value); /* because we must reach value's length */
2092                                 /* we must have greater or equal length to values */
2093                                 if ((unsigned long)strlen(string) < comp_len) {
2094                                         /* special case for unfinished dialing */
2095                                         if (cond->match == MATCH_DIALING) {
2096                                                 couldbetrue = 1; /* could match */
2097                                                 comp_len = strlen(string);
2098                                         } else {
2099                                                 break;
2100                                         }
2101                                 }
2102                                 /* on single string match */
2103                                 if (cond->value_type == VALUE_TYPE_STRING) {
2104                                         if (!strncmp(string, cond->string_value, comp_len)) {
2105                                                 istrue = 1;
2106                                                 /* must be set for changing 'e_extdialing' */
2107                                                 if (cond->match == MATCH_DIALING)
2108                                                         dialing_required = comp_len;
2109                                                 break;
2110                                         }
2111                                         break;
2112                                 }
2113                                 /* on range match */
2114                                 if (cond->value_type == VALUE_TYPE_STRING_RANGE) {
2115                                         /* check if negative range ("55"-"22") */
2116                                         if (cond->comp_string > 0) {
2117                                                 if (strncmp(string, cond->string_value, comp_len) >= 0) {
2118                                                         istrue = 1;
2119                                                         /* must be set for changing 'e_extdialing' */
2120                                                         if (cond->match == MATCH_DIALING)
2121                                                                 dialing_required = comp_len;
2122                                                         break;
2123                                                 }
2124                                                 if (strncmp(string, cond->string_value_to, comp_len) <= 0) {
2125                                                         /* must be set for changing 'e_extdialing' */
2126                                                         istrue = 1;
2127                                                         if (cond->match == MATCH_DIALING)
2128                                                                 dialing_required = comp_len;
2129                                                         break;
2130                                                 }
2131                                                 break;
2132                                         }
2133                                         /* range is positive */
2134                                         if (strncmp(string, cond->string_value, comp_len) < 0)
2135                                                 break;
2136                                         if (strncmp(string, cond->string_value_to, comp_len) > 0)
2137                                                 break;
2138                                         istrue = 1;
2139                                         if (cond->match == MATCH_DIALING)
2140                                                 dialing_required = comp_len;
2141                                         break;
2142                                 }
2143                                 break;
2144                         }
2145
2146                         /* set current condition */
2147                         if (istrue && !couldbetrue)
2148                                 condition = 1; /* element matches, so condition matches */
2149                         if (istrue && couldbetrue && !condition)
2150                                 condition = 2; /* element could match and other elements don't match, so condition could match */
2151
2152                         /* if not matching or could match */
2153                         if (condition != 1) {
2154                                 /* if we have more values to check */
2155                                 if (cond->value_extension && cond->next) {
2156                                         cond = cond->next;
2157                                         goto checkextension;
2158                                 }
2159                                 match = condition;
2160                                 break;
2161                         }
2162                         
2163                         /* skip exteded values, beacuse we already have one matching value */
2164                         while(cond->value_extension && cond->next)
2165                                 cond = cond->next;
2166
2167                         cond = cond->next;
2168                 }
2169                 if (timeout>now_d && match==1) /* the matching rule with timeout in the future */
2170                 if (e_match_timeout<1 || timeout<e_match_timeout) { /* first timeout or lower */
2171                         /* set timeout in the furture */
2172                         e_match_timeout = timeout;
2173                         e_match_to_action = rule->action_first;
2174                         e_match_to_extdialing = e_dialinginfo.id + dialing_required;
2175                         match = 0; /* matches in the future */
2176                 }
2177                 if (match == 1) {
2178                         /* matching, we return first action */
2179                         action = rule->action_first;
2180                         e_match_timeout = 0; /* no timeout */
2181                         e_match_to_action = NULL;
2182                         e_extdialing = e_dialinginfo.id + dialing_required;
2183                         break;
2184                 }
2185                 if (match == 2) {
2186                         /* rule could match if more is dialed */
2187                         couldmatch = 1;
2188                 }
2189                 rule = rule->next;
2190         }
2191         return(action);
2192 }
2193
2194 /*
2195  * parses the current action's parameters and return them
2196  */
2197 struct route_param *EndpointAppPBX::routeparam(struct route_action *action, unsigned long long id)
2198 {
2199         struct route_param *param = action->param_first;
2200
2201         while(param) {
2202                 if (param->id == id)
2203                         break;
2204                 param = param->next;
2205         }
2206         return(param);
2207 }
2208
2209
2210 /*
2211  * internal rules that are not defined by route.conf
2212  */
2213 struct route_action action_password = {
2214         NULL,
2215         NULL,
2216         ACTION_PASSWORD,
2217         0,
2218         0,
2219 };
2220
2221 struct route_action action_password_write = {
2222         NULL,
2223         NULL,
2224         ACTION_PASSWORD_WRITE,
2225         0,
2226         0,
2227 };
2228
2229 struct route_action action_external = {
2230         NULL,
2231         NULL,
2232         ACTION_EXTERNAL,
2233         0,
2234         0,
2235 };
2236
2237 struct route_action action_internal = {
2238         NULL,
2239         NULL,
2240         ACTION_INTERNAL,
2241         0,
2242         0,
2243 };
2244
2245 struct route_action action_remote = {
2246         NULL,
2247         NULL,
2248         ACTION_REMOTE,
2249         0,
2250         0,
2251 };
2252
2253 struct route_action action_vbox = {
2254         NULL,
2255         NULL,
2256         ACTION_VBOX_RECORD,
2257         0,
2258         0,
2259 };
2260
2261 struct route_action action_partyline = {
2262         NULL,
2263         NULL,
2264         ACTION_PARTYLINE,
2265         0,
2266         0,
2267 };
2268
2269 struct route_action action_disconnect = {
2270         NULL,
2271         NULL,
2272         ACTION_DISCONNECT,
2273         0,
2274         0,
2275 };
2276
2277