forked from tphelps/xtickertape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usenet_sub.c
860 lines (740 loc) · 24.6 KB
/
usenet_sub.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
/* -*- mode: c; c-file-style: "elvin" -*- */
/***********************************************************************
Copyright (C) 1997-2009 by Mantara Software (ABN 17 105 665 594).
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the Mantara Software nor the names
of its contributors may be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***********************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h> /* fprintf, snprintf */
#ifdef HAVE_STDLIB_H
# include <stdlib.h> /* exit, free, malloc, realloc */
#endif
#ifdef HAVE_STRING_H
# include <string.h> /* strlen */
#endif
#include <X11/Intrinsic.h>
#include <elvin/elvin.h>
#include <elvin/xt_mainloop.h>
#include "replace.h"
#include "globals.h"
#include "usenet_sub.h"
#include "utils.h"
/* Some notification field names */
#define BODY "BODY"
#define FROM_NAME "FROM_NAME"
#define FROM_EMAIL "FROM_EMAIL"
#define FROM "From"
#define KEYWORDS "KEYWORDS"
#define MESSAGE_ID "Message-ID"
#define MIME_ARGS "MIME_ARGS"
#define MIME_TYPE "MIME_TYPE"
#define NEWSGROUPS "NEWSGROUPS"
#define SUBJECT "SUBJECT"
#define URL_MIME_TYPE "x-elvin/url"
#define NEWS_MIME_TYPE "x-elvin/news"
#define XPOSTS "CROSS_POSTS"
#define X_NNTP_HOST "X-NNTP-Host"
#define F_MATCHES "regex(%s, \"%s\")"
#define F_NOT_MATCHES "!regex(%s, \"%s\")"
#define F_EQ "%s == %s"
#define F_STRING_EQ "%s == \"%s\""
#define F_NEQ "%s != %s"
#define F_STRING_NEQ "%s != \"%s\""
#define F_LT "%s < %s"
#define F_GT "%s > %s"
#define F_LE "%s <= %s"
#define F_GE "%s >= %s"
#define ONE_SUB "ELVIN_CLASS == \"NEWSWATCHER\" && (%s)"
#define PATTERN_ONLY "%sregex(NEWSGROUPS, \"%s\")"
#define PATTERN_PLUS "(%sregex(NEWSGROUPS, \"%s\"))"
#define USENET_PREFIX "usenet: %s"
#define NEWS_URL "news://%s/%s"
#define ATTACHMENT_FMT \
"MIME-Version: 1.0\n" \
"Content-Type: %s; charset=us-ascii\n" \
"\n" \
"%s\n"
/* compatability code for return code of callbacks */
#if !defined(ELVIN_VERSION_AT_LEAST)
# define ELVIN_RETURN_FAILURE
# define ELVIN_RETURN_SUCCESS
#elif ELVIN_VERSION_AT_LEAST(4, 1, -1)
# define ELVIN_RETURN_FAILURE 0
# define ELVIN_RETURN_SUCCESS 1
#endif
/* The structure of a usenet subscription */
struct usenet_sub {
/* The receiver's subscription expression */
char *expression;
/* The length of the subscription expression */
size_t expression_size;
/* The receiver's elvin connection handle */
elvin_handle_t handle;
/* The reciever's subscription */
elvin_subscription_t subscription;
/* The receiver's callback */
usenet_sub_callback_t callback;
/* The argument for the receiver's callback */
void *rock;
/* Non-zero if the receiver is waiting on a change to the subscription */
int is_pending;
};
#if !defined(ELVIN_VERSION_AT_LEAST)
/* Delivers a notification which matches the receiver's subscription
* expression */
static void
notify_cb(elvin_handle_t handle,
elvin_subscription_t subscription,
elvin_notification_t notification,
int is_secure,
void *rock,
elvin_error_t error)
{
usenet_sub_t self = (usenet_sub_t)rock;
message_t message;
elvin_basetypes_t type;
elvin_value_t value;
char *string;
char *newsgroups;
char *name;
char *subject;
char *mime_type;
char *mime_args;
char *attachment = NULL;
size_t length;
char *buffer = NULL;
/* If we don't have a callback than bail out now */
if (self->callback == NULL) {
return;
}
/* Get the newsgroups to which the message was posted */
if (elvin_notification_get(notification, NEWSGROUPS, &type, &value,
error) &&
type == ELVIN_STRING) {
string = value.s;
} else {
string = "news";
}
/* Prepend `usenet:' to the beginning of the group field */
length = strlen(USENET_PREFIX) + strlen(string) - 1;
newsgroups = malloc(length);
if (newsgroups == NULL) {
return;
}
snprintf(newsgroups, length, USENET_PREFIX, string);
/* Get the name from the FROM_NAME field (if provided) */
if (elvin_notification_get(notification, FROM_NAME, &type, &value,
error) &&
type == ELVIN_STRING) {
name = value.s;
} else {
/* If no FROM_NAME field then try FROM_EMAIL */
if (elvin_notification_get(notification, FROM_EMAIL, &type, &value,
error) &&
type == ELVIN_STRING) {
name = value.s;
} else {
/* If no FROM_EMAIL then try FROM */
if (elvin_notification_get(notification, FROM, &type, &value,
error) &&
type == ELVIN_STRING) {
name = value.s;
} else {
name = "anonymous";
}
}
}
/* Get the SUBJECT field (if provided) */
if (elvin_notification_get(notification, SUBJECT, &type, &value,
error) &&
type == ELVIN_STRING) {
subject = value.s;
} else {
subject = "[no subject]";
}
/* Get the MIME_ARGS field (if provided) */
if (elvin_notification_get(notification, MIME_ARGS, &type, &value,
error) &&
type == ELVIN_STRING) {
mime_args = value.s;
/* Get the MIME_TYPE field (if provided) */
if (elvin_notification_get(notification, MIME_TYPE, &type, &value,
error) &&
type == ELVIN_STRING) {
mime_type = value.s;
} else {
mime_type = URL_MIME_TYPE;
}
} else {
/* No MIME_ARGS provided. Construct one using the Message-ID
* field */
if (elvin_notification_get(notification, MESSAGE_ID, &type, &value,
error) &&
type == ELVIN_STRING) {
char *message_id = value.s;
char *news_host;
if (elvin_notification_get(notification, X_NNTP_HOST, &type,
&value, error) &&
type == ELVIN_STRING) {
news_host = value.s;
} else {
news_host = "news";
}
length = strlen(NEWS_URL) + strlen(news_host) +
strlen(message_id) - 3;
buffer = malloc(length);
if (buffer == NULL) {
mime_type = NULL;
mime_args = NULL;
} else {
snprintf(buffer, length, NEWS_URL, news_host, message_id);
mime_args = buffer;
mime_type = NEWS_MIME_TYPE;
}
} else {
mime_type = NULL;
mime_args = NULL;
}
}
/* Convert the mime type and args into an attachment */
if (mime_type == NULL || mime_args == NULL) {
attachment = NULL;
length = 0;
} else {
length = strlen(ATTACHMENT_FMT) + strlen(mime_type) + strlen(
mime_args) - 4;
attachment = malloc(length + 1);
if (attachment == NULL) {
length = 0;
} else {
snprintf(attachment, length + 1, ATTACHMENT_FMT,
mime_type, mime_args);
}
}
/* Construct a message out of all of that */
message = message_alloc(NULL, newsgroups, name, subject, 300,
attachment, length - 1,
NULL, NULL, NULL, NULL);
if (message != NULL) {
/* Deliver the message */
self->callback(self->rock, message, False);
}
/* Clean up */
free(newsgroups);
if (buffer != NULL) {
free(buffer);
}
if (attachment != NULL) {
free(attachment);
}
}
#elif ELVIN_VERSION_AT_LEAST(4, 1, -1)
/* Delivers a notification which matches the receiver's subscription
* expression */
static int
notify_cb(elvin_handle_t handle,
elvin_subscription_t subscription,
elvin_notification_t notification,
int is_secure,
void *rock,
elvin_error_t error)
{
usenet_sub_t self = (usenet_sub_t)rock;
message_t message;
char *string;
char *newsgroups;
char *name;
char *subject;
char *mime_type;
char *mime_args;
char *buffer = NULL;
char *attachment = NULL;
size_t length = 0;
int found;
/* If we don't have a callback than bail out now */
if (self->callback == NULL) {
return 1;
}
/* Get the newsgroups to which the message was posted */
if (!elvin_notification_get_string(notification, NEWSGROUPS, &found,
&string, error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
/* Use a reasonable default */
string = found ? string : "news";
/* Prepend `usenet:' to the beginning of the group field */
length = strlen(USENET_PREFIX) + strlen(string) - 1;
newsgroups = malloc(length);
if (newsgroups == NULL) {
return 0;
}
snprintf(newsgroups, length, USENET_PREFIX, string);
/* Get the name from the FROM_NAME field (if provided) */
if (!elvin_notification_get_string(notification, FROM_NAME, &found, &name,
error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
if (!found) {
/* No FROM_NAME field, so try FROM_EMAIL */
if (!elvin_notification_get_string(notification, FROM_EMAIL, &found,
&name, error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
if (!found) {
/* No FROM_EMAIL, so try FROM */
if (!elvin_notification_get_string(notification, FROM, &found,
&name, error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
if (!found) {
/* Give up */
name = "anonymous";
}
}
}
/* Get the SUBJECT field (if provided) */
if (!elvin_notification_get_string(notification, SUBJECT, &found,
&subject, error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
/* Use a default if none found */
subject = found ? subject : "[no subject]";
/* Get the MIME_ARGS field (if provided) */
if (!elvin_notification_get_string(notification, MIME_ARGS, &found,
&mime_args, error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
/* Was the MIME_ARGS field provided? */
if (found) {
/* Get the MIME_TYPE field (if provided) */
if (!elvin_notification_get_string(notification, MIME_TYPE, &found,
&mime_type, error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
/* Use a default if none found */
mime_type = found ? mime_type : URL_MIME_TYPE;
} else {
char *message_id;
/* No MIME_ARGS. Look for a message-id */
if (!elvin_notification_get_string(notification, MESSAGE_ID, &found,
&message_id, error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
if (found) {
char *news_host;
/* Look up the news host field */
if (!elvin_notification_get_string(
notification,
X_NNTP_HOST,
&found, &news_host,
error)) {
eeprintf(error, "elvin_notification_get_string failed\n");
exit(1);
}
news_host = found ? news_host : "news";
length = strlen(NEWS_URL) + strlen(news_host) +
strlen(message_id) - 3;
buffer = malloc(length);
if (buffer == NULL) {
mime_type = NULL;
mime_args = NULL;
} else {
snprintf(buffer, length, NEWS_URL, news_host, message_id);
mime_args = buffer;
mime_type = NEWS_MIME_TYPE;
}
} else {
mime_type = NULL;
mime_args = NULL;
}
}
/* Convert the mime type and args into an attachment */
if (mime_type == NULL || mime_args == NULL) {
attachment = NULL;
length = 0;
} else {
length = strlen(ATTACHMENT_FMT) - 4 + strlen(mime_type) + strlen(
mime_args);
attachment = malloc(length + 1);
if (attachment == NULL) {
length = 0;
} else {
snprintf(attachment, length + 1, ATTACHMENT_FMT,
mime_type, mime_args);
}
}
/* Construct a message out of all of that */
message = message_alloc(NULL, newsgroups, name, subject, 60,
attachment, length,
NULL, NULL, NULL, NULL);
if (message != NULL) {
/* Deliver the message */
self->callback(self->rock, message, False);
}
/* Clean up */
free(newsgroups);
if (buffer != NULL) {
free(buffer);
}
return 1;
}
#else
# error "Unsupported Elvin library version"
#endif /* ELVIN_VERSION_AT_LEAST */
/* Construct a portion of the subscription expression */
static char *
alloc_expr(usenet_sub_t self, struct usenet_expr *expression)
{
const char *field_name;
const char *format;
size_t format_length;
size_t length;
char *result;
/* Get the string representation for the field */
switch (expression->field) {
case F_BODY:
/* body -> BODY */
field_name = BODY;
break;
case F_FROM:
/* from -> FROM_NAME */
field_name = FROM_NAME;
break;
case F_EMAIL:
/* email -> FROM_EMAIL */
field_name = FROM_EMAIL;
break;
case F_SUBJECT:
/* subject -> SUBJECT */
field_name = SUBJECT;
break;
case F_KEYWORDS:
/* keywords -> KEYWORDS */
field_name = KEYWORDS;
break;
case F_XPOSTS:
/* xposts -> CROSS_POSTS */
field_name = XPOSTS;
break;
default:
/* Should never get here */
fprintf(stderr, "%s: internal error: field not handled: %u\n",
progname, expression->field);
return NULL;
}
/* Look up the string representation for the operator */
switch (expression->operator) {
case O_MATCHES:
/* matches */
format = F_MATCHES;
format_length = sizeof(F_MATCHES);
break;
case O_NOT:
/* not [matches] */
format = F_NOT_MATCHES;
format_length = sizeof(F_NOT_MATCHES);
break;
case O_EQ:
/* = */
if (expression->field == F_XPOSTS) {
format = F_EQ;
format_length = sizeof(F_EQ);
break;
}
format = F_STRING_EQ;
format_length = sizeof(F_STRING_EQ);
break;
case O_NEQ:
/* != */
if (expression->field == F_XPOSTS) {
format = F_NEQ;
format_length = sizeof(F_NEQ);
break;
}
format = F_STRING_NEQ;
format_length = sizeof(F_STRING_NEQ);
break;
case O_LT:
/* < */
format = F_LT;
format_length = sizeof(F_LT);
break;
case O_GT:
/* > */
format = F_GT;
format_length = sizeof(F_GT);
break;
case O_LE:
/* <= */
format = F_LE;
format_length = sizeof(F_LE);
break;
case O_GE:
/* >= */
format = F_GE;
format_length = sizeof(F_GE);
break;
default:
fprintf(stderr, "%s: internal error: operator %d not handled\n",
progname, expression->operator);
return NULL;
}
/* Allocate space for the result */
length = format_length + strlen(field_name) +
strlen(expression->pattern) - 3;
result = malloc(length);
if (result == NULL) {
return NULL;
}
/* Construct the string */
snprintf(result, length, format, field_name, expression->pattern);
return result;
}
/* Construct a subscription expression for the given group entry */
static char *
alloc_sub(usenet_sub_t self,
int has_not,
const char *pattern,
struct usenet_expr *expressions,
size_t count)
{
struct usenet_expr *pointer;
const char *not_string = has_not ? "!" : "";
char *result;
size_t length;
/* Shortcut -- if there are no expressions then we don't need parens */
if (count == 0) {
length = strlen(PATTERN_ONLY) + strlen(not_string) +
strlen(pattern) - 3;
result = malloc(length);
if (result == NULL) {
return NULL;
}
snprintf(result, length, PATTERN_ONLY, not_string, pattern);
return result;
}
/* Otherwise we have to do this the hard way. Allocate space for
* the initial pattern and then extend it for each of the expressions */
length = strlen(PATTERN_PLUS) + strlen(not_string) + strlen(pattern) - 3;
result = malloc(length);
if (result == NULL) {
return NULL;
}
snprintf(result, length, PATTERN_PLUS, not_string, pattern);
/* Insert the expressions */
for (pointer = expressions; pointer < expressions + count; pointer++) {
/* Get the string for the expression */
char *expr = alloc_expr(self, pointer);
size_t new_length = length + strlen(expr) + 4;
/* Expand the buffer */
result = realloc(result, new_length);
if (result == NULL) {
free(expr);
return NULL;
}
/* Overwrite the trailing right paren with the new stuff */
snprintf(result + length - 2, new_length - length + 2,
" && %s)", expr);
length = new_length;
free(expr);
}
return result;
}
/* Allocates and initializes a new usenet_sub_t */
usenet_sub_t
usenet_sub_alloc(usenet_sub_callback_t callback, void *rock)
{
usenet_sub_t self;
/* Allocate memory for the new subscription */
self = malloc(sizeof(struct usenet_sub));
if (self == NULL) {
return NULL;
}
/* Initialize its contents */
self->expression = NULL;
self->expression_size = 0;
self->handle = NULL;
self->subscription = NULL;
self->callback = callback;
self->rock = rock;
self->is_pending = 0;
return self;
}
/* Releases the resources consumed by the receiver */
void
usenet_sub_free(usenet_sub_t self)
{
/* Free the subscription expression */
if (self->expression != NULL) {
free(self->expression);
self->expression = NULL;
}
/* Don't free a pending subscription */
if (self->is_pending) {
return;
}
/* It should be safe to free the subscription now */
free(self);
}
/* Adds a new entry to the usenet subscription */
int
usenet_sub_add(usenet_sub_t self,
int has_not,
const char *pattern,
struct usenet_expr *expressions,
size_t count)
{
char *entry_sub;
size_t entry_length;
/* Figure out what the new entry is */
entry_sub = alloc_sub(self, has_not, pattern, expressions, count);
if (entry_sub == NULL) {
return -1;
}
/* And how long it is */
entry_length = strlen(entry_sub);
/* If we had no previous expression, then simply create one */
if (self->expression == NULL) {
self->expression_size = strlen(ONE_SUB) + entry_length - 1;
self->expression = malloc(self->expression_size);
if (self->expression == NULL) {
free(entry_sub);
return -1;
}
snprintf(self->expression, self->expression_size, ONE_SUB, entry_sub);
} else {
/* We need to extend the existing expression */
size_t new_size;
char *new_expression;
/* Expand the expression buffer */
new_size = self->expression_size + entry_length + 4;
new_expression = realloc(self->expression, new_size);
if (new_expression == NULL) {
return -1;
}
/* Overwrite the trailing right paren with the new stuff */
snprintf(new_expression + self->expression_size - 2,
new_size - self->expression_size + 2,
" || %s)", entry_sub);
self->expression = new_expression;
self->expression_size = new_size;
}
/* If we're connected then resubscribe */
if (self->handle != NULL) {
fprintf(stderr, "%s: hmmm\n", progname);
abort();
}
/* Clean up */
free(entry_sub);
return 0;
}
/* Callback for a subscribe request */
static
#if !defined(ELVIN_VERSION_AT_LEAST)
void
#elif ELVIN_VERSION_AT_LEAST(4, 1, -1)
int
#endif
subscribe_cb(elvin_handle_t handle,
int result,
elvin_subscription_t subscription,
void *rock,
elvin_error_t error)
{
usenet_sub_t self = (usenet_sub_t)rock;
self->subscription = subscription;
self->is_pending = 0;
/* Unsubscribe if we were pending when we were freed */
if (self->expression == NULL) {
usenet_sub_set_connection(self, NULL, error);
}
return ELVIN_RETURN_SUCCESS;
}
/* Callback for an unsubscribe request */
static
#if !defined(ELVIN_VERSION_AT_LEAST)
void
#elif ELVIN_VERSION_AT_LEAST(4, 1, -1)
int
#endif
unsubscribe_cb(elvin_handle_t handle,
int result,
elvin_subscription_t subscription,
void *rock,
elvin_error_t error)
{
usenet_sub_t self = (usenet_sub_t)rock;
self->is_pending = 0;
/* Free the receiver if it was pending when it was freed */
if (self->expression == NULL) {
usenet_sub_free(self);
}
return ELVIN_RETURN_SUCCESS;
}
/* Sets the receiver's elvin connection */
void
usenet_sub_set_connection(usenet_sub_t self,
elvin_handle_t handle,
elvin_error_t error)
{
/* Disconnect from the old connection */
if (self->handle != NULL && self->subscription != NULL) {
if (elvin_async_delete_subscription(self->handle,
self->subscription,
unsubscribe_cb, self,
error) == 0) {
fprintf(stderr, "elvin_async_delete_subscription(): failed\n");
exit(1);
}
self->is_pending = 1;
}
/* Connect to the new one */
self->handle = handle;
if (self->handle != NULL && self->expression != NULL) {
if (elvin_async_add_subscription(self->handle,
self->expression, NULL, 1,
notify_cb, self,
subscribe_cb, self,
error) == 0) {
fprintf(stderr, "elvin_async_add_subscription(): failed\n");
exit(1);
}
self->is_pending = 1;
}
}
/**********************************************************************/