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