]> Pileus Git - ~andy/fetchmail/blob - pop2.c
Change type of socket descriptors from FILE * to int. Change SockGets
[~andy/fetchmail] / pop2.c
1 /*
2  * pop2.c -- POP@ protocol methods
3  *
4  * Copyright 1996 by Eric S. Raymond
5  * All rights reserved.
6  * For license terms, see the file COPYING in this directory.
7  */
8
9 #include  <config.h>
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, int*countp, int*newp)
62 /* get range of messages to be fetched */
63 {
64     /*
65      * We should have picked up a count of messages in the user's
66      * default inbox from the pop2_getauth() response.
67      */
68     if (pound_arg == -1)
69         return(PS_ERROR);
70
71     /* maybe the user wanted a non-default folder */
72     if (ctl->mailbox)
73     {
74         int     ok = gen_transact(sock, "FOLD %s", ctl->mailbox);
75
76         if (ok != 0)
77             return(ok);
78         if (pound_arg == -1)
79             return(PS_ERROR);
80     }
81
82     *countp = pound_arg;
83     *newp = -1;
84
85     return(0);
86 }
87
88 static int pop2_fetch(int sock, struct query *ctl, int number, int *lenp)
89 /* request nth message */
90 {
91     int ok;
92
93     *lenp = 0;
94     ok = gen_transact(sock, "READ %d", number);
95     if (ok)
96         return(0);
97     *lenp = equal_arg;
98
99     gen_send(sock, "RETR");
100
101     return(ok);
102 }
103
104 static int pop2_trail(int sock, struct query *ctl, int number)
105 /* send acknowledgement for message data */
106 {
107     return(gen_transact(sock, ctl->keep ? "ACKS" : "ACKD"));
108 }
109
110 const static struct method pop2 =
111 {
112     "POP2",                             /* Post Office Protocol v2 */
113     109,                                /* standard POP2 port */
114     0,                                  /* this is not a tagged protocol */
115     0,                                  /* does not use message delimiter */
116     pop2_ok,                            /* parse command response */
117     pop2_getauth,                       /* get authorization */
118     pop2_getrange,                      /* query range of messages */
119     NULL,                               /* no way to get sizes */
120     NULL,                               /* messages are always new */
121     pop2_fetch,                         /* request given message */
122     pop2_trail,                         /* eat message trailer */
123     NULL,                               /* no POP2 delete method */
124     "QUIT",                             /* the POP2 exit command */
125 };
126
127 int doPOP2 (struct query *ctl)
128 /* retrieve messages using POP2 */
129 {
130     peek_capable = FALSE;
131     return(do_protocol(ctl, &pop2));
132 }
133
134 /* pop2.c ends here */