-
Notifications
You must be signed in to change notification settings - Fork 6
/
urltitle.tcl
428 lines (387 loc) · 13.6 KB
/
urltitle.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
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
setctx lains
global urltitle
set urltitle(timeout) 2500 ;# geturl timeout (1/1000ths of a second)
set urltitle(length) 8
setudef flag urltitle
package require http ;# You need the http package..
package require bee
package require tls
http::register https 443 [list ::tls::socket -tls1 1]
bind pub -|- ".url" pub:urltitle
bind pub -|- "|url" pubn:urltitle
proc pubn:urltitle {nick host user chan text} {
global urltitle
set word [lindex $text 0]
if {![regexp -nocase {^((f|ht)tp(s|)://|www\.[^\.]+\.)} $word] || \
[regexp {://([^/:]*:([^/]*@|\d+(/|$))|.*/\.)} $word]} {
putnotc $nick "That isn't a valid url?"
return;
}
set word [string tolower $word 0 [string wordend $word 0]]
set urtitle [urltitle $word 1]
putnotc $nick "$urtitle"
}
proc pub:urltitle {nick host user chan text} {
global urltitle
set word [lindex $text 0]
if {![regexp -nocase {^((f|ht)tp(s|)://|www\.[^\.]+\.)} $word] || \
[regexp {://([^/:]*:([^/]*@|\d+(/|$))|.*/\.)} $word]} {
putnotc $nick "That isn't a valid url?"
return;
}
set word [string tolower $word 0 [string wordend $word 0]]
set urtitle [urltitle $word 1]
if {[lsearch -exact [channel info $chan] "urltitle"] != -1} {
putchan $chan "$urtitle"
} else {
putnotc $nick "$urtitle"
}
}
bind pubm -|- "*" pubm:urltitle
proc pubm:urltitle {nick host user chan text} {
if {[string match -nocase "*bot*" $nick]} { return }
if {[string match -nocase "*github*" $nick]} { return }
if {[string match -nocase "*script*" $nick]} { return }
if {[string match -nocase "crow" $nick]} { return }
if {[khfloodc $nick] >= 1} { return }
global urltitle
if {[lsearch -exact [channel info $chan] "urltitle"] != -1} {
foreach word [split $text] {
if {[string match -nocase "*pastie.org*" $word]} { break }
if {[string match -nocase "*pastebin*" $word]} { break }
if {[string match -nocase "*google*" $word]} { break }
if {[string match -nocase "*wiki*" $word]} { break }
if {[string length $word] >= $urltitle(length) && \
[regexp -nocase {^((f|ht)tp(s|)://|www\.[^\.]+\.)} $word] && \
![regexp {://([^/:]*:([^/]*@|\d+(/|$))|.*/\.)} $word]} {
set word [string tolower $word 0 [string wordend $word 0]]
set urtitle [urltitle $word]
if {[string length $urtitle]} {
if {$urtitle == 0} { break }
if {[string match -nocase "*Title:*" [lindex [split ${urtitle} { }] 0]]} {
set rwords [split [lrange [split ${urtitle} { }] 1 end] { }]
set rwordc 1
foreach rword $rwords {
if {[string length ${rword}] < 3} {
incr rwordc
} else {
if {[string match -nocase "*${rword}*" $word]} { incr rwordc }
}
}
if {$rwordc >= [llength $rwords]} { break }
}
if {[khflood $nick] >= 1} { return }
set s [expr {[string index $nick end]!="s"?"s":""}];
putserv "PRIVMSG $chan :[dehighlight $nick]'$s URL: $urtitle"
}
putlog "<$nick:$chan> $word -> $urtitle"
break
}
}
}
return 1
}
proc urltitle {url {nolookup 0} {loop 0}} {
global urltitle
set botcolour 04
#Check to see if we're stuck in some kind of endless redirect loop.
if {$loop >= 6} {
return "Forwards oddly..."
} else {
incr loop 1
}
#Starting header request, this will normally return a status code, and location information
if {[info exists url] && [string length $url]} {
putlog "urltitle: Fetching title for $url, attempt $loop"
set prehttp [urltitle:getsocket $url 1]
upvar #0 $prehttp prestate
if {[info exists prestate(url)] == 0} { return $prehttp }
set type [lindex [split $prestate(type) ";"] 0]
set size $prestate(totalsize)
set status [::http::ncode $prehttp]
array set meta [set ${prehttp}(meta)]
if { $type == "text/html" } {
if {$status == 301 || $status == 302 || $status == 303 || $status == 307} {
#Chuck the redirect handling over to a more specialised method
return [urltitle:handleforward $prehttp $nolookup $loop]
} else {
#Parsing bit html files is done elsewhere.
::http::cleanup $prehttp
return [urltitle:html $url $nolookup $loop]
}
} elseif {$type == "application/x-bittorrent"} {
if {$nolookup == 1} { return "Location: $url" }
#Parsing bit torrent files is done elsewhere.
::http::cleanup $prehttp
return [urltitle:bittorrent $url $loop $type $size]
} else {
if {$nolookup == 1} { return "Location: $url" }
#No parser... so lets return some useful info.
::http::cleanup $prehttp
if {$size > 0} { set size [bytes $size] } else { set size "Unknown" }
set filename [lindex [split $url {/}] end]
foreach met [array names meta] {
if {[string match -nocase "*filename*" $meta($met)] == 1} {
set fn [lindex [split $meta($met) "\""] end-1]
if {[string match "*.*" $fn] == 1} { set filename $fn; break }
set fn [lindex [split $meta($met) {=}] end]
if {[string match "*.*" $fn] == 1} { set filename $fn; break }
}
}
if {[string match "*.*" $filename] == 0} { set filename "Unknown" }
return "\003${botcolour}Type:\003 $type \003${botcolour}Filename:\003 $filename \003${botcolour}Size:\003 $size"
#return "\003${botcolour}Type:\003 $type \003${botcolour}Size:\003 $size"
}
}
}
proc urltitle:html {url nolookup loop} {
global urltitle
set botcolour 04
set http [urltitle:getsocket $url 0]
upvar #0 $http state
if {[info exists state(url)] == 0} { return $http }
set data [::http::data $http]
set status [::http::ncode $http]
array set meta [set ${http}(meta)]
set title ""
if {$status == 301 || $status == 302 || $status == 303 || $status == 307} {
#Chuck the redirect handling over to a more specialised method
return [urltitle:handleforward $http $nolookup $loop]
} else {
if {$nolookup == 1} { return "Location: $url" }
if {[regexp -nocase {<title>(.*?)</title>} $data match title]} {
set return "\003${botcolour}Title:\003 [urldecode [string map { {href=} "" \" "" \n " " } $title]]"
} else {
if {$status != 200} {
set return "Returned a $status code..."
} else {
set return "No page title found..."
}
}
}
::http::cleanup $http
if {$loop == 1} {
return "$return"
} else {
return "\[After $loop hops\] $return"
}
}
proc urltitle:bittorrent {url loop type size} {
global urltitle
set botcolour 04
if {$size > 512000} {
return "Bittorrent file exceeds size cap, $type [bytes $size]"
}
set http [urltitle:getsocket $url 0]
upvar #0 $http state
if {[info exists state(url)] == 0} { return $http }
catch {set data [::bee::decode [::http::data $http]]} error
if {[string match -nocase "*not large enough for value*" $error]} {
return "Bittorrent file, unable to parse, $type [bytes $size]"
}
set return "\003${botcolour}Type:\003 BitTorrent "
set announce [lsearch $data "announce"]
if {$announce != -1} {
append return "\003${botcolour}Tracker:\003 [lindex $data [expr {$announce + 1}]] "
}
set title [lsearch $data "title"]
if {$title != -1} {
append return "\003${botcolour}Title:\003 [lindex $data [expr {$title + 1}]] "
}
set info [lsearch $data "info"]
if {$info != -1} {
set info "[lindex $data [expr {$info + 1}]]"
set name [lsearch $info "name"]
if {$name != -1} {
append return "\003${botcolour}Name:\003 [lindex $info [expr {$name + 1}]] "
}
set length [lsearch $info "length"]
if {$length != -1} {
append return "\003${botcolour}File size:\003 [bytes [lindex $info [expr {$length + 1}]]] "
}
set files [lsearch $info "files"]
if {$files != -1 && $length == -1} {
set files "[lindex $info [expr {$files + 1}]]"
set length 0
set filecount 0
foreach file $files {
incr filecount
set llength [lsearch $file "length"]
if {$llength != -1} {
set length [expr {double($length) + [lindex $file [expr {$llength + 1}]]}]
}
}
append return "\003${botcolour}Total Size:\003 [bytes $length] "
append return "\003${botcolour}Files:\003 $filecount "
}
}
return $return
}
proc urltitle:getsocket { url validate } {
global urltitle
catch { set prehttp [::http::geturl $url -validate $validate -timeout $::urltitle(timeout)]} error
if { [info exists prehttp] == "0" } {
putmainlog "Title Error: $error - $url"
return 0
}
if {[string match -nocase "*couldn't open socket*" $error]} {
::http::cleanup $prehttp
return "Couldn't connect..."
}
if { [::http::status $prehttp] == "timeout" } {
::http::cleanup $prehttp
return "Timed out..."
}
return $prehttp
}
proc urltitle:handleforward { http nolookup loop } {
set data [::http::data $http]
set status [::http::ncode $http]
array set meta [set ${http}(meta)]
set url [set ${http}(url)]
set url [urltitle:urlsplit $url]
if {[info exists meta(Location)]} {
set location $meta(Location)
} elseif {[info exists meta(location)]} {
set location $meta(location)
} else {
::http::cleanup $http
return "Forwards to an invalid location"
}
if {![urltitle:urlsplit $location 1]} {
set location "[lindex $url 0]$location"
}
set return "[urltitle $location $nolookup $loop] "
if { $return == "0" } {
set return "Forwards to $meta(Location)"
}
::http::cleanup $http
return $return
}
proc urltitle:urlsplit {url {toggle 0}} {
set URLmatcher {(?x) # this is _expanded_ syntax
^
(?: (\w+) : ) ? # <protocol scheme>
(?: //
(?:
(
[^@/\#?]+ # <userinfo part of authority>
) @
)?
( [^/:\#?]+ ) # <host part of authority>
(?: : (\d+) )? # <port part of authority>
)?
( / [^\#]*)? # <path> (including query)
(?: \# (.*) )? # <fragment>
$
}
if {![regexp -- $URLmatcher $url -> proto user host port srvurl]} { return 0 }
if {$host eq ""} { return 0 }
if {$port eq ""} { set port 80 }
if {$toggle == 1} { return 1 }
return "${proto}://${host}:$port $srvurl"
}
proc dehighlight {content} {
set escapes {
A \xc5 E \xc9 I \xcd O \xd6
a \xe5 e \xeb i \xed o \xf6
};
set content [string map $escapes $content];
}
proc bytes { text } {
for {set fsize [expr {double($text)}]; set pos 0} {$fsize >= 1024} {set fsize [expr {$fsize / 1024}]} {
incr pos;
}
set a [lindex [list "B" "KiB" "MiB" "GiB" "TiB" "PiB" "EiB" "ZiB" "YiB"] $pos];
set fsize [format "%.2f%s" $fsize $a];
return "$fsize"
}
proc urldecode {content} {
set escapes {
\x20 " \x22 & \x26 ' \x27 – \x2D
< \x3C > \x3E ˜ \x7E € \x80 ¡ \xA1
¢ \xA2 £ \xA3 ¤ \xA4 ¥ \xA5 ¦ \xA6
§ \xA7 ¨ \xA8 © \xA9 ª \xAA « \xAB
¬ \xAC ­ \xAD ® \xAE &hibar; \xAF ° \xB0
± \xB1 ² \xB2 ³ \xB3 ´ \xB4 µ \xB5
¶ \xB6 · \xB7 ¸ \xB8 ¹ \xB9 º \xBA
» \xBB ¼ \xBC ½ \xBD ¾ \xBE ¿ \xBF
À \xC0 Á \xC1 Â \xC2 Ã \xC3 Ä \xC4
Å \xC5 Æ \xC6 Ç \xC7 È \xC8 É \xC9
Ê \xCA Ë \xCB Ì \xCC Í \xCD Î \xCE
Ï \xCF Ð \xD0 Ñ \xD1 Ò \xD2 Ó \xD3
Ô \xD4 Õ \xD5 Ö \xD6 × \xD7 Ø \xD8
Ù \xD9 Ú \xDA Û \xDB Ü \xDC Ý \xDD
Þ \xDE ß \xDF à \xE0 á \xE1 â \xE2
ã \xE3 ä \xE4 å \xE5 æ \xE6 ç \xE7
è \xE8 é \xE9 ê \xEA ë \xEB ì \xEC
í \xED î \xEE ï \xEF ð \xF0 ñ \xF1
ò \xF2 ó \xF3 ô \xF4 õ \xF5 ö \xF6
÷ \xF7 ø \xF8 ù \xF9 ú \xFA û \xFB
ü \xFC ý \xFD þ \xFE ÿ \xFF
};
set content [string map $escapes $content];
set content [string map [list "\]" "\\\]" "\[" "\\\[" "\$" "\\\$" "\\" "\\\\"] $content];
regsub -all -- {&#([[:digit:]]{1,5});} $content {[format %c [string trimleft "\1" "0"]]} content;
regsub -all -- {&#x([[:xdigit:]]{1,4});} $content {[format %c [scan "\1" %x]]} content;
regsub -all -- {&#?[[:alnum:]]{2,7};} $content "?" content;
while {[regsub -all -- {\ \ } $content " " content] > 0} { }
return [subst $content];
}
global khfloodlines khfloodin khflood_array khfloodlinespub
set khfloodlines 8
set khfloodlinespub 3
set khfloodin 60
variable khflood_array
if { [info exists khflood_array] == 1} { unset khflood_array }
proc khflood {nick} {
global khfloodin khfloodlines khfloodlinespub khflood_array botnick
if { [info exists khflood_array($nick,0)] == 0} {
set i [expr {$khfloodlines - 1}]
set khflood_array($nick,warn) 0
while {$i >= 0} {
set khflood_array($nick,$i) 0
incr i -1
}
return 0
}
set i [expr {${khfloodlines} - 1}]
while {$i >= 1} {
set khflood_array($nick,$i) $khflood_array($nick,[expr {$i - 1}])
incr i -1
}
set khflood_array($nick,0) [unixtime]
if {[expr [unixtime] - $khflood_array($nick,[expr {${khfloodlinespub} - 1}])] <= ${khfloodin}} {
if {[expr [unixtime] - $khflood_array($nick,[expr {${khfloodlines} - 1}])] <= ${khfloodin}} {
return 2
}
return 1
} else {
return 0
}
}
proc khfloodc {nick} {
global khfloodin khfloodlines khfloodlinespub khflood_array botnick
if { [info exists khflood_array($nick,0)] == 0} {
return 0
}
if {[expr [unixtime] - $khflood_array($nick,[expr {${khfloodlinespub} - 1}])] <= ${khfloodin}} {
if {[expr [unixtime] - $khflood_array($nick,[expr {${khfloodlines} - 1}])] <= ${khfloodin}} {
return 2
}
return 1
} else {
return 0
}
}
proc strip-html {html} {
set m {[][\;\$]}
regsub -all $m $html \\\\& html
foreach i $html {
regsub -all -- {<[^>]*>} $i "" i
set i [subst $i]
lappend html2 $i
}
return [join $html2]
}
return "URL parser is loaded-dattebayo"