-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTelegram-API.tcl
More file actions
1496 lines (1328 loc) · 68.6 KB
/
Telegram-API.tcl
File metadata and controls
1496 lines (1328 loc) · 68.6 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
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
# ---------------------------------------------------------------------------- #
# Telegram-API module v20190806 for Eggdrop #
# #
# written by Eelco Huininga 2016-2019 #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Global internal variables #
# ---------------------------------------------------------------------------- #
# Create a namespace for the Telegram-API script
namespace eval ::telegram {}
# Declare global variables and set default values
set ::telegram::chanflags "iptmsw"
set ::telegram::cmdmodifier "/!."
set ::telegram::colorize_nicknames 0
set ::telegram::debuglog false
set ::telegram::msglogfile "/tmp/telegram-messages.json"
set ::telegram::locale "en"
set ::telegram::tg_poll_freq 5
set ::telegram::tg_web_page_preview false
set ::telegram::tg_prefer_usernames true
set ::telegram::timeformat "%Y-%m-%d %H:%M:%S"
set ::telegram::userflags "jlvck"
# Declare global internal variables; not user-configurable
set ::telegram::tg_update_id 0
set ::telegram::tg_bot_nickname ""
set ::telegram::tg_bot_realname ""
set ::telegram::irc_bot_nickname ""
array set ::telegram::tg_chat_title {}
array set ::telegram::tg_chat_description {}
array set ::telegram::tg_pinned_messages {}
array set ::telegram::tg_invite_link {}
array set ::telegram::tg_public_commands {}
array set ::telegram::tg_public_commands_help {}
array set ::telegram::tg_private_commands {}
array set ::telegram::tg_private_commands_help {}
array set ::telegram::irc_public_commands {}
array set ::telegram::irc_public_commands_help {}
array set ::telegram::irc_private_commands {}
array set ::telegram::irc_private_commands_help {}
array set ::telegram::filetransfers {}
# ---------------------------------------------------------------------------- #
# Initialization procedures #
# ---------------------------------------------------------------------------- #
# Initialize some variables (botnames) #
# ---------------------------------------------------------------------------- #
proc ::telegram::initialize {} {
global tcl_version nick
# Check pre-requisites: Tcl
if {$tcl_version < "8.6"} {
putlog "telegram::initialize: Error - Your Tcl version is [info patchlevel], but Telegram-API needs Tcl version 8.6 or higher"
return false
} else {
::telegram::putdebuglog "telegram::initialize: Debug - Tcl version=[info patchlevel]"
}
# Check pre-requisites: curl
if {[catch {set curl_version [lindex [exec -ignorestderr curl --version] 1]}]} {
putlog "telegram::initialize: Error - curl not found. Please install curl before starting the Telegram-API script."
return false
} else {
::telegram::putdebuglog "telegram::initialize: Debug - curl version=$curl_version"
if {$curl_version < "7.34.0"} {
putlog "telegram::initialize: Error - Your curl version is $curl_version, but Telegram-API needs curl version 7.34.0 or higher"
return false
}
}
# Check pre-requisites: jq
if {[catch {set jq_version [lindex [split [exec -ignorestderr jq --version] " -"] end]}]} {
putlog "telegram::initialize: Error - jq not found. Please install jq before starting the Telegram-API script."
return false
} else {
::telegram::putdebuglog "telegram::initialize: Debug - jq version=$jq_version"
if {$jq_version < "1.5"} {
putlog "telegram::initialize: Error - Your jq version is $jq_version, but Telegram-API needs jq version 1.5 or higher"
return false
}
}
# Output some debug info
::telegram::putdebuglog "telegram::initialize: Debug - encodingSystem=[encoding system]"
foreach envvar [array names ::env] {
::telegram::putdebuglog "telegram::initialize: Debug - environment variable $envvar = $::env($envvar)"
}
# Get some basic info about the Telegram bot
if {[::libtelegram::getMe] ne 0} {
putlog "telegram::initialize: Unable to get bot info from Telegram ($::libtelegram::errorMessage)"
return false
}
# Get the Telegram bot's nickname and realname
set ::telegram::tg_bot_nickname [::libjson::getValue $::libtelegram::result ".result.username"]
set ::telegram::tg_bot_realname [concat [::libjson::getValue $::libtelegram::result ".result.first_name//empty"] [::libjson::getValue $::libtelegram::result ".result.last_name//empty"]]
set ::telegram::irc_bot_nickname "$nick"
# Get up to date information on all Telegram groups/supergroups/channels
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
# Chat titles: Only get chat titles and descriptions for (super)groups we haven't queried yet
if {![info exists ::telegram::tg_chat_title($tg_chat_id)]} {
if {[::libtelegram::getChat $tg_chat_id] ne 0} {
putlog $::libtelegram::errorMessage
# return $::libtelegram::errorNumber
}
set ::telegram::tg_chat_type($tg_chat_id) [::libunicode::utf82ascii [::libjson::getValue $::libtelegram::result ".result.type"]]
set ::telegram::tg_chat_title($tg_chat_id) [::libunicode::utf82ascii [::libjson::getValue $::libtelegram::result ".result.title//empty"]]
set ::telegram::tg_chat_description($tg_chat_id) [::libjson::getValue $::libtelegram::result ".result.description//empty"]
set ::telegram::tg_chat_photo($tg_chat_id) [::libjson::getValue $::libtelegram::result ".result.photo.big_file_id//empty"]
::telegram::putdebuglog "telegram::initialize: Debug - tg_chat_id=$tg_chat_id tg_chat_type=$::telegram::tg_chat_type($tg_chat_id)"
::telegram::putdebuglog "telegram::initialize: Debug - tg_chat_id=$tg_chat_id tg_chat_title=$::telegram::tg_chat_title($tg_chat_id)"
::telegram::putdebuglog "telegram::initialize: Debug - tg_chat_id=$tg_chat_id tg_chat_description=$::telegram::tg_chat_description($tg_chat_id)"
::telegram::putdebuglog "telegram::initialize: Debug - tg_chat_id=$tg_chat_id tg_chat_photo=$::telegram::tg_chat_photo($tg_chat_id)"
}
# Pinned messages: Only get pinned messages for (super)groups we haven't queried yet
if {![info exists ::telegram::tg_pinned_messages($tg_chat_id)]} {
if {[set chattype [::libjson::getValue $::libtelegram::result ".result.pinned_message.chat.type"]] ne "null"} {
set ::telegram::tg_pinned_messages($tg_chat_id) [::telegram::getPinnedMessage $chattype [::libjson::getValue $::libtelegram::result ".result.pinned_message"]]
if {$::telegram::tg_pinned_messages($tg_chat_id) eq ""} {
unset -nocomplain ::telegram::tg_pinned_messages($tg_chat_id)
} else {
::telegram::putdebuglog "telegram::initialize: Debug - tg_chat_id=$tg_chat_id tg_pinned_message=$::telegram::tg_pinned_messages($tg_chat_id)"
}
}
}
# Invite links: Only get invite links for supergroups and channels we haven't queried yet
if {![info exists ::telegram::tg_invite_link($tg_chat_id)]} {
# Check if an invite link is already available in the chat object
if {[set ::telegram::tg_invite_link($tg_chat_id) [::libjson::getValue $::libtelegram::result ".result.invite_link//empty"]] eq ""} {
# If not, then create a new one (for supergroups and channels only)
if {$::telegram::tg_chat_type($tg_chat_id) != "group"} {
if {[::libtelegram::exportChatInviteLink $tg_chat_id] ne 0} {
putlog $::libtelegram::errorMessage
}
set ::telegram::tg_invite_link($tg_chat_id) [::libjson::getValue $::libtelegram::result ".result//empty"]
}
if {$::telegram::tg_invite_link($tg_chat_id) eq ""} {
unset -nocomplain ::telegram::tg_invite_link($tg_chat_id)
} else {
::telegram::putdebuglog "telegram::initialize: Debug - tg_chat_id=$tg_chat_id tg_invite_link=$::telegram::tg_invite_link($tg_chat_id)"
}
}
}
}
return true
}
# ---------------------------------------------------------------------------- #
# Procedures for reading data from the Telegram servers #
# ---------------------------------------------------------------------------- #
# Poll the Telegram server for updates #
# ---------------------------------------------------------------------------- #
proc ::telegram::pollTelegram {} {
global serveraddress
# Check if the bot has already joined a channel
if { [botonchan] != 1 } {
# Eggdrop hasn't joined all channels yet, so don't start polling Telegram yet
putlog "telegram::pollTelegram: Not connected to IRC, skipping Telegram poll"
utimer $::telegram::tg_poll_freq ::telegram::pollTelegram
return -1
}
# Poll the Telegram API for updates and check if we got a result
if {[::libtelegram::getUpdates $::telegram::tg_update_id] ne 0} {
# Network is probably down, so schedule the next poll
putlog $::libtelegram::errorMessage
if {[::libjson::getValue $::libtelegram::result ".parameters"] ne "null"} {
if {[::libjson::getValue $::libtelegram::result ".parameters.migrate_to_chat_id"] ne "null"} {
# A chat group has been migrated to a supergroup, but the conf file still got the chat_id for the old group
putlog "telegram::pollTelegram: Please edit your conf file with your new chat_id: [::libjson::getValue $::libtelegram::result ".parameters.migrate_to_chat_id"]"
} else {
putlog "telegram::pollTelegram: [::libjson::getValue $::libtelegram::result ".parameters"]"
}
}
utimer $::telegram::tg_poll_freq ::telegram::pollTelegram
return $::libtelegram::errorNumber
}
# Output raw json data received from the Telegram servers to the logfile
if {[::libjson::getValue $::libtelegram::result ".result"] ne ""} {
::telegram::putdebuglogfile $::libtelegram::result
}
# Cycle through each status update
foreach msg [split [::libjson::getValue $::libtelegram::result ".result\[\]"] "\n"] {
set ::telegram::tg_update_id [::libjson::getValue $msg ".update_id"]
# set msgtype [::libjson::getValue $msg ". | keys\[\] | select(. != \"update_id\")"]
set msgtype [::libjson::getValue $msg "keys_unsorted\[1\]"]
set chattype [::libjson::getValue $msg ".$msgtype.chat.type"]
::telegram::putdebuglog "telegram::pollTelegram: Debug - Processing update_id $::telegram::tg_update_id msgtype=$msgtype chattype=$chattype"
switch $chattype {
"private" {
# Record is a private chat record
if {[::libjson::hasKey $msg ".message.text"]} {
set txt [::libunicode::utf82ascii [::libjson::getValue $msg ".message.text"]]
set msgid [::libjson::getValue $msg ".message.message_id"]
set fromid [::libjson::getValue $msg ".message.from.id"]
::telegram::privateCommand "$fromid" "$msgid" "$txt"
}
}
"group" -
"supergroup" -
"channel" {
# Record is a group, supergroup or channel record
set chatid [::libjson::getValue $msg ".$msgtype.chat.id"]
# Get the sender's name for this message
if {$chattype eq "channel"} {
set name [::telegram::getUsername [::libjson::getValue $msg ".channel_post.chat"]]
set from_id [::libjson::getValue $msg ".channel_post.chat.id"]
} else {
set name [::telegram::getUsername [::libjson::getValue $msg ".$msgtype.from"]]
set from_id [::libjson::getValue $msg ".$msgtype.from.id"]
}
# Get the caption of this message (if any)
if {[::libjson::hasKey $msg ".$msgtype.caption"]} {
set caption " ([::libunicode::utf82ascii [::libjson::getValue $msg ".$msgtype.caption//empty"]])"
} else {
set caption ""
}
# Get the reply-to name for this message (if available)
if {[::libjson::hasKey $msg ".$msgtype.reply_to_message"]} {
set replyname [::telegram::getUsername [::libjson::getValue $msg ".$msgtype.reply_to_message.from"]]
}
# Check if this message is a forwarded message
if {[::libjson::hasKey $msg ".$msgtype.forward_from"]} {
set forwardname [::telegram::getUsername [::libjson::getValue $msg ".$msgtype.forward_from"]]
}
# Check if this message is a forwarded channelmessage
if {[::libjson::hasKey $msg ".$msgtype.forward_from_chat"]} {
set forwardname [::telegram::getUsername [::libjson::getValue $msg ".$msgtype.forward_from_chat"]]
}
# Check if this message was edited
if {$msgtype eq "edited_message"} {
set msgoriginaldate [clock format [::libjson::getValue $msg ".edited_message.date"] -format $::telegram::timeformat]
set msgediteddate [clock format [::libjson::getValue $msg ".edited_message.edit_date"] -format $::telegram::timeformat]
}
# Check if a text message has been sent to the Telegram group
if {[set txt [::libjson::getValue $msg ".$msgtype.text"]] ne "null"} {
# Modify text if it was edited
if {$msgtype eq "edited_message"} {
set txt "[::msgcat::mc MSG_TG_MSGEDITED "$txt" "$msgoriginaldate" "$msgediteddate"]"
}
# Modify text if it is a reply-to or forwarded from
if {[info exists replyname]} {
set replytomsg [::libjson::getValue $msg ".$msgtype.reply_to_message.text"]
set txt "[::msgcat::mc MSG_TG_MSGREPLYTOSENT "$txt" "$replyname" "$replytomsg"]"
} elseif {[info exists forwardname]} {
set txt "[::msgcat::mc MSG_TG_MSGFORWARDED "$txt" "$forwardname"]"
}
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
# Send the text message if it matches
if {$chatid eq $tg_chat_id} {
# Treat each line seperate
foreach line [split [string map {\\n \n} [::libunicode::utf82ascii $txt]] "\n"] {
::telegram::putdebuglog "telegram::pollTelegram: Debug - tg_chat_id=$tg_chat_id irc_channel=$irc_channel name=$name text=$line"
putchan $irc_channel [::msgcat::mc MSG_TG_MSGSENT "$name" "$line"]
# If the text contains an URL, get the title of the website
if {![info exists replytomsg]} {
if {[string match -nocase "*http://?*" $line] || [string match -nocase "*https://?*" $line] || [string match -nocase "*www.?*" $line]} {
putchan $irc_channel [::libunicode::utf82ascii [::telegram::getWebsiteTitle $line]]
}
}
}
# Check if it was a public bot command
if {[string match "*\\[string index $txt 0]*" "$::telegram::cmdmodifier"]} {
set msgid [::libjson::getValue $msg ".$msgtype.message_id"]
::telegram::publicCommand $from_id "$tg_chat_id" "$msgid" "$irc_channel" "$txt"
}
}
}
}
# Check if this message is a pinned message
if {[::libjson::hasKey $msg ".$msgtype.pinned_message"]} {
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
set ::telegram::tg_pinned_messages($chatid) [::telegram::getPinnedMessage $chattype [::libjson::getValue $msg ".$msgtype.pinned_message"]]
putchan $irc_channel [::msgcat::mc MSG_TG_MSGSENT "$name" "$::telegram::tg_pinned_messages($chatid)"]
}
}
}
# Check if a sticker has been sent to the Telegram group
if {[set file_id [::libjson::getValue $msg ".$msgtype.sticker.file_id"]] ne "null"} {
# set tg_width [::libjson::getValue $msg ".$msgtype.sticker.width"]
# set tg_height [::libjson::getValue $msg ".$msgtype.sticker.height"]
# set tg_thumb [::libjson::getValue $msg ".$msgtype.sticker.thumb"]
set emoji [::libjson::getValue $msg ".$msgtype.sticker.emoji"]
set setname [::libjson::getValue $msg ".$msgtype.sticker.set_name"]
# set tg_mask_position [::libjson::getValue $msg ".$msgtype.sticker.mask_position"]
# set tg_file_size [::libjson::getValue $msg ".$msgtype.sticker.file_size"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_STICKERSENT "$name" "$setname" "[sticker2ascii $file_id]"]
}
}
}
# Check if audio has been sent to the Telegram group
if {[set tg_file_id [::libjson::getValue $msg ".$msgtype.audio.file_id"]] ne "null"} {
if {[set tg_duration [::libjson::getValue $msg ".$msgtype.audio.duration"]] eq ""} {
set tg_duration "0"
}
set tg_performer [::libjson::getValue $msg ".$msgtype.audio.performer"]
set tg_title [::libjson::getValue $msg ".$msgtype.audio.title"]
# set tg_mime_type [::libjson::getValue $msg ".$msgtype.audio.mime_type"]
# set tg_file_size [::libjson::getValue $msg ".$msgtype.audio.file_size"]
# set tg_thumb [::libjson::getValue $msg ".$msgtype.audio.thumb"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_AUDIOSENT "$name" "$tg_performer" "$tg_title" "[format "%02d" [expr {$tg_duration/60}]]:[format "%02d" [expr {$tg_duration%60}]]" "$tg_file_id"]
}
}
}
# Check if a document has been sent to the Telegram group
if {[set tg_file_id [::libjson::getValue $msg ".$msgtype.document.file_id"]] ne "null"} {
# Check if an animation has been sent to the Telegram group
if {[set tg_duration [::libjson::getValue $msg ".$msgtype.animation.duration"]] ne "null"} {
# An animation has been sent to the Telegram group
# set tg_width [::libjson::getValue $msg ".$msgtype.animation.width"]
# set tg_height [::libjson::getValue $msg ".$msgtype.animation.height"]
# set tg_thumb [::libjson::getValue $msg ".$msgtype.animation.thumb"]
set tg_file_name [::libjson::getValue $msg ".$msgtype.animation.file_name"]
# set tg_mime_type [::libjson::getValue $msg ".$msgtype.animation.mime_type"]
set tg_file_size [::libjson::getValue $msg ".$msgtype.animation.file_size"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_ANIMATIONSENT "$name" "$caption" "[format "%02d" [expr {$tg_duration/60}]]:[format "%02d" [expr {$tg_duration%60}]]" "$tg_file_id"]
}
}
} else {
# Not an animation, so it must be a document
# set tg_thumb [::libjson::getValue $msg ".$msgtype.document.thumb"]
set tg_file_name [::libjson::getValue $msg ".$msgtype.document.file_name"]
# set tg_mime_type [::libjson::getValue $msg ".$msgtype.document.mime_type"]
set tg_file_size [::libjson::getValue $msg ".$msgtype.document.file_size"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_DOCSENT "$name" "$caption" "$tg_file_name" "$tg_file_size" "$tg_file_id"]
}
}
}
}
# Check if a photo has been sent to the Telegram group
if {[set tg_file_id [::libjson::getValue $msg ".$msgtype.photo\[-1\].file_id"]] ne "null"} {
# set tg_width [::libjson::getValue $msg ".$msgtype.photo\[-1\].width"]
# set tg_height [::libjson::getValue $msg ".$msgtype.photo\[-1\].height"]
set tg_file_size [::libjson::getValue $msg ".$msgtype.photo\[-1\].file_size"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_PHOTOSENT "$name" "$caption" "$tg_file_size" "$tg_file_id"]
}
}
}
# Check if a video has been sent to the Telegram group
if {[set tg_file_id [::libjson::getValue $msg ".$msgtype.video.file_id"]] ne "null"} {
# set tg_width [::libjson::getValue $msg ".$msgtype.video.width"]
# set tg_height [::libjson::getValue $msg ".$msgtype.video.height"]
if {[set tg_duration [::libjson::getValue $msg ".$msgtype.video.duration"]] eq "null"} {
set tg_duration "0"
}
# set tg_thumb [::libjson::getValue $msg ".$msgtype.video.thumb"]
# set tg_mime_type [::libjson::getValue $msg ".$msgtype.video.mime_type"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_VIDEOSENT "$name" "$caption" "[format "%02d" [expr {$tg_duration/60}]]:[format "%02d" [expr {$tg_duration%60}]]" "$tg_file_id"]
}
}
}
# Check if a voice object has been sent to the Telegram group
if {[set tg_file_id [::libjson::getValue $msg ".$msgtype.voice.file_id"]] ne "null"} {
if {[set tg_duration [::libjson::getValue $msg ".$msgtype.voice.duration"]] eq ""} {
set tg_duration "0"
}
# set tg_mime_type [::libjson::getValue $msg ".$msgtype.voice.mime_type"]
set tg_file_size [::libjson::getValue $msg ".$msgtype.voice.file_size"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_VOICESENT "$name" "[format "%02d" [expr {$tg_duration/60}]]:[format "%02d" [expr {$tg_duration%60}]]" "$tg_file_size" "$tg_file_id"]
}
}
}
# Check if a contact has been sent to the Telegram group
if {[set tg_phone_number [::libjson::getValue $msg ".$msgtype.contact.phone_number"]] ne "null"} {
set tg_first_name [::libjson::getValue $msg ".$msgtype.contact.first_name//empty"]
set tg_last_name [::libjson::getValue $msg ".$msgtype.contact.last_name//empty"]
# set tg_user_id [::libjson::getValue $msg ".$msgtype.contact.user_id//empty"]
# set tg_vcard [::libjson::getValue $msg ".$msgtype.contact.vcard//empty"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_CONTACTSENT "$name" "+$tg_phone_number" "$tg_first_name" "$tg_last_name"]
}
}
}
# Check if a location has been sent to the Telegram group
if {[::libjson::hasKey $msg ".$msgtype.location"]} {
# Check if a venue has been sent to the Telegram group
if {[set tg_location [::libjson::getValue $msg ".$msgtype.venue.location"]] ne "null"} {
set tg_title [::libjson::getValue $msg ".$msgtype.venue.title"]
set tg_address [::libjson::getValue $msg ".$msgtype.venue.address"]
set tg_foursquare_id [::libjson::getValue $msg ".$msgtype.venue.foursquare_id"]
# set tg_foursquare_type [::libjson::getValue $msg ".$msgtype.venue.foursquare_type"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_VENUESENT "$name" "$tg_location" "$tg_title" "$tg_address" "$tg_foursquare_id"]
}
}
} else {
# Not a venue, so it must be a location
set tg_longitude [::libjson::getValue $msg ".$msgtype.location.longitude"]
set tg_latitude [::libjson::getValue $msg ".$msgtype.location.latitude"]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_LOCATIONSENT "$name" "$tg_longitude" "$tg_latitude"]
}
}
}
}
# Check if someone has been added to the Telegram group
if {[set new_member_id [::libjson::getValue $msg ".$msgtype.new_chat_member.id"]] ne "null"} {
set new_chat_member [::telegram::getUsername [::libjson::getValue $msg ".$msgtype.new_chat_member"]]
# Check if we want to send a public welcome message to the new participant
if {[string match "*w*" $::telegram::chanflags]} {
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
::libtelegram::sendMessage $chatid [::msgcat::mc MSG_TG_WELCOME "$::telegram::tg_chat_title($chatid)" "$::telegram::tg_bot_nickname" "$serveraddress/$irc_channel" "$irc_channel"] "html" false "" ""
}
}
}
# Check if we want to send a private welcome message to the new participant
if {[string match "*W*" $::telegram::chanflags]} {
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
::libtelegram::sendMessage $new_member_id [::msgcat::mc MSG_TG_WELCOME "$::telegram::tg_chat_title($chatid)" "$::telegram::tg_bot_nickname" "$serveraddress/$irc_channel" "$irc_channel"] "html" false "" ""
}
}
}
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
if {$name eq $new_chat_member} {
putchan $irc_channel [::msgcat::mc MSG_TG_USERJOINED "[::libunicode::utf82ascii $name]"]
} else {
putchan $irc_channel [::msgcat::mc MSG_TG_USERADD "[::libunicode::utf82ascii $name]" "[::libunicode::utf82ascii $new_chat_member]"]
}
}
}
}
# Check if someone has been removed from the Telegram group
if {[::libjson::hasKey $msg ".$msgtype.left_chat_member"]} {
set left_chat_member [::telegram::getUsername [::libjson::getValue $msg ".$msgtype.left_chat_member"]]
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
if {$name eq $left_chat_member} {
putchan $irc_channel [::msgcat::mc MSG_TG_USERLEFT "[::libunicode::utf82ascii $name]"]
} else {
putchan $irc_channel [::msgcat::mc MSG_TG_USERREMOVED "[::libunicode::utf82ascii $name]" "[::libunicode::utf82ascii $left_chat_member]"]
}
}
}
}
# Check if the title of the Telegram group chat has changed
if {[set chat_title [::libjson::getValue $msg ".$msgtype.new_chat_title"]] ne "null"} {
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
set ::telegram::tg_chat_title($tg_chat_id) [::libunicode::utf82ascii $chat_title]
putchan $irc_channel [::msgcat::mc MSG_TG_TITLECHANGE "[::libunicode::utf82ascii $name]" "$::telegram::tg_chat_title($tg_chat_id)"]
}
}
}
# Check if the photo of the Telegram group chat has changed
if {[set file_id [::libjson::getValue $msg ".$msgtype.new_chat_photo\[-1\].file_id"]] ne "null"} {
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_PICCHANGE "[::libunicode::utf82ascii $name]" "$file_id"]
}
}
}
# Check if the photo of the Telegram group chat has been deleted
if {[::libjson::hasKey $msg ".$msgtype.delete_chat_photo"]} {
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_PICDELETE "[::libunicode::utf82ascii $name]"]
}
}
}
# Check if the group is migrated to a supergroup
if {[set newchatid [::libjson::getValue $msg ".$msgtype.migrate_to_chat_id"]] ne "null"} {
# Scan all IRC channels to check if it's connected to this Telegram group
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$chatid eq $tg_chat_id} {
putchan $irc_channel [::msgcat::mc MSG_TG_GROUPMIGRATED "[::libunicode::utf82ascii $name] $chatid $newchatid"]
putlog "telegram::pollTelegram: The group with id $chatid has been migrated to a supergroup by $name. Please edit your config file and add \{$newchatid $irc_channel\}"
}
}
}
}
default {
# Handle any unknown messages
putlog "telegram::pollTelegram: Unknown message received: $msg"
}
}
incr ::telegram::tg_update_id
}
# ...and set a timer so it triggers the next poll
utimer $::telegram::tg_poll_freq ::telegram::pollTelegram
return 0
}
# ---------------------------------------------------------------------------- #
# Respond to group commands send by Telegram users #
# ---------------------------------------------------------------------------- #
proc ::telegram::publicCommand {from_id chat_id msgid channel message} {
# Don't process single character commands
if {[set parameter_start [string wordend $message 1]] == 1} {
return 0
}
# Only process is command is in range a...z
if {![string is alpha [set command [string tolower [string range $message 1 $parameter_start-1]]]]} {
return 0
}
# Check if this command has a bot identifier in it (/command@BotIdentifier)
if {[string match -nocase "@*" [string range $message $parameter_start end]]} {
# If so, then check if the identifier matches our bot
if {![string match -nocase "@$::telegram::tg_bot_realname*" [string range $message $parameter_start end]]} {
# If not, then stop processing the command
return 0
} else {
set parameter_start [string wordend $message $parameter_start+1]
}
}
# Let the Telegram users know that we've received the bot command, and we're preparing an answer
::libtelegram::sendChatAction $chat_id "typing"
if {$command eq "help"} {
set response "[::msgcat::mc MSG_BOT_PUBHELP "$::telegram::irc_bot_nickname"]\n\n"
foreach {command helpmessage} [lsort -stride 2 [array get ::telegram::tg_public_commands_help]] {
append response "/$command $helpmessage\n"
}
::libtelegram::sendMessage $chat_id "[url_encode $response]" "html" false $msgid ""
putchan $channel "[strip_html $response]"
} else {
# Not one of the standard bot commands, so check if the bot command is in our dynamic command list
foreach {cmd prc} [array get ::telegram::tg_public_commands] {
if {$command == $cmd} {
if {[$prc $from_id $chat_id $msgid $channel $message $parameter_start] ne 0} {
# The module returned an error, so show the help message for the specified command
set response "/$command $::telegram::tg_public_commands_help($command)"
::libtelegram::sendMessage $chat_id "[url_encode $response]" "html" false $msgid ""
putchan $channel "[strip_html $response]"
}
return 0
}
}
# Not in our dynamic command list either, so respond with an unknown command message
::libtelegram::sendMessage $chat_id "[::msgcat::mc MSG_BOT_UNKNOWNCMD]" "html" false $msgid ""
putchan $channel "[::msgcat::mc MSG_BOT_UNKNOWNCMD]"
return -1
}
return 0
}
# ---------------------------------------------------------------------------- #
# Add a bot command to the dynamic bot command list #
# ---------------------------------------------------------------------------- #
proc ::telegram::addPublicTgCommand {keyword procedure helpmessage} {
set ::telegram::tg_public_commands($keyword) $procedure
set ::telegram::tg_public_commands_help($keyword) $helpmessage
}
# ---------------------------------------------------------------------------- #
# Remove a bot command from the dynamic bot command list #
# ---------------------------------------------------------------------------- #
proc ::telegram::delPublicTgCommand {keyword} {
if {[info exists ::telegram::tg_public_commands($keyword)]} {
unset -nocomplain ::telegram::tg_public_commands($keyword)
unset -nocomplain ::telegram::tg_public_commands_help($keyword)
return true
} else {
return false
}
}
# ---------------------------------------------------------------------------- #
# Respond to private commands send by Telegram users #
# ---------------------------------------------------------------------------- #
proc ::telegram::privateCommand {from_id msgid message} {
set parameter_start [string wordend $message 1]
set command [string tolower [string range $message 1 $parameter_start-1]]
::libtelegram::sendChatAction $from_id "typing"
switch $command {
"help" {
set response "[::msgcat::mc MSG_BOT_PRVHELP "$::telegram::irc_bot_nickname"]\n\n"
foreach {command helpmessage} [lsort -stride 2 [array get ::telegram::tg_private_commands_help]] {
append response "/$command $helpmessage\n"
}
::libtelegram::sendMessage $from_id "[url_encode $response]" "html" false $msgid ""
}
"login" {
set login_start [string wordend $message 1]
set login_end [string wordend $message $login_start+1]
set irchandle [string trim [string range $message $login_start $login_end]]
set ircpassword [string trim [string range $message $login_end end]]
# Set the password if this is the first time this user logs in
# if {[getuser $irchandle PASS] == ""} {
# setuser $irchandle PASS "$ircpassword"
# ::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_PASSWORDSET "$::telegram::tg_bot_nickname"]" "html" false $msgid ""
# }
# Check if the password matches
if {[passwdok $irchandle $ircpassword]} {
setuser $irchandle XTRA "TELEGRAM_USERID" "[string range $from_id 0 12]"
# setuser $irchandle XTRA "IRL" "[string range $first_name 0 159] [string range $last_name 0 159]"
# Lookup the last login time
if {[set lastlogin [getuser $irchandle XTRA "TELEGRAM_LASTLOGIN"]] == ""} {
# First login of this user, so set LASTLOGOUT and LASTUSERID to defaults
set lastlogin "[::msgcat::mc MSG_BOT_FIRSTLOGIN "$::telegram::tg_bot_nickname"]"
setuser $irchandle XTRA "TELEGRAM_LASTLOGOUT" "0"
setuser $irchandle XTRA "TELEGRAM_LASTUSERID" "0"
} else {
# Prepare string with last login time
set lastlogin "[::msgcat::mc MSG_BOT_LASTLOGIN "$::telegram::tg_bot_nickname" "[clock format $lastlogin -format $::telegram::timeformat]"]"
}
# ...and set the last login time to the current time
setuser $irchandle XTRA "TELEGRAM_LASTLOGIN" "[clock seconds]"
# Set the Telegram account creation date if this is the first time the user logs in
if {[getuser $irchandle XTRA "TELEGRAM_CREATED"] == ""} {
setuser $irchandle XTRA "TELEGRAM_CREATED" "[clock seconds]"
}
# Set the userflags to the default settings
if {[getuser $irchandle XTRA "TELEGRAM_USERFLAGS"] == ""} {
setuser $irchandle XTRA "TELEGRAM_USERFLAGS" "$::telegram::userflags"
}
::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_USERLOGIN "$::telegram::tg_bot_nickname" "$irchandle"]\n\n $lastlogin" "html" false $msgid ""
putlog "telegram::privateCommand: Succesful login from $from_id, username $irchandle"
} else {
# Username/password combo doesn't match
::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_USERPASSWRONG]" "html" false $msgid ""
putlog "telegram::privateCommand: Failed login attempt from $from_id, username $irchandle"
}
}
"logout" {
if {[set irchandle [::telegram::getIRCNickFromTelegramID $from_id]] != -1} {
setuser $irchandle XTRA "TELEGRAM_USERID" ""
setuser $irchandle XTRA "TELEGRAM_LASTUSERID" "$from_id"
setuser $irchandle XTRA "TELEGRAM_LASTLOGOUT" "[clock seconds]"
::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_USERLOGOUT "$irchandle" "$from_id"]" "html" false $msgid ""
putlog "telegram::privateCommand: Succesful logout from $from_id, username $irchandle"
} else {
::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_NOTLOGGEDIN]" "html" false $msgid ""
}
}
# Show user information
"myinfo" {
if {[set irchandle [::telegram::getIRCNickFromTelegramID $from_id]] != -1} {
set tg_lastlogin [clock format [getuser $irchandle XTRA "TELEGRAM_LASTLOGIN"] -format $::telegram::timeformat]
set tg_lastlogout [clock format [getuser $irchandle XTRA "TELEGRAM_LASTLOGOUT"] -format $::telegram::timeformat]
set tg_lastuserid [getuser $irchandle XTRA "TELEGRAM_LASTUSERID"]
set tg_created [clock format [getuser $irchandle XTRA "TELEGRAM_CREATED"] -format $::telegram::timeformat]
set tg_userflags [getuser $irchandle XTRA "TELEGRAM_USERFLAGS"]
set irc_created [clock format [getuser $irchandle XTRA "created"] -format $::telegram::timeformat]
set irc_laston [clock format [lindex [split [getuser $irchandle LASTON] " "] 0] -format $::telegram::timeformat]
set irc_hosts [getuser $irchandle HOSTS]
set irc_info [getuser $irchandle INFO]
::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_USERINFO "$irchandle" "$from_id" "$tg_lastlogin" "$tg_lastlogout" "$tg_lastuserid" "$tg_created" "$irc_created" "$irc_laston" "irc_hosts" "$irc_info"]" "html" false $msgid ""
putlog "telegram::privateCommand: My information accessed by $from_id, username $irchandle"
} else {
::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_NOTLOGGEDIN]" "html" false $msgid ""
}
}
default {
# Not one of the standard bot commands, so check if the bot command is in our dynamic command list
foreach {cmd prc} [array get ::telegram::tg_private_commands] {
if {$command == $cmd} {
$prc $from_id $msgid $channel $message $parameter_start
return
}
}
# Not in our dynamic command list either, so respond with an unknown command message
::libtelegram::sendMessage $from_id "[::msgcat::mc MSG_BOT_UNKNOWNCMD]" "html" false $msgid ""
}
}
}
# ---------------------------------------------------------------------------- #
# Add a bot command to the dynamic bot command list #
# ---------------------------------------------------------------------------- #
proc ::telegram::addPrivateTgCommand {keyword procedure helpmessage} {
set ::telegram::tg_private_commands($keyword) $procedure
set ::telegram::tg_private_commands_help($keyword) $procedure
}
# ---------------------------------------------------------------------------- #
# Remove a bot command from the dynamic bot command list #
# ---------------------------------------------------------------------------- #
proc ::telegram::delPrivateTgCommand {keyword} {
if {[info exists ::telegram::tg_private_commands($keyword)]} {
unset -nocomplain ::telegram::tg_private_commands($keyword)
unset -nocomplain ::telegram::tg_private_commands_help($keyword)
return true
} else {
return false
}
}
# ---------------------------------------------------------------------------- #
# Format the pinned message to a message readable by IRC users #
# ---------------------------------------------------------------------------- #
proc ::telegram::getPinnedMessage {chattype pinned_message} {
# Get the name of the Telegram user who wrote the message
if {$chattype eq "channel"} {
set pin_name [::libunicode::utf82ascii [::libjson::getValue $pinned_message ".chat.username"]]
} else {
set pin_name [::telegram::getUsername [::libjson::getValue $pinned_message ".from"]]
}
set pin_date "[clock format [::libjson::getValue $pinned_message ".date"] -format $::telegram::timeformat]"
set pin_txt "[::libunicode::utf82ascii [::libjson::getValue $pinned_message ".text"]]"
return [::msgcat::mc MSG_TG_PINNEDMESSAGE "$pin_name" "$pin_txt" "$pin_date"]
}
# ---------------------------------------------------------------------------- #
# Get the username or realname for a Telegram user object #
# ---------------------------------------------------------------------------- #
proc ::telegram::getUsername {userobject} {
# Set sender's name for group or supergroup messages
if {$::telegram::tg_prefer_usernames} {
if {[set name [::libjson::getValue $userobject ".username"]] == "null"} {
set name [concat [::libjson::getValue $userobject ".first_name//empty"] [::libjson::getValue $userobject ".last_name//empty"]]
}
} else {
set name [concat [::libjson::getValue $userobject ".first_name//empty"] [::libjson::getValue $userobject ".last_name//empty"]]
}
set name "\003[::telegram::getColorFromUserID [::libjson::getValue $userobject ".id"]][::libunicode::utf82ascii $name]\003"
return $name
}
# ---------------------------------------------------------------------------- #
# Procedures for sending data from IRC to Telegram #
# ---------------------------------------------------------------------------- #
# Send a message from IRC to Telegram #
# ---------------------------------------------------------------------------- #
proc ::telegram::ircSendMessage {nick hostmask handle channel msg} {
# Check if this is a bot command
if {[string match "*\\[string index $msg 0]*" "$::telegram::cmdmodifier"]} {
# If so, then check which bot command it is, and process it. Don't send it to the Telegram group though.
set parameter_start [string wordend $msg 1]
set command [string tolower [string range $msg 1 $parameter_start-1]]
if {[string match $command "tgadmins"]} {
::telegram::tgAdminsInfo $channel $nick]
return 0
}
if {[string match $command "tgfile"]} {
::telegram::ircSendFile $nick [string trim [string range $msg $parameter_start end]]
return 0
}
if {[string match $command "tginfo"]} {
::telegram::tgInfo $channel $nick [string trim [string range $msg $parameter_start end]]
return 0
}
if {[string match $command "tgwhois"]} {
::telegram::tgWhoIs $channel $nick [string trim [string range $msg $parameter_start end]]
return 0
}
}
# Only send a message to the Telegram group if the 'voice'-flag is set in the user flags variable
if {[string match "*v*" $::telegram::userflags]} {
foreach {tg_chat_id tg_channel} [array get ::telegram::tg_channels] {
if {$channel eq $tg_channel} {
::libtelegram::sendMessage $tg_chat_id [::msgcat::mc MSG_IRC_MSGSENT "$nick" "[url_encode [::libunicode::ascii2utf8 $msg]]"] "html" false "" ""
}
}
}
return 0
}
# ---------------------------------------------------------------------------- #
# Inform the Telegram group(s) that someone joined an IRC channel #
# ---------------------------------------------------------------------------- #
proc ::telegram::ircNickJoined {nick uhost handle channel} {
global serveraddress
# Show the invite link for all Telegram groups connected to this IRC channel
foreach {tg_chat_id irc_channel} [array get ::telegram::tg_channels] {
if {$channel eq $irc_channel} {
# Check if we want to show a pinned message on this IRC channel
if {[string match "*p*" $::telegram::chanflags]} {
# Show pinned messages (if any) as a notice to the new user on IRC
if {[info exists ::telegram::tg_pinned_messages($tg_chat_id)]} {
puthelp "NOTICE $channel :$::telegram::tg_pinned_messages($tg_chat_id)"
}
}
# Check if we want to show an invite link on this IRC channel
if {[string match "*i*" $::telegram::chanflags]} {
# Show the Telegram chat invite link
if {[info exists ::telegram::tg_invite_link($tg_chat_id)]} {
puthelp "NOTICE $channel :[::msgcat::mc MSG_IRC_INVITELINK $::telegram::tg_chat_type($tg_chat_id) $::telegram::tg_chat_title($tg_chat_id) $::telegram::tg_invite_link($tg_chat_id)]"
}
}
}
}
# Don't notify the Telegram users when the bot joins an IRC channel
if {$nick ne $::telegram::irc_bot_nickname} {
foreach {tg_chat_id tg_channel} [array get ::telegram::tg_channels] {
if {$channel eq $tg_channel} {
# Only send a join message to the Telegram group if the 'join'-flag is set in the user flags variable
if {[string match "*j*" [::telegram::getUserFlags $handle]]} {
if {![validuser $handle]} {
::libtelegram::sendMessage $tg_chat_id [::msgcat::mc MSG_IRC_NICKJOINED "$nick" "$serveraddress/$channel" "$channel"] "html" false "" ""
}
}
}
}
}
return 0
}
# ---------------------------------------------------------------------------- #
# Inform the Telegram group(s) that someone has left an IRC channel #
# ---------------------------------------------------------------------------- #
proc ::telegram::ircNickLeft {nick uhost handle channel message} {
global serveraddress
# Don't notify the Telegram users when the bot leaves an IRC channel
if {$nick ne $::telegram::irc_bot_nickname} {
foreach {tg_chat_id tg_channel} [array get ::telegram::tg_channels] {
if {$channel eq $tg_channel} {
# Only send a leave message to the Telegram group if the 'leave'-flag is set in the user flags variable
if {[string match "*l*" [::telegram::getUserFlags $handle]]} {
if {![validuser $handle]} {
::libtelegram::sendMessage $tg_chat_id [::msgcat::mc MSG_IRC_NICKLEFT "$nick" "$serveraddress/$channel" "$channel" "$message"] "html" false "" ""
}
}
}
}
}
return 0
}
# ---------------------------------------------------------------------------- #
# Send an action from an IRC user to Telegram #
# ---------------------------------------------------------------------------- #
proc ::telegram::ircNickAction {nick uhost handle channel keyword message} {
# Only send an action message to the Telegram group if the 'voice'-flag is set in the user flags variable
if {[string match "*v*" $::telegram::userflags]} {
foreach {tg_chat_id tg_channel} [array get ::telegram::tg_channels] {
if {$channel eq $tg_channel} {
::libtelegram::sendMessage $tg_chat_id [::msgcat::mc MSG_IRC_NICKACTION "$nick" "$nick" "$message"] "html" false "" ""
}
}
}
return 0
}
# ---------------------------------------------------------------------------- #
# Inform the Telegram group(s) that an IRC nickname has been changed #
# ---------------------------------------------------------------------------- #
proc ::telegram::ircNickChange {nick uhost handle channel newnick} {
# Only send a nick change message to the Telegram group if the 'change'-flag is set in the user flags variable
if {[string match "*c*" $::telegram::userflags]} {
foreach {tg_chat_id tg_channel} [array get ::telegram::tg_channels] {
if {$channel eq $tg_channel} {
::libtelegram::sendMessage $tg_chat_id [::msgcat::mc MSG_IRC_NICKCHANGE "$nick" "$newnick"] "html" false "" ""
}
}
}
return 0
}
# ---------------------------------------------------------------------------- #
# Inform the Telegram group(s) that the topic of an IRC channel has changed #
# ---------------------------------------------------------------------------- #
proc ::telegram::ircTopicChange {nick uhost handle channel topic} {
global serveraddress
if {[string match "*t*" $::telegram::chanflags]} {
foreach {tg_chat_id tg_channel} [array get ::telegram::tg_channels] {
if {$channel eq $tg_channel} {
if {$nick ne "*"} {
::libtelegram::sendMessage $tg_chat_id [::msgcat::mc MSG_IRC_TOPICCHANGE "$nick" "$serveraddress/$channel" "$channel" "$topic"] "html" false "" ""
if {[string match "*s*" $::telegram::chanflags]} {
::libtelegram::setChatTitle $tg_chat_id $topic
}
}
}
}
}
return 0
}
# ---------------------------------------------------------------------------- #
# Inform the Telegram group(s) that someone has been kicked from the channel #
# ---------------------------------------------------------------------------- #
proc ::telegram::ircNickKicked {nick uhost handle channel target reason} {
# Only send a kick message to the Telegram group if the 'kick'-flag is set in the user flags variable
if {[string match "*k*" $::telegram::userflags]} {
foreach {tg_chat_id tg_channel} [array get ::telegram::tg_channels] {
if {$channel eq $tg_channel} {
::libtelegram::sendMessage $tg_chat_id [::msgcat::mc MSG_IRC_KICK "$nick" "$target" "$channel" "$reason"] "html" false "" ""
}
}
}
return 0