Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use Xchat qw(:all);
- use Tie::File;
- # usage info:
- # put a directory named "quotes" in your xchat config directory (usually ~/.xchat2/quotes)
- # place one file named "quotes.txt" with some default quotes
- # place a file named by the user nickname mask for custom quotes for a specific nickname
- # e.g. for ignore mask AnnoyingUser*!*@* => annoyinguser*.txt
- # (!) keep filenames lowercase
- # register plugin and hook into messages
- register( "IgnoreExt", "0.5",
- "Extended ignore method which shows a random quote when the ignored users write something" );
- hook_server("PRIVMSG", \&ignore_ext);
- hook_server("NOTICE", \&ignore_ext);
- hook_server("INVITE", \&ignore_ext);
- my $defaultQuotesFilename = get_info("xchatdir")."/quotes/quotes.txt";
- my @customQuotes;
- loadQuotes();
- sub loadQuotes{
- prnt "loading quotes";
- my @ignoreList = get_list("ignore");
- for my $i (0 .. $#ignoreList){
- my ($nick, $hostmask) = split("!", $ignoreList[$i]->{'mask'});
- loadQuotesForNick($nick);
- }
- }
- sub getQuoteIndexByUser{
- my $nick = lc($_[0]);
- for my $i (0 .. $#customQuotes){
- if(${$customQuotes[$i]}[0] eq $nick){
- return $i;
- }
- }
- return -1;
- }
- sub loadQuotesForNick{
- my $nick = lc($_[0]);
- prnt "loading quotes for $nick";
- my @quotes = ();
- my $filename = get_info("xchatdir")."/quotes/". $nick . ".txt";
- use Fcntl 'O_RDONLY';
- tie @quotes, 'Tie::File', $filename, mode => O_RDONLY;
- if($#quotes > 0){
- prnt "loaded $#quotes quotes from '" . $filename
- }
- else {
- prnt "couldn't read file '$filename'";
- 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;
- }
- my $quoteIndex = getQuoteIndexByUser($nick);
- if(not $quoteIndex eq -1){
- $customQuotes[$quoteIndex][1] = @quotes
- }
- else {
- push @customQuotes, [(lc($nick), @quotes)]
- }
- }
- # Bit field of flags
- my @flagNames = ("private", "notice", "channel", "ctcp", "invite", "unignore", "nosave", "dcc", "unknown");
- sub makeRegexString{
- # replace * by .*
- $_[0] =~ s/\*/\.*/g;
- # escape |
- $_[0] =~ s/\|/\\|/g;
- # return as lowercase
- return lc($_[0]);
- }
- sub getIgnoreListEntry{
- my @ignoreList = get_list("ignore");
- for my $i (0 .. $#ignoreList){
- my ($nick, $mask) = split("!", $ignoreList[$i]->{'mask'});
- $mask = makeRegexString($mask);
- $nick = makeRegexString($nick);
- if (lc($_[0]) =~ m/$nick/ and lc($_[1]) =~ m/$mask/){
- return $ignoreList[$i];
- }
- }
- return 0;
- }
- sub getFlagsForUser {
- return getIgnoreListEntry($_[0], $_[1])->{flags};
- }
- sub getMaskForUser {
- return getIgnoreListEntry($_[0], $_[1])->{mask};
- }
- sub checkFlag {
- return ($_[0] >> $_[1]) & 1;
- }
- sub getContext {
- if ($_[0] eq "NOTICE"){
- return 1;
- }
- if ($_[0] eq "INVITE"){
- return 4;
- }
- if (get_info("nick") eq $_[1]){
- return 0;
- }
- if (substr($_[1], 0, 1) eq "#"){
- return 2;
- }
- return $_[0];
- return 8; # unknown bit flag
- }
- sub quoteForIgnoredUser {
- my $nick = lc($_[0]);
- my $quoteIndex = getQuoteIndexByUser($nick);
- if($quoteIndex eq -1){
- loadQuotesForNick($nick);
- }
- my $quoteCount = $#{$customQuotes[$quoteIndex]};
- if($quoteCount eq 0){
- return "ignored";
- }
- return ${$customQuotes[$quoteIndex]}[int(rand($quoteCount))];
- }
- # message parsing subroutine
- sub ignore_ext {
- my ($nick, $hostmask) = split("!", substr($_[0][0], 1));
- # return if user isnt on list
- if(getIgnoreListEntry($nick, $hostmask) eq 0){
- return EAT_NONE;
- }
- my $userFlags = getFlagsForUser($nick, $hostmask);
- # return if unignore is checked
- if(checkFlag($userFlags, 5)){
- return EAT_NONE;
- }
- my $context = getContext($_[0][1], $_[0][2]);
- # return on invalid context (channelmessage, invite, notice and private message are supported)
- if($context eq 8){
- prnt "unknown context";
- return EAT_NONE;
- }
- # check if the current context is ignored
- if(not checkFlag($userFlags, $context)){
- return EAT_NONE;
- }
- prnt "2$nick (". $flagNames[$context] .") ~ " . quoteForIgnoredUser(split("!", getMaskForUser($nick, $hostmask)));
- return EAT_XCHAT;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement