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