]> Pileus Git - ~andy/fetchmail/blob - pop2.c
RC file syntax change, folder->mailbox, remote->remotename.
[~andy/fetchmail] / pop2.c
1 /* Copyright 1996 by Eric S. Raymond
2  * All rights reserved.
3  * For license terms, see the file COPYING in this directory.
4  */
5
6 /***********************************************************************
7   module:       pop2.c
8   project:      fetchmail
9   programmer:   Eric S. Raymond
10   description:  POP2 method code.
11
12  ***********************************************************************/
13
14 #include  <config.h>
15 #include  <stdio.h>
16 #include  "socket.h"
17 #include  "fetchmail.h"
18
19 static int pound_arg, equal_arg;
20
21 int pop2_ok (socket, argbuf)
22 /* parse POP2 command response */
23 int socket;
24 char *argbuf;
25 {
26     int ok;
27     char buf [POPBUFSIZE+1];
28
29     pound_arg = equal_arg = -1;
30     if (SockGets(socket, buf, sizeof(buf)) >= 0) {
31         if (outlevel == O_VERBOSE)
32             fprintf(stderr,"%s\n",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(socket, queryctl, buf)
61 /* apply for connection authorization */
62 int socket;
63 struct hostrec *queryctl;
64 char *buf;
65 {
66     return(gen_transact(socket,
67                   "HELO %s %s",
68                   queryctl->remotename, queryctl->password));
69 }
70
71 static pop2_getrange(socket, queryctl, countp)
72 /* get range of messages to be fetched */
73 int socket;
74 struct hostrec *queryctl;
75 int *countp;
76 {
77     /*
78      * We should have picked up a count of messages in the user's
79      * default inbox from the pop2_getauth() response.
80      */
81     if (pound_arg == -1)
82         return(PS_ERROR);
83
84     /* maybe the user wanted a non-default folder */
85     if (queryctl->mailbox[0])
86     {
87         int     ok = gen_transact(socket, "FOLD %s", queryctl->mailbox);
88
89         if (ok != 0)
90             return(ok);
91         if (pound_arg == -1)
92             return(PS_ERROR);
93     }
94
95     *countp = pound_arg;
96
97     return(0);
98 }
99
100 static int pop2_fetch(socket, number, lenp)
101 /* request nth message */
102 int socket;
103 int number;
104 int *lenp; 
105 {
106     int ok;
107
108     *lenp = 0;
109     ok = gen_transact(socket, "READ %d", number);
110     if (ok)
111         return(0);
112     *lenp = equal_arg;
113
114     gen_send(socket, "RETR");
115
116     return(ok);
117 }
118
119 static pop2_trail(socket, queryctl, number)
120 /* send acknowledgement for message data */
121 int socket;
122 struct hostrec *queryctl;
123 int number;
124 {
125     return(gen_transact(socket, queryctl->keep ? "ACKS" : "ACKD"));
126 }
127
128 const static struct method pop2 =
129 {
130     "POP2",                             /* Post Office Protocol v2 */
131     109,                                /* standard POP2 port */
132     0,                                  /* this is not a tagged protocol */
133     0,                                  /* does not use message delimiter */
134     pop2_ok,                            /* parse command response */
135     pop2_getauth,                       /* get authorization */
136     pop2_getrange,                      /* query range of messages */
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 (queryctl)
146 struct hostrec *queryctl;
147 {
148     return(do_protocol(queryctl, &pop2));
149 }