1use strict;
2use vars qw($VERSION %IRSSI);
3use Irssi::TextUI;
4
5$VERSION = "0.2";
6%IRSSI = (
7    authors     => 'Doncho N. Gunchev',
8    contact     => 'mr_700@yahoo.com',
9    name        => 'UNIBG-autoident',
10    description => 'Automaticaly /msg ident NS yourpassword when you connect or services come back from death',
11    license     => 'Public Domain',
12    url         => 'http://not.available.yet/',
13    changed	=> '2018-09-10'
14);
15
16# UNIBG NS auto identifyer
17# for irssi 0.8.1 by Doncho N. Gunchev
18#
19# Check /id help for help.
20
21
22my $msghead='autoident:';
23# list of nicks/passwords
24my %passwords = ();
25my $numpasswords = 0;
26my $passwordspassword = '';
27my $nsnick='NS';
28my $nshost='NickServ@UniBG.services';
29my $nsreq='This nickname is registered and protected';
30my $nsok  ='Password accepted - you are now recognized';
31my $nscmd ='identify';
32# 2DO
33#	0. Do it!
34#	1. Make it take nick, passwor and network as parameters
35#	2. Make NS, CS, MS and maybe OS support
36#	3. Add eggdrop support
37#       4. Add encrypted passwords in config file...
38#	5. Add Global support or maybe /notify NS or bouth?
39#	6. Don't autoident 2 times in less than xxx seconds
40#	7. Change nick if we don't have the password / ask user for it
41#	   in xxx seconds before changing nicks
42#	8. Add /id newpass,setpass,permpas,ghost,kill....
43#	9. Add /id chanadd chandel chan....
44#
45
46sub cmd_print_help {
47  Irssi::print(<<EOF, MSGLEVEL_CRAP);
48$msghead
49          WELL... as I'm starting to write this - no help!
50          /id add nick password - add new nick with password to autoident
51          /id del nick          - delete nick from autoident list
52          /id list              - show nicks in autoident list
53          /id show              - same as /id list
54          /id help              - this one
55EOF
56#          /id check             - see if current nick is in autoident list
57}
58
59
60sub msg {
61  my ($msg, $lvl) = @_;
62  Irssi::print("$msghead $msg", $lvl);
63}
64
65
66sub event_notice {
67  # $server = server record where the message came
68  # $data = the raw data received from server, with NOTICEs it is:
69  #         "target :text" where target is either your nick or #channel
70  # $nick = the nick who sent the message
71  # $host = host of the nick who sent the message
72  my ($server, $data, $nick, $host) = @_;
73  #04:06 -!- autoident(debug): server= Irssi::Irc::Server=HASH(0x86786cc)
74  #04:06 -!- autoident(debug): data  = Mr_700 :This nickname is owned by someone else
75  #04:06 -!- autoident(debug): nick  = NS
76  #04:06 -!- autoident(debug): host  = NickServ@UniBG.services
77  #04:06 -!- autoident(debug): target = Mr_700
78  #04:06 -!- autoident(debug): text   = This nickname is owned by someone else
79
80  # split data to target/text
81  my ($target, $text) = $data =~ /^(\S*)\s:(.*)/;
82
83  # check the sent text
84  return if ($text !~ /$nsreq/) && ($text !~ /$nsok/);
85
86  # check the sender's nick
87  return if ($nick !~ /$nsnick/);
88
89  # check the sender's host
90  if ($host !~ /$nshost/) {
91    msg("!!! '$nsnick' host is bad, hack attempt? !!!", MSGLEVEL_CRAP);
92    msg("!!!", MSGLEVEL_CRAP);
93    msg("!!!  sender: '$nick!$host'", MSGLEVEL_CRAP);
94    msg("!!!  target: '$target'", MSGLEVEL_CRAP);
95    msg("!!!  text  : '$text'", MSGLEVEL_CRAP);
96    msg("!!!", MSGLEVEL_CRAP);
97    msg("!!! '$nsnick' host is bad, hack attempt? !!!", MSGLEVEL_CRAP);
98    return;
99  }
100
101  # check if sent to us directly
102  return if ($target !~ /$server->{nick}/);
103
104  if ($text =~ /$nsreq/) {
105    if (exists($passwords{$server->{nick}})) {
106      msg("'$nsnick!$nshost' requested identity, sending...", MSGLEVEL_CRAP);
107      $server->command("MSG $nsnick $nscmd " . $passwords{$server->{nick}});
108    } else {
109      msg("'$nsnick!$nshost' says '$nsreq' and we have no password set for it!", MSGLEVEL_CRAP);
110      msg("          use /id add " . $server->{nick} . " <password> to set it!", MSGLEVEL_CRAP);
111      msg("          ... autoident has left you in /dev/random", MSGLEVEL_CRAP);
112    }
113  } else {
114    msg("'$nsnick!$nshost' accepted identity", MSGLEVEL_CRAP);
115  }
116}
117
118
119sub addpassword {
120  my ($name, $password) = @_;
121
122  if (exists($passwords{$name})) {
123    if ($password eq $passwords{$name}) {
124      msg("Nick $name already has this password for autoident", MSGLEVEL_CRAP);
125    } else {
126      msg("Nick $name's autoident password changed", MSGLEVEL_CRAP);
127      $passwords{$name} = $password;
128    }
129  } else {
130    $passwords{$name} = $password;
131    $numpasswords++;
132    msg("Nick $name added to autoident list ($numpasswords total)", MSGLEVEL_CRAP);
133  }
134}
135
136sub delpassword {
137  my $name = $_[0];
138
139  if (exists($passwords{$name})) {
140    delete($passwords{$name});
141    $numpasswords--;
142    msg("Nick $name removed from autoidentify list ($numpasswords left)", MSGLEVEL_CRAP);
143  } else {
144    msg("Nick $name is not in autoident list", MSGLEVEL_CRAP);
145  }
146}
147
148sub init_passwords {
149  # Add the passwords at startup of the script
150  my $passwordsstring = Irssi::settings_get_str('autoident');
151  if (length($passwordsstring) > 0) {
152    my @passwords = split(/,/, $passwordsstring);
153
154    foreach my $i (@passwords) {
155      my $name = substr($i, 0, index($i, '='));
156      my $password = substr($i, index($i, '=') + 1, length($i));
157      addpassword($name, $password);
158    }
159  }
160}
161
162
163sub read_settings {
164#  my $passwords = Irssi::settings_get_str('passwords');
165}
166
167
168sub update_settings_string {
169  my $setting;
170
171  foreach my $name (keys(%passwords)) {
172    $setting .= $name . "=" . $passwords{$name} . ",";
173  }
174
175  Irssi::settings_set_str("autoident", $setting);
176}
177
178
179sub cmd_addpassword {
180  my ($name, $password) = split(/ +/, $_[0]);
181
182  if ($name eq "" || $password eq "") {
183    msg("Use /id add <name> <password> to add new nick to autoident list", MSGLEVEL_CRAP);
184    return;
185  }
186  addpassword($name, $password);
187  update_settings_string();
188}
189
190sub cmd_delpassword {
191  my $name = $_[0];
192
193  if ($name eq "") {
194    msg("Use /id del <name> to delete a nick from autoident list", MSGLEVEL_CRAP);
195    return;
196  }
197
198  delpassword($name);
199  update_settings_string();
200}
201
202sub cmd_showpasswords {
203  if ($numpasswords == 0) {
204    msg("No nicks defined for autoident", MSGLEVEL_CRAP);
205    return;
206  }
207  msg("Nicks for autoident:", MSGLEVEL_CRAP);
208  my $n = 1;
209  foreach my $nick (keys(%passwords)) {
210#    msg("$nick: " . $mailboxes{$password}, MSGLEVEL_CRAP);
211    msg("$n. $nick: ***", MSGLEVEL_CRAP);
212    $n++;
213  }
214}
215
216sub cmd_id {
217  my ($data, $server, $item) = @_;
218  if ($data =~ m/^[(show)|(add)|(del)|(help)]/i ) {
219    Irssi::command_runsub('id', $data, $server, $item);
220  } else {
221    msg("Use /id (show|add|del|help)", MSGLEVEL_CRAP);
222  }
223}
224
225Irssi::command_bind('id show', 'cmd_showpasswords');
226Irssi::command_bind('id list', 'cmd_showpasswords');
227Irssi::command_bind('id add', 'cmd_addpassword');
228Irssi::command_bind('id del', 'cmd_delpassword');
229Irssi::command_bind('id help', 'cmd_print_help');
230Irssi::command_bind('id', 'cmd_id');
231Irssi::settings_add_str('misc', 'autoident', '');
232
233read_settings();
234init_passwords();
235Irssi::signal_add('setup changed', 'read_settings');
236
237#Irssi::signal_add('event privmsg', 'event_privmsg');
238Irssi::signal_add('event notice', 'event_notice');
239
240msg("loaded ok", MSGLEVEL_CRAP);
241
242# EOF
243