]>
diplodocus.org Git - nmh/blob - uip/popsbr.c
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>
28 #define TRMLEN (sizeof TRM - 1)
30 static int poprint
= 0;
31 static int pophack
= 0;
33 char response
[BUFSIZ
];
40 #if !defined(NNTP) && defined(MPOP)
41 # define command pop_command
42 # define multiline pop_multiline
46 # ifdef BPOP /* stupid */
47 static int xtnd_last
= -1;
48 static int xtnd_first
= 0;
49 static char xtnd_name
[512]; /* INCREDIBLE HACK!! */
56 #if !defined(NNTP) && defined(APOP)
57 static char *pop_auth (char *, char *);
60 #if defined(NNTP) || !defined(MPOP)
61 /* otherwise they are not static functions */
62 static int command(const char *, ...);
63 static int multiline(void);
66 static int traverse (int (*)(), const char *, ...);
67 static int vcommand(const char *, va_list);
68 static int getline (char *, int, FILE *);
69 static int putline (char *, FILE *);
72 #if !defined(NNTP) && defined(APOP)
74 pop_auth (char *user
, char *pass
)
78 unsigned char *dp
, *ep
, digest
[16];
80 static char buffer
[BUFSIZ
];
82 if ((cp
= strchr (response
, '<')) == NULL
83 || (lp
= strchr (cp
, '>')) == NULL
) {
84 snprintf (buffer
, sizeof(buffer
), "APOP not available: %s", response
);
85 strncpy (response
, buffer
, sizeof(response
));
90 snprintf (buffer
, sizeof(buffer
), "%s%s", cp
, pass
);
93 MD5Update (&mdContext
, (unsigned char *) buffer
,
94 (unsigned int) strlen (buffer
));
95 MD5Final (digest
, &mdContext
);
98 buflen
= sizeof(buffer
);
100 snprintf (cp
, buflen
, "%s ", user
);
105 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]); dp
< ep
; ) {
106 snprintf (cp
, buflen
, "%02x", *dp
++ & 0xff);
114 #endif /* !NNTP && APOP */
118 pop_init (char *host
, char *user
, char *pass
, int snoop
, int rpop
)
126 if ((apop
= rpop
) < 0)
132 if ((fd1
= client (host
, "tcp", POPSERVICE
, rpop
, response
, sizeof(response
))) == NOTOK
)
134 snprintf (buffer
, sizeof(buffer
), "%s/%s", KPOP_PRINCIPAL
, POPSERVICE
);
135 if ((fd1
= client (host
, "tcp", buffer
, rpop
, response
, sizeof(response
))) == NOTOK
)
138 if ((fd1
= client (host
, "tcp", "nntp", rpop
, response
, sizeof(response
))) == NOTOK
)
142 if ((fd2
= dup (fd1
)) == NOTOK
) {
145 if ((s
= strerror(errno
)))
146 snprintf (response
, sizeof(response
),
147 "unable to dup connection descriptor: %s", s
);
149 snprintf (response
, sizeof(response
),
150 "unable to dup connection descriptor: unknown error");
155 if (pop_set (fd1
, fd2
, snoop
) == NOTOK
)
157 if (pop_set (fd1
, fd2
, snoop
, (char *)0) == NOTOK
)
161 SIGNAL (SIGPIPE
, SIG_IGN
);
163 switch (getline (response
, sizeof response
, input
)) {
166 fprintf (stderr
, "<--- %s\n", response
);
168 if (*response
== '+') {
172 char *cp
= pop_auth (user
, pass
);
174 if (cp
&& command ("APOP %s", cp
) != NOTOK
)
179 if (command ("USER %s", user
) != NOTOK
180 && command ("%s %s", rpop
? "RPOP" : (pophack
++, "PASS"),
184 if (command ("USER %s", user
) != NOTOK
185 && command ("PASS %s", pass
) != NOTOK
)
190 if (*response
< CHAR_ERR
) {
191 command ("MODE READER");
195 strncpy (buffer
, response
, sizeof(buffer
));
197 strncpy (response
, buffer
, sizeof(response
));
203 fprintf (stderr
, "%s\n", response
);
209 return NOTOK
; /* NOTREACHED */
214 pop_set (int in
, int out
, int snoop
, char *myname
)
217 pop_set (int in
, int out
, int snoop
)
222 if (myname
&& *myname
) {
223 /* interface from bbc to msh */
224 strncpy (xtnd_name
, myname
, sizeof(xtnd_name
));
228 if ((input
= fdopen (in
, "r")) == NULL
229 || (output
= fdopen (out
, "w")) == NULL
) {
230 strncpy (response
, "fdopen failed on connection descriptor", sizeof(response
));
246 pop_fd (char *in
, int inlen
, char *out
, int outlen
)
248 snprintf (in
, inlen
, "%d", fileno (input
));
249 snprintf (out
, outlen
, "%d", fileno (output
));
255 * Find out number of messages available
256 * and their total size.
260 pop_stat (int *nmsgs
, int *nbytes
)
267 if (command ("STAT") == NOTOK
)
270 *nmsgs
= *nbytes
= 0;
271 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
274 if (xtnd_last
< 0) { /* in msh, xtnd_name is set from myname */
275 if (command("GROUP %s", xtnd_name
) == NOTOK
)
278 ap
= brkstring (response
, " ", "\n"); /* "211 nart first last ggg" */
279 xtnd_first
= atoi (ap
[2]);
280 xtnd_last
= atoi (ap
[3]);
283 /* nmsgs is not the real nart, but an incredible simuation */
285 *nmsgs
= xtnd_last
- xtnd_first
+ 1; /* because of holes... */
288 *nbytes
= xtnd_first
; /* for subtracting offset in msh() */
296 pop_exists (int (*action
)())
298 #ifdef XMSGS /* hacked into NNTP 1.5 */
299 if (traverse (action
, "XMSGS %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
) == OK
)
302 /* provided by INN 1.4 */
303 if (traverse (action
, "LISTGROUP") == OK
)
305 return traverse (action
, "XHDR NONAME %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
);
312 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
, int *ids
)
315 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
)
325 if (command ("LIST %d", msgno
) == NOTOK
)
330 sscanf (response
, "+OK %d %d %d", msgs
, bytes
, ids
);
333 sscanf (response
, "+OK %d %d", msgs
, bytes
);
336 if (command ("STAT %d", msgno
) == NOTOK
)
346 if (command ("LIST") == NOTOK
)
349 for (i
= 0; i
< *nmsgs
; i
++)
350 switch (multiline ()) {
360 sscanf (response
, "%d %d %d",
361 msgs
++, bytes
++, ids
++);
364 sscanf (response
, "%d %d", msgs
++, bytes
++);
368 switch (multiline ()) {
383 pop_retr (int msgno
, int (*action
)())
386 return traverse (action
, "RETR %d", (targ_t
) msgno
);
388 return traverse (action
, "ARTICLE %d", (targ_t
) msgno
);
394 traverse (int (*action
)(), const char *fmt
, ...)
398 char buffer
[sizeof(response
)];
401 result
= vcommand (fmt
, ap
);
406 strncpy (buffer
, response
, sizeof(buffer
));
409 switch (multiline ()) {
414 strncpy (response
, buffer
, sizeof(response
));
418 (*action
) (response
);
427 return command ("DELE %d", msgno
);
434 return command ("NOOP");
438 #if defined(MPOP) && !defined(NNTP)
442 return command ("LAST");
450 return command ("RSET");
455 pop_top (int msgno
, int lines
, int (*action
)())
458 return traverse (action
, "TOP %d %d", (targ_t
) msgno
, (targ_t
) lines
);
460 return traverse (action
, "HEAD %d", (targ_t
) msgno
);
467 pop_xtnd (int (*action
)(), char *fmt
, ...)
479 /* needs to be fixed... va_end needs to be added */
480 snprintf (buffer
, sizeof(buffer
), "XTND %s", fmt
);
481 result
= traverse (action
, buffer
, a
, b
, c
, d
);
485 snprintf (buffer
, sizeof(buffer
), fmt
, a
, b
, c
, d
);
486 ap
= brkstring (buffer
, " ", "\n"); /* a hack, i know... */
488 if (!strcasecmp(ap
[0], "x-bboards")) { /* XTND "X-BBOARDS group */
489 /* most of these parameters are meaningless under NNTP.
490 * bbc.c was modified to set AKA and LEADERS as appropriate,
491 * the rest are left blank.
495 if (!strcasecmp (ap
[0], "archive") && ap
[1]) {
496 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
498 xtnd_first
= 1; /* setup to fail in pop_stat */
501 if (!strcasecmp (ap
[0], "bboards")) {
503 if (ap
[1]) { /* XTND "BBOARDS group" */
504 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
505 if (command("GROUP %s", xtnd_name
) == NOTOK
)
508 /* action must ignore extra args */
509 strncpy (buffer
, response
, sizeof(buffer
));
510 ap
= brkstring (response
, " ", "\n");/* "211 nart first last g" */
511 xtnd_first
= atoi (ap
[2]);
512 xtnd_last
= atoi (ap
[3]);
517 } else { /* XTND "BBOARDS" */
518 return traverse (action
, "LIST", a
, b
, c
, d
);
521 return NOTOK
; /* unknown XTND command */
532 i
= command ("QUIT");
549 #if !defined(MPOP) || defined(NNTP)
553 command(const char *fmt
, ...)
559 result
= vcommand(fmt
, ap
);
567 vcommand (const char *fmt
, va_list ap
)
569 char *cp
, buffer
[BUFSIZ
];
571 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
574 if ((cp
= strchr (buffer
, ' ')))
576 fprintf (stderr
, "---> %s ********\n", buffer
);
582 fprintf (stderr
, "---> %s\n", buffer
);
584 if (putline (buffer
, output
) == NOTOK
)
587 switch (getline (response
, sizeof response
, input
)) {
590 fprintf (stderr
, "<--- %s\n", response
);
592 return (*response
== '+' ? OK
: NOTOK
);
594 return (*response
< CHAR_ERR
? OK
: NOTOK
);
600 fprintf (stderr
, "%s\n", response
);
604 return NOTOK
; /* NOTREACHED */
608 #if defined(MPOP) && !defined(NNTP)
616 char buffer
[BUFSIZ
+ TRMLEN
];
618 if (getline (buffer
, sizeof buffer
, input
) != OK
)
622 fprintf (stderr
, "<--- %s\n", response
);
624 if (strncmp (buffer
, TRM
, TRMLEN
) == 0) {
625 if (buffer
[TRMLEN
] == 0)
628 strncpy (response
, buffer
+ TRMLEN
, sizeof(response
));
631 strncpy (response
, buffer
, sizeof(response
));
638 getline (char *s
, int n
, FILE *iop
)
644 while (--n
> 0 && (c
= fgetc (iop
)) != EOF
)
645 if ((*p
++ = c
) == '\n')
647 if (ferror (iop
) && c
!= EOF
) {
648 strncpy (response
, "error on connection", sizeof(response
));
651 if (c
== EOF
&& p
== s
) {
652 strncpy (response
, "connection closed by foreign host", sizeof(response
));
666 putline (char *s
, FILE *iop
)
668 fprintf (iop
, "%s\r\n", s
);
671 strncpy (response
, "lost connection", sizeof(response
));