mattack ([info]paintitmatt) wrote,
@ 2008-07-19 14:55:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Perl and Pidgin's API
I'm trying to write a plugin for the Pidgin IM client in Perl that would take your latest post to http://identi.ca from an RSS feed and set it as your Pidgin status message. However the Perl API for Pidgin is poorly documented and I'm having trouble. I based a lot of it on the pidgintwitter-plugin.

I'm not sure what's going on exactly though I suspect I'm not passing the URL correctly to the the parsing subroutine.
Any help would be appreciated.


use Purple;
use XML::RSS;
use LWP::Simple;

%PLUGIN_INFO = (
    perl_api_version    => 2,
    name                => "Identica Status",
    version             => "0.0.1",
    summary             => "Use an identi.ca feed as your Pidgin status.",
    description         => "Use an identi.ca feed as your Pidgin status. Based on the pidgintwitter-status plugin (http://code.google.com/p/pidgin-twitterstatus/).",
    author              => "Mattack <paintitmatt\@gmail.com",> "http://identica.ca",
    load                => "plugin_load",
    unload              => "plugin_unload",
    prefs_info          => "prefs_info_cb",
);

sub fetch_url_cb {
    my $response = shift;                                                   # This section correctly 
    # parse the RSS feed                                                    # parses the URL and pulls
    my $rss = new XML::RSS;                                                 # the latest post if $response
    my $feed = get($response);                                              # is a correct URL. If you pull 
    Purple::Debug::info("Identica Status Feed", "Fetching $response\n");    # it out and put it in a 
    $rss->parse($feed);                                                     # seperate script, add something
    my $item = shift (@{$rss->{'items'}});                                  # like print $status_message; 
    my $status_message = "$item->{'title'}\n";                              # to the end... I'm not sure I'm
    # we don't want @replies                                                # passing the URL to the subroutine
    while ( $status_message =~ /^\@/) {                                     # correctly though...
        $item = shift (@{$rss->{'items'}});                                 # Pidgin's Perl API is poorly
        $status_message = "$item->{'title'}\n";                             # documented.
    }
    Purple::Debug::info("Identica Status Feed", "Status Message is ". $status_message ."\n");
	# set identi.ca status to pidgin status
    my $saved_status = Purple::SavedStatus::get_current();
    my $statusType = $saved_status->get_type();
    my $prev_status = $saved_status->get_message();
    Purple::Debug::info("Identica Status Feed","Your status type is ". $statusType . "\n");
    $saved_status->set_message($status_message);
    $saved_status->set_type($statusType);
    $saved_status->activate();
}

##	if(!($keepOnAway && $statusType == 6))
##	{
##		if(($prev_status ne $status_message) && (length($status_message) > 1) && (index($status_message, '@') != 0))
##		{
##			Purple::Debug::info("Identica Status Feed", "Updating status message with ".$status_message." From ".$identicaurl."\n");
#			$saved_status->set_message($status_message);
#			$saved_status->set_type($statusType);
#			$saved_status->activate();
##		}else{
##		      Purple::Debug::info("Identica Status Feed","Not updating status because it has not changed, was too short, or started with an @\n");
##		}
##	}else{
##		Purple::Debug::info("Identica Status Feed","Not updating status because you are away and like your message\n");
##	}

## print $status;

#}

sub timeout_cb {
	Purple::Debug::info("Identica Status Feed", "Starting the sequence.  Pidgin's timer expired.\n");
	my $plugin = shift;
	
	my $identicausername = Purple::Prefs::get_string("/plugins/core/gtk-mattack-identicastatus/identicausername");
	my $identicaurl = "http://identi.ca/api/statuses/user_timeline/".$identicausername.".rss";
	#if the timeout cannot be parsed, 0 will result
	my $timeout = 0+Purple::Prefs::get_string("/plugins/core/gtk-mattack-identicastatus/timeout");
	my $keepOnAway = Purple::Prefs::get_bool("/plugins/core/gtk-mattack-identicastatus/onaway");
	my $status_message = "";
	if($identicausername eq ""){
	 Purple::Debug::info("Identica Status Feed","Blank username\n");
	 die "blank username";
	}
	my $agent = "pidgin-identicastatusfeed/1.0";
	
	#give some timeout if not otherwise specified
	if($timeout eq 0)
	{
		Purple::Debug::info("identica Status Feed", "Could not parse timeout field. Using 120 seconds default.\n");
		$timeout = 120
	}
	
	Purple::Debug::info("identica Status Feed", "Fetching URL.\n");
	Purple::Util::fetch_url($plugin, $identicaurl, TRUE, $agent,TRUE, "fetch_url_cb");		
		
	# Reschedule timeout
	Purple::Debug::info("identica Status Feed", "Rescheduling timer.\n");
	Purple::timeout_add($plugin, $timeout, \&timeout_cb, $plugin);
	Purple::Debug::info("identica Status Feed", "New timer set for " . $timeout . " seconds.\n");
    
    return 0;
}

sub plugin_init {
    return %PLUGIN_INFO;
}

sub plugin_load {
    my $plugin = shift;
    Purple::Debug::info("Identica Status Feed", "plugin_load() - Identi.ca Status Loaded.\n");
    
    # Here we are adding a set of preferences
    #  The second argument is the default value for the preference.
    Purple::Prefs::add_none("/plugins/core/gtk-mattack-identicastatus");
    Purple::Prefs::add_string("/plugins/core/gtk-mattack-identicastatus/identicausername", "");
    Purple::Prefs::add_bool("/plugins/core/gtk-mattack-ideinticastatus/onidleonly", "");
    Purple::Prefs::add_bool("/plugins/core/gtk-mattack-identicastatus/onaway","");
	Purple::Prefs::add_string("/plugins/core/gtk-mattack-identicastatus/timeout", "120");
    
     # Schedule a timeout for 1 second from now
    Purple::timeout_add($plugin, 10, \&timeout_cb, $plugin);
}

sub plugin_unload {
    my $plugin = shift;
    Purple::Debug::info("Identica Status Feed", "plugin_unload() - Identi.ca Status Unloaded.\n");
}

sub prefs_info_cb {
    # The first step is to initialize the Purple::Pref::Frame that will be returned
    $frame = Purple::PluginPref::Frame->new();

    # Create a new boolean option with a label "Boolean Label" and then add
    # it to the frame
    $ppref = Purple::PluginPref->new_with_label("Identi.ca Account Information");
    $frame->add($ppref);

    # Create a text box.  The default value will be "Foobar" as set by
    # plugin_load
    $ppref = Purple::PluginPref->new_with_name_and_label(
        "/plugins/core/gtk-mattack-identicastatus/identicausername", "Identi.ca User Name");
    $ppref->set_type(2);
    $ppref->set_max_length(120);
    $frame->add($ppref);
	
	#Adding the timeout box
	$tpref = Purple::PluginPref->new_with_name_and_label("/plugins/core/gtk-mattack-identicastatus/timeout", "Timeout Period");
	#TODO: look to see if this type can be automatically set to be an integer so we don't need to parse it later
    $tpref->set_type(2);
    $tpref->set_max_length(3);
    $frame->add($tpref);

	#Update on Away
    $ppref = Purple::PluginPref->new_with_name_and_label(
    	   "/plugins/core/gtk-mattack-identicastatus/onaway", "Preserve Pidgin status when extended away");
    $ppref -> set_type(3);
    $frame->add($ppref);

    return $frame;
}


Advertisement


(Read 2 comments)

Post a comment in response:

From:
Help
Identity URL: 
Username:
Password:
Don't have an account? Create one now.
Subject:
No HTML allowed in subject
   Help
Message:

 
Notice! This user has turned on the option that logs your IP address when posting. Help
Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…