-
Notifications
You must be signed in to change notification settings - Fork 2
/
fish.pl
252 lines (239 loc) · 7.45 KB
/
fish.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'closure';
use Irssi;
use Irssi::Irc;
#use Encode;
use IO::File;
use Crypt::ircBlowfish;
#### TODO:
## Slash commands to make adding/deleting keys easier
## Method of typing in plain text. Maybe +p by default, like FiSH.
## More stuff? who knows!
my $blowfish = Crypt::ircBlowfish->new;
my $keyfile = Irssi::get_irssi_dir.'/fish.keys';
my %keys;
# load our fish keys
sub load_keys {
my $fh = IO::File->new($keyfile, 'r');
if (!$fh) {
Irssi::print "Couldn't open $keyfile for reading.";
return;
}
# Clear the hash, incase we're reloading keys.
%keys = ();
my $count = 0;
while (<$fh>) {
chomp;
s/^\s+//;
s/\s+^//;
next unless length;
my ($net, $chan, $key) = split /:/, $_;
$keys{$net}{$chan} = $key;
$count += 1;
}
undef $fh;
Irssi::print "Loaded keys for $count channels";
return 0;
}
# save keys
sub save_keys {
#my $fh = IO::file->new($keyfile, 'w');
#if (!$fh) {
# Irssi::print "Couldn't open $keyfile for writing.";
# return;
#}
foreach my $net (sort keys %keys) {
foreach my $chan (sort keys %{$keys{$net}}) {
my $key = $keys{$net}{$chan};
Irssi::print "$net:$chan:$key";
}
}
}
## Our encrypt/decrypt functions
sub msg_decrypt {
my ($tag, $chan, $text, $key) = @_;
if (defined $key and index($text, '+OK ') == 0) {
$blowfish->set_key($key);
my $text = ( split(/\+OK /, $text) )[1];
# Check that text actually exists, it's possible that someone might just send '+OK ' and no text.
if ($text) {
return $blowfish->decrypt($text);
}
else {
# If there was no text, return an empty line.
return "";
}
}
else {
# Check if we want to mark unencrypted text or not.
if (Irssi::settings_get_bool('mark_unencrypted')) {
my $mark = Irssi::settings_get_str('mark_string');
return $mark . " " . $text;
}
else {
return $text;
}
}
}
sub msg_encrypt {
my ($tag, $chan, $text, $key) = @_;
# If we've asked for plain text, just return the text right away.
# minus the +p
#if (index($text, $plainprefix) == 0) {
# $text =~ s/^$plainprefix//;
# return $text;
#}
# otherwise, back to normal checking.
if (defined $key) {
$blowfish->set_key($key);
my $eText = "+OK ". $blowfish->encrypt($text);
return $eText;
}
else {
return $text;
}
}
## OK, here we'll process the message and decide if it needs decrypting.
# Decrypt other peoples public messages
sub message_public {
my ($server_rec, $msg, $nick, $addr, $channel) = @_;
my $tag = $server_rec->{tag};
## If the key for the tag and channel exists, run through the decryption sub.
my $key = $keys{$tag}{$channel};
if (defined $key) {
# Decrypt the text
my $text = msg_decrypt($tag, $channel, $msg, $key);
#$text = Encode::encode("utf8", $text);
# Continue with the 'message public' signal, albeit a little modified :)
Irssi::signal_continue(($server_rec, $text, $nick, $addr, $channel));
}
}
sub event_privmsg {
my ($server_rec, $args, $nick, $addr) = @_;
Irssi::print "SVR: $server_rec / ARG: $args / NICK: $nick / ADDR: $addr";
my ($target, $msg) = split /\s/, $args, 2;
# if the target is a channel, we can see if we want to decrypt it.
if ($server_rec->ischannel($target)) {
Irssi::print "Target '$target' is a channel. MSG: $msg";
my $tag = $server_rec->{tag};
my $key = $keys{$tag}{$target};
if (defined $key) {
$msg =~ s/^://; # remove the :, we'll add it later.
# decrypt the text
my $dtext = msg_decrypt($tag, $target, $msg, $key);
# reconstruct the args variable
my $finalargs = $target . " :" . $dtext;
# throw it back into the signal, hopefully to be recoded.
Irssi::signal_continue(($server_rec, $finalargs, $nick, $addr));
}
}
}
## Encrypt what we're sending out (hopefully).
sub send_text {
my ($msg, $server_rec, $witem) = @_;
# only do this on witem channel
# we must also make sure that a message exists. 'send text' will trigger if you just press enter on the prompt with nothing there.
if ($msg and $witem != 0 and $witem->{type} eq "CHANNEL") {
my $tag = $server_rec->{tag};
my $channel = $witem->{name};
my $key = $keys{$tag}{$channel};
if (defined $key) {
# Encrypt the text
my $text = msg_encrypt($tag, $channel, $msg, $key);
# Continue with the signal.
Irssi::signal_continue(($text, $server_rec, $witem));
}
}
}
# decrypt our own public messages, fucking madness, i tell thee.
# This is needed since we encrypted our out going text in 'send_text' and irssi is going to display that string.
sub message_own_public {
my ($server_rec, $msg, $channel) = @_;
my $tag = $server_rec->{tag};
my $key = $keys{$tag}{$channel};
if (defined $key) {
my $text = msg_decrypt($tag, $channel, $msg, $key);
Irssi::signal_continue(($server_rec, $text, $channel));
}
}
# Handle topic changes
sub message_topic {
my ($server_rec, $channel, $topic, $nick, $addr) = @_;
my $tag = $server_rec->{tag};
my $key = $keys{$tag}{$channel};
if (defined $key) {
my $newtopic = msg_decrypt($tag, $channel, $topic, $key);
Irssi::signal_continue(($server_rec, $channel, $newtopic, $nick, $addr));
}
}
# the topic bar stuff
sub refresh_topic {
my ($chanrec) =@_;
my $current_win = Irssi::active_win()->{active}->{name};
if (defined $current_win and $current_win eq $chanrec->{name}) {
#Irssi::statusbar_items_redraw('fishtopicbar');
}
}
sub get_topic {
my $type = Irssi::active_win()->{active}->{type};
if ($type eq "CHANNEL") {
my $channel = Irssi::active_win()->{active}->{name};
my $tag = Irssi::active_win()->{active}->{server}->{tag};
my $topic = Irssi::active_win()->{active}->{topic};
my $key = $keys{$tag}{$channel};
if (defined $key) {
my $newtopic = msg_decrypt($tag, $channel, $topic, $key);
return $newtopic;
}
else {
return $topic;
}
}
else {
return "";
}
}
sub fishtopic_sb {
my ($item, $get_size_only) = @_;
my $text = get_topic();
$item->default_handler($get_size_only, "{topic ".$text."}", undef, 1);
}
# load/unload things for creating/destroying our topic bar item
sub script_unload {
my ($script, $server_rec, $witem) = @_;
if ($script =~ /(.*\/)?fish(\.pl)?$/) {
Irssi::command("STATUSBAR topic REMOVE fishtopic");
Irssi::command("STATUSBAR topic ADD -after topicbarstart -priority 0 -alignment left topic");
}
}
sub script_load {
my ($script, $server_rec, $witem) = @_;
if ($script =~ /(.*\/)?fish(\.pl)?$/) {
Irssi::command("STATUSBAR topic REMOVE topic");
Irssi::command("STATUSBAR topic ADD -after topicbarstart -priority 0 -alignment left fishtopic");
}
}
# Load our keys
load_keys();
# statusbar item and hooks for the encrypted topics
#Irssi::statusbar_item_register('fishtopic', undef, 'fishtopic_sb');
#Irssi::statusbar_recreate_items();
#Irssi::signal_add_first('command script load', 'script_load');
#Irssi::signal_add_first('command script unload', 'script_unload');
# Some settings
Irssi::settings_add_bool('fish', 'mark_unencrypted' => 1);
Irssi::settings_add_str('fish', 'mark_string', '[u]');
Irssi::settings_add_str('fish', 'plain_prefix', '+p');
# Hook the signals and start some work.
#Irssi::signal_add_first('event privmsg', 'event_privmsg');
#Irssi::signal_add_first('channel topic changed', 'refresh_topic');
Irssi::signal_add_first('message public', 'message_public');
Irssi::signal_add_first('message own_public', 'message_own_public');
#Irssi::signal_add_first('message topic', 'message_topic');
Irssi::signal_add_first('send text', 'send_text');
#Irssi::signal_add_first('server incoming', 'server_incoming');
# A few commands.
Irssi::command_bind('keysave', 'save_keys');
Irssi::command_bind('keyload', 'load_keys');