summaryrefslogtreecommitdiff
path: root/zwgc/zephyr.c
blob: 6acfe81f0a9f2506063022693c2c8e4bfcf6566d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Id$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_zephyr_c[] = "$Id$";
#endif

#include <zephyr/mit-copyright.h>

/****************************************************************************/
/*                                                                          */
/*               Module containing code dealing with zephyr:                */
/*                                                                          */
/****************************************************************************/

#include <zephyr/zephyr.h>
#include <sys/socket.h>
#include "new_string.h"
#include "zephyr.h"
#include "error.h"
#include "mux.h"
#include "subscriptions.h"
#include "variables.h"
#include "pointer.h"
#include "main.h"
#ifndef X_DISPLAY_MISSING
#include "X_driver.h"
#endif
#ifdef CMU_ZWGCPLUS
#include "plus.h"
#endif

#ifdef DEBUG
extern int zwgc_debug;
#endif /* DEBUG */

static int zephyr_inited = 0;
static unsigned short zephyr_port = 0;
    
/*
 *  Internal Routine:
 *
 *    string get_zwgc_port_number_filename()
 *        Effects: Returns the filename that the zwgc port # is/should be
 *                 stored in, based on the user's uid & the environment
 *                 variable WGFILE.  The returned string points into a
 *                 static buffer that may change on further calls to this
 *                 routine or getenv.  The returned string should not be
 *                 modified in any way.
 */

static string
get_zwgc_port_number_filename(void)
{
    static char buffer[40];
    char *temp;

    temp = getenv("WGFILE");
    if (temp)
      return(temp);
    else {
	sprintf(buffer, "/tmp/wg.%d", getuid());
	return(buffer);
    }
}

/*
 *  Write out the port number to the wg file.
 */

void
write_wgfile(void)
{
    char *name = get_zwgc_port_number_filename();
    FILE *port_file;

    port_file = fopen(name, "w");
    if (port_file) {
	fprintf(port_file, "%d\n", zephyr_port);
	fclose(port_file);
    } else {
	fprintf(stderr, "zwgc: error while opening %s for writing: ", name);
	perror("");
    }
}

/*
 *
 */

struct notice_handler_ptr {
    void (*notice_handler)(ZNotice_t *);
};

static void
handle_zephyr_input(struct notice_handler_ptr *nhp)
{
    ZNotice_t *notice;
    struct sockaddr_in from;
    int complete_packets_ready;

    for (;;) {
	errno = 0;
	if ( (complete_packets_ready=ZPending()) < 0 )
	  FATAL_TRAP( errno, "while calling ZPending()" );
    
	if (complete_packets_ready==0)
	  return;

	notice = malloc(sizeof(ZNotice_t));

	TRAP( ZReceiveNotice(notice, &from), "while getting zephyr notice" );
	if (!error_code) {
	    notice->z_auth = ZCheckAuthentication(notice, &from);
	    nhp->notice_handler(notice);
	}
#ifdef CMU_ZWGCPLUS
	if (get_list_refcount(notice) <= 0) {
	    /* no windows created */
	    if (!get_notice_fake(notice))
		list_del_notice(notice);
	}
#endif
    }
}

/*
 *
 */

void zephyr_init(void (*notice_handler)(ZNotice_t *))
{
    struct notice_handler_ptr *nhp;
    char *temp;
    char *exposure;
    char *tty = NULL;
    FILE *port_file;

    /*
     * Initialize zephyr.  If error, print error message & exit.
     */
    nhp = malloc(sizeof(struct notice_handler_ptr));
    if (!nhp) {
	fprintf(stderr, "Out of memory setting up zephyr notice handler.\n");
	exit(3);
    }
    FATAL_TRAP( ZInitialize(), "while initializing Zephyr" );
    FATAL_TRAP( ZOpenPort(&zephyr_port), "while opening Zephyr port" );

    /*
     * Save away our port number in a special place so that zctl and
     * other clients can send us control messages: <<<>>>
     */
    temp = get_zwgc_port_number_filename();
    port_file = fopen(temp, "r");
    if (port_file) {
	fprintf(stderr, "zwgc: windowgram file already exists.  If you are\n");
	fprintf(stderr, "zwgc: not already running zwgc, delete %s\n", temp);
	fprintf(stderr, "zwgc: and try again.\n");
	exit(1);
    }
    write_wgfile();

    /* Set hostname and tty for locations.  If we support X, use the
     * display string for the default tty name. */
    if (location_override)
	tty = location_override;
#ifndef X_DISPLAY_MISSING
    else if (x_dpy)
	tty = DisplayString(x_dpy);
#endif
    error_code = ZInitLocationInfo(NULL, tty);
    TRAP( error_code, "while initializing location information" );

    /*
     * Retrieve the user's desired exposure level (from the zephyr variable
     * "exposure"), convert it to the proper internal form then 
     * set the user's location using it.  If the exposure level is
     * not one of the allowed ones, print an error and treat it as
     * EXPOSE_NONE.
     */
    temp = ZGetVariable("exposure");
    if (temp) {
	if (!(exposure = ZParseExposureLevel(temp))) {
	    ERROR2("invalid exposure level %s, using exposure level none instead.\n", temp);
	    exposure = EXPOSE_NONE;
	}
    } else
      exposure = EXPOSE_OPSTAFF;
    error_code = ZSetLocation(exposure); /* <<<>>> */
    if (error_code != ZERR_LOGINFAIL)
      TRAP( error_code, "while setting location" );

    /*
     * If the exposure level isn't EXPOSE_NONE, turn on recieving notices.
     * (this involves reading in the subscription file, etc.)
     */
    if (string_Neq(exposure, EXPOSE_NONE))
      zwgc_startup();

    /*
     * Set $realm to our realm and $user to our zephyr username:
     */
    var_set_variable("realm", (char *)ZGetRealm()); /* XXX should propagate the
						     * const */
    var_set_variable("user", ZGetSender());

    /*
     * <<<>>>
     */
    nhp->notice_handler = notice_handler;
    mux_add_input_source(ZGetFD(), (void (*)(void *))handle_zephyr_input, nhp);
    zephyr_inited = 1;
    return;
}

/*
 *
 */

void finalize_zephyr(void) /* <<<>>> */
{
    string temp;

    if (zephyr_inited) {
	/*
	 * Remove the file containing our port # since it is no longer needed:
	 */
	errno = 0;
	temp = get_zwgc_port_number_filename();
	unlink(temp);
	if (errno) {
	    fprintf(stderr, "zwgc: error while trying to delete %s: ", temp);
	    perror("");
	}

	/*
	 * Cancel our subscriptions, unset our location, and close our zephyr
	 * connection:
	 */
#ifdef DEBUG
	if (zwgc_debug) {
	    TRAP( ZUnsetLocation(), "while unsetting location" );
	    TRAP( ZCancelSubscriptions(0), "while canceling subscriptions" );
	} else {
#endif /* DEBUG */
	    (void) ZUnsetLocation();
	    (void) ZCancelSubscriptions(0);
#ifdef DEBUG
	}
#endif /* DEBUG */
	ZClosePort();
    }
    return;
}