about summary refs log tree commit diff
path: root/weechat/.weechat/python
diff options
context:
space:
mode:
Diffstat (limited to 'weechat/.weechat/python')
-rw-r--r--weechat/.weechat/python/apply_corrections.py351
-rw-r--r--weechat/.weechat/python/autojoin.py177
l---------weechat/.weechat/python/autoload/apply_corrections.py1
l---------weechat/.weechat/python/autoload/autojoin.py1
l---------weechat/.weechat/python/autoload/grep.py1
l---------weechat/.weechat/python/autoload/otr.py1
l---------weechat/.weechat/python/autoload/screen_away.py1
l---------weechat/.weechat/python/autoload/urlview.py1
-rw-r--r--weechat/.weechat/python/grep.py1731
-rw-r--r--weechat/.weechat/python/otr.py2062
-rw-r--r--weechat/.weechat/python/screen_away.py197
-rw-r--r--weechat/.weechat/python/urlview.py57
12 files changed, 4581 insertions, 0 deletions
diff --git a/weechat/.weechat/python/apply_corrections.py b/weechat/.weechat/python/apply_corrections.py
new file mode 100644
index 0000000..2fd8b5c
--- /dev/null
+++ b/weechat/.weechat/python/apply_corrections.py
@@ -0,0 +1,351 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2012 Chris Johnson <raugturi@gmail.com>
+#
+# 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/>.
+#
+
+# apply_corrections
+#
+# A weechat plugin to re-print a user's messages with corrections applied when
+# they send a correction string (ex: s/typo/replacement).
+#
+# The following options are availalble:
+#
+# check_every: Interval, in seconds, between each check for expired messages.
+#              If set to 0 no check will be performed and all messages will be
+#              saved indefinitely.
+#
+# data_timeout: Time, in seconds, before a message is expired. If set to 0
+#               messages will never expire.
+#
+# message_limit: Number of messages to store per nick. If set to 0 all messages
+#                will be saved until they expire.
+#
+# print_format: Format string for the printed corrections.
+#               Default: "[nick]: [corrected]"
+#
+#               Variables allowed:
+#
+#               [nick]:        The nick of the person who sent the messages.
+#               [corrected]:   The corrected text of the previous message(s).
+#               [correction]:  The correction (format: s/typo/replacement).
+#               [original]:    The original message before correction.
+#               [pattern]:     The "typo" portion of the correction.
+#               [replacement]: The "replacement" portion of the correction.
+#               [timestamp]:   The timestamp of the original message.
+#
+# print_limit: Maximum number of lines to correct and print to the buffer. If
+#              set to 0 all lines that match the pattern will be printed.
+#
+# Note: Setting check_every, data_timeout, message_limit, or some combination
+# thereof to 0's will eat a lot of memory very quickly as it will drastically
+# increase the size of the dictionary holding previous messages.  Likewise,
+# setting print_limit to 0 with a large number of saved messages will quickly
+# fill your screen with a bunch of stuff should someone submit a generic
+# enough correction string.
+
+# History:
+#
+# 2014-05-10, Sébastien Helleu <flashcode@flashtux.org>
+#     version 1.2: change hook_print callback argument type of
+#                  displayed/highlight (WeeChat >= 1.0)
+# 2012-10-09, Chris Johnson <raugturi@gmail.com>:
+#     version 1.1: change some more variable names for clarity/consistency
+# 2012-10-08, Chris Johnson <raugturi@gmail.com>:
+#     version 1.0: fix get_corrected_messages so that the most recent messages
+#                  are corrected and returned
+# 2012-10-08, Chris Johnson <raugturi@gmail.com>:
+#     version 0.9: use defaultdict to handle missing keys, flatten dict by
+#                  using (buffer, nick) tuple as key, simplify message logging
+#                  logic, rename some stuff for clarity.
+# 2012-10-08, Chris Johnson <raugturi@gmail.com>:
+#     version 0.8: remove empty buffers and nicks during clean-up
+# 2012-09-05, Chris Johnson <raugturi@gmail.com>:
+#     version 0.7: fix bug when restoring defaults for options that require
+#                  integer values
+# 2012-09-05, Chris Johnson <raugturi@gmail.com>:
+#     version 0.6: copy info from README into script and shorten the variable
+#                  descriptions
+# 2012-09-01, Chris Johnson <raugturi@gmail.com>:
+#     version 0.5: don't log the reprinted messages
+# 2012-08-31, Chris Johnson <raugturi@gmail.com>:
+#     version 0.4: use same timestamp as buffer when reprinting, instead
+#                  of epoch
+# 2012-08-31, Chris Johnson <raugturi@gmail.com>:
+#     version 0.3: switch to [var] style variables for print format
+# 2012-08-30, Chris Johnson <raugturi@gmail.com>:
+#     version 0.2: fixed search for typos so if regex fails it falls back
+#                  to string.find
+# 2012-08-30, Chris Johnson <raugturi@gmail.com>:
+#     version 0.1: initial release
+
+import_ok = True
+
+try:
+    import weechat
+except ImportError:
+    print('This script must be run under WeeChat.')
+    print('Get WeeChat now at: http://www.weechat.org/')
+    import_ok = False
+
+try:
+    import re
+    import time
+    from operator import itemgetter
+    from collections import defaultdict
+except ImportError as message:
+    print('Missing package(s) for %s: %s' % (SCRIPT_NAME, message))
+    import_ok = False
+
+SCRIPT_NAME = 'apply_corrections'
+SCRIPT_AUTHOR = 'Chris Johnson <raugturi@gmail.com>'
+SCRIPT_VERSION = '1.2'
+SCRIPT_LICENSE = 'GPL3'
+SCRIPT_DESC = "When a correction (ex: s/typo/replacement) is sent, print the "\
+              "user's previous message(s) with the corrected text instead."
+
+# Default settings for the plugin.
+settings = {'check_every': '5',
+            'data_timeout': '60',
+            'message_limit': '2',
+            'print_format': '[nick]: [corrected]',
+            'print_limit': '1'}
+
+# Initialize the dictionary to store most recent messages per buffer per nick.
+LASTWORDS = defaultdict(list)
+
+
+def apply_correction(message, pattern, replacement):
+    """
+    Replaces all occurences of pattern in message with replacment.  It tries to
+    treat the pattern and replacement as regular expressions, but falls back to
+    string replace if that fails.
+    """
+
+    try:
+        message = re.compile(pattern).sub(replacement, message)
+    except:
+        message = message.replace(pattern, replacement)
+
+    return message
+
+
+def get_corrected_messages(nick, log, correction):
+    """
+    Return list of messages that match the pattern, with corrections applied.
+    Limited to print_limit items, sorted by timestamp ascending.
+    """
+
+    print_limit = get_option_int('print_limit')
+    corrected_messages = []
+    pattern, replacement = correction.split('/')[1:3]
+
+    for message in log:
+        if print_limit and len(corrected_messages) >= print_limit:
+            break
+        original = message.get('message', '')
+        if original:
+            try:
+                match = re.match(re.compile('.*%s.*' % pattern), original)
+            except:
+                match = original.find(pattern) != -1
+            finally:
+                if match:
+                    corrected = apply_correction(original,
+                                                 pattern,
+                                                 replacement)
+                    timeformat = weechat.config_string(
+                        weechat.config_get('weechat.look.buffer_time_format'))
+                    timestamp = time.strftime(
+                        timeformat,
+                        time.localtime(float(message['timestamp'])))
+                    corrected_messages.append({'nick': nick,
+                                               'corrected': corrected,
+                                               'correction': correction,
+                                               'original': original,
+                                               'pattern': pattern,
+                                               'replacement': replacement,
+                                               'timestamp': timestamp})
+
+    return sorted(corrected_messages, key=itemgetter('timestamp'))
+
+
+def get_option_int(option):
+    """
+    Checks to see if a configuration option is an integer and sets it back to
+    the default if it isn't.  Returns the value when done.
+    """
+
+    try:
+        value = int(weechat.config_get_plugin(option))
+    except ValueError:
+        weechat.config_set_plugin(option, settings[option])
+        value = int(weechat.config_get_plugin(option))
+
+    return value
+
+
+def get_valid_messages(log, expiry):
+    """
+    Return only the messages that haven't expired.
+    """
+
+    valid = []
+    for message in log:
+        try:
+            timestamp = int(message.get('timestamp', 0))
+            if timestamp > expiry:
+                valid.append(message)
+        except ValueError:
+            continue
+
+    return valid
+
+
+def clear_messages_cb(data, remaining_calls):
+    """
+    Callback that clears old messages from the LASTWORDS dictionary.  The time
+    limit is the number of seconds specified in plugin's data_timeout setting.
+    If data_timeout is set to 0 then no messages are cleared.
+    """
+
+    data_timeout = get_option_int('data_timeout')
+    if data_timeout:
+        expiry = time.time() - data_timeout
+        for buff, nick in LASTWORDS.keys():
+            valid_messages = get_valid_messages(LASTWORDS[(buff,nick)], expiry)
+            if valid_messages:
+                LASTWORDS[(buff, nick)] = valid_messages
+            else:
+                del LASTWORDS[(buff,nick)]
+
+    return weechat.WEECHAT_RC_OK
+
+
+def handle_message_cb(data, buffer, date, tags, disp, hl, nick, message):
+    """
+    Callback that handles new messages.  If the message is in the format of a
+    regex find/replace (ex. 's/typo/replacement/', 'nick: s/typo/replacement')
+    then the last print_limit messages for that nick are re-printed to the
+    current buffer in their oringal order with the change applied.  Otherwise
+    the message is stored in LASTWORDS dictionary for this buffer > nick.
+    """
+
+    # Don't do anything if the message isn't suppose to be displayed.
+    if int(disp):
+        buffer_name = weechat.buffer_get_string(buffer, 'name')
+        log = LASTWORDS[(buffer_name, nick)]
+
+        # Matches on both 's/typo/replacement' and 'nick: s/typo/replacement',
+        # mainly because of bitlbee since it puts your nick in front of
+        # incoming messages.
+        #
+        # Nick regex nicked from colorize_nicks available here:
+        # http://www.weechat.org/scripts/source/stable/colorize_nicks.py.html/
+        valid_nick = r'([@~&!%+])?([-a-zA-Z0-9\[\]\\`_^\{|\}]+)'
+        valid_correction = r's/[^/]*/[^/]*'
+        correction_message_pattern = re.compile(
+                r'(%s:\s*)?(%s)(/)?$' % (valid_nick, valid_correction))
+        match = re.match(correction_message_pattern, message)
+
+        if match:
+            # If message is a correction and we have previous messages from
+            # this nick, print up to print_limit of the nick's previous
+            # messages with corrections applied, in their original order.
+            correction = match.group(4)
+            if log and correction:
+                print_format = weechat.config_get_plugin('print_format')
+                for cm in get_corrected_messages(nick, log, correction):
+                    corrected_msg = print_format
+                    for k, v in cm.iteritems():
+                        corrected_msg = corrected_msg.replace('[%s]' % k, v)
+                    weechat.prnt_date_tags(buffer, 0, 'no_log', corrected_msg)
+        else:
+            # If it's not a correction, store the message in LASTWORDS.
+            log.insert(0, {'message': message, 'timestamp': date})
+            # If there's a per-nick limit, shorten the list to match.
+            message_limit = get_option_int('message_limit')
+            while message_limit and len(log) > message_limit:
+                log.pop()
+
+    return weechat.WEECHAT_RC_OK
+
+
+def load_config(data=None, option=None, value=None):
+    """
+    Load configuration options and (re)register hook_timer to clear old
+    messages based on the current value of check_every.  If check_every is 0
+    then messages are never cleared.
+    """
+
+    # On initial load set any unset options to the defaults.
+    if not option:
+        for option, default in settings.iteritems():
+            if not weechat.config_is_set_plugin(option):
+                weechat.config_set_plugin(option, default)
+
+    if not option or option.endswith('check_every'):
+        # If hook_timer for clearing old messages is set already, clear it.
+        old_hook = globals().get('CLEAR_HOOK', None)
+        if old_hook is not None:
+            weechat.unhook(old_hook)
+
+        # Register hook_timer to clear old messages.
+        check_every = get_option_int('check_every') * 1000
+        if check_every:
+            globals()['CLEAR_HOOK'] = weechat.hook_timer(
+                    check_every, 0, 0, 'clear_messages_cb', '')
+
+    return weechat.WEECHAT_RC_OK
+
+
+def desc_options():
+    """
+    Load descriptions for all the options.
+    """
+
+    weechat.config_set_desc_plugin(
+            'check_every', 'Interval between each check for expired messages.')
+
+    weechat.config_set_desc_plugin(
+            'data_timeout', 'Time before a message is expired.')
+
+    weechat.config_set_desc_plugin(
+            'message_limit', 'Number of messages to store per nick.')
+
+    weechat.config_set_desc_plugin(
+            'print_format', 'Format string for the printed corrections.')
+
+    weechat.config_set_desc_plugin(
+            'print_limit', 'Maximum number of lines to correct.')
+
+    return weechat.WEECHAT_RC_OK
+
+
+if __name__ == '__main__' and import_ok:
+    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
+                        SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
+        # Load the configuration options.
+        load_config()
+
+        # Set up the descriptions for each option.
+        desc_options()
+
+        # Register hook to run load_config when options are changed.
+        weechat.hook_config('plugins.var.python.%s.*' % SCRIPT_NAME,
+                            'load_config', '')
+
+        # Register hook_print to process each new message as it comes in.
+        weechat.hook_print('', '', '', 1, 'handle_message_cb', '')
diff --git a/weechat/.weechat/python/autojoin.py b/weechat/.weechat/python/autojoin.py
new file mode 100644
index 0000000..3fe4b6b
--- /dev/null
+++ b/weechat/.weechat/python/autojoin.py
@@ -0,0 +1,177 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2009 by xt <xt@bash.no>
+#
+# 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.0 or newer)
+#
+# History:
+# 2009-06-18, xt <xt@bash.no>
+#     version 0.1: initial release
+#
+# 2009-10-18, LBo <leon@tim-online.nl>
+#     version 0.2: added autosaving of join channels
+#     /set plugins.var.python.autojoin.autosave 'on'
+#
+# 2009-10-19, LBo <leon@tim-online.nl>
+#     version 0.2.1: now only responds to part messages from self
+#     find_channels() only returns join'ed channels, not all the buffers
+#     updated description and docs
+#
+# 2009-10-20, LBo <leon@tim-online.nl>
+#     version 0.2.2: fixed quit callback
+#     removed the callbacks on part & join messages
+#
+# 2012-04-14, Filip H.F. "FiXato" Slagter <fixato+weechat+autojoin@gmail.com>
+#     version 0.2.3: fixed bug with buffers of which short names were changed.
+#                    Now using 'name' from irc_channel infolist.
+#     version 0.2.4: Added support for key-protected channels
+#
+# 2014-05-22, Nathaniel Wesley Filardo <PADEBR2M2JIQN02N9OO5JM0CTN8K689P@cmx.ietfng.org>
+#     version 0.2.5: Fix keyed channel support
+#
+# @TODO: add options to ignore certain buffers
+# @TODO: maybe add an option to enable autosaving on part/join messages
+
+import weechat as w
+import re
+
+SCRIPT_NAME    = "autojoin"
+SCRIPT_AUTHOR  = "xt <xt@bash.no>"
+SCRIPT_VERSION = "0.2.5"
+SCRIPT_LICENSE = "GPL3"
+SCRIPT_DESC    = "Configure autojoin for all servers according to currently joined channels"
+SCRIPT_COMMAND = "autojoin"
+
+# script options
+settings = {
+    "autosave": "off",
+}
+
+if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
+
+    w.hook_command(SCRIPT_COMMAND,
+                   SCRIPT_DESC,
+                   "[--run]",
+                   "   --run: actually run the commands instead of displaying\n",
+                   "--run",
+                   "autojoin_cb",
+                   "")
+
+    #w.hook_signal('*,irc_in2_join', 'autosave_channels_on_activity', '')
+    #w.hook_signal('*,irc_in2_part', 'autosave_channels_on_activity', '')
+    w.hook_signal('quit',           'autosave_channels_on_quit', '')
+
+# Init everything
+for option, default_value in settings.items():
+    if w.config_get_plugin(option) == "":
+        w.config_set_plugin(option, default_value)
+
+def autosave_channels_on_quit(signal, callback, callback_data):
+    ''' Autojoin current channels '''
+    if w.config_get_plugin(option) != "on":
+        return w.WEECHAT_RC_OK
+
+    items = find_channels()
+
+    # print/execute commands
+    for server, channels in items.iteritems():
+        channels = channels.rstrip(',')
+        command = "/set irc.server.%s.autojoin '%s'" % (server, channels)
+        w.command('', command)
+
+    return w.WEECHAT_RC_OK
+
+
+def autosave_channels_on_activity(signal, callback, callback_data):
+    ''' Autojoin current channels '''
+    if w.config_get_plugin(option) != "on":
+        return w.WEECHAT_RC_OK
+
+    items = find_channels()
+
+    # print/execute commands
+    for server, channels in items.iteritems():
+        nick = w.info_get('irc_nick', server)
+
+        pattern = "^:%s!.*(JOIN|PART) :?(#[^ ]*)( :.*$)?" % nick
+        match = re.match(pattern, callback_data)
+
+        if match: # check if nick is my nick. In that case: save
+            channel = match.group(2)
+            channels = channels.rstrip(',')
+            command = "/set irc.server.%s.autojoin '%s'" % (server, channels)
+            w.command('', command)
+        else: # someone else: ignore
+            continue
+
+    return w.WEECHAT_RC_OK
+
+def autojoin_cb(data, buffer, args):
+    """Old behaviour: doesn't save empty channel list"""
+    """In fact should also save open buffers with a /part'ed channel"""
+    """But I can't believe somebody would want that behaviour"""
+    items = find_channels()
+
+    # print/execute commands
+    for server, channels in items.iteritems():
+        channels = channels.rstrip(',')
+        if not channels: # empty channel list
+            continue
+        command = '/set irc.server.%s.autojoin %s' % (server, channels)
+        if args == '--run':
+            w.command('', command)
+        else:
+            w.prnt('', command)
+
+    return w.WEECHAT_RC_OK
+
+def find_channels():
+    """Return list of servers and channels"""
+    #@TODO: make it return a dict with more options like "nicks_count etc."
+    items = {}
+    infolist = w.infolist_get('irc_server', '', '')
+    # populate servers
+    while w.infolist_next(infolist):
+        items[w.infolist_string(infolist, 'name')] = ''
+
+    w.infolist_free(infolist)
+
+    # populate channels per server
+    for server in items.keys():
+        keys = []
+        keyed_channels = []
+        unkeyed_channels = []
+        items[server] = '' #init if connected but no channels
+        infolist = w.infolist_get('irc_channel', '',  server)
+        while w.infolist_next(infolist):
+            if w.infolist_integer(infolist, 'nicks_count') == 0:
+                #parted but still open in a buffer: bit hackish
+                continue
+            if w.infolist_integer(infolist, 'type') == 0:
+                key = w.infolist_string(infolist, "key")
+                if len(key) > 0:
+                    keys.append(key)
+                    keyed_channels.append(w.infolist_string(infolist, "name"))
+                else :
+                    unkeyed_channels.append(w.infolist_string(infolist, "name"))
+        items[server] = ','.join(keyed_channels + unkeyed_channels)
+        if len(keys) > 0:
+            items[server] += ' %s' % ','.join(keys)
+        w.infolist_free(infolist)
+
+    return items
+
diff --git a/weechat/.weechat/python/autoload/apply_corrections.py b/weechat/.weechat/python/autoload/apply_corrections.py
new file mode 120000
index 0000000..9c1ae70
--- /dev/null
+++ b/weechat/.weechat/python/autoload/apply_corrections.py
@@ -0,0 +1 @@
+../apply_corrections.py
\ No newline at end of file
diff --git a/weechat/.weechat/python/autoload/autojoin.py b/weechat/.weechat/python/autoload/autojoin.py
new file mode 120000
index 0000000..6fceded
--- /dev/null
+++ b/weechat/.weechat/python/autoload/autojoin.py
@@ -0,0 +1 @@
+../autojoin.py
\ No newline at end of file
diff --git a/weechat/.weechat/python/autoload/grep.py b/weechat/.weechat/python/autoload/grep.py
new file mode 120000
index 0000000..87d4ca7
--- /dev/null
+++ b/weechat/.weechat/python/autoload/grep.py
@@ -0,0 +1 @@
+../grep.py
\ No newline at end of file
diff --git a/weechat/.weechat/python/autoload/otr.py b/weechat/.weechat/python/autoload/otr.py
new file mode 120000
index 0000000..61d1b18
--- /dev/null
+++ b/weechat/.weechat/python/autoload/otr.py
@@ -0,0 +1 @@
+../otr.py
\ No newline at end of file
diff --git a/weechat/.weechat/python/autoload/screen_away.py b/weechat/.weechat/python/autoload/screen_away.py
new file mode 120000
index 0000000..e485b70
--- /dev/null
+++ b/weechat/.weechat/python/autoload/screen_away.py
@@ -0,0 +1 @@
+../screen_away.py
\ No newline at end of file
diff --git a/weechat/.weechat/python/autoload/urlview.py b/weechat/.weechat/python/autoload/urlview.py
new file mode 120000
index 0000000..cf5bf9e
--- /dev/null
+++ b/weechat/.weechat/python/autoload/urlview.py
@@ -0,0 +1 @@
+../urlview.py
\ No newline at end of file
diff --git a/weechat/.weechat/python/grep.py b/weechat/.weechat/python/grep.py
new file mode 100644
index 0000000..4fd5c64
--- /dev/null
+++ b/weechat/.weechat/python/grep.py
@@ -0,0 +1,1731 @@
+# -*- coding: utf-8 -*-
+###
+# Copyright (c) 2009-2011 by Elián Hanisch <lambdae2@gmail.com>
+#
+# 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/>.
+###
+
+###
+# Search in Weechat buffers and logs (for Weechat 0.3.*)
+#
+#   Inspired by xt's grep.py
+#   Originally I just wanted to add some fixes in grep.py, but then
+#   I got carried away and rewrote everything, so new script.
+#
+#   Commands:
+#   * /grep
+#     Search in logs or buffers, see /help grep
+#   * /logs:
+#     Lists logs in ~/.weechat/logs, see /help logs
+#
+#   Settings:
+#   * plugins.var.python.grep.clear_buffer:
+#     Clear the results buffer before each search. Valid values: on, off
+#
+#   * plugins.var.python.grep.go_to_buffer:
+#     Automatically go to grep buffer when search is over. Valid values: on, off
+#
+#   * plugins.var.python.grep.log_filter:
+#     Coma separated list of patterns that grep will use for exclude logs, e.g.
+#     if you use '*server/*' any log in the 'server' folder will be excluded
+#     when using the command '/grep log'
+#
+#   * plugins.var.python.grep.show_summary:
+#     Shows summary for each log. Valid values: on, off
+#
+#   * plugins.var.python.grep.max_lines:
+#     Grep will only print the last matched lines that don't surpass the value defined here.
+#
+#   * plugins.var.python.grep.size_limit:
+#     Size limit in KiB, is used for decide whenever grepping should run in background or not. If
+#     the logs to grep have a total size bigger than this value then grep run as a new process.
+#     It can be used for force or disable background process, using '0' forces to always grep in
+#     background, while using '' (empty string) will disable it.
+#
+#   * plugins.var.python.grep.default_tail_head:
+#     Config option for define default number of lines returned when using --head or --tail options.
+#     Can be overriden in the command with --number option.
+#
+#
+#   TODO:
+#   * try to figure out why hook_process chokes in long outputs (using a tempfile as a
+#   workaround now)
+#   * possibly add option for defining time intervals
+#
+#
+#   History:
+#
+#   2015-01-31, Nicd-
+#   version 0.7.5:
+#   '~' is now expaned to the home directory in the log file path so
+#   paths like '~/logs/' should work.
+#
+#   2015-01-14, nils_2
+#   version 0.7.4: make q work to quit grep buffer (requested by: gb)
+#
+#   2014-03-29, Felix Eckhofer <felix@tribut.de>
+#   version 0.7.3: fix typo
+#
+#   2011-01-09
+#   version 0.7.2: bug fixes
+#
+#   2010-11-15
+#   version 0.7.1:
+#   * use TempFile so temporal files are guaranteed to be deleted.
+#   * enable Archlinux workaround.
+#
+#   2010-10-26
+#   version 0.7:
+#   * added templates.
+#   * using --only-match shows only unique strings.
+#   * fixed bug that inverted -B -A switches when used with -t
+#
+#   2010-10-14
+#   version 0.6.8: by xt <xt@bash.no>
+#   * supress highlights when printing in grep buffer
+#
+#   2010-10-06
+#   version 0.6.7: by xt <xt@bash.no> 
+#   * better temporary file:
+#    use tempfile.mkstemp. to create a temp file in log dir, 
+#    makes it safer with regards to write permission and multi user
+#
+#   2010-04-08
+#   version 0.6.6: bug fixes
+#   * use WEECHAT_LIST_POS_END in log file completion, makes completion faster
+#   * disable bytecode if using python 2.6
+#   * use single quotes in command string
+#   * fix bug that could change buffer's title when using /grep stop
+#
+#   2010-01-24
+#   version 0.6.5: disable bytecode is a 2.6 feature, instead, resort to delete the bytecode manually
+#
+#   2010-01-19
+#   version 0.6.4: bug fix
+#   version 0.6.3: added options --invert --only-match (replaces --exact, which is still available
+#   but removed from help)
+#   * use new 'irc_nick_color' info
+#   * don't generate bytecode when spawning a new process
+#   * show active options in buffer title
+#
+#   2010-01-17
+#   version 0.6.2: removed 2.6-ish code
+#   version 0.6.1: fixed bug when grepping in grep's buffer
+#
+#   2010-01-14
+#   version 0.6.0: implemented grep in background
+#   * improved context lines presentation.
+#   * grepping for big (or many) log files runs in a weechat_process.
+#   * added /grep stop.
+#   * added 'size_limit' option
+#   * fixed a infolist leak when grepping buffers
+#   * added 'default_tail_head' option
+#   * results are sort by line count
+#   * don't die if log is corrupted (has NULL chars in it)
+#   * changed presentation of /logs
+#   * log path completion doesn't suck anymore
+#   * removed all tabs, because I learned how to configure Vim so that spaces aren't annoying
+#   anymore. This was the script's original policy.
+#
+#   2010-01-05
+#   version 0.5.5: rename script to 'grep.py' (FlashCode <flashcode@flashtux.org>).
+#
+#   2010-01-04
+#   version 0.5.4.1: fix index error when using --after/before-context options.
+#
+#   2010-01-03
+#   version 0.5.4: new features
+#   * added --after-context and --before-context options.
+#   * added --context as a shortcut for using both -A -B options.
+#
+#   2009-11-06
+#   version 0.5.3: improvements for long grep output
+#   * grep buffer input accepts the same flags as /grep for repeat a search with different
+#     options.
+#   * tweaks in grep's output.
+#   * max_lines option added for limit grep's output.
+#   * code in update_buffer() optimized.
+#   * time stats in buffer title.
+#   * added go_to_buffer config option.
+#   * added --buffer for search only in buffers.
+#   * refactoring.
+#
+#   2009-10-12, omero
+#   version 0.5.2: made it python-2.4.x compliant
+#
+#   2009-08-17
+#   version 0.5.1: some refactoring, show_summary option added.
+#
+#   2009-08-13
+#   version 0.5: rewritten from xt's grep.py
+#   * fixed searching in non weechat logs, for cases like, if you're
+#     switching from irssi and rename and copy your irssi logs to %h/logs
+#   * fixed "timestamp rainbow" when you /grep in grep's buffer
+#   * allow to search in other buffers other than current or in logs
+#     of currently closed buffers with cmd 'buffer'
+#   * allow to search in any log file in %h/logs with cmd 'log'
+#   * added --count for return the number of matched lines
+#   * added --matchcase for case sensible search
+#   * added --hilight for color matches
+#   * added --head and --tail options, and --number
+#   * added command /logs for list files in %h/logs
+#   * added config option for clear the buffer before a search
+#   * added config option for filter logs we don't want to grep
+#   * added the posibility to repeat last search with another regexp by writing
+#     it in grep's buffer
+#   * changed spaces for tabs in the code, which is my preference
+#
+###
+
+from os import path
+import sys, getopt, time, os, re, tempfile
+
+try:
+    import weechat
+    from weechat import WEECHAT_RC_OK, prnt, prnt_date_tags
+    import_ok = True
+except ImportError:
+    import_ok = False
+
+SCRIPT_NAME    = "grep"
+SCRIPT_AUTHOR  = "Elián Hanisch <lambdae2@gmail.com>"
+SCRIPT_VERSION = "0.7.5"
+SCRIPT_LICENSE = "GPL3"
+SCRIPT_DESC    = "Search in buffers and logs"
+SCRIPT_COMMAND = "grep"
+
+### Default Settings ###
+settings = {
+'clear_buffer'      : 'off',
+'log_filter'        : '',
+'go_to_buffer'      : 'on',
+'max_lines'         : '4000',
+'show_summary'      : 'on',
+'size_limit'        : '2048',
+'default_tail_head' : '10',
+}
+
+### Class definitions ###
+class linesDict(dict):
+    """
+    Class for handling matched lines in more than one buffer.
+    linesDict[buffer_name] = matched_lines_list
+    """
+    def __setitem__(self, key, value):
+        assert isinstance(value, list)
+        if key not in self:
+            dict.__setitem__(self, key, value)
+        else:
+            dict.__getitem__(self, key).extend(value)
+
+    def get_matches_count(self):
+        """Return the sum of total matches stored."""
+        if dict.__len__(self):
+            return sum(map(lambda L: L.matches_count, self.itervalues()))
+        else:
+            return 0
+
+    def __len__(self):
+        """Return the sum of total lines stored."""
+        if dict.__len__(self):
+            return sum(map(len, self.itervalues()))
+        else:
+            return 0
+
+    def __str__(self):
+        """Returns buffer count or buffer name if there's just one stored."""
+        n = len(self.keys())
+        if n == 1:
+            return self.keys()[0]
+        elif n > 1:
+            return '%s logs' %n
+        else:
+            return ''
+
+    def items(self):
+        """Returns a list of items sorted by line count."""
+        items = dict.items(self)
+        items.sort(key=lambda i: len(i[1]))
+        return items
+
+    def items_count(self):
+        """Returns a list of items sorted by match count."""
+        items = dict.items(self)
+        items.sort(key=lambda i: i[1].matches_count)
+        return items
+
+    def strip_separator(self):
+        for L in self.itervalues():
+            L.strip_separator()
+
+    def get_last_lines(self, n):
+        total_lines = len(self)
+        #debug('total: %s n: %s' %(total_lines, n))
+        if n >= total_lines:
+            # nothing to do
+            return
+        for k, v in reversed(self.items()):
+            l = len(v)
+            if n > 0:
+                if l > n:
+                    del v[:l-n]
+                    v.stripped_lines = l-n
+                n -= l
+            else:
+                del v[:]
+                v.stripped_lines = l
+
+class linesList(list):
+    """Class for list of matches, since sometimes I need to add lines that aren't matches, I need an
+    independent counter."""
+    _sep = '...'
+    def __init__(self, *args):
+        list.__init__(self, *args)
+        self.matches_count = 0
+        self.stripped_lines = 0
+
+    def append(self, item):
+        """Append lines, can be a string or a list with strings."""
+        if isinstance(item, str):
+            list.append(self, item)
+        else:
+            self.extend(item)
+
+    def append_separator(self):
+        """adds a separator into the list, makes sure it doen't add two together."""
+        s = self._sep
+        if (self and self[-1] != s) or not self:
+            self.append(s)
+
+    def onlyUniq(self):
+        s = set(self)
+        del self[:]
+        self.extend(s)
+
+    def count_match(self, item=None):
+        if item is None or isinstance(item, str):
+            self.matches_count += 1
+        else:
+            self.matches_count += len(item)
+
+    def strip_separator(self):
+        """removes separators if there are first or/and last in the list."""
+        if self:
+            s = self._sep
+            if self[0] == s:
+                del self[0]
+            if self[-1] == s:
+                del self[-1]
+
+### Misc functions ###
+now = time.time
+def get_size(f):
+    try:
+        return os.stat(f).st_size
+    except OSError:
+        return 0
+
+sizeDict = {0:'b', 1:'KiB', 2:'MiB', 3:'GiB', 4:'TiB'}
+def human_readable_size(size):
+    power = 0
+    while size > 1024:
+        power += 1
+        size /= 1024.0
+    return '%.2f %s' %(size, sizeDict.get(power, ''))
+
+def color_nick(nick):
+    """Returns coloured nick, with coloured mode if any."""
+    if not nick: return ''
+    wcolor = weechat.color
+    config_string = lambda s : weechat.config_string(weechat.config_get(s))
+    config_int = lambda s : weechat.config_integer(weechat.config_get(s))
+    # prefix and suffix
+    prefix = config_string('irc.look.nick_prefix')
+    suffix = config_string('irc.look.nick_suffix')
+    prefix_c = suffix_c = wcolor(config_string('weechat.color.chat_delimiters'))
+    if nick[0] == prefix:
+        nick = nick[1:]
+    else:
+        prefix = prefix_c = ''
+    if nick[-1] == suffix:
+        nick = nick[:-1]
+        suffix = wcolor(color_delimiter) + suffix
+    else:
+        suffix = suffix_c = ''
+    # nick mode
+    modes = '@!+%'
+    if nick[0] in modes:
+        mode, nick = nick[0], nick[1:]
+        mode_color = wcolor(config_string('weechat.color.nicklist_prefix%d' \
+            %(modes.find(mode) + 1)))
+    else:
+        mode = mode_color = ''
+    # nick color
+    nick_color = weechat.info_get('irc_nick_color', nick)
+    if not nick_color:
+        # probably we're in WeeChat 0.3.0
+        #debug('no irc_nick_color')
+        color_nicks_number = config_int('weechat.look.color_nicks_number')
+        idx = (sum(map(ord, nick))%color_nicks_number) + 1
+        nick_color = wcolor(config_string('weechat.color.chat_nick_color%02d' %idx))
+    return ''.join((prefix_c, prefix, mode_color, mode, nick_color, nick, suffix_c, suffix))
+
+### Config and value validation ###
+boolDict = {'on':True, 'off':False}
+def get_config_boolean(config):
+    value = weechat.config_get_plugin(config)
+    try:
+        return boolDict[value]
+    except KeyError:
+        default = settings[config]
+        error("Error while fetching config '%s'. Using default value '%s'." %(config, default))
+        error("'%s' is invalid, allowed: 'on', 'off'" %value)
+        return boolDict[default]
+
+def get_config_int(config, allow_empty_string=False):
+    value = weechat.config_get_plugin(config)
+    try:
+        return int(value)
+    except ValueError:
+        if value == '' and allow_empty_string:
+            return value
+        default = settings[config]
+        error("Error while fetching config '%s'. Using default value '%s'." %(config, default))
+        error("'%s' is not a number." %value)
+        return int(default)
+
+def get_config_log_filter():
+    filter = weechat.config_get_plugin('log_filter')
+    if filter:
+        return filter.split(',')
+    else:
+        return []
+
+def get_home():
+    home = weechat.config_string(weechat.config_get('logger.file.path'))
+    home = path.abspath(path.expanduser(home))
+    return home.replace('%h', weechat.info_get('weechat_dir', ''))
+
+def strip_home(s, dir=''):
+    """Strips home dir from the begging of the log path, this makes them sorter."""
+    if not dir:
+        global home_dir
+        dir = home_dir
+    l = len(dir)
+    if s[:l] == dir:
+        return s[l:]
+    return s
+
+### Messages ###
+script_nick = SCRIPT_NAME
+def error(s, buffer=''):
+    """Error msg"""
+    prnt(buffer, '%s%s %s' %(weechat.prefix('error'), script_nick, s))
+    if weechat.config_get_plugin('debug'):
+        import traceback
+        if traceback.sys.exc_type:
+            trace = traceback.format_exc()
+            prnt('', trace)
+
+def say(s, buffer=''):
+    """normal msg"""
+    prnt_date_tags(buffer, 0, 'no_highlight', '%s\t%s' %(script_nick, s))
+
+
+
+### Log files and buffers ###
+cache_dir = {} # note: don't remove, needed for completion if the script was loaded recently
+def dir_list(dir, filter_list=(), filter_excludes=True, include_dir=False):
+    """Returns a list of files in 'dir' and its subdirs."""
+    global cache_dir
+    from os import walk
+    from fnmatch import fnmatch
+    #debug('dir_list: listing in %s' %dir)
+    key = (dir, include_dir)
+    try:
+        return cache_dir[key]
+    except KeyError:
+        pass
+    
+    filter_list = filter_list or get_config_log_filter()
+    dir_len = len(dir)
+    if filter_list:
+        def filter(file):
+            file = file[dir_len:] # pattern shouldn't match home dir
+            for pattern in filter_list:
+                if fnmatch(file, pattern):
+                    return filter_excludes
+            return not filter_excludes
+    else:
+        filter = lambda f : not filter_excludes
+
+    file_list = []
+    extend = file_list.extend
+    join = path.join
+    def walk_path():
+        for basedir, subdirs, files in walk(dir):
+            #if include_dir:
+            #    subdirs = map(lambda s : join(s, ''), subdirs)
+            #    files.extend(subdirs)
+            files_path = map(lambda f : join(basedir, f), files)
+            files_path = [ file for file in files_path if not filter(file) ]
+            extend(files_path)
+
+    walk_path()
+    cache_dir[key] = file_list
+    #debug('dir_list: got %s' %str(file_list))
+    return file_list
+
+def get_file_by_pattern(pattern, all=False):
+    """Returns the first log whose path matches 'pattern',
+    if all is True returns all logs that matches."""
+    if not pattern: return []
+    #debug('get_file_by_filename: searching for %s.' %pattern)
+    # do envvar expandsion and check file
+    file = path.expanduser(pattern)
+    file = path.expandvars(file)
+    if path.isfile(file):
+        return [file]
+    # lets see if there's a matching log
+    global home_dir
+    file = path.join(home_dir, pattern)
+    if path.isfile(file):
+        return [file]
+    else:
+        from fnmatch import fnmatch
+        file = []
+        file_list = dir_list(home_dir)
+        n = len(home_dir)
+        for log in file_list:
+            basename = log[n:]
+            if fnmatch(basename, pattern):
+                file.append(log)
+        #debug('get_file_by_filename: got %s.' %file)
+        if not all and file:
+            file.sort()
+            return [ file[-1] ]
+        return file
+
+def get_file_by_buffer(buffer):
+    """Given buffer pointer, finds log's path or returns None."""
+    #debug('get_file_by_buffer: searching for %s' %buffer)
+    infolist = weechat.infolist_get('logger_buffer', '', '')
+    if not infolist: return
+    try:
+        while weechat.infolist_next(infolist):
+            pointer = weechat.infolist_pointer(infolist, 'buffer')
+            if pointer == buffer:
+                file = weechat.infolist_string(infolist, 'log_filename')
+                if weechat.infolist_integer(infolist, 'log_enabled'):
+                    #debug('get_file_by_buffer: got %s' %file)
+                    return file
+                #else:
+                #    debug('get_file_by_buffer: got %s but log not enabled' %file)
+    finally:
+        #debug('infolist gets freed')
+        weechat.infolist_free(infolist)
+
+def get_file_by_name(buffer_name):
+    """Given a buffer name, returns its log path or None. buffer_name should be in 'server.#channel'
+    or '#channel' format."""
+    #debug('get_file_by_name: searching for %s' %buffer_name)
+    # common mask options
+    config_masks = ('logger.mask.irc', 'logger.file.mask')
+    # since there's no buffer pointer, we try to replace some local vars in mask, like $channel and
+    # $server, then replace the local vars left with '*', and use it as a mask for get the path with
+    # get_file_by_pattern
+    for config in config_masks:
+        mask = weechat.config_string(weechat.config_get(config))
+        #debug('get_file_by_name: mask: %s' %mask)
+        if '$name' in mask:
+            mask = mask.replace('$name', buffer_name)
+        elif '$channel' in mask or '$server' in mask:
+            if '.' in buffer_name and \
+                    '#' not in buffer_name[:buffer_name.find('.')]: # the dot isn't part of the channel name
+                #    ^ I'm asuming channel starts with #, i'm lazy.
+                server, channel = buffer_name.split('.', 1)
+            else:
+                server, channel = '*', buffer_name
+            if '$channel' in mask:
+                mask = mask.replace('$channel', channel)
+            if '$server' in mask:
+                mask = mask.replace('$server', server)
+        # change the unreplaced vars by '*'
+        from string import letters
+        if '%' in mask:
+            # vars for time formatting
+            mask = mask.replace('%', '$')
+        if '$' in mask:
+            masks = mask.split('$')
+            masks = map(lambda s: s.lstrip(letters), masks)
+            mask = '*'.join(masks)
+            if mask[0] != '*':
+                mask = '*' + mask
+        #debug('get_file_by_name: using mask %s' %mask)
+        file = get_file_by_pattern(mask)
+        #debug('get_file_by_name: got file %s' %file)
+        if file:
+            return file
+    return None
+
+def get_buffer_by_name(buffer_name):
+    """Given a buffer name returns its buffer pointer or None."""
+    #debug('get_buffer_by_name: searching for %s' %buffer_name)
+    pointer = weechat.buffer_search('', buffer_name)
+    if not pointer:
+        try:
+            infolist = weechat.infolist_get('buffer', '', '')
+            while weechat.infolist_next(infolist):
+                short_name = weechat.infolist_string(infolist, 'short_name')
+                name = weechat.infolist_string(infolist, 'name')
+                if buffer_name in (short_name, name):
+                    #debug('get_buffer_by_name: found %s' %name)
+                    pointer = weechat.buffer_search('', name)
+                    return pointer
+        finally:
+            weechat.infolist_free(infolist)
+    #debug('get_buffer_by_name: got %s' %pointer)
+    return pointer
+
+def get_all_buffers():
+    """Returns list with pointers of all open buffers."""
+    buffers = []
+    infolist = weechat.infolist_get('buffer', '', '')
+    while weechat.infolist_next(infolist):
+        buffers.append(weechat.infolist_pointer(infolist, 'pointer'))
+    weechat.infolist_free(infolist)
+    grep_buffer = weechat.buffer_search('python', SCRIPT_NAME)
+    if grep_buffer and grep_buffer in buffers:
+        # remove it from list
+        del buffers[buffers.index(grep_buffer)]
+    return buffers
+
+### Grep ###
+def make_regexp(pattern, matchcase=False):
+    """Returns a compiled regexp."""
+    if pattern in ('.', '.*', '.?', '.+'):
+        # because I don't need to use a regexp if we're going to match all lines
+        return None
+    # matching takes a lot more time if pattern starts or ends with .* and it isn't needed.
+    if pattern[:2] == '.*':
+        pattern = pattern[2:]
+    if pattern[-2:] == '.*':
+        pattern = pattern[:-2]
+    try:
+        if not matchcase:
+            regexp = re.compile(pattern, re.IGNORECASE)
+        else:
+            regexp = re.compile(pattern)
+    except Exception, e:
+        raise Exception, 'Bad pattern, %s' %e
+    return regexp
+
+def check_string(s, regexp, hilight='', exact=False):
+    """Checks 's' with a regexp and returns it if is a match."""
+    if not regexp:
+        return s
+
+    elif exact:
+        matchlist = regexp.findall(s)
+        if matchlist:
+            if isinstance(matchlist[0], tuple):
+                # join tuples (when there's more than one match group in regexp)
+                return [ ' '.join(t) for t in matchlist ]
+            return matchlist
+
+    elif hilight:
+        matchlist = regexp.findall(s)
+        if matchlist:
+            if isinstance(matchlist[0], tuple):
+                # flatten matchlist
+                matchlist = [ item for L in matchlist for item in L if item ]
+            matchlist = list(set(matchlist)) # remove duplicates if any
+            # apply hilight
+            color_hilight, color_reset = hilight.split(',', 1)
+            for m in matchlist:
+                s = s.replace(m, '%s%s%s' % (color_hilight, m, color_reset))
+            return s
+
+    # no need for findall() here
+    elif regexp.search(s):
+        return s
+
+def grep_file(file, head, tail, after_context, before_context, count, regexp, hilight, exact, invert):
+    """Return a list of lines that match 'regexp' in 'file', if no regexp returns all lines."""
+    if count:
+        tail = head = after_context = before_context = False
+        hilight = ''
+    elif exact:
+        before_context = after_context = False
+        hilight = ''
+    elif invert:
+        hilight = ''
+    #debug(' '.join(map(str, (file, head, tail, after_context, before_context))))
+
+    lines = linesList()
+    # define these locally as it makes the loop run slightly faster
+    append = lines.append
+    count_match = lines.count_match
+    separator = lines.append_separator
+    if invert:
+        def check(s):
+            if check_string(s, regexp, hilight, exact):
+                return None
+            else:
+                return s
+    else:
+        check = lambda s: check_string(s, regexp, hilight, exact)
+    
+    try:
+        file_object = open(file, 'r')
+    except IOError:
+        # file doesn't exist
+        return lines
+    if tail or before_context:
+        # for these options, I need to seek in the file, but is slower and uses a good deal of
+        # memory if the log is too big, so we do this *only* for these options.
+        file_lines = file_object.readlines()
+
+        if tail:
+            # instead of searching in the whole file and later pick the last few lines, we
+            # reverse the log, search until count reached and reverse it again, that way is a lot
+            # faster
+            file_lines.reverse()
+            # don't invert context switches
+            before_context, after_context = after_context, before_context
+
+        if before_context:
+            before_context_range = range(1, before_context + 1)
+            before_context_range.reverse()
+
+        limit = tail or head
+
+        line_idx = 0
+        while line_idx < len(file_lines):
+            line = file_lines[line_idx]
+            line = check(line)
+            if line:
+                if before_context:
+                    separator()
+                    trimmed = False
+                    for id in before_context_range:
+                        try:
+                            context_line = file_lines[line_idx - id]
+                            if check(context_line):
+                                # match in before context, that means we appended these same lines in a
+                                # previous match, so we delete them merging both paragraphs
+                                if not trimmed:
+                                    del lines[id - before_context - 1:]
+                                    trimmed = True
+                            else:
+                                append(context_line)
+                        except IndexError:
+                            pass
+                append(line)
+                count_match(line)
+                if after_context:
+                    id, offset = 0, 0
+                    while id < after_context + offset:
+                        id += 1
+                        try:
+                            context_line = file_lines[line_idx + id]
+                            _context_line = check(context_line)
+                            if _context_line:
+                                offset = id
+                                context_line = _context_line # so match is hilighted with --hilight
+                                count_match()
+                            append(context_line)
+                        except IndexError:
+                            pass
+                    separator()
+                    line_idx += id
+                if limit and lines.matches_count >= limit:
+                    break
+            line_idx += 1
+
+        if tail:
+            lines.reverse()
+    else:
+        # do a normal grep
+        limit = head
+
+        for line in file_object:
+            line = check(line)
+            if line:
+                count or append(line)
+                count_match(line)
+                if after_context:
+                    id, offset = 0, 0
+                    while id < after_context + offset:
+                        id += 1
+                        try:
+                            context_line = file_object.next()
+                            _context_line = check(context_line)
+                            if _context_line:
+                                offset = id
+                                context_line = _context_line
+                                count_match()
+                            count or append(context_line)
+                        except StopIteration:
+                            pass
+                    separator()
+                if limit and lines.matches_count >= limit:
+                    break
+
+    file_object.close()
+    return lines
+
+def grep_buffer(buffer, head, tail, after_context, before_context, count, regexp, hilight, exact,
+        invert):
+    """Return a list of lines that match 'regexp' in 'buffer', if no regexp returns all lines."""
+    lines = linesList()
+    if count:
+        tail = head = after_context = before_context = False
+        hilight = ''
+    elif exact:
+        before_context = after_context = False
+    #debug(' '.join(map(str, (tail, head, after_context, before_context, count, exact, hilight))))
+
+    # Using /grep in grep's buffer can lead to some funny effects
+    # We should take measures if that's the case
+    def make_get_line_funcion():
+        """Returns a function for get lines from the infolist, depending if the buffer is grep's or
+        not."""
+        string_remove_color = weechat.string_remove_color
+        infolist_string = weechat.infolist_string
+        grep_buffer = weechat.buffer_search('python', SCRIPT_NAME)
+        if grep_buffer and buffer == grep_buffer:
+            def function(infolist):
+                prefix = infolist_string(infolist, 'prefix')
+                message = infolist_string(infolist, 'message')
+                if prefix: # only our messages have prefix, ignore it
+                    return None
+                return message
+        else:
+            infolist_time = weechat.infolist_time
+            def function(infolist):
+                prefix = string_remove_color(infolist_string(infolist, 'prefix'), '')
+                message = string_remove_color(infolist_string(infolist, 'message'), '')
+                date = infolist_time(infolist, 'date')
+                return '%s\t%s\t%s' %(date, prefix, message)
+        return function
+    get_line = make_get_line_funcion()
+
+    infolist = weechat.infolist_get('buffer_lines', buffer, '')
+    if tail:
+        # like with grep_file() if we need the last few matching lines, we move the cursor to
+        # the end and search backwards
+        infolist_next = weechat.infolist_prev
+        infolist_prev = weechat.infolist_next
+    else:
+        infolist_next = weechat.infolist_next
+        infolist_prev = weechat.infolist_prev
+    limit = head or tail
+
+    # define these locally as it makes the loop run slightly faster
+    append = lines.append
+    count_match = lines.count_match
+    separator = lines.append_separator
+    if invert:
+        def check(s):
+            if check_string(s, regexp, hilight, exact):
+                return None
+            else:
+                return s
+    else:
+        check = lambda s: check_string(s, regexp, hilight, exact)
+
+    if before_context:
+        before_context_range = range(1, before_context + 1)
+        before_context_range.reverse()
+
+    while infolist_next(infolist):
+        line = get_line(infolist)
+        if line is None: continue
+        line = check(line)
+        if line:
+            if before_context:
+                separator()
+                trimmed = False
+                for id in before_context_range:
+                    if not infolist_prev(infolist):
+                        trimmed = True
+                for id in before_context_range:
+                    context_line = get_line(infolist)
+                    if check(context_line):
+                        if not trimmed:
+                            del lines[id - before_context - 1:]
+                            trimmed = True
+                    else:
+                        append(context_line)
+                    infolist_next(infolist)
+            count or append(line)
+            count_match(line)
+            if after_context:
+                id, offset = 0, 0
+                while id < after_context + offset:
+                    id += 1
+                    if infolist_next(infolist):
+                        context_line = get_line(infolist)
+                        _context_line = check(context_line)
+                        if _context_line:
+                            context_line = _context_line
+                            offset = id
+                            count_match()
+                        append(context_line)
+                    else:
+                        # in the main loop infolist_next will start again an cause an infinite loop
+                        # this will avoid it
+                        infolist_next = lambda x: 0
+                separator()
+            if limit and lines.matches_count >= limit:
+                break
+    weechat.infolist_free(infolist)
+
+    if tail:
+        lines.reverse()
+    return lines
+
+### this is our main grep function
+hook_file_grep = None
+def show_matching_lines():
+    """
+    Greps buffers in search_in_buffers or files in search_in_files and updates grep buffer with the
+    result.
+    """
+    global pattern, matchcase, number, count, exact, hilight, invert
+    global tail, head, after_context, before_context
+    global search_in_files, search_in_buffers, matched_lines, home_dir
+    global time_start
+    matched_lines = linesDict()
+    #debug('buffers:%s \nlogs:%s' %(search_in_buffers, search_in_files))
+    time_start = now()
+
+    # buffers
+    if search_in_buffers:
+        regexp = make_regexp(pattern, matchcase)
+        for buffer in search_in_buffers:
+            buffer_name = weechat.buffer_get_string(buffer, 'name')
+            matched_lines[buffer_name] = grep_buffer(buffer, head, tail, after_context,
+                    before_context, count, regexp, hilight, exact, invert)
+
+    # logs
+    if search_in_files:
+        size_limit = get_config_int('size_limit', allow_empty_string=True)
+        background = False
+        if size_limit or size_limit == 0:
+            size = sum(map(get_size, search_in_files))
+            if size > size_limit * 1024:
+                background = True
+        elif size_limit == '':
+            background = False
+
+        if not background:
+            # run grep normally
+            regexp = make_regexp(pattern, matchcase)
+            for log in search_in_files:
+                log_name = strip_home(log)
+                matched_lines[log_name] = grep_file(log, head, tail, after_context, before_context,
+                        count, regexp, hilight, exact, invert)
+            buffer_update()
+        else:
+            # we hook a process so grepping runs in background.
+            #debug('on background')
+            global hook_file_grep, script_path, bytecode
+            timeout = 1000*60*5 # 5 min
+
+            quotify = lambda s: '"%s"' %s
+            files_string = ', '.join(map(quotify, search_in_files))
+
+            global tmpFile
+            # we keep the file descriptor as a global var so it isn't deleted until next grep
+            tmpFile = tempfile.NamedTemporaryFile(prefix=SCRIPT_NAME,
+                    dir=weechat.info_get('weechat_dir', ''))
+            cmd = grep_process_cmd %dict(logs=files_string, head=head, pattern=pattern, tail=tail,
+                    hilight=hilight, after_context=after_context, before_context=before_context,
+                    exact=exact, matchcase=matchcase, home_dir=home_dir, script_path=script_path,
+                    count=count, invert=invert, bytecode=bytecode, filename=tmpFile.name,
+                    python=weechat.info_get('python2_bin', '') or 'python')
+
+            #debug(cmd)
+            hook_file_grep = weechat.hook_process(cmd, timeout, 'grep_file_callback', tmpFile.name)
+            global pattern_tmpl
+            if hook_file_grep:
+                buffer_create("Searching for '%s' in %s worth of data..." %(pattern_tmpl,
+                    human_readable_size(size)))
+    else:
+        buffer_update()
+
+# defined here for commodity
+grep_process_cmd = """%(python)s -%(bytecode)sc '
+import sys, cPickle, os
+sys.path.append("%(script_path)s") # add WeeChat script dir so we can import grep
+from grep import make_regexp, grep_file, strip_home
+logs = (%(logs)s, )
+try:
+    regexp = make_regexp("%(pattern)s", %(matchcase)s)
+    d = {}
+    for log in logs:
+        log_name = strip_home(log, "%(home_dir)s")
+        lines = grep_file(log, %(head)s, %(tail)s, %(after_context)s, %(before_context)s,
+        %(count)s, regexp, "%(hilight)s", %(exact)s, %(invert)s)
+        d[log_name] = lines
+    fd = open("%(filename)s", "wb")
+    cPickle.dump(d, fd, -1)
+    fd.close()
+except Exception, e:
+    print >> sys.stderr, e'
+"""
+
+grep_stdout = grep_stderr = ''
+def grep_file_callback(filename, command, rc, stdout, stderr):
+    global hook_file_grep, grep_stderr,  grep_stdout
+    global matched_lines
+    #debug("rc: %s\nstderr: %s\nstdout: %s" %(rc, repr(stderr), repr(stdout)))
+    if stdout:
+        grep_stdout += stdout
+    if stderr:
+        grep_stderr += stderr
+    if int(rc) >= 0:
+  
+        def set_buffer_error():
+            grep_buffer = buffer_create()
+            title = weechat.buffer_get_string(grep_buffer, 'title')
+            title = title + ' %serror' %color_title
+            weechat.buffer_set(grep_buffer, 'title', title)
+
+        try:
+            if grep_stderr:
+                error(grep_stderr)
+                set_buffer_error()
+            #elif grep_stdout:
+                #debug(grep_stdout)
+            elif path.exists(filename):
+                import cPickle
+                try:
+                    #debug(file)
+                    fd = open(filename, 'rb')
+                    d = cPickle.load(fd)
+                    matched_lines.update(d)
+                    fd.close()
+                except Exception, e:
+                    error(e)
+                    set_buffer_error()
+                else:
+                    buffer_update()
+            global tmpFile
+            tmpFile = None
+        finally:
+            grep_stdout = grep_stderr = ''
+            hook_file_grep = None
+    return WEECHAT_RC_OK
+
+def get_grep_file_status():
+    global search_in_files, matched_lines, time_start
+    elapsed = now() - time_start
+    if len(search_in_files) == 1:
+        log = '%s (%s)' %(strip_home(search_in_files[0]),
+                human_readable_size(get_size(search_in_files[0])))
+    else:
+        size = sum(map(get_size, search_in_files))
+        log = '%s log files (%s)' %(len(search_in_files), human_readable_size(size))
+    return 'Searching in %s, running for %.4f seconds. Interrupt it with "/grep stop" or "stop"' \
+        ' in grep buffer.' %(log, elapsed)
+
+### Grep buffer ###
+def buffer_update():
+    """Updates our buffer with new lines."""
+    global pattern_tmpl, matched_lines, pattern, count, hilight, invert, exact
+    time_grep = now()
+
+    buffer = buffer_create()
+    if get_config_boolean('clear_buffer'):
+        weechat.buffer_clear(buffer)
+    matched_lines.strip_separator() # remove first and last separators of each list
+    len_total_lines = len(matched_lines)
+    max_lines = get_config_int('max_lines')
+    if not count and len_total_lines > max_lines:
+        weechat.buffer_clear(buffer)
+
+    def _make_summary(log, lines, note):
+        return '%s matches "%s%s%s"%s in %s%s%s%s' \
+                %(lines.matches_count, color_summary, pattern_tmpl, color_info,
+                  invert and ' (inverted)' or '',
+                  color_summary, log, color_reset, note)
+
+    if count:
+        make_summary = lambda log, lines : _make_summary(log, lines, ' (not shown)')
+    else:
+        def make_summary(log, lines):
+            if lines.stripped_lines:
+                if lines:
+                    note = ' (last %s lines shown)' %len(lines)
+                else:
+                    note = ' (not shown)'
+            else:
+                note = ''
+            return _make_summary(log, lines, note)
+
+    global weechat_format
+    if hilight:
+        # we don't want colors if there's match highlighting
+        format_line = lambda s : '%s %s %s' %split_line(s)
+    else:
+        def format_line(s):
+            global nick_dict, weechat_format
+            date, nick, msg = split_line(s)
+            if weechat_format:
+                try:
+                    nick = nick_dict[nick]
+                except KeyError:
+                    # cache nick
+                    nick_c = color_nick(nick)
+                    nick_dict[nick] = nick_c
+                    nick = nick_c
+                return '%s%s %s%s %s' %(color_date, date, nick, color_reset, msg)
+            else:
+                #no formatting
+                return msg
+
+    prnt(buffer, '\n')
+    print_line('Search for "%s%s%s"%s in %s%s%s.' %(color_summary, pattern_tmpl, color_info,
+        invert and ' (inverted)' or '', color_summary, matched_lines, color_reset),
+            buffer)
+    # print last <max_lines> lines
+    if matched_lines.get_matches_count():
+        if count:
+            # with count we sort by matches lines instead of just lines.
+            matched_lines_items = matched_lines.items_count()
+        else:
+            matched_lines_items = matched_lines.items()
+
+        matched_lines.get_last_lines(max_lines)
+        for log, lines in matched_lines_items:
+            if lines.matches_count:
+                # matched lines
+                if not count:
+                    # print lines
+                    weechat_format = True
+                    if exact:
+                        lines.onlyUniq()
+                    for line in lines:
+                        #debug(repr(line))
+                        if line == linesList._sep:
+                            # separator
+                            prnt(buffer, context_sep)
+                        else:
+                            if '\x00' in line:
+                                # log was corrupted
+                                error("Found garbage in log '%s', maybe it's corrupted" %log)
+                                line = line.replace('\x00', '')
+                            prnt_date_tags(buffer, 0, 'no_highlight', format_line(line))
+
+                # summary
+                if count or get_config_boolean('show_summary'):
+                    summary = make_summary(log, lines)
+                    print_line(summary, buffer)
+
+            # separator
+            if not count and lines:
+                prnt(buffer, '\n')
+    else:
+        print_line('No matches found.', buffer)
+
+    # set title
+    global time_start
+    time_end = now()
+    # total time
+    time_total = time_end - time_start
+    # percent of the total time used for grepping
+    time_grep_pct = (time_grep - time_start)/time_total*100
+    #debug('time: %.4f seconds (%.2f%%)' %(time_total, time_grep_pct))
+    if not count and len_total_lines > max_lines:
+        note = ' (last %s lines shown)' %len(matched_lines)
+    else:
+        note = ''
+    title = "'q': close buffer | Search in %s%s%s %s matches%s | pattern \"%s%s%s\"%s %s | %.4f seconds (%.2f%%)" \
+            %(color_title, matched_lines, color_reset, matched_lines.get_matches_count(), note,
+              color_title, pattern_tmpl, color_reset, invert and ' (inverted)' or '', format_options(),
+              time_total, time_grep_pct)
+    weechat.buffer_set(buffer, 'title', title)
+
+    if get_config_boolean('go_to_buffer'):
+        weechat.buffer_set(buffer, 'display', '1')
+
+    # free matched_lines so it can be removed from memory
+    del matched_lines
+    
+def split_line(s):
+    """Splits log's line 's' in 3 parts, date, nick and msg."""
+    global weechat_format
+    if weechat_format and s.count('\t') >= 2:
+        date, nick, msg = s.split('\t', 2) # date, nick, message
+    else:
+        # looks like log isn't in weechat's format
+        weechat_format = False # incoming lines won't be formatted
+        date, nick, msg = '', '', s
+    # remove tabs
+    if '\t' in msg:
+        msg = msg.replace('\t', '    ')
+    return date, nick, msg
+
+def print_line(s, buffer=None, display=False):
+    """Prints 's' in script's buffer as 'script_nick'. For displaying search summaries."""
+    if buffer is None:
+        buffer = buffer_create()
+    say('%s%s' %(color_info, s), buffer)
+    if display and get_config_boolean('go_to_buffer'):
+        weechat.buffer_set(buffer, 'display', '1')
+
+def format_options():
+    global matchcase, number, count, exact, hilight, invert
+    global tail, head, after_context, before_context
+    options = []
+    append = options.append
+    insert = options.insert
+    chars = 'cHmov'
+    for i, flag in enumerate((count, hilight, matchcase, exact, invert)):
+        if flag:
+            append(chars[i])
+
+    if head or tail:
+        n = get_config_int('default_tail_head')
+        if head:
+            append('h')
+            if head != n:
+                insert(-1, ' -')
+                append('n')
+                append(head)
+        elif tail:
+            append('t')
+            if tail != n:
+                insert(-1, ' -')
+                append('n')
+                append(tail)
+
+    if before_context and after_context and (before_context == after_context):
+        append(' -C')
+        append(before_context)
+    else:
+        if before_context:
+            append(' -B')
+            append(before_context)
+        if after_context:
+            append(' -A')
+            append(after_context)
+
+    s = ''.join(map(str, options)).strip()
+    if s and s[0] != '-':
+        s = '-' + s
+    return s
+
+def buffer_create(title=None):
+    """Returns our buffer pointer, creates and cleans the buffer if needed."""
+    buffer = weechat.buffer_search('python', SCRIPT_NAME)
+    if not buffer:
+        buffer = weechat.buffer_new(SCRIPT_NAME, 'buffer_input', '', '', '')
+        weechat.buffer_set(buffer, 'time_for_each_line', '0')
+        weechat.buffer_set(buffer, 'nicklist', '0')
+        weechat.buffer_set(buffer, 'title', title or 'grep output buffer')
+        weechat.buffer_set(buffer, 'localvar_set_no_log', '1')
+    elif title:
+        weechat.buffer_set(buffer, 'title', title)
+    return buffer
+
+def buffer_input(data, buffer, input_data):
+    """Repeats last search with 'input_data' as regexp."""
+    try:
+        cmd_grep_stop(buffer, input_data)
+    except:
+        return WEECHAT_RC_OK
+    if input_data in ('q', 'Q'):
+        weechat.buffer_close(buffer)
+        return weechat.WEECHAT_RC_OK
+
+    global search_in_buffers, search_in_files
+    global pattern
+    try:
+        if pattern and (search_in_files or search_in_buffers):
+            # check if the buffer pointers are still valid
+            for pointer in search_in_buffers:
+                infolist = weechat.infolist_get('buffer', pointer, '')
+                if not infolist:
+                    del search_in_buffers[search_in_buffers.index(pointer)]
+                weechat.infolist_free(infolist)
+            try:
+                cmd_grep_parsing(input_data)
+            except Exception, e:
+                error('Argument error, %s' %e, buffer=buffer)
+                return WEECHAT_RC_OK
+            try:
+                show_matching_lines()
+            except Exception, e:
+                error(e)
+    except NameError:
+        error("There isn't any previous search to repeat.", buffer=buffer)
+    return WEECHAT_RC_OK
+
+### Commands ###
+def cmd_init():
+    """Resets global vars."""
+    global home_dir, cache_dir, nick_dict
+    global pattern_tmpl, pattern, matchcase, number, count, exact, hilight, invert
+    global tail, head, after_context, before_context
+    hilight = ''
+    head = tail = after_context = before_context = invert = False
+    matchcase = count = exact = False
+    pattern_tmpl = pattern = number = None
+    home_dir = get_home()
+    cache_dir = {} # for avoid walking the dir tree more than once per command
+    nick_dict = {} # nick cache for don't calculate nick color every time
+
+def cmd_grep_parsing(args):
+    """Parses args for /grep and grep input buffer."""
+    global pattern_tmpl, pattern, matchcase, number, count, exact, hilight, invert
+    global tail, head, after_context, before_context
+    global log_name, buffer_name, only_buffers, all
+    opts, args = getopt.gnu_getopt(args.split(), 'cmHeahtivn:bA:B:C:o', ['count', 'matchcase', 'hilight',
+        'exact', 'all', 'head', 'tail', 'number=', 'buffer', 'after-context=', 'before-context=',
+        'context=', 'invert', 'only-match'])
+    #debug(opts, 'opts: '); debug(args, 'args: ')
+    if len(args) >= 2:
+        if args[0] == 'log':
+            del args[0]
+            log_name = args.pop(0)
+        elif args[0] == 'buffer':
+            del args[0]
+            buffer_name = args.pop(0)
+
+    def tmplReplacer(match):
+        """This function will replace templates with regexps"""
+        s = match.groups()[0]
+        tmpl_args = s.split()
+        tmpl_key, _, tmpl_args = s.partition(' ')
+        try:
+            template = templates[tmpl_key]
+            if callable(template):
+                r = template(tmpl_args)
+                if not r:
+                    error("Template %s returned empty string "\
+                          "(WeeChat doesn't have enough data)." %t)
+                return r
+            else:
+                return template
+        except:
+            return t
+
+    args = ' '.join(args) # join pattern for keep spaces
+    if args:
+        pattern_tmpl = args  
+        pattern = _tmplRe.sub(tmplReplacer, args)
+        debug('Using regexp: %s', pattern)
+    if not pattern:
+        raise Exception, 'No pattern for grep the logs.'
+
+    def positive_number(opt, val):
+        try:
+            number = int(val)
+            if number < 0:
+                raise ValueError
+            return number
+        except ValueError:
+            if len(opt) == 1:
+                opt = '-' + opt
+            else:
+                opt = '--' + opt
+            raise Exception, "argument for %s must be a positive integer." %opt
+
+    for opt, val in opts:
+        opt = opt.strip('-')
+        if opt in ('c', 'count'):
+            count = not count
+        elif opt in ('m', 'matchcase'):
+            matchcase = not matchcase
+        elif opt in ('H', 'hilight'):
+            # hilight must be always a string!
+            if hilight:
+                hilight = ''
+            else:
+                hilight = '%s,%s' %(color_hilight, color_reset)
+            # we pass the colors in the variable itself because check_string() must not use
+            # weechat's module when applying the colors (this is for grep in a hooked process)
+        elif opt in ('e', 'exact', 'o', 'only-match'):
+            exact = not exact
+            invert = False
+        elif opt in ('a', 'all'):
+            all = not all
+        elif opt in ('h', 'head'):
+            head = not head
+            tail = False
+        elif opt in ('t', 'tail'):
+            tail = not tail
+            head = False
+        elif opt in ('b', 'buffer'):
+            only_buffers = True
+        elif opt in ('n', 'number'):
+            number = positive_number(opt, val)
+        elif opt in ('C', 'context'):
+            n = positive_number(opt, val)
+            after_context = n
+            before_context = n
+        elif opt in ('A', 'after-context'):
+            after_context = positive_number(opt, val)
+        elif opt in ('B', 'before-context'):
+            before_context = positive_number(opt, val)
+        elif opt in ('i', 'v', 'invert'):
+            invert = not invert
+            exact = False
+    # number check
+    if number is not None:
+        if number == 0:
+            head = tail = False
+            number = None
+        elif head:
+            head = number
+        elif tail:
+            tail = number
+    else:
+        n = get_config_int('default_tail_head')
+        if head:
+            head = n
+        elif tail:
+            tail = n
+
+def cmd_grep_stop(buffer, args):
+    global hook_file_grep, pattern, matched_lines, tmpFile
+    if hook_file_grep:
+        if args == 'stop':
+            weechat.unhook(hook_file_grep)
+            hook_file_grep = None
+            s = 'Search for \'%s\' stopped.' %pattern
+            say(s, buffer)
+            grep_buffer = weechat.buffer_search('python', SCRIPT_NAME)
+            if grep_buffer:
+                weechat.buffer_set(grep_buffer, 'title', s)
+            del matched_lines
+            tmpFile = None
+        else:
+            say(get_grep_file_status(), buffer)
+        raise Exception
+
+def cmd_grep(data, buffer, args):
+    """Search in buffers and logs."""
+    global pattern, matchcase, head, tail, number, count, exact, hilight
+    try:
+        cmd_grep_stop(buffer, args)
+    except:
+        return WEECHAT_RC_OK
+
+    if not args:
+        weechat.command('', '/help %s' %SCRIPT_COMMAND)
+        return WEECHAT_RC_OK
+
+    cmd_init()
+    global log_name, buffer_name, only_buffers, all
+    log_name = buffer_name = ''
+    only_buffers = all = False
+
+    # parse
+    try:
+        cmd_grep_parsing(args)
+    except Exception, e:
+        error('Argument error, %s' %e)
+        return WEECHAT_RC_OK
+
+    # find logs
+    log_file = search_buffer = None
+    if log_name:
+        log_file = get_file_by_pattern(log_name, all)
+        if not log_file:
+            error("Couldn't find any log for %s. Try /logs" %log_name)
+            return WEECHAT_RC_OK
+    elif all:
+        search_buffer = get_all_buffers()
+    elif buffer_name:
+        search_buffer = get_buffer_by_name(buffer_name)
+        if not search_buffer:
+            # there's no buffer, try in the logs
+            log_file = get_file_by_name(buffer_name)
+            if not log_file:
+                error("Logs or buffer for '%s' not found." %buffer_name)
+                return WEECHAT_RC_OK
+        else:
+            search_buffer = [search_buffer]
+    else:
+        search_buffer = [buffer]
+
+    # make the log list
+    global search_in_files, search_in_buffers
+    search_in_files = []
+    search_in_buffers = []
+    if log_file:
+        search_in_files = log_file
+    elif not only_buffers:
+        #debug(search_buffer)
+        for pointer in search_buffer:
+            log = get_file_by_buffer(pointer)
+            #debug('buffer %s log %s' %(pointer, log))
+            if log:
+                search_in_files.append(log)
+            else:
+                search_in_buffers.append(pointer)
+    else:
+        search_in_buffers = search_buffer
+
+    # grepping
+    try:
+        show_matching_lines()
+    except Exception, e:
+        error(e)
+    return WEECHAT_RC_OK
+
+def cmd_logs(data, buffer, args):
+    """List files in Weechat's log dir."""
+    cmd_init()
+    global home_dir
+    sort_by_size = False
+    filter = []
+
+    try:
+        opts, args = getopt.gnu_getopt(args.split(), 's', ['size'])
+        if args:
+            filter = args
+        for opt, var in opts:
+            opt = opt.strip('-')
+            if opt in ('size', 's'):
+                sort_by_size = True
+    except Exception, e:
+        error('Argument error, %s' %e)
+        return WEECHAT_RC_OK
+
+    # is there's a filter, filter_excludes should be False
+    file_list = dir_list(home_dir, filter, filter_excludes=not filter)
+    if sort_by_size:
+        file_list.sort(key=get_size)
+    else:
+        file_list.sort()
+
+    file_sizes = map(lambda x: human_readable_size(get_size(x)), file_list)
+    # calculate column lenght
+    if file_list:
+        L = file_list[:]
+        L.sort(key=len)
+        bigest = L[-1]
+        column_len = len(bigest) + 3
+    else:
+        column_len = ''
+
+    buffer = buffer_create()
+    if get_config_boolean('clear_buffer'):
+        weechat.buffer_clear(buffer)
+    file_list = zip(file_list, file_sizes)
+    msg = 'Found %s logs.' %len(file_list)
+
+    print_line(msg, buffer, display=True)
+    for file, size in file_list:
+        separator = column_len and '.'*(column_len - len(file))
+        prnt(buffer, '%s %s %s' %(strip_home(file), separator, size))
+    if file_list:
+        print_line(msg, buffer)
+    return WEECHAT_RC_OK
+
+
+### Completion ###
+def completion_log_files(data, completion_item, buffer, completion):
+    #debug('completion: %s' %', '.join((data, completion_item, buffer, completion)))
+    global home_dir
+    l = len(home_dir)
+    completion_list_add = weechat.hook_completion_list_add
+    WEECHAT_LIST_POS_END = weechat.WEECHAT_LIST_POS_END
+    for log in dir_list(home_dir):
+        completion_list_add(completion, log[l:], 0, WEECHAT_LIST_POS_END)
+    return WEECHAT_RC_OK
+
+def completion_grep_args(data, completion_item, buffer, completion):
+    for arg in ('count', 'all', 'matchcase', 'hilight', 'exact', 'head', 'tail', 'number', 'buffer',
+            'after-context', 'before-context', 'context', 'invert', 'only-match'):
+        weechat.hook_completion_list_add(completion, '--' + arg, 0, weechat.WEECHAT_LIST_POS_SORT)
+    for tmpl in templates:
+        weechat.hook_completion_list_add(completion, '%{' + tmpl, 0, weechat.WEECHAT_LIST_POS_SORT)
+    return WEECHAT_RC_OK
+
+
+### Templates ###
+# template placeholder
+_tmplRe = re.compile(r'%\{(\w+.*?)(?:\}|$)')
+# will match 999.999.999.999 but I don't care
+ipAddress = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
+domain = r'[\w-]{2,}(?:\.[\w-]{2,})*\.[a-z]{2,}'
+url = r'\w+://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?' % (domain, ipAddress)
+
+def make_url_regexp(args):
+    #debug('make url: %s', args)
+    if args:
+        words = r'(?:%s)' %'|'.join(map(re.escape, args.split()))
+        return r'(?:\w+://|www\.)[^\s]*%s[^\s]*(?:/[^\])>\s]*)?' %words
+    else:
+        return url
+
+def make_simple_regexp(pattern):
+    s = ''
+    for c in pattern:
+        if c == '*':
+            s += '.*'
+        elif c == '?':
+            s += '.'
+        else:
+            s += re.escape(c)
+    return s
+
+templates = {
+            'ip': ipAddress,
+           'url': make_url_regexp,
+        'escape': lambda s: re.escape(s),
+        'simple': make_simple_regexp,
+        'domain': domain,
+        }
+
+### Main ###
+def delete_bytecode():
+    global script_path
+    bytecode = path.join(script_path, SCRIPT_NAME + '.pyc')
+    if path.isfile(bytecode):
+        os.remove(bytecode)
+    return WEECHAT_RC_OK
+
+if __name__ == '__main__' and import_ok and \
+        weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, \
+        SCRIPT_DESC, 'delete_bytecode', ''):
+    home_dir = get_home()
+
+    # for import ourselves
+    global script_path
+    script_path = path.dirname(__file__)
+    sys.path.append(script_path)
+    delete_bytecode()
+
+    # check python version
+    import sys
+    global bytecode
+    if sys.version_info > (2, 6):
+        bytecode = 'B'
+    else:
+        bytecode = ''
+
+
+    weechat.hook_command(SCRIPT_COMMAND, cmd_grep.__doc__,
+            "[log <file> | buffer <name> | stop] [-a|--all] [-b|--buffer] [-c|--count] [-m|--matchcase] "
+            "[-H|--hilight] [-o|--only-match] [-i|-v|--invert] [(-h|--head)|(-t|--tail) [-n|--number <n>]] "
+            "[-A|--after-context <n>] [-B|--before-context <n>] [-C|--context <n> ] <expression>",
+# help
+"""
+     log <file>: Search in one log that matches <file> in the logger path.
+                 Use '*' and '?' as wildcards.
+  buffer <name>: Search in buffer <name>, if there's no buffer with <name> it will
+                 try to search for a log file.
+           stop: Stops a currently running search.
+       -a --all: Search in all open buffers.
+                 If used with 'log <file>' search in all logs that matches <file>.
+    -b --buffer: Search only in buffers, not in file logs.
+     -c --count: Just count the number of matched lines instead of showing them.
+ -m --matchcase: Don't do case insensible search.
+   -H --hilight: Colour exact matches in output buffer.
+-o --only-match: Print only the matching part of the line (unique matches).
+ -v -i --invert: Print lines that don't match the regular expression.
+      -t --tail: Print the last 10 matching lines.
+      -h --head: Print the first 10 matching lines.
+-n --number <n>: Overrides default number of lines for --tail or --head.
+-A --after-context <n>: Shows <n> lines of trailing context after matching lines.
+-B --before-context <n>: Shows <n> lines of leading context before matching lines.
+-C --context <n>: Same as using both --after-context and --before-context simultaneously.
+  <expression>: Expression to search.
+
+Grep buffer:
+  Input line accepts most arguments of /grep, it'll repeat last search using the new
+  arguments provided. You can't search in different logs from the buffer's input.
+  Boolean arguments like --count, --tail, --head, --hilight, ... are toggleable
+
+Python regular expression syntax:
+  See http://docs.python.org/lib/re-syntax.html
+
+Grep Templates:
+     %{url [text]}: Matches anything like an url, or an url with text.
+             %{ip}: Matches anything that looks like an ip.
+         %{domain}: Matches anything like a domain.
+    %{escape text}: Escapes text in pattern.
+ %{simple pattern}: Converts a pattern with '*' and '?' wildcards into a regexp.
+
+Examples:
+  Search for urls with the word 'weechat' said by 'nick'
+    /grep nick\\t.*%{url weechat}
+  Search for '*.*' string
+    /grep %{escape *.*}
+""",
+            # completion template
+            "buffer %(buffers_names) %(grep_arguments)|%*"
+            "||log %(grep_log_files) %(grep_arguments)|%*"
+            "||stop"
+            "||%(grep_arguments)|%*",
+            'cmd_grep' ,'')
+    weechat.hook_command('logs', cmd_logs.__doc__, "[-s|--size] [<filter>]",
+            "-s --size: Sort logs by size.\n"
+            " <filter>: Only show logs that match <filter>. Use '*' and '?' as wildcards.", '--size', 'cmd_logs', '')
+
+    weechat.hook_completion('grep_log_files', "list of log files",
+            'completion_log_files', '')
+    weechat.hook_completion('grep_arguments', "list of arguments",
+            'completion_grep_args', '')
+
+    # settings
+    for opt, val in settings.iteritems():
+        if not weechat.config_is_set_plugin(opt):
+            weechat.config_set_plugin(opt, val)
+
+    # colors
+    color_date        = weechat.color('brown')
+    color_info        = weechat.color('cyan')
+    color_hilight     = weechat.color('lightred')
+    color_reset       = weechat.color('reset')
+    color_title       = weechat.color('yellow')
+    color_summary     = weechat.color('lightcyan')
+    color_delimiter   = weechat.color('chat_delimiters')
+    color_script_nick = weechat.color('chat_nick')
+    
+    # pretty [grep]
+    script_nick = '%s[%s%s%s]%s' %(color_delimiter, color_script_nick, SCRIPT_NAME, color_delimiter,
+            color_reset)
+    script_nick_nocolor = '[%s]' %SCRIPT_NAME
+    # paragraph separator when using context options
+    context_sep = '%s\t%s--' %(script_nick, color_info)
+
+    # -------------------------------------------------------------------------
+    # Debug
+
+    if weechat.config_get_plugin('debug'):
+        try:
+            # custom debug module I use, allows me to inspect script's objects.
+            import pybuffer
+            debug = pybuffer.debugBuffer(globals(), '%s_debug' % SCRIPT_NAME)
+        except:
+            def debug(s, *args):
+                if not isinstance(s, basestring):
+                    s = str(s)
+                if args:
+                    s = s %args
+                prnt('', '%s\t%s' %(script_nick, s))
+    else:
+        def debug(*args):
+            pass
+
+# vim:set shiftwidth=4 tabstop=4 softtabstop=4 expandtab textwidth=100:
diff --git a/weechat/.weechat/python/otr.py b/weechat/.weechat/python/otr.py
new file mode 100644
index 0000000..404e96a
--- /dev/null
+++ b/weechat/.weechat/python/otr.py
@@ -0,0 +1,2062 @@
+# -*- coding: utf-8 -*-
+# otr - WeeChat script for Off-the-Record IRC messaging
+#
+# DISCLAIMER: To the best of my knowledge this script securely provides OTR
+# messaging in WeeChat, but I offer no guarantee. Please report any security
+# holes you find.
+#
+# Copyright (c) 2012-2015 Matthew M. Boedicker <matthewm@boedicker.org>
+#                         Nils Görs <weechatter@arcor.de>
+#                         Daniel "koolfy" Faucon <koolfy@koolfy.be>
+#                         Felix Eckhofer <felix@tribut.de>
+#
+# Report issues at https://github.com/mmb/weechat-otr
+#
+# 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/>.
+
+from __future__ import unicode_literals
+
+import collections
+import glob
+import io
+import os
+import platform
+import re
+import traceback
+import shlex
+import shutil
+import sys
+
+class PythonVersion2(object):
+    """Python 2 version of code that must differ between Python 2 and 3."""
+
+    def __init__(self):
+        import cgi
+        self.cgi = cgi
+
+        import HTMLParser
+        self.html_parser = HTMLParser
+        self.html_parser_init_kwargs = {}
+
+        import htmlentitydefs
+        self.html_entities = htmlentitydefs
+
+    def html_escape(self, strng):
+        """Escape HTML characters in a string."""
+        return self.cgi.escape(strng)
+
+    def unicode(self, *args, **kwargs):
+        """Return the Unicode version of a string."""
+        return unicode(*args, **kwargs)
+
+    def unichr(self, *args, **kwargs):
+        """Return the one character string of a Unicode character number."""
+        return unichr(*args, **kwargs)
+
+    def to_unicode(self, strng):
+        """Convert a utf-8 encoded string to a Unicode."""
+        if isinstance(strng, unicode):
+            return strng
+        else:
+            return strng.decode('utf-8', 'replace')
+
+    def to_str(self, strng):
+        """Convert a Unicode to a utf-8 encoded string."""
+        return strng.encode('utf-8', 'replace')
+
+class PythonVersion3(object):
+    """Python 3 version of code that must differ between Python 2 and 3."""
+
+    def __init__(self, minor):
+        self.minor = minor
+
+        import html
+        self.html = html
+
+        import html.parser
+        self.html_parser = html.parser
+        if self.minor >= 4:
+            self.html_parser_init_kwargs = { 'convert_charrefs' : True }
+        else:
+            self.html_parser_init_kwargs = {}
+
+        import html.entities
+        self.html_entities = html.entities
+
+    def html_escape(self, strng):
+        """Escape HTML characters in a string."""
+        return self.html.escape(strng, quote=False)
+
+    def unicode(self, *args, **kwargs):
+        """Return the Unicode version of a string."""
+        return str(*args, **kwargs)
+
+    def unichr(self, *args, **kwargs):
+        """Return the one character string of a Unicode character number."""
+        return chr(*args, **kwargs)
+
+    def to_unicode(self, strng):
+        """Convert a utf-8 encoded string to unicode."""
+        if isinstance(strng, bytes):
+            return strng.decode('utf-8', 'replace')
+        else:
+            return strng
+
+    def to_str(self, strng):
+        """Convert a Unicode to a utf-8 encoded string."""
+        return strng
+
+if sys.version_info.major >= 3:
+    PYVER = PythonVersion3(sys.version_info.minor)
+else:
+    PYVER = PythonVersion2()
+
+import weechat
+
+import potr
+
+SCRIPT_NAME = 'otr'
+SCRIPT_DESC = 'Off-the-Record messaging for IRC'
+SCRIPT_HELP = """{description}
+
+Quick start:
+
+Add an OTR item to the status bar by adding '[otr]' to the config setting
+weechat.bar.status.items. This will show you whether your current conversation
+is encrypted, authenticated and logged. /set otr.* for OTR status bar
+customization options.
+
+Start a private conversation with a friend who has OTR: /query yourpeer hi
+
+In the private chat buffer: /otr start
+
+If you have not authenticated your peer yet, follow the instructions for
+authentication.
+
+You can, at any time, see the current OTR session status and fingerprints with:
+/otr status
+
+View OTR policies for your peer: /otr policy
+
+View default OTR policies: /otr policy default
+
+Start/Stop log recording for the current OTR session: /otr log [start|stop]
+This will be reverted back to the previous log setting at the end of the session.
+
+To refresh the OTR session: /otr refresh
+
+To end your private conversation: /otr finish
+
+This script supports only OTR protocol version 2.
+""".format(description=SCRIPT_DESC)
+
+SCRIPT_AUTHOR = 'Matthew M. Boedicker'
+SCRIPT_LICENCE = 'GPL3'
+SCRIPT_VERSION = '1.8.0'
+
+OTR_DIR_NAME = 'otr'
+
+OTR_QUERY_RE = re.compile(r'\?OTR(\?|\??v[a-z\d]*\?)')
+
+POLICIES = {
+    'allow_v2' : 'allow OTR protocol version 2, effectively enable OTR '
+        'since v2 is the only supported version',
+    'require_encryption' : 'refuse to send unencrypted messages when OTR is '
+        'enabled',
+    'log' : 'enable logging of OTR conversations',
+    'send_tag' : 'advertise your OTR capability using the whitespace tag',
+    'html_escape' : 'escape HTML special characters in outbound messages',
+    'html_filter' : 'filter HTML in incoming messages',
+    }
+
+READ_ONLY_POLICIES = {
+    'allow_v1' : False,
+    }
+
+ACTION_PREFIX = '/me '
+IRC_ACTION_RE = re.compile('^\x01ACTION (?P<text>.*)\x01$')
+PLAIN_ACTION_RE = re.compile('^'+ACTION_PREFIX+'(?P<text>.*)$')
+
+IRC_SANITIZE_TABLE = dict((ord(char), None) for char in '\n\r\x00')
+
+global otr_debug_buffer
+otr_debug_buffer = None
+
+# Patch potr.proto.TaggedPlaintext to not end plaintext tags in a space.
+#
+# When POTR adds OTR tags to plaintext it puts them at the end of the message.
+# The tags end in a space which gets stripped off by WeeChat because it
+# strips trailing spaces from commands. This causes OTR initiation to fail so
+# the following code adds an extra tab at the end of the plaintext tags if
+# they end in a space.
+#
+# The patched version also skips OTR tagging for CTCP messages because it
+# breaks the CTCP format.
+def patched__bytes__(self):
+    # Do not tag CTCP messages.
+    if self.msg.startswith(b'\x01') and \
+        self.msg.endswith(b'\x01'):
+        return self.msg
+
+    data = self.msg + potr.proto.MESSAGE_TAG_BASE
+    for v in self.versions:
+        data += potr.proto.MESSAGE_TAGS[v]
+    if data.endswith(b' '):
+        data += b'\t'
+    return data
+
+potr.proto.TaggedPlaintext.__bytes__ = patched__bytes__
+
+def command(buf, command_str):
+    """Wrap weechat.command() with utf-8 encode."""
+    debug(command_str)
+    weechat.command(buf, PYVER.to_str(command_str))
+
+def privmsg(server, nick, message):
+    """Send a private message to a nick."""
+    for line in message.splitlines():
+        command('', '/quote -server {server} PRIVMSG {nick} :{line}'.format(
+            server=irc_sanitize(server),
+            nick=irc_sanitize(nick),
+            line=irc_sanitize(line)))
+
+def build_privmsg_in(fromm, target, msg):
+    """Build inbound IRC PRIVMSG command."""
+    return ':{user} PRIVMSG {target} :{msg}'.format(
+        user=irc_sanitize(fromm),
+        target=irc_sanitize(target),
+        msg=irc_sanitize(msg))
+
+def build_privmsgs_in(fromm, target, msg, prefix=''):
+    """Build an inbound IRC PRIVMSG command for each line in msg.
+    If prefix is supplied, prefix each line of msg with it."""
+    cmd = []
+    for line in msg.splitlines():
+        cmd.append(build_privmsg_in(fromm, target, prefix+line))
+    return '\r\n'.join(cmd)
+
+def build_privmsg_out(target, msg):
+    """Build outbound IRC PRIVMSG command(s)."""
+    cmd = []
+    for line in msg.splitlines():
+        cmd.append('PRIVMSG {target} :{line}'.format(
+            target=irc_sanitize(target),
+            line=irc_sanitize(line)))
+    return '\r\n'.join(cmd)
+
+def irc_sanitize(msg):
+    """Remove NUL, CR and LF characters from msg.
+    The (utf-8 encoded version of a) string returned from this function
+    should be safe to use as an argument in an irc command."""
+    return PYVER.unicode(msg).translate(IRC_SANITIZE_TABLE)
+
+def prnt(buf, message):
+    """Wrap weechat.prnt() with utf-8 encode."""
+    weechat.prnt(buf, PYVER.to_str(message))
+
+def print_buffer(buf, message, level='info'):
+    """Print message to buf with prefix,
+    using color according to level."""
+    prnt(buf, '{prefix}\t{msg}'.format(
+        prefix=get_prefix(),
+        msg=colorize(message, 'buffer.{}'.format(level))))
+
+def get_prefix():
+    """Returns configured message prefix."""
+    return weechat.string_eval_expression(
+        config_string('look.prefix'),
+        {}, {}, {})
+
+def debug(msg):
+    """Send a debug message to the OTR debug buffer."""
+    debug_option = weechat.config_get(config_prefix('general.debug'))
+    global otr_debug_buffer
+
+    if weechat.config_boolean(debug_option):
+        if not otr_debug_buffer:
+            otr_debug_buffer = weechat.buffer_new("OTR Debug", "", "",
+                "debug_buffer_close_cb", "")
+            weechat.buffer_set(otr_debug_buffer, 'title', 'OTR Debug')
+            weechat.buffer_set(otr_debug_buffer, 'localvar_set_no_log', '1')
+        prnt(otr_debug_buffer, ('{script} debug\t{text}'.format(
+            script=SCRIPT_NAME,
+            text=PYVER.unicode(msg)
+            )))
+
+def debug_buffer_close_cb(data, buf):
+    """Set the OTR debug buffer to None."""
+    global otr_debug_buffer
+    otr_debug_buffer = None
+    return weechat.WEECHAT_RC_OK
+
+def current_user(server_name):
+    """Get the nick and server of the current user on a server."""
+    return irc_user(info_get('irc_nick', server_name), server_name)
+
+def irc_user(nick, server):
+    """Build an IRC user string from a nick and server."""
+    return '{nick}@{server}'.format(
+            nick=nick.lower(),
+            server=server)
+
+def isupport_value(server, feature):
+    """Get the value of an IRC server feature."""
+    args = '{server},{feature}'.format(server=server, feature=feature)
+    return info_get('irc_server_isupport_value', args)
+
+def is_a_channel(channel, server):
+    """Return true if a string has an IRC channel prefix."""
+    prefixes = \
+        tuple(isupport_value(server, 'CHANTYPES')) + \
+        tuple(isupport_value(server, 'STATUSMSG'))
+
+    # If the server returns nothing for CHANTYPES and STATUSMSG use
+    # default prefixes.
+    if not prefixes:
+        prefixes = ('#', '&', '+', '!', '@')
+
+    return channel.startswith(prefixes)
+
+# Exception class for PRIVMSG parsing exceptions.
+class PrivmsgParseException(Exception):
+    pass
+
+def parse_irc_privmsg(message, server):
+    """Parse an IRC PRIVMSG command and return a dictionary.
+
+    Either the to_channel key or the to_nick key will be set depending on
+    whether the message is to a nick or a channel. The other will be None.
+
+    Example input:
+
+    :nick!user@host PRIVMSG #weechat :message here
+
+    Output:
+
+    {'from': 'nick!user@host',
+    'from_nick': 'nick',
+    'to': '#weechat',
+    'to_channel': '#weechat',
+    'to_nick': None,
+    'text': 'message here'}
+    """
+
+    weechat_result = weechat.info_get_hashtable(
+        'irc_message_parse', dict(message=message))
+
+    if weechat_result['command'].upper() == 'PRIVMSG':
+        target, text = PYVER.to_unicode(
+            weechat_result['arguments']).split(' ', 1)
+        if text.startswith(':'):
+            text = text[1:]
+
+        result = {
+            'from': PYVER.to_unicode(weechat_result['host']),
+            'to' : target,
+            'text': text,
+            }
+
+        if weechat_result['host']:
+            result['from_nick'] = PYVER.to_unicode(weechat_result['nick'])
+        else:
+            result['from_nick'] = ''
+
+        if is_a_channel(target, server):
+            result['to_channel'] = target
+            result['to_nick'] = None
+        else:
+            result['to_channel'] = None
+            result['to_nick'] = target
+
+        return result
+    else:
+        raise PrivmsgParseException(message)
+
+def has_otr_end(msg):
+    """Return True if the message is the end of an OTR message."""
+    return msg.endswith('.') or msg.endswith(',')
+
+def first_instance(objs, klass):
+    """Return the first object in the list that is an instance of a class."""
+    for obj in objs:
+        if isinstance(obj, klass):
+            return obj
+
+def config_prefix(option):
+    """Add the config prefix to an option and return the full option name."""
+    return '{script}.{option}'.format(
+            script=SCRIPT_NAME,
+            option=option)
+
+def config_color(option):
+    """Get the color of a color config option."""
+    return weechat.color(weechat.config_color(weechat.config_get(
+            config_prefix('color.{}'.format(option)))))
+
+def config_string(option):
+    """Get the string value of a config option with utf-8 decode."""
+    return PYVER.to_unicode(weechat.config_string(
+        weechat.config_get(config_prefix(option))))
+
+def buffer_get_string(buf, prop):
+    """Wrap weechat.buffer_get_string() with utf-8 encode/decode."""
+    if buf is not None:
+        encoded_buf = PYVER.to_str(buf)
+    else:
+        encoded_buf = None
+
+    return PYVER.to_unicode(weechat.buffer_get_string(
+        encoded_buf, PYVER.to_str(prop)))
+
+def buffer_is_private(buf):
+    """Return True if a buffer is private."""
+    return buffer_get_string(buf, 'localvar_type') == 'private'
+
+def info_get(info_name, arguments):
+    """Wrap weechat.info_get() with utf-8 encode/decode."""
+    return PYVER.to_unicode(weechat.info_get(
+        PYVER.to_str(info_name), PYVER.to_str(arguments)))
+
+def msg_irc_from_plain(msg):
+    """Transform a plain-text message to irc format.
+    This will replace lines that start with /me with the respective
+    irc command."""
+    return PLAIN_ACTION_RE.sub('\x01ACTION \g<text>\x01', msg)
+
+def msg_plain_from_irc(msg):
+    """Transform an irc message to plain-text.
+    Any ACTION found will be rewritten as /me <text>."""
+    return IRC_ACTION_RE.sub(ACTION_PREFIX + r'\g<text>', msg)
+
+def default_peer_args(args, buf):
+    """Get the nick and server of a remote peer from command arguments or
+    a buffer.
+
+    args is the [nick, server] slice of arguments from a command.
+    If these are present, return them. If args is empty and the buffer buf
+    is private, return the remote nick and server of buf."""
+    result = None, None
+
+    if len(args) == 2:
+        result = tuple(args)
+    else:
+        if buffer_is_private(buf):
+            result = (
+                buffer_get_string(buf, 'localvar_channel'),
+                buffer_get_string(buf, 'localvar_server'))
+
+    return result
+
+def format_default_policies():
+    """Return current default policies formatted as a string for the user."""
+    buf = io.StringIO()
+
+    buf.write('Current default OTR policies:\n')
+
+    for policy, desc in sorted(POLICIES.items()):
+        buf.write('  {policy} ({desc}) : {value}\n'.format(
+                policy=policy,
+                desc=desc,
+                value=config_string('policy.default.{}'.format(policy))))
+
+    buf.write('Change default policies with: /otr policy default NAME on|off')
+
+    return buf.getvalue()
+
+def to_bytes(strng):
+    """Convert a python str or unicode to bytes."""
+    return strng.encode('utf-8', 'replace')
+
+def colorize(msg, color):
+    """Colorize each line of msg using color."""
+    result = []
+    colorstr = config_color(color)
+
+    for line in msg.splitlines():
+        result.append('{color}{msg}'.format(
+            color=colorstr,
+            msg=line))
+
+    return '\r\n'.join(result)
+
+def accounts():
+    """Return a list of all IrcOtrAccounts sorted by name."""
+    result = []
+    for key_path in glob.iglob(os.path.join(OTR_DIR, '*.key3')):
+        key_name, _ = os.path.splitext(os.path.basename(key_path))
+        result.append(ACCOUNTS[key_name])
+
+    return sorted(result, key=lambda account: account.name)
+
+def show_account_fingerprints():
+    """Print all account names and their fingerprints to the core buffer."""
+    table_formatter = TableFormatter()
+    for account in accounts():
+        table_formatter.add_row([
+          account.name,
+          str(account.getPrivkey())])
+    print_buffer('', table_formatter.format())
+
+def show_peer_fingerprints(grep=None):
+    """Print peer names and their fingerprints to the core buffer.
+
+    If grep is passed in, show all peer names containing that substring."""
+    trust_descs = {
+        '' : 'unverified',
+        'smp' : 'SMP verified',
+        'verified' : 'verified',
+        }
+
+    table_formatter = TableFormatter()
+    for account in accounts():
+        for peer, peer_data in sorted(account.trusts.items()):
+            for fingerprint, trust in sorted(peer_data.items()):
+                if grep is None or grep in peer:
+                    table_formatter.add_row([
+                      peer,
+                      account.name,
+                      potr.human_hash(fingerprint),
+                      trust_descs[trust],
+                    ])
+    print_buffer('', table_formatter.format())
+
+def private_key_file_path(account_name):
+    """Return the private key file path for an account."""
+    return os.path.join(OTR_DIR, '{}.key3'.format(account_name))
+
+def read_private_key(key_file_path):
+    """Return the private key in a private key file."""
+    debug(('read private key', key_file_path))
+
+    with open(key_file_path, 'rb') as key_file:
+        return potr.crypt.PK.parsePrivateKey(key_file.read())[0]
+
+class AccountDict(collections.defaultdict):
+    """Dictionary that adds missing keys as IrcOtrAccount instances."""
+
+    def __missing__(self, key):
+        debug(('add account', key))
+        self[key] = IrcOtrAccount(key)
+
+        return self[key]
+
+class Assembler(object):
+    """Reassemble fragmented OTR messages.
+
+    This does not deal with OTR fragmentation, which is handled by potr, but
+    fragmentation of received OTR messages that are too large for IRC.
+    """
+    def __init__(self):
+        self.clear()
+
+    def add(self, data):
+        """Add data to the buffer."""
+        self.value += data
+
+    def clear(self):
+        """Empty the buffer."""
+        self.value = ''
+
+    def is_done(self):
+        """Return True if the buffer is a complete message."""
+        return self.is_query() or \
+            not to_bytes(self.value).startswith(potr.proto.OTRTAG) or \
+            has_otr_end(self.value)
+
+    def get(self):
+        """Return the current value of the buffer and empty it."""
+        result = self.value
+        self.clear()
+
+        return result
+
+    def is_query(self):
+        """Return true if the buffer is an OTR query."""
+        return OTR_QUERY_RE.search(self.value)
+
+class IrcContext(potr.context.Context):
+    """Context class for OTR over IRC."""
+
+    def __init__(self, account, peername):
+        super(IrcContext, self).__init__(account, peername)
+
+        self.peer_nick, self.peer_server = peername.split('@', 1)
+        self.in_assembler = Assembler()
+        self.in_otr_message = False
+        self.in_smp = False
+        self.smp_question = False
+
+    def policy_config_option(self, policy):
+        """Get the option name of a policy option for this context."""
+        return config_prefix('.'.join([
+                    'policy', self.peer_server, self.user.nick, self.peer_nick,
+                    policy.lower()]))
+
+    def getPolicy(self, key):
+        """Get the value of a policy option for this context."""
+        key_lower = key.lower()
+
+        if key_lower in READ_ONLY_POLICIES:
+            result = READ_ONLY_POLICIES[key_lower]
+        elif key_lower == 'send_tag' and self.no_send_tag():
+            result = False
+        else:
+            option = weechat.config_get(
+                PYVER.to_str(self.policy_config_option(key)))
+
+            if option == '':
+                option = weechat.config_get(
+                    PYVER.to_str(self.user.policy_config_option(key)))
+
+            if option == '':
+                option = weechat.config_get(config_prefix('.'.join(
+                    ['policy', self.peer_server, key_lower])))
+
+            if option == '':
+                option = weechat.config_get(
+                    config_prefix('policy.default.{}'.format(key_lower)))
+
+            result = bool(weechat.config_boolean(option))
+
+        debug(('getPolicy', key, result))
+
+        return result
+
+    def inject(self, msg, appdata=None):
+        """Send a message to the remote peer."""
+        if isinstance(msg, potr.proto.OTRMessage):
+            msg = PYVER.unicode(msg)
+        else:
+            msg = PYVER.to_unicode(msg)
+
+        debug(('inject', msg, 'len {}'.format(len(msg)), appdata))
+
+        privmsg(self.peer_server, self.peer_nick, msg)
+
+    def setState(self, newstate):
+        """Handle state transition."""
+        debug(('state', self.state, newstate))
+
+        if self.is_encrypted():
+            if newstate == potr.context.STATE_ENCRYPTED:
+                self.print_buffer(
+                    'Private conversation has been refreshed.', 'success')
+            elif newstate == potr.context.STATE_FINISHED:
+                self.print_buffer(
+                    '{peer} has ended the private conversation. You should do '
+                    'the same:\n/otr finish'.format(peer=self.peer_nick))
+        elif newstate == potr.context.STATE_ENCRYPTED:
+            # unencrypted => encrypted
+            trust = self.getCurrentTrust()
+
+            # Disable logging before any proof of OTR activity is generated.
+            # This is necessary when the session is started automatically, and
+            # not by /otr start.
+            if not self.getPolicy('log'):
+                self.previous_log_level = self.disable_logging()
+            else:
+                self.previous_log_level = self.get_log_level()
+                if self.is_logged():
+                    self.hint(
+                        'You have enabled the recording to disk of OTR '
+                        'conversations. By doing this you are potentially '
+                        'putting yourself and your correspondent in danger. '
+                        'Please consider disabling this policy with '
+                        '"/otr policy default log off". To disable logging '
+                        'for this OTR session, use "/otr log stop"')
+
+            if trust is None:
+                fpr = str(self.getCurrentKey())
+                self.print_buffer('New fingerprint: {}'.format(fpr), 'warning')
+                self.setCurrentTrust('')
+
+            if bool(trust):
+                self.print_buffer(
+                    'Authenticated secured OTR conversation started.',
+                    'success')
+            else:
+                self.print_buffer(
+                    'Unauthenticated secured OTR conversation started.',
+                    'warning')
+                self.hint(self.verify_instructions())
+
+        if self.state != potr.context.STATE_PLAINTEXT and \
+                newstate == potr.context.STATE_PLAINTEXT:
+            self.print_buffer('Private conversation ended.')
+
+            # If we altered the logging value, restore it.
+            if self.previous_log_level is not None:
+                self.restore_logging(self.previous_log_level)
+
+        super(IrcContext, self).setState(newstate)
+
+    def maxMessageSize(self, appdata=None):
+        """Return the max message size for this context."""
+        # remove 'PRIVMSG <nick> :' from max message size
+        result = self.user.maxMessageSize - 10 - len(self.peer_nick)
+        debug('max message size {}'.format(result))
+
+        return result
+
+    def buffer(self):
+        """Get the buffer for this context."""
+        return info_get(
+            'irc_buffer', '{server},{nick}'.format(
+                server=self.peer_server,
+                nick=self.peer_nick
+                ))
+
+    def print_buffer(self, msg, level='info'):
+        """Print a message to the buffer for this context.
+        level is used to colorize the message."""
+        buf = self.buffer()
+
+        # add [nick] prefix if we have only a server buffer for the query
+        if self.peer_nick and not buffer_is_private(buf):
+            msg = '[{nick}] {msg}'.format(
+                    nick=self.peer_nick,
+                    msg=msg)
+
+        print_buffer(buf, msg, level)
+
+    def hint(self, msg):
+        """Print a message to the buffer but only when hints are enabled."""
+        hints_option = weechat.config_get(config_prefix('general.hints'))
+
+        if weechat.config_boolean(hints_option):
+            self.print_buffer(msg, 'hint')
+
+    def smp_finish(self, message=False, level='info'):
+        """Reset SMP state and send a message to the user."""
+        self.in_smp = False
+        self.smp_question = False
+
+        self.user.saveTrusts()
+        if message:
+            self.print_buffer(message, level)
+
+    def handle_tlvs(self, tlvs):
+        """Handle SMP states."""
+        if tlvs:
+            smp1q = first_instance(tlvs, potr.proto.SMP1QTLV)
+            smp3 = first_instance(tlvs, potr.proto.SMP3TLV)
+            smp4 = first_instance(tlvs, potr.proto.SMP4TLV)
+
+            if first_instance(tlvs, potr.proto.SMPABORTTLV):
+                debug('SMP aborted by peer')
+                self.smp_finish('SMP aborted by peer.', 'warning')
+            elif self.in_smp and not self.smpIsValid():
+                debug('SMP aborted')
+                self.smp_finish('SMP aborted.', 'error')
+            elif first_instance(tlvs, potr.proto.SMP1TLV):
+                debug('SMP1')
+                self.in_smp = True
+
+                self.print_buffer(
+                    """Peer has requested SMP verification.
+Respond with: /otr smp respond <secret>""")
+            elif smp1q:
+                debug(('SMP1Q', smp1q.msg))
+                self.in_smp = True
+                self.smp_question = True
+
+                self.print_buffer(
+                    """Peer has requested SMP verification: {msg}
+Respond with: /otr smp respond <answer>""".format(
+                        msg=PYVER.to_unicode(smp1q.msg)))
+            elif first_instance(tlvs, potr.proto.SMP2TLV):
+                if not self.in_smp:
+                    debug('Received unexpected SMP2')
+                    self.smp_finish()
+                else:
+                    debug('SMP2')
+                    self.print_buffer('SMP progressing.')
+            elif smp3 or smp4:
+                if smp3:
+                    debug('SMP3')
+                elif smp4:
+                    debug('SMP4')
+
+                if self.smpIsSuccess():
+
+                    if self.smp_question:
+                        self.smp_finish('SMP verification succeeded.',
+                            'success')
+                        if not self.is_verified:
+                            self.print_buffer(
+                            """You may want to authenticate your peer by asking your own question:
+/otr smp ask <'question'> 'secret'""")
+
+                    else:
+                        self.smp_finish('SMP verification succeeded.',
+                            'success')
+
+                else:
+                    self.smp_finish('SMP verification failed.', 'error')
+
+    def verify_instructions(self):
+        """Generate verification instructions for user."""
+        return """You can verify that this contact is who they claim to be in one of the following ways:
+
+1) Verify each other's fingerprints using a secure channel:
+  Your fingerprint : {your_fp}
+  {peer}'s fingerprint : {peer_fp}
+  then use the command: /otr trust {peer_nick} {peer_server}
+
+2) SMP pre-shared secret that you both know:
+  /otr smp ask {peer_nick} {peer_server} 'secret'
+
+3) SMP pre-shared secret that you both know with a question:
+  /otr smp ask {peer_nick} {peer_server} <'question'> 'secret'
+
+Note: You can safely omit specifying the peer and server when
+      executing these commands from the appropriate conversation
+      buffer
+""".format(
+        your_fp=self.user.getPrivkey(),
+        peer=self.peer,
+        peer_nick=self.peer_nick,
+        peer_server=self.peer_server,
+        peer_fp=potr.human_hash(
+            self.crypto.theirPubkey.cfingerprint()),
+        )
+
+    def is_encrypted(self):
+        """Return True if the conversation with this context's peer is
+        currently encrypted."""
+        return self.state == potr.context.STATE_ENCRYPTED
+
+    def is_verified(self):
+        """Return True if this context's peer is verified."""
+        return bool(self.getCurrentTrust())
+
+    def format_policies(self):
+        """Return current policies for this context formatted as a string for
+        the user."""
+        buf = io.StringIO()
+
+        buf.write('Current OTR policies for {peer}:\n'.format(
+            peer=self.peer))
+
+        for policy, desc in sorted(POLICIES.items()):
+            buf.write('  {policy} ({desc}) : {value}\n'.format(
+                    policy=policy,
+                    desc=desc,
+                    value='on' if self.getPolicy(policy) else 'off'))
+
+        buf.write('Change policies with: /otr policy NAME on|off')
+
+        return buf.getvalue()
+
+    def is_logged(self):
+        """Return True if conversations with this context's peer are currently
+        being logged to disk."""
+        infolist = weechat.infolist_get('logger_buffer', '', '')
+
+        buf = self.buffer()
+
+        result = False
+
+        while weechat.infolist_next(infolist):
+            if weechat.infolist_pointer(infolist, 'buffer') == buf:
+                result = bool(weechat.infolist_integer(infolist, 'log_enabled'))
+                break
+
+        weechat.infolist_free(infolist)
+
+        return result
+
+    def get_log_level(self):
+        """Return the current logging level for this context's peer
+        or -1 if the buffer uses the default log level of weechat."""
+        infolist = weechat.infolist_get('logger_buffer', '', '')
+
+        buf = self.buffer()
+
+        if not weechat.config_get(self.get_logger_option_name(buf)):
+            result = -1
+        else:
+            result = 0
+
+            while weechat.infolist_next(infolist):
+                if weechat.infolist_pointer(infolist, 'buffer') == buf:
+                    result = weechat.infolist_integer(infolist, 'log_level')
+                    break
+
+            weechat.infolist_free(infolist)
+
+        return result
+
+    def get_logger_option_name(self, buf):
+        """Returns the logger config option for the specified buffer."""
+        name = buffer_get_string(buf, 'name')
+        plugin = buffer_get_string(buf, 'plugin')
+
+        return 'logger.level.{plugin}.{name}'.format(
+            plugin=plugin, name=name)
+
+    def disable_logging(self):
+        """Return the previous logger level and set the buffer logger level
+        to 0. If it was already 0, return None."""
+        # If previous_log_level has not been previously set, return the level
+        # we detect now.
+        if not hasattr(self, 'previous_log_level'):
+            previous_log_level = self.get_log_level()
+
+            if self.is_logged():
+                weechat.command(self.buffer(), '/mute logger disable')
+                self.print_buffer(
+                    'Logs have been temporarily disabled for the session. They will be restored upon finishing the OTR session.')
+
+            return previous_log_level
+
+        # If previous_log_level was already set, it means we already altered it
+        # and that we just detected an already modified logging level.
+        # Return the pre-existing value so it doesn't get lost, and we can
+        # restore it later.
+        else:
+            return self.previous_log_level
+
+    def restore_logging(self, previous_log_level):
+        """Restore the log level of the buffer."""
+        buf = self.buffer()
+
+        if (previous_log_level >= 0) and (previous_log_level < 10):
+            self.print_buffer(
+                'Restoring buffer logging value to: {}'.format(
+                    previous_log_level), 'warning')
+            weechat.command(buf, '/mute logger set {}'.format(
+                previous_log_level))
+
+        if previous_log_level == -1:
+            logger_option_name = self.get_logger_option_name(buf)
+            self.print_buffer(
+                'Restoring buffer logging value to default', 'warning')
+            weechat.command(buf, '/mute unset {}'.format(
+                logger_option_name))
+
+        del self.previous_log_level
+
+    def msg_convert_in(self, msg):
+        """Transform incoming OTR message to IRC format.
+        This includes stripping html, converting plain-text ACTIONs
+        and character encoding conversion.
+        Only character encoding is changed if context is unencrypted."""
+        msg = PYVER.to_unicode(msg)
+
+        if not self.is_encrypted():
+            return msg
+
+        if self.getPolicy('html_filter'):
+            try:
+                msg = IrcHTMLParser.parse(msg)
+            except PYVER.html_parser.HTMLParseError:
+                pass
+
+        return msg_irc_from_plain(msg)
+
+    def msg_convert_out(self, msg):
+        """Convert an outgoing IRC message to be sent over OTR.
+        This includes escaping html, converting ACTIONs to plain-text
+        and character encoding conversion
+        Only character encoding is changed if context is unencrypted."""
+        if self.is_encrypted():
+            msg = msg_plain_from_irc(msg)
+
+            if self.getPolicy('html_escape'):
+                msg = PYVER.html_escape(msg)
+
+        # potr expects bytes to be returned
+        return to_bytes(msg)
+
+    def no_send_tag(self):
+        """Skip OTR whitespace tagging to bots and services.
+
+        Any nicks matching the otr.general.no_send_tag_regex config setting
+        will not be tagged.
+        """
+        no_send_tag_regex = config_string('general.no_send_tag_regex')
+        debug(('no_send_tag', no_send_tag_regex, self.peer_nick))
+        if no_send_tag_regex:
+            return re.match(no_send_tag_regex, self.peer_nick, re.IGNORECASE)
+ 
+    def __repr__(self):
+        return PYVER.to_str(('<{} {:x} peer_nick={c.peer_nick} '
+            'peer_server={c.peer_server}>').format(
+            self.__class__.__name__, id(self), c=self))
+
+class IrcOtrAccount(potr.context.Account):
+    """Account class for OTR over IRC."""
+
+    contextclass = IrcContext
+
+    PROTOCOL = 'irc'
+    MAX_MSG_SIZE = 415
+
+    def __init__(self, name):
+        super(IrcOtrAccount, self).__init__(
+            name, IrcOtrAccount.PROTOCOL, IrcOtrAccount.MAX_MSG_SIZE)
+
+        self.nick, self.server = self.name.split('@', 1)
+
+        # IRC messages cannot have newlines, OTR query and "no plugin" text
+        # need to be one message
+        self.defaultQuery = self.defaultQuery.replace("\n", ' ')
+
+        self.key_file_path = private_key_file_path(name)
+        self.fpr_file_path = os.path.join(OTR_DIR, '{}.fpr'.format(name))
+
+        self.load_trusts()
+
+    def load_trusts(self):
+        """Load trust data from the fingerprint file."""
+        if os.path.exists(self.fpr_file_path):
+            with open(self.fpr_file_path) as fpr_file:
+                for line in fpr_file:
+                    debug(('load trust check', line))
+
+                    context, account, protocol, fpr, trust = \
+                        PYVER.to_unicode(line[:-1]).split('\t')
+
+                    if account == self.name and \
+                            protocol == IrcOtrAccount.PROTOCOL:
+                        debug(('set trust', context, fpr, trust))
+                        self.setTrust(context, fpr, trust)
+
+    def loadPrivkey(self):
+        """Load key file.
+
+        If no key file exists, load the default key. If there is no default
+        key, a new key will be generated automatically by potr."""
+        debug(('load private key', self.key_file_path))
+
+        if os.path.exists(self.key_file_path):
+            return read_private_key(self.key_file_path)
+        else:
+            default_key = config_string('general.defaultkey')
+            if default_key:
+                default_key_path = private_key_file_path(default_key)
+
+                if os.path.exists(default_key_path):
+                    shutil.copyfile(default_key_path, self.key_file_path)
+                    return read_private_key(self.key_file_path)
+
+    def savePrivkey(self):
+        """Save key file."""
+        debug(('save private key', self.key_file_path))
+
+        with open(self.key_file_path, 'wb') as key_file:
+            key_file.write(self.getPrivkey().serializePrivateKey())
+
+    def saveTrusts(self):
+        """Save trusts."""
+        with open(self.fpr_file_path, 'w') as fpr_file:
+            for uid, trusts in self.trusts.items():
+                for fpr, trust in trusts.items():
+                    debug(('trust write', uid, self.name,
+                           IrcOtrAccount.PROTOCOL, fpr, trust))
+                    fpr_file.write(PYVER.to_str('\t'.join(
+                            (uid, self.name, IrcOtrAccount.PROTOCOL, fpr,
+                             trust))))
+                    fpr_file.write('\n')
+
+    def end_all_private(self):
+        """End all currently encrypted conversations."""
+        for context in self.ctxs.values():
+            if context.is_encrypted():
+                context.disconnect()
+
+    def policy_config_option(self, policy):
+        """Get the option name of a policy option for this account."""
+        return config_prefix('.'.join([
+                    'policy', self.server, self.nick, policy.lower()]))
+
+class IrcHTMLParser(PYVER.html_parser.HTMLParser):
+    """A simple HTML parser that throws away anything but newlines and links"""
+
+    @staticmethod
+    def parse(data):
+        """Create a temporary IrcHTMLParser and parse a single string"""
+        parser = IrcHTMLParser(**PYVER.html_parser_init_kwargs)
+        parser.feed(data)
+        parser.close()
+        return parser.result
+
+    def reset(self):
+        """Forget all state, called from __init__"""
+        PYVER.html_parser.HTMLParser.reset(self)
+        self.result = ''
+        self.linktarget = ''
+        self.linkstart = 0
+
+    def handle_starttag(self, tag, attrs):
+        """Called when a start tag is encountered"""
+        if tag == 'br':
+            self.result += '\n'
+        elif tag == 'a':
+            attrs = dict(attrs)
+            if 'href' in attrs:
+                self.result += '['
+                self.linktarget = attrs['href']
+                self.linkstart = len(self.result)
+
+    def handle_endtag(self, tag):
+        """Called when an end tag is encountered"""
+        if tag == 'a':
+            if self.linktarget:
+                if self.result[self.linkstart:] == self.linktarget:
+                    self.result += ']'
+                else:
+                    self.result += ']({})'.format(self.linktarget)
+                self.linktarget = ''
+
+    def handle_data(self, data):
+        """Called for character data (i.e. text)"""
+        self.result += data
+
+    def handle_entityref(self, name):
+        """Called for entity references, such as &amp;"""
+        try:
+            self.result += PYVER.unichr(
+                PYVER.html_entities.name2codepoint[name])
+        except KeyError:
+            self.result += '&{};'.format(name)
+
+    def handle_charref(self, name):
+        """Called for character references, such as &#39;"""
+        try:
+            if name.startswith('x'):
+                self.result += PYVER.unichr(int(name[1:], 16))
+            else:
+                self.result += PYVER.unichr(int(name))
+        except ValueError:
+            self.result += '&#{};'.format(name)
+
+class TableFormatter(object):
+    """Format lists of string into aligned tables."""
+
+    def __init__(self):
+        self.rows = []
+        self.max_widths = None
+
+    def add_row(self, row):
+        """Add a row to the table."""
+        self.rows.append(row)
+        row_widths = [ len(s) for s in row ]
+        if self.max_widths is None:
+            self.max_widths = row_widths
+        else:
+            self.max_widths = list(map(max, self.max_widths, row_widths))
+
+    def format(self):
+        """Return the formatted table as a string."""
+        return '\n'.join([ self.format_row(row) for row in self.rows ])
+
+    def format_row(self, row):
+        """Format a single row as a string."""
+        return ' |'.join(
+            [ s.ljust(self.max_widths[i]) for i, s in enumerate(row) ])
+
+def message_in_cb(data, modifier, modifier_data, string):
+    """Incoming message callback"""
+    debug(('message_in_cb', data, modifier, modifier_data, string))
+
+    parsed = parse_irc_privmsg(
+        PYVER.to_unicode(string), PYVER.to_unicode(modifier_data))
+    debug(('parsed message', parsed))
+
+    # skip processing messages to public channels
+    if parsed['to_channel']:
+        return string
+
+    server = PYVER.to_unicode(modifier_data)
+
+    from_user = irc_user(parsed['from_nick'], server)
+    local_user = current_user(server)
+
+    context = ACCOUNTS[local_user].getContext(from_user)
+
+    context.in_assembler.add(parsed['text'])
+
+    result = ''
+
+    if context.in_assembler.is_done():
+        try:
+            msg, tlvs = context.receiveMessage(
+                # potr expects bytes
+                to_bytes(context.in_assembler.get()))
+
+            debug(('receive', msg, tlvs))
+
+            if msg:
+                result = PYVER.to_str(build_privmsgs_in(
+                    parsed['from'], parsed['to'],
+                    context.msg_convert_in(msg)))
+
+            context.handle_tlvs(tlvs)
+        except potr.context.ErrorReceived as err:
+            context.print_buffer('Received OTR error: {}'.format(
+                PYVER.to_unicode(err.args[0].error)), 'error')
+        except potr.context.NotEncryptedError:
+            context.print_buffer(
+                'Received encrypted data but no private session established.',
+                'warning')
+        except potr.context.NotOTRMessage:
+            result = string
+        except potr.context.UnencryptedMessage as err:
+            result = PYVER.to_str(build_privmsgs_in(
+                parsed['from'], parsed['to'], PYVER.to_unicode(
+                    msg_plain_from_irc(err.args[0])),
+                'Unencrypted message received: '))
+
+    weechat.bar_item_update(SCRIPT_NAME)
+
+    return result
+
+def message_out_cb(data, modifier, modifier_data, string):
+    """Outgoing message callback."""
+    result = ''
+
+    # If any exception is raised in this function, WeeChat will not send the
+    # outgoing message, which could be something that the user intended to be
+    # encrypted. This paranoid exception handling ensures that the system
+    # fails closed and not open.
+    try:
+        debug(('message_out_cb', data, modifier, modifier_data, string))
+
+        parsed = parse_irc_privmsg(
+            PYVER.to_unicode(string), PYVER.to_unicode(modifier_data))
+        debug(('parsed message', parsed))
+
+        # skip processing messages to public channels
+        if parsed['to_channel']:
+            return string
+
+        server = PYVER.to_unicode(modifier_data)
+
+        to_user = irc_user(parsed['to_nick'], server)
+        local_user = current_user(server)
+
+        context = ACCOUNTS[local_user].getContext(to_user)
+        is_query = OTR_QUERY_RE.search(parsed['text'])
+
+        parsed_text_bytes = to_bytes(parsed['text'])
+
+        is_otr_message = \
+            parsed_text_bytes[:len(potr.proto.OTRTAG)] == potr.proto.OTRTAG
+
+        if is_otr_message and not is_query:
+            if not has_otr_end(parsed['text']):
+                debug('in OTR message')
+                context.in_otr_message = True
+            else:
+                debug('complete OTR message')
+            result = string
+        elif context.in_otr_message:
+            if has_otr_end(parsed['text']):
+                context.in_otr_message = False
+                debug('in OTR message end')
+            result = string
+        else:
+            debug(('context send message', parsed['text'], parsed['to_nick'],
+                   server))
+
+            if context.policyOtrEnabled() and \
+                not context.is_encrypted() and \
+                not is_query and \
+                context.getPolicy('require_encryption'):
+                context.print_buffer(
+                   'Your message will not be sent, because policy requires an '
+                   'encrypted connection.', 'error')
+                context.hint(
+                   'Wait for the OTR connection or change the policy to allow '
+                   'clear-text messages:\n'
+                   '/policy set require_encryption off')
+
+            try:
+                ret = context.sendMessage(
+                    potr.context.FRAGMENT_SEND_ALL,
+                    context.msg_convert_out(parsed['text']))
+
+                if ret:
+                    debug(('sendMessage returned', ret))
+                    result = PYVER.to_str(
+                        build_privmsg_out(
+                            parsed['to_nick'], PYVER.to_unicode(ret)
+                            ))
+
+            except potr.context.NotEncryptedError as err:
+                if err.args[0] == potr.context.EXC_FINISHED:
+                    context.print_buffer(
+                        """Your message was not sent. End your private conversation:\n/otr finish""",
+                        'error')
+                else:
+                    raise
+
+        weechat.bar_item_update(SCRIPT_NAME)
+    except:
+        try:
+            print_buffer('', traceback.format_exc(), 'error')
+            print_buffer('', 'Versions: {versions}'.format(
+                versions=dependency_versions()), 'error')
+            context.print_buffer(
+                'Failed to send message. See core buffer for traceback.',
+                'error')
+        except:
+            pass
+
+    return result
+
+def shutdown():
+    """Script unload callback."""
+    debug('shutdown')
+
+    weechat.config_write(CONFIG_FILE)
+
+    for account in ACCOUNTS.values():
+        account.end_all_private()
+
+    free_all_config()
+
+    weechat.bar_item_remove(OTR_STATUSBAR)
+
+    return weechat.WEECHAT_RC_OK
+
+def command_cb(data, buf, args):
+    """Parse and dispatch WeeChat OTR commands."""
+    result = weechat.WEECHAT_RC_ERROR
+
+    try:
+        arg_parts = [PYVER.to_unicode(arg) for arg in shlex.split(args)]
+    except:
+        debug("Command parsing error.")
+        return result
+
+    if len(arg_parts) in (1, 3) and arg_parts[0] in ('start', 'refresh'):
+        nick, server = default_peer_args(arg_parts[1:3], buf)
+
+        if nick is not None and server is not None:
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+            # We need to wall disable_logging() here so that no OTR-related
+            # buffer messages get logged at any point. disable_logging() will
+            # be called again when effectively switching to encrypted, but
+            # the previous_log_level we set here will be preserved for later
+            # restoring.
+            if not context.getPolicy('log'):
+                context.previous_log_level = context.disable_logging()
+            else:
+                context.previous_log_level = context.get_log_level()
+
+            context.hint('Sending OTR query... Please await confirmation of the OTR session being started before sending a message.')
+            if not context.getPolicy('send_tag'):
+                context.hint(
+                    'To try OTR on all conversations with {peer}: /otr policy send_tag on'.format(
+                    peer=context.peer))
+
+            privmsg(server, nick, '?OTR?')
+
+            result = weechat.WEECHAT_RC_OK
+    elif len(arg_parts) in (1, 3) and arg_parts[0] in ('finish', 'end'):
+        nick, server = default_peer_args(arg_parts[1:3], buf)
+
+        if nick is not None and server is not None:
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+            context.disconnect()
+
+            result = weechat.WEECHAT_RC_OK
+
+    elif len(arg_parts) in (1, 3) and arg_parts[0] == 'status':
+        nick, server = default_peer_args(arg_parts[1:3], buf)
+
+        if nick is not None and server is not None:
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+            if context.is_encrypted():
+                context.print_buffer(
+                    'This conversation is encrypted.', 'success')
+                context.print_buffer("Your fingerprint is: {}".format(
+                    context.user.getPrivkey()))
+                context.print_buffer("Your peer's fingerprint is: {}".format(
+                    potr.human_hash(context.crypto.theirPubkey.cfingerprint())))
+                if context.is_verified():
+                    context.print_buffer(
+                        "The peer's identity has been verified.",
+                        'success')
+                else:
+                    context.print_buffer(
+                        "You have not verified the peer's identity yet.",
+                        'warning')
+            else:
+                context.print_buffer(
+                    "This current conversation is not encrypted.",
+                    'warning')
+
+            result = weechat.WEECHAT_RC_OK
+
+    elif len(arg_parts) in range(2, 7) and arg_parts[0] == 'smp':
+        action = arg_parts[1]
+
+        if action == 'respond':
+            # Check if nickname and server are specified
+            if len(arg_parts) == 3:
+                nick, server = default_peer_args([], buf)
+                secret = arg_parts[2]
+            elif len(arg_parts) == 5:
+                nick, server = default_peer_args(arg_parts[2:4], buf)
+                secret = arg_parts[4]
+            else:
+                return weechat.WEECHAT_RC_ERROR
+
+            if secret:
+                secret = PYVER.to_str(secret)
+
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+            context.smpGotSecret(secret)
+
+            result = weechat.WEECHAT_RC_OK
+
+        elif action == 'ask':
+            question = None
+            secret = None
+
+            # Nickname and server are not specified
+            # Check whether it's a simple challenge or a question/answer request
+            if len(arg_parts) == 3:
+                nick, server = default_peer_args([], buf)
+                secret = arg_parts[2]
+            elif len(arg_parts) == 4:
+                nick, server = default_peer_args([], buf)
+                secret = arg_parts[3]
+                question = arg_parts[2]
+
+            # Nickname and server are specified
+            # Check whether it's a simple challenge or a question/answer request
+            elif len(arg_parts) == 5:
+                nick, server = default_peer_args(arg_parts[2:4], buf)
+                secret = arg_parts[4]
+            elif len(arg_parts) == 6:
+                nick, server = default_peer_args(arg_parts[2:4], buf)
+                secret = arg_parts[5]
+                question = arg_parts[4]
+            else:
+                return weechat.WEECHAT_RC_ERROR
+
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+
+            if secret:
+                secret = PYVER.to_str(secret)
+            if question:
+                question = PYVER.to_str(question)
+
+            try:
+                context.smpInit(secret, question)
+            except potr.context.NotEncryptedError:
+                context.print_buffer(
+                    'There is currently no encrypted session with {}.'.format(
+                        context.peer), 'error')
+            else:
+                if question:
+                    context.print_buffer('SMP challenge sent...')
+                else:
+                    context.print_buffer('SMP question sent...')
+                context.in_smp = True
+                result = weechat.WEECHAT_RC_OK
+
+        elif action == 'abort':
+            # Nickname and server are not specified
+            if len(arg_parts) == 2:
+                nick, server = default_peer_args([], buf)
+            # Nickname and server are specified
+            elif len(arg_parts) == 4:
+                nick, server = default_peer_args(arg_parts[2:4], buf)
+            else:
+                return weechat.WEECHAT_RC_ERROR
+
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+
+            if context.in_smp:
+                try:
+                    context.smpAbort()
+                except potr.context.NotEncryptedError:
+                    context.print_buffer(
+                        'There is currently no encrypted session with {}.'.format(
+                         context.peer), 'error')
+                else:
+                    debug('SMP aborted')
+                    context.smp_finish('SMP aborted.')
+                    result = weechat.WEECHAT_RC_OK
+
+    elif len(arg_parts) in (1, 3) and arg_parts[0] == 'trust':
+        nick, server = default_peer_args(arg_parts[1:3], buf)
+
+        if nick is not None and server is not None:
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+
+            if context.crypto.theirPubkey is not None:
+                context.setCurrentTrust('verified')
+                context.print_buffer('{peer} is now authenticated.'.format(
+                    peer=context.peer))
+
+                weechat.bar_item_update(SCRIPT_NAME)
+            else:
+                context.print_buffer(
+                    'No fingerprint for {peer}. Start an OTR conversation first: /otr start'.format(
+                        peer=context.peer), 'error')
+
+            result = weechat.WEECHAT_RC_OK
+    elif len(arg_parts) in (1, 3) and arg_parts[0] == 'distrust':
+        nick, server = default_peer_args(arg_parts[1:3], buf)
+
+        if nick is not None and server is not None:
+            context = ACCOUNTS[current_user(server)].getContext(
+                irc_user(nick, server))
+
+            if context.crypto.theirPubkey is not None:
+                context.setCurrentTrust('')
+                context.print_buffer(
+                    '{peer} is now de-authenticated.'.format(
+                        peer=context.peer))
+
+                weechat.bar_item_update(SCRIPT_NAME)
+            else:
+                context.print_buffer(
+                    'No fingerprint for {peer}. Start an OTR conversation first: /otr start'.format(
+                        peer=context.peer), 'error')
+
+            result = weechat.WEECHAT_RC_OK
+
+    elif len(arg_parts) in (1, 2) and arg_parts[0] == 'log':
+        nick, server = default_peer_args([], buf)
+        if len(arg_parts) == 1:
+            if nick is not None and server is not None:
+                context = ACCOUNTS[current_user(server)].getContext(
+                    irc_user(nick, server))
+
+                if context.is_encrypted():
+                    if context.is_logged():
+                        context.print_buffer(
+                            'This conversation is currently being logged.',
+                            'warning')
+                        result = weechat.WEECHAT_RC_OK
+
+                    else:
+                        context.print_buffer(
+                            'This conversation is currently NOT being logged.')
+                        result = weechat.WEECHAT_RC_OK
+                else:
+                    context.print_buffer(
+                        'OTR LOG: Not in an OTR session', 'error')
+                    result = weechat.WEECHAT_RC_OK
+
+            else:
+                print_buffer('', 'OTR LOG: Not in an OTR session', 'error')
+                result = weechat.WEECHAT_RC_OK
+
+        if len(arg_parts) == 2:
+            if nick is not None and server is not None:
+                context = ACCOUNTS[current_user(server)].getContext(
+                    irc_user(nick, server))
+
+                if arg_parts[1] == 'start' and \
+                        not context.is_logged() and \
+                        context.is_encrypted():
+                    if context.previous_log_level is None:
+                        context.previous_log_level = context.get_log_level()
+                    context.print_buffer('From this point on, this conversation will be logged. Please keep in mind that by doing so you are potentially putting yourself and your interlocutor at risk. You can disable this by doing /otr log stop', 'warning')
+                    weechat.command(buf, '/mute logger set 9')
+                    result = weechat.WEECHAT_RC_OK
+
+                elif arg_parts[1] == 'stop' and \
+                        context.is_logged() and \
+                        context.is_encrypted():
+                    if context.previous_log_level is None:
+                        context.previous_log_level = context.get_log_level()
+                    weechat.command(buf, '/mute logger set 0')
+                    context.print_buffer('From this point on, this conversation will NOT be logged ANYMORE.')
+                    result = weechat.WEECHAT_RC_OK
+
+                elif not context.is_encrypted():
+                    context.print_buffer(
+                        'OTR LOG: Not in an OTR session', 'error')
+                    result = weechat.WEECHAT_RC_OK
+
+                else:
+                    # Don't need to do anything.
+                    result = weechat.WEECHAT_RC_OK
+
+            else:
+                print_buffer('', 'OTR LOG: Not in an OTR session', 'error')
+
+    elif len(arg_parts) in (1, 2, 3, 4) and arg_parts[0] == 'policy':
+        if len(arg_parts) == 1:
+            nick, server = default_peer_args([], buf)
+
+            if nick is not None and server is not None:
+                context = ACCOUNTS[current_user(server)].getContext(
+                    irc_user(nick, server))
+
+                context.print_buffer(context.format_policies())
+            else:
+                prnt('', format_default_policies())
+
+            result = weechat.WEECHAT_RC_OK
+
+        elif len(arg_parts) == 2 and arg_parts[1].lower() == 'default':
+            nick, server = default_peer_args([], buf)
+
+            if nick is not None and server is not None:
+                context = ACCOUNTS[current_user(server)].getContext(
+                    irc_user(nick, server))
+
+                context.print_buffer(format_default_policies())
+            else:
+                prnt('', format_default_policies())
+
+            result = weechat.WEECHAT_RC_OK
+
+        elif len(arg_parts) == 3 and arg_parts[1].lower() in POLICIES:
+            nick, server = default_peer_args([], buf)
+
+            if nick is not None and server is not None:
+                context = ACCOUNTS[current_user(server)].getContext(
+                    irc_user(nick, server))
+
+                policy_var = context.policy_config_option(arg_parts[1].lower())
+
+                command('', '/set {policy} {value}'.format(
+                    policy=policy_var,
+                    value=arg_parts[2]))
+
+                context.print_buffer(context.format_policies())
+
+                result = weechat.WEECHAT_RC_OK
+
+        elif len(arg_parts) == 4 and \
+            arg_parts[1].lower() == 'default' and \
+            arg_parts[2].lower() in POLICIES:
+            nick, server = default_peer_args([], buf)
+
+            policy_var = "otr.policy.default." + arg_parts[2].lower()
+
+            command('', '/set {policy} {value}'.format(
+                policy=policy_var,
+                value=arg_parts[3]))
+
+            if nick is not None and server is not None:
+                context = ACCOUNTS[current_user(server)].getContext(
+                    irc_user(nick, server))
+
+                context.print_buffer(format_default_policies())
+            else:
+                prnt('', format_default_policies())
+
+            result = weechat.WEECHAT_RC_OK
+    elif len(arg_parts) in (1, 2) and arg_parts[0] == 'fingerprint':
+        if len(arg_parts) == 1:
+            show_account_fingerprints()
+            result = weechat.WEECHAT_RC_OK
+        elif len(arg_parts) == 2:
+            if arg_parts[1] == 'all':
+                show_peer_fingerprints()
+            else:
+                show_peer_fingerprints(grep=arg_parts[1])
+            result = weechat.WEECHAT_RC_OK
+
+    return result
+
+def otr_statusbar_cb(data, item, window):
+    """Update the statusbar."""
+    if window:
+        buf = weechat.window_get_pointer(window, 'buffer')
+    else:
+        # If the bar item is in a root bar that is not in a window, window
+        # will be empty.
+        buf = weechat.current_buffer()
+
+    result = ''
+
+    if buffer_is_private(buf):
+        local_user = irc_user(
+            buffer_get_string(buf, 'localvar_nick'),
+            buffer_get_string(buf, 'localvar_server'))
+
+        remote_user = irc_user(
+            buffer_get_string(buf, 'localvar_channel'),
+            buffer_get_string(buf, 'localvar_server'))
+
+        context = ACCOUNTS[local_user].getContext(remote_user)
+
+        encrypted_str = config_string('look.bar.state.encrypted')
+        unencrypted_str = config_string('look.bar.state.unencrypted')
+        authenticated_str = config_string('look.bar.state.authenticated')
+        unauthenticated_str = config_string('look.bar.state.unauthenticated')
+        logged_str = config_string('look.bar.state.logged')
+        notlogged_str = config_string('look.bar.state.notlogged')
+
+        bar_parts = []
+
+        if context.is_encrypted():
+            if encrypted_str:
+                bar_parts.append(''.join([
+                            config_color('status.encrypted'),
+                            encrypted_str,
+                            config_color('status.default')]))
+
+            if context.is_verified():
+                if authenticated_str:
+                    bar_parts.append(''.join([
+                                config_color('status.authenticated'),
+                                authenticated_str,
+                                config_color('status.default')]))
+            elif unauthenticated_str:
+                bar_parts.append(''.join([
+                            config_color('status.unauthenticated'),
+                            unauthenticated_str,
+                            config_color('status.default')]))
+
+            if context.is_logged():
+                if logged_str:
+                    bar_parts.append(''.join([
+                                config_color('status.logged'),
+                                logged_str,
+                                config_color('status.default')]))
+            elif notlogged_str:
+                bar_parts.append(''.join([
+                            config_color('status.notlogged'),
+                            notlogged_str,
+                            config_color('status.default')]))
+
+        elif unencrypted_str:
+            bar_parts.append(''.join([
+                        config_color('status.unencrypted'),
+                        unencrypted_str,
+                        config_color('status.default')]))
+
+        result = config_string('look.bar.state.separator').join(bar_parts)
+
+        if result:
+            result = '{color}{prefix}{result}'.format(
+                color=config_color('status.default'),
+                prefix=config_string('look.bar.prefix'),
+                result=result)
+
+    return result
+
+def bar_config_update_cb(data, option):
+    """Callback for updating the status bar when its config changes."""
+    weechat.bar_item_update(SCRIPT_NAME)
+
+    return weechat.WEECHAT_RC_OK
+
+def policy_completion_cb(data, completion_item, buf, completion):
+    """Callback for policy tab completion."""
+    for policy in POLICIES:
+        weechat.hook_completion_list_add(
+            completion, policy, 0, weechat.WEECHAT_LIST_POS_SORT)
+
+    return weechat.WEECHAT_RC_OK
+
+def policy_create_option_cb(data, config_file, section, name, value):
+    """Callback for creating a new policy option when the user sets one
+    that doesn't exist."""
+    weechat.config_new_option(
+        config_file, section, name, 'boolean', '', '', 0, 0, value, value, 0,
+        '', '', '', '', '', '')
+
+    return weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED
+
+def logger_level_update_cb(data, option, value):
+    """Callback called when any logger level changes."""
+    weechat.bar_item_update(SCRIPT_NAME)
+
+    return weechat.WEECHAT_RC_OK
+
+def buffer_switch_cb(data, signal, signal_data):
+    """Callback for buffer switched.
+
+    Used for updating the status bar item when it is in a root bar.
+    """
+    weechat.bar_item_update(SCRIPT_NAME)
+
+    return weechat.WEECHAT_RC_OK
+
+def buffer_closing_cb(data, signal, signal_data):
+    """Callback for buffer closed.
+
+    It closes the OTR session when the buffer is about to be closed.
+    """
+    result = weechat.WEECHAT_RC_ERROR
+    nick, server = default_peer_args([], signal_data)
+
+    if nick is not None and server is not None:
+        context = ACCOUNTS[current_user(server)].getContext(
+            irc_user(nick, server))
+        context.disconnect()
+
+        result = weechat.WEECHAT_RC_OK
+    return result
+
+def init_config():
+    """Set up configuration options and load config file."""
+    global CONFIG_FILE
+    CONFIG_FILE = weechat.config_new(SCRIPT_NAME, 'config_reload_cb', '')
+
+    global CONFIG_SECTIONS
+    CONFIG_SECTIONS = {}
+
+    CONFIG_SECTIONS['general'] = weechat.config_new_section(
+        CONFIG_FILE, 'general', 0, 0, '', '', '', '', '', '', '', '', '', '')
+
+    for option, typ, desc, default in [
+        ('debug', 'boolean', 'OTR script debugging', 'off'),
+        ('hints', 'boolean', 'Give helpful hints how to use this script and how to stay secure while using OTR (recommended)', 'on'),
+        ('defaultkey', 'string',
+         'default private key to use for new accounts (nick@server)', ''),
+        ('no_send_tag_regex', 'string',
+         'do not OTR whitespace tag messages to nicks matching this regex '
+         '(case insensitive)',
+         '^(alis|chanfix|global|.+serv|\*.+)$'),
+        ]:
+        weechat.config_new_option(
+            CONFIG_FILE, CONFIG_SECTIONS['general'], option, typ, desc, '', 0,
+            0, default, default, 0, '', '', '', '', '', '')
+
+    CONFIG_SECTIONS['color'] = weechat.config_new_section(
+        CONFIG_FILE, 'color', 0, 0, '', '', '', '', '', '', '', '', '', '')
+
+    for option, desc, default, update_cb in [
+        ('status.default', 'status bar default color', 'default',
+         'bar_config_update_cb'),
+        ('status.encrypted', 'status bar encrypted indicator color', 'green',
+         'bar_config_update_cb'),
+        ('status.unencrypted', 'status bar unencrypted indicator color',
+         'lightred', 'bar_config_update_cb'),
+        ('status.authenticated', 'status bar authenticated indicator color',
+         'green', 'bar_config_update_cb'),
+        ('status.unauthenticated', 'status bar unauthenticated indicator color',
+         'lightred', 'bar_config_update_cb'),
+        ('status.logged', 'status bar logged indicator color', 'lightred',
+         'bar_config_update_cb'),
+        ('status.notlogged', 'status bar not logged indicator color',
+         'green', 'bar_config_update_cb'),
+        ('buffer.hint', 'text color for hints', 'lightblue', ''),
+        ('buffer.info', 'text color for informational messages', 'default', ''),
+        ('buffer.success', 'text color for success messages', 'lightgreen', ''),
+        ('buffer.warning', 'text color for warnings', 'yellow', ''),
+        ('buffer.error', 'text color for errors', 'lightred', ''),
+        ]:
+        weechat.config_new_option(
+            CONFIG_FILE, CONFIG_SECTIONS['color'], option, 'color', desc, '', 0,
+            0, default, default, 0, '', '', update_cb, '', '', '')
+
+    CONFIG_SECTIONS['look'] = weechat.config_new_section(
+        CONFIG_FILE, 'look', 0, 0, '', '', '', '', '', '', '', '', '', '')
+
+    for option, desc, default, update_cb in [
+        ('bar.prefix', 'prefix for OTR status bar item', 'OTR:',
+         'bar_config_update_cb'),
+        ('bar.state.encrypted',
+         'shown in status bar when conversation is encrypted', 'SEC',
+         'bar_config_update_cb'),
+        ('bar.state.unencrypted',
+         'shown in status bar when conversation is not encrypted', '!SEC',
+         'bar_config_update_cb'),
+        ('bar.state.authenticated',
+         'shown in status bar when peer is authenticated', 'AUTH',
+         'bar_config_update_cb'),
+        ('bar.state.unauthenticated',
+         'shown in status bar when peer is not authenticated', '!AUTH',
+         'bar_config_update_cb'),
+        ('bar.state.logged',
+         'shown in status bar when peer conversation is being logged to disk',
+         'LOG',
+         'bar_config_update_cb'),
+        ('bar.state.notlogged',
+         'shown in status bar when peer conversation is not being logged to disk',
+         '!LOG',
+         'bar_config_update_cb'),
+        ('bar.state.separator', 'separator for states in the status bar', ',',
+         'bar_config_update_cb'),
+        ('prefix', 'prefix used for messages from otr (note: content is evaluated, see /help eval)',
+         '${color:default}:! ${color:brown}otr${color:default} !:', ''),
+        ]:
+        weechat.config_new_option(
+            CONFIG_FILE, CONFIG_SECTIONS['look'], option, 'string', desc, '',
+            0, 0, default, default, 0, '', '', update_cb, '', '', '')
+
+    CONFIG_SECTIONS['policy'] = weechat.config_new_section(
+        CONFIG_FILE, 'policy', 1, 1, '', '', '', '', '', '',
+        'policy_create_option_cb', '', '', '')
+
+    for option, desc, default in [
+        ('default.allow_v2', 'default allow OTR v2 policy', 'on'),
+        ('default.require_encryption', 'default require encryption policy',
+         'off'),
+        ('default.log', 'default enable logging to disk', 'off'),
+        ('default.send_tag', 'default send tag policy', 'off'),
+        ('default.html_escape', 'default HTML escape policy', 'off'),
+        ('default.html_filter', 'default HTML filter policy', 'on'),
+        ]:
+        weechat.config_new_option(
+            CONFIG_FILE, CONFIG_SECTIONS['policy'], option, 'boolean', desc, '',
+            0, 0, default, default, 0, '', '', '', '', '', '')
+
+    weechat.config_read(CONFIG_FILE)
+
+def config_reload_cb(data, config_file):
+    """/reload callback to reload config from file."""
+    free_all_config()
+    init_config()
+
+    return weechat.WEECHAT_CONFIG_READ_OK
+
+def free_all_config():
+    """Free all config options, sections and config file."""
+    for section in CONFIG_SECTIONS.values():
+        weechat.config_section_free_options(section)
+        weechat.config_section_free(section)
+
+    weechat.config_free(CONFIG_FILE)
+
+def create_dir():
+    """Create the OTR subdirectory in the WeeChat config directory if it does
+    not exist."""
+    if not os.path.exists(OTR_DIR):
+        weechat.mkdir_home(OTR_DIR_NAME, 0o700)
+
+def git_info():
+    """If this script is part of a git repository return the repo state."""
+    result = None
+    script_dir = os.path.dirname(os.path.realpath(__file__))
+    git_dir = os.path.join(script_dir, '.git')
+    if os.path.isdir(git_dir):
+        import subprocess
+        try:
+            result = PYVER.to_unicode(subprocess.check_output([
+                'git',
+                '--git-dir', git_dir,
+                '--work-tree', script_dir,
+                'describe', '--dirty', '--always',
+                ])).lstrip('v').rstrip()
+        except (OSError, subprocess.CalledProcessError):
+            pass
+
+    return result
+
+def weechat_version_ok():
+    """Check if the WeeChat version is compatible with this script.
+
+    If WeeChat version < 0.4.2 log an error to the core buffer and return
+    False. Otherwise return True.
+    """
+    weechat_version = weechat.info_get('version_number', '') or 0
+    if int(weechat_version) < 0x00040200:
+        error_message = (
+            '{script_name} requires WeeChat version >= 0.4.2. The current '
+            'version is {current_version}.').format(
+            script_name=SCRIPT_NAME,
+            current_version=weechat.info_get('version', ''))
+        prnt('', error_message)
+        return False
+    else:
+        return True
+
+SCRIPT_VERSION = git_info() or SCRIPT_VERSION
+
+def dependency_versions():
+    """Return a string containing the versions of all dependencies."""
+    return ('weechat-otr {script_version}, '
+        'potr {potr_major}.{potr_minor}.{potr_patch}-{potr_sub}, '
+        'Python {python_version}, '
+        'WeeChat {weechat_version}'
+        ).format(
+        script_version=SCRIPT_VERSION,
+        potr_major=potr.VERSION[0],
+        potr_minor=potr.VERSION[1],
+        potr_patch=potr.VERSION[2],
+        potr_sub=potr.VERSION[3],
+        python_version=platform.python_version(),
+        weechat_version=weechat.info_get('version', ''))
+
+def excepthook(typ, value, traceback):
+    sys.stderr.write('Versions: ')
+    sys.stderr.write(dependency_versions())
+    sys.stderr.write('\n')
+
+    sys.__excepthook__(typ, value, traceback)
+
+sys.excepthook = excepthook
+
+if weechat.register(
+    SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENCE, SCRIPT_DESC,
+    'shutdown', ''):
+    if weechat_version_ok():
+        init_config()
+
+        OTR_DIR = os.path.join(info_get('weechat_dir', ''), OTR_DIR_NAME)
+        create_dir()
+
+        ACCOUNTS = AccountDict()
+
+        weechat.hook_modifier('irc_in_privmsg', 'message_in_cb', '')
+        weechat.hook_modifier('irc_out_privmsg', 'message_out_cb', '')
+
+        weechat.hook_command(
+            SCRIPT_NAME, SCRIPT_HELP,
+            'start [NICK SERVER] || '
+            'refresh [NICK SERVER] || '
+            'finish [NICK SERVER] || '
+            'end [NICK SERVER] || '
+            'status [NICK SERVER] || '
+            'smp ask [NICK SERVER] [QUESTION] SECRET || '
+            'smp respond [NICK SERVER] SECRET || '
+            'smp abort [NICK SERVER] || '
+            'trust [NICK SERVER] || '
+            'distrust [NICK SERVER] || '
+            'log [on|off] || '
+            'policy [POLICY on|off] || '
+            'fingerprint [SEARCH|all]',
+            '',
+            'start %(nick) %(irc_servers) %-||'
+            'refresh %(nick) %(irc_servers) %-||'
+            'finish %(nick) %(irc_servers) %-||'
+            'end %(nick) %(irc_servers) %-||'
+            'status %(nick) %(irc_servers) %-||'
+            'smp ask|respond %(nick) %(irc_servers) %-||'
+            'smp abort %(nick) %(irc_servers) %-||'
+            'trust %(nick) %(irc_servers) %-||'
+            'distrust %(nick) %(irc_servers) %-||'
+            'log on|off %-||'
+            'policy %(otr_policy) on|off %-||'
+            'fingerprint all %-||',
+            'command_cb',
+            '')
+
+        weechat.hook_completion(
+            'otr_policy', 'OTR policies', 'policy_completion_cb', '')
+
+        weechat.hook_config('logger.level.irc.*', 'logger_level_update_cb', '')
+
+        weechat.hook_signal('buffer_switch', 'buffer_switch_cb', '')
+        weechat.hook_signal('buffer_closing', 'buffer_closing_cb', '')
+
+        OTR_STATUSBAR = weechat.bar_item_new(
+            SCRIPT_NAME, 'otr_statusbar_cb', '')
+        weechat.bar_item_update(SCRIPT_NAME)
diff --git a/weechat/.weechat/python/screen_away.py b/weechat/.weechat/python/screen_away.py
new file mode 100644
index 0000000..8f68505
--- /dev/null
+++ b/weechat/.weechat/python/screen_away.py
@@ -0,0 +1,197 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2009 by xt <xt@bash.no>
+# Copyright (c) 2009 by penryu <penryu@gmail.com>
+# Copyright (c) 2010 by Blake Winton <bwinton@latte.ca>
+# Copyright (c) 2010 by Aron Griffis <agriffis@n01se.net>
+# Copyright (c) 2010 by Jani Kesänen <jani.kesanen@gmail.com>
+#
+# 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.0 or newer)
+#
+# History:
+# 2014-08-02, Nils Görs <weechatter@arcor.de>
+#  version 0.14: add time to detach message. (idea by Mikaela)
+# 2014-06-19, Anders Bergh <anders1@gmail.com>
+#  version 0.13: Fix a simple typo in an option description.
+# 2014-01-12, Phyks (Lucas Verney) <phyks@phyks.me>
+#  version 0.12: Added an option to check status of relays to set unaway in
+#                   case of a connected relay.
+# 2013-08-30, Anders Einar Hilden <hildenae@gmail.com>
+#  version: 0.11: Fix reading of set_away
+# 2013-06-16, Renato Botelho <rbgarga@gmail.com>
+#  version 0.10: add option to don't set away, only change nick
+#                   allow multiple commands on attach/dettach
+#                   do not add suffix if nick already have it
+# 2012-12-29, David Flatz <david@upcs.at>
+#  version 0.9: add option to ignore servers and don't set away status for them
+#               add descriptions to config options
+# 2010-08-07, Filip H.F. "FiXato" Slagter <fixato@gmail.com>
+#  version 0.8: add command on attach feature
+# 2010-05-07, Jani Kesänen <jani.kesanen@gmail.com>
+#  version 0.7: add command on detach feature
+# 2010-03-07, Aron Griffis <agriffis@n01se.net>
+#  version 0.6: move socket check to register,
+#               add hook_config for interval,
+#               reduce default interval from 60 to 5
+# 2010-02-19, Blake Winton <bwinton@latte.ca>
+#  version 0.5: add option to change nick when away
+# 2010-01-18, xt
+#  version 0.4: only update servers that are connected
+# 2009-11-30, xt <xt@bash.no>
+#  version 0.3: do not touch servers that are manually set away
+# 2009-11-27, xt <xt@bash.no>
+#  version 0.2: code for TMUX from penryu
+# 2009-11-27, xt <xt@bash.no>
+#  version 0.1: initial release
+
+import weechat as w
+import re
+import os
+import datetime, time
+
+SCRIPT_NAME    = "screen_away"
+SCRIPT_AUTHOR  = "xt <xt@bash.no>"
+SCRIPT_VERSION = "0.14"
+SCRIPT_LICENSE = "GPL3"
+SCRIPT_DESC    = "Set away status on screen detach"
+
+settings = {
+        'message': ('Detached from screen', 'Away message'),
+        'time_format': ('since %Y-%m-%d %H:%M:%S%z', 'time format append to away message'),
+        'interval': ('5', 'How often in seconds to check screen status'),
+        'away_suffix': ('', 'What to append to your nick when you\'re away.'),
+        'command_on_attach': ('', 'Commands to execute on attach, separated by semicolon'),
+        'command_on_detach': ('', 'Commands to execute on detach, separated by semicolon'),
+        'ignore': ('', 'Comma-separated list of servers to ignore.'),
+        'set_away': ('on', 'Set user as away.'),
+        'ignore_relays': ('off', 'Only check screen status and ignore relay interfaces'),
+}
+
+TIMER = None
+SOCK = None
+AWAY = False
+CONNECTED_RELAY = False
+
+def set_timer():
+    '''Update timer hook with new interval'''
+
+    global TIMER
+    if TIMER:
+        w.unhook(TIMER)
+    TIMER = w.hook_timer(int(w.config_get_plugin('interval')) * 1000,
+            0, 0, "screen_away_timer_cb", '')
+
+def screen_away_config_cb(data, option, value):
+    if option.endswith(".interval"):
+        set_timer()
+    return w.WEECHAT_RC_OK
+
+def get_servers():
+    '''Get the servers that are not away, or were set away by this script'''
+
+    ignores = w.config_get_plugin('ignore').split(',')
+    infolist = w.infolist_get('irc_server','','')
+    buffers = []
+    while w.infolist_next(infolist):
+        if not w.infolist_integer(infolist, 'is_connected') == 1 or \
+               w.infolist_string(infolist, 'name') in ignores:
+            continue
+        if not w.config_string_to_boolean(w.config_get_plugin('set_away')) or \
+                not w.infolist_integer(infolist, 'is_away') or \
+                    w.config_get_plugin('message') in w.infolist_string(infolist, 'away_message'):
+#                    w.infolist_string(infolist, 'away_message') == \
+#                    w.config_get_plugin('message'):
+            buffers.append((w.infolist_pointer(infolist, 'buffer'),
+                w.infolist_string(infolist, 'nick')))
+    w.infolist_free(infolist)
+    return buffers
+
+def screen_away_timer_cb(buffer, args):
+    '''Check if screen is attached, update awayness'''
+
+    global AWAY, SOCK, CONNECTED_RELAY
+
+    set_away = w.config_string_to_boolean(w.config_get_plugin('set_away'))
+    check_relays = not w.config_string_to_boolean(w.config_get_plugin('ignore_relays'))
+    suffix = w.config_get_plugin('away_suffix')
+    attached = os.access(SOCK, os.X_OK) # X bit indicates attached
+
+    # Check wether a client is connected on relay or not
+    CONNECTED_RELAY = False
+    if check_relays:
+        infolist = w.infolist_get('relay', '', '')
+        if infolist:
+            while w.infolist_next(infolist):
+                status = w.infolist_string(infolist, 'status_string')
+                if status == 'connected':
+                    CONNECTED_RELAY = True
+                    break
+            w.infolist_free(infolist)
+
+    if (attached and AWAY) or (check_relays and CONNECTED_RELAY and not attached and AWAY):
+        w.prnt('', '%s: Screen attached. Clearing away status' % SCRIPT_NAME)
+        for server, nick in get_servers():
+            if set_away:
+                w.command(server,  "/away")
+            if suffix and nick.endswith(suffix):
+                nick = nick[:-len(suffix)]
+                w.command(server,  "/nick %s" % nick)
+        AWAY = False
+        for cmd in w.config_get_plugin("command_on_attach").split(";"):
+            w.command("", cmd)
+
+    elif not attached and not AWAY:
+        if not CONNECTED_RELAY:
+            w.prnt('', '%s: Screen detached. Setting away status' % SCRIPT_NAME)
+            for server, nick in get_servers():
+                if suffix and not nick.endswith(suffix):
+                    w.command(server, "/nick %s%s" % (nick, suffix));
+                if set_away:
+                    w.command(server, "/away %s %s" % (w.config_get_plugin('message'), time.strftime(w.config_get_plugin('time_format'))))
+            AWAY = True
+            for cmd in w.config_get_plugin("command_on_detach").split(";"):
+                w.command("", cmd)
+
+    return w.WEECHAT_RC_OK
+
+
+if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
+                    SCRIPT_DESC, "", ""):
+    version = w.info_get('version_number', '') or 0
+    for option, default_desc in settings.iteritems():
+        if not w.config_is_set_plugin(option):
+            w.config_set_plugin(option, default_desc[0])
+        if int(version) >= 0x00030500:
+            w.config_set_desc_plugin(option, default_desc[1])
+
+    if 'STY' in os.environ.keys():
+        # We are running under screen
+        cmd_output = os.popen('env LC_ALL=C screen -ls').read()
+        match = re.search(r'Sockets? in (/.+)\.', cmd_output)
+        if match:
+            SOCK = os.path.join(match.group(1), os.environ['STY'])
+
+    if not SOCK and 'TMUX' in os.environ.keys():
+        # We are running under tmux
+        socket_data = os.environ['TMUX']
+        SOCK = socket_data.rsplit(',',2)[0]
+
+    if SOCK:
+        set_timer()
+        w.hook_config("plugins.var.python." + SCRIPT_NAME + ".*",
+            "screen_away_config_cb", "")
diff --git a/weechat/.weechat/python/urlview.py b/weechat/.weechat/python/urlview.py
new file mode 100644
index 0000000..a429f1e
--- /dev/null
+++ b/weechat/.weechat/python/urlview.py
@@ -0,0 +1,57 @@
+# This weechat plugin pipes the current weechat buffer through urlview
+#
+# Usage:
+# /urlview
+#
+# History:
+# 10-04-2015
+# Version 1.0.0: initial release
+# Version 1.0.1: reverse text passed to urlview
+# Version 1.0.2: remove weechat color from messages
+
+import distutils.spawn
+import os
+import pipes
+import weechat
+
+
+def urlview(data, buf, args):
+    infolist = weechat.infolist_get("buffer_lines", buf, "")
+    lines = []
+    while weechat.infolist_next(infolist) == 1:
+        lines.append(
+            weechat.string_remove_color(
+                weechat.infolist_string(infolist, "message"),
+                ""
+            )
+        )
+
+    weechat.infolist_free(infolist)
+
+    if not lines:
+        weechat.prnt(buf, "No URLs found")
+        return weechat.WEECHAT_RC_OK
+
+    text = "\n".join(reversed(lines))
+    response = os.system("echo %s | urlview" % pipes.quote(text))
+    if response != 0:
+        weechat.prnt(buf, "No URLs found")
+
+    weechat.command(buf, "/window refresh")
+
+    return weechat.WEECHAT_RC_OK
+
+
+def main():
+    if distutils.spawn.find_executable("urlview") is None:
+        return weechat.WEECHAT_RC_ERROR
+
+    if not weechat.register("urlview", "Keith Smiley", "1.0.2", "MIT",
+                            "Use urlview on the current buffer", "", ""):
+        return weechat.WEECHAT_RC_ERROR
+
+    weechat.hook_command("urlview", "Pass the current buffer to urlview", "",
+                         "", "", "urlview", "")
+
+if __name__ == "__main__":
+    main()