-socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!";
-connect(S,$paddr) or die "connect: $!";
+&connect;
+if ($mode eq 'all') {
+ # Write out the entire contents of a newsgroup in mbox format.
+ $numbers = &docmd("GROUP $group");
+ @numbers = split / /, $numbers;
+ $fatal = 0; # ignore failure to retrieve any given article
+ for ($mid = $numbers[1]; $mid <= $numbers[2]; $mid++) {
+ $art = &getart("$group:$mid");
+ $art =~ s/\n(>*From )/\n>$1/gs;
+ print "From nntpid ".(localtime)."\n".$art."\n";
+ }
+} elsif ($mode eq 'stdin') {
+ while (<>) {
+ chomp;
+ s/^\s+//; s/\s+$//; # trim whitespace
+ &displayarticle($_);
+ }
+} elsif ($mode eq 'list') {
+ for $item (@list) {
+ &displayarticle($item);
+ }
+}
+
+sub parsearticle {
+ # Article identifiers used as input to this program can be in a
+ # variety of formats. This function untangles one into a standard
+ # format, which is either (undef, message-id) or (group, article
+ # number). In case of parse failure, it returns (undef, undef).
+ my $art = shift @_;
+ if ($art =~ /^(.*<)?([^<>]*\@[^<>]*)(>.*)?$/) {
+ # Anything with an @ sign is treated as a Message-ID. We trim
+ # angle brackets and anything outside them.
+ return (undef, $2);
+ } elsif ($art =~ /^(\S+)(\s+|:)(\d+)$/) {
+ # A group name and article number separated by whitespace or a
+ # colon.
+ return ($1, $3);
+ } else {
+ # Unable to parse.
+ return (undef, undef);
+ }
+}