]> Pileus Git - ~andy/fetchmail/blob - pop2.c
Yet morer error message cleanup.
[~andy/fetchmail] / pop2.c
1 /*
2  * pop2.c -- POP@ protocol methods
3  *
4  * Copyright 1996 by Eric S. Raymond
5  * All rights reserved.
6  * For license terms, see the file COPYING in this directory.
7  */
8
9 #include  <config.h>
10 #include  <stdio.h>
11 #if defined(STDC_HEADERS)
12 #include <stdlib.h>
13 #endif
14 #include  "socket.h"
15 #include  "fetchmail.h"
16
17 static int pound_arg, equal_arg;
18
19 int pop2_ok (FILE *sockfp, char *argbuf)
20 /* parse POP2 command response */
21 {
22     int ok;
23     char buf [POPBUFSIZE+1];
24
25     pound_arg = equal_arg = -1;
26     if (SockGets(buf, sizeof(buf), sockfp) >= 0) {
27         if (outlevel == O_VERBOSE)
28             error(0, 0, "< %s", buf);
29
30         if (buf[0] == '+')
31             ok = 0;
32         else if (buf[0] == '#')
33         {
34             pound_arg = atoi(buf+1);
35             ok = 0;
36         }
37         else if (buf[0] == '=')
38         {
39             equal_arg = atoi(buf+1);
40             ok = 0;
41         }
42         else if (buf[0] == '-')
43             ok = PS_ERROR;
44         else
45             ok = PS_PROTOCOL;
46
47         if (argbuf != NULL)
48             strcpy(argbuf,buf);
49     }
50     else 
51         ok = PS_SOCKET;
52
53     return(ok);
54 }
55
56 int pop2_getauth(FILE *sockfp, struct query *ctl, char *buf)
57 /* apply for connection authorization */
58 {
59     return(gen_transact(sockfp,
60                   "HELO %s %s",
61                   ctl->remotename, ctl->password));
62 }
63
64 static int pop2_getrange(FILE *sockfp, struct query *ctl, int*countp, int*newp)
65 /* get range of messages to be fetched */
66 {
67     /*
68      * We should have picked up a count of messages in the user's
69      * default inbox from the pop2_getauth() response.
70      */
71     if (pound_arg == -1)
72         return(PS_ERROR);
73
74     /* maybe the user wanted a non-default folder */
75     if (ctl->mailbox[0])
76     {
77         int     ok = gen_transact(sockfp, "FOLD %s", ctl->mailbox);
78
79         if (ok != 0)
80             return(ok);
81         if (pound_arg == -1)
82             return(PS_ERROR);
83     }
84
85     *countp = pound_arg;
86     *newp = -1;
87
88     return(0);
89 }
90
91 static int pop2_fetch(FILE *sockfp, int number, int *lenp)
92 /* request nth message */
93 {
94     int ok;
95
96     *lenp = 0;
97     ok = gen_transact(sockfp, "READ %d", number);
98     if (ok)
99         return(0);
100     *lenp = equal_arg;
101
102     gen_send(sockfp, "RETR");
103
104     return(ok);
105 }
106
107 static int pop2_trail(FILE *sockfp, struct query *ctl, int number)
108 /* send acknowledgement for message data */
109 {
110     return(gen_transact(sockfp, ctl->keep ? "ACKS" : "ACKD"));
111 }
112
113 const static struct method pop2 =
114 {
115     "POP2",                             /* Post Office Protocol v2 */
116     109,                                /* standard POP2 port */
117     0,                                  /* this is not a tagged protocol */
118     0,                                  /* does not use message delimiter */
119     pop2_ok,                            /* parse command response */
120     pop2_getauth,                       /* get authorization */
121     pop2_getrange,                      /* query range of messages */
122     NULL,                               /* no way to get sizes */
123     NULL,                               /* messages are always new */
124     pop2_fetch,                         /* request given message */
125     pop2_trail,                         /* eat message trailer */
126     NULL,                               /* no POP2 delete method */
127     NULL,                               /* no POP2 expunge command */
128     "QUIT",                             /* the POP2 exit command */
129 };
130
131 int doPOP2 (struct query *ctl)
132 /* retrieve messages using POP2 */
133 {
134     peek_capable = FALSE;
135     return(do_protocol(ctl, &pop2));
136 }
137
138 /* pop2.c ends here */