Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtmail / libDtMail / Common / POP2Server.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: POP2Server.C /main/6 1998/11/10 17:08:56 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 <stdlib.h>
73
74 #include <DtMail/DtMailServer.hh>
75 #include <DtMail/IO.hh>
76
77 POP2Server::POP2Server(
78                 char                    *folder,
79                 DtMail::Session         *session,
80                 DtMail::MailBox         *mailbox,
81                 DtMailAppendCallback    append_mailbox_cb,
82                 void                    *append_mailbox_cb_data)
83 : DtMailServer(folder, session, mailbox,
84                append_mailbox_cb, append_mailbox_cb_data)
85 {
86     _pound_arg = 0;
87     _equal_arg = 0;
88 }
89
90 POP2Server::~POP2Server()
91 {
92 }
93
94
95 //
96 // Request nth message.
97 //
98 DTMailError_t
99 POP2Server::ptrans_retrieve_start(int msg, int *lenp)
100 {
101     DTMailError_t       ok;
102
103     *lenp = 0;
104     ok = do_transaction("READ %d", msg);
105     if (DTME_NoError != ok) return ok;
106
107     *lenp = _equal_arg;
108     ok = do_send("RETR");
109     return ok;
110 }
111
112 //
113 // Apply for connection authorization.
114 //
115 DTMailError_t
116 POP2Server::ptrans_authorize(char*)
117 {
118     DTMailError_t       ok;
119     ok = do_transaction("HELO %s %s", _username, _password);
120     if (DTME_NoError != ok) return DTME_MailServerAccess_AuthorizationFailed;
121     return DTME_NoError;
122 }
123
124 //
125 // Get range of messages to be fetched.
126 //
127 DTMailError_t
128 POP2Server::ptrans_fldstate_read(int *countp, int *newp)
129 {
130     //
131     // We should have picked up a count of messages in the user's
132     // default inbox from the pop2_getauth() response.
133     //
134     if (_pound_arg == -1)
135         return DTME_MailServerAccess_Error;
136
137     // Maybe the user wanted a non-default folder.
138     if (! is_inbox())
139     {
140         DTMailError_t   ok;
141         
142         ok = do_transaction("FOLD %s", _folder);
143         if (DTME_NoError != ok) return ok;
144         if (_pound_arg == -1) return DTME_MailServerAccess_Error;
145     }
146
147     *countp = _pound_arg;
148     *newp = -1;
149
150     return DTME_NoError;
151 }
152
153 //
154 // Capture the sizes of all messages.
155 //
156 DTMailError_t
157 POP2Server::ptrans_msgsizes(int countp, int *sizes)
158 {
159     memset((char*) sizes, 0, countp * sizeof(int));
160     return DTME_NoError;
161 }
162
163 //
164 // Is the given message old?
165 //
166 int
167 POP2Server::ptrans_msgisold(int)
168 {
169     return 0;
170 }
171
172 //
173 // Parse POP2 command response.
174 //
175 DTMailError_t
176 POP2Server::ptrans_parse_response (char *argbuf)
177 {
178     DTMailError_t       ok;
179     char                buf[DTMAS_POPBUFSIZE+1];
180
181     _pound_arg = _equal_arg = -1;
182     if (SockGets(buf, sizeof(buf), _sockfp))
183     {
184         if (buf[strlen(buf)-1] == '\n')
185           buf[strlen(buf)-1] = '\0';
186         if (buf[strlen(buf)-1] == '\r')
187           buf[strlen(buf)-1] = '\r';
188
189         if (_protologging)
190           _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
191
192         if (buf[0] == '+')
193           ok = DTME_NoError;
194         else if (buf[0] == '#')
195         {
196             _pound_arg = atoi(buf+1);
197             ok = DTME_NoError;
198         }
199         else if (buf[0] == '=')
200         {
201             _equal_arg = atoi(buf+1);
202             ok = DTME_NoError;
203         }
204         else if (buf[0] == '-')
205         {
206             _logger.logError(DTM_FALSE, "Protocol Error reading response");
207             ok = DTME_MailServerAccess_Error;
208         }
209         else
210         {
211             _logger.logError(DTM_FALSE, "Protocol Violation reading response");
212             ok = DTME_MailServerAccess_ProtocolViolation;
213         }
214
215         if (argbuf != NULL)
216           strcpy(argbuf,buf);
217     }
218     else 
219     {
220         _logger.logError(DTM_FALSE, "Socket Error reading response");
221         ok = DTME_MailServerAccess_SocketIOError;
222     }
223
224     return ok;
225 }
226
227 //
228 // retrieve messages using POP2.
229 //
230 void
231 POP2Server::retrieve_messages(DtMailEnv &error)
232 {
233     DtMailServer::retrieve_messages(error);
234 }
235
236 //
237 // Send acknowledgement for message data.
238 //
239 DTMailError_t
240 POP2Server::ptrans_retrieve_end(int)
241 {
242     if (_removeafterdelivery)
243       return do_transaction("ACKD");
244     else
245       return do_transaction("ACKS");
246 }