bitlbee_typing_notice 0.5 mod

From eeew, 2 Years ago, written in Python, viewed 166 times.
URL https://paste.godclan.hu/view/kbBASPlI Embed
Download Paste or View Raw
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (c) 2010 by Alexander Schremmer <alex@alexanderweb.de>
  4. # Copyright (c) 2013 by Corey Halpin <chalpin@cs.wisc.edu>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19.  
  20. #
  21. # (this script requires WeeChat 0.3.6 or newer)
  22. #
  23. # History:
  24. # 2014-02-15, Corey Halpin <chalpin@cs.wisc.edu>
  25. #  version 0.5:
  26. #    * Improve documentation for the 'server' setting
  27. #    * Change the default for 'server' to bitlbee, as this is probably
  28. #      what most people use.
  29. # 2013-06-24, Priska Herger <policecar@23bit.net>
  30. #  version 0.4: bug fix: if TYPING 0, don't show typing.
  31. # 2013-04-27, Corey Halpin <chalpin@cs.wisc.edu>
  32. #  version 0.3:
  33. #    * Use irc_message_parse to extract nicks
  34. #    * Send typing = 0 at message completion in private buffers
  35. #    * Make server, channel, and timeout configurable w/o editing plugin code.
  36. # 2010-05-20, Alexander Schremmer <alex@alexanderweb.de>
  37. #   version 0.2: also handle users that do not send a TYPING 0 msg before quitting
  38. #                removed InfoList code
  39. # 2010-05-16, Alexander Schremmer <alex@alexanderweb.de>
  40. #   version 0.1: initial release
  41.  
  42.  
  43. # Configuration options via /set:
  44. #
  45. # 'channel'
  46. #   description: Name of your bitlbee channel
  47. #   command: /set plugins.var.python.bitlbee_typing_notice.channel &bitlbee
  48. #
  49. # 'server'
  50. #   description: Name of the server running your bitlbee instance.  This will
  51. #     be whatever you type after /connect to get to your bitlbee server
  52. #     (e.g., localhost, bitlbee).
  53. #   command: /set plugins.var.python.bitlbee_typing_notice.server bitlbee
  54. #
  55. # 'timeout'
  56. #   description: Send "not typing" after this many seconds w/o typing.
  57. #   command: /set plugins.var.python.bitlbee_typing_notice.timeout 4
  58. #
  59. # Note, the plugin must be reloaded after either of these settings are changed.
  60. # /python reload works for this.
  61.  
  62.  
  63. import weechat as w
  64. import re
  65.  
  66. SCRIPT_NAME    = "bitlbee_typing_notice"
  67. SCRIPT_AUTHOR  = "Alexander Schremmer <alex@alexanderweb.de>"
  68. SCRIPT_VERSION = "0.5"
  69. SCRIPT_LICENSE = "GPL3"
  70. SCRIPT_DESC    = "Shows when somebody is typing on bitlbee and sends the notice as well"
  71.  
  72. typing = {} # Record nicks who sent typing notices.  key = subject nick, val = typing level.
  73.  
  74. sending_typing = {} # Nicks to whom we're sending typing notices.
  75. #  key = target nick, val = sequence number used to determine when the typing notice
  76. #    should be removed.
  77.  
  78. typing_disable_timer = None
  79.  
  80. def channel_has_nick(server, channel, nick):
  81.     buffer = w.buffer_search("", "%s.%s" % (server, channel))
  82.     return bool(w.nicklist_search_nick(buffer, "", nick))
  83.  
  84. # Callback which checks for ctcp typing notices sent to us.
  85. # Updates typing data, hides the ctcp notices.
  86. def ctcp_cb(data, modifier, modifier_data, string):
  87.     if modifier_data != w.config_get_plugin("server"):
  88.         return string
  89.     msg_hash = w.info_get_hashtable(
  90.         "irc_message_parse", {"message": string} )
  91.     if msg_hash["command"] != "PRIVMSG":
  92.         return string
  93.     match = re.search('\001TYPING ([0-9])\001', msg_hash["arguments"])
  94.     if match:
  95.         nick = msg_hash["nick"]
  96.         typing_level = int(match.groups()[0])
  97.         if typing_level == 0 and nick in typing:
  98.             del typing[nick]
  99.         elif typing_level > 0:
  100.             typing[nick] = {"level": typing_level, "timeout": 15}
  101.             run_typing_timer()
  102.         w.bar_item_update("bitlbee_typing_notice")
  103.         return ""
  104.     else:
  105.         return string
  106.  
  107.  
  108. def stop_typing(data, signal, signal_data):
  109.     msg_hash = w.info_get_hashtable(
  110.         "irc_message_parse", {"message": signal_data } )
  111.     if msg_hash["nick"] in typing:
  112.         del typing[msg_hash["nick"]]
  113.     w.bar_item_update("bitlbee_typing_notice")
  114.     return w.WEECHAT_RC_OK
  115.  
  116. def typed_char(data, signal, signal_data):
  117.     buffer = w.current_buffer()
  118.     input_s = w.buffer_get_string(buffer, 'input')
  119.     server = w.buffer_get_string(buffer, 'localvar_server')
  120.     channel = w.buffer_get_string(buffer, 'localvar_channel')
  121.     buffer_type = w.buffer_get_string(buffer, 'localvar_type')
  122.  
  123.     if buffer_type == "server" or server != w.config_get_plugin("server") or input_s.startswith("/"):
  124.         return w.WEECHAT_RC_OK
  125.     if buffer_type == "private":
  126.         if len(input_s)==0:
  127.             send_typing(channel, 0) # Line sent or deleted -- no longer typing
  128.         else:
  129.             send_typing(channel, 1)
  130.     elif channel == w.config_get_plugin("channel"):
  131.         #nick_completer = w.config_string("weechat.completion.nick_completer")
  132.         parts = input_s.split(":", 1)
  133.         if len(parts) > 1:
  134.             nick = parts[0]
  135.             send_typing(nick, 1)
  136.     #w.prnt(w.current_buffer(), "dbg typed_char")
  137.     return w.WEECHAT_RC_OK
  138.  
  139.  
  140. def typing_disable_timer_cb(data, remaining_calls):
  141.     #nick, cookie = data.rsplit(":", 1)
  142.     #cookie = int(cookie)
  143.     #if nick in sending_typing and sending_typing[nick]==cookie:
  144.     #    send_typing_ctcp(nick, 0)
  145.     #    del sending_typing[nick]
  146.     #return w.WEECHAT_RC_OK
  147.     for nick, seconds in sending_typing.items():
  148.         if seconds <= 1:
  149.             send_typing_ctcp(nick, 0)
  150.             del sending_typing[nick]
  151.         else:
  152.             sending_typing[nick] = seconds - 1
  153.  
  154.     for nick, data in typing.items():
  155.         data["timeout"] -= 1
  156.         if data["timeout"] < 1:
  157.             del typing[nick]
  158.  
  159.     if not sending_typing and not typing:
  160.         global typing_disable_timer
  161.         w.unhook(typing_disable_timer)
  162.         typing_disable_timer = None
  163.  
  164.     return w.WEECHAT_RC_OK
  165.  
  166. def run_typing_timer():
  167.     global typing_disable_timer
  168.     if not typing_disable_timer:
  169.         typing_disable_timer = w.hook_timer(1000, 0, 0, "typing_disable_timer_cb", "")
  170.  
  171. def send_typing_ctcp(nick, level):
  172.     #if not channel_has_nick(w.config_get_plugin("server"),
  173.     #                        w.config_get_plugin("channel"), nick):
  174.     #    return
  175.     buffer = w.buffer_search("irc", "server.%s" % (w.config_get_plugin("server"),))
  176.     if not buffer:
  177.         return
  178.     w.command(buffer, "/mute -all /ctcp %s TYPING %i" % (nick, level))
  179.     #w.prnt(w.current_buffer(), "dbg send_typing_ctcp %s %i" % (nick, level))
  180.  
  181.  
  182. def send_typing(nick, level):
  183.     #w.prnt(w.current_buffer(), "dbg send_typing %s %i %s" % (nick, level, repr(sending_typing)))
  184.     if level == 0 and nick in sending_typing:
  185.         send_typing_ctcp(nick, 0)
  186.         del sending_typing[nick]
  187.     elif level > 0:
  188.         if nick not in sending_typing:
  189.             send_typing_ctcp(nick, level)
  190.         #cookie = sending_typing.get(nick, 0) + 1
  191.         #sending_typing[nick] = cookie
  192.         #w.hook_timer( int(1000 * float(w.config_get_plugin('timeout'))), 0, 1,
  193.         #              "typing_disable_timer", "%s:%i" % (nick, cookie))
  194.         sending_typing[nick] = int(w.config_get_plugin('timeout'))
  195.         run_typing_timer()
  196.  
  197.  
  198. def typing_notice_item_cb(data, buffer, args):
  199.     if typing:
  200.         msgs = []
  201.         for key, value in typing.items():
  202.             msg = key
  203.             if value["level"] == 2:
  204.                 msg += " (stale)"
  205.             msgs.append(msg)
  206.         return "typing: " + ", ".join(sorted(msgs))
  207.     return ""
  208.  
  209.  
  210. # Main
  211. if __name__ == "__main__":
  212.     if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
  213.                     SCRIPT_DESC, "", ""):
  214.  
  215.         if not w.config_get_plugin('channel'):
  216.             w.config_set_plugin('channel', "&bitlbee")
  217.         if not w.config_get_plugin('server'):
  218.             w.config_set_plugin('server', "bitlbee")
  219.         if not w.config_get_plugin('timeout'):
  220.             w.config_set_plugin('timeout', "4")
  221.  
  222.         w.hook_signal("input_text_changed", "typed_char", "")
  223.         w.hook_signal(w.config_get_plugin("server")+",irc_in_quit", "stop_typing", "")
  224.         w.hook_signal(w.config_get_plugin("server")+",irc_in_privmsg", "stop_typing", "")
  225.         w.bar_item_new('bitlbee_typing_notice', 'typing_notice_item_cb', '')
  226.         w.hook_modifier("irc_in_privmsg", "ctcp_cb", "")
  227.         #w.prnt(w.current_buffer(), "dbg bitlbee typing notice")

Reply to "bitlbee_typing_notice 0.5 mod"

Here you can reply to the paste above