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