# -*- coding: utf-8 -*-## Copyright (c) 2010 by Alexander Schremmer <alex@alexanderweb.de># Copyright (c) 2013 by Corey Halpin <chalpin@cs.wisc.edu>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.### (this script requires WeeChat 0.3.6 or newer)## History:# 2014-02-15, Corey Halpin <chalpin@cs.wisc.edu># version 0.5:# * Improve documentation for the 'server' setting# * Change the default for 'server' to bitlbee, as this is probably# what most people use.# 2013-06-24, Priska Herger <policecar@23bit.net># version 0.4: bug fix: if TYPING 0, don't show typing.# 2013-04-27, Corey Halpin <chalpin@cs.wisc.edu># version 0.3:# * Use irc_message_parse to extract nicks# * Send typing = 0 at message completion in private buffers# * Make server, channel, and timeout configurable w/o editing plugin code.# 2010-05-20, Alexander Schremmer <alex@alexanderweb.de># version 0.2: also handle users that do not send a TYPING 0 msg before quitting# removed InfoList code# 2010-05-16, Alexander Schremmer <alex@alexanderweb.de># version 0.1: initial release# Configuration options via /set:## 'channel'# description: Name of your bitlbee channel# command: /set plugins.var.python.bitlbee_typing_notice.channel &bitlbee## 'server'# description: Name of the server running your bitlbee instance. This will# be whatever you type after /connect to get to your bitlbee server# (e.g., localhost, bitlbee).# command: /set plugins.var.python.bitlbee_typing_notice.server bitlbee## 'timeout'# description: Send "not typing" after this many seconds w/o typing.# command: /set plugins.var.python.bitlbee_typing_notice.timeout 4## Note, the plugin must be reloaded after either of these settings are changed.# /python reload works for this.importweechataswimportreSCRIPT_NAME="bitlbee_typing_notice"SCRIPT_AUTHOR="Alexander Schremmer <alex@alexanderweb.de>"SCRIPT_VERSION="0.5"SCRIPT_LICENSE="GPL3"SCRIPT_DESC="Shows when somebody is typing on bitlbee and sends the notice as well"typing={}# Record nicks who sent typing notices. key = subject nick, val = typing level.sending_typing={}# Nicks to whom we're sending typing notices.# key = target nick, val = sequence number used to determine when the typing notice# should be removed.defchannel_has_nick(server,channel,nick):buffer=w.buffer_search("","%s.%s"%(server,channel))returnbool(w.nicklist_search_nick(buffer,"",nick))# Callback which checks for ctcp typing notices sent to us.# Updates typing data, hides the ctcp notices.defctcp_cb(data,modifier,modifier_data,string):ifmodifier_data!=w.config_get_plugin("server"):returnstringmsg_hash=w.info_get_hashtable("irc_message_parse",{"message":string})ifmsg_hash["command"]!="PRIVMSG":returnstringmatch=re.search('\001TYPING ([0-9])\001',msg_hash["arguments"])ifmatch:nick=msg_hash["nick"]typing_level=int(match.groups()[0])iftyping_level==0andnickintyping:deltyping[nick]eliftyping_level>0:typing[nick]=typing_levelw.bar_item_update("bitlbee_typing_notice")return""else:returnstringdefstop_typing(data,signal,signal_data):msg_hash=w.info_get_hashtable("irc_message_parse",{"message":signal_data})ifmsg_hash["nick"]intyping:deltyping[msg_hash["nick"]]w.bar_item_update("bitlbee_typing_notice")returnw.WEECHAT_RC_OKdeftyped_char(data,signal,signal_data):buffer=w.current_buffer()input_s=w.buffer_get_string(buffer,'input')server=w.buffer_get_string(buffer,'localvar_server')channel=w.buffer_get_string(buffer,'localvar_channel')buffer_type=w.buffer_get_string(buffer,'localvar_type')ifserver!=w.config_get_plugin("server")orinput_s.startswith("/"):returnw.WEECHAT_RC_OKifbuffer_type=="private":iflen(input_s)==0:send_typing(channel,0)# Line sent or deleted -- no longer typingelse:send_typing(channel,1)elifchannel==w.config_get_plugin("channel"):nick_completer=w.config_string("weechat.completion.nick_completer")parts=input_s.split(":",1)iflen(parts)>1:nick=parts[0]send_typing(nick,1)returnw.WEECHAT_RC_OKdeftyping_disable_timer(data,remaining_calls):nick,cookie=data.rsplit(":",1)cookie=int(cookie)ifnickinsending_typingandsending_typing[nick]==cookie:send_typing_ctcp(nick,0)delsending_typing[nick]returnw.WEECHAT_RC_OKdefsend_typing_ctcp(nick,level):ifnotchannel_has_nick(w.config_get_plugin("server"),w.config_get_plugin("channel"),nick):returnbuffer=w.buffer_search("irc","%s.%s"%(w.config_get_plugin("server"),w.config_get_plugin("channel")))w.command(buffer,"/mute -all /ctcp %s TYPING %i"%(nick,level))defsend_typing(nick,level):iflevel==0andnickinsending_typing:send_typing_ctcp(nick,0)delsending_typing[nick]eliflevel>0:ifnicknotinsending_typing:send_typing_ctcp(nick,level)cookie=sending_typing.get(nick,0)+1sending_typing[nick]=cookiew.hook_timer(int(1000*float(w.config_get_plugin('timeout'))),0,1,"typing_disable_timer","%s:%i"%(nick,cookie))deftyping_notice_item_cb(data,buffer,args):iftyping:msgs=[]forkey,valueintyping.items():msg=keyifvalue==2:msg+=" (stale)"msgs.append(msg)return"typing: "+", ".join(sorted(msgs))return""# Mainif__name__=="__main__":ifw.register(SCRIPT_NAME,SCRIPT_AUTHOR,SCRIPT_VERSION,SCRIPT_LICENSE,SCRIPT_DESC,"",""):ifnotw.config_get_plugin('channel'):w.config_set_plugin('channel',"&bitlbee")ifnotw.config_get_plugin('server'):w.config_set_plugin('server',"bitlbee")ifnotw.config_get_plugin('timeout'):w.config_set_plugin('timeout',"4")w.hook_signal("input_text_changed","typed_char","")w.hook_signal(w.config_get_plugin("server")+",irc_in_quit","stop_typing","")w.hook_signal(w.config_get_plugin("server")+",irc_in_privmsg","stop_typing","")w.bar_item_new('bitlbee_typing_notice','typing_notice_item_cb','')w.hook_modifier("irc_in_privmsg","ctcp_cb","")