maketiny.tcl 2.9 KB

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