Skip to content

Instantly share code, notes, and snippets.

Created April 6, 2013 20:17
What would you like to do?
event_command script for mcabber
#! /usr/bin/python
This script is meant to respond to external 'eventcmd' calls from
the mcabber command-line jabber client.
import sys
import re
from os import remove, system
from gntp.notifier import mini
import logging # this is necessary to handle GNTP errors
Set global values here
debug = False
my_name = "Randy"
icon_dir = "/Users/randy/.mcabber/icons"
Main functionality -
This section defines two core classes which make up the core
functionality of this script:
parses command-line input and converts it into an object to be
processed by handlers (defined later)
defines a list that holds the handlers you will define later
class MessageHandler(list):
''' Decorator list for message handlers '''
def __call__(self, types=['IN', 'MUC', 'STATUS'], **kwargs):
def decorator(fn):
fn.types = types
return fn
return decorator
def process_events(self, message):
for fn in self:
if message.kind in fn.types:
handler = MessageHandler()
class Message(object):
# member properties
text = ""
sender = ""
sticky = False
def __init__(self):
self.args = sys.argv
self.kind = sys.argv[1]
self.sender = sys.argv[3]
if self.kind == "MSG":
self.sticky = False
self.kind = sys.argv[2]
self.text = self.get_text(sys.argv[4])
# get the name differently for MUC
if self.kind == "MUC":
rx = re.compile(r"(^<[^>]+>) (.+$)")
mg = rx.match(self.text)
if mg:
self.sender =[1:-1]
self.text =
if self.sender == my_name:
elif self.kind == "STATUS":
self.text = sys.argv[2]
def get_text(self, filename):
msg = ""
with open(filename, "r") as f:
msg =
except OSError:
return msg
This is where you define individual handlers for incoming messages.
Each message handler is marked by the @handler decorator, and receives
the message object as an input parameter.
def debugger(message):
if debug:
mini(message.args, title="Debug", applicationName="mcabber")
def status_change(message):
states = {
"o": "came online",
"f": "is free for chat",
"d": "do not disturb",
"n": "is not available",
"a": "went away",
"_": "went offline",
msg = states[message.text]
title = message.sender
image = open('%s/info_icon.png' % icon_dir, 'rb').read()
mini(msg, title=title, applicationName="mcabber",
@handler(['IN', 'MUC'])
def sticky_mentions(message):
if, message.text):
message.sticky = True
@handler(['IN', 'MUC'])
def queue_up_links(message):
matches = re.findall(r'(https?://[^\s\>]+)', message.text)
for url in matches:
image = open('%s/url_icon.png' % icon_dir, 'rb').read()
mini(url, title="Open URL?", applicationName="mcabber",
notificationIcon=image, sticky=True, callback=url)
@handler(['IN', 'MUC'])
def growl(message):
''' Growl notifications '''
image = open('%s/chat_icon.png' % icon_dir, 'rb').read()
mini(message.text, title=message.sender, applicationName="mcabber",
notificationIcon=image, sticky=message.sticky)
def for_the_lulz(message):
''' Speak "lol" '''
if'\b([ha]{3,}|lol)\b', message.text):
system("say -v Hysterical lol")
def droid(message):
''' Say "Droid" '''
if'\bandroid\b', message.text):
system("say -v Cellos droid")
Copy link

good stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment