]> Pileus Git - ~andy/fetchmail/blob - etrn.c
Fix typo repsonsible -> responsible.
[~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 #include  <netdb.h>
13 #include  <errno.h>
14 #include  <unistd.h>
15 #include  "gettext.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     (void)argbuf;
26     ok = SMTP_ok(sock, SMTP_MODE, TIMEOUT_DEFAULT);
27     if (ok == SM_UNRECOVERABLE)
28         return(PS_PROTOCOL);
29     else
30         return(ok);
31 }
32
33 static int etrn_getrange(int sock, struct query *ctl, const char *id, 
34                          int *countp, int *newp, int *bytes)
35 /* send ETRN and interpret the response */
36 {
37     int ok, opts;
38     char buf [MSGBUFSIZE+1];
39     struct idlist *qnp;         /* pointer to Q names */
40
41     (void)id;
42     if ((ok = SMTP_ehlo(sock, SMTP_MODE, fetchmailhost,
43                         ctl->server.esmtp_name, ctl->server.esmtp_password,
44                         &opts)))
45     {
46         report(stderr, GT_("%s's SMTP listener does not support ESMTP\n"),
47               ctl->server.pollname);
48         return(ok);
49     }
50     else if (!(opts & ESMTP_ETRN))
51     {
52         report(stderr, GT_("%s's SMTP listener does not support ETRN\n"),
53               ctl->server.pollname);
54         return(PS_PROTOCOL);
55     }
56
57     /* make sure we don't enter the fetch loop */
58     *bytes = *countp = *newp = -1;
59
60     /*
61      * By default, the hostlist has a single entry, the fetchmail host's
62      * canonical DNS name.
63      */
64     for (qnp = ctl->domainlist; qnp; qnp = qnp->next)
65     {
66         /* ship the actual poll and get the response */
67         gen_send(sock, "ETRN %s", qnp->id);
68         if ((ok = gen_recv(sock, buf, sizeof(buf))))
69             return(ok);
70
71         /* this switch includes all response codes described in RFC1985 */
72         switch(atoi(buf))
73         {
74         case 250:       /* OK, queuing for node <x> started */
75             if (outlevel > O_SILENT)
76                 report(stdout, GT_("Queuing for %s started\n"), qnp->id);
77             break;
78
79         case 251:       /* OK, no messages waiting for node <x> */
80             if (outlevel > O_SILENT)
81                 report(stdout, GT_("No messages waiting for %s\n"), qnp->id);
82             return(PS_NOMAIL);
83
84         case 252:       /* OK, pending messages for node <x> started */
85         case 253:       /* OK, <n> pending messages for node <x> started */
86             if (outlevel > O_SILENT)
87                 report(stdout, GT_("Pending messages for %s started\n"), qnp->id);
88             break;
89
90         case 458:       /* Unable to queue messages for node <x> */
91             report(stderr, GT_("Unable to queue messages for node %s\n"),qnp->id);
92             return(PS_PROTOCOL);
93
94         case 459:       /* Node <x> not allowed: <reason> */
95             report(stderr, GT_("Node %s not allowed: %s\n"), qnp->id, buf);
96             return(PS_AUTHFAIL);
97
98         case 500:       /* Syntax Error */
99             report(stderr, GT_("ETRN syntax error\n"));
100             return(PS_PROTOCOL);
101
102         case 501:       /* Syntax Error in Parameters */
103             report(stderr, GT_("ETRN syntax error in parameters\n"));
104             return(PS_PROTOCOL);
105
106         default:
107             report(stderr, GT_("Unknown ETRN error %d\n"), atoi(buf));
108             return(PS_PROTOCOL);
109         }
110     }
111
112     return(0);
113 }
114
115 static int etrn_logout(int sock, struct query *ctl)
116 /* send logout command */
117 {
118     (void)ctl;
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 */