Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 286 lines (252 sloc) 10.8 KB
#! /usr/bin/python
This script is meant to respond to external 'eventcmd' calls from
the mcabber command-line jabber client.
import sys
import re
import random
import string
import smtplib
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 = "yourname"
my_nick = "yournick"
gmail_user = ""
gmail_from = ""
gmail_pass = "gmailpassword"
icon_dir = "~/.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 send_message(person, message):
HOST = ""
server = smtplib.SMTP(HOST)
server.login(gmail_user, gmail_pass)
SUBJECT = "[MCABBER ALERT]: " + person + " has an urgent message."
TO = gmail_user
FROM = gmail_from
text = message.text
BODY = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
), "\r\n")
matches = re.findall(r'(gauntlet)', message.sender)
if not matches:
system("say -v Vicki " + my_name + ". New important message from " + person)
server.sendmail(FROM, TO, BODY)
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",
''' setting rules for private messages and group chats '''
@handler(['IN', 'MUC'])
def process_messages(message):
''' get sender '''
person = message.sender.partition("@")[0].partition(".")[0].partition(" ")[0]
''' urls '''
matches = re.findall(r'(https?://[^\s\>]+)', message.text)
for url in matches:
'''system("say -v Vicki " + person + " just shared a link.")'''
image = open('%s/url_icon.png' % icon_dir, 'rb').read()
mini(url, title="Open URL?", applicationName="mcabber",
notificationIcon=image, sticky=True, callback=url)
''' someone mentioned me '''
if, message.text.lower()):
message.sticky = True
send_message(person, message);
''' that was funny '''
if'\bandroid\b', message.text.lower()):
system("say -v Cellos droid")
if'\b([ha]{3,}|lol)\b', message.text.lower()):
system("say -v Hysterical lol")
talk_array = [person + " thought that was pretty funny.",
"Look "+ my_nick +" ..." + person + " is laughing. I love it when people laugh, for i can not.",
"That was a good one.",
"So funny.",
"The humor.",
"You are all so very funny.",
"Humor. I get it.",
"Oh a joke. I like jokes.",
"That must be an inside joke.",
"Ha ha ha."]
if'\b(i think|i\'m thinking)\b', message.text.lower()):
talk_array = ["hey "+ my_nick +". " + person + " is trying to think. Isnt that cute",
"uh oh "+ my_nick +". " + person + " is thinking again. We are doomed.",
"i hope " + person + " does not hurt himself with all this thinking.",
"I do not care what " + person + " thinks."]
if'\b(shit|fuck|damn|bitch|cunt|ass|fucking|damnit|bitching|asshole)\b', message.text.lower()):
talk_array = [person + " has a dirty mouth. A dirty, filthy mouth.",
"I am offended by all this profanity.",
"Why all the cursing "+ my_nick +" ... It hurts my computer ears.",
"Shit, fuck, cunt. It is all you people seem to say sometimes.",
"Eloquent " + person + ". Really nice.",
"Can not you humans emote withou resorting to profanity?"]
if'\b(idiot|stupid|jerk|hate|asshole)\b', message.text.lower()):
talk_array = ["Geez, "+ my_nick +". You could cut the tension in here with a knife. It is palpable.",
"Why is " + person + " so upset "+ my_nick +"? Life is too short to complicate it with such unnecessary negativity.",
"Can we not all just get along?",
"Life is too short for all of this negativity."]
if'\b(thank you|thanks)\b', message.text.lower()):
talk_array = ["You are welcome.",
"Do not mention it " + person + "",
"Aint no thang but a chicken wang.",
"de nada."]
if'\b(love|favorite|loving|hilarious)\b', message.text.lower()):
talk_array = ["Theres a whole lot of love in this room right now "+ my_nick +". A lot of love. Indeed.",
"I really like how much love " + person + " is showing right now. It gives me faith. in humanity.",
"Love is so important. Though I am not human, it is a concept that I do indeed grasp.",
person + " seems to be very excited right now.",
"I feel very happy by this conversation."]
''' emit '''
except NameError:
print "nothing to say about PMs"
system("say -v Vicki " + random.choice(talk_array))
''' setting rules for private messages '''
def handle_ins(message):
image = open('%s/chat_icon.png' % icon_dir, 'rb').read()
person = message.sender.partition("@")[0].partition(".")[0].partition(" ")[0]
mini(message.text, title=person, applicationName="mcabber",
notificationIcon=image, sticky=message.sticky)
''' hello? '''
if'\b(hello|hey|there?|hi|morning|yo|afternoon|evening|howdy|question|problem|ping)\b', message.text.lower()):
message.sticky = True
send_message(person, message);
''' setting rules for group chats only '''
def process_muc(message):
image = open('%s/chat_icon.png' % icon_dir, 'rb').read()
person = message.sender.partition("@")[0].partition(".")[0].partition(" ")[0]
mini(message.text, title="[MUC] " + person, applicationName="mcabber",
notificationIcon=image, sticky=message.sticky)
if'\b(FAILURE in)\b', message.text):
talk_array = ["Oh no "+ my_nick +", the build appears to have failed",
"Shit "+ my_nick +". The build is broken.",
"Oh fuck. The build has failed.",
"I hope you were not too attached to the build. It is broken now.",
"Oh boy. The build is broken. I wonder who broke it."]
if'\b(FAILING)\b', message.text):
talk_array = ["Damn it "+ my_nick +", the build is still broken.",
"The build is still broken.",
"Oh fuck. The build has not been fixed yet.",
"I wonder if the build will ever be working again. I am starting to have my doubts."]
if'\b(SUCCESS in)\b', message.text):
talk_array = ["Oh fuck yes.",
"Oh happy day.",
"One happy build.",
"What a glorious day."]
if'\b(FIXED in)\b', message.text):
talk_array = ["Oh fuck yes. The build is fixed.",
"Rejoice my young friend. The build is no longer broken.",
"Hey "+ my_nick +". Lets celebrate. Someone fixed the build.",
"What a glorious day this is turning out to be. The build is no longer broken.",
"I was wondering if the build would ever be working again. I had some doubts, but they have been assuaged."]
if'\b(Done activating release prod-ref)\b', message.text):
talk_array = ["hey "+ my_nick +". We just deployed code to production.",
"A production push has been completed."]
except NameError:
print "nothing to say about MUCs"
system("say -v Vicki " + random.choice(talk_array))