Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################################################
- #### BSTRD::IMAP
- #### v1.00
- #### (C)2009 Warbucks Heavy Industries, LLC
- ############################################################
- use MooseX::Declare;
- use MooseX::AttributeHelpers;
- use BSTRD::Object::Email;
- use IO::Socket::SSL;
- #with qw(
- # MooseX::LogDispatch::Levels
- #);
- #has logger => (
- # metaclass => 'NoGetopt',
- # isa => 'Log::Dispatch::Config',
- # is => 'rw',
- # lazy_build => 1,
- #);
- class BSTRD::IMAP {
- our $VERSION = '1.00';
- has username => (
- isa => 'Str',
- is => 'ro',
- );
- has password => (
- isa => 'Str',
- is => 'ro',
- );
- has imap => (
- isa => 'IO::Socket::SSL',
- is => 'rw',
- default => { IO::Socket::SSL->new("imap.gmail.com:993") },
- );
- method login {
- my $email = $self->email;
- my $pass = $self->pass;
- #### Clear the banner line;
- $self->imap->getline();
- $self->imap->write( "NIC1 LOGIN $email $pass\015\012" );
- $self->get_data;
- }
- method imapselect (Str $box) {
- $box = "INBOX" unless $box;
- $self->imap->write("NIC2 SELECT $box\015\012");
- $self->get_data;
- }
- method get_unseen {
- $self->imap->write("NIC3 SEARCH UNSEEN\015\012");
- my $res = $self->get_data;
- chomp $res;
- $res =~ s/^\* SEARCH //i;
- my @ids = split( / / , $res );
- return \@ids;
- }
- method mark_read (Int $msgid) {
- return unless ($msgid);
- $self->imap->write("NIC4 STORE $msgid FLAGS(\\Seen)\015\012");
- $self->get_data;
- }
- method fetch_message (Int $msgid) {
- $self->imap->write("NIC5 FETCH $msgid UID\015\012") ;
- $res = $self->get_data;
- my ( $uid ) = $res =~ /UID (\d+)/ ;
- $self->imap->write("NIC5 UID FETCH $uid BODY.PEEK[]\015\012");
- return get_data;
- }
- method get_data {
- my $data;
- while ( my $tmp = $self->imap->getline() ) {
- last if $data =~ /NIC\d+ OK.*Success/;
- $data .= $tmp;
- }
- return $data;
- }
- method process (Bool $markread) {
- $self->login;
- $self->imapselect;
- my $ids = $self->get_unseen;
- my @emailobjs;
- foreach my $id (@$ids) {
- push (@emailobjs,
- BSTRD::Object::Email->new(
- login => $self->email,
- rawmsg => $self->fetch_message($id)
- )
- );
- $self->mark_read ($id) if ($markread);
- }
- return \@emailobjs;
- }
- }
- 1; # End of BSTRD::IMAP
Add Comment
Please, Sign In to add comment