Script: msg_command.py

WARNING: this script has been disabled (not compatible with Python 3).

Run chat received in a buffer as commands.
Author: xt — Version: 0.2 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0.
Tags: command, py2
Added: 2010-11-05 — Updated: 2010-11-08

Download GitHub Repository

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
# -*- coding: utf-8 -*-
###
# Copyright (c) 2010 by xt <xt@bash.no>
# License: GPL3
#
#
#
#   Usage scenarios:
#
#   * Remote control weechat from a jabber account (from phone for example)
#       Requires you to send commands starting with a /
#   * Reply to messages sent via away_action
#       Requires away_action.py installed, will send input to the last buffer away_action recieved a
#       message from
#       
#
#   History:
#   2010-11-08:
#   version 0.2: check that message is from remote side
#   2010-11-04
#   version 0.1: initial release
#
###

SCRIPT_NAME    = "msg_command"
SCRIPT_AUTHOR  = "xt <xt@bash.no>"
SCRIPT_VERSION = "0.2"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Run chat recieved in a buffer as commands"

### Default Settings ###
settings = {
        'buffer': 'jabber.gtalk.otheraccount@otherserver.com', # Buffer to listen for commands
}

buffer_hooked_pointer = ''
hook = ''

try:
    import weechat
    w = weechat
    WEECHAT_RC_OK = weechat.WEECHAT_RC_OK
    import_ok = True
except:
    print "This script must be run under WeeChat."
    print "Get WeeChat now at: http://www.weechat.org/"
    import_ok = False

def msg_command_cb(data, buffer, time, tags, display, hilight, prefix, msg):

    if msg.startswith('/'):
        w.command('', msg)
    else:
        buffer_name = w.info_get('away_action_buffer', '')
        plugin = buffer_name.split('.')[0]
        buffer_name = '.'.join(buffer_name.split('.')[1:])
        buffer = w.buffer_search(plugin, buffer_name)
        if buffer:
            # Check if message is from remote and not a local
            if prefix in w.config_get_plugin('buffer'):
                w.command(buffer, msg)
    return WEECHAT_RC_OK

def hook_it(buffer_pointer):
    ''' Check if we need new hook, remove previous hook if exists '''

    global buffer_hooked_pointer, hook
    if buffer_pointer != buffer_hooked_pointer:
        w.unhook(hook)
        buffer_hooked_pointer = buffer_pointer
        hook = w.hook_print(buffer_pointer, '', '', 1, 'msg_command_cb', '')

def msg_command_conf_update(*args):
    if w.config_get_plugin('buffer'):
        buffer = w.buffer_search('', w.config_get_plugin('buffer'))
        hook_it(buffer)
    return WEECHAT_RC_OK


def msg_command_buffer_opened_cb(data, signal, signal_data):

    buffer = signal_data
    buffer_name = w.buffer_get_string(buffer, "name")
    if buffer_name == w.config_get_plugin('buffer'):
        hook_it(buffer)

    return WEECHAT_RC_OK

if __name__ == '__main__' and import_ok and \
        weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC,
        '', ''):


    for opt, val in settings.iteritems():
        if not weechat.config_is_set_plugin(opt):
            weechat.config_set_plugin(opt, val)

    weechat.hook_signal('buffer_opened', 'msg_command_buffer_opened_cb', '')
    weechat.hook_config('plugins.var.python.%s' %SCRIPT_NAME, 'msg_command_conf_update', '')
    msg_command_conf_update() # To init hook


# vim:set shiftwidth=4 tabstop=4 softtabstop=4 expandtab textwidth=100: