]> Sergey Matveev's repositories - public-inbox.git/commitdiff
ds: support greeting protocols
authorEric Wong <e@80x24.org>
Sat, 23 Jul 2022 04:41:48 +0000 (04:41 +0000)
committerEric Wong <e@80x24.org>
Sat, 23 Jul 2022 14:22:29 +0000 (14:22 +0000)
We can share some common code between IMAP, NNTP, and POP3
without too much trouble, so cut down our LoC.

lib/PublicInbox/DS.pm
lib/PublicInbox/IMAP.pm
lib/PublicInbox/NNTP.pm
lib/PublicInbox/POP3.pm

index bf8c4466218a37a43688aaaca2b063c65e98c86e..79f7046f87a4dbfe3732d221ecdbc05976336484 100644 (file)
@@ -347,6 +347,24 @@ retry:
     $DescriptorMap{$fd} = $self;
 }
 
+# for IMAP, NNTP, and POP3 which greet clients upon connect
+sub greet {
+       my ($self, $sock) = @_;
+       my $ev = EPOLLIN;
+       my $wbuf;
+       if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
+               return CORE::close($sock) if $! != EAGAIN;
+               $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
+               $wbuf = [ \&accept_tls_step, $self->can('do_greet')];
+       }
+       new($self, $sock, $ev | EPOLLONESHOT);
+       if ($wbuf) {
+               $self->{wbuf} = $wbuf;
+       } else {
+               $self->do_greet;
+       }
+       $self;
+}
 
 #####################################################################
 ### I N S T A N C E   M E T H O D S
index 7e695fd8e0eb20c5e37ffb10152cd361967aa1b1..89a278c14e90683395eb16756341d669fb86a84e 100644 (file)
@@ -36,7 +36,6 @@ use parent qw(PublicInbox::DS);
 use PublicInbox::Eml;
 use PublicInbox::EmlContentFoo qw(parse_content_disposition);
 use PublicInbox::DS qw(now);
-use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
 use PublicInbox::GitAsyncCat;
 use Text::ParseWords qw(parse_line);
 use Errno qw(EAGAIN);
@@ -99,29 +98,15 @@ undef %FETCH_NEED;
 my $valid_range = '[0-9]+|[0-9]+:[0-9]+|[0-9]+:\*';
 $valid_range = qr/\A(?:$valid_range)(?:,(?:$valid_range))*\z/;
 
-sub greet ($) {
+sub do_greet {
        my ($self) = @_;
        my $capa = capa($self);
        $self->write(\"* OK [$capa] public-inbox-imapd ready\r\n");
 }
 
-sub new ($$$) {
-       my ($class, $sock, $imapd) = @_;
-       my $self = bless { imapd => $imapd }, 'PublicInbox::IMAP_preauth';
-       my $ev = EPOLLIN;
-       my $wbuf;
-       if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
-               return CORE::close($sock) if $! != EAGAIN;
-               $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
-               $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
-       }
-       $self->SUPER::new($sock, $ev | EPOLLONESHOT);
-       if ($wbuf) {
-               $self->{wbuf} = $wbuf;
-       } else {
-               greet($self);
-       }
-       $self;
+sub new {
+       my (undef, $sock, $imapd) = @_;
+       (bless { imapd => $imapd }, 'PublicInbox::IMAP_preauth')->greet($sock)
 }
 
 sub logged_in { 1 }
index e4ca7d1462e292d1127f8f1abe02731b1c7d2afd..533a0c59f9398d67aef89739a806f47bdd1c46d8 100644 (file)
@@ -29,7 +29,6 @@ use constant {
        r225 => "225 Headers follow (multi-line)\r\n",
        r430 => '430 No article with that message-id',
 };
-use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
 use Errno qw(EAGAIN);
 my $ONE_MSGID = qr/\A$MID_EXTRACT\z/;
 my @OVERVIEW = qw(Subject From Date Message-ID References);
@@ -47,25 +46,11 @@ HDR\r
 OVER\r
 COMPRESS DEFLATE\r
 
-sub greet ($) { $_[0]->write($_[0]->{nntpd}->{greet}) };
-
-sub new ($$$) {
-       my ($class, $sock, $nntpd) = @_;
-       my $self = bless { nntpd => $nntpd }, $class;
-       my $ev = EPOLLIN;
-       my $wbuf;
-       if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
-               return CORE::close($sock) if $! != EAGAIN;
-               $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
-               $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
-       }
-       $self->SUPER::new($sock, $ev | EPOLLONESHOT);
-       if ($wbuf) {
-               $self->{wbuf} = $wbuf;
-       } else {
-               greet($self);
-       }
-       $self;
+sub do_greet ($) { $_[0]->write($_[0]->{nntpd}->{greet}) };
+
+sub new {
+       my ($cls, $sock, $nntpd) = @_;
+       (bless { nntpd => $nntpd }, $cls)->greet($sock)
 }
 
 sub args_ok ($$) {
index ec73893c5694d6cdda85ad5d3739d4c910c21d69..c368615d6f94143af05be534ae782cdea4b6faad 100644 (file)
@@ -33,7 +33,6 @@
 package PublicInbox::POP3;
 use v5.12;
 use parent qw(PublicInbox::DS);
-use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
 use PublicInbox::GitAsyncCat;
 use PublicInbox::DS qw(now);
 use Errno qw(EAGAIN);
@@ -113,29 +112,15 @@ sub long_response ($$;@) {
        undef;
 }
 
-sub _greet ($) {
+sub do_greet {
        my ($self) = @_;
        my $s = $self->{salt} = sprintf('%x.%x', int(rand(0x7fffffff)), time);
        $self->write("+OK POP3 server ready <$s\@public-inbox>\r\n");
 }
 
-sub new ($$$) {
-       my ($class, $sock, $pop3d) = @_;
-       my $self = bless { pop3d => $pop3d }, __PACKAGE__;
-       my $ev = EPOLLIN;
-       my $wbuf;
-       if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
-               return CORE::close($sock) if $! != EAGAIN;
-               $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
-               $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&_greet ];
-       }
-       $self->SUPER::new($sock, $ev | EPOLLONESHOT);
-       if ($wbuf) {
-               $self->{wbuf} = $wbuf;
-       } else {
-               _greet($self);
-       }
-       $self;
+sub new {
+       my ($cls, $sock, $pop3d) = @_;
+       (bless { pop3d => $pop3d }, $cls)->greet($sock)
 }
 
 # POP user is $UUID1@$NEWSGROUP.$SLICE