Script: unset_unused.pl

Remove options of scripts that are no longer installed.
Author: nils_2 — Version: 0.5 — License: GPL3
For WeeChat ≥ 0.3.0.
Tags: settings
Added: 2011-08-28 — Updated: 2019-07-31

Download GitHub Repository

  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
#
# Copyright (c) 2011-2019 by Nils Görs <weechatter@arcor.de>
#
# unset script option(s) from not installed scripts
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# 22-07-19: 0.5: fixed: scripts currently unloaded wasn't recognised (reported by squigz)
# 18-04-18: 0.4: add support for php and javascript
# 14-10-04: 0.3: fixed: problem with unset options (reported by GermainZ)
# 13-07-27: 0.2: added: support for guile_script
# 11-08-28: 0.1
#
# Development is currently hosted at
# https://github.com/weechatter/weechat-scripts

use strict;

my $PRGNAME     = "unset_unused";
my $VERSION     = "0.5";
my $AUTHOR      = "Nils Görs <weechatter\@arcor.de>";
my $LICENCE     = "GPL3";
my $DESCR       = "unset script option(s) from not installed scripts (YOU ARE USING THIS SCRIPT AT YOUR OWN RISK!)";
my $weechat_version = "";
my %script_plugins = (
                    "python"        => "python_script",
                    "perl"          => "perl_script",
                    "ruby"          => "ruby_script",
                    "tcl"           => "tcl_script",
                    "lua"           => "lua_script",
                    "guile"         => "guile_script",
                    "php"           => "php_script",
                    "javascript"    => "javascript_script",
);

my $str;

# get installed scripts
sub get_scripts
{
    foreach my $script (values %script_plugins)
    {
        my $infolist = weechat::infolist_get($script,"","");
        while (weechat::infolist_next($infolist))
        {
            my $script_name = weechat::infolist_string($infolist, "name");
            $str .= $script_name . "|";
        }
        weechat::infolist_free($infolist);
    }
}

sub get_options
{
    # $flag: 0 = list options; 1 = unset options
    my ( $flag, $count ) = ( $_[0], $_[1] );
    my $key;
    my $number = 0;
    chop($str);
    my $option_struct;
    my %option_struct;

    foreach my $plugin (keys %script_plugins)
    {
        my $infolist = weechat::infolist_get("option","","plugins.var.$plugin.*");
        while (weechat::infolist_next($infolist))
        {
          my $option_name = weechat::infolist_string($infolist, "option_name");
          my $full_name = weechat::infolist_string($infolist, "full_name");
          (undef,$option_name,undef) = split(/\./, $option_name);
            $option_struct->{"full_name"} = $full_name;
            while ( my ($key,$value) = each %$option_struct )
            {
                if ( index($value, "check_license") == -1)
                {
                    if( not $value =~ m/($str)/i)
                    {
                        $number ++;
                        weechat::print("",$value) if ( $flag == 0 );    # list options
                        if ( $flag == 1 )                               # remove options
                        {
                            weechat::print("",$number . "/" . $count . " deleted... " .$value);
                            weechat::command("","/mute unset $value");
                            if ($weechat_version >= 0x00030600)
                            {
                                my $name = substr($value, length("plugins.var."), length($value));
                                weechat::command("","/mute unset plugins.desc.$name");
                            }
                        }
                    }
                }
            }
        }
      weechat::infolist_free($infolist);
    }
    return $number;
}

# delete double entries
sub del_double{
  my %all = ();
  @all{@_} = 1;
  return (keys %all);
}

sub my_command_cb{
  my ($getargs) = ($_[2]);
  return weechat::WEECHAT_RC_OK if ($getargs eq "");

  $str = "";            # reset
  get_scripts();

  if ( $getargs eq "list")
  {
      weechat::print("","unused script options:");
      my $count = get_options(0,0);
      weechat::print("","Number of unused options: $count") if ( $count > 0 );
  }
  elsif ($getargs eq "unset")
  {
      my $count = get_options(0,0);
      get_options(1,$count);
  }
return weechat::WEECHAT_RC_OK
}
# -------------------------------[ init ]-------------------------------------
# first function called by a WeeChat-script.
weechat::register($PRGNAME, $AUTHOR, $VERSION,
                  $LICENCE, $DESCR, "", "");

$weechat_version = weechat::info_get("version_number", "");

weechat::hook_command($PRGNAME, $DESCR,
                "list || unset\n",
                "   list: list all script options from not installed scripts (run this command first!)\n".
                "  unset: remove script options from not installed scripts (without warning!)\n\n".
                "If \"plugins.desc.\" exists, it will be removed, too.\n\n".
                "save your settings with \"/save plugins\" or restore settings with \"/reload plugins\"\n".
                "You can also use \"/unset -mask\", see /help unset".
                "\n",
                "list %-||".
                "unset %-",
                "my_command_cb", "");