commit b2b8298e78436b45bd4e6e53c748746f99cca3e5 from: Izzy Blacklock date: Wed May 17 04:58:03 2023 UTC added support for searching username or email users without a search returns count of users. commit - 7d9238c442e71ea7b906beb7d16e5550568ce326 commit + b2b8298e78436b45bd4e6e53c748746f99cca3e5 blob - 8b11009dd0a28d30842f9ae9f0225f182d36fecc blob + a596a0dcc4888321007d0101794ea518d3d730c2 --- lib/Bot/BasicBot/Pluggable/Module/BotnowDB.pm +++ lib/Bot/BasicBot/Pluggable/Module/BotnowDB.pm @@ -36,7 +36,6 @@ sub connectDB { exit 1; } main::debug(ALL, "connected to $dbpath"); - if (!defined($dbh)) { die "failed to connect to $dbpath"; } @@ -52,23 +51,29 @@ sub disconnectDB { sub listUsers { my $self=shift; my $msg=shift; - my $reply; + my $search=shift; # Third arg is search term + my $reply=1; if ($msg->{channel} ne 'msg') { # Notify we are replying in PM. $reply = "See Private Message"; } my $dbh=$self->connectDB(); - my $stmt=qq{SELECT DISTINCT username,email,password from bnc}; - my @lines; - for my $usr (@{$dbh->selectall_arrayref($stmt)}) { - my $seen = $self->bot->module('seen')->get("seen_$usr->[0]"); - $seen = {time => 'UNKNOWN'} if (not defined $seen); - next unless (defined $usr->[2]); - push @lines, qq{$usr->[0] : $usr->[1] : $seen->{time}}; + if ( defined $search and $search ne "") { + my $stmt = qq{ SELECT DISTINCT(username), email from bnc where username like ?}; + my @lines; + warn $search; + for my $usr (@{$dbh->selectall_arrayref($stmt, undef, "%". $search . "%")}) { + my $seen = $self->bot->module('seen')->get("seen_$usr->[0]"); + $seen = {time => 'UNKNOWN'} if (not defined $seen); + push @lines, qq{$usr->[0] : $usr->[1] : $seen->{time}}; + } + my $pm = join ("\n", @lines); + $self->say({who=>$msg->{who},channel=>'msg',body=>$pm}); + } else { + $reply = "Count: " . $dbh->selectrow_arrayref(qq{SELECT count( DISTINCT(username))from bnc})->[0]; + } $self->disconnectDB($dbh); - my $pm = join ("\n", @lines); - $self->say({who=>$msg->{who},channel=>'msg',body=>$pm}); return $reply; } @@ -76,11 +81,6 @@ sub getUser { my $self = shift; my $msg=shift; my $user = shift; - my $reply; - if ($msg->{channel} ne 'msg') { # Notify we are replying in PM. - $reply = "See Private Message"; - } - my $dbh = $self->connectDB(); my $stmt = qq{select username,email from bnc where username is ?}; my @userDetails = $dbh->selectrow_array($stmt, undef, $user); @@ -92,10 +92,24 @@ sub getUser { } $self->disconnectDB($dbh); $self->say({who=>$msg->{who},channel=>'msg',body=>$pm}); - return $reply; + if ($msg->{channel} ne 'msg') { # Notify we are replying in PM. + return "See Private Message"; + } + return 1; } +sub getEmail { + my $self = shift; + my $msg=shift; + my $email = shift; + my $dbh = $self->connectDB(); + my $stmt = qq{SELECT username from bnc where email is ?}; + my $users = $dbh->selectcol_arrayref($stmt, undef, $email); + my $pm = join (", ", @$users); + $self->disconnectDB($dbh); + $self->say({who=>$msg->{who}, channel=>'msg',body=>$pm}); +} # my $stmt=qq{select * from bnc join irc on (bnc.ircid = irc.id) where username is ?}; # my $sth=$dbh->prepare($stmt); # $sth->execute($username) or die "execution failed: $dbh->errstr()"; @@ -122,7 +136,7 @@ sub told { my %actions = ( users => sub { return $self->listUsers( @_ ) }, user => sub { return $self->getUser( @_ ) }, - + email => sub { return $self->getEmail( @_ ) }, ); if (!defined($actions{$cmds[0]})) { return $self->help();