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