]>
diplodocus.org Git - nmh/blob - uip/popsbr.c
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
, int kpop
)
129 if ((apop
= rpop
) < 0)
136 snprintf (buffer
, sizeof(buffer
), "%s/%s", KPOP_PRINCIPAL
, "kpop");
137 if ((fd1
= client (host
, "tcp", buffer
, 0, response
, sizeof(response
))) == NOTOK
) {
142 if ((fd1
= client (host
, "tcp", POPSERVICE
, rpop
, response
, sizeof(response
))) == NOTOK
)
148 if ((fd1
= client (host
, "tcp", "nntp", rpop
, response
, sizeof(response
))) == NOTOK
)
152 if ((fd2
= dup (fd1
)) == NOTOK
) {
155 if ((s
= strerror(errno
)))
156 snprintf (response
, sizeof(response
),
157 "unable to dup connection descriptor: %s", s
);
159 snprintf (response
, sizeof(response
),
160 "unable to dup connection descriptor: unknown error");
165 if (pop_set (fd1
, fd2
, snoop
) == NOTOK
)
167 if (pop_set (fd1
, fd2
, snoop
, (char *)0) == NOTOK
)
171 SIGNAL (SIGPIPE
, SIG_IGN
);
173 switch (getline (response
, sizeof response
, input
)) {
176 fprintf (stderr
, "<--- %s\n", response
);
178 if (*response
== '+') {
182 char *cp
= pop_auth (user
, pass
);
184 if (cp
&& command ("APOP %s", cp
) != NOTOK
)
189 if (command ("USER %s", user
) != NOTOK
190 && command ("%s %s", rpop
? "RPOP" : (pophack
++, "PASS"),
194 if (command ("USER %s", user
) != NOTOK
195 && command ("PASS %s", pass
) != NOTOK
)
200 if (*response
< CHAR_ERR
) {
201 command ("MODE READER");
205 strncpy (buffer
, response
, sizeof(buffer
));
207 strncpy (response
, buffer
, sizeof(response
));
213 fprintf (stderr
, "%s\n", response
);
219 return NOTOK
; /* NOTREACHED */
224 pop_set (int in
, int out
, int snoop
, char *myname
)
227 pop_set (int in
, int out
, int snoop
)
232 if (myname
&& *myname
) {
233 /* interface from bbc to msh */
234 strncpy (xtnd_name
, myname
, sizeof(xtnd_name
));
238 if ((input
= fdopen (in
, "r")) == NULL
239 || (output
= fdopen (out
, "w")) == NULL
) {
240 strncpy (response
, "fdopen failed on connection descriptor", sizeof(response
));
256 pop_fd (char *in
, int inlen
, char *out
, int outlen
)
258 snprintf (in
, inlen
, "%d", fileno (input
));
259 snprintf (out
, outlen
, "%d", fileno (output
));
265 * Find out number of messages available
266 * and their total size.
270 pop_stat (int *nmsgs
, int *nbytes
)
277 if (command ("STAT") == NOTOK
)
280 *nmsgs
= *nbytes
= 0;
281 sscanf (response
, "+OK %d %d", nmsgs
, nbytes
);
284 if (xtnd_last
< 0) { /* in msh, xtnd_name is set from myname */
285 if (command("GROUP %s", xtnd_name
) == NOTOK
)
288 ap
= brkstring (response
, " ", "\n"); /* "211 nart first last ggg" */
289 xtnd_first
= atoi (ap
[2]);
290 xtnd_last
= atoi (ap
[3]);
293 /* nmsgs is not the real nart, but an incredible simuation */
295 *nmsgs
= xtnd_last
- xtnd_first
+ 1; /* because of holes... */
298 *nbytes
= xtnd_first
; /* for subtracting offset in msh() */
306 pop_exists (int (*action
)())
308 #ifdef XMSGS /* hacked into NNTP 1.5 */
309 if (traverse (action
, "XMSGS %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
) == OK
)
312 /* provided by INN 1.4 */
313 if (traverse (action
, "LISTGROUP") == OK
)
315 return traverse (action
, "XHDR NONAME %d-%d", (targ_t
) xtnd_first
, (targ_t
) xtnd_last
);
322 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
, int *ids
)
325 pop_list (int msgno
, int *nmsgs
, int *msgs
, int *bytes
)
335 if (command ("LIST %d", msgno
) == NOTOK
)
340 sscanf (response
, "+OK %d %d %d", msgs
, bytes
, ids
);
343 sscanf (response
, "+OK %d %d", msgs
, bytes
);
346 if (command ("STAT %d", msgno
) == NOTOK
)
356 if (command ("LIST") == NOTOK
)
359 for (i
= 0; i
< *nmsgs
; i
++)
360 switch (multiline ()) {
370 sscanf (response
, "%d %d %d",
371 msgs
++, bytes
++, ids
++);
374 sscanf (response
, "%d %d", msgs
++, bytes
++);
378 switch (multiline ()) {
393 pop_retr (int msgno
, int (*action
)())
396 return traverse (action
, "RETR %d", (targ_t
) msgno
);
398 return traverse (action
, "ARTICLE %d", (targ_t
) msgno
);
404 traverse (int (*action
)(), const char *fmt
, ...)
408 char buffer
[sizeof(response
)];
411 result
= vcommand (fmt
, ap
);
416 strncpy (buffer
, response
, sizeof(buffer
));
419 switch (multiline ()) {
424 strncpy (response
, buffer
, sizeof(response
));
428 (*action
) (response
);
437 return command ("DELE %d", msgno
);
444 return command ("NOOP");
448 #if defined(MPOP) && !defined(NNTP)
452 return command ("LAST");
460 return command ("RSET");
465 pop_top (int msgno
, int lines
, int (*action
)())
468 return traverse (action
, "TOP %d %d", (targ_t
) msgno
, (targ_t
) lines
);
470 return traverse (action
, "HEAD %d", (targ_t
) msgno
);
477 pop_xtnd (int (*action
)(), char *fmt
, ...)
489 /* needs to be fixed... va_end needs to be added */
490 snprintf (buffer
, sizeof(buffer
), "XTND %s", fmt
);
491 result
= traverse (action
, buffer
, a
, b
, c
, d
);
495 snprintf (buffer
, sizeof(buffer
), fmt
, a
, b
, c
, d
);
496 ap
= brkstring (buffer
, " ", "\n"); /* a hack, i know... */
498 if (!strcasecmp(ap
[0], "x-bboards")) { /* XTND "X-BBOARDS group */
499 /* most of these parameters are meaningless under NNTP.
500 * bbc.c was modified to set AKA and LEADERS as appropriate,
501 * the rest are left blank.
505 if (!strcasecmp (ap
[0], "archive") && ap
[1]) {
506 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
508 xtnd_first
= 1; /* setup to fail in pop_stat */
511 if (!strcasecmp (ap
[0], "bboards")) {
513 if (ap
[1]) { /* XTND "BBOARDS group" */
514 snprintf (xtnd_name
, sizeof(xtnd_name
), "%s", ap
[1]); /* save the name */
515 if (command("GROUP %s", xtnd_name
) == NOTOK
)
518 /* action must ignore extra args */
519 strncpy (buffer
, response
, sizeof(buffer
));
520 ap
= brkstring (response
, " ", "\n");/* "211 nart first last g" */
521 xtnd_first
= atoi (ap
[2]);
522 xtnd_last
= atoi (ap
[3]);
527 } else { /* XTND "BBOARDS" */
528 return traverse (action
, "LIST", a
, b
, c
, d
);
531 return NOTOK
; /* unknown XTND command */
542 i
= command ("QUIT");
559 #if !defined(MPOP) || defined(NNTP)
563 command(const char *fmt
, ...)
569 result
= vcommand(fmt
, ap
);
577 vcommand (const char *fmt
, va_list ap
)
579 char *cp
, buffer
[BUFSIZ
];
581 vsnprintf (buffer
, sizeof(buffer
), fmt
, ap
);
584 if ((cp
= strchr (buffer
, ' ')))
586 fprintf (stderr
, "---> %s ********\n", buffer
);
592 fprintf (stderr
, "---> %s\n", buffer
);
595 if (putline (buffer
, output
) == NOTOK
)
598 switch (getline (response
, sizeof response
, input
)) {
601 fprintf (stderr
, "<--- %s\n", response
);
603 return (*response
== '+' ? OK
: NOTOK
);
605 return (*response
< CHAR_ERR
? OK
: NOTOK
);
611 fprintf (stderr
, "%s\n", response
);
615 return NOTOK
; /* NOTREACHED */
619 #if defined(MPOP) && !defined(NNTP)
627 char buffer
[BUFSIZ
+ TRMLEN
];
629 if (getline (buffer
, sizeof buffer
, input
) != OK
)
633 fprintf (stderr
, "<--- %s\n", response
);
635 if (strncmp (buffer
, TRM
, TRMLEN
) == 0) {
636 if (buffer
[TRMLEN
] == 0)
639 strncpy (response
, buffer
+ TRMLEN
, sizeof(response
));
642 strncpy (response
, buffer
, sizeof(response
));
649 getline (char *s
, int n
, FILE *iop
)
655 while (--n
> 0 && (c
= fgetc (iop
)) != EOF
)
656 if ((*p
++ = c
) == '\n')
658 if (ferror (iop
) && c
!= EOF
) {
659 strncpy (response
, "error on connection", sizeof(response
));
662 if (c
== EOF
&& p
== s
) {
663 strncpy (response
, "connection closed by foreign host", sizeof(response
));
677 putline (char *s
, FILE *iop
)
679 fprintf (iop
, "%s\r\n", s
);
682 strncpy (response
, "lost connection", sizeof(response
));