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