| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # (C) Simon Drabble 2003,2003 | ||
| 4 | # sdrabble@cpan.org 2002/03/22 | ||
| 5 | # $Id: yahootils.pl,v 1.4 2003/10/10 15:44:03 simon Exp $ | ||
| 6 | # | ||
| 7 | |||
| 8 | |||
| 9 | use strict; | ||
| 10 | |||
| 11 | use Mail::Webmail::Yahoo; | ||
| 12 | use Getopt::Long; | ||
| 13 | |||
| 14 | |||
| 15 | # With only username & password arguments, acts like 'snagmail.pl'. | ||
| 16 | |||
| 17 | |||
| 18 | my %opts; | ||
| 19 | # All options are called in long-opt format (--option) | ||
| 20 | # | ||
| 21 | # FUTURE actions (not implemented yet) | ||
| 22 | # list : Lists Subject:/From:/Date: in specified folders | ||
| 23 | # search: Yeah! | ||
| 24 | # delete: Deletes messages by (search?) criteria | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | # Set send flags - may be overridden on cmd-line | ||
| 29 | $opts{flags} = $ENV{MWY_SEND_FLAGS} || ''; | ||
| 30 | |||
| 31 | $opts{get_folders} = []; | ||
| 32 | $opts{empty_folders} = []; | ||
| 33 | $opts{to} = []; | ||
| 34 | $opts{cc} = []; | ||
| 35 | $opts{bcc} = []; | ||
| 36 | $opts{flags}= 0; | ||
| 37 | |||
| 38 | # Parse @ARGV.. | ||
| 39 | GetOptions( | ||
| 40 | # General options | ||
| 41 | 'username=s' => \$opts{user}, | ||
| 42 | 'password:s' => \$opts{pass}, # password can be passed via STDIN | ||
| 43 | # 'action=s' => \$opts{action}, ***DEPRECATED*** | ||
| 44 | 'cookies=s' => \$opts{cookies}, | ||
| 45 | 'trace=i' => \$opts{trace}, | ||
| 46 | 'debug!' => \$opts{debug}, | ||
| 47 | |||
| 48 | # Action options | ||
| 49 | 'send' => sub { $opts{actions}->{send} = 1 }, | ||
| 50 | 'get:s' => sub { | ||
| 51 | $opts{actions}->{get} = 1; | ||
| 52 | push @{$opts{get_folders}}, $_[1] || $ENV{MWY_INBOX} || 'Inbox'; | ||
| 53 | }, | ||
| 54 | 'empty:s' => sub { | ||
| 55 | $opts{actions}->{empty} = 1; | ||
| 56 | push @{$opts{empty_folders}}, $_[1] || $ENV{MWY_TRASH} || 'Trash'; | ||
| 57 | }, | ||
| 58 | 'folders' => sub { $opts{actions}->{folders} = 1}, | ||
| 59 | |||
| 60 | # Action-specific options | ||
| 61 | # 'folder=s' => \$opts{folder}, ***DEPRECATED*** | ||
| 62 | 'to=s' => $opts{to}, | ||
| 63 | 'cc=s' => $opts{cc}, | ||
| 64 | 'bcc=s' => $opts{bcc}, | ||
| 65 | 'subject=s' => \$opts{subject}, | ||
| 66 | 'message=s' => \$opts{message}, | ||
| 67 | 'flag=s' => sub { | ||
| 68 | my $v = $_[1]; | ||
| 69 | if ($v =~ /^\d+$/) { # Numeric flags ok | ||
| 70 | $opts{flags} |= $v; | ||
| 71 | } else { # as are things like 'MOVE_ON_READ' | ||
| 72 | for my $f (YAHOO_MSG_FLAGS) { | ||
| 73 | if (uc $v eq $f) { | ||
| 74 | $opts{flags} |= &{\&{uc $v}}; # constants are really functions | ||
| 75 | } | ||
| 76 | } # loop through possible flags | ||
| 77 | } # non-numeric flag passed | ||
| 78 | }, | ||
| 79 | 'moveto=s' => \$opts{moveto}, | ||
| 80 | |||
| 81 | # Utility options | ||
| 82 | 'help' => sub { &usage; exit }, | ||
| 83 | 'examples' => sub { &examples; exit }, | ||
| 84 | 'list-flags' => sub { &list_flags; exit }, | ||
| 85 | ); # end of GetOptions | ||
| 86 | |||
| 87 | |||
| 88 | &usage, die"\n" unless $opts{user}; | ||
| 89 | |||
| 90 | # Allow the password to be entered via stdin - hides it from the command line. | ||
| 91 | # Beware of shoulder-surfers! | ||
| 92 | if (!$opts{pass}) { | ||
| 93 | print "Password: "; | ||
| 94 | my $p = <STDIN>; | ||
| 95 | chomp $p; | ||
| 96 | $opts{pass} = $p or &usage, die; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | # Set up some defaults.. | ||
| 101 | $opts{cookies} ||= '~/.webmail-cookies'; | ||
| 102 | unless ($opts{actions}) { | ||
| 103 | $opts{actions} = {get => 1}; | ||
| 104 | push @{$opts{get_folders}}, $ENV{MWY_INBOX} || 'Inbox'; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | # Defines what-to-do-with-which | ||
| 109 | my $dispatch = { | ||
| 110 | empty => \&do_empty, | ||
| 111 | get => \&do_get, | ||
| 112 | send => \&do_send, | ||
| 113 | folders => \&do_folders, | ||
| 114 | }; | ||
| 115 | |||
| 116 | |||
| 117 | # Check all actions before continuing.. | ||
| 118 | for (keys %{$opts{action}}) { | ||
| 119 | &usage, die"\n" unless $dispatch->{$_}; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | # Login first | ||
| 125 | my $yahoo = new Mail::Webmail::Yahoo( | ||
| 126 | username => $opts{user}, | ||
| 127 | password => $opts{pass}, | ||
| 128 | cookie_file => $opts{cookies}, | ||
| 129 | ) or die $@; | ||
| 130 | |||
| 131 | $| = 1; | ||
| 132 | |||
| 133 | |||
| 134 | # Set the trace level | ||
| 135 | $yahoo->trace($opts{trace} || 0); | ||
| 136 | |||
| 137 | # Finally perform the require actions | ||
| 138 | for (keys %{$opts{actions}}) { | ||
| 139 | &{$dispatch->{$_}}($yahoo, \%opts); | ||
| 140 | } | ||
| 141 | |||
| 142 | # ..and we're done | ||
| 143 | exit; | ||
| 144 | |||
| 145 | |||
| 146 | sub usage | ||
| 147 | { | ||
| 148 | print qq{ | ||
| 149 | $0 --user <username> [--pass <password>] [--options] | ||
| 150 | General options (apply to all actions) (+ = mandatory) | ||
| 151 | user : username+ | ||
| 152 | pass : password+ (may be supplied via STDIN) | ||
| 153 | cookies: where to load/store cookies (default= ~/.webmail-cookies) | ||
| 154 | trace : sets the Yahoo.pm trace level | ||
| 155 | debug : sets the MessageParser.pm debug level (Not yet implemented fully) | ||
| 156 | |||
| 157 | help : Display this screen and exit | ||
| 158 | examples : Display a list of examples and exit | ||
| 159 | list-flags: Display a list of flags understood by Yahoo.pm and exit | ||
| 160 | |||
| 161 | Action-specific options (* = may be repeated) [used by which actions] | ||
| 162 | to : To: field of message* [send] | ||
| 163 | cc : Cc: field of message* [send] | ||
| 164 | bcc : BCc: field of message* [send] | ||
| 165 | subject: Subject: field of message [send] | ||
| 166 | message: Message body [send] | ||
| 167 | flag : Message flags [send,get] | ||
| 168 | moveto : Folder to move read messages to (required unless MWY_MOVETO) | ||
| 169 | |||
| 170 | Valid actions | ||
| 171 | send : Sends a message :) | ||
| 172 | get : Gets all messages within folders | ||
| 173 | empty: Empties specified folders -- NOT the same as delete! | ||
| 174 | folders: Displays a list of folders | ||
| 175 | |||
| 176 | |||
| 177 | Also the following ENV vars are recognised, which may be overridden by the | ||
| 178 | above options: | ||
| 179 | |||
| 180 | MWY_SEND_FLAGS : Default flags used when sending | ||
| 181 | MWY_INBOX : If --get specified without a folder | ||
| 182 | MWY_TRASH : If --empty specified without a folder to empty | ||
| 183 | MWT_MOVETO : If --moveto specified without a target folder | ||
| 184 | |||
| 185 | Flags passed in via --flag or stored in MWY_SEND_FLAGS may be specified as | ||
| 186 | either a numeric value, or as Mail::Webmail::Yahoo flag constants, for | ||
| 187 | example, DELETE_ON_READ ATTACH_SIG. | ||
| 188 | }; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | sub examples | ||
| 194 | { | ||
| 195 | print <<"EOT"; | ||
| 196 | For clarity, --user and --pass parameters are shown as [user] in some examples. | ||
| 197 | |||
| 198 | Examples: | ||
| 199 | |||
| 200 | To download your Inbox, the following are normally equivalent: | ||
| 201 | $0 --user Yahooser --pass xxxx | ||
| 202 | $0 --user Yahooser --pass xxxx --get | ||
| 203 | $0 --user Yahooser --pass xxxx --get Inbox | ||
| 204 | MWY_INBOX=Inbox $0 --user Yahooser --pass xxxx | ||
| 205 | MWY_INBOX=Inbox $0 --user Yahooser --pass xxxx --get | ||
| 206 | |||
| 207 | The same thing, deleting downloaded messages: | ||
| 208 | $0 [user] --flag DELETE_ON_READ | ||
| 209 | |||
| 210 | To send a simple message: | ||
| 211 | $0 [user] --send \\ | ||
| 212 | --to user\@server.com \\ | ||
| 213 | --subject 'Make \$\$\$ fast!' \\ | ||
| 214 | --message "I'll show you how" | ||
| 215 | |||
| 216 | The same thing, saving a copy to the 'Sent' folder: | ||
| 217 | $0 [user] --send \\ | ||
| 218 | --to user\@server.com \\ | ||
| 219 | --subject 'Make \$\$\$ fast!' \\ | ||
| 220 | --message "I'll show you how" \\ | ||
| 221 | --flag SAVE_COPY_TO_SENT_FOLDER | ||
| 222 | |||
| 223 | Or, equivalent: | ||
| 224 | $0 [user] --send \\ | ||
| 225 | --to user\@server.com \\ | ||
| 226 | --subject 'Make \$\$\$ fast!' \\ | ||
| 227 | --message "I'll show you how" \\ | ||
| 228 | --flag 1 | ||
| 229 | |||
| 230 | |||
| 231 | EOT | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | sub list_flags | ||
| 237 | { | ||
| 238 | print 'Flag', ' 'x 46, 'Numeric value', "\n"; | ||
| 239 | for (YAHOO_MSG_FLAGS) { printf "%-40s%s%8d\n", $_, ' 'x 10, &{\&{$_}} } | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | |||
| 244 | # Empties all folders in $args->{empty_folders} | ||
| 245 | sub do_empty | ||
| 246 | { | ||
| 247 | my ($y, $args) = @_; | ||
| 248 | |||
| 249 | for my $f (@{$args->{empty_folders}}) { | ||
| 250 | if ($y->empty($f)) { | ||
| 251 | print "$f.. emptied!\n"; | ||
| 252 | } else { | ||
| 253 | print "$f.. $@\n"; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | # Downloads all messages in folders in $args->{get_folders}. Messages are | ||
| 261 | # saved to "$username_$folder" | ||
| 262 | sub do_get | ||
| 263 | { | ||
| 264 | my ($y, $args) = @_; | ||
| 265 | |||
| 266 | my $flags = $args->{flags} || 0; | ||
| 267 | my $moveto = $args->{moveto} || $ENV{MWY_MOVETO}; | ||
| 268 | |||
| 269 | if ($flags & MOVE_ON_READ && $flags & DELETE_ON_READ) { | ||
| 270 | warn "do_get: MOVE_ON_READ and DELETE_ON_READ are mutually incompatible. Messages will not be deleted when read.\n"; | ||
| 271 | $flags ^= DELETE_ON_READ; | ||
| 272 | } | ||
| 273 | |||
| 274 | if ($flags & MOVE_ON_READ and !$moveto) { | ||
| 275 | die "do_get: MOVE_ON_READ but no folder to move to!\n"; | ||
| 276 | } | ||
| 277 | if (!($flags & MOVE_ON_READ) and $moveto) { | ||
| 278 | warn "do_get: MOVE_ON_READ not specified, although you provided a folder.\n"; | ||
| 279 | } | ||
| 280 | |||
| 281 | for my $folder (@{$args->{get_folders}}) { | ||
| 282 | |||
| 283 | my @messages = $y->get_mail_messages($folder, 'all', $flags, $moveto); | ||
| 284 | print "Message Headers in $folder: ", 0+@messages, "\n"; | ||
| 285 | print "Messages will be delivered to ./$args->{user}_$folder.\n"; | ||
| 286 | if ($flags & DELETE_ON_READ && !($flags & MOVE_ON_READ)) { | ||
| 287 | print "Messages will be deleted from server after download.\n"; | ||
| 288 | } | ||
| 289 | if ($moveto) { | ||
| 290 | if ($flags & MOVE_ON_READ) { | ||
| 291 | print "Messages will be moved to $moveto after download.\n"; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | # TODO: --output/--append for messages (default=STDOUT? - useful for passing | ||
| 296 | # into procmail etc) | ||
| 297 | open INBOX, ">$args->{user}_$folder"; | ||
| 298 | for (@messages) { print INBOX $_->as_mbox_string } | ||
| 299 | close INBOX; | ||
| 300 | print "\n"; | ||
| 301 | |||
| 302 | } | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | # Sends a mail message. | ||
| 309 | sub do_send | ||
| 310 | { | ||
| 311 | my ($y, $args) = @_; | ||
| 312 | # Require a recipient and a subject. | ||
| 313 | unless ((@{$args->{to}}||@{$args->{cc}}||@{$args->{bcc}}) && | ||
| 314 | $args->{subject}) { | ||
| 315 | die << 'EOT'; | ||
| 316 | do_send: usage: --send --to=recipient --subject=Subject --message=msg_body | ||
| 317 | EOT | ||
| 318 | } | ||
| 319 | # If not found, read message from stdin | ||
| 320 | unless ($args->{message}) { | ||
| 321 | while (<STDIN>) { | ||
| 322 | last if /^\.\s*$/; | ||
| 323 | $args->{message} .= $_; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | $y->send( | ||
| 328 | $args->{to}, # To | ||
| 329 | $args->{subject}, # Subject | ||
| 330 | $args->{message}, # Body | ||
| 331 | $args->{cc} || '', # Cc | ||
| 332 | $args->{bcc} || '', # BCc | ||
| 333 | $args->{flags}, # flags | ||
| 334 | ); | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | # TODO: get message count in each folder | ||
| 340 | sub do_folders | ||
| 341 | { | ||
| 342 | my ($y) = @_; | ||
| 343 | print join("\n", $y->get_folder_list), "\n"; | ||
| 344 | } | ||
| 345 | |||
| 346 | |||
| 347 | __END__ |