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