Wednesday, May 20, 2009

Logging to GTalk

I was reading yesterday that Apple is thought to be using XMPP the Jabber protocol for their new iPhone Push Notification technology.

That's funny I thought, I had just started using XMPP for logging!!!

I am experimenting with Asterisk at the moment. Asterisk for those that do not know is an Open Source PBX (Private Branch Exchange) a phone system in software. I have it running on my ancient 500 Mhz Mac Cube, it has been extremely reliable. My home office now has a ridiculously sophisticated phone system ;-)

IMHO most of the monitoring tools for Asterisk suck. They are written in nasty languages like PHP, Perl, Flash etc. that not only do I not want on my system, they are overkill.

What I wanted was to receive simple messages about the run-time status of the system. I wanted to be able to receive these messages securely, from wherever I was (because I can use my phone system from wherever I am, but more on that another time).

Asterisk has a Jabber module, so this was an easy way to go.

So, I have two GMail Accounts, one for personal email, one for mailing lists.
I have iChat on my desktop/laptop/iPhone already subscribed to GTalk on my personal GMail account.

First I added my spare GTalk account as a buddy to my personal account (requires some fiddling around, but if you know your IM client, it's pretty easy).

Then I configured Asterisk (beware, she's a complex beast, BTW a semi-colon marks a comment).

First configure /etc/asterisk/jabber.conf :
[general]
debug=yes ;Turn on debugging by default.
autoprune=no ;Auto remove users from buddy list.
autoregister=no ;do not auto register users from buddy list.

[gtalk-logger] ;label
type=client ;Client or Component connection
serverhost=talk.google.com
username=MY_SPARE_GTALK_USERNAME@gmail.com/asterisk ;Username with optional roster.
secret=************** ;Password for MY_SPARE_GTALK_USERNAME
port=5222 ;Port to use defaults to 5222
usetls=yes ;Use tls or not
usesasl=yes ;Use sasl or not
buddy=MY_MAIN_GTALK_USERNAME@gmail.com ;Manual addition of buddy to list.
statusmessage=Up and Running ;custom status message for Asterisk.
timeout=100 ;Timeout on the message stack.


This allows Asterisk to connect to GTalk to send messages.

The next thing to do is to emit messages from the appropriate parts of your dialplan.

First I defined a Macro, that can be used from anywhere in the dialplan, this goes in /etc/asterisk/extensions.conf :

[macro-logger]
; log message - to Jabber
; @param ${ARG1} - the message to send
exten => s,,Jabbersend(gtalk-logger,MY_MAIN_GTALK_USERNAME@gmail.com,${STRFTIME(${EPOCH},GMT,%C%y-%m-%d %H:%M%n)} ${ARG1})


This sends the message in $ARG1 with a timestamp.

Next is to use the macro from the dialplan. Here is an example of an outbound route to go via SipBroker, matching dialed numbers beginning with * :
[via-sipbroker]
exten => _*X.,1,Macro(logger,Outbound call to ${EXTEN} via SipBroker) ; log the call
exten => _*X.,2,Set(CALLERID(all)=${JQNAME} <${JQOFFICE}>) ; Set outbound CallerID
exten => _*X.,3,ChanIsAvail(SIP/${EXTEN}@sipbroker,j) ; Check to see if available (jumps to priority + 101 on fail)
exten => _*X.,4,Dial(SIP/${EXTEN}@sipbroker,,tTW) ; Dial, allowing transferring and recording
exten => _*X.,5,Macro(dial-result) ; Check result
exten => _*X.,104,Playback(all-circuits-busy-now) ; if ChanIsAvail fails, say message


I know, it looks really weird !!!! :-)

I left out a few details like, looking up the phone number in my AddressBook.app to show the recipient's name and using Growl.app to splash these messages received by iChat. I plan to cover these in a subsequent post.

I have found this to be a really simple and reliable solution.

There are XMPP libraries for many systems, I may start using this technique elsewhere as well.

No comments: