1# Paste script for irssi
2# (C) Simon Huggins 2002
3# huggie@earth.li
4
5# Reformat pasted text ready to paste onto channels.
6
7# This program is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by the Free
9# Software Foundation; either version 2 of the License, or (at your option)
10# any later version.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15# for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc., 59
19# Temple Place, Suite 330, Boston, MA 02111-1307  USA
20#
21
22use strict;
23
24use vars qw($VERSION %IRSSI);
25
26use Irssi 20020217.1542 (); # Version 0.8.1
27$VERSION = "0.5";
28%IRSSI = (
29authors     => "Simon Huggins",
30contact     => "huggie-irssi\@earth.li",
31name        => "Paste",
32description => "Paste reformats long pieces of text typically pasted into your client from webpages so that they fit nicely into your channel.  Width of client may be specified",
33license     => "GPLv2",
34url         => "http://the.earth.li/~huggie/irssi/",
35changed     => "Sat Mar  9 10:59:49 GMT 2002",
36);
37
38use Irssi::Irc;
39use Text::Wrap;
40use Text::Tabs;
41use POSIX qw(strftime);
42
43=pod
44
45=head1 paste.pl
46
47B<paste.pl> - a script for irssi to manage reformatting before pasting to channels
48
49To stop people pasting from webpages with very poor formatting this script
50allows you to reformat as you paste.
51
52=head1 USAGE
53
54Load the script then create a new unused window and paste your text into it.
55The defaults should be reasonable so then B</paste> will paste it in the
56current window (either by saying it to the current channel, msging it to the
57current recipient in a query window or by printing it up as client messages
58in a blank window).
59
60Try altering I<paste_width> (0 is the autoformatted default based on your
61nick length) to affect the width of the pasted text.
62
63Alter I<paste_prefix> to change the prefix added to each line (B</set -clear
64paste_prefix> to remove it altogether).
65
66Set I<paste_showbuf> to show the lines pasted into the buffer as you paste it.
67
68=head1 AUTHOR
69
70Send suggestions to Simon Huggins <huggie@earth.li>
71
72=cut
73
74my $pastewin;
75
76BEGIN {
77	$pastewin = Irssi::window_find_name('paste');
78	if (!$pastewin) {
79		Irssi::command("window new hide");
80		Irssi::command("window name paste");
81		$pastewin = Irssi::window_find_name('paste');
82	}
83	$pastewin->print(">>> Paste your buffer here to start <<<");
84}
85
86Irssi::settings_add_bool("paste","paste_showbuf",0);
87Irssi::settings_add_int("paste","paste_width",0);
88Irssi::settings_add_str("paste","paste_prefix",">> ");
89
90{
91	my @buffer;
92	my $buf=0;
93	my $last_ts;
94
95sub event_send_text {
96	my ($line, $server, $windowitem) = @_;
97
98	return if $windowitem;
99
100	if ($last_ts < (time() - 60)) {
101		@buffer=();
102		$buf= 0;
103		$pastewin->print("Buffer cleared!");
104	}
105	$line =~ s/^\s+/ /;
106	$line =~ s/\s+$/ /;
107	$buffer[$buf] .= $line." ";
108
109	if (!$line and $buffer[$buf] ne "") {
110		$buf++;
111	}
112
113	if (Irssi::settings_get_bool("paste_showbuf")) {
114		$pastewin->print($line,MSGLEVEL_CLIENTCRAP);
115	}
116	$last_ts = time();
117
118	Irssi::signal_stop();
119}
120
121sub paste {
122	my ($data, $server, $witem) = @_;
123
124	my $offset;
125
126	if (!$buf and $buffer[0] eq "") {
127		$pastewin->print("No buffer to paste!",MSGLEVEL_HILIGHT);
128		return;
129	}
130
131	my $anyoldwin = Irssi::active_win();
132	my $width = Irssi::settings_get_int("paste_width");
133	my $prefix = Irssi::settings_get_str("paste_prefix");
134	my $prefixlen = length($prefix);
135	if ($width > 0) {
136		if ($width < 3+$prefixlen) {
137			$pastewin->print("paste_width is too small ($width<".
138					(3+$prefixlen).")!",
139					MSGLEVEL_HILIGHT);
140			return;
141		}
142		$Text::Wrap::columns = $width;
143	} else {
144		if ($server->{nick}) {
145			$offset+=length($server->{nick})+$prefixlen+15;
146		}
147		$Text::Wrap::columns = $anyoldwin->{'width'} - $offset;
148		if ($Text::Wrap::columns < 3+$prefixlen) {
149			$pastewin->print("Width would be too small (".
150					$Text::Wrap::columns."<".
151					(3+$prefixlen).", window width was ".
152					$anyoldwin->{'width'}.
153					")!",
154					MSGLEVEL_HILIGHT);
155			return;
156		}
157	}
158
159	foreach my $outbuffer (@buffer) {
160		$outbuffer =~ s/^\s*//;
161		$outbuffer =~ s/\s*$//;
162		$outbuffer = wrap("","", $outbuffer);
163		$outbuffer = expand($outbuffer);
164
165		if ($witem) {
166			foreach (split '\n', $outbuffer) {
167				$witem->command("say ".$prefix.$_);
168			}
169		} else {
170			foreach (split '\n', $outbuffer) {
171				$anyoldwin->print($prefix.$_, MSGLEVEL_HILIGHT);
172			}
173		}
174	}
175}
176
177sub clear_buffer {
178	@buffer = ();
179	$buf = 0;
180	$pastewin->print("Buffer cleared!");
181}
182
183}
184
185Irssi::signal_add_first("send text", "event_send_text");
186Irssi::command_bind("paste", "paste");
187Irssi::command_bind("clear_buffer", "clear_buffer");
188