#!/usr/bin/perl -w ### Sidekick::Appointment v0.3.1 ### ### Copyright 2002 Leigh L. Klotz Jr. ### Permission is hereby granted, free of charge, to any person obtaining ### a copy of this software and associated documentation files (the ### "Software"), to deal in the Software without restriction, including ### without limitation the rights to use, copy, modify, merge, publish, ### distribute, sublicense, and/or sell copies of the Software, and to ### permit persons to whom the Software is furnished to do so, subject to ### the following conditions: ### ### The above copyright notice and this permission notice shall be ### included in all copies or substantial portions of the Software. ### ### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ### EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ### MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ### NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE ### LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION ### OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION ### WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package Sidekick::Appointment; use LWP::UserAgent; use LWP::Protocol::https; use HTTP::Request::Common; use Sidekick::Login; # $NOICON=0; # $BACKPACK=1; # $BIRTHDAY=2; # $BRIEFCASE=3; # $COFFEE=4; # $CONCER=5; # $EXERCISE=6; # $FLOWER=7; # $HOME=8; # $MARTINI=9; # $MEETING=10; # $POPCORN=11; # $ROMANTIC=12; # $SHOPPING=13; # $SPORTS=14; # $TRAVEL=15; # $VACATION=16; # # pass in parameters { username => ..., cookies => ... } sub new($$) { my $type = shift; my %params = @_; my $self = {}; my $appt = $params{'appointment'}; # Parameters for authentication # Either cookies or username (and optionally cookiedir) $self->{'cookies'} = $params{'cookies'} || Sidekick::Login->new( "username" => $params{'username'} || $ENV{'USER'}, "cookiedir" => $params{'cookiedir'} || $ENV{'HOME'} )->cookies; # Result data $self->{'results'} = "(never added)"; $self->{'success'} = 0; bless $self, $type; return $self; } sub retrieve { my $self = shift; my $params = shift; # Parameters for retrieving $self->{'eventid'} = $params{'eventid'} || die "eventid must be specified for retrieve"; die "Cannot retrieve events yet"; } sub update { my $self = shift; my $params = shift; # Parameters for retrieving $self->{'eventid'} = $params{'eventid'} || die "eventid must be specified for retrieve"; die "eventid must be specified for update" if (! $self->{'eventid'}); die "Cannot update events yet"; } sub delete($$) { my $self = shift; my $params = shift; # Parameters for retrieving $self->{'eventid'} = $params->{'eventid'}; print STDERR " params = " , %params, "\n"; my $eventid = $self->{'eventid'} || die "eventid must be specified for delete"; my $result = "Cancelling. "; # Create a user agent object my $ua = LWP::UserAgent->new; $ua->agent("Sidekick::Appointment/0.4 "); # Get the cookies $ua->cookie_jar($self->{'cookies'}); # Create a request # Evil to delete things on GET instead of POST; tell Danger about this. my $req = GET 'http://calendar.sidekick.dngr.com/edit', [ action=>"discard", id=>$eventid, ]; print STDERR "About to do $req for eventid=$eventid\n"; # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_info) { $result .= $res->status_line . "\n"; } if ($res->is_redirect) { my $redir = $res->header('Location'); if ($redir =~ m/\?date=1970-01-01/) { $result .= "\nFailed to delete $eventid \n"; print STDERR __LINE__, " Redirect to $redir -- lost \n"; $self->{'success'} = 0; } else { print STDERR __LINE__, " Redirect to $redir -- won\n"; $self->{'success'} = 1; } } if ($res->is_error) { $self->{'success'} = 0; $result .= "\nFailed: an error\n" . $res->error_as_HTML; } if ($res->is_success) { # I assume it succeeded. #print STDERR $res->content; #print STDERR " Was success\n"; $self->{'success'} = 1; } $self->{'results'} = $result; return $result; } # appt is an Outlook::Appointment or other object with the following keys: # title # month_start # day_start # year_start # hour_start # min_start # hour_dur # min_dur # notes # icon_id sub add($$) { my $self = shift; my $appt = shift; # Parameters for adding appointments my $title = $self->{'title'} = $appt->{'title'}; my $month_start = $self->{'month_start'} = $appt->{'month_start'}; my $day_start = $self->{'day_start'} = $appt->{'day_start'}; my $year_start = $self->{'year_start'} = $appt->{'year_start'}; my $hour_start = $self->{'hour_start'} = $appt->{'hour_start'}; my $min_start = $self->{'min_start'} = $appt->{'min_start'}; my $hour_dur = $self->{'hour_dur'} = $appt->{'hour_dur'}; my $min_dur = $self->{'min_dur'} = $appt->{'min_dur'}; my $notes = $self->{'notes'} = $appt->{'notes'}; my $icon_id = $self->{'icon_id'} = $appt->{'icon_id'}; my $use_duration = $self->{'use_duration'} = ($min_dur > 0 || $hour_dur > 0) ? "true" : "false"; die "eventid must not be specified for add" if ($self->{'eventid'}); my $result = ""; $result .= "type=$icon_id" if ($icon_id != 0); $result .= $title; $result .= "\n" . $appt->{'From'} . "\n" if ($appt->{'From'}); $result .= " $month_start/$day_start/$year_start $hour_start:$min_start ($hour_dur:$min_dur long)"; $result .= "\nNotes:\t$notes\n" if ($notes); #print STDERR $result; # Create a user agent object my $ua = LWP::UserAgent->new; $ua->agent("Sidekick::Appointment/0.3 "); # Get the cookies $ua->cookie_jar($self->{'cookies'}); # Create a request my $req = POST 'http://calendar.sidekick.dngr.com/add', [ step => "save", e_allday => "f", editing_offshoot => "false", provides_title => "true", provides_starttime => "true", provides_endtime => "true", provides_notes => "true", provides_icon => "true", e_title => $title, month_start => $month_start, day_start => $day_start, year_start => $year_start, hour_start => $hour_start, min_start => $min_start, use_duration => $use_duration, e_hour_dur => $hour_dur, e_min_dur => $min_dur, e_notes => $notes, e_icon_id => $icon_id ]; # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response $result .= $res->status_line . "\n" if ($res->is_info || $res->is_success); if ($res->is_redirect) { my $redir = $res->header('Location'); if ($redir =~ m,/event\?id=([0-9]+),) { $self->{'eventid'} = $1; $self->{'success'} = 1; } else { $self->{'success'} = 0; $result .= "\nFailed: Redirect to $redir\n"; } } if ($res->is_error) { $self->{'success'} = 0; $result .= "\nFailed: an error"; } $self->{'results'} = $result; return $result; } sub success { my $self = shift; return $self->{'success'}; } sub results { my $self = shift; return $self->{'results'}; } sub eventid { my $self = shift; return $self->{'eventid'}; } sub eventurl { my $self = shift; return "http://calendar.sidekick.dngr.com/event?id=" . $self->{'eventid'}; } sub as_short_string($) { my $self = shift; return sprintf("appointment %s%02d/%02d/%d %02d:%02d (%02d:%02d)", (defined($self->{'eventid'}) ? $self->{'eventid'} . " " : ""), $self->{'month_start'}, $self->{'day_start'}, $self->{'year_start'}, $self->{'hour_start'}, $self->{'min_start'}, $self->{'hour_dur'}, $self->{'min_dur'}); } # package main; # my $appointment = Appointment->new('title' => "Trick or Treat", # 'month_start' => 10, # 'day_start' => 31, # 'year_start' => 2002, # 'hour_start' => 18, # 'min_start' => 0, # 'hour_dur' => 2, # 'min_dur' => 0, # 'notes' => "", # 'icon_id' => 0); # $appointment.add(); # print "eventid=", $appointment->eventid, " results= ", $appointment->results, "\n"; 1;