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