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