Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtmail / libDtMail / Common / IMAPServer.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /*
24  *+SNOTICE
25  *
26  *      $TOG: IMAPServer.C /main/7 1998/11/10 17:08:32 mgreess $
27  *
28  *      RESTRICTED CONFIDENTIAL INFORMATION:
29  *      
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
36  *      Sun's request.
37  *
38  *      Copyright 1993, 1995, 1995 Sun Microsystems, Inc.  All rights reserved.
39  *
40  *+ENOTICE
41  */
42 /*
43  *                   Common Desktop Environment
44  *
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.
52  *                                                                   
53  *
54  *                     RESTRICTED RIGHTS LEGEND                              
55  *
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).
61
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
69  */
70
71 #include  <stdio.h>
72 #include  <string.h>
73 #include  <ctype.h>
74 #include  <stdlib.h>
75
76 #include  <DtMail/DtMailServer.hh>
77 #include  <DtMail/IO.hh>
78
79 #define dtmasTAGGET()   (_transtag)
80
81 IMAPServer::IMAPServer(
82                 char                    *folder,
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)
89 {
90     _append_mailbox_cb = append_mailbox_cb;
91     _append_mailbox_cb_data = append_mailbox_cb_data;
92     _count = 0;
93     _imap4 = 0;
94     _recent = 0;
95     _seen = 0;
96     _unseen = 0;
97 }
98
99 IMAPServer::~IMAPServer()
100 {
101 }
102
103
104 //
105 // Set delete flag for given message.
106 //
107 DTMailError_t
108 IMAPServer::ptrans_delete(int msg)
109 {
110     // Use SILENT if possible as a minor throughput optimization.
111     if (_imap4)
112       return do_transaction("STORE %d +FLAGS.SILENT (\\Deleted)", msg);
113     else
114       return do_transaction("STORE %d +FLAGS (\\Deleted)", msg);
115 }
116
117 //
118 // Apply for connection authorization
119 //
120 DTMailError_t
121 IMAPServer::ptrans_authorize(char*)
122 {
123     /* try to get authorized */
124     DTMailError_t       ok;
125     
126     ok = do_transaction("LOGIN %s \"%s\"", _username, _password);
127     if (DTME_NoError != ok) return DTME_MailServerAccess_AuthorizationFailed;
128
129     // probe to see if we're running IMAP4 and can use RFC822.PEEK 
130     _imap4 = ((do_transaction("CAPABILITY")) == 0);
131
132     return DTME_NoError;
133 }
134
135 //
136 // Get range of messages to be fetched
137 //
138 DTMailError_t
139 IMAPServer::ptrans_fldstate_read(int *countp, int *newp)
140 {
141     DTMailError_t       ok;
142
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;
147
148     *countp = _count;
149
150     if (_unseen)        // Optional response, but better if we see it.
151       *newp = _unseen;
152     else if (_recent)
153       *newp = _recent;  // Mandatory
154     else
155       *newp = -1;       // Should never happen, RECENT is mandatory.
156
157     return DTME_NoError;
158 }
159
160 //
161 // Capture the sizes of all messages.
162 //
163 DTMailError_t
164 IMAPServer::ptrans_msgsizes(int count, int *sizes)
165 {
166     char                buf[DTMAS_POPBUFSIZE+1];
167     DTMailError_t       ok = DTME_NoError;
168
169     ok = do_send("FETCH 1:%d RFC822.SIZE", count);
170     if (DTME_NoError != ok) return ok;
171
172     while (SockGets(buf, sizeof(buf), _sockfp))
173     {
174         int num, size;
175
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';
180
181         if (_protologging)
182           _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
183
184         if (strstr(buf, "OK"))
185           break;
186         else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
187           sizes[num - 1] = size;
188         else
189           sizes[num - 1] = -1;
190     }
191
192     return DTME_NoError;
193 }
194
195 //
196 // Is the given message old?
197 //
198 int
199 IMAPServer::ptrans_msgisold(int num)
200 {
201     DTMailError_t       ok;
202
203     ok = do_transaction("FETCH %d FLAGS", num);
204     if (DTME_NoError != ok) return 0;
205
206     return _seen;
207 }
208
209 //
210 // request nth message
211 //
212 DTMailError_t
213 IMAPServer::ptrans_retrieve_start(int msg, int *lenp)
214 {
215     char                buf[DTMAS_POPBUFSIZE+1];
216     DTMailError_t       ok = DTME_NoError;
217     int                 num;
218
219     //
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
223     // the server.
224     //
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.
228     //
229     if (_imap4 && _removeafterdelivery)
230       ok = do_send("FETCH %d RFC822.PEEK", msg);
231     else
232       ok = do_send("FETCH %d RFC822", msg);
233     if (DTME_NoError != ok) return ok;
234
235     // looking for FETCH response
236     do
237     {
238         if (! SockGets(buf, sizeof(buf), _sockfp))
239         {
240             _logger.logError(DTM_FALSE, "Socket Error fetching message");
241             return DTME_MailServerAccess_SocketIOError;
242         }
243
244         if (_protologging)
245           _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
246     } while (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
247
248     if (num != msg)
249     {
250         _logger.logError(DTM_FALSE, "Protocol Error fetching message");
251         return DTME_MailServerAccess_Error;
252     }
253     else
254     {
255         return DTME_NoError;
256     }
257 }
258
259 //
260 // Parse command response
261 //
262 DTMailError_t
263 IMAPServer::ptrans_parse_response(char *argbuf)
264 {
265     char buf[DTMAS_POPBUFSIZE+1];
266
267     _seen = 0;
268     do
269     {
270         if (! SockGets(buf, sizeof(buf), _sockfp))
271         {
272             _logger.logError(DTM_FALSE, "Socket Error reading response");
273             return DTME_MailServerAccess_SocketIOError;
274         }
275
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';
280
281         if (_protologging)
282           _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
283
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);
293
294     } while (strlen(dtmasTAGGET()) &&
295              strncmp(buf, dtmasTAGGET(), strlen(dtmasTAGGET())));
296
297     if (! strlen(dtmasTAGGET()))
298     {
299         strcpy(argbuf, buf);
300         return DTME_NoError; 
301     }
302     else
303     {
304         char    *cp;
305
306         /* skip the tag */
307         for (cp = buf; !isspace(*cp); cp++)
308           continue;
309
310         while (isspace(*cp))
311           cp++;
312
313         if (strncmp(cp, "OK", 2) == 0)
314         {
315             strcpy(argbuf, cp);
316             return DTME_NoError;
317         }
318         else if (strncmp(cp, "BAD", 2) == 0)
319         {
320             _logger.logError(DTM_FALSE, "Protocol Error reading response");
321             return DTME_MailServerAccess_Error;
322         }
323         else
324         {
325             _logger.logError(DTM_FALSE, "Protocol Violation reading response");
326             return DTME_MailServerAccess_ProtocolViolation;
327         }
328     }
329 }
330
331 //
332 // Retrieve messages using IMAP Version 2bis or Version 4.
333 //
334 void
335 IMAPServer::retrieve_messages(DtMailEnv &error)
336 {
337     DtMailServer::retrieve_messages(error);
338 }
339
340 //
341 // Discard tail of FETCH response after reading message text.
342 //
343 DTMailError_t
344 IMAPServer::ptrans_retrieve_end(int)
345 {
346     char buf [DTMAS_POPBUFSIZE+1];
347
348     if (! SockGets(buf, sizeof(buf), _sockfp))
349     {
350         _logger.logError(DTM_FALSE, "Socket Error reading trail");
351         return DTME_MailServerAccess_SocketIOError;
352     }
353
354     return DTME_NoError;
355 }