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