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