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