-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patho-filehost.c
More file actions
849 lines (709 loc) · 22.2 KB
/
o-filehost.c
File metadata and controls
849 lines (709 loc) · 22.2 KB
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
/*
* o-filehost - ObsidianIRC Module
* (C) Copyright 2025
*
* This module detects URLs in channel messages and provides
* a preview of the link content (title and snippet) via message tags.
* Includes IRCv3 draft/FILEHOST support for image caching.
*
* License: GPLv3
*/
/*** <<<MODULE MANAGER START>>>
module
{
documentation "https://github.com/ObsidianIRC/UnrealIRCd-Modules/blob/main/README.md";
troubleshooting "In case of problems, check documentation or contact the author";
min-unrealircd-version "6.1.3";
max-unrealircd-version "6.*";
post-install-text {
"The module is installed. Now all you need to do is add a loadmodule line:";
"loadmodule \"third/o-filehost\";";
"And configure filehosts in your unrealircd.conf:";
"filehosts { host \"http://your-filehost.com\"; };";
"And /REHASH the IRCd.";
}
}
*** <<<MODULE MANAGER END>>>
*/
#include "unrealircd.h"
#define CONF_FILEHOST "filehosts"
/* FILEHOST config struct */
struct
{
char *isupport_line;
MultiLine *hosts;
unsigned short int has_hosts;
} cfg;
ModuleHeader MOD_HEADER = {
"third/o-filehost",
"1.0",
"Provides link previews via message tags and IRCv3 draft/FILEHOST support",
"ObsidianIRC Team",
"unrealircd-6",
};
/* Maximum sizes for safety */
#define MAX_DOWNLOAD_SIZE 1048576 /* 1MB */
#define MAX_TITLE_LENGTH 500
#define MAX_SNIPPET_LENGTH 500
#define MAX_META_LENGTH 2048
#define MAX_URL_LENGTH 2048
/* Structure to hold context for async callback */
typedef struct {
char *channel;
char *msgid;
char *url;
} LinkPreviewContext;
/* Structure to hold context for image upload callback */
typedef struct {
char *channel;
char *msgid;
char *title;
char *snippet;
} ImageUploadContext;
/* Function prototypes */
void link_preview_download_complete(OutgoingWebRequest *request, OutgoingWebResponse *response);
void image_upload_complete(OutgoingWebRequest *request, OutgoingWebResponse *response);
int link_preview_chanmsg(Client *client, Channel *channel, int sendflags, const char *member_modes, const char *target, MessageTag *mtags, const char *text, SendType sendtype);
char *extract_url_from_message(const char *text);
char *extract_title_from_html(const char *html);
char *extract_snippet_from_html(const char *html);
char *extract_meta_image_from_html(const char *html);
void send_link_preview(const char *channel, const char *msgid, const char *title, const char *snippet, const char *meta_image);
int link_preview_mtag_is_ok(Client *client, const char *name, const char *value);
int filehost_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs);
int filehost_configrun(ConfigFile *cf, ConfigEntry *ce, int type);
void setconf(void);
void freeconf(void);
/* Module test */
MOD_TEST()
{
setconf();
HookAdd(modinfo->handle, HOOKTYPE_CONFIGTEST, 0, filehost_configtest);
return MOD_SUCCESS;
}
/* Module init */
MOD_INIT()
{
MessageTagHandlerInfo mtag;
RegisterApiCallbackWebResponse(modinfo->handle, "link_preview_download_complete", link_preview_download_complete);
RegisterApiCallbackWebResponse(modinfo->handle, "image_upload_complete", image_upload_complete);
HookAdd(modinfo->handle, HOOKTYPE_CHANMSG, 0, link_preview_chanmsg);
HookAdd(modinfo->handle, HOOKTYPE_CONFIGRUN, 0, filehost_configrun);
/* Register our custom message tags */
memset(&mtag, 0, sizeof(mtag));
mtag.name = "+reply";
mtag.is_ok = link_preview_mtag_is_ok;
mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED;
MessageTagHandlerAdd(modinfo->handle, &mtag);
memset(&mtag, 0, sizeof(mtag));
mtag.name = "obsidianirc/link-preview-title";
mtag.is_ok = link_preview_mtag_is_ok;
mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED;
MessageTagHandlerAdd(modinfo->handle, &mtag);
memset(&mtag, 0, sizeof(mtag));
mtag.name = "obsidianirc/link-preview-snippet";
mtag.is_ok = link_preview_mtag_is_ok;
mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED;
MessageTagHandlerAdd(modinfo->handle, &mtag);
memset(&mtag, 0, sizeof(mtag));
mtag.name = "obsidianirc/link-preview-meta";
mtag.is_ok = link_preview_mtag_is_ok;
mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED;
MessageTagHandlerAdd(modinfo->handle, &mtag);
return MOD_SUCCESS;
}
MOD_LOAD()
{
if (cfg.has_hosts)
{
ISupport *is;
if (!(is = ISupportAdd(modinfo->handle, "FILEHOST", cfg.isupport_line)))
return MOD_FAILED;
}
return MOD_SUCCESS;
}
MOD_UNLOAD()
{
freeconf();
return MOD_SUCCESS;
}
/**
* Hook function called when a message is sent to a channel
*/
int link_preview_chanmsg(Client *client, Channel *channel, int sendflags, const char *member_modes, const char *target, MessageTag *mtags, const char *text, SendType sendtype)
{
char *url;
const char *msgid = NULL;
MessageTag *mtag;
LinkPreviewContext *context;
OutgoingWebRequest *request;
/* Only process PRIVMSG, not NOTICE or TAGMSG */
if (sendtype != SEND_TYPE_PRIVMSG)
{
return 0;
}
/* Don't process messages from servers or ulines */
if (IsServer(client) || IsULine(client))
{
return 0;
}
/* Extract URL from message */
url = extract_url_from_message(text);
if (!url)
{
return 0; /* No URL found */
}
/* Find the msgid tag from the message */
mtag = find_mtag(mtags, "msgid");
if (mtag)
msgid = mtag->value;
/* If no msgid, we can't send a reply, so skip */
if (!msgid)
{
safe_free(url);
return 0;
}
/* Create context for the async callback */
context = safe_alloc(sizeof(LinkPreviewContext));
safe_strdup(context->channel, channel->name);
safe_strdup(context->msgid, msgid);
context->url = url; /* Transfer ownership */
/* Start async web request */
request = safe_alloc(sizeof(OutgoingWebRequest));
safe_strdup(request->url, url);
request->http_method = HTTP_METHOD_GET;
safe_strdup(request->apicallback, "link_preview_download_complete");
request->max_redirects = 3;
request->callback_data = context;
add_nvplist(&request->headers, 0, "User-Agent", "UnrealIRCd-LinkPreview/1.0");
url_start_async(request);
return 0; /* Don't modify the message */
}
/**
* Callback when web request completes
*/
void link_preview_download_complete(OutgoingWebRequest *request, OutgoingWebResponse *response)
{
LinkPreviewContext *context = (LinkPreviewContext *)request->callback_data;
char *title = NULL;
char *snippet = NULL;
char *meta_image = NULL;
if (!context)
{
return;
}
/* Check for errors */
if (response->errorbuf || !response->memory)
{
unreal_log(ULOG_DEBUG, "o-filehost", "DOWNLOAD_ERROR", NULL,
"Error downloading $url: $error",
log_data_string("url", context->url),
log_data_string("error", response->errorbuf ? response->errorbuf : "No data"));
goto cleanup;
}
/* Safety check: limit download size */
if (response->memory_len > MAX_DOWNLOAD_SIZE)
{
unreal_log(ULOG_DEBUG, "o-filehost", "DOWNLOAD_TOO_LARGE", NULL,
"Download from $url exceeded size limit ($size bytes)",
log_data_string("url", context->url),
log_data_integer("size", response->memory_len));
goto cleanup;
}
/* Extract title and snippet from HTML */
title = extract_title_from_html(response->memory);
snippet = extract_snippet_from_html(response->memory);
meta_image = extract_meta_image_from_html(response->memory);
/* Only send if we got at least a title */
if (title && *title)
{
/* If we have a meta image, upload it to configured filehost first */
if (meta_image && *meta_image && cfg.has_hosts && cfg.hosts)
{
ImageUploadContext *upload_ctx;
OutgoingWebRequest *upload_req;
char *json_payload;
char upload_url[512];
snprintf(upload_url, sizeof(upload_url), "%s/upload", cfg.hosts->line);
/* Create context for image upload callback */
upload_ctx = safe_alloc(sizeof(ImageUploadContext));
safe_strdup(upload_ctx->channel, context->channel);
safe_strdup(upload_ctx->msgid, context->msgid);
safe_strdup(upload_ctx->title, title);
safe_strdup(upload_ctx->snippet, snippet ? snippet : "");
/* Build JSON payload: {"url": "image_url"} */
json_payload = safe_alloc(strlen(meta_image) + 50);
snprintf(json_payload, strlen(meta_image) + 50, "{\"url\":\"%s\"}", meta_image);
/* Start async upload request */
upload_req = safe_alloc(sizeof(OutgoingWebRequest));
safe_strdup(upload_req->url, upload_url);
upload_req->http_method = HTTP_METHOD_POST;
safe_strdup(upload_req->apicallback, "image_upload_complete");
upload_req->callback_data = upload_ctx;
safe_strdup(upload_req->body, json_payload);
add_nvplist(&upload_req->headers, 0, "Content-Type", "application/json");
add_nvplist(&upload_req->headers, 0, "User-Agent", "UnrealIRCd-LinkPreview/1.0");
url_start_async(upload_req);
safe_free(json_payload);
}
else
{
if (meta_image && *meta_image && !cfg.has_hosts)
{
}
/* No image or no filehost configured, send preview directly */
send_link_preview(context->channel, context->msgid, title, snippet, meta_image && *meta_image ? meta_image : NULL);
}
}
else
{
}
cleanup:
safe_free(title);
safe_free(snippet);
safe_free(meta_image);
safe_free(context->channel);
safe_free(context->msgid);
safe_free(context->url);
safe_free(context);
}
/**
* Callback when image upload to configured filehost completes
*/
void image_upload_complete(OutgoingWebRequest *request, OutgoingWebResponse *response)
{
ImageUploadContext *context = (ImageUploadContext *)request->callback_data;
json_t *result;
json_error_t jerr;
const char *saved_url = NULL;
if (!context)
{
return;
}
/* Check for errors */
if (response->errorbuf || !response->memory)
{
/* Send preview without image */
send_link_preview(context->channel, context->msgid, context->title, context->snippet, NULL);
goto cleanup;
}
/* Parse JSON response to extract saved_url */
result = json_loads(response->memory, JSON_REJECT_DUPLICATES, &jerr);
if (!result)
{
/* Send preview without image */
send_link_preview(context->channel, context->msgid, context->title, context->snippet, NULL);
goto cleanup;
}
/* Extract saved_url from JSON */
json_t *saved_url_obj = json_object_get(result, "saved_url");
if (saved_url_obj && json_is_string(saved_url_obj))
{
saved_url = json_string_value(saved_url_obj);
/* Send preview with local image URL */
send_link_preview(context->channel, context->msgid, context->title, context->snippet, saved_url);
}
else
{
/* Send preview without image */
send_link_preview(context->channel, context->msgid, context->title, context->snippet, NULL);
}
json_decref(result);
cleanup:
safe_free(context->channel);
safe_free(context->msgid);
safe_free(context->title);
safe_free(context->snippet);
safe_free(context);
}
/**
* Extract first URL from message text
*/
char *extract_url_from_message(const char *text)
{
pcre2_code *re;
pcre2_match_data *match_data;
PCRE2_SPTR pattern = (PCRE2_SPTR)"https?://[^\\s<>\"]+";
PCRE2_SPTR subject = (PCRE2_SPTR)text;
int errornumber;
PCRE2_SIZE erroroffset;
int rc;
char *result = NULL;
/* Compile regex */
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, NULL);
if (!re)
return NULL;
/* Match */
match_data = pcre2_match_data_create_from_pattern(re, NULL);
rc = pcre2_match(re, subject, strlen(text), 0, 0, match_data, NULL);
if (rc > 0)
{
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
size_t url_len = ovector[1] - ovector[0];
/* Limit URL length for safety */
if (url_len > MAX_URL_LENGTH)
url_len = MAX_URL_LENGTH;
result = safe_alloc(url_len + 1);
memcpy(result, text + ovector[0], url_len);
result[url_len] = '\0';
}
pcre2_match_data_free(match_data);
pcre2_code_free(re);
return result;
}
/**
* Extract title from HTML using regex
*/
char *extract_title_from_html(const char *html)
{
pcre2_code *re;
pcre2_match_data *match_data;
PCRE2_SPTR pattern = (PCRE2_SPTR)"<title[^>]*>([^<]+)</title>";
PCRE2_SPTR subject = (PCRE2_SPTR)html;
int errornumber;
PCRE2_SIZE erroroffset;
int rc;
char *result = NULL;
/* Compile regex (case insensitive) */
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
if (!re)
return NULL;
/* Match */
match_data = pcre2_match_data_create_from_pattern(re, NULL);
rc = pcre2_match(re, subject, strlen(html), 0, 0, match_data, NULL);
if (rc > 1)
{
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
size_t title_len = ovector[3] - ovector[2];
/* Limit title length */
if (title_len > MAX_TITLE_LENGTH)
title_len = MAX_TITLE_LENGTH;
result = safe_alloc(title_len + 1);
memcpy(result, html + ovector[2], title_len);
result[title_len] = '\0';
/* Trim whitespace */
char *p = result;
while (*p && isspace(*p)) p++;
if (p != result)
memmove(result, p, strlen(p) + 1);
p = result + strlen(result) - 1;
while (p > result && isspace(*p))
*p-- = '\0';
}
pcre2_match_data_free(match_data);
pcre2_code_free(re);
return result;
}
/**
* Extract description/snippet from HTML meta tags or first paragraph
*/
char *extract_snippet_from_html(const char *html)
{
pcre2_code *re;
pcre2_match_data *match_data;
PCRE2_SPTR pattern;
PCRE2_SPTR subject = (PCRE2_SPTR)html;
int errornumber;
PCRE2_SIZE erroroffset;
int rc;
char *result = NULL;
/* Try meta description first */
pattern = (PCRE2_SPTR)"<meta[^>]+name=[\"']description[\"'][^>]+content=[\"']([^\"']+)[\"']";
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
if (!re)
{
/* Try alternative format */
pattern = (PCRE2_SPTR)"<meta[^>]+content=[\"']([^\"']+)[\"'][^>]+name=[\"']description[\"']";
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
}
if (re)
{
match_data = pcre2_match_data_create_from_pattern(re, NULL);
rc = pcre2_match(re, subject, strlen(html), 0, 0, match_data, NULL);
if (rc > 1)
{
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
size_t snippet_len = ovector[3] - ovector[2];
/* Limit snippet length */
if (snippet_len > MAX_SNIPPET_LENGTH)
snippet_len = MAX_SNIPPET_LENGTH;
result = safe_alloc(snippet_len + 1);
memcpy(result, html + ovector[2], snippet_len);
result[snippet_len] = '\0';
/* Trim whitespace */
char *p = result;
while (*p && isspace(*p)) p++;
if (p != result)
memmove(result, p, strlen(p) + 1);
p = result + strlen(result) - 1;
while (p > result && isspace(*p))
*p-- = '\0';
}
pcre2_match_data_free(match_data);
pcre2_code_free(re);
}
/* If no meta description, try Open Graph description */
if (!result)
{
pattern = (PCRE2_SPTR)"<meta[^>]+property=[\"']og:description[\"'][^>]+content=[\"']([^\"']+)[\"']";
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
if (re)
{
match_data = pcre2_match_data_create_from_pattern(re, NULL);
rc = pcre2_match(re, subject, strlen(html), 0, 0, match_data, NULL);
if (rc > 1)
{
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
size_t snippet_len = ovector[3] - ovector[2];
if (snippet_len > MAX_SNIPPET_LENGTH)
snippet_len = MAX_SNIPPET_LENGTH;
result = safe_alloc(snippet_len + 1);
memcpy(result, html + ovector[2], snippet_len);
result[snippet_len] = '\0';
/* Trim whitespace */
char *p = result;
while (*p && isspace(*p)) p++;
if (p != result)
memmove(result, p, strlen(p) + 1);
p = result + strlen(result) - 1;
while (p > result && isspace(*p))
*p-- = '\0';
}
pcre2_match_data_free(match_data);
pcre2_code_free(re);
}
}
return result;
}
/**
* Extract meta image from HTML (Open Graph or Twitter Card)
*/
char *extract_meta_image_from_html(const char *html)
{
pcre2_code *re;
pcre2_match_data *match_data;
PCRE2_SPTR pattern;
PCRE2_SPTR subject = (PCRE2_SPTR)html;
int errornumber;
PCRE2_SIZE erroroffset;
int rc;
char *result = NULL;
/* Try Open Graph image first (og:image) */
pattern = (PCRE2_SPTR)"<meta[^>]+property=[\"']og:image[\"'][^>]+content=[\"']([^\"']+)[\"']";
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
if (!re)
{
/* Try alternative format */
pattern = (PCRE2_SPTR)"<meta[^>]+content=[\"']([^\"']+)[\"'][^>]+property=[\"']og:image[\"']";
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
}
if (re)
{
match_data = pcre2_match_data_create_from_pattern(re, NULL);
rc = pcre2_match(re, subject, strlen(html), 0, 0, match_data, NULL);
if (rc > 1)
{
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
size_t image_len = ovector[3] - ovector[2];
/* Limit image URL length */
if (image_len > MAX_META_LENGTH)
image_len = MAX_META_LENGTH;
result = safe_alloc(image_len + 1);
memcpy(result, html + ovector[2], image_len);
result[image_len] = '\0';
/* Trim whitespace */
char *p = result;
while (*p && isspace(*p)) p++;
if (p != result)
memmove(result, p, strlen(p) + 1);
p = result + strlen(result) - 1;
while (p > result && isspace(*p))
*p-- = '\0';
}
pcre2_match_data_free(match_data);
pcre2_code_free(re);
}
/* If no og:image, try Twitter Card image (twitter:image or twitter:image:src) */
if (!result)
{
pattern = (PCRE2_SPTR)"<meta[^>]+name=[\"']twitter:image(:src)?[\"'][^>]+content=[\"']([^\"']+)[\"']";
re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &errornumber, &erroroffset, NULL);
if (re)
{
match_data = pcre2_match_data_create_from_pattern(re, NULL);
rc = pcre2_match(re, subject, strlen(html), 0, 0, match_data, NULL);
if (rc > 1)
{
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
/* The captured group for the URL will be at index 2 (group 1 is :src optional) */
size_t image_len;
int url_group = (rc > 2) ? 5 : 3; /* If optional :src matched, URL is in group 2, otherwise group 1 */
image_len = ovector[url_group] - ovector[url_group - 1];
if (image_len > MAX_META_LENGTH)
image_len = MAX_META_LENGTH;
result = safe_alloc(image_len + 1);
memcpy(result, html + ovector[url_group - 1], image_len);
result[image_len] = '\0';
/* Trim whitespace */
char *p = result;
while (*p && isspace(*p)) p++;
if (p != result)
memmove(result, p, strlen(p) + 1);
p = result + strlen(result) - 1;
while (p > result && isspace(*p))
*p-- = '\0';
}
pcre2_match_data_free(match_data);
pcre2_code_free(re);
}
}
return result;
}
/**
* Validate our custom message tags
*/
int link_preview_mtag_is_ok(Client *client, const char *name, const char *value)
{
/* Only allow from servers (our module) */
if (IsServer(client) || IsMe(client))
return 1;
return 0;
}
/**
* Send TAGMSG with link preview to channel
*/
void send_link_preview(const char *channel, const char *msgid, const char *title, const char *snippet, const char *meta_image)
{
char truncated_title[MAX_TITLE_LENGTH + 1];
char truncated_snippet[MAX_SNIPPET_LENGTH + 1];
char truncated_meta[MAX_META_LENGTH + 1];
MessageTag *mtags = NULL;
MessageTag *m;
Channel *chan;
Member *member;
chan = find_channel(channel);
if (!chan)
{
return;
}
/* Truncate title, snippet, and meta to max lengths (UnrealIRCd will handle escaping) */
strlcpy(truncated_title, title, sizeof(truncated_title));
if (snippet && *snippet)
strlcpy(truncated_snippet, snippet, sizeof(truncated_snippet));
else
truncated_snippet[0] = '\0';
if (meta_image && *meta_image)
strlcpy(truncated_meta, meta_image, sizeof(truncated_meta));
else
truncated_meta[0] = '\0';
/* Add +reply tag */
m = safe_alloc(sizeof(MessageTag));
safe_strdup(m->name, "+reply");
safe_strdup(m->value, msgid);
AddListItem(m, mtags);
/* Add title tag */
m = safe_alloc(sizeof(MessageTag));
safe_strdup(m->name, "obsidianirc/link-preview-title");
safe_strdup(m->value, truncated_title);
AddListItem(m, mtags);
/* Add snippet tag if available */
if (truncated_snippet[0])
{
m = safe_alloc(sizeof(MessageTag));
safe_strdup(m->name, "obsidianirc/link-preview-snippet");
safe_strdup(m->value, truncated_snippet);
AddListItem(m, mtags);
}
/* Add meta tag if available */
if (truncated_meta[0])
{
m = safe_alloc(sizeof(MessageTag));
safe_strdup(m->name, "obsidianirc/link-preview-meta");
safe_strdup(m->value, truncated_meta);
AddListItem(m, mtags);
}
/* Use new_message_special to add standard tags (msgid, time) and prepare for sending */
new_message_special(&me, mtags, &mtags, ":%s TAGMSG %s", me.name, channel);
/* Send TAGMSG to all channel members */
for (member = chan->members; member; member = member->next)
{
Client *acptr = member->client;
if (MyConnect(acptr))
{
sendto_one(acptr, mtags, ":%s TAGMSG %s", me.name, channel);
}
}
free_message_tags(mtags);
}
void setconf(void)
{
memset(&cfg, 0, sizeof(cfg));
cfg.has_hosts = 0;
safe_strdup(cfg.isupport_line,"");
}
void freeconf(void)
{
freemultiline(cfg.hosts);
cfg.has_hosts = 0;
safe_free(cfg.isupport_line);
memset(&cfg, 0, sizeof(cfg));
}
int filehost_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs)
{
int errors = 0;
ConfigEntry *cep;
if (type != CONFIG_MAIN)
return 0;
if (!ce || !ce->name)
return 0;
if (strcmp(ce->name, CONF_FILEHOST))
return 0;
for (cep = ce->items; cep; cep = cep->next)
{
if (!cep->name)
{
config_error("%s:%i: blank %s item", cep->file->filename, cep->line_number, CONF_FILEHOST);
++errors;
continue;
}
if (!strcasecmp(cep->name, "host"))
{
if (!BadPtr(cep->value))
cfg.has_hosts = 1;
else
config_error("%s:%i: Empty host at %s::%s", cep->file->filename, cep->line_number, CONF_FILEHOST, cep->name);
continue;
}
// Anything else is unknown to us =]
config_warn("%s:%i: unknown item %s::%s", cep->file->filename, cep->line_number, CONF_FILEHOST, cep->name); // So display just a warning
}
*errs = errors;
return errors ? -1 : 1;
}
int filehost_configrun(ConfigFile *cf, ConfigEntry *ce, int type)
{
ConfigEntry *cep;
char buf[BUFSIZE] = "\0";
if (type != CONFIG_MAIN)
return 0;
if (!ce || !ce->name)
return 0;
if (strcmp(ce->name, "filehosts"))
return 0;
for (cep = ce->items; cep; cep = cep->next)
{
if (!cep->name)
continue;
if (!strcmp(cep->name, "host"))
addmultiline(&cfg.hosts, cep->value);
}
for (MultiLine *m = cfg.hosts; m; m = m->next)
{
strlcat(buf,m->line, sizeof(buf));
if (m->next)
strlcat(buf,"\\x20",sizeof(buf));
}
if (strlen(buf))
safe_strdup(cfg.isupport_line, buf);
return 1; // We good
}