Minor fix for GSM HR codec negotiation: Add missing 'break'.
[lcr.git] / action.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** all actions (and hangup) are processed here                               **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 extern char **environ;
15
16
17 /*
18  * process init 'internal' / 'external' / 'vbox-record' / 'partyline'...
19  */
20 void EndpointAppPBX::action_init_call(void)
21 {
22         class Join              *join;
23
24         /* a created call, this should never happen */
25         if (ea_endpoint->ep_join_id) {
26                 if (options.deb & DEBUG_EPOINT)
27                         PERROR("EPOINT(%d): We already have a call instance, this should never happen!\n", ea_endpoint->ep_serial);
28                 return;
29         }
30
31         /* create join */
32         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): Creating new join instance.\n", ea_endpoint->ep_serial);
33         join = new JoinPBX(ea_endpoint);
34         if (!join)
35                 FATAL("No memoy for Join instance.\n");
36         ea_endpoint->ep_join_id = join->j_serial;
37         return;
38 }
39
40 /*
41  * process dialing 'internal'
42  */
43 void EndpointAppPBX::action_dialing_internal(void)
44 {
45         struct capa_info        capainfo;
46         struct caller_info      callerinfo;
47         struct redir_info       redirinfo;
48         struct rtp_info         rtpinfo;
49         struct dialing_info     dialinginfo;
50         struct port_list        *portlist = ea_endpoint->ep_portlist;
51         struct lcr_msg          *message;
52         struct extension        ext;
53         struct route_param      *rparam;
54
55         /* send proceeding, because number is complete */
56         set_tone(portlist, "proceeding");
57         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_PROCEEDING);
58         message_put(message);
59         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
60         new_state(EPOINT_STATE_IN_PROCEEDING);
61
62         /* create bearer/caller/dialinginfo */
63         memcpy(&capainfo, &e_capainfo, sizeof(capainfo));
64         memcpy(&callerinfo, &e_callerinfo, sizeof(callerinfo));
65         memcpy(&redirinfo, &e_redirinfo, sizeof(redirinfo));
66         memcpy(&rtpinfo, &e_rtpinfo, sizeof(rtpinfo));
67         memset(&dialinginfo, 0, sizeof(dialinginfo));
68         dialinginfo.itype = INFO_ITYPE_ISDN_EXTENSION;
69         SCPY(dialinginfo.id, e_dialinginfo.id);
70
71         /* process extension */
72         if ((rparam = routeparam(e_action, PARAM_EXTENSION)))
73                 SCPY(dialinginfo.id, rparam->string_value);
74
75         /* process number type */
76         if ((rparam = routeparam(e_action, PARAM_TYPE)))
77                 dialinginfo.ntype = rparam->integer_value;
78
79         /* process service */
80         if ((rparam = routeparam(e_action, PARAM_CAPA))) {
81                 capainfo.bearer_capa = rparam->integer_value;
82                 if (capainfo.bearer_capa != INFO_BC_SPEECH
83                  && capainfo.bearer_capa != INFO_BC_AUDIO) {
84                         capainfo.bearer_mode = INFO_BMODE_PACKET;
85                 }
86                 capainfo.bearer_info1 = INFO_INFO1_NONE;
87                 capainfo.hlc = INFO_HLC_NONE;
88                 capainfo.exthlc = INFO_HLC_NONE;
89         }
90         if ((rparam = routeparam(e_action, PARAM_BMODE))) {
91                 capainfo.bearer_mode = rparam->integer_value;
92         }
93         if ((rparam = routeparam(e_action, PARAM_INFO1))) {
94                 capainfo.bearer_info1 = rparam->integer_value;
95         }
96         if ((rparam = routeparam(e_action, PARAM_HLC))) {
97                 capainfo.hlc = rparam->integer_value;
98         }
99         if ((rparam = routeparam(e_action, PARAM_EXTHLC))) {
100                 capainfo.exthlc = rparam->integer_value;
101         }
102
103         /* process presentation */
104         if ((rparam = routeparam(e_action, PARAM_PRESENT))) {
105                 callerinfo.present = (rparam->integer_value)?INFO_PRESENT_ALLOWED:INFO_PRESENT_RESTRICTED;
106         }
107
108         /* check if extension exists AND only if not multiple extensions */
109         if (!strchr(dialinginfo.id,',') && !read_extension(&ext, dialinginfo.id)) {
110                 trace_header("ACTION extension (extension doesn't exist)", DIRECTION_NONE);
111                 add_trace("extension", NULL, dialinginfo.id);
112                 end_trace();
113                 release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0, 0, 0);
114                 new_state(EPOINT_STATE_OUT_DISCONNECT);
115                 message_disconnect_port(portlist, CAUSE_UNALLOCATED, LOCATION_PRIVATE_LOCAL, "", NULL);
116                 set_tone(portlist, "cause_86");
117                 return;
118         }
119         /* check if internal calls are denied */
120         if (e_ext.rights < 1) {
121                 trace_header("ACTION extension (dialing to extension denied)", DIRECTION_NONE);
122                 add_trace("extension", NULL, dialinginfo.id);
123                 end_trace();
124                 new_state(EPOINT_STATE_OUT_DISCONNECT);
125                 release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0, 0, 0);
126                 message_disconnect_port(portlist, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, "", NULL);
127                 set_tone(portlist, "cause_81");
128                 return;
129         }
130
131         /* add or update internal call */
132         trace_header("ACTION extension (calling)", DIRECTION_NONE);
133         add_trace("extension", NULL, dialinginfo.id);
134         end_trace();
135         message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_SETUP);
136         memcpy(&message->param.setup.dialinginfo, &dialinginfo, sizeof(struct dialing_info));
137         memcpy(&message->param.setup.redirinfo, &redirinfo, sizeof(struct redir_info));
138         memcpy(&message->param.setup.callerinfo, &callerinfo, sizeof(struct caller_info));
139         memcpy(&message->param.setup.capainfo, &capainfo, sizeof(struct capa_info));
140         memcpy(&message->param.setup.rtpinfo, &rtpinfo, sizeof(struct rtp_info));
141         message_put(message);
142 }
143
144 /* process dialing external
145  */
146 void EndpointAppPBX::action_dialing_external(void)
147 {
148         struct capa_info capainfo;
149         struct caller_info callerinfo;
150         struct redir_info redirinfo;
151         struct rtp_info rtpinfo;
152         struct dialing_info dialinginfo;
153         char *p;
154         struct port_list *portlist = ea_endpoint->ep_portlist;
155         struct lcr_msg *message;
156         struct route_param *rparam;
157
158         /* special processing of delete characters '*' and '#' */
159         if (e_ext.delete_ext) {
160                 /* dialing a # causes a clearing of complete number */
161                 if (strchr(e_extdialing, '#')) {
162                         e_extdialing[0] = '\0';
163                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): '#' detected: terminal '%s' selected caller id '%s' and continues dialing: '%s'\n", ea_endpoint->ep_serial, e_ext.number, e_callerinfo.id, e_extdialing);
164                 }
165                 /* eliminate digits before '*', which is a delete digit
166                  */
167                 if (strchr(e_extdialing, '*')) {
168                         /* remove digits */
169                         while((p=strchr(e_extdialing, '*'))) {
170                                 if (p > e_extdialing) { /* only if there is a digit in front */
171                                         UCPY(p-1, p);
172                                         p--;
173                                 }
174                                 UCPY(p, p+1); /* remove '*' */
175                         }
176                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s deleted digits and got new string: %s\n", ea_endpoint->ep_serial, e_ext.number, e_extdialing);
177                 }
178         }
179
180         /* create bearer/caller/dialinginfo */
181         memcpy(&capainfo, &e_capainfo, sizeof(capainfo));
182         memcpy(&callerinfo, &e_callerinfo, sizeof(callerinfo));
183         memcpy(&redirinfo, &e_redirinfo, sizeof(redirinfo));
184         memcpy(&rtpinfo, &e_rtpinfo, sizeof(rtpinfo));
185         memset(&dialinginfo, 0, sizeof(dialinginfo));
186         dialinginfo.itype = INFO_ITYPE_ISDN;
187 //      dialinginfo.sending_complete = 0;
188         SCPY(dialinginfo.id, e_extdialing);
189
190         /* process prefix */
191         if ((rparam = routeparam(e_action, PARAM_PREFIX)))
192                 SPRINT(dialinginfo.id, "%s%s", rparam->string_value, e_extdialing);
193
194         if ((rparam = routeparam(e_action, PARAM_CONTEXT)))
195                 SCPY(dialinginfo.context, rparam->string_value);
196
197         /* process keypad */
198         if ((rparam = routeparam(e_action, PARAM_KEYPAD))) {
199                 SCPY(dialinginfo.keypad, dialinginfo.id);
200                 dialinginfo.id[0] = '\0';
201         }
202
203         /* process number complete */
204         if ((rparam = routeparam(e_action, PARAM_COMPLETE)))
205                 dialinginfo.sending_complete = 1;
206
207         /* process number type */
208         if ((rparam = routeparam(e_action, PARAM_TYPE)))
209                 dialinginfo.ntype = rparam->integer_value;
210
211         /* process service */
212         if ((rparam = routeparam(e_action, PARAM_CAPA))) {
213                 capainfo.bearer_capa = rparam->integer_value;
214                 if (capainfo.bearer_capa != INFO_BC_SPEECH
215                  && capainfo.bearer_capa != INFO_BC_AUDIO) {
216                         capainfo.bearer_mode = INFO_BMODE_PACKET;
217                 }
218                 capainfo.bearer_info1 = INFO_INFO1_NONE;
219                 capainfo.hlc = INFO_HLC_NONE;
220                 capainfo.exthlc = INFO_HLC_NONE;
221         }
222         if ((rparam = routeparam(e_action, PARAM_BMODE))) {
223                 capainfo.bearer_mode = rparam->integer_value;
224         }
225         if ((rparam = routeparam(e_action, PARAM_INFO1))) {
226                 capainfo.bearer_info1 = rparam->integer_value;
227         }
228         if ((rparam = routeparam(e_action, PARAM_HLC))) {
229                 capainfo.hlc = rparam->integer_value;
230         }
231         if ((rparam = routeparam(e_action, PARAM_EXTHLC))) {
232                 capainfo.exthlc = rparam->integer_value;
233         }
234
235
236         /* process callerid */
237         if ((rparam = routeparam(e_action, PARAM_CALLERID))) {
238                 SCPY(callerinfo.id, rparam->string_value);
239         }
240         if ((rparam = routeparam(e_action, PARAM_CALLERIDTYPE))) {
241                 callerinfo.ntype = rparam->integer_value;
242         }
243
244         /* process presentation */
245         if ((rparam = routeparam(e_action, PARAM_PRESENT))) {
246                 callerinfo.present = (rparam->integer_value)?INFO_PRESENT_ALLOWED:INFO_PRESENT_RESTRICTED;
247         }
248
249         /* process interfaces */
250         if ((rparam = routeparam(e_action, PARAM_INTERFACES)))
251                 SCPY(dialinginfo.interfaces, rparam->string_value);
252
253         /* check if local calls are denied */
254         if (e_ext.rights < 2) {
255                 trace_header("ACTION extern (calling denied)", DIRECTION_NONE);
256                 end_trace();
257                 release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, 0, 0, 0);
258                 set_tone(portlist, "cause_82");
259                 denied:
260                 message_disconnect_port(portlist, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, "", NULL);
261                 new_state(EPOINT_STATE_OUT_DISCONNECT);
262                 return;
263         }
264
265         if (!strncmp(dialinginfo.id, options.national, strlen(options.national))
266          || dialinginfo.ntype == INFO_NTYPE_NATIONAL
267          || dialinginfo.ntype == INFO_NTYPE_INTERNATIONAL) {
268                 /* check if national calls are denied */
269                 if (e_ext.rights < 3) {
270                         trace_header("ACTION extern (national calls denied)", DIRECTION_NONE);
271                         end_trace();
272                         release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, 0, 0, 0);
273                         set_tone(portlist, "cause_83");
274                         goto denied;
275                 }
276         }
277
278         if (!strncmp(dialinginfo.id, options.international, strlen(options.international))
279          || dialinginfo.ntype == INFO_NTYPE_INTERNATIONAL) {
280                 /* check if international calls are denied */
281                 if (e_ext.rights < 4) {
282                         trace_header("ACTION extern (international calls denied)", DIRECTION_NONE);
283                         end_trace();
284                         release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, 0, 0, 0);
285                         set_tone(portlist, "cause_84");
286                         goto denied;
287                 }
288         }
289
290         /* add or update outgoing call */
291         trace_header("ACTION extern (calling)", DIRECTION_NONE);
292         add_trace("number", NULL, dialinginfo.id);
293         if (dialinginfo.sending_complete)
294         add_trace("number", "complete", "yes");
295         if (dialinginfo.interfaces[0])
296                 add_trace("interfaces", NULL, dialinginfo.interfaces);
297         end_trace();
298         message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_SETUP);
299         memcpy(&message->param.setup.dialinginfo, &dialinginfo, sizeof(struct dialing_info));
300         memcpy(&message->param.setup.redirinfo, &redirinfo, sizeof(struct redir_info));
301         memcpy(&message->param.setup.callerinfo, &callerinfo, sizeof(struct caller_info));
302         memcpy(&message->param.setup.capainfo, &capainfo, sizeof(struct capa_info));
303         memcpy(&message->param.setup.rtpinfo, &rtpinfo, sizeof(struct rtp_info));
304         message_put(message);
305 }
306
307
308 /*
309  * process dialing the "am" and record
310  */
311 void EndpointAppPBX::action_dialing_vbox_record(void)
312 {
313         struct dialing_info dialinginfo;
314         struct port_list *portlist = ea_endpoint->ep_portlist;
315         struct lcr_msg *message;
316         struct extension ext;
317         struct route_param *rparam;
318
319         portlist = ea_endpoint->ep_portlist;
320
321         /* check for given extension */
322         if (!(rparam = routeparam(e_action, PARAM_EXTENSION))) {
323                 trace_header("ACTION vbox-record (no extension given by parameter)", DIRECTION_NONE);
324                 end_trace();
325
326                 new_state(EPOINT_STATE_OUT_DISCONNECT);
327                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
328                 set_tone(portlist, "cause_3f");
329                 return;
330         }
331
332         /* check if extension exists */
333         if (!read_extension(&ext, rparam->string_value)) {
334                 trace_header("ACTION vbox-record (given extension does not exists)", DIRECTION_NONE);
335                 add_trace("extension", NULL, "%s", rparam->string_value);
336                 end_trace();
337                 new_state(EPOINT_STATE_OUT_DISCONNECT);
338                 message_disconnect_port(portlist, CAUSE_UNALLOCATED, LOCATION_PRIVATE_LOCAL, "", NULL);
339                 set_tone(portlist, "cause_86");
340                 return;
341         }
342
343         /* check if internal calls are denied */
344         if (e_ext.rights < 1) {
345                 trace_header("ACTION vbox-record (internal calls are denied)", DIRECTION_NONE);
346                 end_trace();
347                 new_state(EPOINT_STATE_OUT_DISCONNECT);
348                 message_disconnect_port(portlist, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, "", NULL);
349                 set_tone(portlist, "cause_81");
350                 return;
351         }
352
353         set_tone(portlist, "proceeding");
354         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_PROCEEDING);
355         message_put(message);
356         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
357         new_state(EPOINT_STATE_IN_PROCEEDING);
358
359         memset(&dialinginfo, 0, sizeof(dialinginfo));
360         dialinginfo.itype = INFO_ITYPE_VBOX;
361         dialinginfo.sending_complete = 1;
362         SCPY(dialinginfo.id, rparam->string_value);
363
364         /* append special announcement (if given) */
365         if ((rparam = routeparam(e_action, PARAM_ANNOUNCEMENT)))
366         if (rparam->string_value[0]) {
367                 SCAT(dialinginfo.id, ",");
368                 SCAT(dialinginfo.id, rparam->string_value);
369         }
370
371         /* add or update internal call */
372         trace_header("ACTION vbox-record (calling)", DIRECTION_NONE);
373         add_trace("extension", NULL, "%s", dialinginfo.id);
374         end_trace();
375         message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_SETUP);
376         memcpy(&message->param.setup.dialinginfo, &dialinginfo, sizeof(struct dialing_info));
377         memcpy(&message->param.setup.redirinfo, &e_redirinfo, sizeof(struct redir_info));
378         memcpy(&message->param.setup.callerinfo, &e_callerinfo, sizeof(struct caller_info));
379         memcpy(&message->param.setup.capainfo, &e_capainfo, sizeof(struct capa_info));
380         message_put(message);
381 }
382
383
384 /*
385  * process partyline
386  */
387 void EndpointAppPBX::action_init_partyline(void)
388 {
389         class Join *join;
390         class JoinPBX *joinpbx;
391         struct port_list *portlist = ea_endpoint->ep_portlist;
392         struct lcr_msg *message;
393         struct route_param *rparam;
394         int partyline, jingle = 0;
395         struct join_relation *relation;
396
397         portlist = ea_endpoint->ep_portlist;
398
399         /* check for given extension */
400         if (!(rparam = routeparam(e_action, PARAM_ROOM))) {
401                 trace_header("ACTION partyline (no room parameter)", DIRECTION_NONE);
402                 end_trace();
403                 noroom:
404                 new_state(EPOINT_STATE_OUT_DISCONNECT);
405                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
406                 set_tone(portlist, "cause_3f");
407                 return;
408         }
409         if (rparam->integer_value <= 0) {
410                 trace_header("ACTION partyline (illegal room parameter)", DIRECTION_NONE);
411                 add_trace("room", NULL, "%d", rparam->integer_value);
412                 end_trace();
413                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): invalid value for 'room'.\n", ea_endpoint->ep_serial);
414                 goto noroom;
415         }
416         partyline = rparam->integer_value;
417         if ((rparam = routeparam(e_action, PARAM_JINGLE)))
418                 jingle = 1;
419
420         /* don't create join if partyline exists */
421         join = join_first;
422         while(join) {
423                 if (join->j_type == JOIN_TYPE_PBX) {
424                         joinpbx = (class JoinPBX *)join;
425                         if (joinpbx->j_partyline == partyline)
426                                 break;
427                 }
428                 join = join->next;
429         }
430         if (!join) {
431                 /* create join */
432                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): Creating new join instance.\n", ea_endpoint->ep_serial);
433                 if (!(join = new JoinPBX(ea_endpoint)))
434                         FATAL("No memory for join object\n");
435         } else {
436 //NOTE: joinpbx must be set here
437                 /* add relation to existing join */
438                 if (!(relation=joinpbx->add_relation()))
439                         FATAL("No memory for join relation\n");
440                 relation->type = RELATION_TYPE_SETUP;
441                 relation->channel_state = 1;
442                 relation->rx_state = NOTIFY_STATE_ACTIVE;
443                 relation->tx_state = NOTIFY_STATE_ACTIVE;
444                 relation->epoint_id = ea_endpoint->ep_serial;
445
446         }
447         ea_endpoint->ep_join_id = join->j_serial;
448
449         set_tone(portlist, "proceeding");
450         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_PROCEEDING);
451         message_put(message);
452         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
453         new_state(EPOINT_STATE_IN_PROCEEDING);
454
455         /* send setup to join */
456         trace_header("ACTION partyline (calling)", DIRECTION_NONE);
457         add_trace("room", NULL, "%d", partyline);
458         add_trace("jingle", NULL, (jingle)?"on":"off");
459         end_trace();
460         message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_SETUP);
461         message->param.setup.partyline = partyline;
462         message->param.setup.partyline_jingle = jingle;
463         memcpy(&message->param.setup.dialinginfo, &e_dialinginfo, sizeof(struct dialing_info));
464         memcpy(&message->param.setup.redirinfo, &e_redirinfo, sizeof(struct redir_info));
465         memcpy(&message->param.setup.callerinfo, &e_callerinfo, sizeof(struct caller_info));
466         memcpy(&message->param.setup.capainfo, &e_capainfo, sizeof(struct capa_info));
467         message_put(message);
468 }
469
470
471 /*
472  * process hangup of all calls
473  */
474 void EndpointAppPBX::action_hangup_call(void)
475 {
476         trace_header("ACTION hangup", DIRECTION_NONE);
477         end_trace();
478 }
479
480
481 /*
482  * process dialing 'login'
483  */
484 void EndpointAppPBX::action_dialing_login(void)
485 {
486         struct port_list *portlist = ea_endpoint->ep_portlist;
487         struct lcr_msg *message;
488         char *extension;
489         struct route_param *rparam;
490
491         /* extension parameter */
492         if ((rparam = routeparam(e_action, PARAM_EXTENSION))) {
493                 /* extension is given by parameter */
494                 extension = rparam->string_value;
495                 if (extension[0] == '\0')
496                         return;
497                 if (!read_extension(&e_ext, extension)) {
498                         trace_header("ACTION login (extension doesn't exist)", DIRECTION_NONE);
499                         add_trace("extension", NULL, "%s", extension);
500                         end_trace();
501                         /* extension doesn't exist */
502                         new_state(EPOINT_STATE_OUT_DISCONNECT);
503                         message_disconnect_port(portlist, CAUSE_UNALLOCATED, LOCATION_PRIVATE_LOCAL, "", NULL);
504                         set_tone(portlist, "cause_86");
505                         return;
506                 }
507         } else {
508                 /* extension must be given by dialstring */
509                 extension = e_extdialing;
510                 if (extension[0] == '\0')
511                         return;
512                 if (!read_extension(&e_ext, extension)) {
513                         trace_header("ACTION login (extension incomplete or does not exist)", DIRECTION_NONE);
514                         add_trace("extension", NULL, "%s", extension);
515                         end_trace();
516                         return;
517                 }
518         }
519
520         /* we changed our extension */
521         SCPY(e_ext.number, extension);
522         new_state(EPOINT_STATE_CONNECT);
523         e_dtmf = 1;
524         e_connectedmode = 1;
525
526         /* send connect with extension's caller id (COLP) */
527         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
528         SCPY(message->param.connectinfo.id, e_ext.callerid);
529         message->param.connectinfo.ntype = e_ext.callerid_type;
530         if (e_ext.callerid_present==INFO_PRESENT_ALLOWED && e_callerinfo.present==INFO_PRESENT_RESTRICTED)
531                 message->param.connectinfo.present = INFO_PRESENT_RESTRICTED;
532         else    message->param.connectinfo.present = e_ext.callerid_present;
533         /* handle restricted caller ids */
534         apply_callerid_restriction(&e_ext, message->param.connectinfo.id, &message->param.connectinfo.ntype, &message->param.connectinfo.present, &message->param.connectinfo.screen, message->param.connectinfo.extension, message->param.connectinfo.name);
535         /* display callerid if desired for extension */
536         SCPY(message->param.connectinfo.display, apply_callerid_display(message->param.connectinfo.id, message->param.connectinfo.itype, message->param.connectinfo.ntype, message->param.connectinfo.present, message->param.connectinfo.screen, message->param.connectinfo.extension, message->param.connectinfo.name));
537         message->param.connectinfo.ntype = e_ext.callerid_type;
538         message_put(message);
539         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
540
541         /* set our caller id */
542         SCPY(e_callerinfo.id, e_ext.callerid);
543         e_callerinfo.ntype = e_ext.callerid_type;
544         e_callerinfo.present = e_ext.callerid_present;
545
546         /* enable connectedmode */
547         e_connectedmode = 1;
548         e_dtmf = 1;
549
550         if (!(rparam = routeparam(e_action, PARAM_NOPASSWORD))) {
551                 /* make call state to enter password */
552                 trace_header("ACTION login (ask for password)", DIRECTION_NONE);
553                 add_trace("extension", NULL, "%s", e_ext.number);
554                 end_trace();
555                 new_state(EPOINT_STATE_IN_OVERLAP);
556                 e_ruleset = NULL;
557                 e_rule = NULL;
558                 e_action = &action_password;
559                 unsched_timer(&e_match_timeout);
560                 e_match_to_action = NULL;
561                 e_dialinginfo.id[0] = '\0';
562                 e_extdialing = strchr(e_dialinginfo.id, '\0');
563
564                 /* set timeout */
565                 schedule_timer(&e_password_timeout, 20, 0);
566
567                 /* do dialing */
568                 process_dialing(0);
569         } else {
570                 /* make call state  */
571                 new_state(EPOINT_STATE_IN_OVERLAP);
572                 e_ruleset = ruleset_main;
573                 if (e_ruleset)
574                         e_rule = e_ruleset->rule_first;
575                 e_action = NULL;
576                 e_dialinginfo.id[0] = '\0';
577                 e_extdialing = e_dialinginfo.id;
578                 set_tone(portlist, "dialpbx");
579         }
580 }
581
582
583 /*
584  * process init 'change_callerid'
585  */
586 void EndpointAppPBX::action_init_change_callerid(void)
587 {
588         struct port_list *portlist = ea_endpoint->ep_portlist;
589
590         if (!e_ext.change_callerid) {
591                 /* service not available */
592                 trace_header("ACTION change-callerid (denied for this caller)", DIRECTION_NONE);
593                 end_trace();
594                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
595                 new_state(EPOINT_STATE_OUT_DISCONNECT);
596                 set_tone(portlist,"cause_87");
597                 return;
598         }
599 }
600
601 /* process dialing callerid
602  */
603 void EndpointAppPBX::_action_callerid_calleridnext(int next)
604 {
605         struct port_list *portlist = ea_endpoint->ep_portlist;
606         struct route_param *rparam;
607         char buffer[64], *callerid;
608         char old_id[64] = "", new_id[64] = "";
609         int old_type=0, new_type=0, old_present=0, new_present=0;
610
611         if ((rparam = routeparam(e_action, PARAM_CALLERID))) {
612                 /* the caller ID is given by parameter */
613                 callerid = rparam->string_value;
614         } else {
615                 /* caller ID is dialed */
616                 if (!strchr(e_extdialing, '#')) {
617                         /* no complete ID yet */
618                         return;
619                 }
620                 *strchr(e_extdialing, '#') = '\0';
621                 callerid = e_extdialing;
622         }
623
624         /* given callerid type */
625         if ((rparam = routeparam(e_action, PARAM_CALLERIDTYPE)))
626                 switch(rparam->integer_value) {
627                         case INFO_NTYPE_SUBSCRIBER:
628                         SPRINT(buffer, "s%s", callerid);
629                         callerid = buffer;
630                         break;
631                         case INFO_NTYPE_NATIONAL:
632                         SPRINT(buffer, "n%s", callerid);
633                         callerid = buffer;
634                         break;
635                         case INFO_NTYPE_INTERNATIONAL:
636                         SPRINT(buffer, "i%s", callerid);
637                         callerid = buffer;
638                         break;
639                         default:
640                         SPRINT(buffer, "%s", callerid);
641                         callerid = buffer;
642                         break;
643                 }
644
645         /* caller id complete, dialing with new caller id */
646         /* write new parameters */
647         if (read_extension(&e_ext, e_ext.number)) {
648                 old_present = (!next)?e_ext.callerid_present:e_ext.id_next_call_present;
649                 old_type = (!next)?e_ext.callerid_type:e_ext.id_next_call_type;
650                 SCPY(old_id, (!next)?e_ext.callerid:e_ext.id_next_call);
651                 if (callerid[0] == '\0') {
652                         /* no caller id */
653                         (!next)?e_ext.callerid_present:e_ext.id_next_call_present = INFO_PRESENT_RESTRICTED;
654                 } else {
655                         /* new caller id */
656                         (!next)?e_ext.callerid_present:e_ext.id_next_call_present = INFO_PRESENT_ALLOWED;
657                         if ((rparam = routeparam(e_action, PARAM_PRESENT))) if (rparam->integer_value == 0)
658                                 (!next)?e_ext.callerid_present:e_ext.id_next_call_present = INFO_PRESENT_RESTRICTED;
659                         if (e_ext.callerid_type == INFO_NTYPE_UNKNOWN) /* if callerid is unknown, the given id is not nationalized */ {
660                                 SCPY((!next)?e_ext.callerid:e_ext.id_next_call, callerid);
661                                 (!next)?e_ext.callerid_type:e_ext.id_next_call_type = INFO_NTYPE_UNKNOWN;
662                         } else {
663                                 SCPY((!next)?e_ext.callerid:e_ext.id_next_call, nationalize_callerinfo(callerid,&((!next)?e_ext.callerid_type:e_ext.id_next_call_type), options.national, options.international));
664                         }
665                         if (!next) e_ext.id_next_call_type = -1;
666                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): nationalized callerid: '%s' type=%d\n", ea_endpoint->ep_serial, (!next)?e_ext.callerid:e_ext.id_next_call, (!next)?e_ext.callerid_type:e_ext.id_next_call_type);
667                 }
668                 new_present = (!next)?e_ext.callerid_present:e_ext.id_next_call_present;
669                 new_type = (!next)?e_ext.callerid_type:e_ext.id_next_call_type;
670                 SCPY(new_id, (!next)?e_ext.callerid:e_ext.id_next_call);
671                 write_extension(&e_ext, e_ext.number);
672         }
673
674         /* function activated */
675         if (next)
676                 trace_header("ACTION change-callerid (only next call)", DIRECTION_NONE);
677         else
678                 trace_header("ACTION change-callerid (all future calls)", DIRECTION_NONE);
679         add_trace("old", "caller id", "%s", numberrize_callerinfo(old_id, old_type, options.national, options.international));
680         add_trace("old", "present", "%s", (old_present==INFO_PRESENT_RESTRICTED)?"restricted":"allowed");
681         add_trace("new", "caller id", "%s", numberrize_callerinfo(new_id, new_type, options.national, options.international));
682         add_trace("new", "present", "%s", (new_present==INFO_PRESENT_RESTRICTED)?"restricted":"allowed");
683         end_trace();
684         message_disconnect_port(portlist, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, "", NULL);
685         new_state(EPOINT_STATE_OUT_DISCONNECT);
686         set_tone(portlist,"activated");
687 }
688
689 /* process dialing callerid for all call
690  */
691 void EndpointAppPBX::action_dialing_callerid(void)
692 {
693         _action_callerid_calleridnext(0);
694 }
695
696 /* process dialing callerid for next call
697  */
698 void EndpointAppPBX::action_dialing_calleridnext(void)
699 {
700         _action_callerid_calleridnext(1);
701 }
702
703
704 /*
705  * process init 'change_forward'
706  */
707 void EndpointAppPBX::action_init_change_forward(void)
708 {
709         struct port_list *portlist = ea_endpoint->ep_portlist;
710
711         if (!e_ext.change_forward) {
712                 trace_header("ACTION change-forward (denied for this caller)", DIRECTION_NONE);
713                 end_trace();
714                 /* service not available */             
715                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
716                 new_state(EPOINT_STATE_OUT_DISCONNECT);
717                 set_tone(portlist,"cause_87");
718                 return;
719         }
720 }
721
722 /* process dialing forwarding
723  */
724 void EndpointAppPBX::action_dialing_forward(void)
725 {
726         struct port_list *portlist = ea_endpoint->ep_portlist;
727         int diversion = INFO_DIVERSION_CFU;
728         char *dest = e_extdialing;
729         struct route_param *rparam;
730
731         /* if diversion type is given */
732         if ((rparam = routeparam(e_action, PARAM_DIVERSION)))
733                 diversion = rparam->integer_value;
734
735         if ((rparam = routeparam(e_action, PARAM_DEST))) {
736                 /* if destination is given */
737                 dest = rparam->string_value;
738         } else {
739                 if (!strchr(e_extdialing, '#'))
740                         return;
741                 *strchr(e_extdialing, '#') = '\0';
742                 dest = e_extdialing;
743         }
744
745         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: storing forwarding to '%s'.\n", ea_endpoint->ep_serial, e_ext.number, dest);
746         if (read_extension(&e_ext, e_ext.number)) {
747                 switch(diversion) {
748                         case INFO_DIVERSION_CFU:
749                         trace_header("ACTION change-forward (new CFU=unconditional)", DIRECTION_NONE);
750                         add_trace("destin'", NULL, "%s", dest);
751                         end_trace();
752                         SCPY(e_ext.cfu, dest);
753                         break;
754                         case INFO_DIVERSION_CFB:
755                         trace_header("ACTION change-forward (new CFB=busy)", DIRECTION_NONE);
756                         add_trace("destin'", NULL, "%s", dest);
757                         end_trace();
758                         SCPY(e_ext.cfb, dest);
759                         break;
760                         case INFO_DIVERSION_CFNR:
761                         if ((rparam = routeparam(e_action, PARAM_DELAY)))
762                                 e_ext.cfnr_delay = rparam->integer_value;
763                         trace_header("ACTION change-forward (new CFNR=no response)", DIRECTION_NONE);
764                         add_trace("destin'", NULL, "%s", dest);
765                         add_trace("delay", NULL, "%s", e_ext.cfnr_delay);
766                         end_trace();
767                         SCPY(e_ext.cfnr, dest);
768                         break;
769                         case INFO_DIVERSION_CFP:
770                         trace_header("ACTION change-forward (new CFP=parallel)", DIRECTION_NONE);
771                         add_trace("destin'", NULL, "%s", dest);
772                         end_trace();
773                         SCPY(e_ext.cfp, dest);
774                         break;
775                 }
776                 write_extension(&e_ext, e_ext.number);
777         }
778         /* function (de)activated */
779         message_disconnect_port(portlist, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, "", NULL);
780         new_state(EPOINT_STATE_OUT_DISCONNECT);
781         if (dest[0])
782                 set_tone(portlist,"activated");
783         else
784                 set_tone(portlist,"deactivated");
785 }
786
787
788 /* process dialing redial
789 */
790 void EndpointAppPBX::action_init_redial_reply(void)
791 {
792         struct port_list *portlist = ea_endpoint->ep_portlist;
793
794         e_select = 0;
795         if (!e_ext.last_out[0]) {
796                 trace_header("ACTION redial/reply (no last number stored)", DIRECTION_NONE);
797                 end_trace();
798                 new_state(EPOINT_STATE_OUT_DISCONNECT);
799                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
800                 set_tone(portlist, "cause_3f");
801                 return;
802         }
803 }
804
805 /* process dialing redial
806 */
807 void EndpointAppPBX::_action_redial_reply(int in)
808 {
809         struct lcr_msg *message;
810         char *last;
811         struct route_param *rparam;
812
813         last = (in)?e_ext.last_in[0]:e_ext.last_out[0];
814
815         /* if no display is available */
816         if (!e_ext.display_menu)
817                 goto nodisplay;
818         if (ea_endpoint->ep_portlist->port_type!=PORT_TYPE_DSS1_NT_IN && ea_endpoint->ep_portlist->port_type!=PORT_TYPE_DSS1_NT_OUT)
819                 goto nodisplay;
820
821         /* if select is not given */
822         if (!(rparam = routeparam(e_action, PARAM_SELECT)))
823                 goto nodisplay;
824
825         /* scroll menu */
826         if (e_extdialing[0]=='*' || e_extdialing[0]=='1') {
827                 /* find prev entry */
828                 e_select--;
829                 if (e_select < 0)
830                         e_select = 0;
831
832         }
833         if (e_extdialing[0]=='#' || e_extdialing[0]=='3') {
834                 /* find next entry */
835                 e_select++;
836                 if (e_select >= MAX_REMEMBER) {
837                         e_select--;
838                 } else if (in) {
839                         if (e_ext.last_in[e_select][0] == '\0')
840                                 e_select--;
841                 } else
842                         if (e_ext.last_out[e_select][0] == '\0')
843                                 e_select--;
844
845         }
846
847         last = (in)?e_ext.last_in[e_select]:e_ext.last_out[e_select];
848         if (e_extdialing[0]=='0' || e_extdialing[0]=='2') {
849                 nodisplay:
850                 if (in)
851                         trace_header("ACTION reply (dialing)", DIRECTION_NONE);
852                 else
853                         trace_header("ACTION redial (dialing)", DIRECTION_NONE);
854                 add_trace("number", NULL, "%s", last);
855                 add_trace("last but", NULL, "%d", e_select);
856                 end_trace();
857                 SCPY(e_dialinginfo.id, last);
858                 e_extdialing = e_dialinginfo.id;
859                 e_action = NULL;
860                 process_dialing(0);
861                 return;
862         }
863         e_extdialing[0] = '\0';
864         
865         /* send display message to port */
866         message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_portlist->port_id, EPOINT_TO_PORT, MESSAGE_NOTIFY);
867         if (!strncmp(last, "extern:", 7))
868                 SPRINT(message->param.notifyinfo.display, "(%d) %s ext", e_select+1, last+7);
869         else
870         if (!strncmp(last, "intern:", 7))
871                 SPRINT(message->param.notifyinfo.display, "(%d) %s int", e_select+1, last+7);
872         else
873         if (!strncmp(last, "chan:", 4))
874                 SPRINT(message->param.notifyinfo.display, "(%d) %s chan", e_select+1, last+5);
875         else
876         if (!strncmp(last, "vbox:", 5))
877                 SPRINT(message->param.notifyinfo.display, "(%d) %s vbox", e_select+1, last+5);
878         else
879                 SPRINT(message->param.notifyinfo.display, "(%d) %s", e_select+1, (last[0])?last:"- empty -");
880         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s sending display:%s\n", ea_endpoint->ep_serial, e_ext.number, message->param.notifyinfo.display);
881         message_put(message);
882         logmessage(message->type, &message->param, ea_endpoint->ep_portlist->port_id, DIRECTION_OUT);
883 }
884
885 /* process dialing redial
886 */
887 void EndpointAppPBX::action_dialing_redial(void)
888 {
889         _action_redial_reply(0);
890 }
891
892 /* process dialing reply
893 */
894 void EndpointAppPBX::action_dialing_reply(void)
895 {
896         _action_redial_reply(1);
897 }
898
899
900 /* dialing powerdialing delay
901  */
902 void EndpointAppPBX::action_dialing_powerdial(void)
903 {
904         struct port_list *portlist = ea_endpoint->ep_portlist;
905         struct lcr_msg *message;
906         struct route_param *rparam;
907
908         /* power dialing only possible if we have a last dialed number */
909         if (!e_ext.last_out[0]) {
910                 trace_header("ACTION powerdial (no last number stored)", DIRECTION_NONE);
911                 end_trace();
912                 new_state(EPOINT_STATE_OUT_DISCONNECT);
913                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
914                 set_tone(portlist, "cause_3f");
915                 return;
916         }
917
918         /* limit */
919         if ((rparam = routeparam(e_action, PARAM_LIMIT))) {
920                 e_powerlimit = rparam->integer_value;
921         } else {
922                 e_powerlimit = 0;
923         }
924
925         /* delay */
926         if ((rparam = routeparam(e_action, PARAM_DELAY))) {
927                 e_powerdelay = rparam->integer_value;
928         } else {
929                 /* delay incomplete */
930                 if (!strchr(e_extdialing, '#'))
931                         return;
932                 *strchr(e_extdialing, '#') = '\0';
933                 e_powerdelay = e_extdialing[0]?atoi(e_extdialing): 0;
934         }
935
936         if (e_powerdelay < 1)
937                 e_powerdelay = 0.2;
938         trace_header("ACTION powerdial (dialing)", DIRECTION_NONE);
939         add_trace("number", NULL, "%s", e_ext.last_out[0]);
940         add_trace("delay", NULL, "%d", e_powerdelay);
941         end_trace();
942
943         /* send connect to avoid overlap timeout */
944 //      new_state(EPOINT_STATE_CONNECT); connect may prevent further dialing
945         if (e_ext.number[0])
946                 e_dtmf = 1;
947         memset(&e_connectinfo, 0, sizeof(e_connectinfo));
948         message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
949         message_put(message);
950         logmessage(message->type, &message->param, ea_endpoint->ep_portlist->port_id, DIRECTION_OUT);
951
952         /* do dialing */
953         SCPY(e_dialinginfo.id, e_ext.last_out[0]);
954         e_powerdial_on = 1; /* indicates the existence of powerdialing but no redial time given */
955         e_powercount = 0;
956         e_action = NULL;
957         process_dialing(0);
958 }
959
960
961 /* dialing callback
962  */
963 void EndpointAppPBX::action_dialing_callback(void)
964 {
965         struct port_list *portlist = ea_endpoint->ep_portlist;
966         struct route_param *rparam;
967         struct extension cbext;
968
969         portlist = ea_endpoint->ep_portlist;
970
971         /* check given extension */
972         if (!(rparam = routeparam(e_action, PARAM_EXTENSION))) {
973                 noextension:
974                 trace_header("ACTION callback (no extension defined)", DIRECTION_NONE);
975                 end_trace();
976                 disconnect:
977
978                 new_state(EPOINT_STATE_OUT_DISCONNECT);
979                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
980                 set_tone(portlist, "cause_3f");
981                 e_action = NULL;
982                 e_cbcaller[0] = e_cbdialing[0] = '\0';
983                 return;
984         }
985
986         /* if extension is given */
987         SCPY(e_cbcaller, rparam->string_value);
988         if (e_cbcaller[0] == '\0')
989                 goto noextension;
990
991         /* read callback extension */
992         memset(&cbext, 0, sizeof(cbext));
993         if (!read_extension(&cbext, e_cbcaller)) {
994                 trace_header("ACTION callback (extension doesn't exist)", DIRECTION_NONE);
995                 add_trace("extension", NULL, "%s", e_cbcaller);
996                 end_trace();
997                 goto disconnect;
998         }
999
1000         /* if password is not given */
1001         if (cbext.password[0] == '\0') {
1002                 trace_header("ACTION callback (no password set)", DIRECTION_NONE);
1003                 add_trace("extension", NULL, "%s", e_cbcaller);
1004                 end_trace();
1005                 goto disconnect;
1006         }
1007
1008         /* callback only possible if callerid exists OR it is given */
1009         if ((rparam = routeparam(e_action, PARAM_CALLTO)))
1010                 SCPY(e_cbto, rparam->string_value);
1011         if (e_cbto[0]) {
1012                 trace_header("ACTION callback (alternative caller id)", DIRECTION_NONE);
1013                 add_trace("extension", NULL, "%s", e_cbcaller);
1014                 add_trace("callerid", NULL, "%s", e_cbto);
1015                 end_trace();
1016                 SCPY(e_callerinfo.id, e_cbto);
1017                 e_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1018                 e_callerinfo.present = INFO_PRESENT_ALLOWED;
1019         }
1020         if (e_callerinfo.id[0]=='\0' || e_callerinfo.present==INFO_PRESENT_NOTAVAIL) {
1021                 trace_header("ACTION callback (no caller ID available)", DIRECTION_NONE);
1022                 add_trace("extension", NULL, "%s", e_cbcaller);
1023                 end_trace();
1024                 goto disconnect;
1025         }
1026         /* present id */
1027         e_callerinfo.present = INFO_PRESENT_ALLOWED;
1028
1029 }
1030
1031 /*
1032  * process hangup 'callback'
1033  */
1034 void EndpointAppPBX::action_hangup_callback(void)
1035 {
1036         struct route_param *rparam;
1037         int delay;
1038
1039         /* set delay */
1040         delay = 2; /* default value */
1041         if ((rparam = routeparam(e_action, PARAM_DELAY)))
1042         if (rparam->integer_value>0)
1043                 delay = rparam->integer_value;
1044
1045         /* dialing after callback */
1046         if ((rparam = routeparam(e_action, PARAM_PREFIX)))
1047                 SCPY(e_cbdialing, rparam->string_value);
1048         else
1049                 SCPY(e_cbdialing, e_extdialing);
1050
1051         trace_header("ACTION callback (dialing)", DIRECTION_NONE);
1052         add_trace("extension", NULL, "%s", e_cbcaller);
1053         add_trace("caller id", NULL, "%s", e_callerinfo.id);
1054         add_trace("delay", NULL, "%d", delay);
1055         add_trace("dialing", NULL, "%s", e_cbdialing);
1056         end_trace();
1057
1058         /* set time to callback */
1059         schedule_timer(&e_callback_timeout, delay, 0);
1060 }
1061
1062
1063 /*
1064  * dialing action abbreviation
1065  */
1066 void EndpointAppPBX::action_dialing_abbrev(void)
1067 {
1068         struct port_list *portlist = ea_endpoint->ep_portlist;
1069         char *abbrev, *phone, *name;
1070         int result;
1071
1072         portlist = ea_endpoint->ep_portlist;
1073
1074         /* abbrev dialing is only possible if we have a caller defined */
1075         if (!e_ext.number[0]) {
1076                 trace_header("ACTION abbreviation (only for extension)", DIRECTION_NONE);
1077                 end_trace();
1078                 new_state(EPOINT_STATE_OUT_DISCONNECT);
1079                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
1080                 set_tone(portlist, "cause_3f");
1081                 return;
1082         }
1083
1084         /* check abbreviation */
1085         abbrev = e_extdialing;
1086         phone = NULL;
1087         name = NULL;
1088         result = parse_phonebook(e_ext.number, &abbrev, &phone, &name);
1089         if (result == 0) {
1090                 trace_header("ACTION abbreviation (not found)", DIRECTION_NONE);
1091                 add_trace("abbrev", NULL, "%s", abbrev);
1092                 end_trace();
1093                 new_state(EPOINT_STATE_OUT_DISCONNECT);
1094                 message_disconnect_port(portlist, CAUSE_UNALLOCATED, LOCATION_PRIVATE_LOCAL, "", NULL);
1095                 set_tone(portlist, "cause_01");
1096                 return;
1097         }
1098         if (result == -1) { /* may match if more digits are dialed */
1099                 return;
1100         }
1101
1102         /* dial abbreviation */ 
1103         trace_header("ACTION abbreviation (dialing)", DIRECTION_NONE);
1104         add_trace("abbrev", NULL, "%s", abbrev);
1105         add_trace("number", NULL, "%s", phone);
1106         if (name) if (name[0])
1107                 add_trace("name", NULL, "%s", name);
1108         end_trace();
1109         SCPY(e_dialinginfo.id, phone);
1110         e_extdialing = e_dialinginfo.id;
1111         e_action = NULL;
1112         process_dialing(0);
1113 }
1114
1115
1116 /* process dialing 'test'
1117  */
1118 void EndpointAppPBX::action_dialing_test(void)
1119 {
1120         unsigned int cause;
1121         char causestr[16];
1122         struct port_list *portlist = ea_endpoint->ep_portlist;
1123         struct lcr_msg *message;
1124         class Port *port;
1125         char testcode[32] = "";
1126         struct route_param *rparam;
1127
1128         /* given testcode */
1129         if ((rparam = routeparam(e_action, PARAM_PREFIX)))
1130                 SCPY(testcode, rparam->string_value);
1131         SCAT(testcode, e_extdialing);
1132
1133         switch(testcode[0]) {
1134                 case '1':
1135                 trace_header("ACTION test", DIRECTION_NONE);
1136                 add_trace("test", NULL, "proceeding");
1137                 end_trace();
1138                 new_state(EPOINT_STATE_IN_PROCEEDING);
1139                 set_tone(portlist, "proceeding");
1140                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_PROCEEDING);
1141                 message_put(message);
1142                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1143                 break;
1144                 
1145                 case '2':
1146                 trace_header("ACTION test", DIRECTION_NONE);
1147                 add_trace("test", NULL, "alerting");
1148                 end_trace();
1149                 new_state(EPOINT_STATE_IN_ALERTING);
1150                 set_tone(portlist, "ringpbx");
1151                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_ALERTING);
1152                 message_put(message);
1153                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1154                 break;
1155                 
1156                 case '3':
1157                 trace_header("ACTION test", DIRECTION_NONE);
1158                 add_trace("test", NULL, "echo");
1159                 end_trace();
1160                 new_state(EPOINT_STATE_CONNECT);
1161                 if (e_ext.number[0])
1162                         e_dtmf = 1;
1163                 set_tone(portlist, NULL);
1164                 memset(&e_connectinfo, 0, sizeof(e_connectinfo));
1165                 SCPY(e_connectinfo.id, e_callerinfo.id);
1166                 SCPY(e_connectinfo.extension, e_callerinfo.extension);
1167                 e_connectinfo.itype = e_callerinfo.itype;
1168                 e_connectinfo.ntype = e_callerinfo.ntype;
1169                 e_connectinfo.present = e_callerinfo.present;
1170                 e_connectinfo.screen = e_callerinfo.screen;
1171                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
1172                 memcpy(&message->param.connectinfo, &e_connectinfo, sizeof(struct connect_info));
1173                 /* handle restricted caller ids */
1174                 apply_callerid_restriction(&e_ext, message->param.connectinfo.id, &message->param.connectinfo.ntype, &message->param.connectinfo.present, &message->param.connectinfo.screen, message->param.connectinfo.extension, message->param.connectinfo.name);
1175                 /* display callerid if desired for extension */
1176                 SCPY(message->param.connectinfo.display, apply_callerid_display(message->param.connectinfo.id, message->param.connectinfo.itype, message->param.connectinfo.ntype, message->param.connectinfo.present, message->param.connectinfo.screen, message->param.connectinfo.extension, message->param.connectinfo.name));
1177                 message_put(message);
1178                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1179
1180                 port = find_port_id(portlist->port_id);
1181                 if (port) {
1182                         port->set_echotest(1);
1183                 }
1184                 break;
1185                 
1186                 case '4':
1187                 trace_header("ACTION test", DIRECTION_NONE);
1188                 add_trace("test", NULL, "tone");
1189                 end_trace();
1190                 new_state(EPOINT_STATE_CONNECT);
1191                 if (e_ext.number[0])
1192                         e_dtmf = 1;
1193                 memset(&e_connectinfo, 0, sizeof(e_connectinfo));
1194                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
1195                 message_put(message);
1196                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1197                 set_tone(portlist, "test");
1198                 break;
1199                 
1200                 case '5':
1201                 trace_header("ACTION test", DIRECTION_NONE);
1202                 add_trace("test", NULL, "hold music");
1203                 end_trace();
1204                 new_state(EPOINT_STATE_CONNECT);
1205                 if (e_ext.number[0])
1206                         e_dtmf = 1;
1207                 memset(&e_connectinfo, 0, sizeof(e_connectinfo));
1208                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
1209                 message_put(message);
1210                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1211                 set_tone(portlist, "hold");
1212                 break;
1213                 
1214                 case '6':
1215                 if (strlen(testcode) < 4)
1216                         break;
1217                 testcode[4] = '\0';
1218                 cause = atoi(testcode+1);
1219                 if (cause > 255)
1220                         cause = 0;
1221                 trace_header("ACTION test", DIRECTION_NONE);
1222                 add_trace("test", NULL, "announcement");
1223                 add_trace("cause", NULL, "%d", cause);
1224                 end_trace();
1225                 new_state(EPOINT_STATE_CONNECT);
1226                 if (e_ext.number[0])
1227                         e_dtmf = 1;
1228                 SPRINT(causestr,"cause_%02x",cause);
1229                 memset(&e_connectinfo, 0, sizeof(e_connectinfo));
1230                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
1231                 message_put(message);
1232                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1233                 set_tone(portlist, causestr);
1234                 break;
1235                 
1236                 case '7':
1237                 if (strlen(testcode) < 4)
1238                         break;
1239                 testcode[4] = '\0';
1240                 cause = atoi(testcode+1);
1241                 if (cause > 127)
1242                         cause = 0;
1243                 trace_header("ACTION test", DIRECTION_NONE);
1244                 add_trace("test", NULL, "disconnect");
1245                 add_trace("cause", NULL, "%d", cause);
1246                 end_trace();
1247                 new_state(EPOINT_STATE_OUT_DISCONNECT);
1248                 SPRINT(causestr,"cause_%02x",cause);
1249                 message_disconnect_port(portlist, cause, LOCATION_PRIVATE_LOCAL, "", NULL);
1250                 set_tone(portlist, causestr);
1251                 break;
1252
1253                 case '8': /* release */
1254                 trace_header("ACTION test", DIRECTION_NONE);
1255                 add_trace("test", NULL, "release");
1256                 add_trace("cause", NULL, "16");
1257                 end_trace();
1258                 new_state(EPOINT_STATE_OUT_DISCONNECT);
1259                 message_disconnect_port(portlist, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, "", NULL);
1260                 set_tone(portlist, "release");
1261                 break;
1262
1263                 case '9': /* text callerid test */
1264                 trace_header("ACTION test", DIRECTION_NONE);
1265                 add_trace("test", NULL, "callerid");
1266                 end_trace();
1267                 new_state(EPOINT_STATE_CONNECT);
1268                 if (e_ext.number[0])
1269                         e_dtmf = 1;
1270                 memset(&e_connectinfo, 0, sizeof(e_connectinfo));
1271                 SCPY(e_connectinfo.id, "12345678");
1272                 SCPY(e_connectinfo.name, "Welcome to LCR");
1273                 SCPY(e_connectinfo.display, "Welcome to LCR");
1274                 e_connectinfo.ntype = INFO_NTYPE_UNKNOWN;
1275                 e_connectinfo.present = INFO_PRESENT_ALLOWED;
1276                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
1277                 memcpy(&message->param.connectinfo, &e_connectinfo, sizeof(message->param.connectinfo));
1278                 message_put(message);
1279                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1280                 set_tone(portlist, "hold");
1281                 break;
1282         }
1283 }
1284
1285
1286 /* process init play
1287  */
1288 void EndpointAppPBX::action_init_play(void)
1289 {
1290         struct route_param *rparam;
1291         struct port_list *portlist = ea_endpoint->ep_portlist;
1292
1293         /* check given sample */
1294         if (!(rparam = routeparam(e_action, PARAM_SAMPLE))) {
1295                 trace_header("ACTION play (no sample given)", DIRECTION_NONE);
1296                 end_trace();
1297
1298                 disconnect:
1299                 new_state(EPOINT_STATE_OUT_DISCONNECT);
1300                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
1301                 set_tone(portlist, "cause_3f");
1302                 e_action = NULL;
1303                 return;
1304         }
1305
1306         /* if sample is given */
1307         if (rparam->string_value[0] == '\0') {
1308                 trace_header("ACTION play (no sample given)", DIRECTION_NONE);
1309                 end_trace();
1310                 goto disconnect;
1311         }
1312
1313         if (e_ext.number[0])
1314                 e_dtmf = 1;
1315
1316         set_tone(ea_endpoint->ep_portlist, rparam->string_value);
1317 }
1318
1319
1320 /*
1321  * action_*_vbox_play is implemented in "action_vbox.cpp"
1322  */
1323
1324
1325 /*
1326  * process dialing of calculator
1327  */
1328 void EndpointAppPBX::action_dialing_calculator(void)
1329 {
1330         struct port_list *portlist = ea_endpoint->ep_portlist;
1331         struct lcr_msg *message;
1332         double value1, value2, v, sign1;
1333         int komma1, komma2, k, state, mode = 0, first;
1334         char *p;
1335
1336         portlist = ea_endpoint->ep_portlist;
1337
1338         /* remove error message */
1339         if (!strncmp(e_extdialing, "Error", 5)) {
1340                 UCPY(e_extdialing, e_extdialing+5);
1341         }
1342         if (!strncmp(e_extdialing, "inf", 3)) {
1343                 UCPY(e_extdialing, e_extdialing+3);
1344         }
1345         if (!strncmp(e_extdialing, "-inf", 4)) {
1346                 UCPY(e_extdialing, e_extdialing+4);
1347         }
1348
1349         /* process dialing */
1350         state = 0;
1351         value1 = 0;
1352         value2 = 0;
1353         komma1 = 0;
1354         komma2 = 0;
1355         sign1 = 1;
1356         p = e_extdialing;
1357         if (!p)
1358                 return;
1359         first = 1;
1360         while(*p) {
1361                 if (*p>='0' && *p<='9') {
1362 #if 0
1363                         if (first) {
1364                                 UCPY(p, p+1);
1365                                 continue;
1366                         }
1367                         if ((p[-1]<'0' || p[-1]>'0') && p[-1]!='.') {
1368                                 p--;
1369                                 UCPY(p, p+1);
1370                                 continue;
1371                         }
1372 #endif
1373                         switch(state) {
1374                                 case 0: /* first number */
1375                                 if (!komma1) {
1376                                         value1 = value1*10 + (*p-'0');
1377                                 } else {
1378                                         k = komma1++;
1379                                         v = *p-'0';
1380                                         while(k--)
1381                                                 v /= 10;
1382                                         value1 += v; 
1383                                 }
1384                                 break;
1385                                 case 1: /* second number */
1386                                 if (!komma2) {
1387                                         value2 = value2*10 + (*p-'0');
1388                                 } else {
1389                                         k = komma2++;
1390                                         v = *p-'0';
1391                                         while(k--)
1392                                                 v /= 10;
1393                                         value2 += v; 
1394                                 }
1395                                 break;
1396                         }
1397                 } else
1398                 switch(*p) {
1399                         case '*':
1400                         if (first) {
1401                                 UCPY(e_extdialing, "Error");
1402                                 goto done;
1403                         }
1404                         /* if there is a multiplication, we change to / */
1405                         if (p[-1] == '*') {
1406                                 mode = 1;
1407                                 p[-1] = '/';
1408                                 UCPY(p, p+1);
1409                                 p--;
1410                                 break;
1411                         }
1412                         /* if there is a division, we change to + */
1413                         if (p[-1] == '/') {
1414                                 mode = 2;
1415                                 p[-1] = '+';
1416                                 UCPY(p, p+1);
1417                                 p--;
1418                                 break;
1419                         }
1420                         /* if there is a addition, we change to - */
1421                         if (p[-1] == '+') {
1422                                 mode = 3;
1423                                 p[-1] = '-';
1424                                 UCPY(p, p+1);
1425                                 p--;
1426                                 break;
1427                         }
1428                         /* if there is a substraction and a comma, we change to * */
1429                         if (p[-1]=='-' && komma1) {
1430                                 mode = 0;
1431                                 p[-1] = '*';
1432                                 UCPY(p, p+1);
1433                                 p--;
1434                                 break;
1435                         }
1436                         /* if there is a substraction and no comma and the first or second value, we change to , */
1437                         if (p[-1]=='-') {
1438                                 p[-1] = '.';
1439                                 UCPY(p, p+1);
1440                                 p--;
1441                                 komma1 = 1;
1442                                 break;
1443                         }
1444                         /* if there is a komma and we are at the first value, we change to * */
1445                         if (p[-1]=='.' && state==0) {
1446                                 mode = 0;
1447                                 p[-1] = '*';
1448                                 UCPY(p, p+1);
1449                                 p--;
1450                                 komma1 = 0;
1451                                 break;
1452                         }
1453                         /* if there is a komma and we are at the second value, we display error */
1454                         if (komma2 && state==1) {
1455                                 UCPY(e_extdialing, "Error");
1456                                 goto done;
1457                         }
1458                         /* if we are at state 1, we write a comma */
1459                         if (state == 1) {
1460                                 *p = '.';
1461                                 komma2 = 1;
1462                                 break;
1463                         }
1464                         /* we assume multiplication */
1465                         mode = 0;
1466                         state = 1;
1467                         komma1 = 0;
1468                         break;
1469
1470                         case '#':
1471                         /* if just a number is displayed, the input is cleared */
1472                         if (state==0) {
1473                                 *e_extdialing = '\0';
1474                                 break;
1475                         }
1476                         /* calculate the result */
1477                         switch(mode) {
1478                                 case 0: /* multiply */
1479                                 UNPRINT(e_extdialing, sizeof(e_dialinginfo.id)-strlen(e_dialinginfo.id), "%.8f", sign1*value1*value2);
1480                                 break;
1481                                 case 1: /* divide */
1482                                 UNPRINT(e_extdialing, sizeof(e_dialinginfo.id)-strlen(e_dialinginfo.id), "%.8f", sign1*value1/value2);
1483                                 break;
1484                                 case 2: /* add */
1485                                 UNPRINT(e_extdialing, sizeof(e_dialinginfo.id)-strlen(e_dialinginfo.id), "%.8f", sign1*value1+value2);
1486                                 break;
1487                                 case 3: /* substract */
1488                                 UNPRINT(e_extdialing, sizeof(e_dialinginfo.id)-strlen(e_dialinginfo.id), "%.8f", sign1*value1-value2);
1489                                 break;
1490                         }
1491                         e_dialinginfo.id[sizeof(e_dialinginfo.id)-1] = '\0';
1492                         if (strchr(e_extdialing, '.')) { /* remove zeroes */
1493                                 while (e_extdialing[strlen(e_extdialing)-1] == '0')
1494                                         e_extdialing[strlen(e_extdialing)-1] = '\0';
1495                                 if (e_extdialing[strlen(e_extdialing)-1] == '.')
1496                                         e_extdialing[strlen(e_extdialing)-1] = '\0'; /* and remove dot */
1497                         }
1498                         p = strchr(e_extdialing,'\0')-1;
1499                         break;
1500
1501                         case '.':
1502                         if (state)
1503                                 komma2 = 1;
1504                         else    komma1 = 1;
1505                         break;
1506
1507                         case '/':
1508                         komma2 = 0;
1509                         state = 1;
1510                         mode = 1;
1511                         break;
1512
1513                         case '+':
1514                         komma2 = 0;
1515                         state = 1;
1516                         mode = 2;
1517                         break;
1518
1519                         case '-':
1520                         if (first) {
1521                                 sign1=-1;
1522                                 break;
1523                         }
1524                         komma2 = 0;
1525                         state = 1;
1526                         mode = 3;
1527                         break;
1528
1529                         default:
1530                         UCPY(e_extdialing, "Error");
1531                         goto done;
1532                 }
1533
1534                 p++;
1535                 first = 0;
1536         }
1537         done:
1538
1539         /* display dialing */   
1540         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_NOTIFY);
1541         SPRINT(message->param.notifyinfo.display, ">%s", e_extdialing);
1542         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s displaying interpreted dialing '%s' internal values: %f %f\n", ea_endpoint->ep_serial, e_ext.number, e_extdialing, value1, value2);
1543         message_put(message);
1544         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
1545
1546 }
1547
1548 /*
1549  * process dialing of timer
1550  */
1551 void EndpointAppPBX::action_dialing_timer(void)
1552 {
1553 }
1554
1555
1556 /*
1557  * process 'goto' or 'menu'
1558  */
1559 void EndpointAppPBX::_action_goto_menu(int mode)
1560 {
1561         struct port_list *portlist = ea_endpoint->ep_portlist;
1562         struct route_param *rparam;
1563
1564         /* check given ruleset */
1565         if (!(rparam = routeparam(e_action, PARAM_RULESET))) {
1566                 no_ruleset:
1567                 trace_header("ACTION goto/menu (no ruleset given)", DIRECTION_NONE);
1568                 end_trace();
1569
1570                 disconnect:
1571                 new_state(EPOINT_STATE_OUT_DISCONNECT);
1572                 message_disconnect_port(portlist, CAUSE_SERVICEUNAVAIL, LOCATION_PRIVATE_LOCAL, "", NULL);
1573                 set_tone(portlist, "cause_3f");
1574                 e_action = NULL;
1575                 return;
1576         }
1577         if (rparam->string_value[0] == '\0')
1578                 goto no_ruleset;
1579         e_ruleset = getrulesetbyname(rparam->string_value);
1580         if (!e_ruleset) {
1581                 trace_header("ACTION goto/menu (ruleset not found)", DIRECTION_NONE);
1582                 add_trace("ruleset", NULL, "%s", rparam->string_value);
1583                 end_trace();
1584                 goto disconnect;
1585         }
1586
1587         /* if the 'menu' was selected, we will flush all digits */
1588         if (mode) {
1589                 e_dialinginfo.id[0] = 0;
1590                 e_dialinginfo.sending_complete = 0;
1591                 e_extdialing = e_dialinginfo.id;
1592         } else {
1593                 /* remove digits that are required to match the rule */
1594                 if ((rparam = routeparam(e_action, PARAM_STRIP))) {
1595                         if (e_extdialing)
1596                                 SCPY(e_dialinginfo.id, e_extdialing);
1597                         e_extdialing = e_dialinginfo.id;
1598                 }
1599                 /* add prefix */
1600                 if ((rparam = routeparam(e_action, PARAM_PREFIX))) {
1601                         char extdialing[256];
1602                         SCPY(extdialing, e_extdialing);
1603                         SPRINT(e_dialinginfo.id, "%s%s", rparam->string_value, extdialing);
1604                         e_extdialing = e_dialinginfo.id;
1605                 }
1606
1607         }
1608
1609         /* play sample */
1610         trace_header("ACTION goto/menu (change to)", DIRECTION_NONE);
1611         add_trace("ruleset", NULL, "%s", e_ruleset->name);
1612         if (e_dialinginfo.id[0]) {
1613                 add_trace("dialing", NULL, "%s", e_dialinginfo.id);
1614         }
1615         if ((rparam = routeparam(e_action, PARAM_SAMPLE))) {
1616                 add_trace("sample", NULL, "%s", rparam->string_value);
1617                 end_trace();
1618                 set_tone(ea_endpoint->ep_portlist, rparam->string_value);
1619         } else {
1620                 end_trace();
1621         }
1622
1623         /* do dialing with new ruleset */
1624         e_action = NULL;
1625         process_dialing(0);
1626 }
1627
1628 /* process dialing goto
1629 */
1630 void EndpointAppPBX::action_dialing_goto(void)
1631 {
1632         _action_goto_menu(0);
1633 }
1634
1635 /* process dialing menu
1636 */
1637 void EndpointAppPBX::action_dialing_menu(void)
1638 {
1639         _action_goto_menu(1);
1640 }
1641
1642
1643 /*
1644  * process dialing disconnect
1645  */
1646 void EndpointAppPBX::action_dialing_disconnect(void)
1647 {
1648         struct route_param *rparam;
1649         struct port_list *portlist = ea_endpoint->ep_portlist;
1650         struct lcr_msg *message;
1651         int cause = CAUSE_NORMAL; /* normal call clearing */
1652         int location = LOCATION_PRIVATE_LOCAL;
1653         char cause_string[256] = "", display[84] = "";
1654
1655         /* check cause parameter */
1656         if ((rparam = routeparam(e_action, PARAM_CAUSE))) {
1657                 cause = rparam->integer_value;
1658                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): 'cause' is given: %d\n", ea_endpoint->ep_serial, cause);
1659         }
1660         if ((rparam = routeparam(e_action, PARAM_LOCATION))) {
1661                 location = rparam->integer_value;
1662                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): 'location' is given: %d\n", ea_endpoint->ep_serial, location);
1663         }
1664
1665
1666         /* use cause as sample, if not given later */
1667         SPRINT(cause_string, "cause_%02x", cause);
1668
1669         /* check sample parameter */
1670         if ((rparam = routeparam(e_action, PARAM_SAMPLE))) {
1671                 SCPY(cause_string, rparam->string_value);
1672                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): 'sample' is given: %s\n", ea_endpoint->ep_serial, cause_string);
1673         }
1674
1675         /* check display */
1676         if ((rparam = routeparam(e_action, PARAM_DISPLAY))) {
1677                 SCPY(display, rparam->string_value);
1678                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): 'display' is given: %s\n", ea_endpoint->ep_serial, display);
1679         }
1680
1681         /* disconnect only if connect parameter is not given */
1682         trace_header("ACTION disconnect", DIRECTION_NONE);
1683         add_trace("cause", "value", "%d", cause);
1684         add_trace("cause", "location", "%d", location);
1685         if (cause_string[0])
1686                 add_trace("sample", NULL, "%s", cause_string);
1687         if (display[0])
1688                 add_trace("display", NULL, "%s", display);
1689         end_trace();
1690         new_state(EPOINT_STATE_OUT_DISCONNECT);
1691         set_tone(portlist, cause_string);
1692         if (!(rparam = routeparam(e_action, PARAM_CONNECT))) {
1693                 message_disconnect_port(portlist, cause, location, display, NULL);
1694         } else {
1695                 message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_portlist->port_id, EPOINT_TO_PORT, MESSAGE_NOTIFY);
1696                 SCPY(message->param.notifyinfo.display, display);
1697                 message_put(message);
1698                 logmessage(message->type, &message->param, ea_endpoint->ep_portlist->port_id, DIRECTION_OUT);
1699         }
1700         e_action = NULL;
1701 }
1702
1703
1704 /*
1705  * process dialing release
1706  */
1707 void EndpointAppPBX::action_dialing_release(void)
1708 {
1709         struct route_param *rparam;
1710         int cause = CAUSE_NORMAL; /* normal call clearing */
1711         int location = LOCATION_PRIVATE_LOCAL;
1712         char cause_string[256] = "", display[84] = "";
1713
1714         /* check cause parameter */
1715         if ((rparam = routeparam(e_action, PARAM_CAUSE))) {
1716                 cause = rparam->integer_value;
1717                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): 'cause' is given: %d\n", ea_endpoint->ep_serial, cause);
1718         }
1719         if ((rparam = routeparam(e_action, PARAM_LOCATION))) {
1720                 location = rparam->integer_value;
1721                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): 'location' is given: %d\n", ea_endpoint->ep_serial, location);
1722         }
1723
1724
1725         /* use cause as sample, if not given later */
1726         SPRINT(cause_string, "cause_%02x", cause);
1727
1728         /* check display */
1729         if ((rparam = routeparam(e_action, PARAM_DISPLAY))) {
1730                 SCPY(display, rparam->string_value);
1731                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): 'display' is given: %s\n", ea_endpoint->ep_serial, display);
1732         }
1733
1734         /* disconnect only if connect parameter is not given */
1735         trace_header("ACTION release", DIRECTION_NONE);
1736         add_trace("cause", "value", "%d", cause);
1737         add_trace("cause", "location", "%d", location);
1738         if (display[0])
1739                 add_trace("display", NULL, "%s", display);
1740         end_trace();
1741         e_action = NULL;
1742         release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, location, cause, 1);
1743         return;
1744 }
1745
1746 /*
1747  * process dialing help
1748  */
1749 void EndpointAppPBX::action_dialing_help(void)
1750 {
1751         /* show all things that would match */
1752 #if 0
1753         struct numbering *numbering = numbering_int;
1754         char dialing[sizeof(e_dialinginfo.id)];
1755         int i;
1756         struct lcr_msg *message;
1757         struct route_param *rparam;
1758
1759         /* in case we have no menu (this should never happen) */
1760         if (!numbering)
1761                 return;
1762
1763         /* scroll menu */
1764         if (strchr(e_dialinginfo.id,'*')) {
1765                 e_menu--;
1766                 e_dialinginfo.id[0] = '\0';
1767         }
1768         if (strchr(e_dialinginfo.id,'#')) {
1769                 e_menu++;
1770                 e_dialinginfo.id[0] = '\0';
1771         }
1772         
1773         /* get position in menu */
1774         if (e_menu < 0) {
1775                 /* get last menu position */
1776                 e_menu = 0;
1777                 while(numbering->next) {
1778                         e_menu++;
1779                         numbering = numbering->next;
1780                 }
1781         } else {
1782                 /* get menu position */
1783                 i = 0;
1784                 while(i < e_menu) {
1785                         numbering = numbering->next;
1786                         if (!numbering) {
1787                                 e_menu = 0;
1788                                 numbering = numbering_int;
1789                                 break;
1790                         }
1791                         i++;
1792                 }
1793         }
1794
1795         /* if we dial something else we need to add the prefix and change the action */
1796         if (e_dialinginfo.id[0]) {
1797                 e_action = NUMB_ACTION_NONE;
1798                 SCPY(dialing, numbering->prefix);
1799                 //we ignore the first digit after selecting
1800                 //SCAT(dialing, e_dialinginfo.id);
1801                 SCPY(e_dialinginfo.id, dialing);
1802                 e_extdialing = e_dialinginfo.id+strlen(numbering->prefix);
1803                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s selected a new menu '%s' dialing: %s\n", ea_endpoint->ep_serial, e_ext.number, numb_actions[numbering->action], e_dialinginfo.id);
1804 nesting?:
1805                 process_dialing(0);
1806                 return;
1807         }
1808
1809         /* send display message to port */
1810         message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_portlist->port_id, EPOINT_TO_PORT, MESSAGE_NOTIFY);
1811         SPRINT(message->param.notifyinfo.display, ">%s %s%s%s", numbering->prefix, numb_actions[numbering->action], (numbering->param[0])?" ":"", numbering->param);
1812         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s selected a new menu '%s' sending display:%s\n", ea_endpoint->ep_serial, e_ext.number, numb_actions[numbering->action], message->param.notifyinfo.display);
1813         message_put(message);
1814         logmessage(message->type, message->paramea_endpoint->ep_portlist->port_id, DIRECTION_OUT);
1815 #endif
1816 }
1817
1818
1819 /*
1820  * process dialing deflect
1821  */
1822 void EndpointAppPBX::action_dialing_deflect(void)
1823 {
1824 }
1825
1826
1827 /*
1828  * process dialing setforward
1829  */
1830 void EndpointAppPBX::action_dialing_setforward(void)
1831 {
1832 }
1833
1834 /*
1835  * process init 'execute'
1836  */ 
1837 void EndpointAppPBX::action_init_execute(void)
1838 {
1839         struct route_param *rparam;
1840         int executeon = INFO_ON_HANGUP;  /* Use Hangup as a default for compatibility */
1841         
1842         /* Get the execute on parameter */
1843         if ((rparam = routeparam(e_action, PARAM_ON)))
1844         executeon = rparam->integer_value;
1845
1846         /* Execute this action if init was specified */
1847         if (executeon == INFO_ON_INIT) {
1848                 trace_header("ACTION execute ON init", DIRECTION_NONE);
1849                 end_trace();
1850                 action_execute();
1851         }
1852 }
1853
1854 /*
1855  * process hangup 'execute'
1856  */ 
1857 void EndpointAppPBX::action_hangup_execute(void)
1858 {
1859         struct route_param *rparam;
1860         int executeon = INFO_ON_HANGUP;  /* Use Hangup as a default for compatibility */
1861         
1862         /* Get the execute on parameter */
1863         if ((rparam = routeparam(e_action, PARAM_ON)))
1864         executeon = rparam->integer_value;
1865
1866         /* Execute this action if init was specified */
1867         if (executeon == INFO_ON_HANGUP) {
1868                 trace_header("ACTION execute ON hangup", DIRECTION_NONE);
1869                 end_trace();
1870                 action_execute();
1871         }
1872 }
1873
1874 /*
1875  * process 'execute' from action_init_execute or action_hangup_execute
1876  */
1877 void EndpointAppPBX::action_execute(void)
1878 {
1879         struct route_param *rparam;
1880         pid_t pid;
1881         pid_t pid2;
1882         int iWaitStatus;
1883         char *command = (char *)"";
1884         char isdn_port[10];
1885         char *argv[12]; /* check also number of args below */
1886         int i = 0;
1887
1888         /* get script / command */
1889         if ((rparam = routeparam(e_action, PARAM_EXECUTE)))
1890                 command = rparam->string_value;
1891         if (command[0] == '\0') {
1892                 trace_header("ACTION execute (no parameter given)", DIRECTION_NONE);
1893                 end_trace();
1894                 return;
1895         }
1896 #if 0
1897         argv[i++] = (char *)"/bin/sh";
1898         argv[i++] = (char *)"-c";
1899         argv[i++] = command;
1900 #endif
1901         argv[i++] = command;
1902         if ((rparam = routeparam(e_action, PARAM_PARAM))) {
1903                 argv[i++] = rparam->string_value;
1904         }
1905         argv[i++] = e_extdialing;
1906         argv[i++] = (char *)numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international);
1907         argv[i++] = e_callerinfo.extension;
1908         argv[i++] = e_callerinfo.name;
1909         SPRINT(isdn_port, "%d", e_callerinfo.isdn_port);
1910         argv[i++] = isdn_port;
1911         argv[i++] = e_callerinfo.imsi;
1912         argv[i++] = NULL; /* check also number of args above */
1913         switch (pid = fork ()) {
1914                 case -1:
1915                         trace_header("ACTION execute (fork failed)", DIRECTION_NONE);
1916                         end_trace();
1917                         break;
1918                 case 0:
1919                         /* To be shure there are no zombies created double fork */
1920                         if ((pid2 = fork()) == 0) {
1921                                 execve(command, argv, environ);
1922                         }
1923                         /* Exit immediately and release the waiting parent. The subprocess falls to init because the parent died */
1924                         exit(0);
1925                         break;
1926                 default:
1927                         trace_header("ACTION execute", DIRECTION_NONE);
1928                         add_trace("command", NULL, "%s", command);
1929                         end_trace();
1930
1931                         /* Wait for the pid. The forked process will exit immediately so there is no problem waiting. */
1932                         waitpid(pid, &iWaitStatus, 0);
1933                         break;
1934         }
1935 }
1936
1937
1938 /*
1939  * process hangup 'file'
1940  */
1941 void EndpointAppPBX::action_hangup_file(void)
1942 {
1943         struct route_param *rparam;
1944         const char *file, *content, *mode;
1945         FILE *fp;
1946
1947         /* get file / content */
1948         if (!(rparam = routeparam(e_action, PARAM_FILE)))
1949                 file = rparam->string_value;
1950         else
1951                 file = "";
1952         if (!(rparam = routeparam(e_action, PARAM_CONTENT)))
1953                 content = rparam->string_value;
1954         else
1955                 content = e_extdialing;
1956         if (!(rparam = routeparam(e_action, PARAM_APPEND)))
1957                 mode = "a";
1958         else
1959                 mode = "w";
1960         if (file[0] == '\0') {
1961                 trace_header("ACTION file (no filename given)", DIRECTION_NONE);
1962                 end_trace();
1963                 return;
1964         }
1965         if (!(fp = fopen(file, mode))) {
1966                 trace_header("ACTION file (failed to open)", DIRECTION_NONE);
1967                 add_trace("file", "name", "%s", file);
1968                 add_trace("file", "mode", "%s", (mode[0]=='w')?"write":"append");
1969                 end_trace();
1970                 return;
1971         }
1972         trace_header("ACTION file", DIRECTION_NONE);
1973         add_trace("file", "name", "%s", file);
1974         add_trace("file", "mode", "%s", (mode[0]=='w')?"write":"append");
1975         add_trace("content", NULL, "%s", content);
1976         end_trace();
1977         fprintf(fp, "%s\n", content);
1978         fclose(fp);
1979 }
1980
1981
1982 /*
1983  * process init 'pick'
1984  */
1985 void EndpointAppPBX::action_init_pick(void)
1986 {
1987         struct route_param *rparam;
1988         char *extensions = NULL;
1989
1990         if ((rparam = routeparam(e_action, PARAM_EXTENSIONS)))
1991                 extensions = rparam->string_value;
1992         
1993         trace_header("ACTION pick", DIRECTION_NONE);
1994         if (extensions) if (extensions[0])
1995                 add_trace("extensions", NULL, "%s", extensions);
1996         end_trace();
1997         pick_join(extensions);
1998 }
1999
2000
2001 /*
2002  * process dialing 'password'
2003  */
2004 void EndpointAppPBX::action_dialing_password(void)
2005 {
2006         struct port_list *portlist = ea_endpoint->ep_portlist;
2007
2008         /* prompt for password */
2009         if (e_extdialing[0] == '\0') {
2010                 /* give password tone */
2011                 set_tone(portlist, "password");
2012         } else // ELSE!!
2013         if (e_extdialing[1] == '\0') {
2014                 /* give password tone */
2015                 set_tone(portlist, "dialing");
2016         }
2017
2018         /* wait until all digits are dialed */
2019         if (strlen(e_ext.password) != strlen(e_extdialing))
2020                 return; /* more digits needed */
2021
2022         /* check the password */
2023         if (e_ext.password[0]=='\0' || (strlen(e_ext.password)==strlen(e_extdialing) && !!strcmp(e_ext.password,e_extdialing))) {
2024                 trace_header("ACTION password_write (wrong password)", DIRECTION_NONE);
2025                 add_trace("dialed", NULL, "%s", e_extdialing);
2026                 end_trace();
2027                 e_connectedmode = 0;
2028                 e_dtmf = 0;
2029                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2030                 message_disconnect_port(portlist, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, "", NULL);
2031                 set_tone(portlist, "cause_10");
2032                 return;
2033         }
2034
2035         /* write caller id if ACTION_PASSWORD_WRITE was selected */
2036         if (e_action)
2037         if (e_action->index == ACTION_PASSWORD_WRITE) {
2038                 append_callbackauth(e_ext.number, &e_callbackinfo);
2039                 trace_header("ACTION password_write (written)", DIRECTION_NONE);
2040                 add_trace("dialed", NULL, "%s", e_extdialing);
2041                 end_trace();
2042         }
2043
2044         /* make call state  */
2045         new_state(EPOINT_STATE_IN_OVERLAP);
2046         e_ruleset = ruleset_main;
2047         if (e_ruleset)
2048                 e_rule = e_ruleset->rule_first;
2049         e_action = NULL;
2050         e_dialinginfo.id[0] = '\0';
2051         e_extdialing = e_dialinginfo.id;
2052         set_tone(portlist, "dialpbx");
2053 }
2054
2055 void EndpointAppPBX::action_dialing_password_wr(void)
2056 {
2057         action_dialing_password();
2058 }
2059
2060
2061 /* process pots-retrieve
2062  */
2063 void EndpointAppPBX::action_init_pots_retrieve(void)
2064 {
2065 #ifdef ISDN_P_FXS_POTS
2066         struct route_param *rparam;
2067         struct port_list *portlist = ea_endpoint->ep_portlist;
2068         class Port *port;
2069         class Pfxs *ourfxs, *fxs;
2070         int count = 0;
2071         class Endpoint *epoint;
2072
2073         /* check given call */
2074         if (!(rparam = routeparam(e_action, PARAM_POTS_CALL))) {
2075                 trace_header("ACTION pots-retrieve (no call given)", DIRECTION_NONE);
2076                 end_trace();
2077
2078                 disconnect:
2079                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2080                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
2081                 set_tone(portlist, "cause_3f");
2082                 e_action = NULL;
2083                 return;
2084         }
2085
2086         /* find call */
2087         port = find_port_id(portlist->port_id);
2088         if (!port)
2089                 goto disconnect;
2090         if ((port->p_type & PORT_CLASS_POTS_MASK) != PORT_CLASS_POTS_FXS) {
2091                 trace_header("ACTION pots-retrieve (call not of FXS type)", DIRECTION_NONE);
2092                 end_trace();
2093                 goto disconnect;
2094         }
2095         ourfxs = (class Pfxs *)port;
2096
2097         port = port_first;
2098         while(port) {
2099                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
2100                         fxs = (class Pfxs *)port;
2101                         if (fxs->p_m_mISDNport == ourfxs->p_m_mISDNport && fxs != ourfxs) {
2102                                 count++;
2103                                 if (count == rparam->integer_value)
2104                                         break;
2105                         }
2106                 }
2107                 port = port->next;
2108         }
2109         if (!port) {
2110                 trace_header("ACTION pots-retrieve (call # does not exist)", DIRECTION_NONE);
2111                 end_trace();
2112                 goto disconnect;
2113         }
2114
2115         /* release our call */
2116         ourfxs->hangup_ind(0);
2117
2118         /* retrieve selected call */
2119         fxs->retrieve_ind(0);
2120
2121         /* split if selected call is member of a 3pty */
2122         epoint = find_epoint_id(ACTIVE_EPOINT(fxs->p_epointlist));
2123         if (epoint && epoint->ep_app_type == EAPP_TYPE_PBX) {
2124                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d) try spliting 3pty. this may fail because we don't have a 3pty.\n", epoint->ep_serial);
2125                 ((class EndpointAppPBX *)epoint->ep_app)->split_3pty();
2126         }
2127 #endif
2128 }
2129
2130
2131 /* process pots-release
2132  */
2133 void EndpointAppPBX::action_init_pots_release(void)
2134 {
2135 #ifdef ISDN_P_FXS_POTS
2136         struct route_param *rparam;
2137         struct port_list *portlist = ea_endpoint->ep_portlist;
2138         class Port *port;
2139         class Pfxs *ourfxs, *fxs;
2140         int count = 0;
2141
2142         /* check given call */
2143         if (!(rparam = routeparam(e_action, PARAM_POTS_CALL))) {
2144                 trace_header("ACTION pots-release (no call given)", DIRECTION_NONE);
2145                 end_trace();
2146
2147                 disconnect:
2148                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2149                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
2150                 set_tone(portlist, "cause_3f");
2151                 e_action = NULL;
2152                 return;
2153         }
2154
2155         /* find call */
2156         port = find_port_id(portlist->port_id);
2157         if (!port)
2158                 goto disconnect;
2159         if ((port->p_type & PORT_CLASS_POTS_MASK) != PORT_CLASS_POTS_FXS) {
2160                 trace_header("ACTION pots-release (call not of FXS type)", DIRECTION_NONE);
2161                 end_trace();
2162                 goto disconnect;
2163         }
2164         ourfxs = (class Pfxs *)port;
2165
2166         port = port_first;
2167         while(port) {
2168                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
2169                         fxs = (class Pfxs *)port;
2170                         if (fxs->p_m_mISDNport == ourfxs->p_m_mISDNport && fxs != ourfxs) {
2171                                 count++;
2172                                 if (count == rparam->integer_value)
2173                                         break;
2174                         }
2175                 }
2176                 port = port->next;
2177         }
2178         if (!port) {
2179                 trace_header("ACTION pots-release (call # does not exist)", DIRECTION_NONE);
2180                 end_trace();
2181                 goto disconnect;
2182         }
2183
2184 #if 0
2185         /* disconnect our call */
2186         new_state(EPOINT_STATE_OUT_DISCONNECT);
2187         message_disconnect_port(portlist, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, "", NULL);
2188         set_tone(portlist, "hangup");
2189         e_action = NULL;
2190 #endif
2191
2192         /* release selected call */
2193         fxs->hangup_ind(0);
2194
2195         /* indicate timeout, so next action will be processed */
2196         process_dialing(1);
2197 #endif
2198 }
2199
2200
2201 /* process pots-reject
2202  */
2203 void EndpointAppPBX::action_init_pots_reject(void)
2204 {
2205 #ifdef ISDN_P_FXS_POTS
2206         struct port_list *portlist = ea_endpoint->ep_portlist;
2207         class Port *port;
2208         class Pfxs *ourfxs, *fxs;
2209
2210         /* find call */
2211         port = find_port_id(portlist->port_id);
2212         if (!port)
2213                 goto disconnect;
2214         if ((port->p_type & PORT_CLASS_POTS_MASK) != PORT_CLASS_POTS_FXS) {
2215                 trace_header("ACTION pots-reject (call not of FXS type)", DIRECTION_NONE);
2216                 end_trace();
2217                 disconnect:
2218                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2219                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
2220                 set_tone(portlist, "cause_3f");
2221                 e_action = NULL;
2222                 return;
2223         }
2224         ourfxs = (class Pfxs *)port;
2225
2226         port = port_first;
2227         while(port) {
2228                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
2229                         fxs = (class Pfxs *)port;
2230                         if (fxs->p_m_mISDNport == ourfxs->p_m_mISDNport && fxs != ourfxs) {
2231                                 if (fxs->p_state == PORT_STATE_OUT_ALERTING)
2232                                         break;
2233                         }
2234                 }
2235                 port = port->next;
2236         }
2237         if (!port) {
2238                 trace_header("ACTION pots-reject (no call waiting)", DIRECTION_NONE);
2239                 end_trace();
2240                 goto disconnect;
2241         }
2242
2243         /* reject alerting call */
2244         fxs->reject_ind(0);
2245
2246         /* indicate timeout, so next action will be processed */
2247         process_dialing(1);
2248 #endif
2249 }
2250
2251
2252 /* process pots-answer
2253  */
2254 void EndpointAppPBX::action_init_pots_answer(void)
2255 {
2256 #ifdef ISDN_P_FXS_POTS
2257         struct port_list *portlist = ea_endpoint->ep_portlist;
2258         class Port *port;
2259         class Pfxs *ourfxs, *fxs;
2260
2261         /* find call */
2262         port = find_port_id(portlist->port_id);
2263         if (!port)
2264                 goto disconnect;
2265         if ((port->p_type & PORT_CLASS_POTS_MASK) != PORT_CLASS_POTS_FXS) {
2266                 trace_header("ACTION pots-answer (call not of FXS type)", DIRECTION_NONE);
2267                 end_trace();
2268                 disconnect:
2269                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2270                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
2271                 set_tone(portlist, "cause_3f");
2272                 e_action = NULL;
2273                 return;
2274         }
2275         ourfxs = (class Pfxs *)port;
2276
2277         port = port_first;
2278         while(port) {
2279                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
2280                         fxs = (class Pfxs *)port;
2281                         if (fxs->p_m_mISDNport == ourfxs->p_m_mISDNport && fxs != ourfxs) {
2282                                 if (fxs->p_state == PORT_STATE_OUT_ALERTING)
2283                                         break;
2284                         }
2285                 }
2286                 port = port->next;
2287         }
2288         if (!port) {
2289                 trace_header("ACTION pots-answer (no call waiting)", DIRECTION_NONE);
2290                 end_trace();
2291                 goto disconnect;
2292         }
2293
2294         /* release our call */
2295         ourfxs->hangup_ind(0);
2296
2297         /* answer alerting call */
2298         fxs->answer_ind(0);
2299 #endif
2300 }
2301
2302
2303 /* process pots-3pty
2304  */
2305 void EndpointAppPBX::action_init_pots_3pty(void)
2306 {
2307 #ifdef ISDN_P_FXS_POTS
2308         struct port_list *portlist = ea_endpoint->ep_portlist;
2309         class Port *port;
2310         class Pfxs *ourfxs, *fxs, *fxs1 = NULL, *fxs2 = NULL;
2311         class Endpoint *epoint;
2312         int count = 0;
2313
2314         /* find call */
2315         port = find_port_id(portlist->port_id);
2316         if (!port)
2317                 goto disconnect;
2318         if ((port->p_type & PORT_CLASS_POTS_MASK) != PORT_CLASS_POTS_FXS) {
2319                 trace_header("ACTION pots-3pty (call not of FXS type)", DIRECTION_NONE);
2320                 end_trace();
2321                 disconnect:
2322                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2323                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
2324                 set_tone(portlist, "cause_3f");
2325                 e_action = NULL;
2326                 return;
2327         }
2328         ourfxs = (class Pfxs *)port;
2329
2330         port = port_first;
2331         while(port) {
2332                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
2333                         fxs = (class Pfxs *)port;
2334                         if (fxs->p_m_mISDNport == ourfxs->p_m_mISDNport && fxs != ourfxs) {
2335                                 if (count == 0)
2336                                         fxs1 = fxs;
2337                                 if (count == 1)
2338                                         fxs2 = fxs;
2339                                 count++;
2340                         }
2341                 }
2342                 port = port->next;
2343         }
2344         if (count != 2) {
2345                 trace_header("ACTION pots-3pty (exactly two calls don't exist)", DIRECTION_NONE);
2346                 end_trace();
2347                 goto disconnect;
2348         }
2349
2350         /* release our call */
2351         ourfxs->hangup_ind(0);
2352
2353         /* retrieve latest active call */
2354         if (fxs2->p_m_fxs_age > fxs1->p_m_fxs_age) {
2355                 fxs2->retrieve_ind(0);
2356                 epoint = find_epoint_id(ACTIVE_EPOINT(fxs2->p_epointlist));
2357         } else {
2358                 fxs1->retrieve_ind(0);
2359                 epoint = find_epoint_id(ACTIVE_EPOINT(fxs2->p_epointlist));
2360         }
2361
2362         if (!epoint) {
2363                 trace_header("ACTION pots-3pty (interal error: no endpoint)", DIRECTION_NONE);
2364                 end_trace();
2365                 return;
2366         }
2367
2368         if (epoint->ep_app_type != EAPP_TYPE_PBX) {
2369                 trace_header("ACTION pots-3pty (interal error: endpoint not PBX type)", DIRECTION_NONE);
2370                 end_trace();
2371                 return;
2372         }
2373
2374         /* bridge calls */
2375         if (((class EndpointAppPBX *)epoint->ep_app)->join_3pty_fxs()) {
2376                 trace_header("ACTION pots-3pty (interal error: join_3pty_fsx failed)", DIRECTION_NONE);
2377                 end_trace();
2378                 return;
2379         }
2380 #endif
2381 }
2382
2383 /* process pots-transfer
2384  */
2385 void EndpointAppPBX::action_init_pots_transfer(void)
2386 {
2387 #ifdef ISDN_P_FXS_POTS
2388         struct route_param *rparam;
2389         struct port_list *portlist = ea_endpoint->ep_portlist;
2390         class Port *port;
2391         class Pfxs *ourfxs, *fxs, *fxs1 = NULL, *fxs2 = NULL;
2392         int count = 0;
2393
2394         /* check given call */
2395         if (!(rparam = routeparam(e_action, PARAM_POTS_CALL))) {
2396                 trace_header("ACTION pots-transfer (no call given)", DIRECTION_NONE);
2397                 end_trace();
2398
2399                 disconnect:
2400                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2401                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
2402                 set_tone(portlist, "cause_3f");
2403                 e_action = NULL;
2404                 return;
2405         }
2406
2407         /* find call */
2408         port = find_port_id(portlist->port_id);
2409         if (!port)
2410                 goto disconnect;
2411         if ((port->p_type & PORT_CLASS_POTS_MASK) != PORT_CLASS_POTS_FXS) {
2412                 trace_header("ACTION pots-transfer (call not of FXS type)", DIRECTION_NONE);
2413                 end_trace();
2414                 goto disconnect;
2415         }
2416         ourfxs = (class Pfxs *)port;
2417
2418         port = port_first;
2419         while(port) {
2420                 if ((port->p_type & PORT_CLASS_POTS_MASK) == PORT_CLASS_POTS_FXS) {
2421                         fxs = (class Pfxs *)port;
2422                         if (fxs->p_m_mISDNport == ourfxs->p_m_mISDNport && fxs != ourfxs) {
2423                                 if (count == 0)
2424                                         fxs1 = fxs;
2425                                 if (count == 1)
2426                                         fxs2 = fxs;
2427                                 count++;
2428                         }
2429                 }
2430                 port = port->next;
2431         }
2432         if (count != 2) {
2433                 trace_header("ACTION pots-transfer (exactly two calls don't exist)", DIRECTION_NONE);
2434                 end_trace();
2435                 goto disconnect;
2436         }
2437
2438         /* retrieve call */
2439         if (fxs2->p_m_fxs_age > fxs1->p_m_fxs_age)
2440                 fxs2->retrieve_ind(0);
2441         else
2442                 fxs1->retrieve_ind(0);
2443         /* bridge calls */
2444         join_join_fxs();
2445 #endif
2446 }
2447
2448
2449 /* general process dialing of incoming call
2450  * depending on the detected prefix, subfunctions above (action_*) will be
2451  * calles.
2452  */
2453 void EndpointAppPBX::process_dialing(int timeout)
2454 {
2455         struct port_list *portlist = ea_endpoint->ep_portlist;
2456         struct lcr_msg *message;
2457         struct route_param *rparam;
2458         struct timeval current_time;
2459
2460         /* set if timeout is active, or if timeout value was given due to timeout action */
2461         if (e_action_timeout.active)
2462                 timeout = 1;
2463
2464 //#warning Due to HANG-BUG somewhere here, I added some HANG-BUG-DEBUGGING output that cannot be disabled. after bug has been found, this will be removed.
2465 //PDEBUG(~0, "HANG-BUG-DEBUGGING: entered porcess_dialing\n");
2466         portlist = ea_endpoint->ep_portlist;
2467         /* check if we have a port instance linked to our epoint */
2468         if (!portlist) {
2469                 portlist_error:
2470                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): note: dialing call requires exactly one port object to process dialing. this case could happen due to a parked call. we end dialing here.\n", ea_endpoint->ep_serial, e_ext.number);
2471                 unsched_timer(&e_action_timeout);
2472                 unsched_timer(&e_match_timeout);
2473                 return;
2474         }
2475         if (portlist->next) {
2476                 goto portlist_error;
2477         }
2478
2479         /* check nesting levels */
2480         if (++e_rule_nesting > RULE_NESTING) {
2481                 trace_header("ACTION (nesting too deep)", DIRECTION_NONE);
2482                 add_trace("max-levels", NULL, "%d", RULE_NESTING);
2483                 end_trace();
2484                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2485                 message_disconnect_port(portlist, CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL, "", NULL);
2486                 set_tone(portlist, "cause_3f");
2487                 unsched_timer(&e_action_timeout);
2488                 unsched_timer(&e_match_timeout);
2489                 goto end;
2490         }
2491
2492 //PDEBUG(~0, "HANG-BUG-DEBUGGING: before action-timeout processing\n");
2493         /* process timeout */
2494         if (e_action && timeout) { /* e_action may be NULL, but e_action_timeout may still be set and must be ignored */
2495                 unsched_timer(&e_action_timeout);
2496                 if (e_state == EPOINT_STATE_CONNECT) {
2497                         PDEBUG(DEBUG_ROUTE|DEBUG_EPOINT, "EPOINT(%d): action timed out, but we already have connected, so we stop timer and continue.\n", ea_endpoint->ep_serial);
2498                         goto end;
2499                 }
2500                 if (e_action->index == ACTION_DISCONNECT
2501                  || e_state == EPOINT_STATE_OUT_DISCONNECT) {
2502                         /* release after disconnect */
2503                         release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0);
2504                         goto end;
2505                 }
2506                 release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0, 0, 0);
2507                 e_action = e_action->next;
2508                 if (!e_action) {
2509                         /* nothing more, so we release */
2510                         PDEBUG(DEBUG_ROUTE|DEBUG_EPOINT, "EPOINT(%d): action timed out, and we have no next action, so we disconnect.\n", ea_endpoint->ep_serial);
2511                         new_state(EPOINT_STATE_OUT_DISCONNECT);
2512                         message_disconnect_port(portlist, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, "", NULL);
2513                         set_tone(portlist, "cause_3f");
2514                         goto end;
2515                 }
2516                 goto action_timeout;
2517         }
2518
2519 //PDEBUG(~0, "HANG-BUG-DEBUGGING: before setup/overlap state checking\n");
2520         if (e_state!=EPOINT_STATE_IN_SETUP
2521          && e_state!=EPOINT_STATE_IN_OVERLAP) {
2522                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): we are not in incoming setup/overlap state, so we ignore init/dialing process.\n", ea_endpoint->ep_serial, e_rule_nesting);
2523                 unsched_timer(&e_match_timeout);
2524                 goto end;
2525         }
2526
2527 #if 0
2528         /* check if we do menu selection */
2529         if (e_action==NUMB_ACTION_NONE && (e_dialinginfo.id[0]=='*' || e_dialinginfo.id[0]=='#'))
2530         /* do menu selection */
2531         if (e_ext.display_menu) {
2532                 if (portlist->port_type==PORT_TYPE_DSS1_NT_IN || portlist->port_type==PORT_TYPE_DSS1_NT_OUT) { /* only if the dialing terminal is an isdn telephone connected to an internal port */
2533                         e_dialinginfo.id[0] = '\0';
2534                         e_action = NUMB_ACTION_MENU;
2535                         e_menu = 0;
2536                         process_dialing(0);
2537                         unsched_timer(&e_match_timeout);
2538                         goto end;
2539                 }
2540                 /* invalid dialing */
2541                 message_disconnect_port(portlist, CAUSE_INCALID, LOCATION_PRIVATE_LOCAL, "", NULL);
2542                         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_DISCONNECT);
2543                         message->param.disconnectinfo.cause = CAUSE_INVALID;
2544                         message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2545                                 } else {
2546                                         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_NOTIFY);
2547                                         SCPY(message->param.notifyinfo.display,get_isdn_cause(LOCATION_PRIVATE_LOCAL, epoint->e_ext.display_cause, param->disconnectinfo.location, param->disconnectinfo.cause));
2548                                 }
2549                         message_put(message);
2550                         logmessage(message->type, message->param, portlist->port_id, DIRECTION_OUT);
2551                 }
2552                 new_state(EPOINT_STATE_OUT_DISCONNECT);
2553                 set_tone(portlist,"cause_1c");
2554                 unsched_timer(&e_match_timeout);
2555                 goto end;
2556         }
2557 #endif
2558
2559 //PDEBUG(~0, "HANG-BUG-DEBUGGING: before e_action==NULL\n");
2560         /* if no action yet, we will call try to find a matching rule */
2561         if (!e_action) {
2562                 /* be sure that all selectors are initialized */
2563                 e_select = 0;
2564
2565                 /* check for external call */
2566                 if (!strncmp(e_dialinginfo.id, "extern:", 7)) {
2567                         e_extdialing = e_dialinginfo.id+7;
2568                         e_action = &action_external;
2569                         goto process_action;
2570                 }
2571                 /* check for internal call */
2572                 if (!strncmp(e_dialinginfo.id, "intern:", 7)) {
2573                         e_extdialing = e_dialinginfo.id+7;
2574                         e_action = &action_internal;
2575                         goto process_action;
2576                 }
2577                 /* check for vbox call */
2578                 if (!strncmp(e_dialinginfo.id, "vbox:", 5)) {
2579                         e_extdialing = e_dialinginfo.id+5;
2580                         e_action = &action_vbox;
2581                         goto process_action;
2582                 }
2583
2584                 gettimeofday(&current_time, NULL);
2585                 if (e_match_to_action && TIME_SMALLER(&e_match_timeout.timeout, &current_time)) {
2586                         /* return timeout rule */
2587                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal '%s' dialing: '%s', timeout in ruleset '%s'\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id, e_ruleset->name);
2588                         unsched_timer(&e_match_timeout);
2589                         e_action = e_match_to_action;
2590                         e_match_to_action = NULL;
2591                         e_extdialing = e_match_to_extdialing;
2592                         trace_header("ROUTING (timeout)", DIRECTION_NONE);
2593                         add_trace("action", NULL, "%s", action_defs[e_action->index].name);
2594                         add_trace("line", NULL, "%d", e_action->line);
2595                         end_trace();
2596                 } else {
2597 //PDEBUG(~0, "HANG-BUG-DEBUGGING: before routing\n");
2598                         /* check for matching rule */
2599                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal '%s' dialing: '%s', checking matching rule of ruleset '%s'\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id, e_ruleset->name);
2600                         if (e_ruleset) {
2601                                 e_action = route(e_ruleset);
2602                                 if (e_action) {
2603                                         trace_header("ACTION (match)", DIRECTION_NONE);
2604                                         add_trace("action", NULL, "%s", action_defs[e_action->index].name);
2605                                         add_trace("line", NULL, "%d", e_action->line);
2606                                         end_trace();
2607                                 }
2608                         } else {
2609                                 e_action = &action_disconnect;
2610                                 if (e_action) {
2611                                         trace_header("ACTION (no main ruleset, disconnecting)", DIRECTION_NONE);
2612                                         end_trace();
2613                                 }
2614                         }
2615 //PDEBUG(~0, "HANG-BUG-DEBUGGING: after routing\n");
2616                 }
2617                 if (!e_action) {
2618                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): no rule within the current ruleset matches yet.\n", ea_endpoint->ep_serial, e_ext.number);
2619                         goto display;
2620                 }
2621
2622                 /* matching */
2623                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): a rule with action '%s' matches.\n", ea_endpoint->ep_serial, action_defs[e_action->index].name);
2624
2625                 action_timeout:
2626
2627                 /* set timeout */
2628                 unsched_timer(&e_action_timeout);
2629                 if (e_action->timeout) {
2630                         schedule_timer(&e_action_timeout, e_action->timeout, 0);
2631                         PDEBUG(DEBUG_ROUTE|DEBUG_EPOINT, "EPOINT(%d): action has a timeout of %d secods.\n", ea_endpoint->ep_serial, e_action->timeout);
2632                 }
2633
2634                 process_action:
2635                 /* check param proceeding / alerting / connect */
2636                 if ((rparam = routeparam(e_action, PARAM_CONNECT))) {
2637                         /* NOTE: we may not change our state to connect, because dialing will then not possible */
2638                         e_dtmf = 1;
2639                         memset(&e_connectinfo, 0, sizeof(e_connectinfo));
2640                         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
2641                         message_put(message);
2642                         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
2643                 } else
2644                 if ((rparam = routeparam(e_action, PARAM_ALERTING))) {
2645                         /* NOTE: we may not change our state to alerting, because dialing will then not possible */
2646                         memset(&e_connectinfo, 0, sizeof(e_connectinfo));
2647                         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_ALERTING);
2648                         message_put(message);
2649                         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
2650                 } else
2651                 if ((rparam = routeparam(e_action, PARAM_PROCEEDING))) {
2652                         /* NOTE: we may not change our state to proceeding, because dialing will then not possible */
2653                         memset(&e_connectinfo, 0, sizeof(e_connectinfo));
2654                         message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_PROCEEDING);
2655                         message_put(message);
2656                         logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
2657                 }
2658
2659                 if (action_defs[e_action->index].init_func) {
2660                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: current action '%s' has a init function, so we call it...\n", ea_endpoint->ep_serial, e_ext.number, action_defs[e_action->index].name);
2661                         (this->*(action_defs[e_action->index].init_func))();
2662                 }
2663                 if (e_state!=EPOINT_STATE_IN_SETUP
2664                  && e_state!=EPOINT_STATE_IN_OVERLAP) {
2665                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): AFTER init process: we are not in incoming setup/overlap state anymore, so we ignore further dialing process.\n", ea_endpoint->ep_serial, e_rule_nesting);
2666                         goto display_action;
2667                 }
2668         }
2669
2670         /* show what we are doing */
2671         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal '%s' action: %s (dialing '%s')\n", ea_endpoint->ep_serial, e_ext.number, action_defs[e_action->index].name, e_extdialing);
2672         /* go to action's dialing function */
2673         if (action_defs[e_action->index].dialing_func) {
2674                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: current action '%s' has a dialing function, so we call it...\n", ea_endpoint->ep_serial, e_ext.number, action_defs[e_action->index].name);
2675                 (this->*(action_defs[e_action->index].dialing_func))();
2676         }
2677
2678         /* display selected dialing action if enabled and still in setup state */
2679         display_action:
2680         if (e_action) {
2681                 if (e_action->index==ACTION_MENU
2682                  || e_action->index==ACTION_REDIAL
2683                  || e_action->index==ACTION_REPLY
2684                  || e_action->index==ACTION_TIMER
2685                  || e_action->index==ACTION_CALCULATOR
2686                  || e_action->index==ACTION_TEST)
2687                         goto end;
2688         }
2689         display:
2690         if (!e_ext.display_dialing)
2691                 goto end;
2692         if (e_state==EPOINT_STATE_IN_OVERLAP || e_state==EPOINT_STATE_IN_PROCEEDING || e_state==EPOINT_STATE_IN_ALERTING || e_state==EPOINT_STATE_CONNECT/* || e_state==EPOINT_STATE_IN_DISCONNECT || e_state==EPOINT_STATE_OUT_DISCONNECT*/)
2693         if (portlist->port_type==PORT_TYPE_DSS1_NT_IN || portlist->port_type==PORT_TYPE_DSS1_NT_OUT) { /* only if the dialing terminal is an isdn telephone connected to an internal port */
2694                 message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_NOTIFY);
2695
2696                 if (!e_action) {
2697                         SPRINT(message->param.notifyinfo.display, "> %s", e_dialinginfo.id);
2698                 } else {
2699                         SPRINT(message->param.notifyinfo.display, "%s%s%s", action_defs[e_action->index].name, (e_extdialing[0])?" ":"", e_extdialing);
2700                 }
2701
2702                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s displaying interpreted dialing '%s'\n", ea_endpoint->ep_serial, e_ext.number, message->param.notifyinfo.display);
2703                 message_put(message);
2704                 logmessage(message->type, &message->param, portlist->port_id, DIRECTION_OUT);
2705         }
2706
2707 end:
2708         e_rule_nesting--;
2709         return;
2710 }
2711
2712
2713 /* some services store information after hangup */
2714 void EndpointAppPBX::process_hangup(int cause, int location)
2715 {
2716         char callertext[256], dialingtext[256];
2717         int writeext = 0, i;
2718
2719         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal '%s'\n", ea_endpoint->ep_serial, e_ext.number);
2720         if (e_ext.number[0]) {
2721                 if (read_extension(&e_ext, e_ext.number))
2722                         writeext = 0x10;
2723
2724                 if (!e_start) {
2725                         time(&e_start);
2726                         e_stop = 0;
2727                 } else
2728                 if (!e_stop)
2729                         time(&e_stop);
2730                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): writing connect from %s to %s into logfile of %s\n", ea_endpoint->ep_serial, e_callerinfo.id, e_dialinginfo.id, e_ext.number);
2731                 switch(e_dialinginfo.itype) {
2732                         case INFO_ITYPE_CHAN:
2733                         SPRINT(dialingtext, "chan:%s", e_dialinginfo.id);
2734                         break;
2735                         case INFO_ITYPE_ISDN_EXTENSION:
2736                         SPRINT(dialingtext, "intern:%s", e_dialinginfo.id);
2737                         break;
2738                         case INFO_ITYPE_VBOX:
2739                         SPRINT(dialingtext, "vbox:%s", e_dialinginfo.id);
2740                         break;
2741                         default:
2742                         SPRINT(dialingtext, "%s", e_dialinginfo.id);
2743                 }
2744
2745                 if (e_callerinfo.id[0])
2746                         SPRINT(callertext, "%s", numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international));
2747                 else
2748                         SPRINT(callertext, "unknown");
2749                 /* allpy restriction */
2750                 if (!e_ext.anon_ignore && e_callerinfo.present==INFO_PRESENT_RESTRICTED)
2751                         SPRINT(callertext, "anonymous");
2752                 if (e_callerinfo.extension[0]) /* add intern if present */
2753                         UNPRINT(strchr(callertext,'\0'), sizeof(callertext)-1+strlen(callertext), " (intern %s)", e_callerinfo.extension);
2754                 write_log(e_ext.number, callertext, dialingtext, e_start, e_stop, 0, cause, location);
2755
2756                 /* store last received call for reply-list */
2757                 if (e_origin == 1) // outgoing to phone is incoming for user
2758                 if (e_callerinfo.id[0] || e_callerinfo.extension[0])
2759                 if (e_ext.anon_ignore || e_callerinfo.present!=INFO_PRESENT_RESTRICTED) {
2760                         if (e_callerinfo.extension[0])
2761                                 SPRINT(callertext, "intern:%s", e_callerinfo.extension);
2762                         else
2763                                 SPRINT(callertext, "extern:%s", numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international));
2764                         if (!!strcmp(callertext, e_ext.last_in[0])) {
2765                                 i = MAX_REMEMBER-1;
2766                                 while(i) {
2767                                         UCPY(e_ext.last_in[i], e_ext.last_in[i-1]);
2768                                         i--;
2769                                 }
2770                                 SCPY(e_ext.last_in[0], callertext);
2771                                 writeext |= 1; /* store extension later */
2772                                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: storing last received caller id '%s'.\n", ea_endpoint->ep_serial, e_ext.number, e_ext.last_in[0]);
2773                         } else
2774                                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: cannot store last received id '%s' because it is identical with the last one.\n", ea_endpoint->ep_serial, e_ext.number, callertext);
2775                 }
2776
2777                 /* store last made call for reply-list */
2778                 if (e_origin == 0) // incoming from phone is outgoing for user
2779                 if (e_dialinginfo.id[0]) {
2780                         if (!!strcmp(e_dialinginfo.id, e_ext.last_out[0])) {
2781                                 i = MAX_REMEMBER-1;
2782                                 while(i) {
2783                                         UCPY(e_ext.last_out[i], e_ext.last_out[i-1]);
2784                                         i--;
2785                                 }
2786                                 SCPY(e_ext.last_out[0], e_dialinginfo.id);
2787                                 writeext |= 1; /* store extension later */
2788                                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: storing last number '%s'.\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id);
2789                         } else
2790                                 PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: cannot store last number '%s' because it is identical with the last one.\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id);
2791                 }
2792         }
2793         /* write extension if needed */
2794         if (writeext == 0x11)
2795                 write_extension(&e_ext, e_ext.number);
2796
2797         if (e_action) {
2798                 if (action_defs[e_action->index].hangup_func) {
2799                         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: current action '%s' has a hangup function, so we call it...\n", ea_endpoint->ep_serial, e_ext.number, action_defs[e_action->index].name);
2800                         (this->*(action_defs[e_action->index].hangup_func))();
2801                 }
2802         }
2803 }
2804