| 1 | package Net::FS::Gmail; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use Mail::Webmail::Gmail; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use File::Basename; | ||
| 6 | use Time::ParseDate; | ||
| 7 | use URI::Escape; | ||
| 8 | our $VERSION = "0.2"; | ||
| 9 | our $FILESTORE_VERSION = "0.1"; # this way we can track different revisions of filestore format | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Net::FS::Gmail - store and retrieve files on Gmail | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | my $fs = Net::FS::Gmail->new( username => $user, password => $pass ); | ||
| 18 | |||
| 19 | $fs->store("file.txt"); | ||
| 20 | $fs->store("piccy.jpg", "renamed_piccy.jpg"); | ||
| 21 | |||
| 22 | open (FILE, ">output.jpg") || die "Couldn't write to file: $!\n"; | ||
| 23 | binmode (FILE); | ||
| 24 | print FILE $fs->retrieve("renamed_piccy.jpg"); | ||
| 25 | close (FILE); | ||
| 26 | |||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | =head2 new | ||
| 33 | |||
| 34 | Takes the same options as Mail::Webmail::Gmail | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub new { | ||
| 39 | my $class = shift; | ||
| 40 | my %opts = @_; | ||
| 41 | my $gmail = Mail::Webmail::Gmail->new( %opts ); | ||
| 42 | # TODO better error reporting | ||
| 43 | $gmail->login() || die "Couldn't log into gmail : ".$gmail->error_msg(); | ||
| 44 | my $self = { _gmail => $gmail, _user => $opts{username} }; | ||
| 45 | return bless $self, $class; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | =head2 store <filename> [as] | ||
| 50 | |||
| 51 | Store the file <filename> on Gmail. If a second filename is given then use that | ||
| 52 | as the name on GMail | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub store { | ||
| 57 | my $self = shift; | ||
| 58 | my $file = shift; | ||
| 59 | my $as = shift; $as = $file unless defined $as; | ||
| 60 | die "No such file $file\n" unless -f $file; | ||
| 61 | |||
| 62 | my $subject = "GmailStore v$FILESTORE_VERSION $as"; | ||
| 63 | my $user = $self->{_user}; $user .= '@googlemail.com' unless $user =~ m!\@googlemail\.com$!; | ||
| 64 | |||
| 65 | $self->{_gmail}->send_message( to => $user, subject => $subject, msgbody => '', file0 => [ $file ] ); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | =head2 retrieve <filename> [version] | ||
| 71 | |||
| 72 | Get <filename> from Gmail. | ||
| 73 | |||
| 74 | If the file has multiple versions then you can pass in a version number to get version | ||
| 75 | - 1 being the oldest. If you don't pass in a version then you get the latest. | ||
| 76 | |||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub retrieve { | ||
| 81 | my $self = shift; | ||
| 82 | my $file = shift; | ||
| 83 | my $version = shift; | ||
| 84 | |||
| 85 | my @versions = $self->versions($file); | ||
| 86 | |||
| 87 | die "Couldn't find $file\n" unless @versions; | ||
| 88 | |||
| 89 | my $mid; | ||
| 90 | if (!defined $version) { | ||
| 91 | $mid = $versions[0]->{id}; | ||
| 92 | } elsif ($version > @versions || $version < 1) { | ||
| 93 | die "No such version $version\n"; | ||
| 94 | } else { | ||
| 95 | $mid = $versions[-$version]->{id}; | ||
| 96 | } | ||
| 97 | |||
| 98 | my $message = $self->{_gmail}->get_indv_email( id => $mid, label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } )->{$mid}; | ||
| 99 | |||
| 100 | |||
| 101 | die "Error: couldn't get attachments\n" unless defined $message->{ 'attachments' }; | ||
| 102 | my $attachment = $self->{_gmail}->get_attachment( attachment => $message->{ 'attachments' }->[0] ); | ||
| 103 | if ( $self->{_gmail}->error() ) { | ||
| 104 | die $self->{_gmail}-error_msg()."\n"; | ||
| 105 | } | ||
| 106 | return $$attachment; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 versions <filename> | ||
| 110 | |||
| 111 | Returns a list of all the versions of a file | ||
| 112 | |||
| 113 | Each item on the list is a hashref containing the date the file was saved | ||
| 114 | and the id of that version using the keys I<timestamp> and I<id> respectively. | ||
| 115 | |||
| 116 | The list is sorted, latest version first. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub versions { | ||
| 121 | my $self = shift; | ||
| 122 | my $file = shift; | ||
| 123 | my @versions; | ||
| 124 | foreach my $message (@{$self->{_gmail}->get_messages()}) { | ||
| 125 | my $email = $self->{_gmail}->get_indv_email( msg => $message ); | ||
| 126 | foreach my $id (keys %$email) { | ||
| 127 | my $item = $email->{$id}; | ||
| 128 | # TODO the subject may be html encoded | ||
| 129 | next unless uri_unescape($item->{'subject'}) =~ m!^GmailStore v[\d.]+ $file$!; | ||
| 130 | # TODO the sent time may be html encoded and need to be de-unicoded (7½ hours ago for example) | ||
| 131 | my $epoch_date = parsedate($item->{'sent'}); | ||
| 132 | push @versions, { id => $id, timestamp => $epoch_date }; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | return sort { $b->{timestamp} <=> $a->{timestamp} } @versions; | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | =head2 files | ||
| 140 | |||
| 141 | Get a list of all the files on the system | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub files { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | my @files; | ||
| 149 | foreach my $message (@{$self->{_gmail}->get_messages()}) { | ||
| 150 | # we do this to force it to be read | ||
| 151 | my $email = $self->{_gmail}->get_indv_email( msg => $message ); | ||
| 152 | my $subject = uri_unescape((values(%$email))[0]->{subject}); | ||
| 153 | next unless $subject =~ m!^GmailStore v[\d.]+ !; | ||
| 154 | push @files, $'; | ||
| 155 | } | ||
| 156 | return @files; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | =head2 delete <file> [version] | ||
| 161 | |||
| 162 | Delete a file. If you pass a version number than only delete that version. | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub delete { | ||
| 167 | my $self = shift; | ||
| 168 | my $file = shift; | ||
| 169 | my $version = shift; | ||
| 170 | |||
| 171 | $self->_delete($file, 1, $version); | ||
| 172 | } | ||
| 173 | |||
| 174 | =head2 remove <file> [version] | ||
| 175 | |||
| 176 | The same as remove except that the file is merely moved to the trash. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub remove { | ||
| 181 | my $self = shift; | ||
| 182 | my $file = shift; | ||
| 183 | my $version = shift; | ||
| 184 | |||
| 185 | $self->_delete($file, 0, $version); | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub _delete { | ||
| 190 | my $self = shift; | ||
| 191 | my $file = shift; | ||
| 192 | my $delete = shift; | ||
| 193 | my $version = shift; | ||
| 194 | |||
| 195 | |||
| 196 | my @versions = $self->versions($file); | ||
| 197 | |||
| 198 | die "Couldn't find $file\n" unless @versions; | ||
| 199 | |||
| 200 | my @mids; | ||
| 201 | if (!defined $version) { | ||
| 202 | @mids = map { $_->{id} } @versions; | ||
| 203 | } elsif ($version > @versions || $version < 1) { | ||
| 204 | die "No such version $version\n"; | ||
| 205 | } else { | ||
| 206 | push @mids, $versions[-$version]->{id}; | ||
| 207 | } | ||
| 208 | print STDERR "Deleting ".join(", ", @mids)."\n"; | ||
| 209 | |||
| 210 | $self->{_gmail}->delete_message( msgid => [ @mids ], del_message => $delete ); | ||
| 211 | |||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | =head2 quota | ||
| 217 | |||
| 218 | Get your current remaining quota, just like in Mail::Webmail::Gmail i.e | ||
| 219 | returns a scalar with the amount of MB remaining in you account. | ||
| 220 | |||
| 221 | If called in list context, returns an array as follows: | ||
| 222 | |||
| 223 | [ Used, Total, Percent Used ] [ "0 MB", "1000 MB", "0%" ] | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | sub quota { | ||
| 228 | my $self = shift; | ||
| 229 | return $self->{_gmail}->size_usage(); | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | =head1 AUTHOR | ||
| 234 | |||
| 235 | Simon Wistow <simon@thegestalt.org> | ||
| 236 | |||
| 237 | =head1 COPYRIGHT | ||
| 238 | |||
| 239 | Copyright 2006, Simon Wistow | ||
| 240 | |||
| 241 | Released under the same terms as Perl itself | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | |||
| 248 | 1; | ||