Created
April 6, 2013 20:17
-
-
Save letsgetrandy/5327469 to your computer and use it in GitHub Desktop.
event_command script for mcabber
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
''' | |
EVENTCMD - | |
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 | |
logging.basicConfig(level=logging.ERROR) | |
''' | |
CONFIG - | |
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: | |
Message | |
parses command-line input and converts it into an object to be | |
processed by handlers (defined later) | |
MessageHander | |
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 | |
self.append(fn) | |
return fn | |
return decorator | |
def process_events(self, message): | |
for fn in self: | |
if message.kind in fn.types: | |
fn(message) | |
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 = mg.group(1)[1:-1] | |
self.text = mg.group(2) | |
if self.sender == my_name: | |
return | |
elif self.kind == "STATUS": | |
self.text = sys.argv[2] | |
handler.process_events(self) | |
def get_text(self, filename): | |
msg = "" | |
with open(filename, "r") as f: | |
msg = f.read() | |
try: | |
remove(filename) | |
except OSError: | |
pass | |
return msg | |
''' | |
MESSAGE HANDLERS - | |
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. | |
''' | |
@handler() | |
def debugger(message): | |
if debug: | |
mini(message.args, title="Debug", applicationName="mcabber") | |
@handler(['STATUS']) | |
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", | |
notificationIcon=image) | |
@handler(['IN', 'MUC']) | |
def sticky_mentions(message): | |
if re.search(my_name.lower(), 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) | |
@handler(['MUC']) | |
def for_the_lulz(message): | |
''' Speak "lol" ''' | |
if re.search(r'\b([ha]{3,}|lol)\b', message.text): | |
system("say -v Hysterical lol") | |
@handler(['MUC']) | |
def droid(message): | |
''' Say "Droid" ''' | |
if re.search(r'\bandroid\b', message.text): | |
system("say -v Cellos droid") | |
Message() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good stuff.