1 # ho_qoper.pl
  2 #
  3 # $Id: ho_qoper.pl,v 1.8 2004/08/21 10:56:07 jvunder REL_0_3 $
  4 #
  5 # Part of the Hybrid Oper Script Collection
  6 #
  7 # Quick Oper script - keeps your oper pass in memory and uses it to oper
  8 # up directly after being connected.
  9 #
 10 
 11 use strict;
 12 use Irssi;
 13 use HOSC::again;
 14 use HOSC::again 'HOSC::Base';
 15 use HOSC::again 'HOSC::Tools';
 16 import HOSC::Tools qw(get_named_token);
 17 
 18 use vars qw[$VERSION %IRSSI $SCRIPT_NAME];
 19 
 20 $SCRIPT_NAME = "Qoper";
 21 ($VERSION) = '$Revision: 1.8 $' =~ / (\d+\.\d+) /;
 22 %IRSSI = (
 23     authors     => 'Garion',
 24     contact     => 'garion@irssi.org',
 25     name        => 'ho_qoper',
 26     description => 'Automatic opering on connect.',
 27     license     => 'Public Domain',
 28     url         => 'http://www.garion.org/irssi/hosc/',
 29     changed     => '04 April 2004 12:34:38',
 30 );
 31 
 32 my $main_password;
 33 my $tag_passwords;
 34 
 35 # ----------------------------------------------------------------------
 36 
 37 sub cmd_qoper {
 38     my ($args, $server, $item) = @_;
 39 
 40     if ($args =~ m/^(help)|(status)|(password)|(clearpass)/i ) {
 41         Irssi::command_runsub ('qoper', $args, $server, $item);
 42         return;
 43     }
 44 
 45     if ($args =~ /^\S+$/) {
 46         cmd_qoper_operup($args);
 47     } else {
 48         print_usage();
 49     }
 50 }
 51 
 52 # ----------------------------------------------------------------------
 53 
 54 sub cmd_qoper_help {
 55     print_help();
 56 }
 57 
 58 # ----------------------------------------------------------------------
 59 
 60 sub cmd_qoper_status {
 61     ho_print("Qoper status:");
 62     if (defined $main_password) {
 63         ho_print("Main password is set.");
 64     } elsif (keys %$tag_passwords == 0) {
 65         ho_print("No passwords set.");
 66         return;
 67     }
 68 
 69     if (keys %$tag_passwords == 1) {
 70         ho_print("Password set on tag '" . (keys %$tag_passwords)[0] . "'.");
 71     } else {
 72         ho_print("Passwords set on tags " . 
 73             join(' ', sort keys %$tag_passwords) . ".");
 74     }
 75 }
 76 
 77 # ----------------------------------------------------------------------
 78 
 79 sub cmd_qoper_password {
 80     my ($args, $server, $item) = @_;
 81 
 82     if ($args =~ /^(\S+)\s+(.+)$/) {
 83         set_password($2, $1);
 84     } else {
 85         set_password($args);
 86     }
 87 }
 88 
 89 # ----------------------------------------------------------------------
 90 
 91 sub set_password {
 92     my ($password, $tag) = @_;
 93 
 94     if (defined $tag) {
 95         $tag_passwords->{$tag} = $password;
 96         ho_print("Password set on tag '$tag'.");
 97 
 98         my @networks = get_networks();
 99         if (!grep $tag, @networks) {
100             ho_print_warning("This script is not active for tag '$tag'. Use ".
101                 "the setting ho_qoper_networks to enable it for '$tag'.");
102         }
103     } else {
104         $main_password = $password;
105         ho_print("Main password set.");
106     }
107 }
108 
109 # ----------------------------------------------------------------------
110 
111 sub cmd_qoper_clearpass {
112     my ($args, $server, $item) = @_;
113 
114     clear_password($args);
115 }
116 
117 # ----------------------------------------------------------------------
118 
119 sub clear_password {
120     my ($tag) = @_;
121 
122     if (defined $tag && $tag =~ /\S/) {
123         if (defined $tag_passwords->{$tag}) {
124             delete $tag_passwords->{$tag};
125             ho_print("Deleted password for tag '$tag'.");
126         } else {
127             ho_print("No password stored for tag '$tag'.");
128         }
129     } else {
130         if (defined $main_password) {
131             $main_password = undef;
132             ho_print("Deleted main password.");
133         } else {
134             ho_print("No main password stored.");
135         }
136     }
137 }
138 
139 # ----------------------------------------------------------------------
140 
141 sub cmd_qoper_operup {
142     my ($tag, $server, $item) = @_;
143 
144     my @networks = get_networks();
145     if (!grep $tag, @networks) { 
146         ho_print("Qoper is not activated for tag '$tag'. Use the setting ".
147             "ho_qoper_networks to set this.");
148         return;
149     }
150 
151     my $server = Irssi::server_find_tag($tag);
152     if (!$server) {
153         ho_print("You are not connected to tag '$tag'.");
154         return;
155     }
156 
157     if ($server->{server_operator}) {
158         ho_print("You are already opered on tag '$tag'.");
159         return;
160     }
161 
162     my $password = get_password($tag);
163     if (!defined $password) {
164         ho_print("There is no password set for tag '$tag'");
165         return;
166     }
167 
168     ho_print("Opering up on tag '$tag'.");
169     my $opernick = get_opernick($server->{tag});
170     $server->send_raw_now("OPER $opernick $password");
171 }
172 
173 # ----------------------------------------------------------------------
174 
175 sub event_connected {
176     my ($server) = @_;
177 
178     my @networks = get_networks();
179     my $tag = lc $server->{tag};
180     return unless grep /^$tag$/, @networks;
181 
182     my $password = get_password($server->{tag});
183     if (defined $password) {
184         ho_print("qoper - connected; sending OPER command.");
185         my $opernick = get_opernick($server->{tag});
186         if (!defined $opernick || length $opernick  == 0) {
187             $opernick = $server->{nick};
188         }
189 
190         $server->send_raw_now("OPER $opernick $password");
191     } else {
192         ho_print("qoper - connected to " . $server->{tag} .
193             " but no password is set.");
194     }
195 }
196 
197 # ----------------------------------------------------------------------
198 
199 sub event_opered {
200     my ($server, $msg) = @_;
201 
202     my @networks = get_networks();
203     return unless grep lc $server->{tag}, @networks;
204 
205     my $usermodes = get_usermodes($server->{tag});
206 
207     if (defined $usermodes && length $usermodes > 0) {
208         ho_print("qoper - just opered up. setting user modes $usermodes");
209         $server->send_raw_now('MODE ' . $server->{nick} . " $usermodes");
210     } else {
211         ho_print("qoper - no usermodes set for tag " . $server->{tag} . ".");
212     }
213 }
214 
215 # ----------------------------------------------------------------------
216 
217 sub get_networks {
218     my @networks = split / +/, 
219         lc Irssi::settings_get_str('ho_qoper_network_tags');
220     return @networks;
221 }
222 
223 # ----------------------------------------------------------------------
224 
225 sub get_password {
226     my ($tag) = @_;
227     return $tag_passwords->{$tag} if defined $tag_passwords->{$tag};
228     return $main_password;
229 }
230 
231 # ----------------------------------------------------------------------
232 
233 sub get_opernick {
234     my ($tag) = @_;
235     return get_named_token(Irssi::settings_get_str('ho_qoper_nick'), $tag);
236 }
237 
238 # ----------------------------------------------------------------------
239 
240 sub get_usermodes {
241     my ($tag) = @_;
242     return get_named_token(Irssi::settings_get_str('ho_qoper_usermode'), $tag);
243 }
244     
245 # ----------------------------------------------------------------------
246 
247 ho_print_init_begin();
248 
249 Irssi::signal_add_first('event 001', 'event_connected');
250 Irssi::signal_add_first('event 381', 'event_opered');
251 
252 Irssi::command_bind('qoper',           'cmd_qoper');
253 Irssi::command_bind('qoper help',      'cmd_qoper_help');
254 Irssi::command_bind('qoper status',    'cmd_qoper_status');
255 Irssi::command_bind('qoper password',  'cmd_qoper_password');
256 Irssi::command_bind('qoper clearpass', 'cmd_qoper_clearpass');
257 
258 Irssi::settings_add_str('ho', 'ho_qoper_network_tags', '');
259 Irssi::settings_add_str('ho', 'ho_qoper_nick', '');
260 Irssi::settings_add_str('ho', 'ho_qoper_usermode', '+xy-c');
261 
262 ho_print_init_end();
263 
264 # ----------------------------------------------------------------------
265 
266 sub print_usage {
267     ho_print_help('section', 'Syntax');
268     ho_print_help('syntax', 'QOPER help');
269     ho_print_help('syntax', 'QOPER status');
270     ho_print_help('syntax', 'QOPER password <password>');
271     ho_print_help('syntax', 'QOPER password <tag> <password>');
272     ho_print_help('syntax', 'QOPER clearpass');
273     ho_print_help('syntax', 'QOPER clearpass <tag>');
274     ho_print_help('syntax', 'QOPER <tag>');
275 }
276 
277 sub print_help {
278     ho_print_help('head', $SCRIPT_NAME);
279 
280     print_usage();
281 
282     ho_print_help('section', 'Description');
283     ho_print_help('This script allows your client to be opered ' .
284         "automatically upon connect, and set usermodes when opered.\n");
285     ho_print_help("You can store a main oper password ".
286         "which will be used as default, plus one or more exceptions ".
287         "for any networks that you use a different password on. The same ".
288         "can be done for your oper nick.\n");
289     ho_print_help("To oper up manually, use /QOPER <tag>. If the script " .
290         "is active for this tag and a password has been stored for it, ".
291         "the script will attempt to oper up.\n");
292     ho_print_help('Why should you use this instead of -autosendcmd? ' .
293         'Well, the main reason is that this script stores the oper '.
294         'password(s) in memory, not in a config file. This is much '.
295         'safer than -autosendcmd. The second reason is that this '.
296         "setup is much more flexible.\n");
297 
298     ho_print_help('section', 'Settings');
299     ho_print_help('setting', 'ho_qoper_network_tags',
300         'A space separated list of the server tags of the networks that ' .
301         'this script should work on.');
302     ho_print_help('setting', 'ho_qoper_nick',
303         'Your oper nick. If not set, your current nick is used. ' .
304         'This is a multitoken. See /HO HELP MULTITOKEN');
305     ho_print_help('setting', 'ho_qoper_usermodes',
306         'The usermodes that are set right after you oper up. Use the '.
307         'format +abc-def. '.
308         'This is a multitoken. See /HO HELP MULTITOKEN');
309 }


syntax highlighted by Code2HTML, v. 0.9.1