asterisk/wakeup.agi

Mon, 28 Jan 2013 17:37:18 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 28 Jan 2013 17:37:18 +0100
changeset 758
a2c6460cfb16
parent 202
f29abea29121
permissions
-rw-r--r--

Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.

michael@202 1 #! @l_prefix@/bin/perl
michael@202 2 #
michael@202 3 # wakeup.agi 1.1
michael@202 4 #
michael@202 5 # A wakeup agi script for Asterisk
michael@202 6 #
michael@202 7 # Copyright (C) 2007
michael@202 8 #
michael@202 9 # Jonas Arndt <jonas_arndt@comcast.net>
michael@202 10 #
michael@202 11 # This program is free software, distributed under the terms of the
michael@202 12 # GNU General Public License v2.
michael@202 13 #
michael@202 14 #
michael@202 15 # This program is free software: you can redistribute it and/or modify
michael@202 16 # it under the terms of the GNU General Public License as published by
michael@202 17 # the Free Software Foundation, either version 3 of the License, or
michael@202 18 # (at your option) any later version.
michael@202 19 #
michael@202 20 # This program is distributed in the hope that it will be useful,
michael@202 21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@202 22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
michael@202 23 # GNU General Public License for more details.
michael@202 24 #
michael@202 25 # You should have received a copy of the GNU General Public License
michael@202 26 # along with this program. If not, see <http://www.gnu.org/licenses/>.
michael@202 27 #
michael@202 28
michael@202 29 use strict;
michael@202 30 use Time::Local;
michael@202 31 $|=1;
michael@202 32 # Setup some variables
michael@202 33 my %AGI; my $DEBUG=0;
michael@202 34 # Some constants
michael@202 35 my $OUTDIR="@l_prefix@/var/asterisk/spool/outgoing";
michael@202 36 my $WAKEDIR="@l_prefix@/var/asterisk/spool/tmp";
michael@202 37 my $debugfile="@l_prefix@/var/asterisk/spool/tmp/wakeup.log";
michael@202 38 my $DEBUGOUT = "filehandle";
michael@202 39 my $CALL = "filehandle";
michael@202 40 my $TOUCH = "/usr/bin/touch";
michael@202 41
michael@202 42 ############ check_result ##########
michael@202 43 # Use this to check the result of #
michael@202 44 # a sent command #
michael@202 45 # I pretty much stole this from #
michael@202 46 # the regular agi-test.agi #
michael@202 47 ####################################
michael@202 48 sub checkresult {
michael@202 49 my ($res) = @_;
michael@202 50 my $retval;
michael@202 51 chomp $res;
michael@202 52 if ($res =~ /^200/) {
michael@202 53 $res =~ /result=(-?\d+)/;
michael@202 54 if (!length($1)) {
michael@202 55 print DEBUGOUT "FAIL ($res)\n";
michael@202 56 exit(1);
michael@202 57 } elsif ($DEBUG=1) {
michael@202 58 print DEBUGOUT "PASS ($1)\n";
michael@202 59 }
michael@202 60 } else {
michael@202 61 print STDERR "FAIL (unexpected result '$res')\n";
michael@202 62 exit(1);
michael@202 63 }
michael@202 64 }
michael@202 65
michael@319 66 ############ send_file ####
michael@319 67 # Use this to send a wave #
michael@319 68 # file on the channel #
michael@319 69 ###########################
michael@202 70 sub send_file {
michael@202 71 my ($myfile) = @_;
michael@202 72 chomp($myfile);
michael@202 73 if ($DEBUG == 1 ) {
michael@202 74 print DEBUGOUT "Sending stream $myfile \n";
michael@202 75 }
michael@202 76 print "STREAM FILE $myfile \"0123456789\"\n";
michael@202 77 my $result = <STDIN>;
michael@202 78 &checkresult($result);
michael@202 79 $result =~ /result=(-?\d+)/;
michael@202 80 return $1;
michael@202 81 }
michael@202 82
michael@202 83 ############ hangup ###############
michael@319 84 # Use this to hang up a channel #
michael@202 85 ####################################
michael@202 86 sub hangup {
michael@202 87 if ($DEBUG == 1 ) {
michael@202 88 print DEBUGOUT "Hanging up \n";
michael@202 89 }
michael@202 90 print "HANGUP \"\" \n";
michael@202 91 my $result = <STDIN>;
michael@202 92 &checkresult($result);
michael@202 93 }
michael@202 94
michael@202 95 ############ say_number ############
michael@202 96 # Use this to say a number #
michael@202 97 # over the channel #
michael@202 98 # #
michael@202 99 ####################################
michael@202 100 sub say_number {
michael@202 101 my ($mynumber) = @_;
michael@202 102 chomp($mynumber);
michael@202 103 if ($DEBUG == 1 ) {
michael@202 104 print DEBUGOUT "Saying number $mynumber \n";
michael@202 105 }
michael@202 106 print "SAY NUMBER $mynumber \"0123456789\"\n";
michael@202 107 my $result = <STDIN>;
michael@202 108 &checkresult($result);
michael@202 109 $result =~ /result=(-?\d+)/;
michael@202 110 return $1;
michael@202 111 }
michael@202 112
michael@319 113 ############ say_digits ###################
michael@319 114 # Use this to say digits over the channel #
michael@319 115 ###########################################
michael@202 116 sub say_digits {
michael@202 117 my ($mynumber) = @_;
michael@202 118 chomp($mynumber);
michael@202 119 if ($DEBUG == 1 ) {
michael@202 120 print DEBUGOUT "Saying digits $mynumber \n";
michael@202 121 }
michael@202 122 print "SAY DIGITS $mynumber \"0123456789\"\n";
michael@202 123 my $result = <STDIN>;
michael@202 124 &checkresult($result);
michael@202 125 }
michael@202 126
michael@319 127 ######## get_choice ############
michael@319 128 # Use this to receive a DTMF #
michael@319 129 # choice from the channel #
michael@319 130 ################################
michael@202 131 sub get_choice {
michael@202 132 if ($DEBUG == 1 ) {
michael@202 133 print DEBUGOUT "Getting choice \n";
michael@202 134 }
michael@202 135 print "WAIT FOR DIGIT 15000\n";
michael@202 136 my $result = <STDIN>;
michael@202 137 &checkresult($result);
michael@202 138 $result =~ /result=(-?\d+)/;
michael@202 139 return $1;
michael@202 140 }
michael@202 141
michael@319 142 ###### answer ######
michael@319 143 # Anser the channel #
michael@319 144 #####################
michael@202 145 sub answer {
michael@202 146 if ($DEBUG == 1 ) {
michael@202 147 print DEBUGOUT "Answering the channel \n";
michael@202 148 }
michael@202 149 print "ANSWER\n";
michael@202 150 my $result = <STDIN>;
michael@202 151 &checkresult($result);
michael@202 152 $result =~ /result=(-?\d+)/;
michael@202 153 return $1;
michael@202 154 }
michael@202 155
michael@202 156 ######## get_data ##################
michael@202 157 # Feed with (file, maxnumbers) #
michael@202 158 # where file is the sound file #
michael@202 159 # to be played and maxnumbers is #
michael@202 160 # the maximum amount of digits to #
michael@202 161 # allow in the answer #
michael@202 162 ####################################
michael@202 163 sub get_data {
michael@202 164 my @mydata = @_;
michael@202 165 my $myfile = $mydata[0];
michael@202 166 my $mymax = $mydata[1];
michael@202 167 if ($DEBUG == 1 ) {
michael@202 168 print DEBUGOUT "Getting data \n";
michael@202 169 }
michael@202 170 print "GET DATA $myfile 15000 $mymax \n";
michael@202 171 my $result = <STDIN>;
michael@202 172 &checkresult($result);
michael@202 173 $result =~ /result=(-?\d+)/;
michael@202 174 return $1;
michael@202 175 }
michael@202 176
michael@202 177 ###### check_outstanding_calls #####
michael@202 178 # Are there any outstanding wakeup #
michael@202 179 # calls for this extensions? #
michael@202 180 # Pass the extension to the #
michael@202 181 # function. The function returns #
michael@202 182 # a list of files #
michael@202 183 ####################################
michael@202 184 sub check_outstanding_calls {
michael@202 185 my $myext = @_;
michael@202 186 #opendir DIR, $WAKEDIR;
michael@202 187 opendir DIR, $OUTDIR;
michael@202 188 my @files = grep {/$myext/} readdir(DIR);
michael@202 189 closedir DIR;
michael@202 190 return @files;
michael@202 191 }
michael@202 192
michael@202 193 ######## get_extension #############
michael@202 194 # Receive the AIG variable and #
michael@202 195 # return the extension #
michael@202 196 ####################################
michael@202 197 sub get_extension {
michael@202 198 my (@aig) = @_;
michael@202 199 if ($aig[11] == '') {
michael@202 200 print STDERR "No extension found in function get_exension \n";
michael@202 201 return "FAIL";
michael@202 202 }
michael@202 203 my $myext = $aig[11];
michael@202 204 return $myext;
michael@202 205 }
michael@202 206
michael@202 207 ######## get_context ###############
michael@202 208 # Receive the AIG variable and #
michael@202 209 # return the context #
michael@202 210 ####################################
michael@202 211 sub get_context {
michael@202 212 my (@aig) = @_;
michael@202 213 if ($aig[8] == '') {
michael@202 214 print STDERR "No extension found in function get_exension \n";
michael@202 215 return "FAIL";
michael@202 216 }
michael@202 217 my $mycont = $aig[8];
michael@202 218 return $mycont;
michael@202 219 }
michael@202 220
michael@202 221 ########### get_clid ###############
michael@202 222 # Receive the AIG variable and #
michael@202 223 # return the clid #
michael@202 224 ####################################
michael@202 225 sub get_clid {
michael@202 226 my (@aig) = @_;
michael@202 227 if ($aig[1] == '') {
michael@202 228 print STDERR "No clid found in function get_clid \n";
michael@202 229 return "FAIL";
michael@202 230 }
michael@202 231 my $myext = $aig[1];
michael@202 232 return $myext;
michael@202 233 }
michael@319 234
michael@319 235 ############### init_agi ##################
michael@319 236 # Use this to initialize the AGI variable #
michael@319 237 ###########################################
michael@202 238 sub init_agi {
michael@202 239 while(<STDIN>) {
michael@202 240 chomp;
michael@202 241 last unless length($_);
michael@202 242 if (/^agi_(\w+)\:\s+(.*)$/) {
michael@202 243 $AGI{$1} = $2;
michael@202 244 }
michael@202 245 }
michael@202 246 }
michael@202 247
michael@319 248 ######### ascii2num ##########
michael@319 249 # Removes 48 to get a number #
michael@319 250 # out of the asciss return #
michael@319 251 ##############################
michael@202 252 sub ascii2num {
michael@202 253 my ($asc) = @_;
michael@202 254 my $ret;
michael@202 255 $ret = $asc - 48;
michael@202 256 return $ret;
michael@202 257 }
michael@202 258
michael@319 259 ############# Welcome ##############
michael@202 260 # This is the welcome menu #
michael@202 261 ####################################
michael@202 262 sub welcome {
michael@202 263 my $ret = 0;
michael@202 264 $ret = &send_file("welcome");
michael@202 265 if ($ret == 0) {
michael@202 266 $ret = &send_file("for-wakeup-call");
michael@202 267 }
michael@202 268 if ($ret == 0) {
michael@202 269 $ret = &send_file("press-1");
michael@202 270 }
michael@202 271 if ($ret == 0) {
michael@202 272 $ret = &send_file("for-a-list-of");
michael@202 273 }
michael@202 274 if ($ret == 0) {
michael@202 275 $ret = &send_file("or");
michael@202 276 }
michael@202 277 if ($ret == 0) {
michael@202 278 $ret = &send_file("to-cancel-wakeup");
michael@202 279 }
michael@202 280 if ($ret != 0) {
michael@202 281 $ret = &ascii2num($ret);
michael@202 282 }
michael@202 283 if ($ret == 0) {
michael@202 284 $ret = &get_data("press-2",1);
michael@202 285 }
michael@202 286 if ($ret == 1) {
michael@202 287 $ret = &schedule_new();
michael@202 288 } elsif ($ret == 2) {
michael@202 289 &manage_calls();
michael@202 290 } else {
michael@202 291 $ret = &send_file("goodbye");
michael@202 292 }
michael@202 293 }
michael@202 294
michael@202 295 ######### manage_calls #############
michael@202 296 # This is what is called if you #
michael@202 297 # want to manage already scheduled #
michael@202 298 # wakeup calls #
michael@202 299 ####################################
michael@202 300 sub manage_calls {
michael@202 301 my $checker = "false";
michael@202 302 my @calls;
michael@202 303 my $del;
michael@202 304 #my $ret;
michael@202 305 my $hours;
michael@202 306 my $minutes;
michael@202 307 # Send out a welcome message and ask for return
michael@202 308 @calls = &check_outstanding_calls($AGI{callerid});
michael@202 309 if ($#calls + 1 == 0) {
michael@202 310 $del = &send_file("not-rqsted-wakeup");
michael@202 311 $del = &send_file("goodbye");
michael@202 312 } else {
michael@202 313 foreach (@calls) {
michael@202 314 $del = 0;
michael@202 315 my $wakefile = $_;
michael@202 316 my @wakeup = split /\./, $_;
michael@202 317 my $time = $wakeup[0];
michael@202 318 $_ = $time;
michael@202 319 /(^[0-9][0-9])/;
michael@202 320 my $hours = $1;
michael@202 321 /^[0-9][0-9]([0-9][0-9])/;
michael@202 322 my $minutes = $1;
michael@202 323 $del = &send_file("rqsted-wakeup-for");
michael@202 324 if ($del == 0) {
michael@202 325 $del = &say_number($hours);
michael@202 326 }
michael@202 327 if ($del == 0) {
michael@202 328 if ($minutes >= 10 ) {
michael@202 329 $del = &say_number($minutes);
michael@202 330 } elsif ($minutes > 0 && $minutes < 10) {
michael@202 331 $del = &send_file("digits/oh");
michael@202 332 $del = &say_number($minutes);
michael@202 333 }
michael@202 334 }
michael@202 335 if ($del == 0) {
michael@202 336 $del = &send_file("digits/oclock");
michael@202 337 }
michael@202 338 if ($del == 0) {
michael@202 339 $del = &send_file("to-cancel-wakeup");
michael@202 340 }
michael@202 341 if ($del == 0) {
michael@202 342 $del = &send_file("press-1");
michael@202 343 }
michael@202 344 if ($del == 0) {
michael@202 345 $del = &send_file("otherwise-press");
michael@202 346 }
michael@202 347 if ($del != 0) {
michael@202 348 $del = &ascii2num($del);
michael@202 349 }
michael@202 350 if ($del == 0) {
michael@202 351 $del = &get_data("digits/2",1);
michael@202 352 }
michael@202 353 if ($del == 1) {
michael@202 354 my @sysargs = ("rm", "-f", "$WAKEDIR/$wakefile", "$OUTDIR/$wakefile");
michael@202 355 system(@sysargs) == 0
michael@202 356 or die "system @sysargs failed: $?";
michael@202 357 $del = &send_file("cancelled");
michael@202 358 }
michael@202 359 }
michael@202 360 $del = &send_file("goodbye");
michael@202 361 }
michael@202 362 }
michael@202 363
michael@319 364 ######## schedule_new ##########
michael@319 365 # This is the menu to schedule #
michael@319 366 # a new wakeup call #
michael@319 367 ################################
michael@202 368 sub schedule_new {
michael@202 369 my $checker = "false";
michael@202 370 my $ret_var;
michael@202 371 my $ret_dummy = 0;
michael@202 372 my $time;
michael@202 373 my $perm;
michael@202 374 my $file;
michael@202 375 my $calltype;
michael@202 376 my $extension;
michael@202 377 my $context;
michael@202 378 my $hours;
michael@202 379 my $minutes;
michael@202 380 if ($DEBUG == 1 ) {
michael@202 381 print DEBUGOUT "From schedule_new\n";
michael@202 382 }
michael@202 383 while ( $checker eq "false" ) {
michael@202 384 $ret_var = &send_file("to-rqst-wakeup-call");
michael@202 385 if ($ret_var != 0) {
michael@202 386 my $tmp = &get_data("silence/1",3);
michael@202 387 $ret_var = &ascii2num($ret_var);
michael@202 388 $ret_var = $ret_var . $tmp;
michael@202 389 } else {
michael@202 390 $ret_var = &get_data("enter-a-time",4);
michael@202 391 }
michael@202 392 # if ($ret_var < 1300 && $ret_var >= 0100) {
michael@202 393 # my $pm = &get_data("1-for-am-2-for-pm",1);
michael@202 394 # if ($pm == 2 && $ret_var <= 1159) {
michael@202 395 # $ret_var = $ret_var + 1200;
michael@202 396 # $checker = "true";
michael@202 397 # } elsif ($pm == 1 && $ret_var > 1159) {
michael@202 398 # $ret_var = $ret_var - 1200;
michael@202 399 # # Fix the zero
michael@202 400 # $ret_var = "00" . $ret_var;
michael@202 401 # $checker = "true";
michael@202 402 # } else {
michael@202 403 # $checker = "true";
michael@202 404 # }
michael@202 405 # } elsif ($ret_var > 2359) {
michael@202 406 # $ret_dummy = &send_file("please-try-again");
michael@202 407 # } else {
michael@202 408 # $checker = "true";
michael@202 409 # }
michael@202 410 if ($ret_var > 2359) {
michael@202 411 $ret_dummy = &send_file("please-try-again");
michael@202 412 } else {
michael@202 413 $checker = "true";
michael@202 414 }
michael@202 415 }
michael@202 416 $perm = 0;
michael@202 417 $perm = &send_file("wakeup-for-one-time");
michael@202 418 if ($perm == 0) {
michael@202 419 $perm = &send_file("press-1");
michael@202 420 }
michael@202 421 if ($perm == 0) {
michael@202 422 $perm = &send_file("for-a-daily-wakeup-call");
michael@202 423 }
michael@202 424 if ($perm != 0) {
michael@202 425 $perm = &ascii2num($perm);
michael@202 426 }
michael@202 427 if ($perm == 0) {
michael@202 428 $perm = $perm = &get_data("press-2",1);
michael@202 429 }
michael@202 430 # Open the file and populate it with data
michael@202 431 $extension = $AGI{callerid};
michael@202 432 $context = $AGI{context};
michael@202 433 if ($perm == 2) {
michael@202 434 $file = "$WAKEDIR/$ret_var.perm.1.$extension.call";
michael@202 435 $calltype = "perm";
michael@202 436 open (CALL, '>', $file) or die "Cannot open call file for write :$!";
michael@202 437 } else {
michael@202 438 $file = "$WAKEDIR/$ret_var.temp.1.$extension.call";
michael@202 439 $calltype = "temp";
michael@202 440 open (CALL, '>', $file) or die "Cannot open call file for write :$!";
michael@202 441 }
michael@202 442 my $myprint = "channel: Local" . "/" . $extension . "@" . $context . "\n";
michael@202 443 print CALL $myprint;
michael@202 444 print CALL "maxretries: 3\n";
michael@202 445 print CALL "retrytime: 60\n";
michael@202 446 print CALL "waittime: 60\n";
michael@202 447 print CALL "callerid: \"AsterPBX Weckruf\" <$AGI{extension}>\n";
michael@202 448 print CALL "application: AGI\n";
michael@202 449 print CALL "data: wakeup|$ret_var.$calltype.1.$extension.call\n";
michael@202 450 close ($CALL);
michael@202 451 # Now touch the file
michael@202 452 # Get the time variable
michael@202 453 $time = get_time_string($ret_var);
michael@202 454 my @command = ("$TOUCH", "-t", "$time", "${file}");
michael@202 455 system(@command) == 0
michael@202 456 or die "system @command failed: $?";
michael@202 457 # Move it to the OUT directory
michael@202 458 my @command = ("mv", "${file}", "${OUTDIR}/");
michael@202 459 system(@command) == 0
michael@202 460 or die "system @command failed: $?";
michael@202 461
michael@202 462 # Stream out the wakeup
michael@202 463 $_ = $ret_var;
michael@202 464 /(^[0-9][0-9])/;
michael@202 465 my $hours = $1;
michael@202 466 /^[0-9][0-9]([0-9][0-9])/;
michael@202 467 my $minutes = $1;
michael@202 468 $ret_dummy = &send_file("rqsted-wakeup-for");
michael@202 469 $ret_dummy = &say_number($hours);
michael@202 470 if ($minutes >= 10 ) {
michael@202 471 &say_number($minutes);
michael@202 472 } elsif ($minutes > 0 && $minutes < 10) {
michael@202 473 &send_file("digits/oh");
michael@202 474 &say_number($minutes);
michael@202 475 }
michael@202 476 $ret_dummy = &send_file("digits/oclock");
michael@202 477 $ret_dummy = &send_file("goodbye");
michael@202 478 return $ret_var;
michael@202 479 }
michael@202 480
michael@202 481 ######## get_time_string ###########
michael@202 482 # This will return the time string #
michael@319 483 # when inputing a string like hhmi #
michael@202 484 ####################################
michael@202 485 sub get_time_string {
michael@202 486 my ($intime) = @_;
michael@202 487 my $minutes = substr($intime, 2, 4);
michael@202 488 my $hours = substr($intime, 0, 2);
michael@202 489 my $tmpepoch;
michael@202 490 my $day;
michael@202 491 my $month;
michael@202 492 my $ret_val;
michael@202 493 my $epoch = time();
michael@202 494 my @timedata = localtime($epoch);
michael@202 495 # Insert the minutes and hours from input
michael@202 496 $timedata[1] = $minutes;
michael@202 497 $timedata[2] = $hours;
michael@202 498 # Get tmpepoch
michael@202 499 $tmpepoch = timelocal(@timedata);
michael@202 500 #Now compare them
michael@202 501 if ($tmpepoch < $epoch) { # Means it is tomorrow
michael@202 502 $tmpepoch += 86400; # Add 24 hours
michael@202 503 }
michael@202 504 # Now get the new timedata
michael@202 505 my @timedata = localtime($tmpepoch);
michael@202 506 $minutes = $timedata[1];
michael@202 507 $hours = $timedata[2];
michael@202 508 $day = $timedata[3];
michael@202 509 $month = $timedata[4] + 1;
michael@202 510 #Correct the "First hour after midnight" problem
michael@202 511 if ($minutes < 10) {
michael@202 512 $minutes = "0" . $minutes;
michael@202 513 }
michael@202 514 if ($hours < 10) {
michael@202 515 $hours = "0" . $hours;
michael@202 516 }
michael@202 517 if ($day < 10) {
michael@202 518 $day = "0" . $day;
michael@202 519 }
michael@202 520 if ($month < 10) {
michael@202 521 $month = "0" . $month;
michael@202 522 }
michael@202 523 $ret_val = $month . $day . $hours . $minutes;
michael@202 524 return $ret_val;
michael@202 525 }
michael@202 526
michael@319 527 ############ new_time ################
michael@319 528 # This will return the time string #
michael@319 529 # with a time set 10 minute into #
michael@319 530 # the future. The string is MMDDhhmi #
michael@319 531 ######################################
michael@202 532 sub new_time {
michael@202 533 my ($input) = @_;
michael@202 534 my @timedata;
michael@202 535 my $minutes;
michael@202 536 my $hours;
michael@202 537 my $day;
michael@202 538 my $month;
michael@202 539 my $ret_val;
michael@202 540 my $epoc = time();
michael@202 541 if ($input eq "10m") {
michael@202 542 # add 10 minutes
michael@202 543 $epoc += 600;
michael@202 544 #$epoc += 120; #just for debugs
michael@202 545 } else {
michael@202 546 # add 24 hours
michael@202 547 $epoc += 86400;
michael@202 548 }
michael@202 549 @timedata = localtime($epoc);
michael@202 550 $minutes = $timedata[1];
michael@202 551 $hours = $timedata[2];
michael@202 552 $day = $timedata[3];
michael@202 553 $month = $timedata[4] + 1;
michael@202 554 #Correct the "First hour after midnight" problem
michael@202 555 if ($minutes < 10) {
michael@202 556 $minutes = "0" . $minutes;
michael@202 557 }
michael@202 558 if ($hours < 10) {
michael@202 559 $hours = "0" . $hours;
michael@202 560 }
michael@202 561 if ($day < 10) {
michael@202 562 $day = "0" . $day;
michael@202 563 }
michael@202 564 if ($month < 10) {
michael@202 565 $month = "0" . $month;
michael@202 566 }
michael@202 567 $ret_val = $month . $day . $hours . $minutes;
michael@202 568 return $ret_val;
michael@202 569 }
michael@202 570
michael@319 571 ########### snooze ##########
michael@319 572 # This is the menu to snooze #
michael@319 573 # the wakeup call #
michael@319 574 ##############################
michael@202 575 sub snooze {
michael@202 576 my ($oldfile) = @_;
michael@202 577 my $newfile;
michael@202 578 my $extension;
michael@202 579 my $context;
michael@202 580 my @filestore = split (/\./, $oldfile);
michael@202 581 my @permstore = split (/\./, $oldfile);
michael@202 582 my $time;
michael@202 583 my $ret_var = 0;
michael@202 584 my $ret_dummy;
michael@202 585 my $myprint;
michael@202 586 # Answer the channel
michael@202 587 &answer();
michael@202 588 # Is this a reoccuring call, then add 24h
michael@202 589 if ($permstore[1] eq "perm") {
michael@202 590 $permstore[2] += 1; #Just to get a new file name
michael@202 591 $newfile = join(".",@permstore);
michael@202 592 $extension = $AGI{extension};
michael@202 593 $context = $AGI{context};
michael@202 594 # Open the file
michael@202 595 open (CALL, '>', "${WAKEDIR}/${newfile}") or die "Cannot open call file for write :$!";
michael@202 596 $myprint = "channel: Local" . "/" . $extension . "@" . $context . "\n";
michael@202 597 print CALL $myprint;
michael@202 598 print CALL "maxretries: 3\n";
michael@202 599 print CALL "retrytime: 60\n";
michael@202 600 print CALL "waittime: 60\n";
michael@202 601 print CALL "callerid: \"AsterPBX Weckruf\" <$AGI{callerid}>\n";
michael@202 602 print CALL "application: AGI\n";
michael@202 603 print CALL "data: wakeup|$newfile\n";
michael@202 604 close ($CALL);
michael@202 605 # Get a time 24h from now
michael@202 606 $time = &new_time("24h");
michael@202 607 # Touch the file with the new time
michael@202 608 my @command = ("$TOUCH", "-t", "$time", "${WAKEDIR}/${newfile}");
michael@202 609 system(@command) == 0
michael@202 610 or die "system @command failed: $?";
michael@202 611 # Now move it
michael@202 612 my @command = ("mv", "${WAKEDIR}/${newfile}", "${OUTDIR}/${newfile}");
michael@202 613 system(@command) == 0
michael@202 614 or die "system @command failed: $?";
michael@202 615 }
michael@202 616 #Replace the file name time with snooze
michael@202 617 $filestore[1] = "snooze";
michael@202 618 # Also add 10 minutes to the name
michael@202 619 $time = new_time("10m");
michael@202 620 $filestore[0] = substr($time, 4, 8);
michael@202 621 # Get the new file name
michael@202 622 $newfile = join(".",@filestore);
michael@202 623 $ret_var = &send_file("this-is-yr-wakeup-call");
michael@202 624 if ($ret_var == 0 ) {
michael@202 625 $ret_var = &send_file("to-confirm-wakeup");
michael@202 626 }
michael@202 627 if ($ret_var == 0 ) {
michael@202 628 $ret_var = &send_file("press-1");
michael@202 629 }
michael@202 630 if ($ret_var == 0 ) {
michael@202 631 $ret_var = &send_file("to-snooze-for");
michael@202 632 }
michael@202 633 if ($ret_var == 0 ) {
michael@202 634 $ret_var = &send_file("digits/10");
michael@202 635 }
michael@202 636 if ($ret_var == 0 ) {
michael@202 637 $ret_var = &send_file("minutes");
michael@202 638 }
michael@202 639 if ($ret_var != 0 ) {
michael@202 640 $ret_var = &ascii2num($ret_var);
michael@202 641 }
michael@202 642 if ($ret_var == 0 ) {
michael@202 643 $ret_var = &get_data("press-2",1);
michael@202 644 }
michael@202 645 if ($ret_var == 2 ) {
michael@202 646 # Populate some variables
michael@202 647 $time = &new_time("10m");
michael@202 648 $extension = $AGI{extension};
michael@202 649 $context = $AGI{context};
michael@202 650 # Open the file
michael@202 651 open (CALL, '>', "${WAKEDIR}/${newfile}") or die "Cannot open call file for write :$!";
michael@202 652 $myprint = "channel: Local" . "/" . $extension . "@" . $context . "\n";
michael@202 653 print CALL $myprint;
michael@202 654 print CALL "maxretries: 3\n";
michael@202 655 print CALL "retrytime: 60\n";
michael@202 656 print CALL "waittime: 60\n";
michael@202 657 print CALL "callerid: \"AsterPBX Weckruf\" <$AGI{callerid}>\n";
michael@202 658 print CALL "application: AGI\n";
michael@202 659 print CALL "data: wakeup|$newfile\n";
michael@202 660 close ($CALL);
michael@202 661 # Touch the file with the new time
michael@202 662 my @command = ("$TOUCH", "-t", "$time", "${WAKEDIR}/${newfile}");
michael@202 663 system(@command) == 0
michael@202 664 or die "system @command failed: $?";
michael@202 665 # Now move it
michael@202 666 my @command = ("mv", "${WAKEDIR}/${newfile}", "${OUTDIR}/${newfile}");
michael@202 667 system(@command) == 0
michael@202 668 or die "system @command failed: $?";
michael@202 669 $ret_dummy = &send_file("goodbye");
michael@202 670
michael@202 671 } elsif ($ret_var == 1) {
michael@202 672 $ret_dummy = &send_file("goodbye");
michael@202 673 } else {
michael@202 674 $ret_dummy = &send_file("goodbye");
michael@202 675 }
michael@202 676
michael@202 677 # Stream out the wakeup
michael@202 678 return 0;
michael@202 679 }
michael@202 680
michael@319 681 ######## main program ########
michael@319 682 # Here goes the main program #
michael@319 683 ##############################
michael@202 684 my $numargs = $#ARGV + 1;
michael@202 685 if ($DEBUG == 1) {
michael@202 686 open (DEBUGOUT, '>', $debugfile) or die "Cannot open $debugfile for write :$!";
michael@202 687 }
michael@202 688
michael@202 689 # Start by reading in the stuff Asterisk is sending
michael@202 690 &init_agi(); # Comment out in case of debug outside Asterisk
michael@202 691
michael@202 692 # If DEBUG is set, dump the AGI variable
michael@202 693 if ($DEBUG == 1) {
michael@202 694 foreach my $i (sort keys %AGI) {
michael@202 695 print DEBUGOUT " -- $i = $AGI{$i}\n";
michael@202 696 }
michael@202 697 }
michael@202 698
michael@202 699 if ( $numargs == 0 ) {
michael@202 700 &welcome();
michael@202 701 &hangup();
michael@202 702 exit(0);
michael@202 703 } elsif ( $ARGV[0] eq "move" ) {
michael@202 704 &move();
michael@202 705 &hangup();
michael@202 706 exit(0);
michael@202 707 } else {
michael@202 708 &snooze($ARGV[0]);
michael@202 709 &hangup();
michael@202 710 exit(0);
michael@202 711 }
michael@202 712
michael@202 713 if ($DEBUG ==1) {
michael@202 714 close $DEBUGOUT;
michael@202 715 }

mercurial