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