filemessages.pl irssi notification script

6. Juni 2009 Frank Grimm Comments off

I felt like sharing a perl script for irssi I wrote the other day. It’s just a quick’n'dirty RAD implementation but it serves it’s purpose.

What it does…

The script simply loads the contents of a textfile, splits it at un*x line-break (\n) and posts it to a predefined channel.

Afterwards the file is cleared in order to prevent sending it out again the next time the file is checked.

Please note that the script does absolutely nothing to cache new notifications that are written between reading the file and clearing it. I solely use this script to post new ticket / ticket responses on a low-frequency ticket system () so there was no vital need for implementing such a mechanism.

The code…

# Posts file content to a specified channel and clears file afterwards.
# Quick'n'Dirty implementation, could need support for Irssi::settings functions and optimization
use strict;
use Irssi;
 
use vars qw($VERSION %IRSSI);
 
$VERSION = '1.0';
%IRSSI = (
    authors     => 'Frank Grimm',
    contact     => 'frankgrimm at gmail.com',
    name        => 'filemessages',
    description => 'Notification from file.',
    license     => 'LGPL',
    url         => 'http://blog.frankgrimm.net',
    changed     => 'Sat Jun  6 14:55:01 CEST 2009',
);
 
# -- configuration
 
#the data source (make sure it is readable by the irssi user)
my $datafile = "/path/to/file";
#the script sends the file content to the following recipient
my $notify_target = "#targetchannel";
#check interval in seconds
my $interval = 60; 
 
sub update_filemessages {
	my (@servers) = Irssi::servers();
 
	#uncomment this line to check if the script is working correctly
	#Irssi::print('Starting update_filemessages');
 
	if ($servers[0] || $servers[0]->{connected}) {
		#connected to server
 
		#open file
		if (!open(FDATA, '<', $datafile)) {
			Irssi::print('filemessages.pl: Datafile not found. Creating.');
			if (!open(FDATA, '>', $datafile)) {
				Irrsi::print('filemessages.pl: Error. Datafile not created.');
				return 1;
			} else {
				Irssi::print('filemessages.pl: Datafile created.');
			}
			close FDATA;
		} else {
			#data file opened
 
			my @datafile_lines = ;
			my $current_line;
			foreach $current_line (@datafile_lines) {
				if ($current_line eq "\n") {
					next;
				} else {
					Irssi::print("filemessages.pl: $current_line");
				   $servers[0]->command("MSG $notify_target $current_line");
				}
			}
			close FDATA;
 
			#clear file by recreating it
			if (!open(FDATA, '>', $datafile)) {
				Irssi::print('filemessages.pl: Could not clear datafile.');
			}
		}
	} else {
		Irssi::print('filemessages.pl: Not connected to server');
	}
}
 
sub init_filemessages {
	#start update timer
	Irssi::timeout_add( $interval * 1000, \&update_filemessages, "filemessages");
}
 
# initialize
init_filemessages();
 
Irssi::print("filemessages.pl loaded...");

Use and modify it at your own joy & risk ;-)

Categories: perl, quickndirty Tags:

Testing the twitter tools plug…

1. Juni 2009 frank_grimm_net Comments off

Testing the twitter tools plugin for #wordpress

Categories: twitter Tags: