1 /* pshsbr.c - NNTP client subroutines */
3 static char ident[] = "@(#)$Id: pshsbr.c,v 2.4 1993/08/25 17:27:12 jromine Exp shettich $";
8 #include "../h/strings.h"
19 #define TRMLEN (sizeof TRM - 1)
24 extern char *sys_errlist[];
27 static int poprint = 0;
28 static int pophack = 0;
30 char response[BUFSIZ];
35 #ifdef BPOP /* stupid */
36 static int xtnd_last = -1,
38 static char xtnd_name[512]; /* INCREDIBLE HACK!! */
41 #define command pop_command
42 #define multiline pop_multiline
44 static int traverse(), getline();
48 int pop_init (host, user, pass, snoop)
59 if ((fd1 = client (host, "tcp", "nntp", response)) == NOTOK)
62 if ((fd2 = dup (fd1)) == NOTOK) {
63 (void) sprintf (response, "unable to dup connection descriptor: %s",
64 errno > 0 && errno < sys_nerr ? sys_errlist[errno]
69 if (pop_set (fd1, fd2, snoop, (char *)0) == NOTOK)
72 (void) signal (SIGPIPE, SIG_IGN);
74 switch (getline (response, sizeof response, input)) {
77 fprintf (stderr, "<--- %s\n", response);
78 if (*response < CHAR_ERR)
81 (void) strcpy (buffer, response);
82 (void) command ("QUIT");
83 (void) strcpy (response, buffer);
89 fprintf (stderr, "%s\n", response);
90 (void) fclose (input);
91 (void) fclose (output);
99 int pop_set (in, out, snoop, myname)
105 if (myname && *myname)
106 strcpy (xtnd_name, myname); /* interface from bbc to msh */
108 if ((input = fdopen (in, "r")) == NULL
109 || (output = fdopen (out, "w")) == NULL) {
110 (void) strcpy (response, "fdopen failed on connection descriptor");
112 (void) fclose (input);
129 (void) sprintf (in, "%d", fileno (input));
130 (void) sprintf (out, "%d", fileno (output));
136 int pop_stat (nmsgs, nbytes)
141 extern char **brkstring();
143 if (xtnd_last < 0) { /* in msh, xtnd_name is set from myname */
144 if (command("GROUP %s", xtnd_name) == NOTOK)
147 ap = brkstring (response, " ", "\n"); /* "211 nart first last ggg" */
148 xtnd_first = atoi (ap[2]);
149 xtnd_last = atoi (ap[3]);
152 /* nmsgs is not the real nart, but an incredible simuation */
154 *nmsgs = xtnd_last - xtnd_first + 1; /* because of holes... */
157 *nbytes = xtnd_first; /* for subtracting offset in msh() */
162 int pop_exists (action)
165 if (traverse (action, "XMSGS %d-%d", xtnd_first, xtnd_last) == OK)
168 return traverse (action, "XHDR NONAME %d-%d", xtnd_first, xtnd_last);
173 int pop_list (msgno, nmsgs, msgs, bytes)
175 int pop_list (msgno, nmsgs, msgs, bytes, ids)
190 if (command ("STAT %d", msgno) == NOTOK)
205 static int traverse (action, fmt, a, b, c, d)
213 char buffer[sizeof response];
215 if (command (fmt, a, b, c, d) == NOTOK)
217 (void) strcpy (buffer, response);
220 switch (multiline ()) {
225 (void) strcpy (response, buffer);
229 (*action) (response);
239 return command ("DELE %d", msgno);
244 return command ("NOOP");
249 return command ("RSET");
254 int pop_top (msgno, lines, action)
256 lines, /* sadly, ignored */
259 return traverse (action, "HEAD %d", msgno);
263 int pop_retr (msgno, action)
267 return traverse (action, "ARTICLE %d", msgno);
273 int pop_xtnd (action, fmt, a, b, c, d)
275 char *fmt, *a, *b, *c, *d;
277 extern char **brkstring();
278 char buffer[BUFSIZ], **ap;
280 sprintf (buffer, fmt, a, b, c, d);
281 ap = brkstring (buffer, " ", "\n"); /* a hack, i know... */
283 if (uleq(ap[0], "x-bboards")) { /* XTND "X-BBOARDS group */
284 /* most of these parameters are meaningless under NNTP.
285 * bbc.c was modified to set AKA and LEADERS as appropriate,
286 * the rest are left blank.
290 if (uleq (ap[0], "archive") && ap[1]) {
291 sprintf (xtnd_name, "%s", ap[1]); /* save the name */
293 xtnd_first = 1; /* setup to fail in pop_stat */
296 if (uleq (ap[0], "bboards")) {
298 if (ap[1]) { /* XTND "BBOARDS group" */
299 sprintf (xtnd_name, "%s", ap[1]); /* save the name */
300 if (command("GROUP %s", xtnd_name) == NOTOK)
303 strcpy (buffer, response); /* action must ignore extra args */
304 ap = brkstring (response, " ", "\n");/* "211 nart first last g" */
305 xtnd_first = atoi (ap[2]);
306 xtnd_last = atoi (ap[3]);
311 } else { /* XTND "BBOARDS" */
312 return traverse (action, "LIST", a, b, c, d);
315 return NOTOK; /* unknown XTND command */
324 i = command ("QUIT");
332 (void) fclose (input);
333 (void) fclose (output);
342 int command (fmt, a, b, c, d)
352 (void) sprintf (buffer, fmt, a, b, c, d);
355 if (cp = index (buffer, ' '))
357 fprintf (stderr, "---> %s ********\n", buffer);
363 fprintf (stderr, "---> %s\n", buffer);
365 if (putline (buffer, output) == NOTOK)
368 switch (getline (response, sizeof response, input)) {
371 fprintf (stderr, "<--- %s\n", response);
372 return (*response < CHAR_ERR ? OK : NOTOK);
377 fprintf (stderr, "%s\n", response);
384 char buffer[BUFSIZ + TRMLEN];
386 if (getline (buffer, sizeof buffer, input) != OK)
390 fprintf (stderr, "<--- %s\n", response);
392 if (strncmp (buffer, TRM, TRMLEN) == 0) {
393 if (buffer[TRMLEN] == NULL)
396 (void) strcpy (response, buffer + TRMLEN);
399 (void) strcpy (response, buffer);
406 static int getline (s, n, iop)
415 while (--n > 0 && (c = fgetc (iop)) != EOF)
416 if ((*p++ = c) == '\n')
418 if (ferror (iop) && c != EOF) {
419 (void) strcpy (response, "error on connection");
422 if (c == EOF && p == s) {
423 (void) strcpy (response, "connection closed by foreign host");
436 static putline (s, iop)
440 (void) fprintf (iop, "%s\r\n", s);
443 (void) strcpy (response, "lost connection");