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