work on socket. (don't try yet)
[lcr.git] / action.cpp
index 8056435..83e4965 100644 (file)
@@ -236,6 +236,7 @@ void EndpointAppPBX::action_dialing_internal(void)
                trace_header("ACTION extension (extension doesn't exist)", DIRECTION_NONE);
                add_trace("extension", NULL, dialinginfo.id);
                end_trace();
+               release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0, 0);
                new_state(EPOINT_STATE_OUT_DISCONNECT);
                message_disconnect_port(portlist, CAUSE_UNALLOCATED, LOCATION_PRIVATE_LOCAL, "");
                set_tone(portlist, "cause_86");
@@ -248,6 +249,7 @@ void EndpointAppPBX::action_dialing_internal(void)
                add_trace("extension", NULL, dialinginfo.id);
                end_trace();
                new_state(EPOINT_STATE_OUT_DISCONNECT);
+               release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0, 0);
                message_disconnect_port(portlist, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, "");
                set_tone(portlist, "cause_81");
                return;
@@ -382,7 +384,7 @@ void EndpointAppPBX::action_dialing_external(void)
        {
                trace_header("ACTION extern (calling denied)", DIRECTION_NONE);
                end_trace();
-               release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, 0);
+               release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, 0, 0);
                set_tone(portlist, "cause_82");
                denied:
                message_disconnect_port(portlist, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, "");
@@ -399,7 +401,7 @@ void EndpointAppPBX::action_dialing_external(void)
                {
                        trace_header("ACTION extern (national calls denied)", DIRECTION_NONE);
                        end_trace();
-                       release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, 0);
+                       release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, 0, 0);
                        set_tone(portlist, "cause_83");
                        goto denied;
                }
@@ -413,7 +415,7 @@ void EndpointAppPBX::action_dialing_external(void)
                {
                        trace_header("ACTION extern (international calls denied)", DIRECTION_NONE);
                        end_trace();
-                       release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, LOCATION_PRIVATE_LOCAL, 0);
+                       release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_REJECTED, 0, 0);
                        set_tone(portlist, "cause_84");
                        goto denied;
                }
@@ -546,7 +548,7 @@ void EndpointAppPBX::action_init_partyline(void)
        struct port_list *portlist = ea_endpoint->ep_portlist;
        struct message *message;
        struct route_param *rparam;
-       int partyline;
+       int partyline, jingle = 0;
        struct join_relation *relation;
 
        portlist = ea_endpoint->ep_portlist;
@@ -571,6 +573,8 @@ void EndpointAppPBX::action_init_partyline(void)
                goto noroom;
        }
        partyline = rparam->integer_value;
+       if ((rparam = routeparam(e_action, PARAM_JINGLE)))
+               jingle = 1;
 
        /* don't create join if partyline exists */
        join = join_first;
@@ -579,7 +583,7 @@ void EndpointAppPBX::action_init_partyline(void)
                if (join->j_type == JOIN_TYPE_PBX)
                {
                        joinpbx = (class JoinPBX *)join;
-                       if (joinpbx->j_partyline == rparam->integer_value)
+                       if (joinpbx->j_partyline == partyline)
                                break;
                }
                join = join->next;
@@ -614,9 +618,11 @@ void EndpointAppPBX::action_init_partyline(void)
        /* send setup to join */
        trace_header("ACTION partyline (calling)", DIRECTION_NONE);
        add_trace("room", NULL, "%d", partyline);
+       add_trace("jingle", NULL, (jingle)?"on":"off");
        end_trace();
        message = message_create(ea_endpoint->ep_serial, ea_endpoint->ep_join_id, EPOINT_TO_JOIN, MESSAGE_SETUP);
        message->param.setup.partyline = partyline;
+       message->param.setup.partyline_jingle = jingle;
        memcpy(&message->param.setup.dialinginfo, &e_dialinginfo, sizeof(struct dialing_info));
        memcpy(&message->param.setup.redirinfo, &e_redirinfo, sizeof(struct redir_info));
        memcpy(&message->param.setup.callerinfo, &e_callerinfo, sizeof(struct caller_info));
@@ -630,44 +636,8 @@ void EndpointAppPBX::action_init_partyline(void)
  */
 void EndpointAppPBX::action_hangup_call(void)
 {
-       int i;
-
        trace_header("ACTION hangup", DIRECTION_NONE);
        end_trace();
-       /* check */
-       if (e_ext.number[0] == '\0')
-       {
-               PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: cannot store last dialed number '%s' because caller is unknown (not internal).\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id);
-               return;
-       }
-       if (!(read_extension(&e_ext, e_ext.number)))
-       {
-               PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: cannot store last dialed number '%s' because cannot read settings.\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id);
-               return;
-       }
-       if (e_dialinginfo.id[0] == '\0')
-       {
-               PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: cannot store last dialed number because nothing was dialed.\n", ea_endpoint->ep_serial, e_ext.number);
-               return;
-       }
-       if (!strcmp(e_dialinginfo.id, e_ext.last_out[0]))
-       {
-               PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: cannot store last dialed number '%s' because it is identical with the last one.\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id);
-               return;
-       }
-
-       /* insert */
-       PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: storing last number '%s'.\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id);
-       i = MAX_REMEMBER-1;
-       while(i)
-       {
-               UCPY(e_ext.last_out[i], e_ext.last_out[i-1]);
-               i--;
-       }
-       SCPY(e_ext.last_out[0], e_dialinginfo.id);
-
-       /* write extension */
-       write_extension(&e_ext, e_ext.number);
 }
 
 
@@ -728,7 +698,7 @@ void EndpointAppPBX::action_dialing_login(void)
                message->param.connectinfo.present = INFO_PRESENT_RESTRICTED;
        else    message->param.connectinfo.present = e_ext.callerid_present;
        /* handle restricted caller ids */
-       apply_callerid_restriction(e_ext.anon_ignore, message->param.connectinfo.id, &message->param.connectinfo.ntype, &message->param.connectinfo.present, &message->param.connectinfo.screen, message->param.connectinfo.extension, message->param.connectinfo.name);
+       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);
        /* display callerid if desired for extension */
        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));
        message->param.connectinfo.ntype = e_ext.callerid_type;
@@ -1403,7 +1373,7 @@ void EndpointAppPBX::action_dialing_test(void)
                message = message_create(ea_endpoint->ep_serial, portlist->port_id, EPOINT_TO_PORT, MESSAGE_CONNECT);
                memcpy(&message->param.connectinfo, &e_connectinfo, sizeof(struct connect_info));
                /* handle restricted caller ids */
-               apply_callerid_restriction(e_ext.anon_ignore, message->param.connectinfo.id, &message->param.connectinfo.ntype, &message->param.connectinfo.present, &message->param.connectinfo.screen, message->param.connectinfo.extension, message->param.connectinfo.name);
+               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);
                /* display callerid if desired for extension */
                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));
                message_put(message);
@@ -1855,7 +1825,7 @@ void EndpointAppPBX::_action_goto_menu(int mode)
        } else
        {
                /* remove digits that are required to match the rule */
-               if (!(rparam = routeparam(e_action, PARAM_STRIP)))
+               if ((rparam = routeparam(e_action, PARAM_STRIP)))
                {
                        if (e_extdialing)
                                SCPY(e_dialinginfo.id, e_extdialing);
@@ -1867,13 +1837,18 @@ void EndpointAppPBX::_action_goto_menu(int mode)
        trace_header("ACTION goto/menu (change to)", DIRECTION_NONE);
        add_trace("ruleset", NULL, "%s", e_ruleset->name);
        if (e_dialinginfo.id[0])
+       {
                add_trace("dialing", NULL, "%s", e_dialinginfo.id);
+       }
        if ((rparam = routeparam(e_action, PARAM_SAMPLE)))
        {
                add_trace("sample", NULL, "%s", rparam->string_value);
+               end_trace();
                set_tone(ea_endpoint->ep_portlist, rparam->string_value);
+       } else
+       {
+               end_trace();
        }
-       end_trace();
 
        /* do dialing with new ruleset */
        e_action = NULL;
@@ -2065,8 +2040,9 @@ void EndpointAppPBX::action_dialing_setforward(void)
 void EndpointAppPBX::action_hangup_execute(void)
 {
        struct route_param *rparam;
+       pid_t pid;
        char *command = "", isdn_port[10];
-       char *argv[7+1]; /* check also number of args below */
+       char *argv[11]; /* check also number of args below */
        int i = 0;
 
        /* get script / command */
@@ -2078,26 +2054,35 @@ void EndpointAppPBX::action_hangup_execute(void)
                end_trace();
                return;
        }
-       trace_header("ACTION execute", DIRECTION_NONE);
-       add_trace("command", NULL, "%s", command);
-       end_trace();
-
-       argv[0] = command;
-       while(strchr(argv[0], '/'))
-               argv[0] = strchr(argv[0], '/')+1;
+       argv[i++] = "/bin/sh";
+       argv[i++] = "-c";
+       argv[i++] = command;
+       argv[i++] = command;
        if ((rparam = routeparam(e_action, PARAM_PARAM)))
        {
-               argv[1] = rparam->string_value;
-               i++;
+               argv[i++] = rparam->string_value;
        }
-       argv[1+i] = e_extdialing;
-       argv[2+i] = numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype);
-       argv[3+i] = e_callerinfo.extension;
-       argv[4+i] = e_callerinfo.name;
+       argv[i++] = e_extdialing;
+       argv[i++] = numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype);
+       argv[i++] = e_callerinfo.extension;
+       argv[i++] = e_callerinfo.name;
        SPRINT(isdn_port, "%d", e_callerinfo.isdn_port);
-       argv[5+i] = isdn_port;
-       argv[6+i] = NULL; /* check also number of args above */
-       execve("/bin/sh", argv, environ);
+       argv[i++] = isdn_port;
+       argv[i++] = NULL; /* check also number of args above */
+       switch (pid = fork ()) {
+               case -1:
+                       trace_header("ACTION execute (fork failed)", DIRECTION_NONE);
+                       end_trace();
+                       break;
+               case 0:
+                       execve("/bin/sh", argv, environ);
+                       break;
+               default:
+                       trace_header("ACTION execute", DIRECTION_NONE);
+                       add_trace("command", NULL, "%s", command);
+                       end_trace();
+                       break;
+       }
 }
 
 
@@ -2288,7 +2273,7 @@ void EndpointAppPBX::process_dialing(void)
                        release(RELEASE_ALL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL);
                        goto end;
                }
-               release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL, 0);
+               release(RELEASE_JOIN, LOCATION_PRIVATE_LOCAL, CAUSE_NORMAL, 0, 0);
                e_action = e_action->next;
                if (!e_action)
                {
@@ -2306,7 +2291,7 @@ void EndpointAppPBX::process_dialing(void)
        if (e_state!=EPOINT_STATE_IN_SETUP
         && e_state!=EPOINT_STATE_IN_OVERLAP)
        {
-               PDEBUG(DEBUG_EPOINT, "EPOINT(%d): we are not in incomming setup/overlap state, so we ignore init/dialing process.\n", ea_endpoint->ep_serial, e_rule_nesting);
+               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);
                e_match_timeout = 0;
                goto end;
        }
@@ -2474,7 +2459,7 @@ void EndpointAppPBX::process_dialing(void)
                if (e_state!=EPOINT_STATE_IN_SETUP
                 && e_state!=EPOINT_STATE_IN_OVERLAP)
                {
-                       PDEBUG(DEBUG_EPOINT, "EPOINT(%d): AFTER init process: we are not in incomming setup/overlap state anymore, so we ignore further dialing process.\n", ea_endpoint->ep_serial, e_rule_nesting);
+                       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);
                        goto display_action;
                }
        }
@@ -2574,6 +2559,7 @@ void EndpointAppPBX::process_hangup(int cause, int location)
                write_log(e_ext.number, callertext, dialingtext, e_start, e_stop, 0, cause, location);
 
                /* store last received call for reply-list */
+               if (e_origin == 1) // outgoing to phone is incoming for user
                if (e_callerinfo.id[0] || e_callerinfo.extension[0])
                if (e_ext.anon_ignore || e_callerinfo.present!=INFO_PRESENT_RESTRICTED)
                {
@@ -2595,8 +2581,26 @@ void EndpointAppPBX::process_hangup(int cause, int location)
                        } else
                                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);
                }
-       }
 
+               /* store last made call for reply-list */
+               if (e_origin == 0) // incoming from phone is outgoing for user
+               if (e_dialinginfo.id[0])
+               {
+                       if (!!strcmp(e_dialinginfo.id, e_ext.last_out[0]))
+                       {
+                               i = MAX_REMEMBER-1;
+                               while(i)
+                               {
+                                       UCPY(e_ext.last_out[i], e_ext.last_out[i-1]);
+                                       i--;
+                               }
+                               SCPY(e_ext.last_out[0], e_dialinginfo.id);
+                               writeext |= 1; /* store extension later */
+                               PDEBUG(DEBUG_EPOINT, "EPOINT(%d): terminal %s: storing last number '%s'.\n", ea_endpoint->ep_serial, e_ext.number, e_dialinginfo.id);
+                       } else
+                               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);
+               }
+       }
        /* write extension if needed */
        if (writeext == 0x11)
                write_extension(&e_ext, e_ext.number);