home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / alt / gopher / 1075 < prev    next >
Encoding:
Text File  |  1992-07-25  |  6.6 KB  |  314 lines

  1. Newsgroups: alt.gopher
  2. Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!schemers
  3. From: schemers@leland.Stanford.EDU (Roland Schemers)
  4. Subject:  gopher<->NNTP perl script
  5. Message-ID: <1992Jul25.002513.22566@leland.Stanford.EDU>
  6. Sender: news@leland.Stanford.EDU (Mr News)
  7. Organization: Distributed Computing Group, Stanford University
  8. References: <1992Jul25.001230.21597@leland.Stanford.EDU>
  9. Date: Sat, 25 Jul 92 00:25:13 GMT
  10. Lines: 302
  11.  
  12. Ok, here is yet ANOTHER gopher gateway. Gopher to NNTP. This is
  13. different from MSU's approach in that it is done dynamically
  14. by changing gopher requests into NNTP requests and vice-versa.
  15.  
  16. You have to run it from inetd.
  17.  
  18. Your NNTP server must understand the XHDR command. Its possible 
  19. to write this gateway so that command isn't needed, but your 
  20. better off upgrading your NNTP server :-)
  21.  
  22. I just wrote this in a couple of hours, and it works fairly well.
  23.  
  24. Try:
  25.  
  26. gopher slapshot.stanford.edu
  27.  
  28. And select "News".
  29.  
  30. Links should look like:
  31.  
  32. Name=USENET News
  33. Type=1
  34. Port=4327
  35. Path=
  36. Host=Slapshot.Stanford.EDU
  37.  
  38. To get all the news. You can get one group like so:
  39.  
  40. Name=Stanford Events (su.events)
  41. Type=1
  42. Port=4327
  43. Path=ls su.events
  44. Host=Slapshot.Stanford.EDU
  45.  
  46. Or a list of groups:
  47.  
  48. Name=Stanford News Groups
  49. Type=1
  50. Port=4327
  51. Path=ls su
  52. Host=Slapshot.Stanford.EDU
  53.  
  54. Or even one article:
  55.  
  56. Name=Mercury news: Amiga dead!
  57. Type=0
  58. Port=4327
  59. Path=article su.amiga 55
  60. Host=Slapshot.Stanford.EDU
  61.  
  62. The nice thing about this approach is now links could be setup to
  63. better describe news groups.
  64.  
  65. Please let me know what you think!
  66.  
  67. Roland
  68.  
  69. #!/usr/local/bin/perl
  70. #
  71. # switch to nobody if we are root
  72. ($<,$>) = (-2,-2) unless $>;
  73.  
  74. require 'getopts.pl';
  75.  
  76. #
  77. # Simple gopher2nntp server. You can change the following variables.
  78. # nntp_port is hardcoded here since this is a stateless server and
  79. # no sense in reading it from /etc/services every time...
  80. #
  81. # A server that sticks around might be better...
  82. #
  83. # Note: The NNTP daemon must accept the XHDR command.
  84. #
  85. #
  86. # You should run "gopher2nntp -g" from cron to generate the groups 
  87. # file. If you don't generate the groups file then this server will
  88. # grab it from the NNTP server, but thats a waste of time. 
  89. #
  90. #
  91.   $nntp_server = "leland.stanford.edu";
  92.   $nntp_port   = 119;
  93.   $nntp_groups = "/usr/local/etc/newsgroups.gopher"; # could be active file
  94. #
  95. # Commands this server responds to:
  96. # ""                         -> list top level groups
  97. # ls $group                  -> list group's articles and sub-groups
  98. # article $group $number     -> get 1 article
  99. #
  100.  
  101. $port=&my_port();         # port this script is running on 
  102. chop($host = `hostname`);
  103.  
  104. &Getopts('rdga:s:');
  105.  
  106. if ($errs || $#ARGV!=-1) {
  107.  
  108. print<<EOF;
  109.  
  110.  Usage: $0 [-s server] [-a active_file] [-g] [-d] [-r]
  111.  -s server      NNTP server to contact
  112.  -a file        Active file to use
  113.  -n             Don't use active file
  114.  -g             generate new active file\n";
  115.  -d             turn on debug (when typing commands from a tty)
  116.  -r             list articles in reverse order
  117. EOF
  118. exit;
  119.  
  120. }
  121.  
  122. $nntp_server = $opt_s if (defined($opt_s));
  123. $nntp_port = $opt_p if (defined($opt_p));
  124. $nntp_groups = $opt_a if (defined($opt_a));
  125.  
  126. &create_groups($opt_g) if defined($opt_g);
  127.  
  128.   $_ = <STDIN>; s/\r//; s/\n//;
  129.  
  130.   &do_ls("") if /^$/;
  131.   &do_ls($1) if /^ls\s+(.*)/i;
  132.   &do_article($1,$2) if /^article\s+(\S+)\s+(\d+)/i;
  133.  
  134.   &reply("3Unknown command!");
  135.   exit;
  136.  
  137. sub do_article {
  138.   local($group,$number) = @_;
  139.  
  140.   &open_nntp;
  141.  
  142.   &send("GROUP $group");
  143.   $_ = &recv;
  144.   &death if !/^211/;
  145.  
  146.   &send("ARTICLE $number");
  147.   $_ = &recv;
  148.   &death if !/^220/;
  149.  
  150.   while(<SERVER>) {
  151.     print;
  152.     last if /^\.\r\n$/;
  153.   }
  154.  
  155.   &close_nntp;
  156.   exit;
  157. }
  158.  
  159. sub list_group {
  160.   local($group) = @_;
  161.  
  162.   &send("GROUP $group");
  163.   $_ = &recv;
  164.   &death if !/^211/;
  165.  
  166.   ($n,$f,$l) = /211\s+(\d+)\s+(\d+)\s+(\d+)/;
  167.  
  168.   &send("XHDR Subject $f-$l");
  169.   $_ = &recv;
  170.   &death if !/^221/;
  171.  
  172.   while(<SERVER>) {
  173.     chop; chop;
  174.     last if /^\.$/;
  175.     ($article,$subject) = /^(\d+)\s+(.*)/;
  176.     $subject =~ s/\t/ /g; # just in case!
  177.     if (defined($opt_r)) {
  178.        push(@reply,"0$subject\tarticle $group $article\t$host\t$port");
  179.     } else {
  180.        &reply("0$subject\tarticle $group $article\t$host\t$port");
  181.     }
  182.   }
  183.  
  184.   if (defined($opt_r)) { 
  185.     for ($i=$#reply; $i!= -1; $i--) { &reply($reply[$i]); } 
  186.   }
  187.  
  188.   &reply(".");
  189.   &close_nntp;
  190.   exit;
  191. }
  192.  
  193.  
  194. sub do_ls {
  195.   local($prefix) = @_;
  196.  
  197.   &open_nntp;
  198.   &get_groups;
  199.  
  200.   foreach ( sort @groups) {
  201.     if ($_ eq $prefix) { $do_list_group = $_; }
  202.     elsif (/^$prefix\.([^.]*)\.?/) {
  203.          $leaf=$1;
  204.          $save{"$prefix.$leaf"} = "1$leaf\tls $prefix.$leaf\t$host\t$port";
  205.     }
  206.     elsif ($prefix eq '' && /([^.]*)/) {
  207.          $save{"$1"} = "1$1\tls $1\t$host\t$port";
  208.     }
  209.   }
  210.  
  211.   foreach ( sort keys %save) { &reply($save{$_}); }
  212.   &list_group($do_list_group) if ($do_list_group);
  213.  
  214.   &reply(".");
  215.   &close_nntp;
  216.   exit;
  217. }
  218.  
  219. sub open_nntp {
  220.   local($_);
  221.   &open_server($nntp_server,$nntp_port);
  222.   $_ = &recv;
  223.   &death if !/^2/;
  224. }
  225.  
  226. sub close_nntp {
  227.   &send("QUIT");
  228.   close(SERVER);
  229. }
  230.  
  231. sub my_port {
  232.    return -1 if (-t STDIN);
  233.    $sockaddr = 'S n a4 x8';
  234.    $mysockaddr = getsockname(STDIN);
  235.    ($myfamily,$myport,$myaddr) = unpack($sockaddr,$mysockaddr);
  236.    return $myport;
  237. }
  238.  
  239. sub open_server {
  240.  
  241.  local($server,$port) = @_;
  242.  $sockaddr = 'S n a4 x8';
  243.  (($name, $aliases, $type, $len, $saddr) = gethostbyname($server))||&death;
  244.  $sin = pack($sockaddr, 2, $port, $saddr);
  245.  socket(SERVER, 2, 1, 0) || &death;
  246.  connect(SERVER, $sin)   || &death;
  247.  select(SERVER); $| = 1; select(STDOUT); $| = 1;
  248.  
  249. }
  250.  
  251. sub send     { 
  252.      print "send -> |$_[0]|\n" if (defined($opt_d));
  253.      print SERVER "$_[0]\r\n"; 
  254. }
  255.  
  256. sub recv { 
  257.    local ($_); 
  258.    $_= <SERVER>; 
  259.    chop; chop;
  260.    print "recv -> |$_|\n" if (defined($opt_d));
  261.    return $_; 
  262. }
  263.  
  264. sub reply { print "$_[0]\r\n";}
  265. sub death { &reply("."); exit; }
  266.  
  267. sub get_groups {
  268.  if (!defined($opt_n) && open(GROUPS,$nntp_groups)) {
  269.       while(<GROUPS>) {
  270.           chop;
  271.           ($grp) = /^(\S+)/;
  272.           push(@groups,$grp);
  273.       }
  274.       close(GROUPS);
  275.  } else {                  # can't open file, get list from server!
  276.   &load_groups;
  277.  }
  278. }
  279.  
  280. sub load_groups {
  281.  
  282.   &open_nntp;
  283.   &send("LIST");
  284.   $_ = &recv;
  285.   &death if !/^215/;
  286.  
  287.   while(<SERVER>) {
  288.     chop; chop;
  289.     last if /^\.$/;
  290.     s/^(\S+).*/$1/;
  291.     push(@groups,$_);
  292.   }
  293.  
  294. }
  295.  
  296. sub create_groups { 
  297.  
  298.   &load_groups;
  299.  
  300.   open(GROUPS,">$nntp_groups") || die "$nntp_groups: $!";
  301.   foreach (@groups) { print GROUPS "$_\n"; }
  302.   close GROUPS;
  303.  
  304.   &close_nntp;
  305.   exit;
  306. }
  307.  
  308. -- 
  309. Roland J. Schemers III              |            Networking Systems
  310. Systems Programmer                  |            168 Pine Hall   (415)-723-6740
  311. Distributed Computing Group         |            Stanford, CA 94305-4122
  312. Stanford University                 |            schemers@Slapshot.Stanford.EDU
  313.