#!/usr/bin/perl # Asterisk --> Jajah Gateway # copyright 2006 nikolaus hammler # licensed under GNU GPL use strict; use LWP::UserAgent; #use LWP::Debug qw(+); use HTML::Parser; use HTTP::Cookies; use HTTP::Request::Common qw(POST); use Asterisk::AGI; my $AGI = new Asterisk::AGI; my %input = $AGI->ReadParse(); if($ARGV[0] eq "in") { print STDERR "Dialin mode\n"; my $exten = $input{'callerid'}; my $id = $AGI->get_variable("DB(jajah/$exten)"); if($id) { print STDERR "Yes, our callback. exten=$exten, id=$id\n"; $AGI->exec('DBDel', "jajah/$exten"); if(time() - $id < 120) { $AGI->exec('Answer'); $AGI->exec('MeetMe', "$id|Aqd"); $AGI->exec('Hangup'); } else { print STDERR "Warning: time's up!\n"; exit 0; } } else { print STDERR "Maybe not our callback, ignoring\n"; exit 0; } } elsif($ARGV[0] ne "out") { print STDERR "First argument (mode) must either be 'in' or 'out'\n"; $AGI->exec('Set', 'JAJAH_STATUS=8'); exit 0; } print STDERR "Dialout mode\n"; my $UserAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rev:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'; my $ua = LWP::UserAgent->new(); $ua->agent($UserAgent); $ua->cookie_jar(HTTP::Cookies->new); my $username = $AGI->get_variable('JAJAH_USER'); my $password = $AGI->get_variable('JAJAH_PASS'); my $jajahnum = $AGI->get_variable('JAJAH_NUMBER'); print STDERR "Jajah Username: $username\n"; print STDERR "Jajah Password: $password\n"; print STDERR "Jajah Number: $jajahnum\n"; if(!$username) { print STDERR "No username given. Please set variable JAJAH_USER\n"; $AGI->exec('Set', 'JAJAH_STATUS=1'); exit 0; } if(!$password) { print STDERR "No password given. Please set variable JAJAH_PASS\n"; $AGI->exec('Set', 'JAJAH_STATUS=2'); exit 0; } if(!$jajahnum) { print STDERR "No jajah number given. Please set JAJAH_NUMBER\n"; $AGI->exec('Set', 'JAJAH_STATUS=6'); exit 0; } my $exten = $input{'agi_extension'}; if($ARGV[1] ne "") { $exten = $ARGV[1]; } print STDERR "ToDial: $exten\n"; if(!$exten) { print STDERR "Don't know what to dial, sorry\n"; $AGI->exec('Set', 'JAJAH_STATUS=3'); exit 0; } my $area = '43'; my $number = substr($exten, 1); if(substr($exten, 0, 2) eq '00') { # fixxme: Was is wenn vorwahl 3-stellig? $area = substr($exten, 2, 2); $number = substr($exten, 4); } elsif(substr($exten, 0, 1) ne '0') { print STDERR "Wrong format. Pleas use prefix 0 for national and prefix 00 for international calls\n"; $AGI->exec('Set', 'JAJAH_STATUS=5'); exit 0; } $AGI->exec('Answer'); $AGI->exec('Playtones', 'ring'); my $html = &PostFormInHTML('http://www.jajah.com/mini/login.aspx', 'theForm', { 'ctl00$MainContent$Email' => $username, 'ctl00$MainContent$Password' => $password}); if($html =~ /Successfully logged in/) { print STDERR "Login success. Trying to dial...\n"; $html = &PostFormInHTML('http://www.jajah.com/mini/member.aspx?', #$html = &PostFormInHTML('http://www.google.com', 'theForm', {'mynumber' => $jajahnum, 'ctl00$MainContent$CallTo$minidialcode' => $area, 'ctl00$MainContent$CallTo$mininumber' => $number}); if($html =~ /Active call/) { print STDERR "Everything seems fine, callback should come shortly\n"; $AGI->exec('Set', 'JAJAH_STATUS=0'); my $id = time(); $AGI->exec('Set', "DB(jajah/$exten)=$id"); $AGI->exec('StopPlaytones'); $AGI->exec('MeetMe', "$id|1Mdqwx"); $AGI->exec('Hangup'); exit 0; } else { if(open(DEBUG, ">/tmp/asterisk_jajah_debug.html")) { print DEBUG $html; close(DEBUG); } print STDERR "Call failed. Maybe you can find debug info in /tmp/asterisk_jajah_debug.html\n"; $AGI->exec('Set', 'JAJAH_STATUS=7'); $AGI->exec('StopPlaytones'); } } else { print STDERR "Login failed\n"; $AGI->exec('Set', 'JAJAH_STATUS=4'); $AGI->exec('StopPlaytones'); exit 0; } sub WebRequest() { my ($url) = @_; my $req; my $res; $req = HTTP::Request->new('GET', $url); $res = $ua->request($req); if($res->is_error()) { return ""; } return $res->content(); } sub WebPost($;\%;$;) { my ($url, $data, $referer) = @_; #my $ua; my $req; my $res; my $cookie; $req = POST $url, $data; if($referer ne "") { $req->referer($referer); } $res = $ua->request($req); if($res->is_error()) { return ""; } if(exists($res->headers->{'location'})) { return &WebRequest(&UrlGlue($res->headers->{'location'}, $url)); } return $res->content(); } sub UrlGlue() { my ($url, $orig) = @_; my $LastDir = substr($orig, 0, rindex($orig, "/")); # Get server, if no complete URI is given... my $Server = substr($orig, 0, index(substr($orig, 7), "/")+7); # Niki, 051104 # one geht nicht, hat offensichtlich auf https umgestellt. # handle also also https URLs...hoffentlich if(substr($orig, 0, 5) eq "https") { $Server = substr($orig, 0, index(substr($orig, 8), "/")+8); } #print "URL: '$url'\n"; #print "LastDir: '$LastDir'\n"; #print "Server: '$Server'\n"; if(substr($url, 0, 1) eq "/") { # root on server, just add the server! $url = $Server . $url; } elsif(substr($url, 0, 4) ne "http") { # complete relative path... $url = $LastDir . "/" . $url; } return $url; } sub GetFilename() { my ($url) = @_; return substr($url, rindex($url, "/")+1); } ################################### FORMULAR FUNKTIONEN ################################ # Diese Funktionen erlauben es, einfach Zugriff auf ein HTML-Formular zu erlangen und # abzuschicken, ohne es extra parsen zu muessen my $Form_Name = ""; # Haelt den Namen des Formulars my $Form_Jump = 0; # Kann auch gegeben viele Formulare ueberspringen! my $Form_Method = ""; # Sendemethode (GET, POST etc) my $Form_Action = ""; # URL, auf die das Formular geschickt werden soll my $Form_isOur = 0; # BOOL: Ist true, wenn wir in unserem Formular sind my $Form_Break = 0; # BOOL: Ist true, wenn wir komplett mit unserem Formular fertig sind # 10.05.2004 my $Form_Submit = ""; # Welches Submitfeld wird genommen? Wenn leer: Standard, sonst das was da drin steht my $Form_isSubmit = 0; # BOOL: true, wenn es bereits gefunden wurde! my $Form_SubmitRegexp = 0; # BOOL: Soll mit Hilfe von if ~ gesucht werden (falls z.B. auch der Name Parameter teilweise dynamisch erstellt wurde. Dann dem Parameter ein '~' vorstellen my @Form_Elems = (); # Haelt alle Formularelemente my $Form_Elems_cnt = 0; # ...und die Anzahl davon my $Form_isTextarea = 0; # BOOL: Befinden wir uns in einer Textarea? my $Form_isSelect = 0; # BOOL: Befinden wir uns in einer Combobox? my $Form_CatchSelect = 0; # BOOL: falls keine value="" gegeben ist, # muss zwischen