forked from aglasgall/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zephyr.c
1538 lines (1338 loc) · 39.2 KB
/
zephyr.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "owl.h"
#include <stdio.h>
#include <sys/stat.h>
#ifdef HAVE_LIBZEPHYR
static GSource *owl_zephyr_event_source_new(int fd);
static gboolean owl_zephyr_event_prepare(GSource *source, int *timeout);
static gboolean owl_zephyr_event_check(GSource *source);
static gboolean owl_zephyr_event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data);
static GList *deferred_subs = NULL;
typedef struct _owl_sub_list { /* noproto */
ZSubscription_t *subs;
int nsubs;
} owl_sub_list;
Code_t ZResetAuthentication(void);
static GSourceFuncs zephyr_event_funcs = {
owl_zephyr_event_prepare,
owl_zephyr_event_check,
owl_zephyr_event_dispatch,
NULL
};
#endif
#define HM_SVC_FALLBACK htons((unsigned short) 2104)
static CALLER_OWN char *owl_zephyr_dotfile(const char *name, const char *input)
{
if (input != NULL)
return g_strdup(input);
else
return g_build_filename(owl_global_get_homedir(&g), name, NULL);
}
#ifdef HAVE_LIBZEPHYR
void owl_zephyr_initialize(void)
{
Code_t ret;
struct servent *sp;
struct sockaddr_in sin;
ZNotice_t req;
GIOChannel *channel;
/*
* Code modified from libzephyr's ZhmStat.c
*
* Modified to add the fd to our select loop, rather than hanging
* until we get an ack.
*/
if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
owl_function_error("Error opening Zephyr port: %s", error_message(ret));
return;
}
(void) memset(&sin, 0, sizeof(struct sockaddr_in));
sp = getservbyname(HM_SVCNAME, "udp");
sin.sin_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
(void) memset(&req, 0, sizeof(req));
req.z_kind = STAT;
req.z_port = 0;
req.z_class = zstr(HM_STAT_CLASS);
req.z_class_inst = zstr(HM_STAT_CLIENT);
req.z_opcode = zstr(HM_GIMMESTATS);
req.z_sender = zstr("");
req.z_recipient = zstr("");
req.z_default_format = zstr("");
req.z_message_len = 0;
if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
owl_function_error("Initializing Zephyr: %s", error_message(ret));
return;
}
if ((ret = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE) {
owl_function_error("Initializing Zephyr: %s", error_message(ret));
return;
}
channel = g_io_channel_unix_new(ZGetFD());
g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
&owl_zephyr_finish_initialization, NULL);
g_io_channel_unref(channel);
}
gboolean owl_zephyr_finish_initialization(GIOChannel *source, GIOCondition condition, void *data) {
Code_t code;
char *perl;
GSource *event_source;
ZClosePort();
if ((code = ZInitialize()) != ZERR_NONE) {
owl_function_error("Initializing Zephyr: %s", error_message(code));
return FALSE;
}
if ((code = ZOpenPort(NULL)) != ZERR_NONE) {
owl_function_error("Initializing Zephyr: %s", error_message(code));
return FALSE;
}
event_source = owl_zephyr_event_source_new(ZGetFD());
g_source_attach(event_source, NULL);
g_source_unref(event_source);
owl_global_set_havezephyr(&g);
if(g.load_initial_subs) {
owl_zephyr_load_initial_subs();
}
while(deferred_subs != NULL) {
owl_sub_list *subs = deferred_subs->data;
owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs);
owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs);
deferred_subs = g_list_delete_link(deferred_subs, deferred_subs);
g_slice_free(owl_sub_list, subs);
}
/* zlog in if we need to */
if (owl_global_is_startuplogin(&g)) {
owl_function_debugmsg("startup: doing zlog in");
owl_zephyr_zlog_in();
}
/* check pseudo-logins if we need to */
if (owl_global_is_pseudologins(&g)) {
owl_function_debugmsg("startup: checking pseudo-logins");
owl_function_zephyr_buddy_check(0);
}
perl = owl_perlconfig_execute("BarnOwl::Zephyr::_zephyr_startup()");
g_free(perl);
return FALSE;
}
void owl_zephyr_load_initial_subs(void) {
int ret_sd, ret_bd, ret_u;
owl_function_debugmsg("startup: loading initial zephyr subs");
/* load default subscriptions */
ret_sd = owl_zephyr_loaddefaultsubs();
/* load BarnOwl default subscriptions */
ret_bd = owl_zephyr_loadbarnowldefaultsubs();
/* load subscriptions from subs file */
ret_u = owl_zephyr_loadsubs(NULL, 0);
if (ret_sd || ret_bd || ret_u) {
owl_function_error("Error loading zephyr subscriptions");
}
/* load login subscriptions */
if (owl_global_is_loginsubs(&g)) {
owl_function_debugmsg("startup: loading login subs");
owl_function_loadloginsubs(NULL);
}
}
#else
void owl_zephyr_initialize(void)
{
}
#endif
int owl_zephyr_shutdown(void)
{
#ifdef HAVE_LIBZEPHYR
if(owl_global_is_havezephyr(&g)) {
unsuball();
ZClosePort();
}
#endif
return 0;
}
int owl_zephyr_zpending(void)
{
#ifdef HAVE_LIBZEPHYR
Code_t code;
if(owl_global_is_havezephyr(&g)) {
if((code = ZPending()) < 0) {
owl_function_debugmsg("Error (%s) in ZPending()\n",
error_message(code));
return 0;
}
return code;
}
#endif
return 0;
}
int owl_zephyr_zqlength(void)
{
#ifdef HAVE_LIBZEPHYR
Code_t code;
if(owl_global_is_havezephyr(&g)) {
if((code = ZQLength()) < 0) {
owl_function_debugmsg("Error (%s) in ZQLength()\n",
error_message(code));
return 0;
}
return code;
}
#endif
return 0;
}
const char *owl_zephyr_get_realm(void)
{
#ifdef HAVE_LIBZEPHYR
if (owl_global_is_havezephyr(&g))
return(ZGetRealm());
#endif
return "";
}
const char *owl_zephyr_get_sender(void)
{
#ifdef HAVE_LIBZEPHYR
if (owl_global_is_havezephyr(&g))
return(ZGetSender());
#endif
return "";
}
#ifdef HAVE_LIBZEPHYR
int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
{
int ret = 0;
Code_t code;
if (owl_global_is_havezephyr(&g)) {
int i;
/* sub without defaults */
code = ZSubscribeToSansDefaults(subs, count, 0);
if (code != ZERR_NONE) {
owl_function_error("Error subscribing to zephyr notifications: %s",
error_message(code));
ret=-2;
}
/* free stuff */
for (i=0; i<count; i++) {
g_free(subs[i].zsub_class);
g_free(subs[i].zsub_classinst);
g_free(subs[i].zsub_recipient);
}
g_free(subs);
} else {
owl_sub_list *s = g_slice_new(owl_sub_list);
s->subs = subs;
s->nsubs = count;
deferred_subs = g_list_append(deferred_subs, s);
}
return ret;
}
#endif
/* Load zephyr subscriptions from 'filename'. If 'filename' is NULL,
* the default file $HOME/.zephyr.subs will be used.
*
* Returns 0 on success. If the file does not exist, return -1 if
* 'error_on_nofile' is 1, otherwise return 0. Return -1 if the file
* exists but can not be read. Return -2 if there is a failure from
* zephyr to load the subscriptions.
*/
int owl_zephyr_loadsubs(const char *filename, int error_on_nofile)
{
#ifdef HAVE_LIBZEPHYR
FILE *file;
int fopen_errno;
char *tmp, *start;
char *buffer = NULL;
char *subsfile;
ZSubscription_t *subs;
int subSize = 1024;
int count;
subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
count = 0;
file = fopen(subsfile, "r");
fopen_errno = errno;
g_free(subsfile);
if (!file) {
if (error_on_nofile == 1 || fopen_errno != ENOENT)
return -1;
return 0;
}
subs = g_new(ZSubscription_t, subSize);
while (owl_getline(&buffer, file)) {
if (buffer[0] == '#' || buffer[0] == '\n')
continue;
if (buffer[0] == '-')
start = buffer + 1;
else
start = buffer;
if (count >= subSize) {
subSize *= 2;
subs = g_renew(ZSubscription_t, subs, subSize);
}
/* add it to the list of subs */
if ((tmp = strtok(start, ",\n\r")) == NULL)
continue;
subs[count].zsub_class = g_strdup(tmp);
if ((tmp=strtok(NULL, ",\n\r")) == NULL)
continue;
subs[count].zsub_classinst = g_strdup(tmp);
if ((tmp = strtok(NULL, " \t\n\r")) == NULL)
continue;
subs[count].zsub_recipient = g_strdup(tmp);
/* if it started with '-' then add it to the global punt list, and
* remove it from the list of subs. */
if (buffer[0] == '-') {
owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
g_free(subs[count].zsub_class);
g_free(subs[count].zsub_classinst);
g_free(subs[count].zsub_recipient);
} else {
count++;
}
}
fclose(file);
if (buffer)
g_free(buffer);
ZResetAuthentication();
return owl_zephyr_loadsubs_helper(subs, count);
#else
return 0;
#endif
}
/* Load default BarnOwl subscriptions
*
* Returns 0 on success.
* Return -2 if there is a failure from zephyr to load the subscriptions.
*/
int owl_zephyr_loadbarnowldefaultsubs(void)
{
#ifdef HAVE_LIBZEPHYR
ZSubscription_t *subs;
int subSize = 10; /* Max BarnOwl default subs we allow */
int count, ret;
subs = g_new(ZSubscription_t, subSize);
ZResetAuthentication();
count=0;
subs[count].zsub_class=g_strdup("message");
subs[count].zsub_classinst=g_strdup("*");
subs[count].zsub_recipient=g_strdup("%me%");
count++;
ret = owl_zephyr_loadsubs_helper(subs, count);
return(ret);
#else
return(0);
#endif
}
int owl_zephyr_loaddefaultsubs(void)
{
#ifdef HAVE_LIBZEPHYR
Code_t ret;
if (owl_global_is_havezephyr(&g)) {
ZSubscription_t subs[10];
ret = ZSubscribeTo(subs, 0, 0);
if (ret != ZERR_NONE) {
owl_function_error("Error subscribing to default zephyr notifications: %s.",
error_message(ret));
return(-1);
}
}
return(0);
#else
return(0);
#endif
}
int owl_zephyr_loadloginsubs(const char *filename)
{
#ifdef HAVE_LIBZEPHYR
FILE *file;
ZSubscription_t *subs;
int numSubs = 100;
char *subsfile;
char *buffer = NULL;
int count;
subs = g_new(ZSubscription_t, numSubs);
subsfile = owl_zephyr_dotfile(".anyone", filename);
count = 0;
file = fopen(subsfile, "r");
g_free(subsfile);
if (file) {
while (owl_getline_chomp(&buffer, file)) {
if (buffer[0] == '\0' || buffer[0] == '#')
continue;
if (count == numSubs) {
numSubs *= 2;
subs = g_renew(ZSubscription_t, subs, numSubs);
}
subs[count].zsub_class = g_strdup("login");
subs[count].zsub_recipient = g_strdup("*");
subs[count].zsub_classinst = long_zuser(buffer);
count++;
}
fclose(file);
} else {
g_free(subs);
return 0;
}
g_free(buffer);
ZResetAuthentication();
return owl_zephyr_loadsubs_helper(subs, count);
#else
return 0;
#endif
}
bool unsuball(void)
{
#if HAVE_LIBZEPHYR
Code_t ret;
ZResetAuthentication();
ret = ZCancelSubscriptions(0);
if (ret != ZERR_NONE)
owl_function_error("Zephyr: Cancelling subscriptions: %s",
error_message(ret));
return (ret == ZERR_NONE);
#endif
return true;
}
int owl_zephyr_sub(const char *class, const char *inst, const char *recip)
{
#ifdef HAVE_LIBZEPHYR
ZSubscription_t subs[5];
Code_t ret;
subs[0].zsub_class=zstr(class);
subs[0].zsub_classinst=zstr(inst);
subs[0].zsub_recipient=zstr(recip);
ZResetAuthentication();
ret = ZSubscribeTo(subs, 1, 0);
if (ret != ZERR_NONE) {
owl_function_error("Error subbing to <%s,%s,%s>: %s",
class, inst, recip,
error_message(ret));
return(-2);
}
return(0);
#else
return(0);
#endif
}
int owl_zephyr_unsub(const char *class, const char *inst, const char *recip)
{
#ifdef HAVE_LIBZEPHYR
ZSubscription_t subs[5];
Code_t ret;
subs[0].zsub_class=zstr(class);
subs[0].zsub_classinst=zstr(inst);
subs[0].zsub_recipient=zstr(recip);
ZResetAuthentication();
ret = ZUnsubscribeTo(subs, 1, 0);
if (ret != ZERR_NONE) {
owl_function_error("Error unsubbing from <%s,%s,%s>: %s",
class, inst, recip,
error_message(ret));
return(-2);
}
return(0);
#else
return(0);
#endif
}
#ifdef HAVE_LIBZEPHYR
const char *owl_zephyr_first_raw_field(const ZNotice_t *n)
{
if (n->z_message_len == 0)
return NULL;
return n->z_message;
}
const char *owl_zephyr_next_raw_field(const ZNotice_t *n, const char *f)
{
const char *end = n->z_message + n->z_message_len;
f = memchr(f, '\0', end - f);
if (f == NULL)
return NULL;
return f + 1;
}
const char *owl_zephyr_get_raw_field(const ZNotice_t *n, int j)
{
int i;
const char *f;
for (i = 1, f = owl_zephyr_first_raw_field(n); i < j && f != NULL;
i++, f = owl_zephyr_next_raw_field(n, f))
;
return f;
}
CALLER_OWN char *owl_zephyr_field(const ZNotice_t *n, const char *f)
{
if (f == NULL)
return g_strdup("");
return g_strndup(f, n->z_message + n->z_message_len - f);
}
CALLER_OWN char *owl_zephyr_field_as_utf8(const ZNotice_t *n, const char *f)
{
char *tmp = owl_zephyr_field(n, f);
char *out = owl_validate_or_convert(tmp);
g_free(tmp);
return out;
}
CALLER_OWN char *owl_zephyr_get_field(const ZNotice_t *n, int j)
{
return owl_zephyr_field(n, owl_zephyr_get_raw_field(n, j));
}
CALLER_OWN char *owl_zephyr_get_field_as_utf8(const ZNotice_t *n, int j)
{
return owl_zephyr_field_as_utf8(n, owl_zephyr_get_raw_field(n, j));
}
#else
const char *owl_zephyr_first_raw_field(const void *n)
{
return NULL;
}
const char *owl_zephyr_next_raw_field(const void *n, const char *f)
{
return NULL;
}
const char *owl_zephyr_get_raw_field(const void *n, int j)
{
return NULL;
}
CALLER_OWN char *owl_zephyr_field(const void *n, const char *f)
{
return g_strdup("");
}
CALLER_OWN char *owl_zephyr_field_as_utf8(const void *n, const char *f)
{
return g_strdup("");
}
CALLER_OWN char *owl_zephyr_get_field(const void *n, int j)
{
return g_strdup("");
}
CALLER_OWN char *owl_zephyr_get_field_as_utf8(const void *n, int j)
{
return owl_zephyr_field(n, owl_zephyr_get_raw_field(n, j));
}
#endif
#ifdef HAVE_LIBZEPHYR
int owl_zephyr_get_num_fields(const ZNotice_t *n)
{
int i;
const char *f;
for (i = 0, f = owl_zephyr_first_raw_field(n); f != NULL;
i++, f = owl_zephyr_next_raw_field(n, f))
;
return i;
}
#else
int owl_zephyr_get_num_fields(const void *n)
{
return(0);
}
#endif
#ifdef HAVE_LIBZEPHYR
/* return a pointer to the message, place the message length in k
* caller must free the return
*/
CALLER_OWN char *owl_zephyr_get_message(const ZNotice_t *n, const owl_message *m)
{
#define OWL_NFIELDS 5
int i;
char *fields[OWL_NFIELDS + 1];
char *msg = NULL;
/* don't let ping messages have a body */
if (!strcasecmp(n->z_opcode, "ping")) {
return(g_strdup(""));
}
for(i = 0; i < OWL_NFIELDS; i++)
fields[i + 1] = owl_zephyr_get_field(n, i + 1);
/* deal with MIT NOC messages */
if (!strcasecmp(n->z_default_format, "@center(@bold(NOC Message))\n\n@bold(Sender:) $1 <$sender>\n@bold(Time: ) $time\n\n@italic($opcode service on $instance $3.) $4\n")) {
msg = g_strdup_printf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, fields[3], fields[4]);
}
/* deal with MIT Discuss messages */
else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4") ||
!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3\nSubject: $4")) {
msg = g_strdup_printf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s",
fields[1], fields[2], fields[3], fields[5], fields[4]);
}
/* deal with MIT Moira messages */
else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
msg = g_strdup_printf("MOIRA %s on %s: %s",
n->z_class_inst,
owl_message_get_hostname(m),
fields[1]);
} else {
if (owl_zephyr_get_num_fields(n) == 1)
msg = g_strdup(fields[1]);
else
msg = g_strdup(fields[2]);
}
for (i = 0; i < OWL_NFIELDS; i++)
g_free(fields[i + 1]);
return msg;
}
#endif
#ifdef HAVE_LIBZEPHYR
const char *owl_zephyr_get_zsig(const ZNotice_t *n, int *k)
{
/* return a pointer to the zsig if there is one */
/* message length 0? No zsig */
if (n->z_message_len==0) {
*k=0;
return("");
}
/* If there's only one field, no zsig */
if (owl_zephyr_get_num_fields(n) == 1) {
*k=0;
return("");
}
/* Everything else is field 1 */
*k=strlen(n->z_message);
return(n->z_message);
}
#else
const char *owl_zephyr_get_zsig(const void *n, int *k)
{
return("");
}
#endif
int send_zephyr(const char *opcode, const char *zsig, const char *class, const char *instance, const char *recipient, const char *message)
{
#ifdef HAVE_LIBZEPHYR
Code_t ret;
ZNotice_t notice;
char *zsender = NULL;
memset(¬ice, 0, sizeof(notice));
ZResetAuthentication();
if (!zsig) zsig="";
notice.z_kind=ACKED;
notice.z_port=0;
#ifdef ZCHARSET_UTF_8
notice.z_charset = ZCHARSET_UTF_8;
#endif
notice.z_class=zstr(class);
notice.z_class_inst=zstr(instance);
if (!strcmp(recipient, "@")) {
notice.z_recipient=zstr("");
} else {
notice.z_recipient=zstr(recipient);
}
if (!owl_zwrite_recip_is_personal(recipient) && *owl_global_get_zsender(&g))
notice.z_sender = zsender = long_zuser(owl_global_get_zsender(&g));
notice.z_default_format=zstr(ZEPHYR_DEFAULT_FORMAT);
if (opcode) notice.z_opcode=zstr(opcode);
notice.z_message_len=strlen(zsig)+1+strlen(message);
notice.z_message=g_new(char, notice.z_message_len+10);
strcpy(notice.z_message, zsig);
memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
/* ret=ZSendNotice(¬ice, ZAUTH); */
ret=ZSrvSendNotice(¬ice, ZAUTH, send_zephyr_helper);
/* free then check the return */
g_free(notice.z_message);
g_free(zsender);
if (ret != ZERR_NONE) {
owl_function_error("Error sending zephyr: %s", error_message(ret));
return(ret);
}
return(0);
#else
return(0);
#endif
}
#ifdef HAVE_LIBZEPHYR
Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
{
return(ZSendPacket(buf, len, 0));
}
#endif
void send_ping(const char *to, const char *zclass, const char *zinstance)
{
#ifdef HAVE_LIBZEPHYR
send_zephyr("PING", "", zclass, zinstance, to, "");
#endif
}
#ifdef HAVE_LIBZEPHYR
void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
{
char *tmp;
/* if it's an HMACK ignore it */
if (retnotice->z_kind == HMACK) return;
if (retnotice->z_kind == SERVNAK) {
owl_function_error("Authorization failure sending zephyr");
} else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
owl_function_error("Detected server failure while receiving acknowledgement");
} else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
if (!strcasecmp(retnotice->z_opcode, "ping")) {
return;
} else {
if (strcasecmp(retnotice->z_recipient, ""))
{ /* personal */
tmp=short_zuser(retnotice->z_recipient);
if(!strcasecmp(retnotice->z_class, "message") &&
!strcasecmp(retnotice->z_class_inst, "personal")) {
owl_function_makemsg("Message sent to %s.", tmp);
} else if(!strcasecmp(retnotice->z_class, "message")) { /* instanced, but not classed, personal */
owl_function_makemsg("Message sent to %s on -i %s\n", tmp, retnotice->z_class_inst);
} else { /* classed personal */
owl_function_makemsg("Message sent to %s on -c %s -i %s\n", tmp, retnotice->z_class, retnotice->z_class_inst);
}
g_free(tmp);
} else {
/* class / instance message */
owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
}
}
} else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
if (retnotice->z_recipient == NULL
|| !owl_zwrite_recip_is_personal(retnotice->z_recipient)) {
char *buff;
owl_function_error("No one subscribed to class %s", retnotice->z_class);
buff = g_strdup_printf("Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
owl_function_adminmsg("", buff);
g_free(buff);
} else {
char *buff;
owl_zwrite zw;
tmp = short_zuser(retnotice->z_recipient);
owl_function_error("%s: Not logged in or subscribing.", tmp);
/*
* These error messages are often over 80 chars, but users who want to
* see the whole thing can scroll to the side, and for those with wide
* terminals or who don't care, not splitting saves a line in the UI
*/
if(strcasecmp(retnotice->z_class, "message")) {
buff = g_strdup_printf(
"Could not send message to %s: "
"not logged in or subscribing to class %s, instance %s.\n",
tmp,
retnotice->z_class,
retnotice->z_class_inst);
} else if(strcasecmp(retnotice->z_class_inst, "personal")) {
buff = g_strdup_printf(
"Could not send message to %s: "
"not logged in or subscribing to instance %s.\n",
tmp,
retnotice->z_class_inst);
} else {
buff = g_strdup_printf(
"Could not send message to %s: "
"not logged in or subscribing to messages.\n",
tmp);
}
owl_function_adminmsg("", buff);
memset(&zw, 0, sizeof(zw));
zw.class = g_strdup(retnotice->z_class);
zw.inst = g_strdup(retnotice->z_class_inst);
zw.realm = g_strdup("");
zw.opcode = g_strdup(retnotice->z_opcode);
zw.zsig = g_strdup("");
zw.recips = g_ptr_array_new();
g_ptr_array_add(zw.recips, g_strdup(retnotice->z_recipient));
owl_log_outgoing_zephyr_error(&zw, buff);
owl_zwrite_cleanup(&zw);
g_free(buff);
g_free(tmp);
}
} else {
owl_function_error("Internal error on ack (%s)", retnotice->z_message);
}
}
#else
void owl_zephyr_handle_ack(const void *retnotice)
{
}
#endif
#ifdef HAVE_LIBZEPHYR
int owl_zephyr_notice_is_ack(const ZNotice_t *n)
{
if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
return(1);
}
return(0);
}
#else
int owl_zephyr_notice_is_ack(const void *n)
{
return(0);
}
#endif
void owl_zephyr_zaway(const owl_message *m)
{
#ifdef HAVE_LIBZEPHYR
char *tmpbuff, *myuser, *to;
owl_zwrite *z;
/* bail if it doesn't look like a message we should reply to. Some
* of this defined by the way zaway(1) works
*/
if (strcasecmp(owl_message_get_class(m), "message")) return;
if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
if (!strcasecmp(owl_message_get_sender(m), "")) return;
if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
if (owl_message_get_attribute_value(m, "isauto")) return;
if (owl_global_is_smartstrip(&g)) {
to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
} else {
to=g_strdup(owl_message_get_sender(m));
}
send_zephyr("",
"Automated reply:",
owl_message_get_class(m),
owl_message_get_instance(m),
to,
owl_global_get_zaway_msg(&g));
myuser=short_zuser(to);
if (!strcasecmp(owl_message_get_instance(m), "personal")) {
tmpbuff = owl_string_build_quoted("zwrite %q", myuser);
} else {
tmpbuff = owl_string_build_quoted("zwrite -i %q %q", owl_message_get_instance(m), myuser);
}
g_free(myuser);
g_free(to);
z = owl_zwrite_new_from_line(tmpbuff);
g_free(tmpbuff);
if (z == NULL) {
owl_function_error("Error creating outgoing zephyr.");
return;
}
owl_zwrite_set_message(z, owl_global_get_zaway_msg(&g));
owl_zwrite_set_zsig(z, "Automated reply:");
/* display the message as an admin message in the receive window */
owl_function_add_outgoing_zephyrs(z);
owl_zwrite_delete(z);
#endif
}
#ifdef HAVE_LIBZEPHYR
void owl_zephyr_hackaway_cr(ZNotice_t *n)
{
/* replace \r's with ' '. Gross-ish */
int i;
for (i=0; i<n->z_message_len; i++) {
if (n->z_message[i]=='\r') {
n->z_message[i]=' ';
}
}
}
#endif
CALLER_OWN char *owl_zephyr_zlocate(const char *user, int auth)
{
#ifdef HAVE_LIBZEPHYR
int ret, numlocs;
int one = 1;
ZLocations_t locations;
char *myuser;
char *p, *result;
ZResetAuthentication();
ret = ZLocateUser(zstr(user), &numlocs, auth ? ZAUTH : ZNOAUTH);
if (ret != ZERR_NONE)
return g_strdup_printf("Error locating user %s: %s\n",
user, error_message(ret));
myuser = short_zuser(user);
if (numlocs == 0) {
result = g_strdup_printf("%s: Hidden or not logged in\n", myuser);
} else {
result = g_strdup("");
for (; numlocs; numlocs--) {
ZGetLocations(&locations, &one);
p = g_strdup_printf("%s%s: %s\t%s\t%s\n",
result, myuser,
locations.host ? locations.host : "?",
locations.tty ? locations.tty : "?",
locations.time ? locations.time : "?");
g_free(result);
result = p;
}
}
g_free(myuser);
return result;
#else
return g_strdup("");
#endif
}
void owl_zephyr_addsub(const char *filename, const char *class, const char *inst, const char *recip)
{
#ifdef HAVE_LIBZEPHYR
char *line, *subsfile, *s = NULL;
FILE *file;
int duplicate = 0;
line = owl_zephyr_makesubline(class, inst, recip);
subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
/* if the file already exists, check to see if the sub is already there */
file = fopen(subsfile, "r");
if (file) {
while (owl_getline(&s, file)) {
if (strcasecmp(s, line) == 0) {
owl_function_error("Subscription already present in %s", subsfile);
duplicate++;