-
-
Save dep-deprecated/5670726 to your computer and use it in GitHub Desktop.
event_command script for mcabber (fork from letsgetrandy)
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 | |
import string | |
import smtplib | |
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 = "Your Name Here" | |
gmail_user = "to_gmail_address" | |
gmail_from = "from_gmail_address" | |
gmail_pass = "gmail_password" | |
icon_dir = "/path/to/.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): | |
system("say -v Vicki " + my_name + ", you have a new mention.") | |
message.sticky = True | |
@handler(['IN']) | |
def initial(message): | |
if re.search(r'\b(insert|various|keyword|triggers|here)\b', message.text): | |
system("say -v Vicki " + my_name + "? New important message from " + message.sender.partition("@")[0].partition(".")[0]) | |
message.sticky = True | |
HOST = "smtp.gmail.com" | |
server = smtplib.SMTP(HOST) | |
server.starttls() | |
server.login(gmail_user, gmail_pass) | |
SUBJECT = "[MCABBER ALERT]: " + message.sender.partition("@")[0].partition(".")[0] + " has a message for you." | |
TO = gmail_user | |
FROM = gmail_from | |
text = message.text | |
BODY = string.join(( | |
"From: %s" % FROM, | |
"To: %s" % TO, | |
"Subject: %s" % SUBJECT , | |
"", | |
text | |
), "\r\n") | |
server.sendmail(FROM, TO, BODY) | |
server.quit() | |
@handler(['MUC']) | |
def urgent(message): | |
if re.search(r'\b(keyword|triggers|go|here)\b', message.text): | |
message.sticky = True | |
HOST = "smtp.gmail.com" | |
server = smtplib.SMTP(HOST) | |
server.starttls() | |
server.login(gmail_user, gmail_pass) | |
SUBJECT = "[MCABBER ALERT]: " + message.sender.partition("@")[0].partition(".")[0] + " has an urgent chatroom message." | |
TO = gmail_user | |
FROM = gmail_from | |
text = message.text | |
BODY = string.join(( | |
"From: %s" % FROM, | |
"To: %s" % TO, | |
"Subject: %s" % SUBJECT , | |
"", | |
text | |
), "\r\n") | |
matches = re.findall(r'(gauntlet)', message.sender) | |
if not matches: | |
system("say -v Vicki " + my_name + ". Urgent chatroom message from " + message.sender.partition("@")[0].partition(".")[0]) | |
server.sendmail(FROM, TO, BODY) | |
server.quit() | |
@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(['IN', '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