]> Pileus Git - ~andy/fetchmail/blob - pop2.c
1e49a6ca91f3305ef4e54648ea78bf181b34a02d
[~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)
67 /* get range of messages to be fetched */
68 int socket;
69 struct hostrec *queryctl;
70 int *countp;
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
92     return(0);
93 }
94
95 static int pop2_fetch(socket, number, lenp)
96 /* request nth message */
97 int socket;
98 int number;
99 int *lenp; 
100 {
101     int ok;
102
103     *lenp = 0;
104     ok = gen_transact(socket, "READ %d", number);
105     if (ok)
106         return(0);
107     *lenp = equal_arg;
108
109     gen_send(socket, "RETR");
110
111     return(ok);
112 }
113
114 static pop2_trail(socket, queryctl, number)
115 /* send acknowledgement for message data */
116 int socket;
117 struct hostrec *queryctl;
118 int number;
119 {
120     return(gen_transact(socket, queryctl->keep ? "ACKS" : "ACKD"));
121 }
122
123 const static struct method pop2 =
124 {
125     "POP2",                             /* Post Office Protocol v2 */
126     109,                                /* standard POP2 port */
127     0,                                  /* this is not a tagged protocol */
128     0,                                  /* does not use message delimiter */
129     pop2_ok,                            /* parse command response */
130     pop2_getauth,                       /* get authorization */
131     pop2_getrange,                      /* query range of messages */
132     NULL,                               /* messages are always new */
133     pop2_fetch,                         /* request given message */
134     pop2_trail,                         /* eat message trailer */
135     NULL,                               /* no POP2 delete method */
136     NULL,                               /* no POP2 expunge command */
137     "QUIT",                             /* the POP2 exit command */
138 };
139
140 int doPOP2 (queryctl)
141 /* retrieve messages using POP2 */
142 struct hostrec *queryctl;
143 {
144     return(do_protocol(queryctl, &pop2));
145 }
146
147 /* pop2.c ends here */