-
Notifications
You must be signed in to change notification settings - Fork 2
/
topic.tcl
155 lines (138 loc) · 3.87 KB
/
topic.tcl
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
# topic toggler
# (c)2012 cr0n
package require http
package require tls
::http::register https 443 [list ::tls::socket -tls1 1]
set vers "0.3"
set pinghost "your.ping.host"
set pingbin "/bin/ping6"
set sre {(?i)\y(h(?:oe|ö)hle:?\s+)(up|down|unknown|broken)\y}
set pre {(?i)\y(pinghost:?\s+)(up|down)\y}
set statusurl "http://your-server.tld/api"
set statuschan "#your-channel"
set timer 1
bind pub - !hup pub:set:hup
bind pub - !hdown pub:set:hdown
bind pub - !htoggle pub:set:htoggle
bind pub - !hinit pub:hinit
bind pub - !hauto pub:hinit
bind pub - !hstop pub:hstop
bind time - "*" auto:check:status
bind time - "*" auto:check:ping
proc int:disable:timer {c} {
global timer
if {$timer == 1} {
set timer 0
putserv "PRIVMSG $c :auto topic disabled"
}
}
proc pub:hstop {n u h c a} {
global timer
if {$timer == 0} {
putserv "PRIVMSG $c :auto topic already disabled"
} else {
int:disable:timer $c
}
}
proc pub:hinit {n u h c a} {
global timer
if {$timer != 1} {
set timer 1
putserv "PRIVMSG $c :auto topic enabled"
} else {
putserv "PRIVMSG $c :auto topic already enabled"
}
}
proc auto:check:status {m h d w y} {
global statusurl statuschan timer
if {$timer == 1} {
set token [::http::geturl $statusurl -timeout 10000]
set code [::http::ncode $token]
set status [::http::status $token]
if {$code == 200 && $status == "ok"} {
set data [::http::data [::http::geturl $statusurl]]
if {$data == "1"} {
int:set:status "up" $statuschan quiet
} elseif {$data == "0"} {
int:set:status "down" $statuschan quiet
} elseif {$data == "?"} {
int:set:status "unknown" $statuschan quiet
}
} else {
int:set:status "broken" $statuschan quiet
}
}
}
proc int:toggle:status {status} {
if {$status == "up"} {
return "down"
} else {
return "up"
}
}
proc pub:set:htoggle {n u h c a} {
global sre
set status ""
set topic [topic $c]
if {[regexp $sre $topic -> -> status]} {
set topic [regsub $sre $topic "\\1[int:toggle:status [string tolower $status]]"]
putserv "TOPIC $c :$topic"
int:disable:timer $c
} else {
putserv "PRIVMSG $c :no hoehle status section found in topic"
}
}
proc int:ping:host {pinghost} {
global pingbin
if {[catch {exec $pingbin -c 3 $pinghost >/dev/null 2>@1} result]} {
return "down"
} else {
return "up"
}
}
proc auto:check:ping {m h d w y} {
global pre pinghost statuschan timer
if {$timer == 1} {
set status ""
set newstatus [int:ping:host $pinghost]
set topic [topic $statuschan]
set ret 1
if {[regexp $pre $topic -> -> status]} {
if {[string tolower $status] != $newstatus} {
set topic [regsub $pre $topic "\\1$newstatus"]
putserv "TOPIC $statuschan :$topic"
}
}
}
}
proc int:set:status {newstatus c mode} {
global sre
set status ""
set topic [topic $c]
set ret 1
if {[regexp $sre $topic -> -> status]} {
if {[string tolower $status] == $newstatus} {
if {$mode != "quiet"} {
putserv "PRIVMSG $c :see topic :P"
}
} else {
set topic [regsub $sre $topic "\\1$newstatus"]
putserv "TOPIC $c :$topic"
set ret 0
}
} elseif {$mode != "quiet"} {
putserv "PRIVMSG $c :no hoehle status section found in topic"
}
return $ret
}
proc pub:set:hdown {n u h c a} {
if {[int:set:status down $c noisy] == 0} {
int:disable:timer $c
}
}
proc pub:set:hup {n u h c a} {
if {[int:set:status up $c noisy] == 0} {
int:disable:timer $c
}
}
putlog "Status Topic v$vers successfully loaded."