]> Pileus Git - ~andy/fetchmail/blob - etrn.c
_( -> GT_(
[~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, &opts)))
44     {
45         report(stderr, GT_("%s's SMTP listener does not support ESMTP\n"),
46               ctl->server.pollname);
47         return(ok);
48     }
49     else if (!(opts & ESMTP_ETRN))
50     {
51         report(stderr, GT_("%s's SMTP listener does not support ETRN\n"),
52               ctl->server.pollname);
53         return(PS_PROTOCOL);
54     }
55
56     /* make sure we don't enter the fetch loop */
57     *bytes = *countp = *newp = -1;
58
59     /*
60      * By default, the hostlist has a single entry, the fetchmail host's
61      * canonical DNS name.
62      */
63     for (qnp = ctl->domainlist; qnp; qnp = qnp->next)
64     {
65         /* ship the actual poll and get the response */
66         gen_send(sock, "ETRN %s", qnp->id);
67         if ((ok = gen_recv(sock, buf, sizeof(buf))))
68             return(ok);
69
70         /* this switch includes all response codes described in RFC1985 */
71         switch(atoi(buf))
72         {
73         case 250:       /* OK, queuing for node <x> started */
74             if (outlevel >= O_SILENT)
75                 report(stdout, GT_("Queuing for %s started\n"), qnp->id);
76             break;
77
78         case 251:       /* OK, no messages waiting for node <x> */
79             if (outlevel >= O_SILENT)
80                 report(stdout, GT_("No messages waiting for %s\n"), qnp->id);
81             return(PS_NOMAIL);
82
83         case 252:       /* OK, pending messages for node <x> started */
84         case 253:       /* OK, <n> pending messages for node <x> started */
85             if (outlevel >= O_SILENT)
86                 report(stdout, GT_("Pending messages for %s started\n"), qnp->id);
87             break;
88
89         case 458:       /* Unable to queue messages for node <x> */
90             report(stderr, GT_("Unable to queue messages for node %s\n"),qnp->id);
91             return(PS_PROTOCOL);
92
93         case 459:       /* Node <x> not allowed: <reason> */
94             report(stderr, GT_("Node %s not allowed: %s\n"), qnp->id, buf);
95             return(PS_AUTHFAIL);
96
97         case 500:       /* Syntax Error */
98             report(stderr, GT_("ETRN syntax error\n"));
99             return(PS_PROTOCOL);
100
101         case 501:       /* Syntax Error in Parameters */
102             report(stderr, GT_("ETRN syntax error in parameters\n"));
103             return(PS_PROTOCOL);
104
105         default:
106             report(stderr, GT_("Unknown ETRN error %d\n"), atoi(buf));
107             return(PS_PROTOCOL);
108         }
109     }
110
111     return(0);
112 }
113
114 static int etrn_logout(int sock, struct query *ctl)
115 /* send logout command */
116 {
117     return(gen_transact(sock, "QUIT"));
118 }
119
120 const static struct method etrn =
121 {
122     "ETRN",             /* ESMTP ETRN extension */
123 #if INET6_ENABLE
124     "smtp",             /* standard SMTP port */
125     "smtps",            /* ssl SMTP port */
126 #else /* INET6_ENABLE */
127     25,                 /* standard SMTP port */
128     465,                        /* ssl SMTP port */
129 #endif /* INET6_ENABLE */
130     FALSE,              /* this is not a tagged protocol */
131     FALSE,              /* this does not use a message delimiter */
132     etrn_ok,            /* parse command response */
133     NULL,               /* no need to get authentication */
134     etrn_getrange,      /* initialize message sending */
135     NULL,               /* we cannot get a list of sizes */
136     NULL,               /* how do we tell a message is old? */
137     NULL,               /* no way to fetch headers */
138     NULL,               /* no way to fetch body */
139     NULL,               /* no message trailer */
140     NULL,               /* how to delete a message */
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 --remote 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 */