]> Pileus Git - ~andy/fetchmail/blob - pop2.c
Added filtering by length. Ugh.
[~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 #include  "socket.h"
12 #include  "fetchmail.h"
13
14 static int pound_arg, equal_arg;
15
16 int pop2_ok (socket, argbuf)
17 /* parse POP2 command response */
18 int socket;
19 char *argbuf;
20 {
21     int ok;
22     char buf [POPBUFSIZE+1];
23
24     pound_arg = equal_arg = -1;
25     if (SockGets(socket, buf, sizeof(buf)) >= 0) {
26         if (outlevel == O_VERBOSE)
27             fprintf(stderr,"%s\n",buf);
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     else 
50         ok = PS_SOCKET;
51
52     return(ok);
53 }
54
55 int pop2_getauth(socket, queryctl, buf)
56 /* apply for connection authorization */
57 int socket;
58 struct hostrec *queryctl;
59 char *buf;
60 {
61     return(gen_transact(socket,
62                   "HELO %s %s",
63                   queryctl->remotename, queryctl->password));
64 }
65
66 static pop2_getrange(socket, queryctl, countp, newp)
67 /* get range of messages to be fetched */
68 int socket;
69 struct hostrec *queryctl;
70 int *countp, *newp;
71 {
72     /*
73      * We should have picked up a count of messages in the user's
74      * default inbox from the pop2_getauth() response.
75      */
76     if (pound_arg == -1)
77         return(PS_ERROR);
78
79     /* maybe the user wanted a non-default folder */
80     if (queryctl->mailbox[0])
81     {
82         int     ok = gen_transact(socket, "FOLD %s", queryctl->mailbox);
83
84         if (ok != 0)
85             return(ok);
86         if (pound_arg == -1)
87             return(PS_ERROR);
88     }
89
90     *countp = pound_arg;
91     *newp = -1;
92
93     return(0);
94 }
95
96 static int pop2_fetch(socket, number, lenp)
97 /* request nth message */
98 int socket;
99 int number;
100 int *lenp; 
101 {
102     int ok;
103
104     *lenp = 0;
105     ok = gen_transact(socket, "READ %d", number);
106     if (ok)
107         return(0);
108     *lenp = equal_arg;
109
110     gen_send(socket, "RETR");
111
112     return(ok);
113 }
114
115 static pop2_trail(socket, queryctl, number)
116 /* send acknowledgement for message data */
117 int socket;
118 struct hostrec *queryctl;
119 int number;
120 {
121     return(gen_transact(socket, queryctl->keep ? "ACKS" : "ACKD"));
122 }
123
124 const static struct method pop2 =
125 {
126     "POP2",                             /* Post Office Protocol v2 */
127     109,                                /* standard POP2 port */
128     0,                                  /* this is not a tagged protocol */
129     0,                                  /* does not use message delimiter */
130     pop2_ok,                            /* parse command response */
131     pop2_getauth,                       /* get authorization */
132     pop2_getrange,                      /* query range of messages */
133     NULL,                               /* no way to get sizes */
134     NULL,                               /* messages are always new */
135     pop2_fetch,                         /* request given message */
136     pop2_trail,                         /* eat message trailer */
137     NULL,                               /* no POP2 delete method */
138     NULL,                               /* no POP2 expunge command */
139     "QUIT",                             /* the POP2 exit command */
140 };
141
142 int doPOP2 (queryctl)
143 /* retrieve messages using POP2 */
144 struct hostrec *queryctl;
145 {
146     return(do_protocol(queryctl, &pop2));
147 }
148
149 /* pop2.c ends here */