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