root/lang/perl/misc/i18n-emoji/EmailPOP3.pm

Revision 3817, 8.3 kB (checked in by kawa0117, 11 months ago)

see http://www.kawa.net/works/perl/i18n-emoji/i18n-emoji.html (2005/09/13)

Line 
1# ------------------------------------------------------------------------
2#   EmailPOP3.pm --- POP3 Client based on UID
3# ------------------------------------------------------------------------
4    use strict;
5    package EmailPOP3;
6    use IO::Socket::INET;
7# ------------------------------------------------------------------------
8    my $DEFAULT_SOCKET = {
9        PeerPort    =>  "110",
10        Proto       =>  "tcp",
11        Timeout     =>  3,
12    };
13# ------------------------------------------------------------------------
14sub new {
15    my $pkg = shift;
16    my $obj = {};
17    bless $obj, $pkg;
18    $obj->{args} = { @_ };      # options
19    $obj;
20}
21# ------------------------------------------------------------------------
22#   $socket = $pop3->connect();
23# ------------------------------------------------------------------------
24sub connect {
25    my $obj = shift;
26    if ( $obj->{socket} && $obj->{socket}->connected() ) {
27        return $obj->{socket};
28    }
29
30    my $conn = { %$DEFAULT_SOCKET };            # copy
31    foreach my $key ( keys %{$obj->{args}} ) {
32        next unless ( $key =~ /^[A-Z]/ );       # CAPITAL
33        $conn->{$key} = $obj->{args}->{$key};
34    }
35    my $host = $conn->{PeerAddr} || $conn->{PeerHost};
36    $obj->{socket} = new IO::Socket::INET(%$conn);
37    unless ( $obj->{socket} ) {
38        $obj->{error} = "POP3 connection failed - $host";
39        return;
40    }
41
42    $obj->cmd_single() or return;
43    $obj->cmd_single( "USER", $obj->{args}->{user} ) or return;
44    $obj->cmd_single( "PASS", $obj->{args}->{pass} ) or return;
45
46    $obj->{socket};
47}
48# ------------------------------------------------------------------------
49#   $list = $pop3->uidl();
50# ------------------------------------------------------------------------
51sub uidl {
52    my $obj = shift;
53    return $obj->{uidlist} if ref $obj->{uidlist};
54    $obj->connect() or return;
55    my $res = $obj->cmd_multi( "UIDL" ) or return;
56    my $uidlist = [];
57    my $uidhash = {};
58    foreach my $line ( @$res ) {
59        chomp $line;
60        my( $msg, $uid ) = split( /\s+/, $line );
61        push( @$uidlist, $uid );
62        $uidhash->{$uid} = $msg;
63    }
64    $obj->{uidhash} = $uidhash;
65    $obj->{uidlist} = $uidlist;
66}
67# ------------------------------------------------------------------------
68#   ( $mails, $total ) = $pop3->stat();
69# ------------------------------------------------------------------------
70sub stat {
71    my $obj = shift;
72    my $uid = shift;
73    $obj->connect() or return;
74    my $stat = $obj->cmd_single( "STAT" ) or return;
75    my( $mails, $total ) = @$stat;
76    wantarray ? ( $mails, $total ) : $mails;
77}
78# ------------------------------------------------------------------------
79#   $size = $pop3->msgid( $uid );
80# ------------------------------------------------------------------------
81sub msgid {
82    my $obj = shift;
83    my $uid = shift;
84    $obj->uidl() or return;
85    $obj->{uidhash}->{$uid};
86}
87# ------------------------------------------------------------------------
88#   $size = $pop3->size( $uid );
89# ------------------------------------------------------------------------
90sub size {
91    my $obj = shift;
92    my $uid = shift;
93    $obj->uidl() or return;
94    my $msg = $obj->{uidhash}->{$uid} or return;
95    my $res = $obj->cmd_single( "LIST", $msg ) or return;
96    return $res->[1];
97}
98# ------------------------------------------------------------------------
99#   $top = $pop3->top( $uid, $lines );
100# ------------------------------------------------------------------------
101sub top {
102    my $obj = shift;
103    my $uid = shift;
104    my $line = shift || 0;
105    $obj->uidl() or return;
106    my $msg = $obj->{uidhash}->{$uid} or return;
107    return $obj->cmd_multi( "TOP", $msg, $line );
108}
109# ------------------------------------------------------------------------
110#   $mail = $obj->retr( $uid );
111# ------------------------------------------------------------------------
112sub retr {
113    my $obj = shift;
114    my $uid = shift;
115    $obj->uidl() or return;
116    my $msg = $obj->{uidhash}->{$uid} or return;
117    return $obj->cmd_multi( "RETR", $msg );
118}
119# ------------------------------------------------------------------------
120#   $mail = $obj->retr_to_handle( $uid, $fh );
121# ------------------------------------------------------------------------
122sub retr_to_handle {
123    my $obj = shift;
124    my $uid = shift;
125        my $fh = shift;
126    $obj->uidl() or return;
127    my $msg = $obj->{uidhash}->{$uid} or return;
128    return $obj->cmd_multi_to_handle( $fh, "RETR", $msg );
129}
130# ------------------------------------------------------------------------
131#   $ok = $pop3->dele( $uid );
132# ------------------------------------------------------------------------
133sub delete_mail {
134    my $obj = shift;
135    my $uid = shift;
136    $obj->uidl() or return;
137    my $msg = $obj->{uidhash}->{$uid} or return;
138    my $res = $obj->cmd_multi( "DELE", $msg ) or return;
139    $res;
140}
141# ------------------------------------------------------------------------
142#   $pop3->noop();
143# ------------------------------------------------------------------------
144sub noop {
145    my $obj = shift;
146    $obj->connect() or return;
147    return $obj->cmd_single( "NOOP" );
148}
149# ------------------------------------------------------------------------
150#   $pop3->rset();
151# ------------------------------------------------------------------------
152sub rset {
153    my $obj = shift;
154    $obj->connect() or return;
155    return $obj->cmd_single( "RSET" );
156}
157# ------------------------------------------------------------------------
158#   $ok = $pop3->quit();
159# ------------------------------------------------------------------------
160sub quit {
161    my $obj = shift;
162    $obj->connect() or return;
163    return $obj->cmd_single( "QUIT" );
164}
165# ------------------------------------------------------------------------
166#   send a pop command and receive one result line.
167# ------------------------------------------------------------------------
168sub cmd_single {
169    my $obj = shift;
170    my $sock = $obj->{socket} or return;
171    $sock->print( join(" ", @_ ), "\n" ) if scalar @_;
172    my $res = $sock->getline();
173    $res =~ s/[\r\n]+$//s;
174    if ( $res =~ /^\+OK/ ) {
175        my $list = [split( /\s+/, $res )];
176        shift @$list;
177        return $list;                           # return ref
178    } else {
179        $obj->{error} = $res;
180        return undef;                           # return undef
181    }
182}
183# ------------------------------------------------------------------------
184#   send a pop command and receive multiple result lines.
185# ------------------------------------------------------------------------
186sub cmd_multi {
187    my $obj = shift;
188    my $res = $obj->cmd_single( @_ ) or return; # return undef
189    my $sock = $obj->{socket} or return;
190    my $array = [];
191    while( my $line = $sock->getline() ) {
192        $line =~ s/[\r\n]+$/\n/s;
193        last if ( $line =~ /^\.$/ );
194        $line =~ s/^\.\././;
195        push( @$array, $line );
196    }
197    $array;                                     # return ref
198}
199# ------------------------------------------------------------------------
200#   send a pop command and write result to the handle.
201# ------------------------------------------------------------------------
202sub cmd_multi_to_handle {
203    my $obj = shift;
204        my $fh = shift;
205    my $res = $obj->cmd_single( @_ ) or return; # return undef
206    my $sock = $obj->{socket} or return;
207    my $array = [];
208        my $cnt = 0;
209    while( my $line = $sock->getline() ) {
210        $line =~ s/[\r\n]+$/\n/s;
211        last if ( $line =~ /^\.$/ );
212        $line =~ s/^\.\././;
213                print $fh $line;
214                $cnt ++;
215    }
216        $cnt;                                                                           # return count (not undef)
217}
218# ------------------------------------------------------------------------
219;1;
220# ------------------------------------------------------------------------
221
222=head1 NAME
223
224    EmailPOP3.pm - POP3 Client based on UID
225
226=head1 SYNOPSIS
227
228    use EmailPOP3;
229   
230    my $conn = {
231        PeerHost    =>  "pop.example.com",
232        PeerPort    =>  "110",
233        Proto       =>  "tcp",
234        Timeout     =>  3,
235    };
236    my $pop3 = new EmailPOP3( %$conn );
237
238    ( $mails, $total ) = $pop3->stat();         # get status
239    print "$mails mails with $total bytes.\n";
240
241    my $list = $pop3->uidl();                   # get UID list
242
243    foreach my $uid ( @$list ) {
244        my $mail = $obj->retr( $uid );          # get mail header and body
245        print $mail;
246        $pop3->dele( $uid );                    # delete mail
247    }
248    $pop3->quit();                              # quit pop3 connection
249
250=cut
Note: See TracBrowser for help on using the browser.