Fixed parsing capability conditions
[lcr.git] / route.c
diff --git a/route.c b/route.c
index ca8ded8..61a5b60 100644 (file)
--- a/route.c
+++ b/route.c
@@ -251,7 +251,7 @@ struct param_defs param_defs[] = {
 struct action_defs action_defs[] = {
        { ACTION_EXTERNAL,
          "extern",     &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
-         PARAM_CONNECT | PARAM_PREFIX | PARAM_COMPLETE | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_INTERFACES | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_KEYPAD | PARAM_TIMEOUT,
+         PARAM_CONNECT | PARAM_PREFIX | PARAM_COMPLETE | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_INTERFACES | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_KEYPAD | PARAM_CONTEXT | PARAM_TIMEOUT,
          "Call is routed to extern number as dialed."},
        { ACTION_INTERNAL,
          "intern",     &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_internal, &EndpointAppPBX::action_hangup_call,
@@ -261,10 +261,6 @@ struct action_defs action_defs[] = {
          "outdial",    &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
          PARAM_CONNECT | PARAM_PREFIX | PARAM_COMPLETE | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_INTERFACES | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_KEYPAD | PARAM_TIMEOUT,
          "Same as 'extern'"},
-       { ACTION_REMOTE,
-         "remote",     &EndpointAppPBX::action_init_remote, &EndpointAppPBX::action_dialing_remote, &EndpointAppPBX::action_hangup_call,
-         PARAM_CONNECT | PARAM_APPLICATION | PARAM_CONTEXT | PARAM_EXTEN | PARAM_TIMEOUT,
-         "Call is routed to Remote application, like Asterisk."},
        { ACTION_VBOX_RECORD,
          "vbox-record",&EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_vbox_record, &EndpointAppPBX::action_hangup_call,
          PARAM_CONNECT | PARAM_EXTENSION | PARAM_ANNOUNCEMENT | PARAM_TIMEOUT,
@@ -1220,19 +1216,25 @@ struct route_ruleset *ruleset_parse(void)
 
                                /* parse service value */
                                case COND_TYPE_CAPABILITY:
-                               if (!strncasecmp("speech", p, 6))
+                               if (!strncasecmp("speech", p, 6)) {
                                        cond->integer_value = INFO_BC_SPEECH;
-                               else if (!strncasecmp("audio", p, 5))
+                                       p += 6;
+                               } else if (!strncasecmp("audio", p, 5)) {
                                        cond->integer_value = INFO_BC_AUDIO;
-                               else if (!strncasecmp("video", p, 5))
+                                       p += 5;
+                               } else if (!strncasecmp("video", p, 5)) {
                                        cond->integer_value = INFO_BC_VIDEO;
-                               else if (!strncasecmp("digital-restricted", p, 18))
+                                       p += 5;
+                               } else if (!strncasecmp("digital-restricted", p, 18)) {
                                        cond->integer_value = INFO_BC_DATARESTRICTED;
-                               else if (!strncasecmp("digital-unrestricted", p, 20))
+                                       p += 18;
+                               } else if (!strncasecmp("digital-unrestricted", p, 20)) {
                                        cond->integer_value = INFO_BC_DATAUNRESTRICTED;
-                               else if (!strncasecmp("digital-unrestricted-tones", p, 26))
+                                       p += 20;
+                               } else if (!strncasecmp("digital-unrestricted-tones", p, 26)) {
                                        cond->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
-                               else {
+                                       p += 26;
+                               } else {
                                        SPRINT(failure, "Given service type is invalid or misspelled.");
                                        goto parse_error;
                                }
@@ -1241,11 +1243,13 @@ struct route_ruleset *ruleset_parse(void)
 
                                /* parse bmode value */
                                case COND_TYPE_BMODE:
-                               if (!strncasecmp("transparent", p, 11))
+                               if (!strncasecmp("transparent", p, 11)) {
                                        cond->integer_value = INFO_BMODE_CIRCUIT;
-                               else if (!strncasecmp("hdlc", p, 4))
+                                       p += 11;
+                               } else if (!strncasecmp("hdlc", p, 4)) {
                                        cond->integer_value = INFO_BMODE_PACKET;
-                               else {
+                                       p += 4;
+                               } else {
                                        SPRINT(failure, "Given bchannel mode is invalid or misspelled.");
                                        goto parse_error;
                                }
@@ -1254,35 +1258,49 @@ struct route_ruleset *ruleset_parse(void)
 
                                /* parse service value */
                                case COND_TYPE_HLC:
-                               if (!strncasecmp("telephony", p, 9))
+                               if (!strncasecmp("telephony", p, 9)) {
                                        cond->integer_value = INFO_HLC_TELEPHONY;
-                               else if (!strncasecmp("faxg2g3", p, 7))
+                                       p += 9;
+                               } else if (!strncasecmp("faxg2g3", p, 7)) {
                                        cond->integer_value = INFO_HLC_FAXG2G3;
-                               else if (!strncasecmp("faxg4", p, 5))
+                                       p += 7;
+                               } else if (!strncasecmp("faxg4", p, 5)) {
                                        cond->integer_value = INFO_HLC_FAXG4;
-                               else if (!strncasecmp("teletex1", p, 8))
+                                       p += 5;
+                               } else if (!strncasecmp("teletex1", p, 8)) {
                                        cond->integer_value = INFO_HLC_TELETEX1;
-                               else if (!strncasecmp("teletex2", p, 8))
+                                       p += 8;
+                               } else if (!strncasecmp("teletex2", p, 8)) {
                                        cond->integer_value = INFO_HLC_TELETEX2;
-                               else if (!strncasecmp("teletex3", p, 8))
+                                       p += 8;
+                               } else if (!strncasecmp("teletex3", p, 8)) {
                                        cond->integer_value = INFO_HLC_TELETEX3;
-                               else if (!strncasecmp("videotex1", p, 9))
+                                       p += 8;
+                               } else if (!strncasecmp("videotex1", p, 9)) {
                                        cond->integer_value = INFO_HLC_VIDEOTEX1;
-                               else if (!strncasecmp("videotex2", p, 9))
+                                       p += 9;
+                               } else if (!strncasecmp("videotex2", p, 9)) {
                                        cond->integer_value = INFO_HLC_VIDEOTEX2;
-                               else if (!strncasecmp("telex", p, 5))
+                                       p += 9;
+                               } else if (!strncasecmp("telex", p, 5)) {
                                        cond->integer_value = INFO_HLC_TELEX;
-                               else if (!strncasecmp("mhs", p, 3))
+                                       p += 5;
+                               } else if (!strncasecmp("mhs", p, 3)) {
                                        cond->integer_value = INFO_HLC_MHS;
-                               else if (!strncasecmp("osi", p, 3))
+                                       p += 3;
+                               } else if (!strncasecmp("osi", p, 3)) {
                                        cond->integer_value = INFO_HLC_OSI;
-                               else if (!strncasecmp("maintenance", p, 11))
+                                       p += 3;
+                               } else if (!strncasecmp("maintenance", p, 11)) {
                                        cond->integer_value = INFO_HLC_MAINTENANCE;
-                               else if (!strncasecmp("management", p, 10))
+                                       p += 11;
+                               } else if (!strncasecmp("management", p, 10)) {
                                        cond->integer_value = INFO_HLC_MANAGEMENT;
-                               else if (!strncasecmp("audiovisual", p, 11))
+                                       p += 10;
+                               } else if (!strncasecmp("audiovisual", p, 11)) {
                                        cond->integer_value = INFO_HLC_AUDIOVISUAL;
-                               else {
+                                       p += 11;
+                               } else {
                                        SPRINT(failure, "Given HLC type is invalid or misspelled.");
                                        goto parse_error;
                                }
@@ -1882,24 +1900,30 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                istrue,
                                couldbetrue,
                                condition,
-                               dialing_required,
-                               avail,
-                               any;
+                               dialing_required;
        struct route_rule       *rule = ruleset->rule_first;
        struct route_cond       *cond;
        struct route_action     *action = NULL;
        unsigned long           comp_len;
-       int                     j, jj;
+       int                     j;
+       char                    isdn_port[10];
+       char                    *argv[11]; /* check also number of args below */
        char                    callerid[64], callerid2[64], redirid[64];
        int                     integer;
        char                    *string;
        FILE                    *tfp;
        long long               timeout, now_ll = 0, match_timeout = 0;
        struct timeval          current_time;
+#ifdef WITH_MISDN
        struct mISDNport        *mISDNport;
+       int                     avail,
+                               any;
+       int                     jj;
+#endif
        struct admin_list       *admin;
        time_t                  now;
        struct tm               *now_tm;
+       int                     pid, status;
 
        /* reset timeout action */
        e_match_to_action = NULL;
@@ -1932,11 +1956,14 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                break;
 
                                case MATCH_PORT:
+#ifdef WITH_MISDN
                                if (ea_endpoint->ep_portlist)
                                if ((ea_endpoint->ep_portlist->port_type & PORT_CLASS_MASK) != PORT_CLASS_mISDN)
                                        break;
                                integer = e_callerinfo.isdn_port;
                                goto match_integer;
+#endif
+                               break;
 
                                case MATCH_INTERFACE:
                                if (!e_callerinfo.interface[0])
@@ -2068,8 +2095,33 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                break;
 
                                case MATCH_EXECUTE:
-                               if (system(cond->string_value) == 0)
-                                       istrue = 1;
+                               j = 0;
+#if 0
+                               argv[j++] = (char *)"/bin/sh";
+                               argv[j++] = (char *)"-c";
+                               argv[j++] = cond->string_value;
+#endif
+                               argv[j++] = cond->string_value;
+                               argv[j++] = e_extdialing;
+                               argv[j++] = (char *)numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international);
+                               argv[j++] = e_callerinfo.extension;
+                               argv[j++] = e_callerinfo.name;
+                               SPRINT(isdn_port, "%d", e_callerinfo.isdn_port);
+                               argv[j++] = isdn_port;
+                               argv[j++] = e_callerinfo.imsi;
+                               argv[j++] = NULL; /* check also number of args above */
+                               switch ((pid = fork())) {
+                               case 0:
+                                       execve(cond->string_value, argv, environ);
+                                       perror("execve");
+                                       exit(1);
+                               case -1:
+                                       break;
+                               default:
+                                       waitpid(pid, &status, 0);
+                                       if (0 == WEXITSTATUS(status))
+                                               istrue = 1;
+                               }
                                break;
 
                                case MATCH_DEFAULT:
@@ -2088,6 +2140,7 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
 
                                case MATCH_FREE:
                                case MATCH_NOTFREE:
+#ifdef WITH_MISDN
                                if (!(comp_len = (unsigned long)strchr(cond->string_value, ':')))
                                        break;
                                comp_len = comp_len-(unsigned long)cond->string_value;
@@ -2116,10 +2169,12 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                        if (avail < atoi(cond->string_value + comp_len + 1))
                                                istrue = 1;
                                }
+#endif
                                break;
 
 
                                case MATCH_DOWN:
+#ifdef WITH_MISDN
                                mISDNport = mISDNport_first;
                                while(mISDNport) {
                                        if (mISDNport->ifport)
@@ -2130,9 +2185,11 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                }
                                if (!mISDNport) /* all down */
                                        istrue = 1;
+#endif
                                break;
 
                                case MATCH_UP:
+#ifdef WITH_MISDN
                                mISDNport = mISDNport_first;
                                while(mISDNport) {
                                        if (mISDNport->ifport)
@@ -2144,10 +2201,12 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                }
                                if (mISDNport) /* one link at least */
                                        istrue = 1;
+#endif
                                break;
 
                                case MATCH_BUSY:
                                case MATCH_IDLE:
+#ifdef WITH_MISDN
                                any = 0;
                                mISDNport = mISDNport_first;
                                while(mISDNport) {
@@ -2161,6 +2220,7 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
                                        istrue = 1;
                                if (!mISDNport && cond->match==MATCH_IDLE)
                                        istrue = 1;
+#endif
                                break;
 
                                case MATCH_REMOTE:
@@ -2285,12 +2345,13 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
 
                        cond = cond->next;
                }
-               if (timeout>now_ll && match==1) /* the matching rule with timeout in the future */
-               if (match_timeout == 0 || timeout < match_timeout) { /* first timeout or lower */
-                       /* set timeout in the furture */
-                       match_timeout = timeout;
-                       e_match_to_action = rule->action_first;
-                       e_match_to_extdialing = e_dialinginfo.id + dialing_required;
+               if (timeout>now_ll && match==1) { /* the matching rule with timeout in the future */
+                       if (match_timeout == 0 || timeout < match_timeout) { /* first timeout or lower */
+                               /* set timeout in the furture */
+                               match_timeout = timeout;
+                               e_match_to_action = rule->action_first;
+                               e_match_to_extdialing = e_dialinginfo.id + dialing_required;
+                       }
                        match = 0; /* matches in the future */
                }
                if (match == 1) {
@@ -2310,7 +2371,7 @@ struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
        if (match_timeout == 0)
                unsched_timer(&e_match_timeout); /* no timeout */
        else {
-               schedule_timer(&e_match_timeout, match_timeout / 1000000, match_timeout % 1000000);
+               schedule_timer(&e_match_timeout, (match_timeout-now_ll) / 1000000, (match_timeout-now_ll) % 1000000);
        }
        return(action);
 }
@@ -2366,14 +2427,6 @@ struct route_action action_internal = {
        0,
 };
 
-struct route_action action_remote = {
-       NULL,
-       NULL,
-       ACTION_REMOTE,
-       0,
-       0,
-};
-
 struct route_action action_vbox = {
        NULL,
        NULL,