Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtmail / libDtMail / Common / AUTOServer.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: AUTOServer.C /main/6 1998/11/10 17:07:17 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
78 AUTOServer::AUTOServer(
79                 char                    *folder,
80                 DtMail::Session         *session,
81                 DtMail::MailBox         *mailbox,
82                 DtMailAppendCallback    append_mailbox_cb,
83                 void                    *append_mailbox_cb_data)
84 : DtMailServer(folder, session, mailbox,
85                append_mailbox_cb, append_mailbox_cb_data)
86 {
87     _server = NULL;
88 }
89
90 AUTOServer::~AUTOServer()
91 {
92 }
93
94
95 //
96 // Set delete flag for given message.
97 //
98 DTMailError_t
99 AUTOServer::ptrans_delete(int number)
100 {
101     static char *pname = "AUTOServer::ptrans_delete";
102
103     if (_server) return _server->ptrans_delete(number);
104
105     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
106     return DTME_MailServerAccess_Error;
107 }
108
109 //
110 // Apply for connection authorization
111 //
112 DTMailError_t
113 AUTOServer::ptrans_authorize(char *buf)
114 {
115     static char *pname = "AUTOServer::ptrans_authorize";
116
117     if (_server) return _server->ptrans_authorize(buf);
118
119     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
120     return DTME_MailServerAccess_Error;
121 }
122
123 //
124 // Expunge deleted messages
125 //
126 DTMailError_t
127 AUTOServer::ptrans_fldstate_expunge()
128 {
129     static char *pname = "AUTOServer::ptrans_fldstate_expunge";
130
131     if (_server) return _server->ptrans_fldstate_expunge();
132
133     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
134     return DTME_MailServerAccess_Error;
135 }
136
137 //
138 // Get range of messages to be fetched
139 //
140 DTMailError_t
141 AUTOServer::ptrans_fldstate_read(int *countp, int *newp)
142 {
143     static char *pname = "AUTOServer::ptrans_fldstate_read";
144
145     if (_server) return _server->ptrans_fldstate_read(countp, newp);
146
147     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
148     return DTME_MailServerAccess_Error;
149 }
150
151 //
152 // Capture the sizes of all messages.
153 //
154 DTMailError_t
155 AUTOServer::ptrans_msgsizes(int count, int *sizes)
156 {
157     static char *pname = "AUTOServer::ptrans_msgsizes";
158
159     if (_server) return _server->ptrans_msgsizes(count, sizes);
160
161     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
162     return DTME_MailServerAccess_Error;
163 }
164
165 //
166 // Is the given message old?
167 //
168 int
169 AUTOServer::ptrans_msgisold(int num)
170 {
171     static char *pname = "AUTOServer::ptrans_msgisold";
172
173     if (_server) return _server->ptrans_msgisold(num);
174
175     return 0;
176 }
177
178 //
179 // Quit the server
180 //
181 DTMailError_t
182 AUTOServer::ptrans_quit()
183 {
184     static char *pname = "AUTOServer::ptrans_quit";
185
186     if (_server) return _server->ptrans_quit();
187
188     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
189     return DTME_MailServerAccess_Error;
190 }
191
192 //
193 // request nth message
194 //
195 DTMailError_t
196 AUTOServer::ptrans_retrieve_start(int number, int *lenp)
197 {
198     static char *pname = "AUTOServer::ptrans_retrieve_start";
199
200     if (_server) return _server->ptrans_retrieve_start(number, lenp);
201
202     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
203     return DTME_MailServerAccess_Error;
204 }
205
206 //
207 // Parse command response
208 //
209 DTMailError_t
210 AUTOServer::ptrans_parse_response(char *argbuf)
211 {
212     static char *pname = "AUTOServer::ptrans_parse_response";
213
214     if (_server) return _server->ptrans_parse_response(argbuf);
215
216     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
217     return DTME_MailServerAccess_Error;
218 }
219
220 //
221 // Retrieve messages using IMAP Version 2bis or Version 4.
222 //
223 void
224 AUTOServer::retrieve_messages(DtMailEnv &error)
225 {
226     if (NULL != _server)
227     {
228         _server->retrieve_messages(error);
229         return;
230     }
231
232     //
233     // First time through
234     //
235     for (int i=0; i<AUTO_NPROTOCOLS; i++)
236     {
237         if (NULL != _server)
238           delete _server;
239         
240         switch (i)
241         {
242           case AUTO_POP3:
243               _server = new POP3Server(
244                                         _folder,
245                                         _session,
246                                         _mailbox,
247                                         _append_mailbox_cb,
248                                         _append_mailbox_cb_data);
249               break;
250           case AUTO_APOP:
251               _server = new APOPServer(
252                                         _folder,
253                                         _session,
254                                         _mailbox,
255                                         _append_mailbox_cb,
256                                         _append_mailbox_cb_data);
257               break;
258           case AUTO_IMAP:
259               _server = new IMAPServer(
260                                         _folder,
261                                         _session,
262                                         _mailbox,
263                                         _append_mailbox_cb,
264                                         _append_mailbox_cb_data);
265               break;
266           case AUTO_POP2:
267               _server = new POP2Server(
268                                         _folder,
269                                         _session,
270                                         _mailbox,
271                                         _append_mailbox_cb,
272                                         _append_mailbox_cb_data);
273               break;
274         }
275
276         if (NULL != _server)
277         {
278             if (NULL != _password)
279               _server->set_password(_password);
280
281             _server->retrieve_messages(error);
282             if (DTME_MailServerAccess_MissingPassword == (DTMailError_t) error)
283             {
284                 delete _server;
285                 _server = NULL;
286                 return;
287             }
288             else if (error.isNotSet())
289               return;
290         }
291     }
292 }
293
294 //
295 // Discard tail of FETCH response after reading message text.
296 //
297 DTMailError_t
298 AUTOServer::ptrans_retrieve_end(int number)
299 {
300     static char *pname = "AUTOServer::ptrans_retrieve_end";
301
302     if (_server)
303       return _server->ptrans_retrieve_end(number);
304
305     _logger.logError(DTM_FALSE, "%s:  NULL server\n", pname);
306     return DTME_MailServerAccess_Error;
307 }