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