]> Pileus Git - ~andy/fetchmail/blob - pop2.c
Track due time and prevent excess repelling
[~andy/fetchmail] / pop2.c
1 /*
2  * pop2.c -- POP2 protocol methods
3  *
4  * Copyright 1997 by Eric S. Raymond
5  * For license terms, see the file COPYING in this directory.
6  */
7
8 #include  "config.h"
9
10 #ifdef POP2_ENABLE
11 #include  "fetchmail.h"
12 #include  <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include  "socket.h"
16 #include  "i18n.h"
17
18 static int pound_arg, equal_arg;
19
20 static int pop2_ok (int sock, char *argbuf)
21 /* parse POP2 command response */
22 {
23     int ok;
24     char buf [POPBUFSIZE+1];
25
26     pound_arg = equal_arg = -1;
27
28     if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
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
51     return(ok);
52 }
53
54 static int pop2_getauth(int sock, struct query *ctl, char *buf)
55 /* apply for connection authorization */
56 {
57     int status;
58
59     (void)buf;
60
61     if (ctl->sslproto && !strcasecmp(ctl->sslproto, "tls1") && !ctl->use_ssl)
62     {
63         report(stderr, GT_("POP2 does not support STLS. Giving up.\n"));
64         return PS_SOCKET;
65     }
66
67     if (ctl->server.authenticate != A_ANY && ctl->server.authenticate != A_PASSWORD)
68     {
69         report(stderr, GT_("POP2 only supports password authentication. Giving up.\n"));
70         return PS_AUTHFAIL;
71     }
72
73     strlcpy(shroud, ctl->password, sizeof(shroud));
74     status = gen_transact(sock,
75                   "HELO %s %s",
76                   ctl->remotename, ctl->password);
77     memset(shroud, 0x55, sizeof(shroud));
78     shroud[0] = '\0';
79     return status;
80 }
81
82 static int pop2_getrange(int sock, struct query *ctl, const char *folder, 
83                          int *countp, int *newp, unsigned long long *bytes)
84 /* get range of messages to be fetched */
85 {
86     (void)ctl;
87
88     /* maybe the user wanted a non-default folder */
89     if (folder)
90     {
91         int     ok = gen_transact(sock, "FOLD %s", folder);
92
93         if (ok != 0)
94             return(ok);
95         if (pound_arg == -1)
96             return(PS_ERROR);
97     }
98     else
99         /*
100          * We should have picked up a count of messages in the user's
101          * default inbox from the pop2_getauth() response. 
102          *
103          * Note: this logic only works because there is no way to select
104          * both the unnamed folder and named folders within a single
105          * fetchmail run.  If that assumption ever becomes invalid, the
106          * pop2_getauth code will have to stash the pound response away
107          * explicitly in case it gets stepped on.
108          */
109         if (pound_arg == -1)
110             return(PS_ERROR);
111
112     *countp = pound_arg;
113     *bytes = *newp = -1;
114
115     return(0);
116 }
117
118 static int pop2_fetch(int sock, struct query *ctl, int number, int *lenp)
119 /* request nth message */
120 {
121     int ok;
122
123     (void)ctl;
124     *lenp = 0;
125     ok = gen_transact(sock, "READ %d", number);
126     if (ok)
127         return(0);
128     *lenp = equal_arg;
129
130     gen_send(sock, "RETR");
131
132     return(ok);
133 }
134
135 static int pop2_trail(int sock, struct query *ctl, const char *tag)
136 /* send acknowledgement for message data */
137 {
138     (void)ctl;
139     (void)tag;
140     return(gen_transact(sock, ctl->keep ? "ACKS" : "ACKD"));
141 }
142
143 static int pop2_logout(int sock, struct query *ctl)
144 /* send logout command */
145 {
146     (void)ctl;
147     return(gen_transact(sock, "QUIT"));
148 }
149
150 static const struct method pop2 =
151 {
152     "POP2",                             /* Post Office Protocol v2 */
153     "pop2",                             /* standard POP2 port */
154     "pop2",                             /* ssl POP2 port - not */
155     FALSE,                              /* this is not a tagged protocol */
156     FALSE,                              /* does not use message delimiter */
157     pop2_ok,                            /* parse command response */
158     pop2_getauth,                       /* get authorization */
159     pop2_getrange,                      /* query range of messages */
160     NULL,                               /* no way to get sizes */
161     NULL,                               /* no way to get sizes of subsets */
162     NULL,                               /* messages are always new */
163     pop2_fetch,                         /* request given message */
164     NULL,                               /* no way to fetch body alone */
165     pop2_trail,                         /* eat message trailer */
166     NULL,                               /* no POP2 delete method */
167     NULL,                               /* how to mark a message as seen */
168     NULL,                               /* how to end mailbox processing */
169     pop2_logout,                        /* log out, we're done */
170     FALSE                               /* no, we can't re-poll */
171 };
172
173 int doPOP2 (struct query *ctl)
174 /* retrieve messages using POP2 */
175 {
176     peek_capable = FALSE;
177     return(do_protocol(ctl, &pop2));
178 }
179 #endif /* POP2_ENABLE */
180
181 /* pop2.c ends here */