Advertisement
ben_mkiv

XChat2 random quotes for ignored Users

Nov 28th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.24 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use Xchat qw(:all);
  4. use Tie::File;
  5.  
  6. # usage info:
  7. # put a directory named "quotes" in your xchat config directory (usually ~/.xchat2/quotes)
  8. # place one file named "quotes.txt" with some default quotes
  9. # place a file named by the user nickname mask for custom quotes for a specific nickname
  10. # e.g. for ignore mask AnnoyingUser*!*@* => annoyinguser*.txt
  11. # (!) keep filenames lowercase
  12.  
  13. # register plugin and hook into messages
  14. register( "IgnoreExt", "0.5",
  15.      "Extended ignore method which shows a random quote when the ignored users write something" );
  16.  
  17. hook_server("PRIVMSG", \&ignore_ext);
  18. hook_server("NOTICE", \&ignore_ext);
  19. hook_server("INVITE", \&ignore_ext);
  20.  
  21. my $defaultQuotesFilename = get_info("xchatdir")."/quotes/quotes.txt";
  22.  
  23. my @customQuotes;
  24.  
  25. loadQuotes();
  26.  
  27. sub loadQuotes{
  28.     prnt "loading quotes";
  29.     my @ignoreList = get_list("ignore");
  30.     for my $i (0 .. $#ignoreList){
  31.         my ($nick, $hostmask) = split("!", $ignoreList[$i]->{'mask'});
  32.         loadQuotesForNick($nick);
  33.     }
  34. }
  35.  
  36. sub getQuoteIndexByUser{
  37.     my $nick = lc($_[0]);
  38.    
  39.     for my $i (0 .. $#customQuotes){
  40.         if(${$customQuotes[$i]}[0] eq $nick){
  41.             return $i;
  42.         }
  43.     }  
  44.    
  45.     return -1;
  46. }
  47.  
  48. sub loadQuotesForNick{
  49.     my $nick = lc($_[0]);
  50.    
  51.     prnt "loading quotes for $nick";       
  52.    
  53.     my @quotes = ();
  54.     my $filename = get_info("xchatdir")."/quotes/". $nick . ".txt";
  55.     use Fcntl 'O_RDONLY';
  56.     tie @quotes, 'Tie::File', $filename, mode => O_RDONLY;
  57.    
  58.     if($#quotes > 0){
  59.         prnt "loaded $#quotes quotes from '" . $filename
  60.     }
  61.     else {
  62.         prnt "couldn't read file '$filename'";     
  63.         tie @quotes, 'Tie::File', $defaultQuotesFilename, mode => O_RDONLY and prnt "loaded " . $#quotes . " default quotes for " . $nick or prnt "couldn't load default quotes from " . $defaultQuotesFilename . " for " . $nick;
  64.     }      
  65.    
  66.     my $quoteIndex = getQuoteIndexByUser($nick);
  67.     if(not $quoteIndex eq -1){
  68.         $customQuotes[$quoteIndex][1] = @quotes
  69.     }
  70.     else {
  71.         push @customQuotes, [(lc($nick), @quotes)]
  72.     }
  73. }
  74.  
  75. # Bit field of flags
  76. my @flagNames = ("private", "notice", "channel", "ctcp", "invite", "unignore", "nosave", "dcc", "unknown");
  77.  
  78. sub makeRegexString{
  79.     # replace * by .*
  80.     $_[0] =~ s/\*/\.*/g;
  81.     # escape |
  82.     $_[0] =~ s/\|/\\|/g;
  83.     # return as lowercase
  84.     return lc($_[0]);
  85. }
  86.  
  87. sub getIgnoreListEntry{
  88.     my @ignoreList = get_list("ignore");
  89.  
  90.     for my $i (0 .. $#ignoreList){
  91.         my ($nick, $mask) = split("!", $ignoreList[$i]->{'mask'});
  92.         $mask = makeRegexString($mask);    
  93.         $nick = makeRegexString($nick);
  94.        
  95.         if (lc($_[0]) =~ m/$nick/ and lc($_[1]) =~ m/$mask/){
  96.             return $ignoreList[$i];
  97.         }
  98.     }
  99.    
  100.     return 0;
  101. }
  102.  
  103. sub getFlagsForUser {      
  104.     return getIgnoreListEntry($_[0], $_[1])->{flags};
  105. }
  106.  
  107. sub getMaskForUser {       
  108.     return getIgnoreListEntry($_[0], $_[1])->{mask};
  109. }
  110.  
  111. sub checkFlag {
  112.     return ($_[0] >> $_[1]) & 1;
  113. }
  114.  
  115. sub getContext {
  116.     if ($_[0] eq "NOTICE"){
  117.         return 1;
  118.     }  
  119.     if ($_[0] eq "INVITE"){
  120.         return 4;
  121.     }  
  122.     if (get_info("nick") eq $_[1]){
  123.         return 0;
  124.     }
  125.     if (substr($_[1], 0, 1) eq "#"){
  126.         return 2;
  127.     }
  128.    
  129.     return $_[0];
  130.    
  131.     return 8; # unknown bit flag
  132. }
  133.  
  134. sub quoteForIgnoredUser {
  135.     my $nick = lc($_[0]);
  136.     my $quoteIndex = getQuoteIndexByUser($nick);
  137.    
  138.     if($quoteIndex eq -1){     
  139.         loadQuotesForNick($nick);
  140.     }
  141.    
  142.     my $quoteCount = $#{$customQuotes[$quoteIndex]};
  143.    
  144.     if($quoteCount eq 0){
  145.         return "ignored";
  146.     }
  147.    
  148.    
  149.     return ${$customQuotes[$quoteIndex]}[int(rand($quoteCount))];
  150. }
  151.  
  152. # message parsing subroutine
  153. sub ignore_ext {
  154.   my ($nick, $hostmask) = split("!", substr($_[0][0], 1));
  155.  
  156.   # return if user isnt on list
  157.   if(getIgnoreListEntry($nick, $hostmask) eq 0){
  158.     return EAT_NONE;
  159.   }
  160.  
  161.   my $userFlags = getFlagsForUser($nick, $hostmask);
  162.   # return if unignore is checked
  163.   if(checkFlag($userFlags, 5)){
  164.     return EAT_NONE;
  165.   }
  166.  
  167.   my $context = getContext($_[0][1], $_[0][2]);
  168.  
  169.   # return on invalid context (channelmessage, invite, notice and private message are supported)
  170.   if($context eq 8){
  171.      prnt "unknown context";
  172.      return EAT_NONE;
  173.   }
  174.  
  175.   # check if the current context is ignored
  176.   if(not checkFlag($userFlags, $context)){
  177.     return EAT_NONE;
  178.   }
  179.  
  180.   prnt "2$nick (". $flagNames[$context] .") ~ " . quoteForIgnoredUser(split("!", getMaskForUser($nick, $hostmask)));
  181.   return EAT_XCHAT;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement