forked from amyreese/znc-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.cpp
executable file
·2129 lines (1841 loc) · 50.6 KB
/
push.cpp
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
/**
* ZNC Push Module
*
* Allows the user to enter a Push user and API token, and sends
* channel highlights and personal messages to Push.
*
* Copyright (c) 2011 John Reese
* Licensed under the MIT license
*/
#define REQUIRESSL
#ifndef PUSHVERSION
#define PUSHVERSION "dev"
#endif
#include <znc/znc.h>
#include <znc/Chan.h>
#include <znc/User.h>
#include <znc/IRCNetwork.h>
#include <znc/Modules.h>
#include <znc/FileUtils.h>
#include <znc/Client.h>
#include "time.h"
#include <string.h>
#ifdef USE_CURL
#include <curl/curl.h>
#endif // USE_CURL
// Forward declaration
class CPushMod;
/**
* Shorthand for encoding a string for a URL.
*
* @param str String to be encoded
* @return Encoded string
*/
CString urlencode(const CString& str)
{
return str.Escape_n(CString::EASCII, CString::EURL);
}
#ifndef USE_CURL
/**
* Socket class for generating HTTP requests.
*/
class CPushSocket : public CSocket
{
public:
CPushSocket(CModule *p) : CSocket(p)
{
EnableReadLine();
parent = (CPushMod*) p;
first = true;
crlf = "\r\n";
user_agent = "ZNC Push/" + CString(PUSHVERSION);
}
// Implemented after CPushMod
void Request(bool post, const CString& host, const CString& url, MCString& parameters, const CString& auth="");
virtual void ReadLine(const CString& data);
virtual void Disconnected();
private:
CPushMod *parent;
bool first;
// Too lazy to add CString("\r\n\") everywhere
CString crlf;
// User agent to use
CString user_agent;
};
#else
// forward declaration
long make_curl_request(const CString& service_host, const CString& service_url,
const CString& service_auth, MCString& params, int port,
bool use_ssl, bool use_post,
const CString& proxy, bool proxy_ssl_verify,
bool debug);
#endif // USE_CURL
/**
* Push notification module.
*/
class CPushMod : public CModule
{
protected:
// Application name
CString app;
// Time last notification was sent for a given context
std::map <CString, time_t> last_notification_time;
// Time of last message by user to a given context
std::map <CString, time_t> last_reply_time;
// Time of last activity by user for a given context
std::map <CString, time_t> last_active_time;
// Time of last activity by user in any context
time_t idle_time;
// User object
CUser *user;
// Configuration options
MCString options;
MCString defaults;
public:
MODCONSTRUCTOR(CPushMod) {
#ifdef USE_CURL
curl_global_init(CURL_GLOBAL_DEFAULT);
#endif
app = "ZNC";
idle_time = time(NULL);
// Current user
user = GetUser();
// Push service information
defaults["service"] = "";
defaults["username"] = "";
defaults["secret"] = "";
defaults["target"] = "";
defaults["extra_target"] = "";
// Notification settings
defaults["message_content"] = "{context}: [{nick}] {message}";
defaults["message_length"] = "100";
defaults["message_title"] = "{title}";
defaults["message_uri"] = "";
defaults["message_uri_post"] = "no";
defaults["message_uri_title"] = "";
defaults["message_priority"] = "0";
defaults["message_sound"] = "";
defaults["message_escape"] = "";
// Notification conditions
defaults["away_only"] = "no";
defaults["client_count_less_than"] = "0";
defaults["highlight"] = "";
defaults["idle"] = "0";
defaults["last_active"] = "180";
defaults["last_notification"] = "300";
defaults["nick_blacklist"] = "";
defaults["network_blacklist"] = "";
defaults["replied"] = "yes";
defaults["context"] = "*";
// Proxy, for libcurl
defaults["proxy"] = "";
defaults["proxy_ssl_verify"] = "yes";
// Advanced
defaults["channel_conditions"] = "all";
defaults["query_conditions"] = "all";
defaults["debug"] = "off";
}
virtual ~CPushMod() {
#ifdef USE_CURL
curl_global_cleanup();
#endif
}
public:
/**
* Debugging messages. Prints to *push when the debug option is enabled.
*
* @param data Debug message
*/
void PutDebug(const CString& data)
{
if (options["debug"] == "on")
{
PutModule(data);
}
}
protected:
/**
* Performs string expansion on a set of keywords.
* Given an initial string and a dictionary of string replacments,
* iterate over the dictionary, expanding keywords one-by-one.
*
* @param content String contents
* @param replace Dictionary of string replacements
* @return Result of string replacements
*/
CString expand(const CString& content, MCString& replace)
{
CString result = content.c_str();
for(MCString::iterator i = replace.begin(); i != replace.end(); i++)
{
result.Replace(i->first, i->second);
}
return result;
}
/**
* Verifies whether a given string contains only numbers.
*
* @param content String to verify
*/
bool is_number(const CString& content)
{
CString::const_iterator it = content.begin();
while(it != content.end() && std::isdigit(*it)) ++it;
return !content.empty() && it == content.end();
}
/**
* Send a message to the currently-configured push service.
* Requires (and assumes) that the user has already configured their
* username and API secret using the 'set' command.
*
* @param message Message to be sent to the user
* @param title Message title to use
* @param context Channel or nick context
*/
void send_message(const CString& message, const CString& title="New Message", const CString& context="*push", const CNick& nick=CString("*push"), const CString& type="")
{
// Set the last notification time
last_notification_time[context] = time(NULL);
// Shorten message if needed
unsigned int message_length = options["message_length"].ToUInt();
CString short_message = message;
if (message_length > 0)
{
short_message = message.Ellipsize(message_length);
}
// Generate an ISO8601 date string
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char iso8601 [20];
strftime(iso8601, 20, "%Y-%m-%d %H:%M:%S", timeinfo);
// Message string replacements
MCString replace;
replace["{context}"] = context;
replace["{nick}"] = nick.GetNickMask();
replace["{datetime}"] = CString(iso8601);
replace["{unixtime}"] = CString(time(NULL));
replace["{message}"] = short_message;
replace["{title}"] = title;
replace["{username}"] = options["username"];
replace["{secret}"] = options["secret"];
replace["{target}"] = options["target"];
replace["{extra_target}"] = options["extra_target"];
// network is special because it can be nullptr if the user has none set up yet
CIRCNetwork* network = GetNetwork();
if (network) {
replace["{network}"] = network->GetName();
} else {
replace["{network}"] = "(No network)";
}
if (options["message_escape"] != "")
{
CString::EEscape esc = CString::ToEscape(options["message_escape"]);
for (MCString::iterator i = replace.begin(); i != replace.end(); i++) {
i->second = i->second.Escape(esc);
}
}
CString message_uri = expand(options["message_uri"], replace);
CString message_title = expand(options["message_title"], replace);
CString message_content = expand(options["message_content"], replace);
// Set up the connection profile
CString service = options["service"];
bool use_post = true;
int use_port = 443;
bool use_ssl = true;
CString service_host;
CString service_url;
CString service_auth;
MCString params;
// Service-specific profiles
if (service == "pushbullet")
{
if (options["secret"] == "")
{
PutModule("Error: secret (api key) not set");
return;
}
service_host = "api.pushbullet.com";
service_url = "/v2/pushes";
// BASIC auth, base64-encoded APIKey:
service_auth = options["secret"] + CString(":");
if (options["target"] != "")
{
params["device_iden"] = options["target"];
}
if (message_uri == "")
{
params["type"] = "note";
} else {
params["type"] = "link";
params["url"] = message_uri;
}
params["title"] = message_title;
params["body"] = message_content;
}
else if (service == "boxcar")
{
if (options["username"] == "")
{
PutModule("Error: username not set");
return;
}
CString boxcar_api_key = "puSd2qp2gCDZO7nWkvb9";
CString boxcar_api_secret = "wLQQKSyGybIOkggbiKipefeYGLni9B3FPZabopHp";
service_host = "boxcar.io";
service_url = "/devices/providers/" + boxcar_api_key + "/notifications";
params["email"] = options["username"];
params["notification[from_screen_name]"] = context;
params["notification[message]"] = message_content;
params["notification[source_url]"] = message_uri;
}
else if (service == "boxcar2")
{
if (options["secret"] == "")
{
PutModule("Error: secret not set to apikey");
return;
}
service_host = "new.boxcar.io";
service_url = "/api/notifications";
params["user_credentials"] = options["secret"];
params["notification[title]"] = message_title;
params["notification[long_message]"] = message_content;
if ( options["message_sound"] != "" )
{
params["notification[sound]"] = options["message_sound"];
}
}
else if (service == "pushover")
{
if (options["username"] == "")
{
PutModule("Error: username (user key) not set");
return;
}
if (options["secret"] == "")
{
PutModule("Error: secret (application token/key) not set");
return;
}
service_host = "api.pushover.net";
service_url = "/1/messages.json";
params["token"] = options["secret"];
params["user"] = options["username"];
params["title"] = message_title;
params["message"] = message_content;
if (message_uri != "")
{
params["url"] = message_uri;
}
if ( options["message_uri_title"] != "" )
{
params["url_title"] = options["message_uri_title"];
}
if (options["target"] != "")
{
params["device"] = options["target"];
}
if ( options["message_sound"] != "" )
{
params["sound"] = options["message_sound"];
}
if (options["message_priority"] != "")
{
params["priority"] = options["message_priority"];
}
}
else if (service == "pushsafer")
{
if (options["secret"] == "")
{
PutModule("Error: privatekey (private or alias key) not set");
return;
}
service_host = "pushsafer.com";
service_url = "/api";
params["k"] = options["secret"];
params["t"] = message_title;
params["m"] = message_content;
if (message_uri != "")
{
params["u"] = message_uri;
}
if (options["message_uri_title"] != "" )
{
params["ut"] = options["message_uri_title"];
}
if (options["target"] != "")
{
params["d"] = options["target"];
}
if (options["message_sound"] != "" )
{
params["s"] = options["message_sound"];
}
}
else if (service == "pushalot")
{
if (options["secret"] == "")
{
PutModule("Error: secret (authorization token) not set");
return;
}
service_host = "pushalot.com";
service_url = "/api/sendmessage";
params["AuthorizationToken"] = options["secret"];
params["Title"] = message_title;
params["Body"] = message_content;
if (message_uri != "")
{
params["Link"] = message_uri;
}
if (options["message_uri_title"] != "" )
{
params["LinkTitle"] = options["message_uri_title"];
}
}
else if (service == "prowl")
{
if (options["secret"] == "")
{
PutModule("Error: secret not set");
return;
}
service_host = "api.prowlapp.com";
service_url = "/publicapi/add";
params["apikey"] = options["secret"];
params["application"] = app;
params["event"] = message_title;
params["description"] = message_content;
params["url"] = message_uri;
}
else if (service == "igloo")
{
if (options["target"] == "")
{
PutModule("Error: no devices are set");
return;
}
service_host = "api.iglooirc.com";
service_url = "/znc/push";
if (network != NULL)
{
params["network"] = network->GetName();
params["nick"] = network->GetNick();
}
params["sender"] = nick.GetNick();
params["channel"] = context;
params["message"] = message;
params["type"] = type;
params["device1"] = options["target"];
if (options["extra_target"] != "")
{
params["device2"] = options["extra_target"];
}
}
else if (service == "supertoasty")
{
if (options["secret"] == "")
{
PutModule("Error: secret (device id) not set");
return;
}
use_post = false;
use_port = 80;
use_ssl = false;
service_host = "api.supertoasty.com";
service_url = "/notify/"+options["secret"];
params["title"] = message_title;
params["text"] = message_content;
params["image"] = "https://raw2.github.com/jreese/znc-push/master/logo.png";
params["sender"] = "ZNC Push";
}
else if (service == "faast")
{
if (options["secret"] == "")
{
PutModule("Error: secret not set to apikey");
return;
}
service_host = "www.appnotifications.com";
service_url = "/account/notifications.json";
params["user_credentials"] = options["secret"];
params["notification[title]"] = message_title;
params["notification[subtitle]"] = context;
params["notification[message]"] = message_content;
params["notification[long_message]"] = message_content;
params["notification[icon_url]"] = "https://raw2.github.com/jreese/znc-push/master/logo.png";
if ( options["message_sound"] != "" )
{
params["notification[sound]"] = options["message_sound"];
}
if ( options["message_uri"] != "" )
{
params["notification[run_command]"] = options["message_uri"];
}
}
else if (service == "nexmo")
{
if (options["username"] == "")
{
PutModule("Error: username (api key) not set");
return;
}
if (options["secret"] == "")
{
PutModule("Error: secret (api secret) not set");
return;
}
if (options["target"] == "")
{
PutModule("Error: destination mobile number (in international format) not set");
return;
}
service_host = "rest.nexmo.com";
service_url = "/sms/json";
params["api_secret"] = options["secret"];
params["api_key"] = options["username"];
params["from"] = message_title;
params["to"] = options["target"];
params["text"] = message_content;
}
else if (service == "url")
{
if (options["message_uri"] == "")
{
PutModule("Error: message_uri not set");
return;
}
CString::size_type count;
VCString parts;
CString url = options["message_uri"];
// Verify that the URL begins with either http:// or https://
count = url.Split("://", parts, false);
if (count != 2)
{
PutModule("Error: invalid url format");
return;
}
if(options["message_uri_post"] != "yes")
{
use_post = false;
}
if (parts[0] == "https")
{
use_ssl = true;
use_port = 443;
}
else if (parts[0] == "http")
{
use_ssl = false;
use_port = 80;
}
else
{
PutModule("Error: invalid url schema");
return;
}
// HTTP basic auth
if(options["username"] != "" || options["secret"] != "")
{
service_auth = options["username"] + CString(":") + options["secret"];
}
// Process the remaining portion of the URL
url = parts[1];
// Split out the host and optional port number; this breaks with raw IPv6 addresses
CString host = url.Token(0, false, "/");
count = host.Split(":", parts, false);
if (count > 1)
{
use_port = parts[1].ToInt();
}
service_host = parts[0];
// Split remaining URL into path and query components
url = "/" + url.Token(1, true, "/");
service_url = expand(url.Token(0, false, "?"), replace);
// Parse and expand query parameter values
url = url.Token(1, true, "?");
url.URLSplit(params);
for (MCString::iterator i = params.begin(); i != params.end(); i++) {
i->second = expand(i->second, replace);
}
}
else if (service == "airgram")
{
PutModule("Error: Airgram service shut down. Please configure another notification provider.");
}
else if (service == "slack")
{
if (options["secret"] == "")
{
PutModule("Error: secret (from webhook, e.g. T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX) not set");
return;
}
if (options["target"] == "")
{
PutModule("Error: target (channel or username) not set");
return;
}
service_host = "hooks.slack.com";
service_url = "/services/" + options["secret"];
if (options["username"] != "")
{
params["username"] = options["username"];
}
params["payload"] = expand("{\"channel\": \"{target}\", \"text\": \"*{title}*: {message}\"}", replace);
PutDebug("payload: " + params["payload"]);
}
else if (service == "discord")
{
if (options["secret"] == "")
{
PutModule("Error: secret (from webhook, e.g. 111111111111111111/abcdefghijklmopqrstuvwxyz1234567890-ABCDEFGHIJKLMNOPQRSTUVWXYZ123456) not set");
return;
}
service_host = "discord.com";
service_url = "/api/webhooks/" + options["secret"];
if (options["username"] != "")
{
params["username"] = options["username"];
}
params["content"] = message_content;
}
else if (service == "pushjet")
{
if (options["secret"] == "")
{
PutModule("Error: secret (service key) not set");
return;
}
service_host = "api.pushjet.io";
service_url = "/message";
params["secret"] = options["secret"];
params["title"] = message_title;
params["message"] = message_content;
if (message_uri != "")
{
params["link"] = message_uri;
}
if (options["message_priority"] != "")
{
params["level"] = options["message_priority"];
}
}
else if (service == "telegram")
{
if ((options["secret"] == "") || (options["target"] ==""))
{
PutModule("Error: secret (API key) or target (chat_id) not set");
return;
}
service_host = "api.telegram.org";
service_url = "/bot" + options["secret"] + "/sendMessage";
params["chat_id"] = options["target"];
params["text"] = message_content;
if (options["message_escape"] == "HTML") {
params["parse_mode"] = "HTML";
}
}
else
{
PutModule("Error: service type not selected");
return;
}
PutDebug("service: " + service);
PutDebug("service_host: " + service_host);
PutDebug("service_url: " + service_url);
PutDebug("service_auth: " + service_auth);
PutDebug("use_port: " + CString(use_port));
PutDebug("use_ssl: " + CString(use_ssl ? 1 : 0));
PutDebug("use_post: " + CString(use_post ? 1 : 0));
#ifdef USE_CURL
PutDebug("using libcurl");
long http_code = make_curl_request(service_host, service_url, service_auth, params, use_port, use_ssl, use_post, options["proxy"], options["proxy_ssl_verify"] != "no", options["debug"] == "on");
PutDebug("curl: HTTP status code " + CString(http_code));
if (!(http_code >= 200 && http_code < 300)) {
PutModule("Error: HTTP status code " + CString(http_code));
}
#else
PutDebug("NOT using libcurl");
// Create the socket connection, write to it, and add it to the queue
CPushSocket *sock = new CPushSocket(this);
sock->Connect(service_host, use_port, use_ssl);
sock->Request(use_post, service_host, service_url, params, service_auth);
AddSocket(sock);
#endif
}
/**
* Evaluate a boolean expression using condition values.
* All tokens must be separated by spaces, using "and" and "or" for
* boolean operators, "(" and ")" to enclose sub-expressions, and
* condition option names to evaluate each condition.
*
* @param expression Boolean expression string
* @param context Notification context
* @param nick Sender nick
* @param message Message contents
* @return Result of boolean evaluation
*/
bool eval(const CString& expression, const CString& context=CString(""), const CNick& nick=CNick(""), const CString& message=" ")
{
CString padded = expression.Replace_n("(", " ( ");
padded.Replace(")", " ) ");
VCString tokens;
padded.Split(" ", tokens, false);
PutDebug("Evaluating message: <" + nick.GetNick() + "> " + message);
bool result = eval_tokens(tokens.begin(), tokens.end(), context, nick, message);
return result;
}
#define expr(x, y) else if (token == x) { \
bool result = y; \
dbg += CString(x) + "/" + CString(result ? "true" : "false") + " "; \
value = oper ? value && result : value || result; \
}
/**
* Evaluate a tokenized boolean expression, or sub-expression.
*
* @param pos Token vector iterator current position
* @param end Token vector iterator end position
* @param context Notification context
* @param nick Sender nick
* @param message Message contents
* @return Result of boolean expression
*/
bool eval_tokens(VCString::iterator pos, VCString::iterator end, const CString& context, const CNick& nick, const CString& message)
{
bool oper = true;
bool value = true;
CString dbg = "";
for(; pos != end; pos++)
{
CString token = pos->AsLower();
if (token == "(")
{
// recursively evaluate sub-expressions
bool inner = eval_tokens(++pos, end, context, nick, message);
dbg += "( inner/" + CString(inner ? "true" : "false") + " ) ";
value = oper ? value && inner : value || inner;
// search ahead to the matching parenthesis token
unsigned int parens = 1;
while(pos != end)
{
if (*pos == "(")
{
parens++;
}
else if (*pos == ")")
{
parens--;
}
if (parens == 0)
{
break;
}
pos++;
}
}
else if (token == ")")
{
pos++;
PutDebug(dbg);
return value;
}
else if (token == "and")
{
dbg += "and ";
oper = true;
}
else if (token == "or")
{
dbg += "or ";
oper = false;
}
expr("true", true)
expr("false", false)
expr("away_only", away_only())
expr("client_count_less_than", client_count_less_than())
expr("highlight", highlight(message))
expr("idle", idle())
expr("last_active", last_active(context))
expr("last_notification", last_notification(context))
expr("nick_blacklist", nick_blacklist(nick))
expr("network_blacklist", network_blacklist())
expr("replied", replied(context))
expr("context", context_filter(context))
else
{
PutModule("Error: Unexpected token \"" + token + "\"");
}
}
PutDebug(dbg);
return value;
}
#undef expr
protected:
/**
* Check if the away status condition is met.
*
* @return True if away_only is not "yes" or away status is set
*/
bool away_only()
{
CString value = options["away_only"].AsLower();
return value != "yes" || GetNetwork()->IsIRCAway();
}
/**
* Check how many clients are connected to ZNC.
*
* @return Number of connected clients
*/
size_t client_count()
{
return GetNetwork()->GetClients().size();
}
/**
* Check if the client_count condition is met.
*
* @return True if client_count is less than client_count_less_than or if client_count_less_than is zero
*/
bool client_count_less_than()
{
unsigned int value = options["client_count_less_than"].ToUInt();
return value == 0 || client_count() < value;
}
/**
* Determine if the given message matches any highlight rules.
*
* @param message Message contents
* @return True if message matches a highlight
*/
bool highlight(const CString& message)
{
CString msg = " " + message.AsLower() + " ";
VCString values;
options["highlight"].Split(" ", values, false);
values.push_back("%nick%");
for (VCString::iterator i = values.begin(); i != values.end(); i++)
{
CString value = i->AsLower();
char prefix = value[0];
bool negate_match = false;
if (prefix == '-')
{
negate_match = true;
value.LeftChomp(1);
}
else if (prefix == '_')
{
value = " " + value.LeftChomp_n(1) + " ";
}
// Expand substrings like %nick%
if (m_pNetwork)
{
value = m_pNetwork->ExpandString(value);
}
else
{
value = GetUser()->ExpandString(value);
}
value = "*" + value.AsLower() + "*";
if (msg.WildCmp(value))
{
return !negate_match;
}
}
return false;
}
/**
* Determine if the given context matches any context rules.
*
* @param context The context of a message
* @return True if context matches the filter
*/
bool context_filter(const CString& raw_context)
{
CString context = raw_context.AsLower();