Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use Getopt::Std;
- sub help {
- my $error = shift;
- print "Error: $error\n\n" if($error);
- print <<HELP;
- Usage: $0 <params>
- -f Don't fork and stay in foreground
- -h This help message
- -P <port> TCP port to listen on. Always listens on all available IPv4 interfaces. Defaults to tcp/8080
- HELP
- exit(1) if($error);
- };
- our($opt_f, $opt_h, $opt_P);
- $opt_P = 8080;
- getopts('fhP:') or (help() and exit(1));
- if($opt_h) { help(); exit(0); };
- help('Port must be numeric') unless($opt_P =~ m/^\d+$/);
- my $OSASCRIPT = '/usr/bin/osascript';
- {
- package TaskerWebServer;
- use HTTP::Server::Simple::CGI;
- use base qw(HTTP::Server::Simple::CGI);
- use strict;
- use warnings;
- my %dispatch = (
- '/' => \&mainPage,
- '/cmd1' => \&cmd1,
- '/cmd2' => \&cmd2,
- );
- sub handle_request {
- my $self = shift;
- my $cgi = shift;
- my $path = $cgi->path_info();
- my $handler = $dispatch{$path};
- if (ref($handler) eq "CODE") {
- print "HTTP/1.0 200 OK\r\n";
- $handler->($cgi);
- } else {
- print "HTTP/1.0 404 Not found\r\n";
- print $cgi->header,
- $cgi->start_html('Not found'),
- $cgi->h1('Not found'),
- $cgi->end_html;
- };
- };
- sub helper {
- my $cgi = shift;
- my $message = shift;
- my $html = $cgi->header .
- $cgi->start_html('Tasker interface');
- $html .= $cgi->h1($message) if($message);
- $html .= $cgi->end_html;
- return $html;
- };
- sub mainPage {
- my $cgi = shift;
- print helper($cgi, 'Tasker interface');
- };
- sub cmd1 {
- my $cgi = shift;
- system("$OSASCRIPT ~/my.first.applescript");
- print helper($cgi, 'Ran cmd1');
- };
- sub cmd2 {
- my $cgi = shift;
- system("$OSASCRIPT ~/my.second.applescript");
- print helper($cgi, 'Ran cmd1');
- };
- };
- if($opt_f) {
- TaskerWebServer->new($opt_P)->run();
- } else {
- my $pid = TaskerWebServer->new($opt_P)->background();
- print "Started tasker web service with PID $pid\n";
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement