Просмотр исходного кода

update maketiny.tcl

Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
hwipl 6 лет назад
Родитель
Сommit
3874ff311d
1 измененных файлов с 91 добавлено и 36 удалено
  1. 91 36
      scripts/maketiny.tcl

+ 91 - 36
scripts/maketiny.tcl

@@ -1,61 +1,116 @@
-# cheap tinyurl script to convert ling urls into short urls
-# using http://tinyurl.com´s service
+# maketiny.tcl
+#
+# This script converts URLs into short URLs using https://tinyurl.com's service
+# with the !tinyurl and !tinylast commands.
+#
+# Usage:
+#       !tinyurl url            create tinyurl for url
+#       !tinylast               create tinyurl for last url in channel
+#
+# Enable for a channel with:    .chanset #channel +maketiny
+# Disable for a channel with:   .chanset #channel -maketiny
 
-proc tinyurl_convert {nick host hand chan argv} {
+# tested versions, might run on earlier versions
+package require Tcl 8.6
+package require eggdrop 1.8.4
 
-# set variables
+namespace eval ::maketiny {
+	# channel flag for enabling/disabling
+	setudef flag maketiny
 
-set url [lindex $argv 0]
-set command "|wget --quiet --output-document=- http://tinyurl.com/create.php?url=$url"
-
-# check if there are enough arguments
-
-if { [llength $argv] == 0 } {
-	puthelp "PRIVMSG $nick :Usage: !tinyurl <url>  || Example: !tinyurl http://www.google.com"
-} else {
+	# file name for channel's last url, channel name will be appended
+	variable urlFilePrefix "maketiny_lasturl_"
+}
 
-# open command to create tinyurl and retrieve the html document returned by tinyurl
+# convert given url to short url
+proc ::maketiny::tinyurl {nick host hand chan argv} {
+	set wget "wget --quiet --output-document=-"
+	set command "$wget https://tinyurl.com/create.php?url="
+	set example "Example: !tinyurl http://www.google.com"
+	set usage "Usage: !tinyurl <url>  || $example"
 
-if ![catch {open $command r} input] {
-	while {[gets $input line] >= 0} {
+	# check channel flag if enabled in this channel
+	if {![channel get $chan maketiny]} {
+		return 0
+	}
 
-		# regexp to extract the right part of the html file and the get the tiny url
-		# output tiny url
+	# first argument must be the url
+	if { [llength $argv] == 0 } {
+		puthelp "PRIVMSG $nick :$usage"
+		return 0
+	}
+	set url [lindex $argv 0]
 
-		if [regexp {<input type=hidden name=tinyurl value=\"(.*)\">} $line match tinyurl] {
-				puthelp "PRIVMSG $chan :$tinyurl"
+	# open command to create tinyurl and retrieve the html document
+	# returned by tinyurl
+	set command "|$command$url"
+	set pattern {data-clipboard-text=\"(https+://tinyurl.com/\w+)\".+}
+	set output ""
+	if {![catch {open $command r} input]} {
+		while {[gets $input line] >= 0} {
+			# extract the tiny url from the html file
+			if {[regexp $pattern $line match tinyurl]} {
+				set output $tinyurl
+			}
 		}
+		close $input
+	}
 
+	if {$output == ""} {
+		putlog "Error getting tinyurl."
+		return 0
 	}
-	close $input
-}
-}
+	puthelp "PRIVMSG $chan :$output"
+	return 1
 }
-proc tinyurl_tinylast {nick host hand chan argv} {
-	set urlhistory "url_history_$chan"
-	if ![catch {open $urlhistory r} input] {
+
+# convert last url in channel to short url
+proc ::maketiny::tinylast {nick host hand chan argv} {
+	variable urlFilePrefix
+	set urlFile "$urlFilePrefix$chan"
+
+	# check channel flag if enabled in this channel
+	if {![channel get $chan maketiny]} {
+		return 0
+	}
+
+	# read last url from channel's url file and create tinyurl
+	if {![catch {open $urlFile r} input]} {
 		while {[gets $input line] >= 0} {
 			set url $line
 		}
 		close $input
-		tinyurl_convert $nick $host $hand $chan $url
+		tinyurl $nick $host $hand $chan $url
+		return 1
 	}
+	return 0
 }
 
-proc tinyurl_urlhistory {nick host hand chan argv} {
-	set urlhistory "url_history_$chan"
+# set last url in channel
+proc ::maketiny::lasturl {nick host hand chan argv} {
+	variable urlFilePrefix
+	set urlFile "$urlFilePrefix$chan"
 
-	if {[regexp {(http://\S*)} $argv match url] != 0} {
-		if ![catch {open $urlhistory w} output] {
+	# check channel flag if enabled in this channel
+	if {![channel get $chan maketiny]} {
+		return 0
+	}
+
+	# if message contains url, add it to channel's url file
+	# url is assumed to start with http[s]://
+	set pattern {(https?://\S*)}
+	if {[regexp $pattern $argv match url] != 0} {
+		if {![catch {open $urlFile w} output]} {
 			puts $output $url
 			close $output
+			return 0
 		}
 	}
+	return 1
 }
 
-
-# bind channel command
-
-bind pub - !tinyurl tinyurl_convert
-bind pub - !tinylast tinyurl_tinylast
-bind pubm - * tinyurl_urlhistory
+namespace eval ::maketiny {
+	bind pub - !tinyurl ::maketiny::tinyurl
+	bind pub - !tinylast ::maketiny::tinylast
+	bind pubm - * ::maketiny::lasturl
+}