asterisk/wakeup.agi

changeset 202
f29abea29121
child 319
8ad5bb570338
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/asterisk/wakeup.agi	Mon Apr 27 12:19:05 2009 +0200
     1.3 @@ -0,0 +1,733 @@
     1.4 +#! @l_prefix@/bin/perl
     1.5 +#
     1.6 +# wakeup.agi 1.1
     1.7 +#
     1.8 +# A wakeup agi script for Asterisk
     1.9 +#
    1.10 +# Copyright (C) 2007
    1.11 +#
    1.12 +# Jonas Arndt <jonas_arndt@comcast.net>
    1.13 +#
    1.14 +# This program is free software, distributed under the terms of the
    1.15 +# GNU General Public License v2.
    1.16 +#
    1.17 +#
    1.18 +# This program is free software: you can redistribute it and/or modify
    1.19 +# it under the terms of the GNU General Public License as published by
    1.20 +# the Free Software Foundation, either version 3 of the License, or
    1.21 +# (at your option) any later version.
    1.22 +#
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 +# GNU General Public License for more details.
    1.27 +#
    1.28 +# You should have received a copy of the GNU General Public License
    1.29 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.30 +#
    1.31 +
    1.32 +use strict;
    1.33 +use Time::Local;
    1.34 +$|=1;
    1.35 +# Setup some variables
    1.36 +my %AGI; my $DEBUG=0;
    1.37 +# Some constants
    1.38 +my $OUTDIR="@l_prefix@/var/asterisk/spool/outgoing";
    1.39 +my $WAKEDIR="@l_prefix@/var/asterisk/spool/tmp";
    1.40 +my $debugfile="@l_prefix@/var/asterisk/spool/tmp/wakeup.log";
    1.41 +my $DEBUGOUT = "filehandle";
    1.42 +my $CALL = "filehandle";
    1.43 +my $TOUCH = "/usr/bin/touch";
    1.44 +
    1.45 +############ check_result ##########
    1.46 +# Use this to check the result of  #
    1.47 +# a sent command                   #
    1.48 +# I pretty much stole this from    #
    1.49 +# the regular agi-test.agi         #
    1.50 +####################################
    1.51 +sub checkresult {
    1.52 +    my ($res) = @_;
    1.53 +    my $retval;
    1.54 +    chomp $res;
    1.55 +    if ($res =~ /^200/) {
    1.56 +        $res =~ /result=(-?\d+)/;
    1.57 +        if (!length($1)) {
    1.58 +            print DEBUGOUT "FAIL ($res)\n";
    1.59 +            exit(1);
    1.60 +        } elsif ($DEBUG=1) {
    1.61 +            print DEBUGOUT "PASS ($1)\n";
    1.62 +        }
    1.63 +    } else {
    1.64 +        print STDERR "FAIL (unexpected result '$res')\n";
    1.65 +        exit(1);
    1.66 +    }
    1.67 +}
    1.68 +
    1.69 +
    1.70 +############ send_file #############
    1.71 +# Use this to send a wave file on  #
    1.72 +# the channel                      #
    1.73 +#                                  #
    1.74 +####################################
    1.75 +sub send_file {
    1.76 +    my ($myfile) = @_;
    1.77 +    chomp($myfile);
    1.78 +    if ($DEBUG == 1 ) {
    1.79 +        print DEBUGOUT "Sending stream $myfile \n";
    1.80 +    }
    1.81 +    print "STREAM FILE $myfile \"0123456789\"\n";
    1.82 +    my $result = <STDIN>;
    1.83 +    &checkresult($result);
    1.84 +    $result =~ /result=(-?\d+)/;
    1.85 +    return $1;
    1.86 +}
    1.87 +
    1.88 +############ hangup  ###############
    1.89 +# Use this to hand up a channel    #
    1.90 +# the channel                      #
    1.91 +#                                  #
    1.92 +####################################
    1.93 +sub hangup {
    1.94 +    if ($DEBUG == 1 ) {
    1.95 +        print DEBUGOUT "Hanging up \n";
    1.96 +    }
    1.97 +    print "HANGUP \"\" \n";
    1.98 +    my $result = <STDIN>;
    1.99 +    &checkresult($result);
   1.100 +}
   1.101 +
   1.102 +############ say_number ############
   1.103 +# Use this to say a number         #
   1.104 +# over the channel                 #
   1.105 +#                                  #
   1.106 +####################################
   1.107 +sub say_number {
   1.108 +    my ($mynumber) = @_;
   1.109 +    chomp($mynumber);
   1.110 +    if ($DEBUG == 1 ) {
   1.111 +        print DEBUGOUT "Saying number $mynumber \n";
   1.112 +    }
   1.113 +    print "SAY NUMBER $mynumber \"0123456789\"\n";
   1.114 +    my $result = <STDIN>;
   1.115 +    &checkresult($result);
   1.116 +    $result =~ /result=(-?\d+)/;
   1.117 +    return $1;
   1.118 +}
   1.119 +
   1.120 +############ say_digits ############
   1.121 +# Use this to say a digits         #
   1.122 +# over the channel                 #
   1.123 +#                                  #
   1.124 +####################################
   1.125 +sub say_digits {
   1.126 +    my ($mynumber) = @_;
   1.127 +    chomp($mynumber);
   1.128 +    if ($DEBUG == 1 ) {
   1.129 +        print DEBUGOUT "Saying digits $mynumber \n";
   1.130 +    }
   1.131 +    print "SAY DIGITS $mynumber \"0123456789\"\n";
   1.132 +    my $result = <STDIN>;
   1.133 +    &checkresult($result);
   1.134 +}
   1.135 +
   1.136 +############ get_choice ############
   1.137 +# Use this to receive a DTMF       #
   1.138 +# choice from the channel          #
   1.139 +#                                  #
   1.140 +####################################
   1.141 +sub get_choice {
   1.142 +    if ($DEBUG == 1 ) {
   1.143 +        print DEBUGOUT "Getting choice \n";
   1.144 +    }
   1.145 +    print "WAIT FOR DIGIT 15000\n";
   1.146 +    my $result = <STDIN>;
   1.147 +    &checkresult($result);
   1.148 +    $result =~ /result=(-?\d+)/;
   1.149 +    return $1;
   1.150 +}
   1.151 +
   1.152 +############ answer  ###############
   1.153 +# Anser the channel                #
   1.154 +#                                  #
   1.155 +####################################
   1.156 +sub answer {
   1.157 +    if ($DEBUG == 1 ) {
   1.158 +        print DEBUGOUT "Answering the channel \n";
   1.159 +    }
   1.160 +    print "ANSWER\n";
   1.161 +    my $result = <STDIN>;
   1.162 +    &checkresult($result);
   1.163 +    $result =~ /result=(-?\d+)/;
   1.164 +    return $1;
   1.165 +}
   1.166 +
   1.167 +######## get_data ##################
   1.168 +# Feed with (file, maxnumbers)     #
   1.169 +# where file is the sound file     #
   1.170 +# to be played and maxnumbers is   #
   1.171 +# the maximum amount of digits to  #
   1.172 +# allow in the answer              #
   1.173 +####################################
   1.174 +sub get_data {
   1.175 +    my @mydata = @_;
   1.176 +    my $myfile = $mydata[0];
   1.177 +    my $mymax = $mydata[1];
   1.178 +    if ($DEBUG == 1 ) {
   1.179 +        print DEBUGOUT "Getting data \n";
   1.180 +    }
   1.181 +    print "GET DATA $myfile 15000 $mymax \n";
   1.182 +    my $result = <STDIN>;
   1.183 +    &checkresult($result);
   1.184 +    $result =~ /result=(-?\d+)/;
   1.185 +    return $1;
   1.186 +}
   1.187 +
   1.188 +###### check_outstanding_calls #####
   1.189 +# Are there any outstanding wakeup #
   1.190 +# calls for this extensions?       #
   1.191 +# Pass the extension to the        #
   1.192 +# function. The function returns   #
   1.193 +# a list of files                  #
   1.194 +####################################
   1.195 +sub check_outstanding_calls {
   1.196 +    my $myext = @_;
   1.197 +    #opendir DIR, $WAKEDIR;
   1.198 +    opendir DIR, $OUTDIR;
   1.199 +    my @files = grep {/$myext/} readdir(DIR);
   1.200 +    closedir DIR;
   1.201 +    return @files;
   1.202 +}
   1.203 +
   1.204 +######## get_extension #############
   1.205 +# Receive the AIG variable and     #
   1.206 +# return the extension             #
   1.207 +####################################
   1.208 +sub get_extension {
   1.209 +    my (@aig) = @_;
   1.210 +        if ($aig[11] == '') {
   1.211 +        print STDERR "No extension found in function get_exension \n";
   1.212 +        return "FAIL";
   1.213 +    }
   1.214 +    my $myext = $aig[11];
   1.215 +    return $myext;
   1.216 +}
   1.217 +
   1.218 +######## get_context ###############
   1.219 +# Receive the AIG variable and     #
   1.220 +# return the context               #
   1.221 +####################################
   1.222 +sub get_context {
   1.223 +    my (@aig) = @_;
   1.224 +        if ($aig[8] == '') {
   1.225 +        print STDERR "No extension found in function get_exension \n";
   1.226 +        return "FAIL";
   1.227 +    }
   1.228 +    my $mycont = $aig[8];
   1.229 +    return $mycont;
   1.230 +}
   1.231 +
   1.232 +########### get_clid ###############
   1.233 +# Receive the AIG variable and     #
   1.234 +# return the clid                  #
   1.235 +####################################
   1.236 +sub get_clid {
   1.237 +    my (@aig) = @_;
   1.238 +        if ($aig[1] == '') {
   1.239 +        print STDERR "No clid found in function get_clid \n";
   1.240 +        return "FAIL";
   1.241 +    }
   1.242 +    my $myext = $aig[1];
   1.243 +    return $myext;
   1.244 +}
   1.245 +########### init_agi ###############
   1.246 +# Use this to initialize the AGI   #
   1.247 +# variable                         #
   1.248 +#                                  #
   1.249 +####################################
   1.250 +sub init_agi {
   1.251 +    while(<STDIN>) {
   1.252 +        chomp;
   1.253 +        last unless length($_);
   1.254 +        if (/^agi_(\w+)\:\s+(.*)$/) {
   1.255 +            $AGI{$1} = $2;
   1.256 +        }
   1.257 +    }
   1.258 +}
   1.259 +
   1.260 +############ ascii2num #############
   1.261 +# Removes 48 to get a number out   #
   1.262 +# of the asciss return             #
   1.263 +####################################
   1.264 +sub ascii2num {
   1.265 +    my ($asc) = @_;
   1.266 +    my $ret;
   1.267 +    $ret = $asc - 48;
   1.268 +    return $ret;
   1.269 +}
   1.270 +
   1.271 +
   1.272 +########### Welcome  ###############
   1.273 +# This is the welcome menu         #
   1.274 +#                                  #
   1.275 +####################################
   1.276 +sub welcome {
   1.277 +    my $ret = 0;
   1.278 +    $ret = &send_file("welcome");
   1.279 +    if ($ret == 0) {
   1.280 +        $ret = &send_file("for-wakeup-call");
   1.281 +    }
   1.282 +    if ($ret == 0) {
   1.283 +        $ret = &send_file("press-1");
   1.284 +    }
   1.285 +    if ($ret == 0) {
   1.286 +        $ret = &send_file("for-a-list-of");
   1.287 +    }
   1.288 +    if ($ret == 0) {
   1.289 +        $ret = &send_file("or");
   1.290 +    }
   1.291 +    if ($ret == 0) {
   1.292 +        $ret = &send_file("to-cancel-wakeup");
   1.293 +    }
   1.294 +    if ($ret != 0) {
   1.295 +        $ret = &ascii2num($ret);
   1.296 +    }
   1.297 +    if ($ret == 0) {
   1.298 +        $ret = &get_data("press-2",1);
   1.299 +    }
   1.300 +    if ($ret == 1) {
   1.301 +        $ret = &schedule_new();
   1.302 +    } elsif ($ret == 2) {
   1.303 +        &manage_calls();
   1.304 +    } else {
   1.305 +        $ret = &send_file("goodbye");
   1.306 +    }
   1.307 +}
   1.308 +
   1.309 +######### manage_calls #############
   1.310 +# This is what is called if you    #
   1.311 +# want to manage already scheduled #
   1.312 +# wakeup calls                     #
   1.313 +####################################
   1.314 +
   1.315 +sub manage_calls {
   1.316 +    my $checker = "false";
   1.317 +    my @calls;
   1.318 +    my $del;
   1.319 +    #my $ret;
   1.320 +    my $hours;
   1.321 +    my $minutes;
   1.322 +    # Send out a welcome message and ask for return
   1.323 +    @calls = &check_outstanding_calls($AGI{callerid});
   1.324 +    if ($#calls + 1 == 0) {
   1.325 +        $del = &send_file("not-rqsted-wakeup");
   1.326 +        $del = &send_file("goodbye");
   1.327 +    } else {
   1.328 +        foreach (@calls) {
   1.329 +            $del = 0;
   1.330 +            my $wakefile = $_;
   1.331 +            my @wakeup = split /\./, $_;
   1.332 +            my $time = $wakeup[0];
   1.333 +            $_ = $time;
   1.334 +            /(^[0-9][0-9])/;
   1.335 +            my $hours = $1;
   1.336 +            /^[0-9][0-9]([0-9][0-9])/;
   1.337 +            my $minutes = $1;
   1.338 +            $del = &send_file("rqsted-wakeup-for");
   1.339 +            if ($del == 0) {
   1.340 +                $del = &say_number($hours);
   1.341 +            }
   1.342 +            if ($del == 0) {
   1.343 +                if ($minutes >= 10 ) {
   1.344 +                    $del = &say_number($minutes);
   1.345 +                } elsif ($minutes > 0 && $minutes < 10) {
   1.346 +                    $del = &send_file("digits/oh");
   1.347 +                    $del = &say_number($minutes);
   1.348 +                }
   1.349 +            }
   1.350 +            if ($del == 0) {
   1.351 +                $del = &send_file("digits/oclock");
   1.352 +            }
   1.353 +            if ($del == 0) {
   1.354 +                $del = &send_file("to-cancel-wakeup");
   1.355 +            }
   1.356 +            if ($del == 0) {
   1.357 +                $del = &send_file("press-1");
   1.358 +            }
   1.359 +            if ($del == 0) {
   1.360 +                $del = &send_file("otherwise-press");
   1.361 +            }
   1.362 +            if ($del != 0) {
   1.363 +                $del = &ascii2num($del);
   1.364 +            }
   1.365 +            if ($del == 0) {
   1.366 +                $del = &get_data("digits/2",1);
   1.367 +            }
   1.368 +            if ($del == 1) {
   1.369 +                my @sysargs = ("rm", "-f", "$WAKEDIR/$wakefile", "$OUTDIR/$wakefile");
   1.370 +                system(@sysargs) == 0
   1.371 +                or die "system @sysargs failed: $?";
   1.372 +                $del = &send_file("cancelled");
   1.373 +            }
   1.374 +        }
   1.375 +        $del = &send_file("goodbye");
   1.376 +    }
   1.377 +
   1.378 +}
   1.379 +
   1.380 +######## schedule_new ##############
   1.381 +# This is the menu to schedule a   #
   1.382 +# a new wakeup call                #
   1.383 +####################################
   1.384 +sub schedule_new {
   1.385 +    my $checker = "false";
   1.386 +    my $ret_var;
   1.387 +    my $ret_dummy = 0;
   1.388 +    my $time;
   1.389 +    my $perm;
   1.390 +    my $file;
   1.391 +    my $calltype;
   1.392 +    my $extension;
   1.393 +    my $context;
   1.394 +    my $hours;
   1.395 +    my $minutes;
   1.396 +    if ($DEBUG == 1 ) {
   1.397 +        print DEBUGOUT "From schedule_new\n";
   1.398 +    }
   1.399 +    while ( $checker eq "false" ) {
   1.400 +        $ret_var = &send_file("to-rqst-wakeup-call");
   1.401 +        if ($ret_var != 0) {
   1.402 +            my $tmp = &get_data("silence/1",3);
   1.403 +            $ret_var = &ascii2num($ret_var);
   1.404 +            $ret_var = $ret_var . $tmp;
   1.405 +        } else {
   1.406 +            $ret_var = &get_data("enter-a-time",4);
   1.407 +        }
   1.408 +#        if ($ret_var < 1300 && $ret_var >= 0100) {
   1.409 +#            my $pm = &get_data("1-for-am-2-for-pm",1);
   1.410 +#            if ($pm == 2 && $ret_var <= 1159) {
   1.411 +#                $ret_var = $ret_var + 1200;
   1.412 +#                $checker = "true";
   1.413 +#            } elsif ($pm == 1 && $ret_var > 1159) {
   1.414 +#                $ret_var = $ret_var - 1200;
   1.415 +#                # Fix the zero
   1.416 +#                $ret_var = "00" . $ret_var;
   1.417 +#                $checker = "true";
   1.418 +#            } else {
   1.419 +#                $checker = "true";
   1.420 +#            }
   1.421 +#        } elsif ($ret_var > 2359) {
   1.422 +#            $ret_dummy = &send_file("please-try-again");
   1.423 +#        } else {
   1.424 +#            $checker = "true";
   1.425 +#        }
   1.426 +        if ($ret_var > 2359) {
   1.427 +            $ret_dummy = &send_file("please-try-again");
   1.428 +        } else {
   1.429 +            $checker = "true";
   1.430 +        }
   1.431 +    }
   1.432 +    $perm = 0;
   1.433 +    $perm = &send_file("wakeup-for-one-time");
   1.434 +    if ($perm == 0) {
   1.435 +        $perm = &send_file("press-1");
   1.436 +    }
   1.437 +    if ($perm == 0) {
   1.438 +        $perm = &send_file("for-a-daily-wakeup-call");
   1.439 +    }
   1.440 +    if ($perm != 0) {
   1.441 +        $perm = &ascii2num($perm);
   1.442 +    }
   1.443 +    if ($perm == 0) {
   1.444 +        $perm = $perm = &get_data("press-2",1);
   1.445 +    }
   1.446 +    # Open the file and populate it with data
   1.447 +    $extension = $AGI{callerid};
   1.448 +    $context = $AGI{context};
   1.449 +    if ($perm == 2) {
   1.450 +        $file = "$WAKEDIR/$ret_var.perm.1.$extension.call";
   1.451 +        $calltype = "perm";
   1.452 +        open (CALL, '>', $file) or die "Cannot open call file for write :$!";
   1.453 +    } else {
   1.454 +        $file = "$WAKEDIR/$ret_var.temp.1.$extension.call";
   1.455 +        $calltype = "temp";
   1.456 +        open (CALL, '>', $file) or die "Cannot open call file for write :$!";
   1.457 +    }
   1.458 +    my $myprint = "channel: Local" . "/" . $extension . "@" . $context . "\n";
   1.459 +    print CALL $myprint;
   1.460 +    print CALL "maxretries: 3\n";
   1.461 +    print CALL "retrytime: 60\n";
   1.462 +    print CALL "waittime: 60\n";
   1.463 +    print CALL "callerid: \"AsterPBX Weckruf\" <$AGI{extension}>\n";
   1.464 +    print CALL "application: AGI\n";
   1.465 +    print CALL "data: wakeup|$ret_var.$calltype.1.$extension.call\n";
   1.466 +    close ($CALL);
   1.467 +    # Now touch the file
   1.468 +    # Get the time variable
   1.469 +    $time = get_time_string($ret_var);
   1.470 +    my @command = ("$TOUCH", "-t", "$time", "${file}");
   1.471 +    system(@command) == 0
   1.472 +        or die "system @command failed: $?";
   1.473 +    # Move it to the OUT directory
   1.474 +    my @command = ("mv", "${file}", "${OUTDIR}/");
   1.475 +    system(@command) == 0
   1.476 +        or die "system @command failed: $?";
   1.477 +
   1.478 +    # Stream out the wakeup
   1.479 +    $_ = $ret_var;
   1.480 +    /(^[0-9][0-9])/;
   1.481 +    my $hours = $1;
   1.482 +    /^[0-9][0-9]([0-9][0-9])/;
   1.483 +    my $minutes = $1;
   1.484 +    $ret_dummy = &send_file("rqsted-wakeup-for");
   1.485 +    $ret_dummy = &say_number($hours);
   1.486 +    if ($minutes >= 10 ) {
   1.487 +        &say_number($minutes);
   1.488 +    } elsif ($minutes > 0 && $minutes < 10) {
   1.489 +        &send_file("digits/oh");
   1.490 +        &say_number($minutes);
   1.491 +    }
   1.492 +    $ret_dummy = &send_file("digits/oclock");
   1.493 +    $ret_dummy = &send_file("goodbye");
   1.494 +    return $ret_var;
   1.495 +}
   1.496 +
   1.497 +######## get_time_string ###########
   1.498 +# This will return the time string #
   1.499 +# when inputing a string like      #
   1.500 +# hhmi                             #
   1.501 +####################################
   1.502 +sub get_time_string {
   1.503 +    my ($intime) = @_;
   1.504 +    my $minutes = substr($intime, 2, 4);
   1.505 +    my $hours = substr($intime, 0, 2);
   1.506 +    my $tmpepoch;
   1.507 +    my $day;
   1.508 +    my $month;
   1.509 +    my $ret_val;
   1.510 +    my $epoch = time();
   1.511 +    my @timedata = localtime($epoch);
   1.512 +    # Insert the minutes and hours from input
   1.513 +    $timedata[1] = $minutes;
   1.514 +    $timedata[2] = $hours;
   1.515 +    # Get tmpepoch
   1.516 +    $tmpepoch = timelocal(@timedata);
   1.517 +    #Now compare them
   1.518 +    if ($tmpepoch < $epoch) { # Means it is tomorrow
   1.519 +        $tmpepoch += 86400; # Add 24 hours
   1.520 +    }
   1.521 +    # Now get the new timedata
   1.522 +    my @timedata = localtime($tmpepoch);
   1.523 +    $minutes = $timedata[1];
   1.524 +    $hours = $timedata[2];
   1.525 +    $day = $timedata[3];
   1.526 +    $month = $timedata[4] + 1;
   1.527 +    #Correct the "First hour after midnight" problem
   1.528 +    if ($minutes < 10) {
   1.529 +        $minutes = "0" . $minutes;
   1.530 +    }
   1.531 +    if ($hours < 10) {
   1.532 +        $hours = "0" . $hours;
   1.533 +    }
   1.534 +    if ($day < 10) {
   1.535 +        $day = "0" . $day;
   1.536 +    }
   1.537 +    if ($month < 10) {
   1.538 +        $month = "0" . $month;
   1.539 +    }
   1.540 +    $ret_val = $month . $day . $hours . $minutes;
   1.541 +    return $ret_val;
   1.542 +}
   1.543 +
   1.544 +############ new_time ##############
   1.545 +# This will return the time string #
   1.546 +# with a time set 10 minute into   #
   1.547 +# the future                       #
   1.548 +# The string is                    #
   1.549 +# MMDDhhmi                         #
   1.550 +####################################
   1.551 +sub new_time {
   1.552 +    my ($input) = @_;
   1.553 +    my @timedata;
   1.554 +    my $minutes;
   1.555 +    my $hours;
   1.556 +    my $day;
   1.557 +    my $month;
   1.558 +    my $ret_val;
   1.559 +    my $epoc = time();
   1.560 +    if ($input eq "10m") {
   1.561 +        # add 10 minutes
   1.562 +        $epoc += 600;
   1.563 +        #$epoc += 120; #just for debugs
   1.564 +    } else {
   1.565 +        # add 24 hours
   1.566 +        $epoc += 86400;
   1.567 +    }
   1.568 +    @timedata = localtime($epoc);
   1.569 +    $minutes = $timedata[1];
   1.570 +    $hours = $timedata[2];
   1.571 +    $day = $timedata[3];
   1.572 +    $month = $timedata[4] + 1;
   1.573 +    #Correct the "First hour after midnight" problem
   1.574 +    if ($minutes < 10) {
   1.575 +        $minutes = "0" . $minutes;
   1.576 +    }
   1.577 +    if ($hours < 10) {
   1.578 +        $hours = "0" . $hours;
   1.579 +    }
   1.580 +    if ($day < 10) {
   1.581 +        $day = "0" . $day;
   1.582 +    }
   1.583 +    if ($month < 10) {
   1.584 +        $month = "0" . $month;
   1.585 +    }
   1.586 +    $ret_val = $month . $day . $hours . $minutes;
   1.587 +    return $ret_val;
   1.588 +}
   1.589 +
   1.590 +########### snooze  ################
   1.591 +# This is the menu to snooze the   #
   1.592 +# wakeup call                      #
   1.593 +####################################
   1.594 +sub snooze {
   1.595 +    my ($oldfile) = @_;
   1.596 +    my $newfile;
   1.597 +    my $extension;
   1.598 +    my $context;
   1.599 +    my @filestore = split (/\./, $oldfile);
   1.600 +    my @permstore = split (/\./, $oldfile);
   1.601 +    my $time;
   1.602 +    my $ret_var = 0;
   1.603 +    my $ret_dummy;
   1.604 +    my $myprint;
   1.605 +    # Answer the channel
   1.606 +    &answer();
   1.607 +    # Is this a reoccuring call, then add 24h
   1.608 +    if ($permstore[1] eq "perm") {
   1.609 +        $permstore[2] += 1; #Just to get a new file name
   1.610 +        $newfile = join(".",@permstore);
   1.611 +        $extension = $AGI{extension};
   1.612 +        $context = $AGI{context};
   1.613 +        # Open the file
   1.614 +        open (CALL, '>', "${WAKEDIR}/${newfile}") or die "Cannot open call file for write :$!";
   1.615 +        $myprint = "channel: Local" . "/" . $extension . "@" . $context . "\n";
   1.616 +            print CALL $myprint;
   1.617 +            print CALL "maxretries: 3\n";
   1.618 +                print CALL "retrytime: 60\n";
   1.619 +        print CALL "waittime: 60\n";
   1.620 +                print CALL "callerid: \"AsterPBX Weckruf\" <$AGI{callerid}>\n";
   1.621 +            print CALL "application: AGI\n";
   1.622 +            print CALL "data: wakeup|$newfile\n";
   1.623 +            close ($CALL);
   1.624 +        # Get a time 24h from now
   1.625 +        $time = &new_time("24h");
   1.626 +        # Touch the file with the new time
   1.627 +        my @command = ("$TOUCH", "-t", "$time", "${WAKEDIR}/${newfile}");
   1.628 +        system(@command) == 0
   1.629 +            or die "system @command failed: $?";
   1.630 +        # Now move it
   1.631 +        my @command = ("mv", "${WAKEDIR}/${newfile}", "${OUTDIR}/${newfile}");
   1.632 +        system(@command) == 0
   1.633 +            or die "system @command failed: $?";
   1.634 +    }
   1.635 +    #Replace the file name time with snooze
   1.636 +    $filestore[1] = "snooze";
   1.637 +    # Also add 10 minutes to the name
   1.638 +    $time = new_time("10m");
   1.639 +    $filestore[0] = substr($time, 4, 8);
   1.640 +    # Get the new file name
   1.641 +    $newfile = join(".",@filestore);
   1.642 +    $ret_var = &send_file("this-is-yr-wakeup-call");
   1.643 +    if ($ret_var == 0 ) {
   1.644 +        $ret_var = &send_file("to-confirm-wakeup");
   1.645 +    }
   1.646 +    if ($ret_var == 0 ) {
   1.647 +        $ret_var = &send_file("press-1");
   1.648 +    }
   1.649 +    if ($ret_var == 0 ) {
   1.650 +        $ret_var = &send_file("to-snooze-for");
   1.651 +    }
   1.652 +    if ($ret_var == 0 ) {
   1.653 +        $ret_var = &send_file("digits/10");
   1.654 +    }
   1.655 +    if ($ret_var == 0 ) {
   1.656 +        $ret_var = &send_file("minutes");
   1.657 +    }
   1.658 +    if ($ret_var != 0 ) {
   1.659 +        $ret_var = &ascii2num($ret_var);
   1.660 +    }
   1.661 +    if ($ret_var == 0 ) {
   1.662 +        $ret_var = &get_data("press-2",1);
   1.663 +    }
   1.664 +    if ($ret_var == 2 ) {
   1.665 +        # Populate some variables
   1.666 +        $time = &new_time("10m");
   1.667 +        $extension = $AGI{extension};
   1.668 +        $context = $AGI{context};
   1.669 +        # Open the file
   1.670 +        open (CALL, '>', "${WAKEDIR}/${newfile}") or die "Cannot open call file for write :$!";
   1.671 +        $myprint = "channel: Local" . "/" . $extension . "@" . $context . "\n";
   1.672 +            print CALL $myprint;
   1.673 +            print CALL "maxretries: 3\n";
   1.674 +                print CALL "retrytime: 60\n";
   1.675 +        print CALL "waittime: 60\n";
   1.676 +                print CALL "callerid: \"AsterPBX Weckruf\" <$AGI{callerid}>\n";
   1.677 +            print CALL "application: AGI\n";
   1.678 +            print CALL "data: wakeup|$newfile\n";
   1.679 +            close ($CALL);
   1.680 +        # Touch the file with the new time
   1.681 +        my @command = ("$TOUCH", "-t", "$time", "${WAKEDIR}/${newfile}");
   1.682 +        system(@command) == 0
   1.683 +            or die "system @command failed: $?";
   1.684 +        # Now move it
   1.685 +        my @command = ("mv", "${WAKEDIR}/${newfile}", "${OUTDIR}/${newfile}");
   1.686 +        system(@command) == 0
   1.687 +            or die "system @command failed: $?";
   1.688 +        $ret_dummy = &send_file("goodbye");
   1.689 +
   1.690 +    } elsif ($ret_var == 1) {
   1.691 +        $ret_dummy = &send_file("goodbye");
   1.692 +    } else {
   1.693 +        $ret_dummy = &send_file("goodbye");
   1.694 +    }
   1.695 +
   1.696 +    # Stream out the wakeup
   1.697 +    return 0;
   1.698 +}
   1.699 +
   1.700 +########### main program ###########
   1.701 +# Here goes the main program       #
   1.702 +#                                  #
   1.703 +####################################
   1.704 +
   1.705 +my $numargs = $#ARGV + 1;
   1.706 +if ($DEBUG == 1) {
   1.707 +    open (DEBUGOUT, '>', $debugfile) or die "Cannot open $debugfile for write :$!";
   1.708 +}
   1.709 +
   1.710 +# Start by reading in the stuff Asterisk is sending
   1.711 +&init_agi(); # Comment out in case of debug outside Asterisk
   1.712 +
   1.713 +# If DEBUG is set, dump the AGI variable
   1.714 +if ($DEBUG == 1) {
   1.715 +    foreach my $i (sort keys %AGI) {
   1.716 +        print DEBUGOUT " -- $i = $AGI{$i}\n";
   1.717 +    }
   1.718 +}
   1.719 +
   1.720 +if ( $numargs == 0 ) {
   1.721 +    &welcome();
   1.722 +    &hangup();
   1.723 +    exit(0);
   1.724 +} elsif ( $ARGV[0] eq "move" ) {
   1.725 +    &move();
   1.726 +    &hangup();
   1.727 +    exit(0);
   1.728 +} else {
   1.729 +    &snooze($ARGV[0]);
   1.730 +    &hangup();
   1.731 +    exit(0);
   1.732 +}
   1.733 +
   1.734 +if ($DEBUG ==1) {
   1.735 +    close $DEBUGOUT;
   1.736 +}

mercurial