irc PMs to google chat

In open source, irc is a crucial means of communication. For years I’ve connected to screen sessions running irssi so I could stay connected while I was offline. That way I could connect from any computer, my phone, whatever.

But that means that when I want a pretty graphical client, I can’t easily do it. That’s where bouncers like dircproxy come in. I can connect from irssi one day, xchat another. But those only allow one simultaneous connection. So recently I tried out znc, which does support multiple connections. It also supports modules with callbacks when messages are sent.

znc had modules for forwarding messages if you have android or iphone. But I don’t have those. So I found some code for sending messages to Google chat, and used that in the following perl module, which sends personal irc messages to my google chat account.

To use, call it gchat.pm, place it under ~/.znc/modules, load modperl, and then load gchat. I just have

LoadModule = modperl

in my global config section, and

LoadModule = gchat

in each user section (that I want it in).

Enjoy.

# gchat.pm

package gchat;
use base 'ZNC::Module';
#use strict;
use Net::XMPP;
use POSIX qw(strftime);

# Here's a funny thing - Thus0 appears the most likely author of this
# code, however the version of this which I first found was NOT attributed
# to him.
# See http://www.pervasive-network.org/SPIP/Google-Talk-with-perl-bis
#
# author: Thus0 
# Copyright (c) 2005, Thus0 . All rights reserved.
#
# released under the terms of the GNU General Public License v2

sub description {
	"Forward channel messages from znc to Google chat"
}

sub sendmessage {
	my $body = shift;
	## Configuration

	my $username = "my.google.chat.irc.username" # username of extra gchat account
	my $password = "my.google.chat.pwd"

	my $to = "my.google.chat.username"  # real username

	my $resource = "PerlBot";

	## End of configuration

	#------------------------------------

	# Google Talk & Jabber parameters :

	my $hostname = 'talk.google.com';
	my $port = 5222;
	my $componentname = 'gmail.com';
	my $connectiontype = 'tcpip';
	my $tls = 1;

	#------------------------------------

	my $Connection = new Net::XMPP::Client();

	# Connect to talk.google.com
	my $status = $Connection->Connect(
	       hostname => $hostname, port => $port,
	       componentname => $componentname,
	       connectiontype => $connectiontype, tls => $tls);

	if (!(defined($status))) {
	   print "ERROR:  XMPP connection failed.\n";
	   print "        ($!)\n";
	   return 0;
	}

	# Change hostname
	my $sid = $Connection->{SESSION}->{id};
	$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;

	# Authenticate
	my @result = $Connection->AuthSend(
	       username => $username, password => $password,
	       resource => $resource);

	if ($result[0] ne "ok") {
	   print "ERROR: Authorization failed: $result[0] - $result[1]\n";
	   return 0;
	}

	# Send message
	$Connection->MessageSend(
	       to => "$to\@$componentname", body => $body,
	       resource => $resource);
	return 1;
}

sub OnPrivMsg {
    my $self = shift;
    my ($nick, $msg) = @_;

    $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
    $sendmsg = "At $now_string:".  $nick->GetNick. ": ".$msg;
    $ret = sendmessage($sendmsg);
    $self->PutModule("$sendmsg (gchat returned $ret)");
    return $ZNC::CONTINUE;
}

sub OnChanMsg {
    my $self = shift;
    my ($nick, $chan, $msg) = @_;

#    do selective filtering here
    $i = index($msg, "my_nick");  # use your irc nick
    if ($i == -1) {
	    return $ZNC::CONTINUE;
    }

    $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;

    $sendmsg = "At $now_string:";
    $sendmsg .= $nick->GetNick."@".$chan->GetName.": ".$msg;
    $ret = sendmessage($sendmsg);
#the next line sends the same msg to an irc window called '*gchat'
    $self->PutModule("$sendmsg (gchat returned $ret)");
    return $ZNC::CONTINUE;
}

1;
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to irc PMs to google chat

  1. Sem Schilder says:

    what kind of code language is this code u published

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s