Home

Script: gribble.py

Automatically authenticate to gribble for Bitcoin OTC exchange. (for WeeChat ≥ 1.0)
Author: fluter, version 0.1, Apache — added: 2017-06-04
  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
104
105
106
107
108
109
110
111
112
113
114
115
# gribble - automatically authenticate to gribble for bitcoin otc exchange
# by Alex Fluter <afluter@gmail.com>
# irc nick @fluter
#
# before load this script, set your gpg passphrase by
#     /secure set gpg_passphrase xxxxxx

import re
import subprocess
import urllib2
import weechat

NAME = "gribble"
AUTHOR = "fluter <afluter@gmail.com>"
VERSION = "0.1"
LICENSE = "Apache"
DESCRIPTION = "Script to talk to gribble"

gribble_channel = "#bitcoin-fr"
gribble_nick = "gribble"
ident_nick = "fluter"
options = {
        # the channel name to watch to trigger this script
        "channel": "#bitcoin-otc",
        # the key of the secure data storing gpg passphrase
        "pass_key": "gpg_passphrase"
}
gribble_buffer = None


hook_msg = None

def init():
    global gribble_buffer
    gribble_buffer = weechat.buffer_new(NAME, "", "", "", "")
    weechat.prnt(gribble_buffer, "Options:")
    for opt, val in options.iteritems():
        if not weechat.config_is_set_plugin(opt):
            weechat.config_set_plugin(opt, val)
        else:
            options[opt] = weechat.config_get_plugin(opt)
        weechat.prnt(gribble_buffer, "    %s: %s" % (opt, options[opt]))

def privmsg(server, to, msg):
    buffer = weechat.info_get("irc_buffer", server)
    weechat.command(buffer, "/msg %s %s" % (to, msg))

def join_cb(data, signal, signal_data):
    dict_in = {"message": signal_data}
    dict_out = weechat.info_get_hashtable("irc_message_parse", dict_in)
    channel = dict_out["channel"]
    if channel != options["channel"]:
        return weechat.WEECHAT_RC_OK

    server = signal.split(",")[0]
    nick = dict_out["nick"]
    me = weechat.info_get("irc_nick", server)
    if nick != me:
        return weechat.WEECHAT_RC_OK

    weechat.prnt(gribble_buffer, "Channel %s joined" % channel)
    hook_msg = weechat.hook_signal("*,irc_in2_PRIVMSG", "privmsg_cb", "")
    weechat.prnt(gribble_buffer, "Sent eauth to %s" % gribble_nick)
    privmsg(server, gribble_nick, "eauth %s" % ident_nick)

    return weechat.WEECHAT_RC_OK

def privmsg_cb(data, signal, signal_data):
    dict_in = {"message": signal_data}
    dict_out = weechat.info_get_hashtable("irc_message_parse", dict_in)
    if not weechat.info_get("irc_is_nick", dict_out["channel"]):
        return weechat.WEECHAT_RC_OK

    server = signal.split(",")[0]
    nick = dict_out["channel"]
    me = weechat.info_get("irc_nick", server)
    if nick != me:
        return weechat.WEECHAT_RC_OK

    if dict_out["nick"] != gribble_nick:
        return weechat.WEECHAT_RC_OK

    msg = dict_out["text"]
    m = re.match("^.*Get your encrypted OTP from (.*)$", msg)
    if m is None:
        return weechat.WEECHAT_RC_OK
    weechat.prnt(gribble_buffer, "Got OTP")
    otp_url = m.group(1)
    otp = urllib2.urlopen(otp_url).read()
    # the passphrase is stored encrypted in secure data
    expr = "${sec.data.%s}" % options["pass_key"]
    gpg_pass = weechat.string_eval_expression(expr, "", "", "")
    if gpg_pass == "":
        weechat.prnt(gribble_buffer, "no gpg pass found in secure data")
        return weechat.WEECHAT_RC_OK

    p = subprocess.Popen(["gpg", "--batch", "--decrypt", "--passphrase", gpg_pass],
            stdin = subprocess.PIPE,
            stdout = subprocess.PIPE,
            stderr = subprocess.PIPE)
    out, err = p.communicate(otp)
    if err != "":
        weechat.prnt(gribble_buffer, "gpg output: " + err)
    if out != "":
        privmsg(server, gribble_nick, "everify %s" % out)

    return weechat.WEECHAT_RC_OK

def main():
    init()
    hook_join = weechat.hook_signal("*,irc_in2_JOIN", "join_cb", "foo")

weechat.register(NAME, AUTHOR, VERSION, LICENSE, DESCRIPTION, "", "")
weechat.prnt("", "%s %s loaded" % (NAME, VERSION))
main()