3 * popsbr.c -- POP client subroutines
10 extern int client(char *args, char *protocol, char *service, int rproto,
11 char *response, int len_response);
13 #if defined(NNTP) && !defined(PSHSBR)
17 #ifdef NNTP /* building pshsbr.o from popsbr.c */
21 #if !defined(NNTP) && defined(APOP)
26 #include <h/signals.h>
31 #define TRMLEN (sizeof TRM - 1)
33 static int poprint = 0;
34 static int pophack = 0;
36 char response[BUFSIZ];
43 #if !defined(NNTP) && defined(MPOP)
44 # define command pop_command
45 # define multiline pop_multiline
49 # ifdef BPOP /* stupid */
50 static int xtnd_last = -1;
51 static int xtnd_first = 0;
52 static char xtnd_name[512]; /* INCREDIBLE HACK!! */
59 #if !defined(NNTP) && defined(APOP)
60 static char *pop_auth (char *, char *);
63 #if defined(NNTP) || !defined(MPOP)
64 /* otherwise they are not static functions */
65 static int command(const char *, ...);
66 static int multiline(void);
69 static int traverse (int (*)(), const char *, ...);
70 static int vcommand(const char *, va_list);
71 static int getline (char *, int, FILE *);
72 static int putline (char *, FILE *);
75 #if !defined(NNTP) && defined(APOP)
77 pop_auth (char *user, char *pass)
81 unsigned char *dp, *ep, digest[16];
83 static char buffer[BUFSIZ];
85 if ((cp = strchr (response, '<')) == NULL
86 || (lp = strchr (cp, '>')) == NULL) {
87 snprintf (buffer, sizeof(buffer), "APOP not available: %s", response);
88 strncpy (response, buffer, sizeof(response));
93 snprintf (buffer, sizeof(buffer), "%s%s", cp, pass);
96 MD5Update (&mdContext, (unsigned char *) buffer,
97 (unsigned int) strlen (buffer));
98 MD5Final (digest, &mdContext);
101 buflen = sizeof(buffer);
103 snprintf (cp, buflen, "%s ", user);
108 for (ep = (dp = digest) + sizeof(digest) / sizeof(digest[0]); dp < ep; ) {
109 snprintf (cp, buflen, "%02x", *dp++ & 0xff);
117 #endif /* !NNTP && APOP */
121 pop_init (char *host, char *user, char *pass, int snoop, int rpop)
129 if ((apop = rpop) < 0)
135 if ((fd1 = client (host, "tcp", POPSERVICE, rpop, response, sizeof(response))) == NOTOK)
137 snprintf (buffer, sizeof(buffer), "%s/%s", KPOP_PRINCIPAL, POPSERVICE);
138 if ((fd1 = client (host, "tcp", buffer, rpop, response, sizeof(response))) == NOTOK)
141 if ((fd1 = client (host, "tcp", "nntp", rpop, response, sizeof(response))) == NOTOK)
145 if ((fd2 = dup (fd1)) == NOTOK) {
148 if ((s = strerror(errno)))
149 snprintf (response, sizeof(response),
150 "unable to dup connection descriptor: %s", s);
152 snprintf (response, sizeof(response),
153 "unable to dup connection descriptor: unknown error");
158 if (pop_set (fd1, fd2, snoop) == NOTOK)
160 if (pop_set (fd1, fd2, snoop, (char *)0) == NOTOK)
164 SIGNAL (SIGPIPE, SIG_IGN);
166 switch (getline (response, sizeof response, input)) {
169 fprintf (stderr, "<--- %s\n", response);
171 if (*response == '+') {
175 char *cp = pop_auth (user, pass);
177 if (cp && command ("APOP %s", cp) != NOTOK)
182 if (command ("USER %s", user) != NOTOK
183 && command ("%s %s", rpop ? "RPOP" : (pophack++, "PASS"),
187 if (command ("USER %s", user) != NOTOK
188 && command ("PASS %s", pass) != NOTOK)
193 if (*response < CHAR_ERR) {
194 command ("MODE READER");
198 strncpy (buffer, response, sizeof(buffer));
200 strncpy (response, buffer, sizeof(response));
206 fprintf (stderr, "%s\n", response);
212 return NOTOK; /* NOTREACHED */
217 pop_set (int in, int out, int snoop, char *myname)
220 pop_set (int in, int out, int snoop)
225 if (myname && *myname) {
226 /* interface from bbc to msh */
227 strncpy (xtnd_name, myname, sizeof(xtnd_name));
231 if ((input = fdopen (in, "r")) == NULL
232 || (output = fdopen (out, "w")) == NULL) {
233 strncpy (response, "fdopen failed on connection descriptor", sizeof(response));
249 pop_fd (char *in, int inlen, char *out, int outlen)
251 snprintf (in, inlen, "%d", fileno (input));
252 snprintf (out, outlen, "%d", fileno (output));
258 * Find out number of messages available
259 * and their total size.
263 pop_stat (int *nmsgs, int *nbytes)
270 if (command ("STAT") == NOTOK)
273 *nmsgs = *nbytes = 0;
274 sscanf (response, "+OK %d %d", nmsgs, nbytes);
277 if (xtnd_last < 0) { /* in msh, xtnd_name is set from myname */
278 if (command("GROUP %s", xtnd_name) == NOTOK)
281 ap = brkstring (response, " ", "\n"); /* "211 nart first last ggg" */
282 xtnd_first = atoi (ap[2]);
283 xtnd_last = atoi (ap[3]);
286 /* nmsgs is not the real nart, but an incredible simuation */
288 *nmsgs = xtnd_last - xtnd_first + 1; /* because of holes... */
291 *nbytes = xtnd_first; /* for subtracting offset in msh() */
299 pop_exists (int (*action)())
301 #ifdef XMSGS /* hacked into NNTP 1.5 */
302 if (traverse (action, "XMSGS %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last) == OK)
305 /* provided by INN 1.4 */
306 if (traverse (action, "LISTGROUP") == OK)
308 return traverse (action, "XHDR NONAME %d-%d", (targ_t) xtnd_first, (targ_t) xtnd_last);
315 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes, int *ids)
318 pop_list (int msgno, int *nmsgs, int *msgs, int *bytes)
328 if (command ("LIST %d", msgno) == NOTOK)
333 sscanf (response, "+OK %d %d %d", msgs, bytes, ids);
336 sscanf (response, "+OK %d %d", msgs, bytes);
339 if (command ("STAT %d", msgno) == NOTOK)
349 if (command ("LIST") == NOTOK)
352 for (i = 0; i < *nmsgs; i++)
353 switch (multiline ()) {
363 sscanf (response, "%d %d %d",
364 msgs++, bytes++, ids++);
367 sscanf (response, "%d %d", msgs++, bytes++);
371 switch (multiline ()) {
386 pop_retr (int msgno, int (*action)())
389 return traverse (action, "RETR %d", (targ_t) msgno);
391 return traverse (action, "ARTICLE %d", (targ_t) msgno);
397 traverse (int (*action)(), const char *fmt, ...)
401 char buffer[sizeof(response)];
404 result = vcommand (fmt, ap);
409 strncpy (buffer, response, sizeof(buffer));
412 switch (multiline ()) {
417 strncpy (response, buffer, sizeof(response));
421 (*action) (response);
430 return command ("DELE %d", msgno);
437 return command ("NOOP");
441 #if defined(MPOP) && !defined(NNTP)
445 return command ("LAST");
453 return command ("RSET");
458 pop_top (int msgno, int lines, int (*action)())
461 return traverse (action, "TOP %d %d", (targ_t) msgno, (targ_t) lines);
463 return traverse (action, "HEAD %d", (targ_t) msgno);
470 pop_xtnd (int (*action)(), char *fmt, ...)
482 /* needs to be fixed... va_end needs to be added */
483 snprintf (buffer, sizeof(buffer), "XTND %s", fmt);
484 result = traverse (action, buffer, a, b, c, d);
488 snprintf (buffer, sizeof(buffer), fmt, a, b, c, d);
489 ap = brkstring (buffer, " ", "\n"); /* a hack, i know... */
491 if (!strcasecmp(ap[0], "x-bboards")) { /* XTND "X-BBOARDS group */
492 /* most of these parameters are meaningless under NNTP.
493 * bbc.c was modified to set AKA and LEADERS as appropriate,
494 * the rest are left blank.
498 if (!strcasecmp (ap[0], "archive") && ap[1]) {
499 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
501 xtnd_first = 1; /* setup to fail in pop_stat */
504 if (!strcasecmp (ap[0], "bboards")) {
506 if (ap[1]) { /* XTND "BBOARDS group" */
507 snprintf (xtnd_name, sizeof(xtnd_name), "%s", ap[1]); /* save the name */
508 if (command("GROUP %s", xtnd_name) == NOTOK)
511 /* action must ignore extra args */
512 strncpy (buffer, response, sizeof(buffer));
513 ap = brkstring (response, " ", "\n");/* "211 nart first last g" */
514 xtnd_first = atoi (ap[2]);
515 xtnd_last = atoi (ap[3]);
520 } else { /* XTND "BBOARDS" */
521 return traverse (action, "LIST", a, b, c, d);
524 return NOTOK; /* unknown XTND command */
535 i = command ("QUIT");
552 #if !defined(MPOP) || defined(NNTP)
556 command(const char *fmt, ...)
562 result = vcommand(fmt, ap);
570 vcommand (const char *fmt, va_list ap)
572 char *cp, buffer[BUFSIZ];
574 vsnprintf (buffer, sizeof(buffer), fmt, ap);
577 if ((cp = strchr (buffer, ' ')))
579 fprintf (stderr, "---> %s ********\n", buffer);
585 fprintf (stderr, "---> %s\n", buffer);
588 if (putline (buffer, output) == NOTOK)
591 switch (getline (response, sizeof response, input)) {
594 fprintf (stderr, "<--- %s\n", response);
596 return (*response == '+' ? OK : NOTOK);
598 return (*response < CHAR_ERR ? OK : NOTOK);
604 fprintf (stderr, "%s\n", response);
608 return NOTOK; /* NOTREACHED */
612 #if defined(MPOP) && !defined(NNTP)
620 char buffer[BUFSIZ + TRMLEN];
622 if (getline (buffer, sizeof buffer, input) != OK)
626 fprintf (stderr, "<--- %s\n", response);
628 if (strncmp (buffer, TRM, TRMLEN) == 0) {
629 if (buffer[TRMLEN] == 0)
632 strncpy (response, buffer + TRMLEN, sizeof(response));
635 strncpy (response, buffer, sizeof(response));
642 getline (char *s, int n, FILE *iop)
648 while (--n > 0 && (c = fgetc (iop)) != EOF)
649 if ((*p++ = c) == '\n')
651 if (ferror (iop) && c != EOF) {
652 strncpy (response, "error on connection", sizeof(response));
655 if (c == EOF && p == s) {
656 strncpy (response, "connection closed by foreign host", sizeof(response));
670 putline (char *s, FILE *iop)
672 fprintf (iop, "%s\r\n", s);
675 strncpy (response, "lost connection", sizeof(response));