From 88142cc36a86272a3f57619435e641b9336bb8f3 Mon Sep 17 00:00:00 2001 From: Tatsuhiko Miyagawa Date: Thu, 7 May 2009 14:17:40 -0700 Subject: [PATCH] Initial import --- Makefile.PL | 15 +++++++ gcal-quickadd.pl | 107 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 Makefile.PL create mode 100755 gcal-quickadd.pl diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..c669840 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,15 @@ +use inc::Module::Install; + +name 'gcal-quickadd'; +all_from 'gcal-quickadd.pl'; +license 'perl'; + +install_script 'gcal-quickadd.pl'; + +requires 'Net::Google::Calendar'; +requires 'Mouse'; +requires 'Any::Moose'; +requires 'YAML'; +requires 'Term::Prompt'; + +WriteAll; diff --git a/gcal-quickadd.pl b/gcal-quickadd.pl new file mode 100755 index 0000000..6f91c36 --- /dev/null +++ b/gcal-quickadd.pl @@ -0,0 +1,107 @@ +#!/usr/bin/perl +use strict; +use 5.008_001; + +package GCal::QuickAdd; +use Net::Google::Calendar; +use Any::Moose; +use YAML; + +has gcal => ( + is => 'rw', isa => "Net::Google::Calendar", + default => sub { Net::Google::Calendar->new }, + lazy => 1, +); + +has config => ( + is => 'rw', isa => "HashRef", +); + +has configfile => ( + is => 'rw', isa => "Str", + default => "$ENV{HOME}/.gcal-quickadd.yml", +); + +has username => ( + is => 'rw', isa => "Str", +); + +has token => ( + is => 'rw', isa => "Str", +); + +sub run { + my $self = shift; + my @args = @_; + my $text = join " ", @args or die "Usage: gcal-quickadd.pl [some free text]\n"; + + $self->init_authentication; + $self->post_event($text); +} + +sub post_event { + my $self = shift; + my($text) = @_; + + my $entry = Net::Google::Calendar::Entry->new; + $entry->set_attr('xmlns:gCal' => 'http://schemas.google.com/gCal/2005'); + $entry->content($text); + $entry->set( XML::Atom::Namespace->new(gCal => 'http://schemas.google.com/gCal/2005'), + quickadd => '', { value => 'true' } ); + + open my $null, ">/dev/null"; + select $null; # Net::Google::Calendar (0.95) prints extra stuff to STDOUT. Shut it off. + my $res = $self->gcal->add_entry($entry) or die $@; + select STDOUT; + + printf "Event '%s' created at %s.\n", $res->title, ($res->when)[0]->set_time_zone('local'); +} + +sub init_authentication { + my $self = shift; + + if (my $config = eval { YAML::LoadFile($self->configfile) }) { + $self->config($config); + $self->gcal->auth($self->config->{username}, $self->config->{token}); + $self->gcal->{_auth}->{_auth_type} = Net::Google::AuthSub::CLIENT_LOGIN; # UGH + return 1; + } + + require Term::Prompt; + my $username = Term::Prompt::prompt('s' => 'Your Google Email:', '', '', sub { shift =~ /\S/ }); + my $password = Term::Prompt::prompt(p => 'Password:', '', ''); + print "\n"; # ohmy + + unless ($username =~ /\@/) { + $username .= '@gmail.com'; + } + + $self->gcal->login($username, $password); + + if ($self->gcal->{_auth}->authorised) { + $self->config({ username => $username, token => $self->gcal->{_auth}->{_auth} }); + YAML::DumpFile($self->configfile, $self->config); + } else { + die "Google AuthSub authentication failed.\n"; + } +} + +package main; +GCal::QuickAdd->new->run(@ARGV); + +__END__ + +=head1 NAME + +gcal-quickadd.pl - Quick-adds an event to your Google Calendar from command line + +=head1 AUTHOR + +Tatsuhiko Miyagawa + +=head1 LICENSE + +Same as Perl (Artistic and GPL). + +=cut +