2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
26 * $TOG: IMAPServer.C /main/7 1998/11/10 17:08:32 mgreess $
28 * RESTRICTED CONFIDENTIAL INFORMATION:
30 * The information in this document is subject to special
31 * restrictions in a confidential disclosure agreement between
32 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
33 * document outside HP, IBM, Sun, USL, SCO, or Univel without
34 * Sun's specific written approval. This document and all copies
35 * and derivative works thereof must be returned or destroyed at
38 * Copyright 1993, 1995, 1995 Sun Microsystems, Inc. All rights reserved.
43 * Common Desktop Environment
45 * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
46 * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
47 * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
48 * (c) Copyright 1993, 1994, 1995 Novell, Inc.
49 * (c) Copyright 1995 Digital Equipment Corp.
50 * (c) Copyright 1995 Fujitsu Limited
51 * (c) Copyright 1995 Hitachi, Ltd.
54 * RESTRICTED RIGHTS LEGEND
56 *Use, duplication, or disclosure by the U.S. Government is subject to
57 *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
58 *Technical Data and Computer Software clause in DFARS 252.227-7013. Rights
59 *for non-DOD U.S. Government Departments and Agencies are as set forth in
60 *FAR 52.227-19(c)(1,2).
62 *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
63 *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A.
64 *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
65 *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
66 *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
67 *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
68 *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
76 #include <DtMail/DtMailServer.hh>
77 #include <DtMail/IO.hh>
79 #define dtmasTAGGET() (_transtag)
81 IMAPServer::IMAPServer(
83 DtMail::Session *session,
84 DtMail::MailBox *mailbox,
85 DtMailAppendCallback append_mailbox_cb,
86 void *append_mailbox_cb_data)
87 : DtMailServer(folder, session, mailbox,
88 append_mailbox_cb, append_mailbox_cb_data)
90 _append_mailbox_cb = append_mailbox_cb;
91 _append_mailbox_cb_data = append_mailbox_cb_data;
99 IMAPServer::~IMAPServer()
105 // Set delete flag for given message.
108 IMAPServer::ptrans_delete(int msg)
110 // Use SILENT if possible as a minor throughput optimization.
112 return do_transaction("STORE %d +FLAGS.SILENT (\\Deleted)", msg);
114 return do_transaction("STORE %d +FLAGS (\\Deleted)", msg);
118 // Apply for connection authorization
121 IMAPServer::ptrans_authorize(char*)
123 /* try to get authorized */
126 ok = do_transaction("LOGIN %s \"%s\"", _username, _password);
127 if (DTME_NoError != ok) return DTME_MailServerAccess_AuthorizationFailed;
129 // probe to see if we're running IMAP4 and can use RFC822.PEEK
130 _imap4 = ((do_transaction("CAPABILITY")) == 0);
136 // Get range of messages to be fetched
139 IMAPServer::ptrans_fldstate_read(int *countp, int *newp)
143 /* find out how many messages are waiting */
144 _recent = _unseen = 0;
145 ok = do_transaction("SELECT %s", _folder);
146 if (DTME_NoError != ok) return ok;
150 if (_unseen) // Optional response, but better if we see it.
153 *newp = _recent; // Mandatory
155 *newp = -1; // Should never happen, RECENT is mandatory.
161 // Capture the sizes of all messages.
164 IMAPServer::ptrans_msgsizes(int count, int *sizes)
166 char buf[DTMAS_POPBUFSIZE+1];
167 DTMailError_t ok = DTME_NoError;
169 ok = do_send("FETCH 1:%d RFC822.SIZE", count);
170 if (DTME_NoError != ok) return ok;
172 while (SockGets(buf, sizeof(buf), _sockfp))
176 if (buf[strlen(buf)-1] == '\n')
177 buf[strlen(buf)-1] = '\0';
178 if (buf[strlen(buf)-1] == '\r')
179 buf[strlen(buf)-1] = '\r';
182 _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
184 if (strstr(buf, "OK"))
186 else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
187 sizes[num - 1] = size;
196 // Is the given message old?
199 IMAPServer::ptrans_msgisold(int num)
203 ok = do_transaction("FETCH %d FLAGS", num);
204 if (DTME_NoError != ok) return 0;
210 // request nth message
213 IMAPServer::ptrans_retrieve_start(int msg, int *lenp)
215 char buf[DTMAS_POPBUFSIZE+1];
216 DTMailError_t ok = DTME_NoError;
220 // If we're using IMAP4, we can fetch the message without setting its
221 // seen flag. This is good! It means that if the protocol exchange
222 // craps out during the message, it will still be marked `unseen' on
225 // However...*don't* do this if we're using keep to suppress deletion!
226 // In that case, marking the seen flag is the only way to prevent the
227 // message from being re-fetched on subsequent runs.
229 if (_imap4 && _removeafterdelivery)
230 ok = do_send("FETCH %d RFC822.PEEK", msg);
232 ok = do_send("FETCH %d RFC822", msg);
233 if (DTME_NoError != ok) return ok;
235 // looking for FETCH response
238 if (! SockGets(buf, sizeof(buf), _sockfp))
240 _logger.logError(DTM_FALSE, "Socket Error fetching message");
241 return DTME_MailServerAccess_SocketIOError;
245 _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
246 } while (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
250 _logger.logError(DTM_FALSE, "Protocol Error fetching message");
251 return DTME_MailServerAccess_Error;
260 // Parse command response
263 IMAPServer::ptrans_parse_response(char *argbuf)
265 char buf[DTMAS_POPBUFSIZE+1];
270 if (! SockGets(buf, sizeof(buf), _sockfp))
272 _logger.logError(DTM_FALSE, "Socket Error reading response");
273 return DTME_MailServerAccess_SocketIOError;
276 if (buf[strlen(buf)-1] == '\n')
277 buf[strlen(buf)-1] = '\0';
278 if (buf[strlen(buf)-1] == '\r')
279 buf[strlen(buf)-1] = '\r';
282 _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
284 /* interpret untagged status responses */
285 if (strstr(buf, "EXISTS"))
286 _count = atoi(buf+2);
287 if (strstr(buf, "RECENT"))
288 _recent = atoi(buf+2);
289 if (strstr(buf, "UNSEEN"))
290 _unseen = atoi(buf+2);
291 if (strstr(buf, "FLAGS"))
292 _seen = (strstr(buf, "Seen") != (char *)NULL);
294 } while (strlen(dtmasTAGGET()) &&
295 strncmp(buf, dtmasTAGGET(), strlen(dtmasTAGGET())));
297 if (! strlen(dtmasTAGGET()))
307 for (cp = buf; !isspace(*cp); cp++)
313 if (strncmp(cp, "OK", 2) == 0)
318 else if (strncmp(cp, "BAD", 2) == 0)
320 _logger.logError(DTM_FALSE, "Protocol Error reading response");
321 return DTME_MailServerAccess_Error;
325 _logger.logError(DTM_FALSE, "Protocol Violation reading response");
326 return DTME_MailServerAccess_ProtocolViolation;
332 // Retrieve messages using IMAP Version 2bis or Version 4.
335 IMAPServer::retrieve_messages(DtMailEnv &error)
337 DtMailServer::retrieve_messages(error);
341 // Discard tail of FETCH response after reading message text.
344 IMAPServer::ptrans_retrieve_end(int)
346 char buf [DTMAS_POPBUFSIZE+1];
348 if (! SockGets(buf, sizeof(buf), _sockfp))
350 _logger.logError(DTM_FALSE, "Socket Error reading trail");
351 return DTME_MailServerAccess_SocketIOError;