Skip to content

Instantly share code, notes, and snippets.

@starenka
Created July 31, 2011 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save starenka/1116731 to your computer and use it in GitHub Desktop.
Save starenka/1116731 to your computer and use it in GitHub Desktop.
Sets a random message on pidgin start (or plugin acivation). Please place your message file named randomator.txt into ~/.purple/plugins. Each line is a 'status message'
# randomator plugin for Pidgin / libpurple
# @author starenka moc.liamg@0aknerats (backwards) 2008
use Purple;
%PLUGIN_INFO = (
perl_api_version => 2,
name => "randomator",
version => "0.1",
summary => "Sets a random message on pidgin start.",
description => "Sets a random message on pidgin start (or plugin acivation). Please place your message
file named randomator.txt into ~/.purple/plugins. Each line is a 'status message'",
author => "starenka moc.liamg@0aknerats (backwards)",
url => "http://www.starenka.net",
load => "plugin_load",
unload => "plugin_unload"
);
sub plugin_init
{
return %PLUGIN_INFO;
}
sub plugin_load
{
my $plugin = shift;
Purple::Debug::info("randomator","plugin_load() - randomator loaded.\n");
Purple::Prefs::add_none("/plugins/custom");
Purple::Prefs::add_none("/plugins/custom/randomator");
Purple::Prefs::add_none("/plugins/custom/recent");
#set new status time interval [s]
Purple::timeout_add($plugin, 3600, \&set_message);
set_message();
}
sub plugin_unload
{
my $plugin = shift;
Purple::Debug::info("randomator","plugin_unload() - randomator unloaded.\n");
}
sub set_message
{
Purple::Debug::info("randomator", "plugin-load() -".$plugin."-\n");
Purple::Debug::info("randomator","loading cached messages\n");
$recent = Purple::Prefs::get_string("/plugins/custom/randomator/recent");
Purple::Debug::info("randomator","last lines used: $recent\n");
@recent = split(/,/,$recent);
#load the message file
$message_file = $ENV{ HOME }."/.purple/plugins/randomator.txt";
open(DAT,$message_file) || Purple::Notify::message($plugin,1,"randomator","Couldn't read the message file.",
"Please double check the path and permissions. Using: '".$message_file."'\n",NULL,NULL);
@data = <DAT>;
close(DAT);
#pick a random line
if((@data)>0)
{
$rand = int(rand(@data));
#do we have more lines that recent lenght?
if(@data > 10)
{
#don't use recent messages
while(grep $_ eq $rand, @recent)
{
Purple::Debug::info("randomator","the new message has been used before, new random line\n");
$rand = int(rand(@data));
}
}
#just don't use last used
else
{
#we don't wanna stuck in a loop, do we?
if(@data > 1)
{
while(grep $_ eq $rand, @recent[@recent-1])
{
Purple::Debug::info("randomator","the new message is same as the previous, new random line\n");
$rand = int(rand(@data));
}
}
}
#get the message
$new_message = substr($data[$rand],0,-1);
Purple::Debug::info("randomator","new line#: '".$rand."'\n");
Purple::Debug::info("randomator","new message: '".$new_message."'\n");
#save the new message to recent
if(@recent == 10)
{
Purple::Debug::info("randomator","cutting the oldest recent line\n");
shift(@recent);
}
push(@recent,$rand);
Purple::Prefs::set_string("/plugins/custom/randomator/recent",join(',',@recent));
Purple::Debug::info("randomator","saving to prefs: '".join(',',@recent)."'\n");
#set the new message
$status = Purple::SavedStatus::get_current();
$message = Purple::SavedStatus::get_message($status);
Purple::SavedStatus::set_message($status,$new_message);
Purple::SavedStatus::activate($status);
Purple::Debug::info("randomator","message set\n");
#return true to make the timer run again
return 1;
}
else { Purple::Debug::info("randomator","no data - using the old message\n"); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment