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