]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zwgc/zephyr.c
52067a3c94db1f3460557b5a5d92fd833b0b3d00
[1ts-debian.git] / zwgc / zephyr.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It is one of the source files comprising zwgc, the Zephyr WindowGram
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id: zephyr.c,v 1.10 1999/08/13 00:19:52 danw Exp $
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #include <sysdep.h>
15
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_zephyr_c[] = "$Id: zephyr.c,v 1.10 1999/08/13 00:19:52 danw Exp $";
18 #endif
19
20 #include <zephyr/mit-copyright.h>
21
22 /****************************************************************************/
23 /*                                                                          */
24 /*               Module containing code dealing with zephyr:                */
25 /*                                                                          */
26 /****************************************************************************/
27
28 #include <zephyr/zephyr.h>
29 #include <sys/socket.h>
30 #include "new_string.h"
31 #include "zephyr.h"
32 #include "error.h"
33 #include "mux.h"
34 #include "subscriptions.h"
35 #include "variables.h"
36 #include "pointer.h"
37 #include "main.h"
38 #ifndef X_DISPLAY_MISSING
39 #include "X_driver.h"
40 #endif
41
42 #ifdef DEBUG
43 extern int zwgc_debug;
44 #endif /* DEBUG */
45
46 /*
47  *  Internal Routine:
48  *
49  *    string get_zwgc_port_number_filename()
50  *        Effects: Returns the filename that the zwgc port # is/should be
51  *                 stored in, based on the user's uid & the environment
52  *                 variable WGFILE.  The returned string points into a
53  *                 static buffer that may change on further calls to this
54  *                 routine or getenv.  The returned string should not be
55  *                 modified in any way.
56  */
57
58 static string get_zwgc_port_number_filename()
59 {
60     static char buffer[40];
61     char *temp;
62     char *getenv();
63
64     if (temp = getenv("WGFILE"))
65       return(temp);
66     else {
67         sprintf(buffer, "/tmp/wg.%d", getuid());
68         return(buffer);
69     }
70 }
71
72 /*
73  *
74  */
75
76 static void handle_zephyr_input(notice_handler)
77      void (*notice_handler)();
78 {
79     ZNotice_t *notice;
80     struct sockaddr_in from;
81     int complete_packets_ready;
82
83     for (;;) {
84         errno = 0;
85         if ( (complete_packets_ready=ZPending()) < 0 )
86           FATAL_TRAP( errno, "while calling ZPending()" );
87     
88         if (complete_packets_ready==0)
89           return;
90
91         notice = malloc(sizeof(ZNotice_t));
92
93         TRAP( ZReceiveNotice(notice, &from), "while getting zephyr notice" );
94         if (!error_code) {
95             notice->z_auth = ZCheckAuthentication(notice, &from);
96             notice_handler(notice);
97         }
98     }
99 }
100
101 static int zephyr_inited = 0;
102     
103 /*
104  *
105  */
106
107 void zephyr_init(notice_handler)
108      void (*notice_handler)();
109 {
110     unsigned short port = 0;           /* Use any old port */
111     char *temp;
112     char *exposure;
113     char *tty = NULL;
114     FILE *port_file;
115
116     /*
117      * Initialize zephyr.  If error, print error message & exit.
118      */
119     FATAL_TRAP( ZInitialize(), "while initializing Zephyr" );
120     FATAL_TRAP( ZOpenPort(&port), "while opening Zephyr port" );
121
122     /*
123      * Save away our port number in a special place so that zctl and
124      * other clients can send us control messages: <<<>>>
125      */
126     temp = get_zwgc_port_number_filename();
127     errno = 0;
128     port_file = fopen(temp, "r");
129     if (port_file) {
130         fprintf(stderr, "zwgc: windowgram file already exists.  If you are\n");
131         fprintf(stderr, "zwgc: not already running zwgc, delete %s\n", temp);
132         fprintf(stderr, "zwgc: and try again.\n");
133         exit(1);
134     }
135     port_file = fopen(temp, "w");
136     if (port_file) {
137         fprintf(port_file, "%d\n", port);
138         fclose(port_file);
139     } else {
140         fprintf(stderr, "zwgc: error while opening %s for writing: ", temp);
141         perror("");
142     }
143
144     /* Set hostname and tty for locations.  If we support X, use the
145      * display string for the default tty name. */
146     if (location_override)
147         tty = location_override;
148 #ifndef X_DISPLAY_MISSING
149     else if (dpy)
150         tty = DisplayString(dpy);
151 #endif
152     error_code = ZInitLocationInfo(NULL, tty);
153     TRAP( error_code, "while initializing location information" );
154
155     /*
156      * Retrieve the user's desired exposure level (from the zephyr variable
157      * "exposure"), convert it to the proper internal form then 
158      * set the user's location using it.  If the exposure level is
159      * not one of the allowed ones, print an error and treat it as
160      * EXPOSE_NONE.
161      */
162     if (temp = ZGetVariable("exposure")) {
163         if (!(exposure = ZParseExposureLevel(temp))) {
164             ERROR2("invalid exposure level %s, using exposure level none instead.\n", temp);
165             exposure = EXPOSE_NONE;
166         }
167     } else
168       exposure = EXPOSE_NONE;
169     error_code = ZSetLocation(exposure); /* <<<>>> */
170     if (error_code != ZERR_LOGINFAIL)
171       TRAP( error_code, "while setting location" );
172
173     /*
174      * If the exposure level isn't EXPOSE_NONE, turn on recieving notices.
175      * (this involves reading in the subscription file, etc.)
176      */
177     if (string_Neq(exposure, EXPOSE_NONE))
178       zwgc_startup();
179
180     /*
181      * Set $realm to our realm and $user to our zephyr username:
182      */
183     var_set_variable("realm", ZGetRealm());
184     var_set_variable("user", ZGetSender());
185
186     /*
187      * <<<>>>
188      */
189     mux_add_input_source(ZGetFD(), (void (*)())handle_zephyr_input,
190                          notice_handler);
191     zephyr_inited = 1;
192     return;
193 }
194
195 /*
196  *
197  */
198
199 void finalize_zephyr() /* <<<>>> */
200 {
201     string temp;
202
203     if (zephyr_inited) {
204         /*
205          * Remove the file containing our port # since it is no longer needed:
206          */
207         errno = 0;
208         temp = get_zwgc_port_number_filename();
209         unlink(temp);
210         if (errno) {
211             fprintf(stderr, "zwgc: error while trying to delete %s: ", temp);
212             perror("");
213         }
214
215         /*
216          * Cancel our subscriptions, unset our location, and close our zephyr
217          * connection:
218          */
219 #ifdef DEBUG
220         if (zwgc_debug) {
221             TRAP( ZCancelSubscriptions(0), "while canceling subscriptions" );
222             TRAP( ZUnsetLocation(), "while unsetting location" );
223         } else {
224 #endif /* DEBUG */
225             (void) ZCancelSubscriptions(0);
226             (void) ZUnsetLocation();
227 #ifdef DEBUG
228         }
229 #endif /* DEBUG */
230         ZClosePort();
231     }
232     return;
233 }