Advertisement
Dijit

brag.pl

Aug 27th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. ######## 2013-02-24
  3. # 2013-02-24 v1.6 Add pluraltext to pluralize or singularize final output
  4. # 2013-02-24 v1.5 Add prefix2value sub
  5. # 2013-02-24 v1.4 Don't count nicks that are joined to more than one channel shared with myself
  6. # 2013-02-23 v1.3 Count only those nicknames that are equal or lesser in channel prefix power
  7. # 2013-02-23 v1.2 Include all users
  8. # 2013-02-23 v1.1 Include *owner !owner prefixes
  9. # 2013-02-23 v1.0 Brag
  10.  
  11. use strict;
  12. use warnings;
  13.  
  14. Xchat::register("Brag Script", "1.6", "Brag Script v1.6", "");
  15. Xchat::hook_command("brag", \&brag);
  16.  
  17. sub brag {
  18.     # Save context for later, so we can send our message to the active channel at the end of the script
  19.     my $startcontext = Xchat::get_context();
  20.     # Get an array of all channels
  21.     my @channels = Xchat::get_list('channels');
  22.     # Create a hash to keep our keyval counts
  23.     my %powercount = (  networks => 0,
  24.                         channels => 0,
  25.                         users => 0,
  26.                         owner => 0,
  27.                         admin => 0,
  28.                         op => 0,
  29.                         hop => 0,
  30.                         voice => 0,
  31.                         regular => 0,
  32.                         kickable => 0,
  33.                         uniquenicks => 0,
  34.     );
  35.  
  36.     ## Build an internal network-wide nickname list to      ##
  37.     ## prevent adding nicknames more than once if they are  ##
  38.     ## joined to more than one with us.                     ##
  39.     my %NetNickList;
  40.  
  41.     # Iterate through each channel
  42.     foreach my $chanitem (@channels){
  43.         # Check type to see if it is a 1 => server window 2 => channel window 3 => dialog window
  44.         my $chantype = $chanitem->{'type'};
  45.         if ($chantype == 1) {
  46.             # This is a server window, add to network count
  47.             $powercount{'networks'}++;
  48.         } elsif ($chantype == 2) {
  49.             # This is a channel window
  50.             $powercount{'channels'}++;
  51.             # Get channel name
  52.             my $chan = $chanitem->{'channel'};
  53.             # Set context for the following info commands to the chan
  54.             Xchat::set_context("$chan");
  55.  
  56.             #### Calculate the number of owner|admin|op|hop|voice ####
  57.             # Get my user info on this channel
  58.             my $userinfo = Xchat::user_info();
  59.             # Grab user prefix for this channel to check if I am an op here
  60.             my $meprefix = $userinfo->{'prefix'};
  61.             # Grab only the first prefix character (highest power)
  62.             my $pwrprefix = substr $meprefix, 0, 1;
  63.             if ((defined $meprefix) && (length($meprefix) > 0)) {
  64.                 # Map prefix to key
  65.                 my %mapreplace = (  '+' => "voice",
  66.                                     '%' => "hop",
  67.                                     '@' => "op",
  68.                                     '&' => "admin",
  69.                                     '~' => "owner",
  70.                                     '!' => "owner",
  71.                                     '*' => "owner",
  72.                 );
  73.                 $pwrprefix =~ s/^([*!~&@%+])/$mapreplace{$1}/;
  74.             } else {
  75.                 $pwrprefix = "regular";
  76.             }
  77.             # Increment hash based on mapped key
  78.             $powercount{$pwrprefix}++;
  79.  
  80.             #### Calculate the total number of users I can kick   ####
  81.             # Get the array of users for the channel
  82.             my @Users = Xchat::get_list('users');
  83.             # Add the total number of users
  84.             $powercount{'users'} += scalar(@Users);
  85.  
  86.             #### Continue adding nicknames to the global nickname list ####
  87.             # Map prefixname to value
  88.             my $mynickvalue = prefix2value(substr $meprefix, 0, 1);
  89.             # Check each nickanme to see if we can kick it
  90.             my $netname = Xchat::get_info('network');
  91.             my $me = Xchat::get_info('nick');
  92.             foreach my $user (@Users) {
  93.                 my $nickname = $user->{nick};
  94.                 # Make sure I don't count myself
  95.                 if ($nickname ne $me) {
  96.                     # Add the network+nickname to the global nickname list, if it is not alrealy in the list
  97.                     if (!exists($NetNickList{$netname.$nickname})) {
  98.                         $NetNickList{$netname.$nickname} = 0;
  99.                     }
  100.                     # Get their prefix value
  101.                     my $theirnickvalue = prefix2value(substr $user->{prefix}, 0, 1);
  102.                     # If I have a higher prefix, check off that this nick is kickable in at least one channel we share with them
  103.                     if ($theirnickvalue <= $mynickvalue) {
  104.                         $NetNickList{$netname.$nickname}++;
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     # Return context to active window
  111.     Xchat::set_context("$startcontext");
  112.    
  113.     #### Calculate the total number of nicks in our global nickname list that I can kick with no repeats ####
  114.     my @uniquenicks = keys(%NetNickList);
  115.     foreach my $uniquenick (@uniquenicks) {
  116.         $powercount{'uniquenicks'}++;
  117.         if ($NetNickList{$uniquenick} > 0) {
  118.             $powercount{'kickable'}++;
  119.         }
  120.     }
  121.                
  122.     my $outmsg = "I am connected to ".pluraltext($powercount{'networks'},"network")." and joined to ".pluraltext($powercount{'channels'},"channel").". ";
  123.     $outmsg .= "I have ".pluraltext($powercount{'owner'},"owner").", ".pluraltext($powercount{'admin'},"admin").", ".pluraltext($powercount{'op'},"op").", ".pluraltext($powercount{'hop'},"halfop")." and ".pluraltext($powercount{'voice'},"voice").". ";
  124.     $outmsg .= "I have connection resetting power over $powercount{'kickable'} out of $powercount{'uniquenicks'} people. ";
  125.     $outmsg .= "If I did not check for repeat nicks, or my own nick, I would have assumed a total of $powercount{'users'} people. ";
  126.     Xchat::command("say $outmsg");
  127.     return Xchat::EAT_ALL;
  128. }
  129.  
  130. sub prefix2value {
  131.     # Map prefix character to value
  132.     my $prefix = $_[0];
  133.     if ((defined $prefix) && (length($prefix) > 0)) {
  134.         my %valuemap = (    '+' => 2,
  135.                             '%' => 3,
  136.                             '%' => 4,
  137.                             '@' => 5,
  138.                             '&' => 6,
  139.                             '~' => 7,
  140.                             '!' => 8,
  141.                             '*' => 9,
  142.         );
  143.         my $keyregex = join '', keys %valuemap;
  144.         $prefix =~ s/^([$keyregex])/$valuemap{$1}/;
  145.         return $prefix;
  146.     } else {
  147.         return 1;
  148.     }
  149. }
  150.  
  151. sub pluraltext {
  152.     # Takes a number and a word without an 's' at the end
  153.     # Returns the number and word, with or without an 's' if the word needs to be plural
  154.     # Also returns 'no' instead of 0
  155.     my ($X, $W) = @_;
  156.     if ((!defined $X) || ($X < 1)) {
  157.         return "no $W"."s";
  158.     } elsif ($X == 1) {
  159.         return "$X $W";
  160.     } else {
  161.         return "$X $W"."s";
  162.     }
  163. }
  164.  
  165. Xchat::print("PK Brag Script v1.6 loaded.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement