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