#!/usr/bin/perl # # This is the hack to integrate Twitter with Wordpress AJAX Shoutbox # Wordpress AJAX Shoutbox - http://pierre.sudorvoich.free.fr/ # # If you intent to modify this to do other stuff, just change the # logic in recieve_chat() # # This is release under BSD license # http://www.opensource.org/licenses/bsd-license.php # # In short, do whatever you like with it :-) # use Net::Jabber; use Mysql; $DEBUG = 0; # Set this to 1 or 2 for debugging $JABBER_HOST = "HOST"; # talk.google.com $JABBER_PORT = 5222; # default port 5222 $JABBER_USER = "USER"; # Your gtalk account $JABBER_DOMAIN = "DOMAIN"; # gmail.com $JABBER_PASS = "PASSWORD"; # ... $JABBER_TLS = 1; # Set this to 0 if not using TLS $JABBER_RES = 'Gaim'; $TWITTER_ID = 'twitter@twitter.com'; # # MySQL information # $MYSQL_HOST = 'HOST'; $MYSQL_DB = 'DB'; $MYSQL_USER = 'USER'; $MYSQL_PASS = 'PASSWORD'; $MYSQL_PRE = 'WP_PREFIX'; my $client = new Net::Jabber::Client(debuglevel=>$DEBUG); my $mysql = Mysql->connect($MYSQL_HOST, $MYSQL_DB, $MYSQL_USER, $MYSQL_PASS); $mysql->selectdb($MYSQL_DB); $client->SetCallBacks( onconnect=> \&connect, onauth=> \&authenticated, ondisconnected=> \&disconnect, ); $client->SetMessageCallBacks( chat => \&receive_chat, ); $status = $client->Execute( hostname=>$JABBER_HOST, port=>$JABBER_PORT, username=>$JABBER_USER, password=>$JABBER_PASS, tls=>$JABBER_TLS, componentname=>$JABBER_DOMAIN, resource=>$JABBER_RES, ); $client->Disconnect(); $mysql->disconnect(); # # procedure # $sent = 0; $authenticated = 0; sub connect { print "Connected!\n" if $DEBUGLEVEL; } sub authenticated { print "Authenticated!\n"; $authenticated = 1; $client->PresenceSend(); } sub disconnect { print "Disconnect!\n"; } sub receive_chat { my $sid = shift; my $msg = shift; my $from = $msg->GetFrom; my $data = $msg->GetBody; print "RAW: $data\n"; if ($data =~ /^direct from/) { $data =~ s/^direct from //; $data =~ s/\(reply\? send\: d.*$//; $uid = $data; $txt = $data; $uid =~ s/^([^\:]*)\:.*$/\1/; $txt =~ s/^[^\:]*\:\s*(.*)$/\1/; $txt =~ s/\s*$//; } else { $uid = $data; $txt = $data; $uid =~ s/^([^\:]*)\:.*$/\1/; $txt =~ s/^[^\:]*\:\s*(.*)$/\1/; $txt =~ s/\s*$//; if ($txt =~ /\@sws\s/) { $txt =~ s/\@sws\s+//; $data = $uid.": ".$txt; } else { return; } } $query = "INSERT INTO ".$MYSQL_PRE."liveshoutbox (time,name,text,url,ipaddr) VALUES ('".time()."',".$mysql->quote($uid).",".$mysql->quote($txt).",".$mysql->quote("http://twitter.com/".$uid).",".$mysql->quote($uid).")"; $mysql->query($query); send_message($data); } sub send_message { my $txt = shift; $client->MessageSend( to=>$TWITTER_ID, subject=>'', type=>'chat', body=>$txt, ); }