]> Pileus Git - ~andy/fetchmail/blob - etrn.c
Revise some error messages so they are less confusing. Sunil Shetye.
[~andy/fetchmail] / etrn.c
1 /*
2  * etrn.c -- ETRN protocol methods (see RFC 1985)
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include  "config.h"
8 #ifdef ETRN_ENABLE
9 #include  <stdio.h>
10 #include  <stdlib.h>
11 #include  <assert.h>
12 #ifdef HAVE_NET_SOCKET_H /* BeOS needs this */
13 #include <net/socket.h>
14 #endif
15 #include  <netdb.h>
16 #include  <errno.h>
17 #include  <unistd.h>
18 #include  "i18n.h"
19 #include  "fetchmail.h"
20 #include  "smtp.h"
21 #include  "socket.h"
22
23 static int etrn_ok (int sock, char *argbuf)
24 /* parse command response */
25 {
26     int ok;
27
28     ok = SMTP_ok(sock);
29     if (ok == SM_UNRECOVERABLE)
30         return(PS_PROTOCOL);
31     else
32         return(ok);
33 }
34
35 static int etrn_getrange(int sock, struct query *ctl, const char *id, 
36                          int *countp, int *newp, int *bytes)
37 /* send ETRN and interpret the response */
38 {
39     int ok, opts;
40     char buf [MSGBUFSIZE+1];
41     struct idlist *qnp;         /* pointer to Q names */
42
43     if ((ok = SMTP_ehlo(sock, fetchmailhost, 
44                         ctl->server.esmtp_name, ctl->server.esmtp_password,
45                         &opts)))
46     {
47         report(stderr, GT_("%s's SMTP listener does not support ESMTP\n"),
48               ctl->server.pollname);
49         return(ok);
50     }
51     else if (!(opts & ESMTP_ETRN))
52     {
53         report(stderr, GT_("%s's SMTP listener does not support ETRN\n"),
54               ctl->server.pollname);
55         return(PS_PROTOCOL);
56     }
57
58     /* make sure we don't enter the fetch loop */
59     *bytes = *countp = *newp = -1;
60
61     /*
62      * By default, the hostlist has a single entry, the fetchmail host's
63      * canonical DNS name.
64      */
65     for (qnp = ctl->domainlist; qnp; qnp = qnp->next)
66     {
67         /* ship the actual poll and get the response */
68         gen_send(sock, "ETRN %s", (char *)qnp->id);
69         if ((ok = gen_recv(sock, buf, sizeof(buf))))
70             return(ok);
71
72         /* this switch includes all response codes described in RFC1985 */
73         switch(atoi(buf))
74         {
75         case 250:       /* OK, queuing for node <x> started */
76             if (outlevel > O_SILENT)
77                 report(stdout, GT_("Queuing for %s started\n"), qnp->id);
78             break;
79
80         case 251:       /* OK, no messages waiting for node <x> */
81             if (outlevel > O_SILENT)
82                 report(stdout, GT_("No messages waiting for %s\n"), qnp->id);
83             return(PS_NOMAIL);
84
85         case 252:       /* OK, pending messages for node <x> started */
86         case 253:       /* OK, <n> pending messages for node <x> started */
87             if (outlevel > O_SILENT)
88                 report(stdout, GT_("Pending messages for %s started\n"), qnp->id);
89             break;
90
91         case 458:       /* Unable to queue messages for node <x> */
92             report(stderr, GT_("Unable to queue messages for node %s\n"),qnp->id);
93             return(PS_PROTOCOL);
94
95         case 459:       /* Node <x> not allowed: <reason> */
96             report(stderr, GT_("Node %s not allowed: %s\n"), qnp->id, buf);
97             return(PS_AUTHFAIL);
98
99         case 500:       /* Syntax Error */
100             report(stderr, GT_("ETRN syntax error\n"));
101             return(PS_PROTOCOL);
102
103         case 501:       /* Syntax Error in Parameters */
104             report(stderr, GT_("ETRN syntax error in parameters\n"));
105             return(PS_PROTOCOL);
106
107         default:
108             report(stderr, GT_("Unknown ETRN error %d\n"), atoi(buf));
109             return(PS_PROTOCOL);
110         }
111     }
112
113     return(0);
114 }
115
116 static int etrn_logout(int sock, struct query *ctl)
117 /* send logout command */
118 {
119     return(gen_transact(sock, "QUIT"));
120 }
121
122 static const struct method etrn =
123 {
124     "ETRN",             /* ESMTP ETRN extension */
125     "smtp",             /* standard SMTP port */
126     "smtps",            /* ssl SMTP port */
127     FALSE,              /* this is not a tagged protocol */
128     FALSE,              /* this does not use a message delimiter */
129     etrn_ok,            /* parse command response */
130     NULL,               /* no need to get authentication */
131     etrn_getrange,      /* initialize message sending */
132     NULL,               /* we cannot get a list of sizes */
133     NULL,               /* we cannot get a list of sizes of subsets */
134     NULL,               /* how do we tell a message is old? */
135     NULL,               /* no way to fetch headers */
136     NULL,               /* no way to fetch body */
137     NULL,               /* no message trailer */
138     NULL,               /* how to delete a message */
139     NULL,               /* how to mark a message as seen */
140     NULL,               /* no mailbox support */
141     etrn_logout,        /* log out, we're done */
142     FALSE,              /* no, we can't re-poll */
143 };
144
145 int doETRN (struct query *ctl)
146 /* retrieve messages using ETRN */
147 {
148     int status;
149
150     if (ctl->keep) {
151         fprintf(stderr, GT_("Option --keep is not supported with ETRN\n"));
152         return(PS_SYNTAX);
153     }
154     if (ctl->flush) {
155         fprintf(stderr, GT_("Option --flush is not supported with ETRN\n"));
156         return(PS_SYNTAX);
157     }
158     if (ctl->mailboxes->id) {
159         fprintf(stderr, GT_("Option --folder is not supported with ETRN\n"));
160         return(PS_SYNTAX);
161     }
162     if (check_only) {
163         fprintf(stderr, GT_("Option --check is not supported with ETRN\n"));
164         return(PS_SYNTAX);
165     }
166     peek_capable = FALSE;
167
168     status = do_protocol(ctl, &etrn);
169     if (status == PS_NOMAIL)
170         status = PS_SUCCESS;
171     return(status);
172 }
173 #endif /* ETRN_ENABLE */
174
175 /* etrn.c ends here */