user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 04/11] ds: support greeting protocols
  2022-07-23  4:41  7% [PATCH 00/11] IMAP, NNTP, POP3 golfing Eric Wong
@ 2022-07-23  4:41  5% ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2022-07-23  4:41 UTC (permalink / raw)
  To: meta

We can share some common code between IMAP, NNTP, and POP3
without too much trouble, so cut down our LoC.
---
 lib/PublicInbox/DS.pm   | 18 ++++++++++++++++++
 lib/PublicInbox/IMAP.pm | 23 ++++-------------------
 lib/PublicInbox/NNTP.pm | 25 +++++--------------------
 lib/PublicInbox/POP3.pm | 23 ++++-------------------
 4 files changed, 31 insertions(+), 58 deletions(-)

diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index bf8c4466..79f7046f 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -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
diff --git a/lib/PublicInbox/IMAP.pm b/lib/PublicInbox/IMAP.pm
index 7e695fd8..89a278c1 100644
--- a/lib/PublicInbox/IMAP.pm
+++ b/lib/PublicInbox/IMAP.pm
@@ -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 }
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index e4ca7d14..533a0c59 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -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 ($$) {
diff --git a/lib/PublicInbox/POP3.pm b/lib/PublicInbox/POP3.pm
index ec73893c..c368615d 100644
--- a/lib/PublicInbox/POP3.pm
+++ b/lib/PublicInbox/POP3.pm
@@ -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

^ permalink raw reply related	[relevance 5%]

* [PATCH 00/11] IMAP, NNTP, POP3 golfing
@ 2022-07-23  4:41  7% Eric Wong
  2022-07-23  4:41  5% ` [PATCH 04/11] ds: support greeting protocols Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2022-07-23  4:41 UTC (permalink / raw)
  To: meta

More to come, but duplicate code and data structures are
to be eliminated.

Eric Wong (11):
  nntp: pass regexp to split() callers
  nntp: start adding CRLF to responses natively
  nntp: remove more() wrapper
  ds: support greeting protocols
  ds: move no-op ->zflush to common base class
  ds: move requeue_once
  nntp: listgroup_range_i: remove useless `map' op
  nntp: inline CRLF in all response lines
  ds: share long_step between NNTP and IMAP
  nntp: resolve inboxes immediately on group listings
  imap+nntp: share COMPRESS implementation

 MANIFEST                                      |   3 +-
 lib/PublicInbox/DS.pm                         |  72 ++++
 .../{NNTPdeflate.pm => DSdeflate.pm}          |  15 +-
 lib/PublicInbox/IMAP.pm                       | 102 +----
 lib/PublicInbox/IMAPD.pm                      |   2 +-
 lib/PublicInbox/IMAPdeflate.pm                | 126 ------
 lib/PublicInbox/NNTP.pm                       | 404 +++++++-----------
 lib/PublicInbox/NNTPD.pm                      |   4 +-
 lib/PublicInbox/POP3.pm                       |  80 +---
 t/nntpd.t                                     |   2 +-
 xt/mem-imapd-tls.t                            |   4 +-
 11 files changed, 262 insertions(+), 552 deletions(-)
 rename lib/PublicInbox/{NNTPdeflate.pm => DSdeflate.pm} (91%)
 delete mode 100644 lib/PublicInbox/IMAPdeflate.pm

^ permalink raw reply	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2022-07-23  4:41  7% [PATCH 00/11] IMAP, NNTP, POP3 golfing Eric Wong
2022-07-23  4:41  5% ` [PATCH 04/11] ds: support greeting protocols Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).