Fix typo in license headers
[oweals/cde.git] / cde / programs / dtmail / include / DtMail / DtMailError.hh
1 /*
2  *+SNOTICE
3  *
4  *
5  *      $TOG: DtMailError.hh /main/15 1999/03/26 16:59:10 mgreess $
6  *
7  *      RESTRICTED CONFIDENTIAL INFORMATION:
8  *      
9  *      The information in this document is subject to special
10  *      restrictions in a confidential disclosure agreement bertween
11  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
12  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
13  *      Sun's specific written approval.  This documment and all copies
14  *      and derivative works thereof must be returned or destroyed at
15  *      Sun's request.
16  *
17  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
18  *
19  *+ENOTICE
20  */
21
22 #ifndef _DTMAILERROR_H
23 #define _DTMAILERROR_H
24
25 #include <DtMail/DtMailTypes.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <Tt/tt_c.h>
29 #include <nl_types.h>
30
31 //
32 // Name of the messaging database file.
33 //
34 extern  const char *    DtMailCatalogDataFile;
35 static const int        MailErrorSet = 1;
36 static const int        SockErrorSet = 2;
37 static const int        ValuesErrorSet = 2;
38 static const int        FileShareMsgSet = 2;
39
40 //
41 // NOTE - IMPORTANT -- READ ME
42 //
43 // The order of the definitions below must match the
44 // strings in libDtMail/Common/DtMailError.C
45 //
46 typedef enum {
47   DTME_NoError,
48   DTME_AlreadyLocked,
49   DTME_BadArg,
50   DTME_BadMailAddress,
51   DTME_BadRunGroup,
52   DTME_FileCreateFailed,
53   DTME_FolderNotSet,
54   DTME_GetLockRefused,
55   DTME_ImplFailure,
56   DTME_InitializationFailed,
57   DTME_InternalFailure,
58   DTME_InvalidError,
59   DTME_InvalidOperationSequence,
60   DTME_MD5ChecksumFailed,
61   DTME_MailTransportFailed,
62   DTME_NoDataType,
63   DTME_NoImplementations,
64   DTME_NoMailBox,
65   DTME_NoMemory,
66   DTME_NoMsgCat,
67   DTME_NoNewMail,
68   DTME_NoObjectValue,
69   DTME_NoSuchFile,
70   DTME_NoSuchImplementation,
71   DTME_NoSuchType,
72   DTME_NoUser,
73   DTME_NotInbox,
74   DTME_NotLocked,
75   DTME_NotMailBox,
76   DTME_NotSupported,
77   DTME_ObjectAccessFailed,
78   DTME_ObjectCreationFailed,
79   DTME_ObjectInUse,
80   DTME_ObjectInvalid,
81   DTME_OpenContainerInterrupted,
82   DTME_OperationInvalid,
83   DTME_OtherOwnsWrite,
84   DTME_RequestDenied,
85   DTME_TTFailure,
86   DTME_TransportFailed,
87   DTME_UnknownFormat,
88   DTME_UnknownOpenError,
89   DTME_UserAbort,
90   DTME_UserInterrupted,
91   DTME_ObjectReadOnly,
92   DTME_NoPermission,
93   DTME_IsDirectory,
94   DTME_CannotRemoveStaleMailboxLockFile,
95   DTME_CannotCreateMailboxLockFile,
96   DTME_CannotCreateMailboxLockFile_NoPermission,
97   DTME_CannotCreateMailboxLockFile_IsDirectory,
98   DTME_CannotCreateMailboxLockFile_NoSuchFile,
99   DTME_CannotCreateMailboxLockFile_RemoteAccessLost,
100   DTME_CannotObtainInformationOnOpenMailboxFile,
101   DTME_CannotCreateTemporaryMailboxFile,
102   DTME_CannotCreateTemporaryMailboxFile_NoPermission,
103   DTME_CannotCreateTemporaryMailboxFile_IsDirectory,
104   DTME_CannotCreateTemporaryMailboxFile_NoSuchFile,
105   DTME_CannotCreateTemporaryMailboxFile_RemoteAccessLost,
106   DTME_CannotSetPermissionsOfTemporaryMailboxFile,
107   DTME_CannotSetOwnerOfTemporaryMailboxFile,
108   DTME_CannotSetGroupOfTemporaryMailboxFile,
109   DTME_CannotWriteToTemporaryMailboxFile,
110   DTME_CannotWriteToTemporaryMailboxFile_ProcessLimitsExceeded,
111   DTME_CannotWriteToTemporaryMailboxFile_RemoteAccessLost,
112   DTME_CannotWriteToTemporaryMailboxFile_NoFreeSpaceLeft,
113   DTME_CannotReadNewMailboxFile,
114   DTME_CannotReadNewMailboxFile_OutOfMemory,
115   DTME_CannotRemoveMailboxLockFile,
116   DTME_CannotRenameNewMailboxFileOverOld,
117   DTME_InternalAssertionFailure,
118   DTME_ResourceParsingNoEndif,
119   DTME_AlreadyOpened,
120   DTME_OutOfSpace,
121   DTME_CannotCreateMailboxDotDtmailLockFile,
122   DTME_MailboxInodeChanged,
123   DTME_MailServerAccess_AuthorizationFailed,
124   DTME_MailServerAccess_Error,
125   DTME_MailServerAccess_MissingPassword,
126   DTME_MailServerAccess_ProtocolViolation,
127   DTME_MailServerAccess_ServerTimeoutError,
128   DTME_MailServerAccess_SocketIOError,
129   DTME_AppendMailboxFile_Error,
130   DTME_AppendMailboxFile_FileTooBig,
131   DTME_AppendMailboxFile_LinkLost,
132   DTME_AppendMailboxFile_NoSpaceLeft,
133   DTME_AppendMailboxFile_SystemError,
134   DTME_GetmailCommandRetrieval_SystemError,
135   DTME_GetmailCommandRetrieval_AbnormalExit,
136   DTME_PathElementPermissions,
137   DTME_PathElementNotDirectory,
138   DTME_PathElementDoesNotExist,
139   DTME_MailServerAccessInfo_SocketOpen,
140   DTME_MailServerAccessInfo_NoMessages,
141   DTME_MailServerAccessInfo_RetrievingMessage,
142   DTME_MailServerAccessInfo_MessageTooLarge,
143   DTME_MAXDTME
144 } DTMailError_t;
145
146 //
147 // Make a class wrapper for errors so that we can set/clear
148 // errors easier.
149 //
150 class DtMailEnv {
151 public:
152
153   void  clear();        // Sets everything to no error, and frees memory.
154
155   //
156   // logError() and logFatalError():
157   //
158   // This will print out in a formated way all of the error information
159   // that it can. it also sends the results to the system log device for
160   // bug tracking.
161   //
162   // First it will format the error message to be returned.
163   // Then if the _client message is set it will be printed.
164   //
165   // %m is used to insert the system supplied message into the supplied
166   // format (see syslog(3)):
167   //
168   //    error.logError(DTM_FALSE, "I got an error: number %d\n %m , error_code);
169   //
170   // The format may be in a system specific way.
171   //
172   void          logError(DtMailBoolean criticalError,
173                          const char *format, ...) const;
174
175 #ifdef DEAD_WOOD
176   //
177   // This one is the same as logError() plus it sets _fatal.
178   //
179   void          logFatalError(DtMailBoolean criticalError,
180                               const char *format, ...);
181 #endif /* DEAD_WOOD */
182   
183   //
184   // Set the error code. Tt_message is optional.
185   //
186   // Fatal here is within the scope of the function called,
187   // not necessarily the program. If the program can recover - it should.
188   //
189   void          setError(const DTMailError_t,
190                          DtMailBoolean fatal,
191                          Tt_message = NULL);
192
193   void          vSetError(const DTMailError_t minor_code,
194                          DtMailBoolean fatal,
195                          Tt_message msg,
196                          ... );
197   
198   void          setError(const DTMailError_t,
199                          const Tt_message = NULL);
200
201   const char *  errnoMessage(int systemErrorNumber);
202   const char *  errnoMessage();
203   void          setClient(void *);
204   void *        getClient(void) { return _client; }
205   DtMailBoolean isSet() const;  // Returns DTM_TRUE if there was an error.
206   DtMailBoolean isNotSet() const;       // !IsSet()
207   DtMailBoolean isFatal() const;// Returns DTM_TRUE if the error was fatal.
208   DtMailBoolean isNotFatal() const;     // !IsFatal()
209   
210   //
211   // Cast operators.                    While:
212   //
213   //    DtMailEnv                error;
214   //                            
215   //    // You can say....
216   //
217   //    const Tt_message msg    = (const Tt_message)error;
218   //
219   //    const CMEnv env         = (const CMEnv*)error;
220   //
221   //    printf("%s %d", (const char*)error, (DTMailError_t)error);
222   //
223   operator const char*();
224   operator const Tt_message() const;
225   operator DTMailError_t() const;
226
227   //
228   // Constructor/Destructor
229   //
230   DtMailEnv();            // The constructor will set everything to no error.
231
232   ~DtMailEnv();         // Sets everything to no error, and frees memory.
233  
234   //
235   // Message catalog.
236   //
237   static nl_catd        _errorCatalog;
238   static char           *getMessageText(int set, int msg, char *dft);
239
240 protected:
241
242   //
243   // The implementation specific function pointer and the _client
244   // variable below was designed for use C++ API.
245   //
246   //
247   // NOTES:
248   //    1)      CPP == C++.
249   //    2)      THESE FUNCTON POINTERS >>>EXPECT<<< TO CALL MT-SAFE FUNCTIONS.
250   //
251   // Implmentation specific error messages are here.
252   // The pointer to the client data is in _client.
253   //
254   // Pointer to function for C++ 'clear', 'message', and
255   // 'code' functions provided to this class from the implementation.
256   // They are set using setCPP(...) function.
257   //
258   // This is for functions that use the C++ calling conventions.
259   //
260   // The CPPclearF points to a function provided by
261   // the implementation to clear the error and free any
262   // data assoicated with the _client specific data.
263   //
264   // The CPPmessageF points to a function provided by
265   // the implementation to return the error 'message' assoicated with
266   // the _client specific data.
267   // --THE PROVIDED FUNCTION MUST RETURN NULL WHEN THERE IS NO ERROR.
268   //
269   // The CPPerrorF points to a function provided by
270   // the implementation to return the error 'code' assoicated with
271   // the _client specific data.
272   // --THE PROVIDED FUNCTION MUST RETURN ZERO (0) WHEN THERE IS NO ERROR.
273   //
274   // void setClient(void *), sets the value of _client.
275   //
276   // C
277   typedef void          (*CPPclearF)(void *);
278   typedef const char  * (*CPPmessageF)(void *);
279   typedef int           (*CPPerrorF)(void *);
280
281   void                  setCPP(CPPclearF, CPPmessageF, CPPerrorF);
282
283   void                  implClear();
284 #ifdef DEAD_WOOD
285   const char *          implGetMessage();
286   int                   implGetError();
287 #endif /* DEAD_WOOD */
288   
289 private:
290   DTMailError_t         _error;         // The corrected error number.
291   DTMailError_t         _error_minor_code;      // The original error number
292   const char            *_message;      // The error message.
293   Tt_message            _tt_message;    // ToolTalk error message.
294   DtMailBoolean         _fatal;         // DTM_TRUE if this is a fatal error.
295   void                  getErrorMessageText(void);
296   
297   //
298   // Function pointers for implementation specific clear,
299   // get message, and get error functions.
300   //
301   CPPclearF     _implClearFunc;   // C function that will clear the error.
302   CPPmessageF   _implMessageFunc; // C function that will get the error msg.
303   CPPerrorF     _implErrorFunc;   // C function that will get the error code.
304
305   //
306   // Where the implementation stores the data.
307   //
308   void          *_client;       // Implmentation specific message.
309
310   //
311   // Has syslog been opened?
312   //
313   static DtMailBoolean  _syslog_open;
314
315   //
316   // Message catalog.
317   //
318   static const char *   DtMailCatalogDataFile;
319 };
320
321 // Inline member functions.
322
323 inline  void
324 DtMailEnv::setCPP(CPPclearF clear, CPPmessageF message, CPPerrorF code)
325 {
326   _implClearFunc = clear;
327   _implMessageFunc = message;
328   _implErrorFunc = code;
329 }
330
331 inline
332 DtMailEnv::~DtMailEnv()
333 {
334   clear();
335   if (_client)
336       free(_client);
337 }
338
339 inline void
340 DtMailEnv::setError(const DTMailError_t err, const Tt_message msg)
341 {
342   setError(err, DTM_FALSE, msg);
343 }
344
345 inline DtMailBoolean
346 DtMailEnv::isSet() const
347 {
348   return(_error == DTME_NoError ? DTM_FALSE : DTM_TRUE);
349 }
350
351 inline DtMailBoolean
352 DtMailEnv::isNotSet() const
353 {
354   return(_error == DTME_NoError ? DTM_TRUE : DTM_FALSE);
355 }
356
357 inline DtMailBoolean
358 DtMailEnv::isFatal() const
359 {
360   return(_fatal);
361 }
362
363 inline DtMailBoolean
364 DtMailEnv::isNotFatal() const
365 {
366   return(_fatal == DTM_TRUE ? DTM_FALSE : DTM_TRUE);
367 }
368
369 inline  void
370 DtMailEnv::setClient(void *client)
371 {
372   _client = client;
373 }
374
375 inline
376 DtMailEnv::operator const char*()
377 {
378     if (_message == NULL)
379         getErrorMessageText();
380     return(_message == NULL ? "Unknown" : _message);
381 }
382
383 inline
384 DtMailEnv::operator const Tt_message() const
385 {
386   return(_tt_message);
387 }
388
389 inline
390 DtMailEnv::operator DTMailError_t() const
391 {
392   return(_error);
393 }
394
395 inline void
396 DtMailEnv::implClear()
397 {
398   if (_client != NULL && _implClearFunc != NULL) {
399         (*_implClearFunc)(_client);
400   }
401 }
402
403 #ifdef DEAD_WOOD
404 inline Tt_message
405 DtMailError_getTTmsg(DtMailEnv & error)
406 {
407   return((const Tt_message)error);      // The prefered way.
408 }
409 #endif /* DEAD_WOOD */
410
411 #endif  // _DTCM_HH
412