Major DISA improvements (bug #2284)
[asterisk/asterisk.git] / apps / app_disa.c
index 1be7a06..11c9766 100755 (executable)
@@ -7,13 +7,17 @@
  *
  * Jim Dixon <jim@lambdatel.com>
  *
+ * Made only slightly more sane by Mark Spencer <markster@digium.com>
+ *
  * This program is free software, distributed under the terms of
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
+#include <asterisk/indications.h>
 #include <asterisk/pbx.h>
 #include <asterisk/module.h>
 #include <asterisk/translate.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
-#include <pthread.h>
 #include <sys/time.h>
 
-#define        TONE_BLOCK_SIZE 200
 
 static char *tdesc = "DISA (Direct Inward System Access) Application";
 
@@ -61,7 +63,13 @@ static char *descrip =
        "The file that contains the passcodes (if used) allows specification\n"
        "of either just a passcode (defaulting to the \"disa\" context, or\n"
        "passcode|context on each line of the file. The file may contain blank\n"
-       "lines, or comments starting with \"#\" or \";\".\n\n"
+       "lines, or comments starting with \"#\" or \";\". In addition, the\n"
+       "above arguments may have |new-callerid-string appended to them, to\n"
+       "specify a new (different) callerid to be used for this call, for\n"
+       "example: numeric-passcode|context|\"My Phone\" <(234) 123-4567> or \n"
+       "full-pathname-of-passcode-file|\"My Phone\" <(234) 123-4567>. Note that\n"
+       "in the case of specifying the numeric-passcode, the context must be\n"
+       "specified if the callerid is specified also.\n\n"
        "If login is successful, the application parses the dialed number in\n"
        "the specified (or default) context, and returns 0 with the new extension\n"
        "context filled-in and the priority set to 1, so that the PBX may\n"
@@ -72,28 +80,8 @@ STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
-static float loudness=8192.0;
-
-int firstdigittimeout = 10000; /* 10 seconds first digit timeout */
-int digittimeout = 5000; /* 5 seconds subsequent digit timeout */
-
-static void make_tone_block(unsigned char *data, float f1, float f2, int *x);
-
-static void make_tone_block(unsigned char *data, float f1, float f2, int *x)
-{
-int    i;
-float  val;
-
-       for(i = 0; i < TONE_BLOCK_SIZE; i++)
-       {
-               val = loudness * sin((f1 * 2.0 * M_PI * (*x))/8000.0);
-               val += loudness * sin((f2 * 2.0 * M_PI * (*x)++)/8000.0);
-               data[i] = ast_lin2mu[(int)val + 32768];
-        }              
-         /* wrap back around from 8000 */
-       if (*x >= 8000) *x = 0;
-       return;
-}
+static int firstdigittimeout = 20000; /* 20 seconds first digit timeout */
+static int digittimeout = 10000; /* 10 seconds subsequent digit timeout */
 
 static int ms_diff(struct timeval *tv1, struct timeval *tv2)
 {
@@ -108,14 +96,14 @@ static int disa_exec(struct ast_channel *chan, void *data)
 {
        int i,j,k,x;
        struct localuser *u;
-       char tmp[256],exten[AST_MAX_EXTENSION];
-       unsigned char tone_block[TONE_BLOCK_SIZE],sil_block[TONE_BLOCK_SIZE];
-       char *ourcontext;
-       struct ast_frame *f,wf;
-       fd_set  readfds;
-       int waitfor_notime;
-       struct timeval notime = { 0,0 }, lastout, now, lastdigittime;
+       char tmp[256],arg2[256]="",exten[AST_MAX_EXTENSION],acctcode[20]="";
+       char *ourcontext,*ourcallerid;
+       struct ast_frame *f;
+       struct timeval lastout, now, lastdigittime;
+       int res;
+       time_t rstart;
        FILE *fp;
+       char *stringp=NULL;
 
        if (ast_set_write_format(chan,AST_FORMAT_ULAW))
        {
@@ -128,32 +116,47 @@ static int disa_exec(struct ast_channel *chan, void *data)
                return -1;
        }
        lastout.tv_sec = lastout.tv_usec = 0;
-         /* make block of silence */
-       memset(sil_block,0x7f,TONE_BLOCK_SIZE);
        if (!data || !strlen((char *)data)) {
                ast_log(LOG_WARNING, "disa requires an argument (passcode/passcode file)\n");
                return -1;
        }
-       strncpy(tmp, (char *)data, sizeof(tmp));
-       strtok(tmp, "|");
-       ourcontext = strtok(NULL, "|");
+       strncpy(tmp, (char *)data, sizeof(tmp)-1);
+       stringp=tmp;
+       strsep(&stringp, "|");
+       ourcontext = strsep(&stringp, "|");
+       /* if context specified, save 2nd arg and parse third */
+       if (ourcontext) {
+               strncpy(arg2,ourcontext, sizeof(arg2) - 1);
+               ourcallerid = strsep(&stringp,"|");
+       }
          /* if context not specified, use "disa" */
-       if (!ourcontext) ourcontext = "disa";
+       else {
+               arg2[0] = 0;
+               ourcallerid = NULL;
+               ourcontext = "disa";
+       }
        LOCAL_USER_ADD(u);
-       if (chan->state != AST_STATE_UP)
+       if (chan->_state != AST_STATE_UP)
        {
                /* answer */
                ast_answer(chan);
        }
        i = k = x = 0; /* k is 0 for pswd entry, 1 for ext entry */
        exten[0] = 0;
+       acctcode[0] = 0;
        /* can we access DISA without password? */ 
+
+       ast_log(LOG_DEBUG, "Context: %s\n",ourcontext);
+
        if (!strcasecmp(tmp, "no-password"))
-       {
+       {;
                k = 1;
                ast_log(LOG_DEBUG, "DISA no-password login success\n");
        }
        gettimeofday(&lastdigittime,NULL);
+
+       ast_tonepair_start(chan, 350, 440, 0, 0);
+
        for(;;)
        {
                gettimeofday(&now,NULL);
@@ -163,35 +166,13 @@ static int disa_exec(struct ast_channel *chan, void *data)
                {
                        ast_log(LOG_DEBUG,"DISA %s entry timeout on chan %s\n",
                                ((k) ? "extension" : "password"),chan->name);
-                       goto reorder;
+                       break;
                }
-                 /* if first digit or ignore, send dialtone */
-               if ((!i) || (ast_ignore_pattern(ourcontext,exten) && k)) 
-               {
-                       gettimeofday(&now,NULL);
-                       if (lastout.tv_sec && 
-                               (ms_diff(&now,&lastout) < 25)) continue;
-                       lastout.tv_sec = now.tv_sec;
-                       lastout.tv_usec = now.tv_usec;
-                       wf.frametype = AST_FRAME_VOICE;
-                       wf.subclass = AST_FORMAT_ULAW;
-                       wf.offset = AST_FRIENDLY_OFFSET;
-                       wf.mallocd = 0;
-                       wf.data = tone_block;
-                       wf.datalen = TONE_BLOCK_SIZE;                           
-                       /* make this tone block */
-                       make_tone_block(tone_block,350.0,440.0,&x);
-                       wf.timelen = wf.datalen / 8;
-                       if (ast_write(chan, &wf)) 
-                       {
-                               ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
-                               LOCAL_USER_REMOVE(u);
-                               return -1;
-                       }
+               if ((res = ast_waitfor(chan, -1) < 0)) {
+                       ast_log(LOG_DEBUG, "Waitfor returned %d\n", res);
+                       continue;
                }
-               waitfor_notime = notime.tv_usec + notime.tv_sec * 1000;
-               if (!ast_waitfor_nandfds(&chan, 1, &(chan->fds[0]), 1, NULL, NULL,
-                       &waitfor_notime)) continue;
+                       
                f = ast_read(chan);
                if (f == NULL) 
                {
@@ -205,14 +186,22 @@ static int disa_exec(struct ast_channel *chan, void *data)
                        LOCAL_USER_REMOVE(u);
                        return -1;
                }
+               if (f->frametype == AST_FRAME_VOICE) {
+                       ast_frfree(f);
+                       continue;
+               }
                  /* if not DTMF, just do it again */
                if (f->frametype != AST_FRAME_DTMF) 
                {
                        ast_frfree(f);
                        continue;
                }
+
                j = f->subclass;  /* save digit */
                ast_frfree(f);
+               if (i == 0) 
+                       ast_playtones_stop(chan);
+
                gettimeofday(&lastdigittime,NULL);
                  /* got a DTMF tone */
                if (i < AST_MAX_EXTENSION) /* if still valid number of digits */
@@ -234,6 +223,7 @@ static int disa_exec(struct ast_channel *chan, void *data)
                                                tmp[0] = 0;
                                                while(fgets(tmp,sizeof(tmp) - 1,fp))
                                                   {
+                                                       char *stringp=NULL,*stringp2;
                                                        if (!tmp[0]) continue;
                                                        if (tmp[strlen(tmp) - 1] == '\n') 
                                                                tmp[strlen(tmp) - 1] = 0;
@@ -241,8 +231,14 @@ static int disa_exec(struct ast_channel *chan, void *data)
                                                          /* skip comments */
                                                        if (tmp[0] == '#') continue;
                                                        if (tmp[0] == ';') continue;
-                                                       strtok(tmp, "|");
-                                                       ourcontext = strtok(NULL, "|");
+                                                       stringp=tmp;
+                                                       strsep(&stringp, "|");
+                                                       stringp2=strsep(&stringp, "|");
+                                                       if (stringp2) {
+                                                               ourcontext=stringp2;
+                                                               stringp2=strsep(&stringp, "|");
+                                                               if (stringp2) ourcallerid=stringp2;
+                                                       }
                                                          /* password must be in valid format (numeric) */
                                                        if (sscanf(tmp,"%d",&j) < 1) continue;
                                                          /* if we got it */
@@ -255,10 +251,16 @@ static int disa_exec(struct ast_channel *chan, void *data)
                                        {
                                                ast_log(LOG_WARNING,"DISA on chan %s got bad password %s\n",chan->name,exten);
                                                goto reorder;
+
                                        }
                                         /* password good, set to dial state */
+                                       ast_log(LOG_DEBUG,"DISA on chan %s password is good\n",chan->name);
+                                       ast_tonepair_start(chan, 350, 440, 0, 0);
+
                                        k = 1;
                                        i = 0;  /* re-set buffer pointer */
+                                       exten[sizeof(acctcode)] = 0;
+                                       strncpy(acctcode,exten, sizeof(acctcode) - 1);
                                        exten[0] = 0;
                                        ast_log(LOG_DEBUG,"Successful DISA log-in on chan %s\n",chan->name);
                                        continue;
@@ -268,108 +270,49 @@ static int disa_exec(struct ast_channel *chan, void *data)
                        exten[i] = 0;
                        if (!k) continue; /* if getting password, continue doing it */
                          /* if this exists */
-                       if (ast_exists_extension(chan,ourcontext,exten,1, chan->callerid))
-                       {
-                               strcpy(chan->exten,exten);
-                               strcpy(chan->context,ourcontext);
-                               chan->priority = 0;
-                               LOCAL_USER_REMOVE(u);
-                               return 0;
-                       }
+
                          /* if can do some more, do it */
-                       if (ast_canmatch_extension(chan,ourcontext,exten,1, chan->callerid)) continue;
+                       if (!ast_matchmore_extension(chan,ourcontext,exten,1, chan->callerid)) {
+                               break;
+                       }
                }
-reorder:
-               /* something is invalid, give em reorder forever */
-               x = 0;
-               for(;;)
+       }
+
+       if (k && ast_exists_extension(chan,ourcontext,exten,1, chan->callerid))
+       {
+               ast_playtones_stop(chan);
+               /* We're authenticated and have a valid extension */
+               if (ourcallerid && *ourcallerid)
                {
-                       for(i = 0; i < 10; i++)
-                       {
-                               do gettimeofday(&now,NULL);
-                               while (lastout.tv_sec && 
-                                       (ms_diff(&now,&lastout) < 25)) ;
-                               lastout.tv_sec = now.tv_sec;
-                               lastout.tv_usec = now.tv_usec;
-                               wf.frametype = AST_FRAME_VOICE;
-                               wf.subclass = AST_FORMAT_ULAW;
-                               wf.offset = AST_FRIENDLY_OFFSET;
-                               wf.mallocd = 0;
-                               wf.data = tone_block;
-                               wf.datalen = TONE_BLOCK_SIZE;
-                               /* make this tone block */
-                               make_tone_block(tone_block,480.0,620.0,&x);
-                               wf.timelen = wf.datalen / 8;
-                               if (ast_write(chan, &wf)) 
-                               {
-                                       ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
-                                       LOCAL_USER_REMOVE(u);
-                                       return -1;
-                               }
-                               FD_ZERO(&readfds);
-                               FD_SET(chan->fds[0],&readfds);
-                                 /* if no read avail, do send again */
-                               if (select(chan->fds[0] + 1,&readfds,NULL,
-                                       NULL,&notime) < 1) continue;
-                                 /* read frame */
-                               f = ast_read(chan);
-                               if (f == NULL) 
-                               {
-                                       LOCAL_USER_REMOVE(u);
-                                       return -1;
-                               }
-                               if ((f->frametype == AST_FRAME_CONTROL) &&
-                                   (f->subclass == AST_CONTROL_HANGUP))
-                               {
-                                       ast_frfree(f);
-                                       LOCAL_USER_REMOVE(u);
-                                       return -1;
-                               }
-                               ast_frfree(f);
-                       }
-                       for(i = 0; i < 10; i++)
-                       {
-                               do gettimeofday(&now,NULL);
-                               while (lastout.tv_sec && 
-                                       (ms_diff(&now,&lastout) < 25)) ;
-                               lastout.tv_sec = now.tv_sec;
-                               lastout.tv_usec = now.tv_usec;
-                               wf.frametype = AST_FRAME_VOICE;
-                               wf.subclass = AST_FORMAT_ULAW;
-                               wf.offset = AST_FRIENDLY_OFFSET;
-                               wf.mallocd = 0;
-                               wf.data = sil_block;
-                               wf.datalen = TONE_BLOCK_SIZE;
-                               wf.timelen = wf.datalen / 8;
-                               if (ast_write(chan, &wf)) 
-                               {
-                                       ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
-                                       LOCAL_USER_REMOVE(u);
-                                       return -1;
-                               }
-                               FD_ZERO(&readfds);
-                               FD_SET(chan->fds[0],&readfds);
-                                 /* if no read avail, do send again */
-                               if (select(chan->fds[0] + 1,&readfds,NULL,
-                                       NULL,&notime) < 1) continue;
-                                 /* read frame */
-                               f = ast_read(chan);
-                               if (f == NULL) 
-                               {
-                                       LOCAL_USER_REMOVE(u);
-                                       return -1;
-                               }
-                               if ((f->frametype == AST_FRAME_CONTROL) &&
-                                   (f->subclass == AST_CONTROL_HANGUP))
-                               {
-                                       ast_frfree(f);
-                                       LOCAL_USER_REMOVE(u);
-                                       return -1;
-                               }
-                               ast_frfree(f);
-                       }
+                       if (chan->callerid) free(chan->callerid);
+                       chan->callerid = strdup(ourcallerid);
                }
+               strncpy(chan->exten, exten, sizeof(chan->exten) - 1);
+               strncpy(chan->context, ourcontext, sizeof(chan->context) - 1);
+               strncpy(chan->accountcode, acctcode, sizeof(chan->accountcode) - 1);
+               chan->priority = 0;
+               ast_cdr_init(chan->cdr,chan);
+               LOCAL_USER_REMOVE(u);
+               return 0;
+       }
+
+reorder:
+
+       ast_indicate(chan,AST_CONTROL_CONGESTION);
+       /* something is invalid, give em reorder for several seconds */
+       time(&rstart);
+       while(time(NULL) < rstart + 10)
+       {
+               if (ast_waitfor(chan, -1) < 0)
+                       break;
+               f = ast_read(chan);
+               if (!f)
+                       break;
+               ast_frfree(f);
        }
+       ast_playtones_stop(chan);
+       LOCAL_USER_REMOVE(u);
+       return -1;
 }
 
 int unload_module(void)
@@ -395,7 +338,7 @@ int usecount(void)
        return res;
 }
 
-char *key()
+char *key(void)
 {
        return ASTERISK_GPL_KEY;
 }