3 * popsbr.c -- POP client subroutines
10 #if defined(NNTP) && !defined(PSHSBR)
14 #ifdef NNTP /* building pshsbr.o from popsbr.c */
18 #if !defined(NNTP) && defined(APOP)
23 #include <h/signals.h>
27 #define TRMLEN (sizeof TRM - 1)
31 static int poprint = 0;
32 static int pophack = 0;
34 char response[BUFSIZ];
41 #if !defined(NNTP) && defined(MPOP)
42 # define command pop_command
43 # define multiline pop_multiline
47 # ifdef BPOP /* stupid */
48 static int xtnd_last = -1;
49 static int xtnd_first = 0;
50 static char xtnd_name[512]; /* INCREDIBLE HACK!! */
57 #if !defined(NNTP) && defined(APOP)
58 static char *pop_auth (char *, char *);
61 #if defined(NNTP) || !defined(MPOP)
62 /* otherwise they are not static functions */
63 static int command(const char *, ...);
64 static int multiline(void);
67 static int traverse (int (*)(), const char *, ...);
68 static int vcommand(const char *, va_list);
69 static int getline (char *, int, FILE *);
70 static int putline (char *, FILE *);
73 #if !defined(NNTP) && defined(APOP)
75 pop_auth (char *user, char *pass)
79 unsigned char *dp, *ep, digest[16];
81 static char buffer[BUFSIZ];
83 if ((cp = strchr (response, '<')) == NULL
84 || (lp = strchr (cp, '>')) == NULL) {
85 snprintf (buffer, sizeof(buffer), "APOP not available: %s", response);
86 strncpy (response, buffer, sizeof(response));
91 snprintf (buffer, sizeof(buffer), "%s%s", cp, pass);
94 MD5Update (&mdContext, (unsigned char *) buffer,
95 (unsigned int) strlen (buffer));
96 MD5Final (digest, &mdContext);
99 buflen = sizeof(buffer);
101 snprintf (cp, buflen, "%s ", user);
106 for (ep = (dp = digest) + sizeof(digest) / sizeof(digest[0]); dp < ep; ) {
107 snprintf (cp, buflen, "%02x", *dp++ & 0xff);
115 #endif /* !NNTP && APOP */
119 pop_init (char *host, char *user, char *pass, int snoop, int rpop)
127 if ((apop = rpop) < 0)
133 if ((fd1 = client (host, "tcp", POPSERVICE, rpop, response, sizeof(response))) == NOTOK)
135 snprintf (buffer, sizeof(buffer), "%s/%s", KPOP_PRINCIPAL, POPSERVICE);
136 if ((fd1 = client (host, "tcp", buffer, rpop, response, sizeof(response))) == NOTOK)
139 if ((fd1 = client (host, "tcp", "nntp", rpop, response, sizeof(response))) == NOTOK)
143 if ((fd2 = dup (fd1)) == NOTOK) {
146 if ((s = strerror(errno)))
147 snprintf (response, sizeof(response),
148 "unable to dup connection descriptor: %s", s);
150 snprintf (response, sizeof(response),
151 "unable to dup connection descriptor: unknown error");
156 if (pop_set (fd1, fd2, snoop) == NOTOK)
158 if (pop_set (fd1, fd2, snoop, (char *)0) == NOTOK)
162 SIGNAL (SIGPIPE, SIG_IGN);
164 switch (getline (response, sizeof response, input)) {
167 fprintf (stderr, "<--- %s\n", response);
169 if (*response == '+') {
173 char *cp = pop_auth (user, pass);
175 if (cp && command ("APOP %s", cp) != NOTOK)
180 if (command ("USER %s", user) != NOTOK
181 && command ("%s %s", rpop ? "RPOP" : (pophack++, "PASS"),
185 if (command ("USER %s", user) != NOTOK
186 && command ("PASS %s", pass) != NOTOK)
191 if (*response < CHAR_ERR) {
192 command ("MODE READER");
196 strncpy (buffer, response, sizeof(buffer));
198 strncpy (response, buffer, sizeof(response));
204 fprintf (stderr, "%s\n", response);
210 return NOTOK; /* NOTREACHED */
215 pop_set (int in, int out, int snoop, char *myname)
218 pop_set (int in, int out, int snoop)
223 if (myname && *myname) {
224 /* interface from bbc to msh */
225 strncpy (xtnd_name, myname, sizeof(xtnd_name));
229 if ((input = fdopen (in, "r")) == NULL
230 || (output = fdopen (out, "w")) == NULL) {
231 strncpy (response, "fdopen failed on connection descriptor", sizeof(response));
247 pop_fd (char *in, int inlen, char *out, int outlen)
249 snprintf (in, inlen, "%d", fileno (input));
250 snprintf (out, outlen, "%d", fileno (output));
256 * Find out number of messages available
257 * and their total size.
261 pop_stat (int *nmsgs, int *nbytes)
268 if (command ("STAT") == NOTOK)
271 *nmsgs = *nbytes = 0;
272 sscanf (response, "+OK %d %d", nmsgs, nbytes);
275 if (xtnd_last < 0) { /* in msh, xtnd_name is set from myname */
276 if (command("GROUP %s", xtnd_name) == NOTOK)
279 ap = brkstring (response, " ", "\n"); /* "211 nart first last ggg" */
280 xtnd_first = atoi (ap[2]);
281 xtnd_last = atoi (ap[3]);
284 /* nmsgs is not the real nart, but an incredible simuation */
286 *nmsgs = xtnd_last - xtnd_first + 1; /* because of holes... */
289 *nbytes = xtnd_first; /* for subtracting offset in msh() */
297 pop_exists (int (*action)())
299 #ifdef XMSGS /* hacked into NNTP 1.5 */
300 if (traverse (action, "XMSGS %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last) == OK)
303 /* provided by INN 1.4 */
304 if (traverse (action, "LISTGROUP") == OK)
306 return traverse (action, "XHDR NONAME %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last);
313 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes, int *ids)
316 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes)
326 if (command ("LIST %d", msgno) == NOTOK)
331 sscanf (response, "+OK %d %d %d", msgs, bytes, ids);
334 sscanf (response, "+OK %d %d", msgs, bytes);
337 if (command ("STAT %d", msgno) == NOTOK)
347 if (command ("LIST") == NOTOK)
350 for (i = 0; i < *nmsgs; i++)
351 switch (multiline ()) {
361 sscanf (response, "%d %d %d",
362 msgs++, bytes++, ids++);
365 sscanf (response, "%d %d", msgs++, bytes++);
369 switch (multiline ()) {
384 pop_retr (int msgno, int (*action)())
387 return traverse (action, "RETR %d", (targ_t) msgno);
389 return traverse (action, "ARTICLE %d", (targ_t) msgno);
395 traverse (int (*action)(), const char *fmt, ...)
399 char buffer[sizeof(response)];
402 result = vcommand (fmt, ap);
407 strncpy (buffer, response, sizeof(buffer));
410 switch (multiline ()) {
415 strncpy (response, buffer, sizeof(response));
419 (*action) (response);
428 return command ("DELE %d", msgno);
435 return command ("NOOP");
439 #if defined(MPOP) && !defined(NNTP)
443 return command ("LAST");
451 return command ("RSET");
456 pop_top (int msgno, int lines, int (*action)())
459 return traverse (action, "TOP %d %d", (targ_t) msgno, (targ_t) lines);
461 return traverse (action, "HEAD %d", (targ_t) msgno);
468 pop_xtnd (int (*action)(), char *fmt, ...)
480 /* needs to be fixed... va_end needs to be added */
481 snprintf (buffer, sizeof(buffer), "XTND %s", fmt);
482 result = traverse (action, buffer, a, b, c, d);
486 snprintf (buffer, sizeof(buffer), fmt, a, b, c, d);
487 ap = brkstring (buffer, " ", "\n"); /* a hack, i know... */
489 if (!strcasecmp(ap[0], "x-bboards")) { /* XTND "X-BBOARDS group */
490 /* most of these parameters are meaningless under NNTP.
491 * bbc.c was modified to set AKA and LEADERS as appropriate,
492 * the rest are left blank.
496 if (!strcasecmp (ap[0], "archive") && ap[1]) {
497 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
499 xtnd_first = 1; /* setup to fail in pop_stat */
502 if (!strcasecmp (ap[0], "bboards")) {
504 if (ap[1]) { /* XTND "BBOARDS group" */
505 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
506 if (command("GROUP %s", xtnd_name) == NOTOK)
509 /* action must ignore extra args */
510 strncpy (buffer, response, sizeof(buffer));
511 ap = brkstring (response, " ", "\n");/* "211 nart first last g" */
512 xtnd_first = atoi (ap[2]);
513 xtnd_last = atoi (ap[3]);
518 } else { /* XTND "BBOARDS" */
519 return traverse (action, "LIST", a, b, c, d);
522 return NOTOK; /* unknown XTND command */
533 i = command ("QUIT");
550 #if !defined(MPOP) || defined(NNTP)
554 command(const char *fmt, ...)
560 result = vcommand(fmt, ap);
568 vcommand (const char *fmt, va_list ap)
570 char *cp, buffer[BUFSIZ];
572 vsnprintf (buffer, sizeof(buffer), fmt, ap);
575 if ((cp = strchr (buffer, ' ')))
577 fprintf (stderr, "---> %s ********\n", buffer);
583 fprintf (stderr, "---> %s\n", buffer);
585 if (putline (buffer, output) == NOTOK)
588 switch (getline (response, sizeof response, input)) {
591 fprintf (stderr, "<--- %s\n", response);
593 return (*response == '+' ? OK : NOTOK);
595 return (*response < CHAR_ERR ? OK : NOTOK);
601 fprintf (stderr, "%s\n", response);
605 return NOTOK; /* NOTREACHED */
609 #if defined(MPOP) && !defined(NNTP)
617 char buffer[BUFSIZ + TRMLEN];
619 if (getline (buffer, sizeof buffer, input) != OK)
623 fprintf (stderr, "<--- %s\n", response);
625 if (strncmp (buffer, TRM, TRMLEN) == 0) {
626 if (buffer[TRMLEN] == 0)
629 strncpy (response, buffer + TRMLEN, sizeof(response));
632 strncpy (response, buffer, sizeof(response));
639 getline (char *s, int n, FILE *iop)
645 while (--n > 0 && (c = fgetc (iop)) != EOF)
646 if ((*p++ = c) == '\n')
648 if (ferror (iop) && c != EOF) {
649 strncpy (response, "error on connection", sizeof(response));
652 if (c == EOF && p == s) {
653 strncpy (response, "connection closed by foreign host", sizeof(response));
667 putline (char *s, FILE *iop)
669 fprintf (iop, "%s\r\n", s);
672 strncpy (response, "lost connection", sizeof(response));