From c756fea56d9672a66cd55b01fb76a29fc2a04c5b Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Fri, 1 Jan 2016 23:45:39 -0800 Subject: [PATCH] Add the new factoid plugins --- etc/plugins.conf | 86 +++++++++++------- plugins/.badfactstemp.pm.swp | Bin 0 -> 12288 bytes plugins/badfacts.pm | 65 -------------- plugins/badfactstemp.pm | 87 ------------------ plugins/factoids.pm | 9 +- plugins/nfacts.pm | 170 +++++++++++++++++++++++++++++++++++ 6 files changed, 231 insertions(+), 186 deletions(-) create mode 100644 plugins/.badfactstemp.pm.swp delete mode 100644 plugins/badfacts.pm delete mode 100644 plugins/badfactstemp.pm create mode 100644 plugins/nfacts.pm diff --git a/etc/plugins.conf b/etc/plugins.conf index caacebf..d6857b6 100644 --- a/etc/plugins.conf +++ b/etc/plugins.conf @@ -1,34 +1,56 @@ server "*" { - channel "*" { - plugin "*" { addressed: true } - plugin "join" { access: op; addressed: true } - plugin "part" { access: op } - plugin "reload_plugins" { access: root } - plugin "restart" { access: root } - plugin "conf_dump" { access: root; } - plugin "save_config" { access: root; } - plugin "conf" { access: root; } - plugin "karma_modify" { addressed: false; } - plugin "seen" {addressed: false; } - } - channel "#perl" { - plugin "eval" {addressed: false; } - plugin "deparse" {addressed: false; } - } - channel "#perl-cats" { - plugin "eval" {addressed: false; } - plugin "deparse" {addressed: false; } - } - channel "#perlbot" { - plugin "eval" {addressed: false; } - plugin "deparse" {addressed: false; } - plugin "badfacts" {addressed: false; } - } - channel "#buubot" { - plugin "eval" {addressed: false; } - plugin "deparse" {addressed: false; } - } - channel "#regex" { - plugin "badfacts" {addressed: false; } - } + channel "*" { + plugin "*" { addressed: true } + plugin "join" { access: op; addressed: true } + plugin "part" { access: op } + plugin "reload_plugins" { access: root } + plugin "restart" { access: root } + plugin "conf_dump" { access: root; } + plugin "save_config" { access: root; } + plugin "conf" { access: root; } + plugin "karma_modify" { addressed: false; } + plugin "seen" {addressed: false; } + } + channel "#perl" { + plugin "eval" {addressed: false; } + plugin "deparse" {addressed: false; } + } + channel "#perl-cats" { + plugin "eval" {addressed: false; } + plugin "deparse" {addressed: false; } + } + channel "#perlbot" { + plugin "eval" {addressed: false; } + plugin "deparse" {addressed: false; } + plugin "badfacts" {addressed: false; } + } + channel "#buubot" { + plugin "eval" {addressed: false; } + plugin "deparse" {addressed: false; } + } + channel "#regex" { + plugin "badfacts" {addressed: false; } + } +} +server "*.freenode.net" { + channel "#perlbot" { + plugin "nfacts" { + addressed: false; + enabled: true; + prefix: "!"; + plugins: false; + chanspace: "#perlbot"; + serverspace: "freenode.net"; + filtersep: false; + } + } + channel "#regex" { + plugin "nfacts" { + addressed: false; + enabled: true; + prefix: "!"; + plugins: false; + filtersep: true; + } + } } diff --git a/plugins/.badfactstemp.pm.swp b/plugins/.badfactstemp.pm.swp new file mode 100644 index 0000000000000000000000000000000000000000..b0da10102e09998ce30e37c57e839117755611e3 GIT binary patch literal 12288 zcmeHNO>7%Q6rNTTNK1bJi6fdihS*KwO;g7585>{czH9*`<#r7ycX^XAQa z-@Nx`tZaSk=;H_Z-qHgM*AB)`e{lNf#`*^K{_~85tHPt^f4Y>@w*6rDo_%{uj@R_B zD}IU;T6JG5E0CdEk0NaaZq#x-Wz|KaDQvB@YzL*FeO(=GnA7zPXjh5^HXVc=$EKnEMx3ow3T>OdpCZ@B87 zzM7k1z%XDKFbo(53GHFkl!k3>XFs12-W90zddS-NM-UO)wt+|IdE^f4iNr zuYiw%3&6X;S>R>hC7=r|08aqpz`ejW;E!#L{SN#F{0RI2Tm-%WJ_4G61ZIF~;PO_+ zz5_l1&H}!^O4Fg<9mI4uyO{EYUbebLok?3yn=b^a(Vy$J>~M@_`@!|q#t%2!G~SB3C7mgfOM7Q9Ue*@g+2Fev_M_Tga^RaSFg5q^)iwL@DaE=!zpp0=` z%Odm=jZQPZ!wzb1uO_XNJU>0b2HLBbS81Umb%jZjTrPpL4$mjkjaE`tNnAJ$2nTI4 z9Xebe>k_MrXNdbpE1mTPS>uy@9CBGls;Sl%+Fqc}AzII&K8aA0jPx3^Q4?WHkpR{q^>3Y6w*n-SdSNS45FR;@-=#d zZ^T$i5~;_uaZl#`B%iKLFcsB#7^O;1u;`S3bmSx$0Q1ln<}flScrB zWkh6-Q?SrmPYf;a17XQBb%|&2CnPU2&kI+18MHHMX{UuPD(X^@e%)u;0?q3NttAKp1#n0koR zRjPBtMFc2bNkOGFT+CY;cL|dSEF{OZ^<=~5PfQ;#?V2hUrpi?{T~)h!>UR~3Q+bPb zyS=bYtRSB#vYh+9Jf*CvXK||)_9fIBPilEgDiKT9dU$6UNZBEUOF__| z1|&-)XMG^L07K&aYATmK@mx8VE0%J#TE3I9T8q;wTTIeJS4?s`o?J?{Cp^UyL;Lh_ z2mYlJ>kd+{)fe4yWKu}-I1dLAZmFtXK$od8v9yBy5T&&1N^COSP$qX2XBi3??}!Uc zH%B?Xr@9Ja_ChxZeJyR>jlAp$g;QxqN3z=%cIZ>$6sNKrp!o0Q4mr!{4_(AyVr^1W zvwTYGMt_?1_Mj{Y2lSKR(sqY^M0o63%}(GLV?kp=0X_hAnhav?Txg1K9-o3tvEZ{r~^~ literal 0 HcmV?d00001 diff --git a/plugins/badfacts.pm b/plugins/badfacts.pm deleted file mode 100644 index 843ef22..0000000 --- a/plugins/badfacts.pm +++ /dev/null @@ -1,65 +0,0 @@ -package Bot::BB3::Plugin::Badfacts; -use strict; -no warnings 'void'; - -use Data::Dumper; - -sub new { - my( $class ) = @_; - my $self = bless {}, $class; - $self->{name} = 'badfacts'; - $self->{opts} = { - handler => 1, - }; - - return $self; -} - -sub handle { - my ($self, $said, $pm) = @_; - - if ($said->{body} =~ /^!(?[^@].*?)(?:\s@\s*(?\S*))?$/ || - $said->{body} =~ /^!@(?\S+)\s+(?.+)$/) { - my $fact = $+{fact}; - my $user = $+{user}; - - # TODO HACK XXX bad hack to prevent noise until better way is decided - return ('', 'handled') if $fact =~ /^regex\s/; - - my ($s, $r) = runfacts($fact, $said, $pm); - if ($s) { - $r = "$user: $r" if $user; - $r = "\0".$r; - return ($r, 'handled'); - } - } - - return; -} - -sub runfacts { - my( $body, $_said, $pm ) = @_; - - my $said = {%$_said}; - - my $plugin = $pm->get_plugin( 'fact' ); - - $said->{body} = $body; - $said->{recommended_args} = [ split /\s+/, $body ]; - $said->{command_match} = 'fact'; - $said->{addressed} = 1; - - local $@; - my( $status, $results ) = eval { $plugin->command( $said, $pm ) }; - my $err = $@; - - warn $err if $err; - - if( $err ) { return( 0, "Failed to execute plugin: facts because $err" ); } - else { return( 1, $results ) } -} - -"Bot::BB3::Plugin::Badfacts"; - -__DATA__ -Supports calling factoids outside of addressed commands using special syntax. Contact simcop2387 to ask about having it enabled for your channel. diff --git a/plugins/badfactstemp.pm b/plugins/badfactstemp.pm deleted file mode 100644 index c12671e..0000000 --- a/plugins/badfactstemp.pm +++ /dev/null @@ -1,87 +0,0 @@ -package Bot::BB3::Plugin::BadfactsTemp; -use strict; -no warnings 'void'; - -use Data::Dumper; - -sub new { - my( $class ) = @_; - my $self = bless {}, $class; - $self->{name} = 'badfactstemp'; - $self->{opts} = { - handler => 1, - command => 1, - }; - - return $self; -} - -sub command { - my ($self, $said, $pm) = @_; - - # Always require this to be addressed, regardless of plugin config - if ($said->{addressed}) { - my $body = $said->{body}; - - if ($body =~ /^#(?\S+)\s+(?:(?forget|learn|relearn|literal|revert|revisions|search|protect|unprotect|substitue|macro)\s+)?(?.*?)$/) { - my ($command, $channel, $fact) = @+{qw/command channel fact/}; - - my $realfact = ($command?"$command ":"")."__${channel}_$fact"; - - return ('handled', $realfact); - } else { - return ('handled', "helptexthere"); - } - } - - return; -} - -sub handle { - my ($self, $said, $pm) = @_; - - if ($said->{body} =~ /^!(?[^@].*?)(?:\s@\s*(?\S*))?$/ || - $said->{body} =~ /^!@(?\S+)\s+(?.+)$/) { - my $fact = $+{fact}; - my $user = $+{user}; - - # TODO HACK XXX bad hack to prevent noise until better way is decided - return ('', 'handled') if $fact =~ /^regex\s/; - - my ($s, $r) = runfacts($fact, $said, $pm); - if ($s) { - $r = "$user: $r" if $user; - $r = "\0".$r; - return ($r, 'handled'); - } - } - - return; -} - -sub runfacts { - my( $body, $_said, $pm ) = @_; - - my $said = {%$_said}; - - my $plugin = $pm->get_plugin( 'fact' ); - - $said->{body} = $body; - $said->{recommended_args} = [ split /\s+/, $body ]; - $said->{command_match} = 'fact'; - $said->{addressed} = 1; - - local $@; - my( $status, $results ) = eval { $plugin->command( $said, $pm ) }; - my $err = $@; - - warn $err if $err; - - if( $err ) { return( 0, "Failed to execute plugin: facts because $err" ); } - else { return( 1, $results ) } -} - -"Bot::BB3::Plugin::BadfactsTemp"; - -__DATA__ -Supports calling factoids outside of addressed commands using special syntax. Contact simcop2387 to ask about having it enabled for your channel. diff --git a/plugins/factoids.pm b/plugins/factoids.pm index 51d30bc..0010feb 100644 --- a/plugins/factoids.pm +++ b/plugins/factoids.pm @@ -117,12 +117,13 @@ sub command { #i lost the object oriented calling here, but i don't care too much, BECAUSE this avoids using strings for the calling, i might change that. $fact_string = $commandhash{$1}->($self,$subject, $said->{name}, $said); } - elsif (($subject =~ m|^\s*(.*?)\s*=~\s*s/([^/]+)/([^/]*)/([gi]*)|i) || - ($subject =~ m/^\s*(.*?)\s*=~\s*s\{(.+)\}\{(.*)\}([gi]*)/i)) + elsif (($subject =~ m|^\s*(.*?)\s*=~\s*s/([^/]+)/([^/]*)/([gi]*)\s*$|i) || + ($subject =~ m/^\s*(.*?)\s*=~\s*s\{(.+)\}\{(.*)\}([gi]*)\s*$/i)) { $fact_string = $self->get_fact_substitute( $subject, $said->{name}, $said); } elsif( !$call_only and $subject =~ /\s+$COPULA_RE\s+/ ) { + return if $said->{nolearn}; my @ret = $self->store_factoid( $said ); $fact_string = "Failed to store $said->{body}" unless @ret; @@ -441,6 +442,8 @@ sub get_fact_revert { sub get_fact_learn { my( $self, $body, $name, $said, $subject, $predicate ) = @_; + return if ($said->{nolearn}); + $body =~ s/^\s*learn\s+//; ($subject, $predicate ) = split /\s+as\s+/, $body, 2 unless ($subject && $predicate); @@ -620,6 +623,8 @@ sub basic_get_fact { my $matches = $self->_soundex_matches( $soundex ); + push @{$said->{soundex_matches}}, @$matches; + if( ($matches and @$matches) && (!$said->{backdressed}) ) { return "No factoid found. Did you mean one of these: " . join " ", map "[$_]", @$matches; } diff --git a/plugins/nfacts.pm b/plugins/nfacts.pm new file mode 100644 index 0000000..e405c55 --- /dev/null +++ b/plugins/nfacts.pm @@ -0,0 +1,170 @@ +package Bot::BB3::Plugin::NFacts; +use strict; +no warnings 'void'; + +use Data::Dumper; + +our $pname = "nfacts"; +my $fsep = "\034"; # ASCII file seperator + +sub get_conf_for_channel { + my ($self, $pm, $server, $channel) = @_; + my $conf = $pm->plugin_conf($pname, $server, $channel); + return $conf; +} + +sub namespace_factoid { + my ($self, $pm, $body, $said, $forcechan, $forceserver) = @_; + + my ($channel, $server) = @{$said}{qw/channel server/}; + $server =~ s/^.*?([^.]+\.(?:com|net|co.uk|org|bot|info))$/$1/; # grab just the domain and tld, will expand to more domains later + + $channel = $forcechan // $channel; + $server = $forceserver // $server; + + warn "NAMESPACE: [ $channel , $server ]"; + + my $conf = $self->get_conf_for_channel($pm, $said->{server}, $channel); + + warn Dumper($conf); + + my $realserver = $conf->{serverspace} // $server; + my $realchannel = $conf->{chanspace} // $channel; + + if ($body =~ /^(?:(?forget|learn|relearn|literal|revert|revisions|search|protect|unprotect|substitue|macro)\s+)?(?.*?)$/) { + my ($command, $fact) = @+{qw/command fact/}; + $body = "${command} ${fsep}${realserver}${fsep}${realchannel}${fsep}${fact}"; + } else { + $body = "${fsep}${realserver}${fsep}${realchannel}${fsep}${body}"; + } + + return $body; +} + +sub namespace_filter { + my ($body, $enabled) = @_; + + return $body =~ s|$fsep.*?$fsep.*?$fsep(\S+)(?=\s)|$1|rg if $enabled; + $body; +} + +sub new { + my( $class ) = @_; + my $self = bless {}, $class; + $self->{name} = $pname; + $self->{opts} = { + handler => 1, + command => 1, + }; + + return $self; +} + +sub command { + my ($self, $_said, $pm) = @_; + my $said = {%$_said}; # copy it so we can mutate it later + my $conf = $self->get_conf_for_channel($pm, $said->{server}, $said->{channel}); + + # Always require this to be addressed, regardless of plugin config + if ($said->{addressed}) { + my $body = $said->{body}; + + $body =~ s/^\s*$pname\s*//; + $body =~ s/^\s+|\s+$//g; + + if ($body =~ /^(?#\S+)\s+(?.*)$/) { + my ($channel, $fact) = @+{qw/channel fact/}; + + $said->{channel} = $channel; + my ($status, $result) = $self->runfacts($fact, $said, $pm, $conf); + return ('handled', $result); + } else { + my ($status, $result) = $self->runfacts($body, $said, $pm, $conf); + return ('handled', $result); + } + } + + return; +} + +sub handle { + my ($self, $said, $pm) = @_; + my $conf = $self->get_conf_for_channel($pm, $said->{server}, $said->{channel}); + + return unless $conf->{enabled}; + + my $prefix = $conf->{prefix} || "!"; + + my $regex = qr/^\Q$prefix\E(?[^@].*?)(?:\s@\s*(?\S*))?$/; + + if ($said->{body} =~ /^\Q$prefix\E(?[^@].*?)(?:\s@\s*(?\S*))?$/ || + $said->{body} =~ /^\Q$prefix\E!@(?\S+)\s+(?.+)$/) { + my $fact = $+{fact}; + my $user = $+{user}; + +# return (Dumper($said), "handled"); + + my ($s, $r) = $self->runfacts($fact, $said, $pm, $conf); + if ($s) { + $r = "$user: $r" if $user; + $r = "\0".$r; + return ($r, 'handled'); + } + } + + return; +} + +sub runfacts { + my( $self, $body, $_said, $pm, $conf, $server, $channel ) = @_; + + my $said = {%$_said}; + my @suggests; + + my $plugin = $pm->get_plugin( 'fact' ); + + $said->{body} = $self->namespace_factoid($pm, $body, $said, $channel, $server); + $said->{recommended_args} = [ split /\s+/, $said->{body} ]; + $said->{command_match} = 'fact'; + $said->{nolearn} = !($_said->{addressed}); # Only learn if we were addressed originally + $said->{addressed} = 1; + $said->{backdressed} = 1; + + local $@; + my( $status, $results ) = eval { $plugin->command( $said, $pm ) }; + my $err = $@; + + push @suggests, @{$said->{soundex_matches} // []}; + + if (!$results || !$status || $err) { + $said->{body} = $body; + $said->{recommended_args} = [ split /\s+/, $said->{body} ]; + $said->{nolearn} = 1; # never learn a global this way + delete $said->{soundex_matches}; + + ( $status, $results ) = eval { $plugin->command( $said, $pm ) }; + $err = $@; + + push @suggests, @{$said->{soundex_matches} // []}; + } + + warn $err if $err; + + if( $err ) { + return( 0, "Failed to execute plugin: facts because $err" ); + } else { + if (!$status && !$results) { + # Do suggests from here to cross namespaces + #return( 1, "[" . join(", ", @suggests) . "]" ); + return(1, ""); + } else { + return( 1, namespace_filter($results, $conf->{filtersep}) ); + } + } +} + +"Bot::BB3::Plugin::NFacts"; + +__DATA__ +nfacts [#channel] - Use/set a factoid for/from a channel on this server. +Also supports calling factoids outside of addressed commands using special syntax. Contact simcop2387 to ask about having it enabled for your channel.