From 13d05886dbc6f10a513c67dc29e0221bff1150e4 Mon Sep 17 00:00:00 2001 From: fimojoha Date: Wed, 29 Apr 2015 11:11:15 +0200 Subject: Script to suggest slack-mention versions of nicks when completing --- scripts/slack_complete.pl | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 scripts/slack_complete.pl (limited to 'scripts') diff --git a/scripts/slack_complete.pl b/scripts/slack_complete.pl new file mode 100644 index 0000000..c4b0443 --- /dev/null +++ b/scripts/slack_complete.pl @@ -0,0 +1,62 @@ +# +# Copyright (C) 2015 by Morten Lied Johansen +# + +use strict; + +use Irssi; +use Irssi::Irc; + +# ======[ Script Header ]=============================================== + +use vars qw{$VERSION %IRSSI}; +($VERSION) = '$Revision: 1.0 $' =~ / (\d+\.\d+) /; +%IRSSI = ( + name => 'slack_complete', + authors => 'Morten Lied Johansen', + contact => 'mortenjo@ifi.uio.no', + license => 'GPL', + description => 'Convert to slack-mention when completing nicks', + ); + +# ======[ Hooks ]======================================================= + +# --------[ sig_complete_slack_nick ]----------------------------------- + +sub sig_complete_slack_nick { +my ($complist, $window, $word, $linestart, $want_space) = @_; + + my $wi = Irssi::active_win()->{active}; + return unless ref $wi and $wi->{type} eq 'CHANNEL'; + return unless $wi->{server}->{chatnet} eq + Irssi::settings_get_str('slack_network'); + + if ($word =~ /^@/) { + $word =~ s/^@//; + } + foreach my $nick ($wi->nicks()) { + if ($nick->{nick} =~ /^\Q$word\E/i) { + if ($linestart) { + push(@$complist, "\@$nick->{nick}"); + } else { + push(@$complist, "\@$nick->{nick}:"); + } + } + } + + @$complist = sort { + return $a =~ /^\@\Q$word\E(.*)$/i ? 0 : 1; + } @$complist; +} + +# ======[ Setup ]======================================================= + +# --------[ Register settings ]----------------------------------------- + +Irssi::settings_add_str('slack_complete', 'slack_network', 'Slack'); + +# --------[ Register signals ]------------------------------------------ + +Irssi::signal_add('complete word', \&sig_complete_slack_nick); + +# ======[ END ]========================================================= -- cgit v1.2.3 From bb24818ba7a8d5e6909d31f9e608bf497fc7dfb0 Mon Sep 17 00:00:00 2001 From: fimojoha Date: Wed, 29 Apr 2015 11:11:32 +0200 Subject: Script to convert privmsg to notice for selected nicks --- scripts/msg2notice.pl | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 scripts/msg2notice.pl (limited to 'scripts') diff --git a/scripts/msg2notice.pl b/scripts/msg2notice.pl new file mode 100644 index 0000000..33ddf3c --- /dev/null +++ b/scripts/msg2notice.pl @@ -0,0 +1,193 @@ +# +# Copyright (C) 2015 by Morten Lied Johansen +# + +use strict; + +use Irssi; +use Irssi::Irc; + +# ======[ Script Header ]=============================================== + +use vars qw{$VERSION %IRSSI}; +($VERSION) = '$Revision: 1.0 $' =~ / (\d+\.\d+) /; +%IRSSI = ( + name => 'msg2notice', + authors => 'Morten Lied Johansen', + contact => 'mortenjo@ifi.uio.no', + license => 'GPL', + description => 'For a configured list of nicks, convert all their messages to a notice', + ); + +# ======[ Variables ]=================================================== + +my(%nicks); + +# ======[ Helpers ]===================================================== + +# --------[ crap ]------------------------------------------------------ + +sub crap { + my $template = shift; + my $msg = sprintf $template, @_; + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'msg2notice_crap', $msg); +} + +# --------[ list_nicks ]------------------------------------------------ + +sub list_nicks { + my $count = keys %nicks; + + crap("Listing $count nicks"); + foreach my $nick (keys %nicks) { + crap($nick); + } +} + +# --------[ add_nick ]-------------------------------------------------- + +sub add_nick { + my($nick) = @_; + + $nick =~ s/^\s+|\s+$//g; + $nicks{$nick} = 1; + crap("Added $nick to list"); +} + +# --------[ del_nick ]-------------------------------------------------- + +sub del_nick { + my($nick) = @_; + + $nick =~ s/^\s+|\s+$//g; + delete $nicks{$nick}; + crap("Removed $nick from list"); +} + +# --------[ load_nicks ]------------------------------------------------ + +sub load_nicks { + my($file) = Irssi::get_irssi_dir."/msg2notice"; + my($count) = 0; + my($mask,$net,$channel,$flags,$flag); + local(*FILE); + + %nicks = (); + open FILE, "< $file"; + while () { + add_nick($_); + } + close FILE; + $count = keys %nicks; + + crap("Loaded $count nicks"); +} + +# --------[ save_nicks ]------------------------------------------------ + +sub save_nicks { + my($auto) = @_; + my($file) = Irssi::get_irssi_dir."/msg2notice"; + my($count) = 0; + local(*FILE); + + return if $auto && !Irssi::settings_get_bool('msg2notice_autosave'); + + open FILE, "> $file"; + for my $nick (keys %nicks) { + $count++; + print FILE "$nick\n"; + } + close FILE; + + crap("Saved $count nicks to $file") + unless $auto; +} + +# ======[ Hooks ]======================================================= + +# --------[ sig_event_privmsg ]----------------------------------------- + +sub sig_event_privmsg { + my ($server, $data, $sender_nick, $sender_address) = @_; + + if (exists $nicks{$sender_nick}) { + Irssi::signal_emit('event notice', $server, $data, $sender_nick, $sender_address); + Irssi::signal_stop(); + } +} + +# --------[ sig_setup_reread ]------------------------------------------ + +sub sig_setup_reread { + load_nicks; +} + +# --------[ sig_setup_save ]-------------------------------------------- + +sub sig_setup_save { + my($mainconf,$auto) = @_; + save_nicks($auto); +} + +# ======[ Commands ]==================================================== + +# --------[ MSG2NOTICE ]------------------------------------------------ + +# Usage: /MSG2NOTICE [list|add|del|load|save] [ ...] +sub cmd_msg2notice { + my($param,$serv,$chan) = @_; + my(@split) = split " ", $param; + my $cmd = shift @split; + my $save = 0; + + if ($cmd eq "list") { + list_nicks; + } elsif ($cmd eq "add") { + while (@split) { + add_nick(shift @split); + $save = 1; + } + } elsif ($cmd eq "del") { + while (@split) { + del_nick(shift @split); + $save = 1; + } + } elsif ($cmd eq "load") { + load_nicks; + } elsif ($cmd eq "save") { + save_nicks; + } else { + crap("Unknown command: $cmd"); + } + + if ($save) { + save_nicks(1); + } +} + +# ======[ Setup ]======================================================= + +# --------[ Register commands ]----------------------------------------- + +Irssi::command_bind('msg2notice', \&cmd_msg2notice); + +# --------[ Register settings ]----------------------------------------- + +Irssi::settings_add_bool('msg2notice', 'msg2notice_autosave', 1); + +# --------[ Register signals ]------------------------------------------ + +Irssi::signal_add('event privmsg', \&sig_event_privmsg); +Irssi::signal_add('setup saved', 'sig_setup_save'); +Irssi::signal_add('setup reread', 'sig_setup_reread'); + +# --------[ Register formats ]------------------------------------------ + +Irssi::theme_register( +[ + 'msg2notice_crap', + '{line_start}{hilight Msg->Notice:} $0', +]); + +# ======[ END ]========================================================= -- cgit v1.2.3 From 5ee9e0b72ab4c78de1ccc54b0bf982a91b915a11 Mon Sep 17 00:00:00 2001 From: fimojoha Date: Wed, 29 Apr 2015 16:03:35 +0200 Subject: Use 3-arg open, and check return values --- scripts/msg2notice.pl | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'scripts') diff --git a/scripts/msg2notice.pl b/scripts/msg2notice.pl index 33ddf3c..5dc3ba9 100644 --- a/scripts/msg2notice.pl +++ b/scripts/msg2notice.pl @@ -72,15 +72,18 @@ sub load_nicks { my($mask,$net,$channel,$flags,$flag); local(*FILE); - %nicks = (); - open FILE, "< $file"; - while () { - add_nick($_); - } - close FILE; - $count = keys %nicks; + %nicks = (); + if (open FILE, "<", $file) { + while () { + add_nick($_); + } + close FILE; + $count = keys %nicks; - crap("Loaded $count nicks"); + crap("Loaded $count nicks"); + } else { + crap("Unable to open $file for loading: $!"); + } } # --------[ save_nicks ]------------------------------------------------ @@ -93,15 +96,18 @@ sub save_nicks { return if $auto && !Irssi::settings_get_bool('msg2notice_autosave'); - open FILE, "> $file"; - for my $nick (keys %nicks) { - $count++; - print FILE "$nick\n"; - } - close FILE; + if (open FILE, ">", $file) { + for my $nick (keys %nicks) { + $count++; + print FILE "$nick\n"; + } + close FILE; - crap("Saved $count nicks to $file") - unless $auto; + crap("Saved $count nicks to $file") + unless $auto; + } else { + crap("Unable to open $file for saving: $!"); + } } # ======[ Hooks ]======================================================= -- cgit v1.2.3 From baa299bacfd4124524602e2da9251f692a90a978 Mon Sep 17 00:00:00 2001 From: fimojoha Date: Wed, 29 Apr 2015 16:06:27 +0200 Subject: Load nicks on script-load --- scripts/msg2notice.pl | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'scripts') diff --git a/scripts/msg2notice.pl b/scripts/msg2notice.pl index 5dc3ba9..f8febad 100644 --- a/scripts/msg2notice.pl +++ b/scripts/msg2notice.pl @@ -196,4 +196,8 @@ Irssi::theme_register( '{line_start}{hilight Msg->Notice:} $0', ]); +# --------[ Load config ]----------------------------------------------- + +load_nicks; + # ======[ END ]========================================================= -- cgit v1.2.3