Created
May 27, 2011 09:37
-
-
Save stt/994943 to your computer and use it in GitHub Desktop.
Chatbot plugin for Pidgin instant messaging client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Chatbot integration plugin for Pidgin | |
# (c)2011, <samuli@tuomola.net> | |
# | |
# INSTALL: | |
# 1. Get Net::AIML, e.g. sudo cpan App::cpanminus; sudo cpanm Net::AIML | |
# 2. Copy the script in your ~/.purple/plugins/ directory | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA | |
use Purple; | |
use Net::AIML; | |
use HTML::Entities; | |
%PLUGIN_INFO = ( | |
perl_api_version => 2, | |
name => "Chatbot integration", | |
version => "0.1", | |
summary => "Chatbot integration using pandorabots.com API", | |
description => "Chatbot integration using pandorabots.com API", | |
author => '<samuli@tuomola.net>', | |
url => "https://gist.github.com/994943", | |
load => "plugin_load", | |
unload => "plugin_unload", | |
prefs_info => "prefs_info_cb" | |
); | |
# select one from http://www.pandorabots.com/botmaster/en/mostactive | |
$bot = Net::AIML->new( botid => da4d216c0e345aa1 ); | |
%lastmsg = (); | |
sub plugin_init { | |
return %PLUGIN_INFO; | |
} | |
sub plugin_load { | |
$plugin = shift; | |
Purple::Debug::info("chatbot", "->plugin_load()\n"); | |
Purple::Prefs::add_none("/plugins/core/chatbot"); | |
Purple::Prefs::add_string("/plugins/core/chatbot/account", "None"); | |
Purple::Prefs::add_string("/plugins/core/chatbot/buddy", "None"); | |
$convs_handle = Purple::Conversations::get_handle(); | |
Purple::Signal::connect($convs_handle, "receiving-im-msg", $plugin, | |
\&receiving_im_msg_cb, "yyy"); | |
Purple::Debug::info("chatbot", "<-plugin_load()\n"); | |
} | |
sub plugin_unload { | |
my $plugin = shift; | |
Purple::Debug::info("chatbot", "plugin_unload()\n"); | |
} | |
sub receiving_im_msg_cb { | |
my ($account, $who, $msg, $conv, $flags) = @_; | |
$msg =~ s/<[^>]*>//g; | |
$msg = decode_entities($msg); | |
Purple::Debug::info("chatbot", "received from ". $who .": ". $msg ."\n"); | |
$chatbotaccount = Purple::Prefs::get_string("/plugins/core/chatbot/account"); | |
$chatbotbud = Purple::Prefs::get_string("/plugins/core/chatbot/buddy"); | |
if ($account->get_username() eq $chatbotaccount) { | |
if($lastmsg{$who} > time()-5) { | |
Purple::Debug::info("chatbot", "loop prevention.. now ".time()." last msg from ".$who." at ".$lastmsg{$who}."\n"); | |
} elsif ($chatbotbud ne "All" && $chatbotbud ne $who) { | |
Purple::Debug::info("chatbot", "Buddy setting: $chatbotbud doesn't match: $who\n"); | |
} else { | |
$response = $bot->tell($msg); | |
$conv->get_im_data()->send($response); | |
#$bot=0; | |
$lastmsg{$who} = time(); | |
} | |
} | |
$_[2]; | |
} | |
sub prefs_info_cb { | |
$frame = Purple::PluginPref::Frame->new(); | |
# account selection | |
@account_array = Purple::Accounts::get_all(); | |
$acpref = Purple::PluginPref->new_with_name_and_label("/plugins/core/chatbot/account", "Account: "); | |
$acpref->set_type(Purple::PluginPref::Type::CHOICE); | |
@acnames = []; | |
$selaccname = Purple::Prefs::get_string("/plugins/core/chatbot/account"); | |
foreach $acc (@account_array) { | |
$acnames[++$#acnames] = $acc->get_username(); | |
if($selaccname eq $acc->get_username()) { | |
$selacc = $acc; | |
} | |
} | |
foreach $acc (@acnames) { | |
$acpref->add_choice($acc, $acc); | |
} | |
$acpref->add_choice("None", "None"); | |
$frame->add($acpref); | |
# buddy selection | |
@buddy_array = Purple::Find::buddies($selacc, ""); | |
Purple::Debug::info("chatbot", $buddy_array."\n"); | |
$budpref = Purple::PluginPref->new_with_name_and_label("/plugins/core/chatbot/buddy", "Buddy: "); | |
$budpref->set_type(Purple::PluginPref::Type::CHOICE); | |
if (@buddy_array) { | |
@budnames = []; | |
foreach $bud (@buddy_array) { | |
$budnames[++$#budnames] = Purple::BuddyList::Buddy::get_name($bud); | |
} | |
foreach $bud (@budnames) { | |
$budpref->add_choice($bud, $bud); | |
} | |
} | |
$budpref->add_choice("All", "All"); | |
$budpref->add_choice("None", "None"); | |
$frame->add($budpref); | |
return $frame; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment