#!/usr/bin/perl

# This script allows you to message the contents of the
# X clipboard to the currently viewed channel in irssi.
# Do this with the command "pst" (i.e., type /pst RET
# when in a channel). It uses the CLI tool xclip and
# requires an X server running. (Another tool to do
# this is xsel which is possibly better, though by the
# time I discovered xsel I had already got this script
# working with xclip.)
#
# To install, put the below code in ~/.irssi/scripts,
# then, in ~/.irssi/scripts/autorun, run 'ln -s
# ../pst.pl' - this way, the script will be loaded
# automatically on starting irssi. (If you like to try
# it manually, the irssi command is:
# /script load pst RET

use strict;
use warnings;
use Irssi;

sub cmd_pst {
    my $home = $ENV{'HOME'};
    my $clip_file_path = "$home/.irssi/clip";
    system("touch $clip_file_path");
    system("xclip -d ':0' -o -selection clipboard > $clip_file_path");
    open(FILE, $clip_file_path) or die "Cannot open file.\n";
    my $clip = <FILE>;
    close (FILE);
    my $server = Irssi::active_server();
    my $channel = Irssi::active_win()->{active}->{name};
    $server->command("msg $channel $clip");
}

Irssi::command_bind('pst', 'cmd_pst');
