Image of the glider from the Game of Life by John Conway
Skip to content

IRC Notifications Over SMS

Recently, I pinged my boss about a networking question, and got the following response:

I am away but your message is being sent to my phone.

Well, there are a few things he could be doing here:

  1. Logged in 24/7 with a local IRC client on his phone. Easiest, but will drain his battery quickly.
  2. Using an IRC script to send away messages to email. Pull notifications could be slow.
  3. Using an IRC script to send away messages to SMS. Snappy push notifications.

I've done both #1 and #2, but have never attempted #3, so I thought I'd give it a go. That way, the alert would be the most responsive, and I could login to IRC with a local client on my phone to address the issue, if it's important enough.

Now, to be clear, I'm not using Irssi. This may come as a shock to many of you, but last April, I discovered ZNC and WeeChat, and I haven't looked back. I REALLY like this setup. So, that means no more Irssi posts for this blog. It's time for WeeChat posts. Hopefully, I can do as good of a job. Further, this post addresses a script that must be running in WeeChat, not ZNC.

First, install http://weechat.org/scripts/source/stable/zmq_notify.rb.html/ in WeeChat. If running Debian/Ubuntu, this will mean installing the "built-essential", "ruby1.8" and "ruby1.8-dev" packages, then running "gem install zmq" to get the 0mq Ruby modules installed. Then restart WeeChat (yes, this is necessary- trust me) and load the script, and you should be good to go. If not, troubleshoot.

Now, the script sends YAML through the 0mq socket. Unfortunately, the YAML is not syntactically correct, and it's delivering base64-encoded binary. Meh. We can handle that. So, we need to connect to the 0mq socket that the script sets up, parse the YAML, then send the message as an email to an email-to-sms gateway. If you have a mobile phone, then the major phone providers have likely already set this up for you: See https://en.wikipedia.org/wiki/List_of_SMS_gateways for a fairly comprehensive list.

So, what does the script look like?

UPDATE: I created a Github project: https://github.com/atoponce/ircsms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python

import base64
import email.utils
import re
import smtplib
import yaml
import zmq
from email.mime.text import MIMEText

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, '')
socket.connect('tcp://127.0.0.1:2428')

while True:
    f = open('/var/log/0mq.log','a')
    msg = socket.recv()
    msg = re.sub('\n:', '\n', msg)
    msg = re.sub('^---| !binary \|-\n','',msg)
    y = yaml.load(msg)

    f.write(msg)
    f.close()

    # Ignore client events that aren't PUBLIC
    if not y['tags']:
        continue

    server = base64.b64decode(y['server'])
    channel = base64.b64decode(y['channel'])
    nick = base64.b64decode(y['tags'][3])
    nick = re.sub('^nick_','',nick)
    message = base64.b64decode(y['message'])

    # If sending messages to the channel while away, it shows up as
    # "prefix_nick_white". This can change it to your nick.
    if nick == 'prefix_nick_white':
        nick = 'eightyeight'

    # Change your email-to-sms address as provided by your mobile provider
    fromaddr = 'weechat@irc.example.com'
    toaddr = '1234567890@messaging.sprintpcs.com'
    msg = MIMEText("{0}/{1}: <{2}> {3}".format(server, channel, nick, message))
    msg['To'] = email.utils.formataddr(('eightyeight', toaddr))
    msg['From'] = email.utils.formataddr(('WeeChat', fromaddr))

    s = smtplib.SMTP('localhost')
    s.sendmail(fromaddr, [toaddr], msg.as_string())
    s.quit()

Place the code in your /etc/rc.local, or create a valid init script for it, and you're ready to go. Next time you set your status to away on IRC, you'll get an SMS alert every time someone highlights you in an IRC channel, or your receive a private message. If you're running WeeChat behind a terminal multiplexor, like GNU Screen or tmux, then you could also install an away script that sets your status away automatically when you detach from your session. Of course, if WeeChat disconnects from your bouncer, then this won't do much good for you.

Here is an example of an SMS alert from a private message from a bot:

weechat@irc.example.com said:
Subject: IRC Notification
21:59:34 freenode/ibot: <ibot> for heaven's sake, eightyeight, don't do that!

There are some outstanding bugs, and I'm working them out. In the meantime, if this interests you, then it should get you started with basic functionality. Happy IRC over SMS!

{ 8 } Comments