4
0

maketiny.tcl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # maketiny.tcl
  2. #
  3. # This script converts URLs into short URLs using https://tinyurl.com's service
  4. # with the !tinyurl and !tinylast commands.
  5. #
  6. # Usage:
  7. # !tinyurl url create tinyurl for url
  8. # !tinylast create tinyurl for last url in channel
  9. #
  10. # Enable for a channel with: .chanset #channel +maketiny
  11. # Disable for a channel with: .chanset #channel -maketiny
  12. #
  13. # See https://github.com/hwipl/eggdrop-scripts for the latest version and
  14. # additional information including the license (MIT).
  15. # tested versions, might run on earlier versions
  16. package require Tcl 8.6
  17. package require eggdrop 1.8.4
  18. namespace eval ::maketiny {
  19. # channel flag for enabling/disabling
  20. setudef flag maketiny
  21. # file name for channel's last url, channel name will be appended
  22. variable urlFilePrefix "maketiny_lasturl_"
  23. }
  24. # convert given url to short url
  25. proc ::maketiny::tinyurl {nick host hand chan argv} {
  26. set wget "wget --quiet --output-document=-"
  27. set command "$wget https://tinyurl.com/create.php?url="
  28. set example "Example: !tinyurl http://www.google.com"
  29. set usage "Usage: !tinyurl <url> || $example"
  30. # check channel flag if enabled in this channel
  31. if {![channel get $chan maketiny]} {
  32. return 0
  33. }
  34. # first argument must be the url
  35. if { [llength $argv] == 0 } {
  36. puthelp "PRIVMSG $nick :$usage"
  37. return 0
  38. }
  39. set url [lindex $argv 0]
  40. # open command to create tinyurl and retrieve the html document
  41. # returned by tinyurl
  42. set command "|$command$url"
  43. set pattern {data-clipboard-text=\"(https+://tinyurl.com/\w+)\".+}
  44. set output ""
  45. if {![catch {open $command r} input]} {
  46. while {[gets $input line] >= 0} {
  47. # extract the tiny url from the html file
  48. if {[regexp $pattern $line match tinyurl]} {
  49. set output $tinyurl
  50. }
  51. }
  52. close $input
  53. }
  54. if {$output == ""} {
  55. putlog "Error getting tinyurl."
  56. return 0
  57. }
  58. puthelp "PRIVMSG $chan :$output"
  59. return 1
  60. }
  61. # convert last url in channel to short url
  62. proc ::maketiny::tinylast {nick host hand chan argv} {
  63. variable urlFilePrefix
  64. set urlFile "$urlFilePrefix$chan"
  65. # check channel flag if enabled in this channel
  66. if {![channel get $chan maketiny]} {
  67. return 0
  68. }
  69. # read last url from channel's url file and create tinyurl
  70. if {![catch {open $urlFile r} input]} {
  71. while {[gets $input line] >= 0} {
  72. set url $line
  73. }
  74. close $input
  75. tinyurl $nick $host $hand $chan $url
  76. return 1
  77. }
  78. return 0
  79. }
  80. # set last url in channel
  81. proc ::maketiny::lasturl {nick host hand chan argv} {
  82. variable urlFilePrefix
  83. set urlFile "$urlFilePrefix$chan"
  84. # check channel flag if enabled in this channel
  85. if {![channel get $chan maketiny]} {
  86. return 0
  87. }
  88. # if message contains url, add it to channel's url file
  89. # url is assumed to start with http[s]://
  90. set pattern {(https?://\S*)}
  91. if {[regexp $pattern $argv match url] != 0} {
  92. if {![catch {open $urlFile w} output]} {
  93. puts $output $url
  94. close $output
  95. return 0
  96. }
  97. }
  98. return 1
  99. }
  100. namespace eval ::maketiny {
  101. bind pub - !tinyurl ::maketiny::tinyurl
  102. bind pub - !tinylast ::maketiny::tinylast
  103. bind pubm - * ::maketiny::lasturl
  104. putlog "Loaded maketiny.tcl"
  105. }