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