dtmail: resolve 'deference before null check' errors related to if(!NULL) checks...
[oweals/cde.git] / cde / programs / dtmail / dtmailpr / mailbox.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 /* $XConsortium: mailbox.C /main/4 1996/04/21 19:44:02 drk $ */
24
25 /*
26  *+SNOTICE
27  *
28  *      $:$
29  *
30  *      RESTRICTED CONFIDENTIAL INFORMATION:
31  *      
32  *      The information in this document is subject to special
33  *      restrictions in a confidential disclosure agreement between
34  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
35  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
36  *      Sun's specific written approval.  This document and all copies
37  *      and derivative works thereof must be returned or destroyed at
38  *      Sun's request.
39  *
40  *      Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
41  *
42  *+ENOTICE
43  */
44
45 #include <stdio.h>
46 #include "dmx.hh"
47
48
49 DmxMailbox::DmxMailbox (void)
50 {
51         mbox = NULL;
52         newMessages = 0;
53         messageCount = 0;
54         firstNew = 0;
55         name = NULL;
56 }
57
58 void
59 DmxMailbox::loadMessages (void)
60 {
61         DtMailEnv               env;
62         boolean_t       moreMessages = B_TRUE;
63
64         // open the mailbox
65         // (O_RDONLY for now)
66         mbox->open (env,        /* DtMailEnv */
67                     DTM_FALSE,  /* auto_create */
68                     O_RDONLY,   /* open(2) flag */ 
69                     (mode_t) 0, /* create(2) mode */
70                     DTM_FALSE,  /* lock_flag */
71                     DTM_TRUE    /* auto_parse */
72           );
73
74         if (handleError (env, "open mailbox") == B_TRUE)
75                 exit (1);
76
77         // get the first message handle
78         DtMailMessageHandle     first = NULL, next = NULL, prev = NULL;
79         DtMailHeaderRequest     request;
80         DtMailHeaderLine        hdrline;
81         int                     i = 1;
82         DtMail::Message         *m = NULL;      // temporary
83
84         createHeaderRequest (request);
85
86         first = mbox->getFirstMessageSummary (env, request, hdrline);
87
88         if (handleError (env, "get first msg summary") == B_TRUE)
89                 exit (1);
90
91         if (first == NULL)
92         {
93                 fprintf (stderr,
94                         "loadMessages: error w/1st message...exiting.\n");
95                 exit (1);
96         }
97
98         // this makes hash of the caching strategy, but oh well....
99         m = mbox->getMessage (env, first);
100         if (handleError (env, "get first msg") == B_TRUE)
101                 exit (1);
102         msg [i].setMessage (m);
103         
104         prev = first;
105
106         msg [i].setHandle (first);
107         msg [i].setHeader (hdrline);
108
109         while (moreMessages == B_TRUE)
110         {
111                 next = mbox->getNextMessageSummary (env, prev,
112                                                 request, hdrline);
113
114                 if (next == NULL)
115                 {
116                         moreMessages = B_FALSE;
117                         if (handleError (env, "msgLoop") == B_TRUE)
118                                 exit (1);
119                         break; // break out if error
120                 } else {
121                         i++;    
122                         msg [i].setHandle (next);
123                         msg [i].setHeader (hdrline);
124                 }
125                 
126                 prev = next;
127         }       
128
129         messageCount = i;
130
131
132         // fill in the remaining message structures .. temporary, honest!
133         for (int j = 1; j <= messageCount; j++)
134         {
135                 m = mbox->getMessage (env, msg [j].msgHandle);
136                 if (handleError (env, "getMessage loop") == B_TRUE)
137                 {
138                         printf ("fatal error...bailing...\n");
139                         exit (1);
140                 }       
141                 msg [j].setMessage (m);
142
143                 char    tmp [100];
144                 sprintf (tmp, "%d", j);         // ick
145                 msg [j].setInfo (tmp);
146
147                 msg[j].getFlags ();
148                 if (msg[j].isNew == B_TRUE)
149                 {
150                         newMessages++;
151                         if (firstNew == 0)
152                         {
153                                 firstNew = j;
154                         }
155                 }
156
157         }
158
159         // no new messages -- start with first message in mailbox
160         if (firstNew == 0)
161                 firstNew = 1;
162                 
163         // free header request structure
164         int k;
165         // this assumes that you have allocated all of the possible headers
166         // in the enum DmxHeaders -- a bad assumption, but currently true
167         for (k = 0; k < DMXNUMHDRS; k++)
168         {
169                 free (request.header_name [k]);
170         }
171         delete request.header_name;
172         
173         return;
174 }
175
176 void
177 DmxMailbox::createHeaderRequest (DtMailHeaderRequest &request)
178 {
179         // set up default headers
180         request.number_of_names = DMXNUMHDRS;
181         request.header_name = new (char* [DMXNUMHDRS]);
182         request.header_name[DMXFROM] = strdup(DtMailMessageSender);
183         request.header_name[DMXSUBJ] = strdup(DtMailMessageSubject);
184         request.header_name[DMXDATE] = strdup(DtMailMessageReceivedTime);
185         request.header_name[DMXCLENGTH] = strdup(DtMailMessageContentLength);
186         request.header_name[DMXSTATUS] = strdup(DtMailMessageStatus);
187         request.header_name[DMXTO] = strdup(DtMailMessageTo);
188         request.header_name[DMXV3CHARSET] = strdup(DtMailMessageV3charset);
189         request.header_name[DMXCONTENTTYPE] = strdup(DtMailMessageContentType);
190
191         return;
192 }
193
194 void
195 DmxMailbox::printMailboxInfo (void)
196 {
197         if (newMessages > 0)
198         {
199                 printf ("\"%s\": %d messages %d new\n",
200                 name, messageCount, newMessages);
201         } else {
202                 printf ("\"%s\": %d messages\n",
203                 name, messageCount);
204         }
205
206         return;
207 }