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