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
|
#include <zephyr/zephyr.h>
#include "xzwrite.h"
#include <strings.h>
static int zeph_send_notice();
extern Defaults defs;
/* ARGSUSED */
void zeph_dispatch(client_data, source, input_id)
XtPointer client_data;
int *source;
XtInputId *input_id;
{
ZNotice_t notice;
struct sockaddr_in from;
int ret;
while (ZPending() > 0) {
ret = ZReceiveNotice(¬ice, &from);
if (ret != ZERR_NONE) {
Warning(error_message(ret), " while receiving Zephyr notice.",
NULL);
continue;
}
if (defs.track_logins &&
(! strcmp(notice.z_opcode, "USER_LOGIN") ||
! strcmp(notice.z_opcode, "USER_LOGOUT")))
logins_deal(¬ice);
else if (defs.auto_reply &&
! strcasecmp(notice.z_class, DEFAULT_CLASS) &&
! strcasecmp(notice.z_recipient, ZGetSender()))
dest_add_reply(¬ice);
else {
Warning("Invalid notice.\n", "To: <", notice.z_class, ", ",
notice.z_class_inst, ", ", (*notice.z_recipient) ?
notice.z_recipient : "*", ">\n",
"From: ", notice.z_sender, "\nOpcode: ",
notice.z_opcode, "\nMessage: ", notice.z_message,
"\n", NULL);
}
ZFreeNotice(¬ice);
}
}
void zeph_init()
{
int retval;
retval = ZInitialize();
if (retval != ZERR_NONE)
Error("Cannot initialize the Zephyr library.", NULL);
retval = ZOpenPort((int *) 0);
if (retval != ZERR_NONE)
Error("Cannot open Zephyr port.", NULL);
}
int zeph_locateable(user)
char *user;
{
char buf[BUFSIZ];
int n;
if (index(user, '@') == NULL)
sprintf(buf, "%s@%s", user, ZGetRealm());
ZLocateUser(buf, &n);
return (!! n);
}
/* XXX This will break on interrealm zephyr */
void zeph_subto_logins(users, num)
char **users;
int num;
{
ZSubscription_t *sublist;
char *name, *realm;
int rlen, c = 0;
realm = ZGetRealm();
rlen = strlen(realm);
sublist = (ZSubscription_t *) Malloc(num*sizeof(ZSubscription_t),
"while subscribing to logins", NULL);
while (c < num) {
sublist[c].zsub_class = "login";
sublist[c].zsub_recipient = "";
name = (char *) Malloc(strlen(users[c])+rlen+2,
"while subscribing to login, ", users[c],
NULL);
if (index(users[c], '@'))
sprintf(name, "%s", users[c]);
else
sprintf(name, "%s@%s", users[c], realm);
sublist[c].zsub_classinst = name;
c += 1;
}
ZSubscribeToSansDefaults(sublist, c, (unsigned short) 0);
for(; c; --c)
free(sublist[c-1].zsub_classinst);
free(sublist);
}
void zeph_subto_replies()
{
ZSubscription_t sub;
sub.zsub_class = "message";
sub.zsub_classinst = "*";
sub.zsub_recipient = ZGetSender();
ZSubscribeToSansDefaults(&sub, 1, (unsigned short) 0);
}
int zeph_send_message(dest, msg)
Dest dest;
char *msg;
{
ZNotice_t notice;
int msglen, siglen, ret;
char *sig_msg;
msglen = strlen(msg);
siglen = strlen(defs.signature);
sig_msg = (char *) Malloc(msglen + siglen + 2, "while sending message",
NULL);
sprintf(sig_msg, "%s%c%s", defs.signature, '\0', msg);
bzero((char *) ¬ice, sizeof(ZNotice_t));
notice.z_kind = ACKED;
notice.z_class = dest->zclass;
notice.z_class_inst = dest->zinst;
notice.z_recipient = dest->zrecip;
notice.z_sender = 0;
notice.z_opcode = defs.opcode;
notice.z_port = 0;
notice.z_message = sig_msg;
notice.z_message_len = msglen + siglen + 1;
/* This really gross looking mess is brought to you by zwrite.c */
if (defs.auth) {
if (*defs.signature)
notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient)\n@bold($1) <$sender>\n\n$2";
else
notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient)\n$message";
}
else {
if (*defs.signature)
notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance:\n@bold($1) <$sender>\n\n$2";
else
notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance:\n$message";
}
ret = zeph_send_notice(¬ice, (defs.auth) ? ZAUTH : ZNOAUTH);
free(sig_msg);
return ret;
}
int zeph_ping(dest)
Dest dest;
{
ZNotice_t notice;
bzero((char *) ¬ice, sizeof(ZNotice_t));
notice.z_kind = ACKED;
notice.z_class = dest->zclass;
notice.z_class_inst = dest->zinst;
notice.z_recipient = dest->zrecip;
notice.z_opcode = "PING";
/* Should a PING ever be authenticated? */
return (zeph_send_notice(¬ice, ZNOAUTH));
}
int zeph_pong(dest)
Dest dest;
{
ZNotice_t notice;
bzero((char *) ¬ice, sizeof(ZNotice_t));
notice.z_kind = ACKED;
notice.z_class = dest->zclass;
notice.z_class_inst = dest->zinst;
notice.z_recipient = dest->zrecip;
notice.z_opcode = "PING";
notice.z_message = "PONG";
notice.z_message_len = 4;
/* Should a PING ever be authenticated? */
return (zeph_send_notice(¬ice, ZNOAUTH));
}
char *zeph_get_signature()
{
char *sig;
sig = ZGetVariable("xzwrite-signature");
if (! sig) sig = ZGetVariable("zwrite-signature");
return sig;
}
static int zeph_send_notice(notice, auth)
ZNotice_t *notice;
int (*auth)();
{
int retval;
ZNotice_t retnotice;
/* Send message with appropriate authentication */
retval = ZSendNotice(notice, auth);
if (retval != ZERR_NONE) {
if (defs.debug)
Warning(error_message(retval), " while sending message.", NULL);
return SENDFAIL_SEND;
}
/* Wait for server acknowledgement */
retval = ZIfNotice(&retnotice, (struct sockaddr_in *) 0,
ZCompareUIDPred, (char *) ¬ice->z_uid);
if (retval != ZERR_NONE) {
if (defs.debug)
Warning(error_message(retval),
" while waiting for acknowledgement.", NULL);
return SENDFAIL_ACK;
}
/* Make sure someone receives it */
if (strcmp(retnotice.z_message, ZSRVACK_NOTSENT)==0)
return SENDFAIL_RECV;
return SEND_OK;
}
#ifdef DEBUG
/* debugging function */
void zeph_display_subscriptions()
{
ZSubscription_t sub;
int n, retval, i = 1;
retval = ZRetrieveSubscriptions((unsigned short) 0, &n);
if (retval != ZERR_NONE) {
Warning(error_message(retval), " while retrieving subscriptions.",
NULL);
return;
}
printf("Retrieving %d subscriptions.\n", n);
while (ZGetSubscriptions(&sub, &i) == ZERR_NONE) {
if (i != 1)
Warning("Subscriptions skipped while printing.", NULL);
printf("<%s,%s,%s>\n", sub.class, (*sub.zsub_classinst) ?
sub.zsub_classinst : "**", (*sub.zsub_recipient) ?
sub.zsub_recipient : "**");
}
}
#endif
|