1use strict;
2use vars qw($VERSION %IRSSI);
3
4my $help = <<EOF;
5Usage: (all on one line)
6/file [-raw] [-command]
7      [-msg [target]] [-notice [target]]
8      [-prefix "text"] [-postfix "text"]
9      filename
10
11-raw: output contents of file as raw irc data
12-command: run contents of file as irssi commands
13-msg: send as messages to active window (default) or target
14-notice: send as notices to active window or target
15
16-prefix: add "text" in front of output
17-postfix: add "text" after output
18
19-echo abuses a bug in the script and is useful for testing
20EOF
21
22$VERSION = 1.0;
23%IRSSI = (
24   authors     => "David Leadbeater",
25   name        => "file.pl",
26   description => "A command to output content of files in various ways",
27   license     => "GNU GPLv2 or later",
28   url         => "http://irssi.dgl.cx/"
29);
30
31Irssi::command_bind('file', sub {
32   my $data = shift;
33
34   if($data eq 'help') {
35      print $help;
36      return;
37   }
38
39   my($type, $target, $prefix, $postfix);
40
41   $type    = 'msg';
42   $target  = '*';
43   $prefix  = '';
44   $postfix = '';
45
46   while($data =~ s/^-([^ ]+) //g) {
47      last if $data eq '-';
48
49      if($1 eq 'msg' || $1 eq 'notice') {
50         $type = $1;
51         next unless $data =~ / /; # >1 params left
52         $data =~ s/^([^ ]+) //;
53         next unless $1;
54         $target = $1;
55      }elsif($1 eq 'prefix') {
56         $data =~ s/^(?:\"([^"]+)\"|([^ ]+)) //;
57         $prefix = $1 || $2 . ' ';
58      }elsif($1 eq 'postfix') {
59         $data =~ s/^(?:\"([^"]+)\"|([^ ]+)) //;
60         $postfix = ' ' . $1 || $2;
61      }else{ # Other options are automatic
62         $type = $1;
63      }
64   }
65
66   # or do borrowed from one of juerd's scripts (needs 5.6 though)
67   open(FILE, "<", $data) or do {
68      print "Error opening '$data': $!";
69      return;
70   };
71
72   while(<FILE>) {
73      chomp;
74
75      if($type eq 'raw') {
76         Irssi::active_server->send_raw($prefix . $_ . $postfix);
77      }elsif($type eq 'command') {
78         Irssi::active_win->command($prefix . $_ . $postfix);
79      }else{
80         Irssi::active_win->command("$type $target $prefix$_$postfix");
81      }
82   }
83
84   close FILE;
85
86} );
87
88# little known way to get -options to tab complete :)
89Irssi::command_set_options('file','raw command prefix postfix msg notice');
90
91