1 /*$Id: xmotd.c,v 1.20 2003/02/14 00:37:38 elf Exp $*/
2
3 /*
4 * Copyright 1994-97, 2003 Luis Fernandes <elf@ee.ryerson.ca>
5 *
6 * Permission to use, copy, hack, and distribute this software and its
7 * documentation for any purpose and without fee is hereby granted,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. This application is presented as is
11 * without any implied or written warranty.
12 *
13 */
14 /*
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <time.h>
33 #include <memory.h>
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <utime.h>
39 #include <errno.h>
40
41 #include <X11/Intrinsic.h>
42 #include <X11/StringDefs.h>
43
44 #ifdef MOTIF
45
46 #include <Xm/Text.h>
47 #include <Xm/PushB.h>
48 #include <Xm/Form.h>
49 #include <Xm/Label.h>
50 #include <Xm/Separator.h>
51
52 #ifdef HAVE_HTML
53 #include <HTML.h>
54 #endif
55
56 #else
57
58 #include <X11/Shell.h>
59 #include <X11/Xaw/AsciiText.h>
60 #include <X11/Xaw/Command.h>
61 #include <X11/Xaw/Form.h>
62 #include <X11/Xaw/Label.h>
63
64 #ifdef HAVE_HTML
65 #include <HTML.h>
66 #endif
67
68 #endif
69
70 #include "maindefs.h"
71 #include "main.h"
72
73 extern time_t motdChanged();
74 extern messageptr freeMessage();
75 extern Pixmap icon_pixmap;
76 extern char timeStamp[256];
77
78 extern messageptr msgslist;/* list of pointers to the motds requested
79 to be displayed */
80 extern app_res_t app_res;
81 extern XtAppContext app_con;
82 extern XtIntervalId timer;
83
84 char *txtbuf; /* file is loaded into this malloc'd pointer */
85
86
87 void
88 /*ARGSUSED*/
nextMessage(Widget w,caddr_t call_data,caddr_t client_data)89 nextMessage(Widget w, caddr_t call_data, caddr_t client_data)
90 {
91
92 char buffer[256];
93 time_t ftime= time((time_t *)NULL);
94 extern Widget info, quit;
95
96 #ifdef MOTIF
97 XmString xmstr;
98 #endif
99
100 /* fprintf(stderr,"nextMessage()\n"); */
101
102 if (!msgslist)
103 return;
104
105 memset(buffer, 0, 256);
106 displayMessage(msgslist->file);
107
108 updateTimeStamp(getTimeStampName()); /* update the timestamp
109 whenever we pop-up to
110 display a message */
111
112 if(app_res.showfilename) /*show the filename */
113 {
114 sprintf(buffer, "%s (%s)", ctime(&ftime), msgslist->file);
115 }
116 else
117 {
118 sprintf(buffer, "%s", ctime(&ftime));
119 }
120
121 *strchr(buffer, '\n')=' '; /* junk the \n at the end*/
122
123 #ifdef MOTIF
124 xmstr=XmStringCreateLocalized(buffer);
125 XtVaSetValues(info, XmNlabelString, xmstr, NULL);
126 XmStringFree(xmstr);
127 #else
128 XtVaSetValues(info, XtNlabel, buffer, NULL);
129 #endif
130
131 /* next time through the loop, we continue at the next message*/
132 msgslist = freeMessage(msgslist);
133
134 /*figure out if we are displaying the last message;
135 if we are, then change the button-label to
136 "Dismiss" and re-direct the callback to Quit()*/
137 if(!msgslist)
138 {
139 extern void Quit(Widget w, caddr_t call_data, caddr_t client_data) ;
140
141 /* fprintf(stderr, "msgindex=%d, displayed %s (LAST MESSAGE)\n", msgindex, buffer);*/
142
143 #ifdef MOTIF
144 xmstr=XmStringCreateLocalized(LAST_MESSAGE_LABEL);
145 XtVaSetValues(quit, XmNlabelString, xmstr, NULL);
146 XmStringFree(xmstr);
147
148 XtRemoveAllCallbacks(quit, XmNactivateCallback);
149
150 XtAddCallback(quit, XmNactivateCallback,
151 (XtCallbackProc)Quit, 0);
152 #else
153 XtVaSetValues(quit, XtNlabel, LAST_MESSAGE_LABEL, NULL);
154 XtRemoveAllCallbacks(quit, XtNcallback);
155
156 XtAddCallback(quit, XtNcallback,
157 (XtCallbackProc)Quit, 0);
158 #endif
159 if(app_res.pto)
160 timer=XtAppAddTimeOut(app_con, (unsigned long)(app_res.pto*1000),
161 (XtTimerCallbackProc)Quit, (caddr_t) NULL);
162 return;
163 }
164 if(app_res.pto)
165 timer=XtAppAddTimeOut(app_con, (unsigned long)(app_res.pto*1000),
166 (XtTimerCallbackProc)nextMessage, (caddr_t) NULL);
167 }
168
169 void
revisitMessagesAndDisplay(int numsg)170 revisitMessagesAndDisplay(int numsg)
171 {
172 extern int gargc;
173 extern char **gargv;
174 extern Widget topLevel, quit;
175 extern void Quit(Widget w, caddr_t call_data, caddr_t client_data);
176
177 /* fprintf(stderr,"revisitMessagesAndDisplay()\n");*/
178
179 if(numsg>1)
180 {
181 #ifdef MOTIF
182 XmString xmstr;
183 xmstr=XmStringCreateLocalized(NEXT_MESSAGE_LABEL);
184 XtVaSetValues(quit, XmNlabelString, xmstr, NULL);
185 XmStringFree(xmstr);
186
187 XtRemoveAllCallbacks(quit,XmNactivateCallback);
188
189 XtAddCallback(quit, XmNactivateCallback,
190 (XtCallbackProc)nextMessage, 0);
191 #else
192 XtVaSetValues(quit, XtNlabel, NEXT_MESSAGE_LABEL, NULL);
193 XtRemoveAllCallbacks(quit,XtNcallback);
194 XtAddCallback(quit, XtNcallback,
195 (XtCallbackProc)nextMessage, 0);
196
197 #endif
198 }
199 /* find and display 1st msg*/
200 nextMessage((Widget)NULL, (caddr_t)NULL, (caddr_t)NULL);
201 XMapRaised(XtDisplay(topLevel), XtWindow(topLevel));
202 XFlush(XtDisplay(topLevel));
203
204 }
205
206
207 int
displayMessage(char * filename)208 displayMessage(char *filename)
209 {
210 FILE *fp;
211 struct stat motdstat;
212 extern Widget text;
213
214 /* fprintf(stderr,"displayMessage()\n"); */
215
216 #ifdef MOTIF
217 XmString xmstr;
218 #endif
219
220 /* memset((char *)(&motdstat), 0, sizeof(struct stat));*/
221
222 if((fp=fopen(filename,"r"))==NULL) /* Read it in...*/
223 {
224 perror(filename);
225 return(0); /* no file by this name*/
226 }
227
228 stat(filename,&motdstat);
229 if(txtbuf) free(txtbuf);
230
231 txtbuf=(char *)calloc(1, (motdstat.st_size+1)*sizeof(char));
232
233 if(!txtbuf)
234 {
235 extern XtAppContext app_con;
236
237 perror("xmotd");
238 XtDestroyApplicationContext(app_con);
239 exit(2);
240 }
241
242 fread(txtbuf,(int)motdstat.st_size,1,fp);
243
244 fclose(fp);
245
246 #ifdef MOTIF
247
248 #ifdef HAVE_HTML
249
250 HTMLSetText(text,txtbuf,NULL,NULL,0,NULL,NULL);
251
252 #else
253 XtVaSetValues(text, XmNvalue, txtbuf, NULL);
254 #endif
255
256 #else
257
258 #ifdef HAVE_HTML
259
260 HTMLSetText(text,txtbuf,NULL,NULL,0,NULL,NULL);
261
262 #else
263 XtVaSetValues(text, XtNstring, txtbuf, NULL);
264 #endif
265
266 #endif
267
268 return(1);
269
270 }/* displayMessage*/
271
272