From f10a7b46d85492220e2c50b4d26cbc64d70ceec4 Mon Sep 17 00:00:00 2001 From: Christophe Simon Date: Wed, 17 Sep 2014 09:52:11 +0200 Subject: [PATCH] Updated client key bindings... ...and added hipchat notification script. --- .gitignore | 1 + bin/mcabber-hipchat-notify | 158 +++++++++++++++++++++++++++++++++++++ rc/keys.lua | 21 +++-- rc/tags.awful.lua | 10 +-- 4 files changed, 177 insertions(+), 13 deletions(-) create mode 100755 bin/mcabber-hipchat-notify diff --git a/.gitignore b/.gitignore index d8382ec..71579a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ rc.lua.test +.ropeproject diff --git a/bin/mcabber-hipchat-notify b/bin/mcabber-hipchat-notify new file mode 100755 index 0000000..c57b198 --- /dev/null +++ b/bin/mcabber-hipchat-notify @@ -0,0 +1,158 @@ +#!/usr/bin/env python + +import hypchat +import cPickle +import subprocess +import traceback +import sys +import os + + +HIPCHAT_TOKEN = "EpAE2LqpWG3HhLtiqGdbOhqz4H9eE5c2dOBSKJli" +HIPCHAT_CONFIG = "~/.hipchatrc" +HIPCHAT_CACHE = os.path.expanduser("~/.mcabber/hipchat.cache") +NOTIF_SCRIPT = "/usr/bin/awesome-client" + + + + +class NaughtyNotification(object): + """ + Nautification class used to send notification using the Naughty + notification system. + """ + kind = "MSG" + + def __init__(self, jid): + self.jid = jid + + def send(self, direction, message): + if self.direction is not None: + direction = self.direction + + cmnd = "mcabber_event_hook('%s', '%s', '%s', '%s')" % ( + self.kind, direction, self.jid, message) + print cmnd + + p = subprocess.Popen([NOTIF_SCRIPT], stdin=subprocess.PIPE) + p.communicate(cmnd) + + +class UserNotification(NaughtyNotification): + direction = None + + +class RoomNotification(NaughtyNotification): + direction = "MUC" + + +class HipchatContactResolver(object): + """ + Resolves an hipchat identifier into a friendly name, and return a + Notification object corresponding to the message type. + """ + + def __init__(self): + self.config = self.get_config() + if os.path.isfile(HIPCHAT_CACHE): + f = open(HIPCHAT_CACHE, "r") + self._cache = cPickle.load(f) + f.close() + else: + self._cache = {} + + def get_config(self): + config = {} + filename = os.path.expanduser(HIPCHAT_CONFIG) + f = open(filename, "r") + for row in f.readlines(): + row.strip() + key, value = row.split("=", 1) + config[key] = value + f.close() + return config + + def cache(self, hid, name): + """ + Adds new data to cache, and dump it on disk. + + @param hid: The hipchat identifier to map in cache. + @param name: The name to associate with it. + """ + self._cache[hid] = name + f = open("%s.tmp" % HIPCHAT_CACHE, "w") + cPickle.dump(self._cache, f) + f.close() + os.rename("%s.tmp" % HIPCHAT_CACHE, HIPCHAT_CACHE) + + def resolve(self, hid): + """ + Tries to resolve an hipchat identifier into a friendly name. + + If found, it updates its cache, and dumps it. + + @param hid: The hipchat identifier to map. + @return a Notificion object + """ + nid, server = hid.split('@') + kind = server.split('.')[0] + + if kind == "chat": + cls = UserNotification + name = self.resolve_user(nid) + else: + cls = RoomNotification + name = self.resolve_room(nid) + + if name is None: + return cls(hid) + else: + return cls(name) + + def resolve_user(self, nid): + """ + Tries to resolve an user identifier. + + @param nid: The numeric id to look for. + @return The mapped name, or None if not found. + """ + if nid in self._cache: + return self._cache[nid] + + gid, cid = nid.split('_') + hipchat = hypchat.HypChat(self.config.get("TOKEN", "")) + try: + user = hipchat.get_user(int(cid)) + name = user["mention_name"] + self.cache(nid, name) + return name + except: + return None + + def resolve_room(self, nid): + """ + Maps a room name into a friendly name. + + @param nid: The numeric id to look for. + @return The mapped name, or None if not found. + """ + room = " ".join(nid.split("_")[1:]) + return room.title() + + +if __name__ == "__main__": + if len(sys.argv) != 5: + print "usage: %s kind direction jid message" % os.path.basename(sys.argv[0]) + sys.exit(1) + + try: + f = open("/tmp/notif.txt", "a") + kind, direction, jid, message = sys.argv[1:] + f.write("%s %s %s %s\n" % (kind, direction, jid, message)) + resolver = HipchatContactResolver() + notification = resolver.resolve(jid) + notification.send(direction, message) + except: + f.write(traceback.format_exc()) + finally: + f.close() diff --git a/rc/keys.lua b/rc/keys.lua index bc0e6bd..cdc08ff 100644 --- a/rc/keys.lua +++ b/rc/keys.lua @@ -27,24 +27,29 @@ global_keys = awful.util.table.join( awful.key({ modkey, "Shift" }, "Tab", function () utils.client.viewnext(-1) end), -- Remapped H - awful.key({ modkey, }, "c", function () awful.tag.incmwfact(-0.05) end), + -- awful.key({ modkey, }, "c", function () awful.tag.incmwfact(-0.05) end), + awful.key({ modkey, }, "c", function () awful.client.swap.byidx(-1) end), awful.key({ modkey, "Shift" }, "c", function () awful.tag.incnmaster(1) end), awful.key({ modkey, "Control" }, "c", function () awful.tag.incncol(1) end), + awful.key({ modkey, "Mod1" }, "c", function () awful.tag.incmwfact(-0.05) end), -- Remapped J - awful.key({ modkey, }, "t", function () utils.client.viewnext(-1) end), - awful.key({ modkey, "Shift" }, "t", function () awful.client.swap.byidx(-1) end), + awful.key({ modkey, }, "t", function () utils.client.viewnext(-1) end), + awful.key({ modkey, "Shift" }, "t", function () awful.tag.incncol(1) end), awful.key({ modkey, "Control" }, "t", function () awful.screen.focus_relative(-1) end), + awful.key({ modkey, "Mod1" }, "t", function () awful.client.incwfact(-0.05) end), -- Remapped K - awful.key({ modkey, }, "s", function () utils.client.viewnext(1) end), - awful.key({ modkey, "Shift" }, "s", function () awful.client.swap.byidx(1) end), + awful.key({ modkey, }, "s", function () utils.client.viewnext(1) end), + awful.key({ modkey, "Shift" }, "s", function () awful.tag.incncol(-1) end), awful.key({ modkey, "Control" }, "s", function () awful.screen.focus_relative(1) end), + awful.key({ modkey, "Mod1" }, "s", function () awful.client.incwfact(0.05) end), -- Remapped L - awful.key({ modkey, }, "r", function () awful.tag.incmwfact(0.05) end), - awful.key({ modkey, "Shift" }, "r", function () awful.tag.incnmaster(-1) end), - awful.key({ modkey, "Control" }, "r", function () awful.tag.incncol(-1) end), + awful.key({ modkey, }, "r", function () awful.client.swap.byidx(1) end), + awful.key({ modkey, "Shift" }, "r", function () awful.tag.incnmaster(-1) end), + awful.key({ modkey, "Control" }, "r", function () awful.tag.incncol(-1) end), + awful.key({ modkey, "Mod1" }, "r", function () awful.tag.incmwfact(0.05) end), -- Jump to urgent client awful.key({ modkey, }, "g", awful.client.urgent.jumpto), diff --git a/rc/tags.awful.lua b/rc/tags.awful.lua index e6947a5..7aca476 100644 --- a/rc/tags.awful.lua +++ b/rc/tags.awful.lua @@ -104,12 +104,12 @@ awful.rules.rules = { { rule = { class = "Thunderbird" }, properties = { tag = tags[1][2]} }, - -- Terms - { rule = { class = "XTerm" }, - properties = { opacity = 0.7, tag = tags[1][3]} }, + -- Terms (no more bound) + -- { rule = { class = "XTerm" }, + -- properties = { opacity = 0.7, tag = tags[1][3]} }, - { rule_any = { class = {"Gnome-terminal", "URxvt"} }, - properties = { tag = tags[1][3] } }, + -- { rule_any = { class = {"Gnome-terminal", "URxvt"} }, + -- properties = { tag = tags[1][3] } }, -- Gvim { rule = { class = "Gvim" },