shortenurl.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Copyright (c) 2010 by John Anderson <sontek@gmail.com>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. # History
  16. # 2011-10-24, Dmitry Geurkov <dmitry_627@mail.ru>
  17. # version 0.4.1: added: option "ignore_list" for a blacklist of shorten urls.
  18. # 2011-01-17, nils_2 <weechatter@arcor.de>
  19. # version 0.4: URI will be shorten in /query, too.
  20. # : added: option "short_own".
  21. # 2010-11-08, John Anderson <sontek@gmail.com>:
  22. # version 0.3: Get python 2.x binary for hook_process (fixes problem
  23. # when python 3.x is default python version, requires
  24. # WeeChat >= 0.3.4)
  25. import re
  26. import weechat
  27. from urllib import urlencode
  28. from urllib2 import urlopen
  29. SCRIPT_NAME = "shortenurl"
  30. SCRIPT_AUTHOR = "John Anderson <sontek@gmail.com>"
  31. SCRIPT_VERSION = "0.4.1"
  32. SCRIPT_LICENSE = "GPL3"
  33. SCRIPT_DESC = "Shorten long incoming and outgoing URLs"
  34. ISGD = 'http://is.gd/api.php?%s'
  35. TINYURL = 'http://tinyurl.com/api-create.php?%s'
  36. # script options
  37. # shortener options:
  38. # - isgd
  39. # - tinyurl
  40. settings = {
  41. "color": "red",
  42. "urllength": "30",
  43. "shortener": "isgd",
  44. "public": "off",
  45. "short_own": "off",
  46. "ignore_list": "",
  47. }
  48. octet = r'(?:2(?:[0-4]\d|5[0-5])|1\d\d|\d{1,2})'
  49. ipAddr = r'%s(?:\.%s){3}' % (octet, octet)
  50. # Base domain regex off RFC 1034 and 1738
  51. label = r'[0-9a-z][-0-9a-z]*[0-9a-z]?'
  52. domain = r'%s(?:\.%s)*\.[a-z][-0-9a-z]*[a-z]?' % (label, label)
  53. urlRe = re.compile(r'(\w+://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % (domain, ipAddr), re.I)
  54. if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
  55. SCRIPT_DESC, "", ""):
  56. for option, default_value in settings.iteritems():
  57. if weechat.config_get_plugin(option) == "":
  58. weechat.config_set_plugin(option, default_value)
  59. # Hooks we want to hook
  60. hook_command_run = {
  61. "input" : ("/input return", "command_input_callback"),
  62. }
  63. # Hook all hooks !
  64. for hook, value in hook_command_run.iteritems():
  65. weechat.hook_command_run(value[0], value[1], "")
  66. weechat.hook_print("", "notify_message", "://", 1, "hook_print_callback", "")
  67. weechat.hook_print("", "notify_private", "://", 1, "hook_print_callback", "")
  68. weechat.hook_print("", "notify_highlight", "://", 1, "hook_print_callback", "")
  69. def command_input_callback(data, buffer, command):
  70. """ Function called when a command "/input xxxx" is run """
  71. if command == '/input return':
  72. input = weechat.buffer_get_string(buffer, 'input')
  73. input = match_url(input, buffer, True)
  74. weechat.buffer_set(buffer, 'input', input)
  75. return weechat.WEECHAT_RC_OK
  76. def hook_print_callback(data, buffer, date, tags, displayed, highlight, prefix, message):
  77. if weechat.config_get_plugin('short_own') == 'on':
  78. # get servername
  79. infolist = weechat.infolist_get('buffer',buffer,'')
  80. weechat.infolist_next(infolist)
  81. servername,undef = weechat.infolist_string(infolist,'name').split('.',1)
  82. weechat.infolist_free(infolist)
  83. # get own nick
  84. my_nick = weechat.info_get( 'irc_nick', servername )
  85. if my_nick in tags:
  86. return weechat.WEECHAT_RC_OK
  87. return match_url(message, buffer, False)
  88. return weechat.WEECHAT_RC_OK
  89. def match_url(message, buffer, from_self):
  90. new_message = message
  91. for url in urlRe.findall(message):
  92. if len(url) > int(weechat.config_get_plugin('urllength')) and not ignore_url(url):
  93. if from_self:
  94. public = weechat.config_get_plugin('public')
  95. if public == 'on':
  96. short_url = tiny_url(url, None)
  97. new_message = new_message.replace(url, short_url)
  98. else:
  99. tiny_url(url, buffer)
  100. if from_self:
  101. return new_message
  102. else:
  103. return weechat.WEECHAT_RC_OK
  104. def tiny_url(url, buffer):
  105. shortener = weechat.config_get_plugin('shortener')
  106. if shortener == 'isgd':
  107. url = ISGD % urlencode({'longurl':url})
  108. if shortener == 'tinyurl':
  109. url = TINYURL % urlencode({'url':url})
  110. try:
  111. if buffer:
  112. python2_bin = weechat.info_get('python2_bin', '') or 'python'
  113. shortenurl_hook_process = weechat.hook_process(
  114. python2_bin + " -c \"import urllib2; print urllib2.urlopen('" + url + "').read()\"",
  115. 10 * 1000, "process_complete", buffer)
  116. else:
  117. return urlopen(url).read()
  118. except:
  119. return url
  120. def ignore_url(url):
  121. ignorelist = weechat.config_get_plugin('ignore_list').split(',')
  122. for ignore in ignorelist:
  123. if len(ignore) > 0 and ignore in url:
  124. return True
  125. return False
  126. def process_complete(data, command, rc, stdout, stderr):
  127. url = stdout.strip()
  128. if url:
  129. color = weechat.color(weechat.config_get_plugin("color"))
  130. weechat.prnt(data, '%s[%s]' % (color, url))
  131. return weechat.WEECHAT_RC_OK