Permalink
Browse files

Added SSL support to wolfram.agi

  • Loading branch information...
1 parent 7d751e2 commit 7f86bb0f68aef3bb8b23c176910c3e4c43c417f1 @zaf committed Jan 23, 2012
Showing with 37 additions and 11 deletions.
  1. +0 −1 speech-recog.agi
  2. +14 −0 wolfram/README
  3. +23 −10 wolfram/wolfram.agi
View
@@ -189,7 +189,6 @@ if ($use_ssl) {
$ua = LWP::UserAgent->new;
}
-
$ua->agent("Mozilla/5.0 (X11; Linux) AppleWebKit/535.2 (KHTML, like Gecko)");
$ua->timeout($ua_timeout);
$uaresponse = $ua->post(
View
@@ -15,6 +15,9 @@ perl-XML::Simple XML API module for Perl.
An app ID from http://products.wolframalpha.com/api
Internet access in order to contact WolframAlpha and get the data.
+If you plan to use SSL you will need to install the 'IO-Socket-SSL' Perl module
+that implements an interface to SSL sockets.
+
------------
Installation
------------
@@ -56,6 +59,17 @@ exten => 1237,n,goto(record)
exten => 1237,n(end),Hangup()
+-----------------------
+Security Considerations
+-----------------------
+This script contacts Wolfram servers in order send the query data and get back the
+answers. If you feel that the data you exchange with Wolfram are sensitive or you want to
+make sure that no 3rd party can eavesdrop your communication with the Wolfram service
+it is advised to enable the use of SSL in the script. This will encrypt all the traffic
+between your pbx and Wolframs servers making it much safer.
+To enable the use of SSL set the variable '$use_ssl' to 1 in the script. Keep in mind that
+security comes with a price, the initialisation of the SSL session adds a small delay in
+communication (usually under 1 sec).
-------
License
View
@@ -24,14 +24,17 @@ use CGI::Util qw(escape);
use XML::Simple;
$| = 1;
-# **To use this script you must get an app ID from http://products.wolframalpha.com/api ** #
+# * To use this script you must get an app ID from http://products.wolframalpha.com/api *
# ----------------------------- #
# User defined parameters: #
# ----------------------------- #
# Your App ID from wolfram #
my $app_id = "";
+# Use SSL #
+my $use_ssl = 0;
+
# Verbose debugging messages #
my $debug = 0;
@@ -41,10 +44,11 @@ my %AGI;
my $name;
my @result;
my $ua;
+my $url;
my $question;
my $w_reply;
my $answer;
-my $url = "http://api.wolframalpha.com/v2/query";
+my $host = "api.wolframalpha.com/v2/query";
my $var_name = "wolfram_answer";
my $results = 0;
@@ -59,17 +63,25 @@ while (<STDIN>) {
$name = " -- $AGI{request}:";
if (!$app_id) {
- &ret_var("You must have an Application ID from WolframAlpha to use this script.");
+ &return_var("You must have an Application ID from WolframAlpha to use this script.");
die "$name You must have an Application ID from WolframAlpha to use this script.\n";
}
if (!length($AGI{arg_1})) {
- &ret_var("No text passed to Wolfram.");
+ &return_var("No text passed to Wolfram.");
die "$name No text passed, Aborting.\n";
}
-print STDERR "$name Questions is: $question\n" if($debug);
+print STDERR "$name Questions is: $AGI{arg_1}\n" if($debug);
$question = escape($AGI{arg_1});
+if ($use_ssl) {
+ $url = "https://" . $host;
+ $ua = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
+} else {
+ $url = "http://" . $host;
+ $ua = LWP::UserAgent->new;
+}
+
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (X11; Linux) AppleWebKit/535.2 (KHTML, like Gecko)");
$ua->timeout(15);
@@ -79,14 +91,14 @@ my $ua_request = HTTP::Request->new(
);
my $ua_response = $ua->request($ua_request);
if (!$ua_response->is_success) {
- &ret_var("Failed to contact Wolfram server.");
+ &return_var("Failed to contact Wolfram server.");
die "$name Failed to contact server.";
}
$w_reply = XMLin($ua_response->content);
if ($w_reply->{success} eq 'false') {
print STDERR "$name Wolfram returned no answer.\n" if ($debug);
- &ret_var("I don't know how to answer that.");
+ &return_var("I don't know how to answer that.");
exit;
}
@@ -132,10 +144,10 @@ if (!$results) {
# In case we were not able to locate any answers. #
if (!$results) {
print STDERR "$name Failed to get any answer.\n" if ($debug);
- &ret_var("Failed to get any answer.");
+ &return_var("Failed to get any answer.");
} else {
print STDERR "$name Answer is:$answer\n" if ($debug);
- &ret_var($answer);
+ &return_var($answer);
}
exit;
@@ -154,8 +166,9 @@ sub add_answer {
}
# Store answers in channel variable. #
-sub ret_var {
+sub return_var {
my $text = shift;
+ print STDERR "$name Setting $var_name to $text\n" if ($debug);
print "SET VARIABLE \"$var_name\" \"$text\"\n";
&checkresponse();
}

0 comments on commit 7f86bb0

Please sign in to comment.