1# ======================================================================
2#
3# Copyright (C) 2000-2001 Paul Kulchenko (paulclinger@yahoo.com)
4# SOAP::Lite is free software; you can redistribute it
5# and/or modify it under the same terms as Perl itself.
6#
7# $Id: POP3.pm,v 1.2 2004/11/14 19:30:50 byrnereese Exp $
8#
9# ======================================================================
10
11package SOAP::Transport::POP3;
12
13use strict;
14use vars qw($VERSION);
15#$VERSION = sprintf("%d.%s", map {s/_//g; $_} q$Name:  $ =~ /-(\d+)_([\d_]+)/);
16$VERSION = $SOAP::Lite::VERSION;
17
18use Net::POP3;
19use URI;
20use SOAP::Lite;
21
22# ======================================================================
23
24package SOAP::Transport::POP3::Server;
25
26use Carp ();
27use vars qw(@ISA $AUTOLOAD);
28@ISA = qw(SOAP::Server);
29
30sub DESTROY { my $self = shift; $self->quit if $self->{_pop3server} }
31
32sub new {
33  my $self = shift;
34
35  unless (ref $self) {
36    my $class = ref($self) || $self;
37    my $address = shift;
38    Carp::carp "URLs without 'pop://' scheme are deprecated. Still continue"
39      if $address =~ s!^(pop://)?!pop://!i && !$1;
40    my $server = URI->new($address);
41    $self = $class->SUPER::new(@_);
42    $self->{_pop3server} = Net::POP3->new($server->host_port) or Carp::croak "Can't connect to '@{[$server->host_port]}': $!";
43    my $method = !$server->auth || $server->auth eq '*' ? 'login' :
44                  $server->auth eq '+APOP' ? 'apop' :
45                  Carp::croak "Unsupported authentication scheme '@{[$server->auth]}'";
46    $self->{_pop3server}->$method(split /:/, $server->user) or Carp::croak "Can't authenticate to '@{[$server->host_port]}' with '$method' method"
47      if defined $server->user;
48  }
49  return $self;
50}
51
52sub AUTOLOAD {
53  my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::') + 2);
54  return if $method eq 'DESTROY';
55
56  no strict 'refs';
57  *$AUTOLOAD = sub { shift->{_pop3server}->$method(@_) };
58  goto &$AUTOLOAD;
59}
60
61sub handle {
62  my $self = shift->new;
63  my $messages = $self->list or return;
64  foreach my $msgid (keys %$messages) {
65    $self->SUPER::handle(join '', @{$self->get($msgid)});
66  } continue {
67    $self->delete($msgid);
68  }
69  return scalar keys %$messages;
70}
71
72sub make_fault { return }
73
74# ======================================================================
75
761;
77
78__END__
79
80=head1 NAME
81
82SOAP::Transport::POP3 - Server side POP3 support for SOAP::Lite
83
84=head1 SYNOPSIS
85
86  use SOAP::Transport::POP3;
87
88  my $server = SOAP::Transport::POP3::Server
89    -> new('pop://pop.mail.server')
90    # if you want to have all in one place
91    # -> new('pop://user:password@pop.mail.server')
92    # or, if you have server that supports MD5 protected passwords
93    # -> new('pop://user:password;AUTH=+APOP@pop.mail.server')
94    # specify list of objects-by-reference here
95    -> objects_by_reference(qw(My::PersistentIterator My::SessionIterator My::Chat))
96    # specify path to My/Examples.pm here
97    -> dispatch_to('/Your/Path/To/Deployed/Modules', 'Module::Name', 'Module::method')
98  ;
99  # you don't need to use next line if you specified your password in new()
100  $server->login('user' => 'password') or die "Can't authenticate to POP3 server\n";
101
102  # handle will return number of processed mails
103  # you can organize loop if you want
104  do { $server->handle } while sleep 10;
105
106  # you may also call $server->quit explicitly to purge deleted messages
107
108=head1 DESCRIPTION
109
110=head1 COPYRIGHT
111
112Copyright (C) 2000-2001 Paul Kulchenko. All rights reserved.
113
114This library is free software; you can redistribute it and/or modify
115it under the same terms as Perl itself.
116
117=head1 AUTHOR
118
119Paul Kulchenko (paulclinger@yahoo.com)
120
121=cut
122