dtmail: resolve 'deference before null check' errors related to if(!NULL) checks...
[oweals/cde.git] / cde / programs / dtmail / dtmailpr / main.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 /* $TOG: main.C /main/7 1998/10/26 17:19:37 mgreess $ */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <locale.h>
28 #include "dmx.hh"
29 #include <locale.h>
30 #include <sys/param.h>
31 // #include <Dt/DtNlUtils.h>
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35     void Dt_nlInit( void );
36 #ifdef __cplusplus
37 }
38 #endif
39 #include <Dt/EnvControlP.h>
40 #include <Dt/DtPStrings.h>
41
42 /*
43  * globals
44  */
45
46 gid_t       _originalEgid;    // startup effective gid
47 gid_t       _originalRgid;    // startup real gid
48
49 void   
50 enableGroupPrivileges(void *)
51 {
52     if(-1 == setgid(_originalEgid)) {
53         fprintf(stderr, "Failed to enable group priviledges\n");
54     }
55 }
56
57 void   
58 disableGroupPrivileges(void *)
59 {
60     if(-1 == setgid(_originalRgid)) {
61         fprintf(stderr, "Failed to disable group priviledges\n");
62     }
63 }
64
65 int
66 main (int argc, char **argv)
67 {
68         // parse command-line options
69         int     c;
70         extern char *optarg;
71         extern int optind;
72         char    *ffile = NULL;
73         int     aflag = 0, pgflag = 0;
74         int     errflag = 0;
75
76         // we have to be set-gid to group "mail" when opening and storing
77         // folders.  But we don't want to do everything as group mail.
78         // here we record our original gid, and set the effective gid
79         // back the the real gid.  We'll set it back when we're dealing
80         // with folders...
81         //
82         _originalEgid = getegid();      // remember effective group ID
83         _originalRgid = getgid();       // remember real group ID
84         disableGroupPrivileges((void *)0); // disable group privileges from
85                                            // here on
86
87         /*
88          * To make DtDts*() function correctly, we have to call
89          * DtInitialize() or DtAppInitialize(). But they require Widget....
90          * Instead, just call Dt_nlInit() that is an internal function of
91          * libDtSvc.a. This is a temporary hack.....
92          */
93         _DtEnvControl(DT_ENV_SET);
94         setlocale( LC_ALL, "" );
95         Dt_nlInit();
96
97         while ((c = getopt(argc, argv, "f:aph?")) != EOF)
98                 switch (c)
99                 {
100                         case 'p':
101                                 pgflag++;
102                                 //printf ("Print each message on its own page\n");
103                                 break;
104                         case 'a':
105                                 aflag++;
106                                 //printf ("Strip attachments\n");
107                                 break;
108                         case 'f':
109                                 ffile = optarg;
110                                 //printf ("Input file is: %s\n", ffile);
111                                 break;
112                         case '?':
113                         case 'h':
114                                 errflag++;
115                                 break;
116                         default:        
117                                 errflag++;
118                 }
119         if (errflag)
120         {
121                 fprintf(stderr, "usage: %s [-p] [-a] [-f <filename>] \n", argv[0]);
122                 exit (1);
123         }
124
125         //for ( ; optind < argc; optind++)
126                 //(void)printf("%s\n", argv[optind]);
127         
128
129         // create DtMail session
130
131         DtMailEnv               dmxenv;
132         DtMail::Session         *session;
133
134         DmxMailbox              mbox;
135
136         session = new DtMail::Session (dmxenv, "dtmailpr");
137
138
139         if (handleError (dmxenv, "new session") == B_TRUE)
140                 exit (1);
141
142         if (session == NULL)
143         {
144                 fprintf (stderr, "Error opening session...exiting.\n");
145                 exit (1);
146         }
147
148         // Register all callbacks the backend may have to deal with
149         session->registerDisableGroupPrivilegesCallback(disableGroupPrivileges,
150                                                         (void *)0);
151         session->registerEnableGroupPrivilegesCallback(enableGroupPrivileges,
152                                                         (void *)0);
153
154         // initialize typing system (will go away eventually)
155         DtDtsLoadDataTypes ();
156
157         // temporary hack, until I'm sure that buffer objects are working
158         char buf [BUFSIZ];
159         int n = 0;
160         char    *name;
161         FILE    *msgFile=NULL;
162
163         if (ffile == NULL)
164         {
165                 static char *tmpdir = new char[MAXPATHLEN+1];
166
167                 sprintf(
168                         tmpdir,
169                         "%s/%s",
170                         getenv("HOME"),
171                         DtPERSONAL_TMP_DIRECTORY);
172
173                 name = tempnam(tmpdir, "dtmpr");
174                 if ((msgFile = fopen (name, "w+")) == NULL)
175                 {
176                         perror ("tmpfile");
177                         exit (1);
178                 }
179
180                 while ( (n = read (fileno (stdin), buf, BUFSIZ)) > 0)
181                 {
182                         if (write (fileno (msgFile), buf, n) != n)
183                         {
184                                 perror ("write");
185                                 exit (1);
186                         }
187                 }
188
189                 if (n < 0)
190                 {
191                         perror ("read");
192                 }
193         
194                 fclose (msgFile);
195                 delete [] tmpdir;
196
197         } else {
198                 name = ffile;
199         }
200         DtMail::MailBox         *mailbox = NULL;
201
202         // try to construct mbox
203         mailbox = session->mailBoxConstruct (
204                                 dmxenv,
205                                 DtMailFileObject,
206                                 name,
207                                 NULL,
208                                 NULL,
209                                 NULL);
210
211         if (handleError (dmxenv, "new DtMail::MailBox") == B_TRUE)
212                 exit (1);
213
214         mbox.name = new char [strlen (name) +1];
215         strcpy (mbox.name, name);
216
217         mbox.mbox = mailbox;
218         mbox.loadMessages ();
219
220         int m = 0;
221
222         for (m = 1; m <= mbox.messageCount; m++)
223         {
224                 mbox.msg[m].getFlags ();
225                 mbox.msg[m].display ();
226                 if (m < mbox.messageCount) {
227                         if (pgflag) {
228                                 printf ("\f");
229                         } else {
230                                 printf ("\n\n");
231                         }
232                 }
233         }
234
235         return 0;
236 }