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