-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram-daemon
executable file
·120 lines (112 loc) · 3.51 KB
/
telegram-daemon
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
#!/usr/bin/expect
# Gloval variables
set user "admin"
set terminator "response"
set folder ""
set prompt "> "
switch -- [llength $argv] {
1 {
set user [lindex $argv 0]
}
2 {
set user [lindex $argv 0]
set folder [lindex $arg 1]
}
0 {
}
default {
send_user "Usage: ./telegram-daemon <username>\n"
send_user "Usage: ./telegram-daemon <username> <telegram/folder/>\n"
exit 1
}
}
send_user "Using telegram-folder: $folder"
# Remove ansi escape sequences
proc un_ansi {data} {
set txt {}
while {[string length ${data}]} {
set match { }
switch -regexp -- ${data} {
{^\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]} {
regexp -- {^\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]} ${data} match
}
{^(.+?)\x1b} {
regexp -- {^(.+?)\x1b} ${data} UNUSED match
# handle special escape sequences
regsub -all -- {\\([\\\[\]])} ${match} {\1} raw_match
append txt ${raw_match}
}
{^\x1b} {
# do nothing
}
default {
set match ${data}
append txt ${match}
}
}
set data [string range ${data} [string length ${match}] end]
}
# remove white spaces not needed anymore
regsub -all -- "\t+" ${txt} { } txt
regsub -all -- " +" ${txt} { } txt
regsub -all -nocase -- "\n+" [string trim ${txt}] "\n" txt
return ${txt}
}
spawn ${folder}telegram
while true {
expect {
"Telegram-client" {
sleep 2
send "msg Daniel_Iñigo Telegram started\r"
expect "Sent:*\r"
}
# When it receives a message
-re {\[\d{2}:\d{2}\].*>>>.*\n} {
set line [un_ansi $expect_out(0,string)]]
# Get the user
regexp -- {\[\d{2}:\d{2}\].*>>>} $line user
regsub -all -- {\[\d{2}:\d{2}\]\s|\s>>>} $user {} user
regsub -all -- {\s} $user "_" user
# Get the command
regexp -- {>>>.*$} $line command
regsub -all -- {>>>\s|\]$|\r.*} $command {} command
# Execute
send "mark_read $user\r"
send "$command\r"
if {$line == "quit"} {
exit 0
}
sleep 1
send "$terminator\r"
expect "${prompt}${command}\r"
}
# For any other line that's not a sent message
"$terminator\r" {
set line [string trim $expect_out(buffer)]
set line [un_ansi $line]
# send [un_ansi $expect_out(0,string)]
set line [string trimright $line "$terminator\r"]]
set lines_list [split $line "\r"]
set lines_list [lrange $lines_list 0 [expr [llength $lines_list] - 2]]
expect $prompt
foreach message $lines_list {
set message [string trim $message]
# Filter feedbacfeedback
if {[string first "messages marked as read" $message] == -1} {
if {[string first "*** t =" $message] == -1} {
if {[string first "<<<" $message] == -1} {
if {[string first "User*is now o*line" $message] == -1} {
if {$message != ">"} {
send "msg $user $message\r"
expect "Sent:.*\n"
}
}
}
}
}
}
}
}
}
send "quit\r"
expect eof