| 1 |
############################################################################ |
| 2 |
# |
| 3 |
# File: newsgrp.icn |
| 4 |
# |
| 5 |
# Subject: Program to get news files from NNTP server |
| 6 |
# |
| 7 |
# Author: Gregg M. Townsend |
| 8 |
# |
| 9 |
# Date: August 4, 2000 |
| 10 |
# |
| 11 |
############################################################################ |
| 12 |
# |
| 13 |
# This file is in the public domain. |
| 14 |
# |
| 15 |
############################################################################ |
| 16 |
# |
| 17 |
# Usage: newsgrp newsgroup.name |
| 18 |
# |
| 19 |
# This program connects to an NNTP server and prints the subject lines |
| 20 |
# of the articles in the specified newsgroup. |
| 21 |
# |
| 22 |
############################################################################ |
| 23 |
|
| 24 |
link cfunc # link standard C functions transparently |
| 25 |
|
| 26 |
$include "libnames.icn" |
| 27 |
|
| 28 |
$define SERVER "news" # default host name for server |
| 29 |
$define PORT 119 # NNTP port |
| 30 |
|
| 31 |
global verbose |
| 32 |
global socket, smode |
| 33 |
global host, group |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
procedure main(args) |
| 38 |
local s, n, l, h, i |
| 39 |
|
| 40 |
group := args[1] | "comp.lang.icon" |
| 41 |
|
| 42 |
host := getenv("NNTPSERVER") | SERVER |
| 43 |
socket := tconnect(host, PORT) | |
| 44 |
stop("can't connect to port ", PORT, " of host ", host) |
| 45 |
|
| 46 |
expect("20") # read greeting line |
| 47 |
|
| 48 |
swrite("group ", group) # send newsgroup request |
| 49 |
expect("211") ? { |
| 50 |
="211" |
| 51 |
n := integer(tab(many(' ')) & tab(upto(' '))) # number of articles |
| 52 |
l := integer(tab(many(' ')) & tab(upto(' '))) # low number |
| 53 |
h := integer(tab(many(' ')) & tab(upto(' '))) # high number |
| 54 |
} |
| 55 |
|
| 56 |
every i := l to h do { |
| 57 |
swrite("head ", i) # request article header |
| 58 |
s := sread() # read response |
| 59 |
if not (s ? ="221") then |
| 60 |
next # if not available |
| 61 |
|
| 62 |
while (s := sread()) ~== "." do # read through end-of-header flag |
| 63 |
if map(s) ? ="subject: " then |
| 64 |
write(i, ". ", s[10:0]) # output subject line |
| 65 |
} |
| 66 |
|
| 67 |
swrite("quit") |
| 68 |
end |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
# expect(prefix) -- read line from socket and check prefix |
| 73 |
|
| 74 |
procedure expect(prefix) |
| 75 |
local s |
| 76 |
|
| 77 |
s := sread() |
| 78 |
if s ? =prefix then |
| 79 |
return s |
| 80 |
stop("expected ", prefix, ", read ", s) |
| 81 |
end |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
# sread() -- read line from socket |
| 86 |
|
| 87 |
procedure sread() |
| 88 |
local s |
| 89 |
|
| 90 |
if \smode := &null then |
| 91 |
seek(socket) # switch file mode from output to input |
| 92 |
|
| 93 |
s := trim(read(socket), '\n\r') | stop("EOF") |
| 94 |
|
| 95 |
if \verbose then # if "verbose" mode set |
| 96 |
write("< ", s) # trace input line |
| 97 |
|
| 98 |
return s |
| 99 |
end |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
# swrite(s, ...) -- write line to socket |
| 104 |
|
| 105 |
procedure swrite(s[]) |
| 106 |
|
| 107 |
push(s, "> ") |
| 108 |
if \verbose then # if "verbose" mode set |
| 109 |
write ! s # trace output |
| 110 |
s[1] := socket |
| 111 |
|
| 112 |
if /smode := 1 then { |
| 113 |
seek(socket) # switch file mode from input to output |
| 114 |
flush(socket) # workaround for Dec Alpha bug |
| 115 |
} |
| 116 |
return write ! s # write strings to port |
| 117 |
end |