summaryrefslogtreecommitdiffstats
path: root/scripts/UNIBG-autoident.pl
blob: c8bee322a3e80aa7da90ec0626c24ca9b721c12a (plain)
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
use strict;
use vars qw($VERSION %IRSSI);
use Irssi::TextUI;

$VERSION = "0.2";
%IRSSI = (
    authors     => 'Doncho N. Gunchev',
    contact     => 'mr_700@yahoo.com',
    name        => 'UNIBG-autoident',
    description => 'Automaticaly /msg ident NS yourpassword when you connect or services come back from death',
    license     => 'Public Domain',
    url         => 'http://not.available.yet/',
    changed	=> '2018-09-10'
);

# UNIBG NS auto identifyer
# for irssi 0.8.1 by Doncho N. Gunchev
#
# Check /id help for help.


my $msghead='autoident:';
# list of nicks/passwords
my %passwords = ();
my $numpasswords = 0;
my $passwordspassword = '';
my $nsnick='NS';
my $nshost='NickServ@UniBG.services';
my $nsreq='This nickname is registered and protected';
my $nsok  ='Password accepted - you are now recognized';
my $nscmd ='identify';
# 2DO
#	0. Do it!
#	1. Make it take nick, passwor and network as parameters
#	2. Make NS, CS, MS and maybe OS support
#	3. Add eggdrop support
#       4. Add encrypted passwords in config file...
#	5. Add Global support or maybe /notify NS or bouth?
#	6. Don't autoident 2 times in less than xxx seconds
#	7. Change nick if we don't have the password / ask user for it
#	   in xxx seconds before changing nicks
#	8. Add /id newpass,setpass,permpas,ghost,kill....
#	9. Add /id chanadd chandel chan....
#

sub cmd_print_help {
  Irssi::print(<<EOF, MSGLEVEL_CRAP);
$msghead
          WELL... as I'm starting to write this - no help!
          /id add nick password - add new nick with password to autoident
          /id del nick          - delete nick from autoident list
          /id list              - show nicks in autoident list
          /id show              - same as /id list
          /id help              - this one
EOF
#          /id check             - see if current nick is in autoident list
}


sub msg {
  my ($msg, $lvl) = @_;
  Irssi::print("$msghead $msg", $lvl);
}


sub event_notice {
  # $server = server record where the message came
  # $data = the raw data received from server, with NOTICEs it is:
  #         "target :text" where target is either your nick or #channel
  # $nick = the nick who sent the message
  # $host = host of the nick who sent the message
  my ($server, $data, $nick, $host) = @_;
  #04:06 -!- autoident(debug): server= Irssi::Irc::Server=HASH(0x86786cc)
  #04:06 -!- autoident(debug): data  = Mr_700 :This nickname is owned by someone else
  #04:06 -!- autoident(debug): nick  = NS
  #04:06 -!- autoident(debug): host  = NickServ@UniBG.services
  #04:06 -!- autoident(debug): target = Mr_700
  #04:06 -!- autoident(debug): text   = This nickname is owned by someone else

  # split data to target/text
  my ($target, $text) = $data =~ /^(\S*)\s:(.*)/;

  # check the sent text
  return if ($text !~ /$nsreq/) && ($text !~ /$nsok/);

  # check the sender's nick
  return if ($nick !~ /$nsnick/);

  # check the sender's host
  if ($host !~ /$nshost/) {
    msg("!!! '$nsnick' host is bad, hack attempt? !!!", MSGLEVEL_CRAP);
    msg("!!!", MSGLEVEL_CRAP);
    msg("!!!  sender: '$nick!$host'", MSGLEVEL_CRAP);
    msg("!!!  target: '$target'", MSGLEVEL_CRAP);
    msg("!!!  text  : '$text'", MSGLEVEL_CRAP);
    msg("!!!", MSGLEVEL_CRAP);
    msg("!!! '$nsnick' host is bad, hack attempt? !!!", MSGLEVEL_CRAP);
    return;
  }

  # check if sent to us directly
  return if ($target !~ /$server->{nick}/);

  if ($text =~ /$nsreq/) {
    if (exists($passwords{$server->{nick}})) {
      msg("'$nsnick!$nshost' requested identity, sending...", MSGLEVEL_CRAP);
      $server->command("MSG $nsnick $nscmd " . $passwords{$server->{nick}});
    } else {
      msg("'$nsnick!$nshost' says '$nsreq' and we have no password set for it!", MSGLEVEL_CRAP);
      msg("          use /id add " . $server->{nick} . " <password> to set it!", MSGLEVEL_CRAP);
      msg("          ... autoident has left you in /dev/random", MSGLEVEL_CRAP);
    }
  } else {
    msg("'$nsnick!$nshost' accepted identity", MSGLEVEL_CRAP);
  }
}


sub addpassword {
  my ($name, $password) = @_;

  if (exists($passwords{$name})) {
    if ($password eq $passwords{$name}) {
      msg("Nick $name already has this password for autoident", MSGLEVEL_CRAP);
    } else {
      msg("Nick $name's autoident password changed", MSGLEVEL_CRAP);
      $passwords{$name} = $password;
    }
  } else {
    $passwords{$name} = $password;
    $numpasswords++;
    msg("Nick $name added to autoident list ($numpasswords total)", MSGLEVEL_CRAP);
  }
}

sub delpassword {
  my $name = $_[0];

  if (exists($passwords{$name})) {
    delete($passwords{$name});
    $numpasswords--;
    msg("Nick $name removed from autoidentify list ($numpasswords left)", MSGLEVEL_CRAP);
  } else {
    msg("Nick $name is not in autoident list", MSGLEVEL_CRAP);
  }
}

sub init_passwords {
  # Add the passwords at startup of the script
  my $passwordsstring = Irssi::settings_get_str('autoident');
  if (length($passwordsstring) > 0) {
    my @passwords = split(/,/, $passwordsstring);

    foreach my $i (@passwords) {
      my $name = substr($i, 0, index($i, '='));
      my $password = substr($i, index($i, '=') + 1, length($i));
      addpassword($name, $password);
    }
  }
}


sub read_settings {
#  my $passwords = Irssi::settings_get_str('passwords');
}


sub update_settings_string {
  my $setting;

  foreach my $name (keys(%passwords)) {
    $setting .= $name . "=" . $passwords{$name} . ",";
  }

  Irssi::settings_set_str("autoident", $setting);
}


sub cmd_addpassword {
  my ($name, $password) = split(/ +/, $_[0]);

  if ($name eq "" || $password eq "") {
    msg("Use /id add <name> <password> to add new nick to autoident list", MSGLEVEL_CRAP);
    return;
  }
  addpassword($name, $password);
  update_settings_string();
}

sub cmd_delpassword {
  my $name = $_[0];

  if ($name eq "") {
    msg("Use /id del <name> to delete a nick from autoident list", MSGLEVEL_CRAP);
    return;
  }

  delpassword($name);
  update_settings_string();
}

sub cmd_showpasswords {
  if ($numpasswords == 0) {
    msg("No nicks defined for autoident", MSGLEVEL_CRAP);
    return;
  }
  msg("Nicks for autoident:", MSGLEVEL_CRAP);
  my $n = 1;
  foreach my $nick (keys(%passwords)) {
#    msg("$nick: " . $mailboxes{$password}, MSGLEVEL_CRAP);
    msg("$n. $nick: ***", MSGLEVEL_CRAP);
    $n++;
  }
}

sub cmd_id {
  my ($data, $server, $item) = @_;
  if ($data =~ m/^[(show)|(add)|(del)|(help)]/i ) {
    Irssi::command_runsub('id', $data, $server, $item);
  } else {
    msg("Use /id (show|add|del|help)", MSGLEVEL_CRAP);
  }
}

Irssi::command_bind('id show', 'cmd_showpasswords');
Irssi::command_bind('id list', 'cmd_showpasswords');
Irssi::command_bind('id add', 'cmd_addpassword');
Irssi::command_bind('id del', 'cmd_delpassword');
Irssi::command_bind('id help', 'cmd_print_help');
Irssi::command_bind('id', 'cmd_id');
Irssi::settings_add_str('misc', 'autoident', '');

read_settings();
init_passwords();
Irssi::signal_add('setup changed', 'read_settings');

#Irssi::signal_add('event privmsg', 'event_privmsg');
Irssi::signal_add('event notice', 'event_notice');

msg("loaded ok", MSGLEVEL_CRAP);

# EOF