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