Permalink
Browse files

Backport deer.pm from services-detox deerserv module

  • Loading branch information...
1 parent 281235d commit 5f47a588b947b92606b8a2fbd22f44769b6689a9 @incognico committed Jun 22, 2013
Showing with 245 additions and 203 deletions.
  1. +245 −0 modules/deer.pm
  2. +0 −88 modules/deerkill.pm
  3. +0 −115 modules/deerme.pm
View
245 modules/deer.pm
@@ -0,0 +1,245 @@
+package deer;
+
+use utf8;
+use strict;
+use warnings;
+
+use DBI;
+
+### start config
+
+my @cankill = qw(nico!nico@korea-dpr.org);
+my $deerchan = '#idletalk';
+my $deeritor = '<temporarily unavailable>';
+my $maxsearch = 20;
+
+my %sql = (
+ host => '',
+ db => '',
+ table => '',
+ user => '',
+ pass => '',
+);
+
+### end config
+
+my $dbh;
+my %prevdeer;
+
+### functions
+
+sub mysql_connect {
+ unless ($dbh = DBI->connect("DBI:mysql:$sql{db}:$sql{host}", $sql{user}, $sql{pass}, {mysql_auto_reconnect => 1, mysql_enable_utf8 => 1})) {
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+
+sub mysql_disconnect {
+ $dbh->disconnect;
+}
+
+sub new {
+ my ($package, %self) = @_;
+ my $self = bless(\%self, $package);
+
+ return $self;
+}
+
+sub countdeer {
+ return 1 unless (mysql_connect() == 0);
+
+ my $result = $dbh->selectrow_arrayref("SELECT count(*) as cnt FROM $sql{table}", {});
+
+ mysql_disconnect();
+
+ unless ($result) {
+ return 2;
+ }
+ else {
+ return 0, @$result[0];
+ }
+}
+
+sub fetchdeer {
+ my $deer = shift;
+ my $ucdeer = uc($deer);
+ my ($stmt, @bind, $special, $creator);
+
+ if ($ucdeer eq 'RANDOM') {
+ $special = 1;
+ $stmt = "SELECT creator,irccode,deer FROM $sql{table} ORDER BY RAND() DESC LIMIT 1";
+ }
+ elsif ($ucdeer eq 'LATEST') {
+ $special = 1;
+ $stmt = "SELECT creator,irccode,deer FROM $sql{table} ORDER BY date DESC LIMIT 1";
+ }
+ else {
+ $stmt = "SELECT creator,irccode,deer FROM $sql{table} WHERE deer = ? ORDER BY id DESC LIMIT 1";
+ @bind = $deer;
+ }
+
+ return 1 unless (mysql_connect() == 0);
+
+ my $result = $dbh->selectrow_arrayref($stmt, {}, @bind);
+
+ mysql_disconnect();
+
+ unless ($result) {
+ return 2;
+ }
+ else {
+ return 0, @$result[0], @$result[1], @$result[2], $special;
+ }
+}
+
+sub killdeer {
+ my $deer = shift;
+
+ return 1 unless (mysql_connect() == 0);
+
+ my $rows = $dbh->do("DELETE FROM $sql{table} WHERE deer = ?", {}, $deer);
+
+ mysql_disconnect();
+
+ unless (defined $rows) {
+ return 2;
+ }
+ else {
+ return 0, $rows;
+ }
+}
+
+sub searchdeer {
+ my $search = shift;
+ my $stmt = "SELECT deer FROM $sql{table} WHERE deer LIKE ? ORDER BY id DESC LIMIT ?";
+ my @bind = ("%$search%", $maxsearch);
+
+ return 1 unless (mysql_connect() == 0);
+
+ my $result = $dbh->selectall_arrayref($stmt, {}, @bind);
+
+ mysql_disconnect();
+
+ if (@$result) {
+ my ($count, $output) = scalar(@$result);
+
+ $output .= $_->[0] . ', ' for (@$result);
+
+ return 0, $count, substr($output, 0, -2);
+ }
+ else {
+ return 2;
+ }
+}
+
+### hooks
+
+sub on_join {
+ my ($self, $chan, $nick, undef, undef, undef) = @_;
+
+ return if ($chan ne $deerchan);
+
+ my ($ret, $creator, $irccode, $deer, $special) = fetchdeer('random');
+
+ if ($ret == 1) {
+ main::msg($chan, 'Hello %s, I was hit by a car :(', $nick);
+ }
+ else {
+ main::msg($chan, 'Hello %s, have a seat and a deer:', $nick);
+ main::msg($chan, $irccode);
+
+ $prevdeer{$chan}{deer} = $deer;
+ $prevdeer{$chan}{creator} = $creator;
+ }
+}
+
+sub on_privmsg {
+ my ($self, $target, $msg, undef, undef, undef, undef, $who) = @_;
+
+ return if ($target ne $deerchan);
+
+ my @args = split(' ', $msg);
+ my $cmd = uc(shift(@args));
+
+ if ($cmd eq 'DEER') {
+ my ($ret, $creator, $irccode, $deer, $special) = fetchdeer($args[0] ? $args[0] : 'random');
+
+ if ($ret == 1) {
+ main::msg($target, 'deer: database error');
+ }
+ elsif ($ret == 2) {
+ main::msg($target, '404 Deer Not Found. Go to %s and create it.', $deeritor);
+ } else {
+ main::msg($target, $irccode);
+ main::msg($target, q{'%s'%s}, $deer, $creator eq 'You' || $creator eq 'n/a' ? '' : " by $creator") if ($special);
+
+ $prevdeer{$target}{deer} = $deer;
+ $prevdeer{$target}{creator} = $creator;
+ }
+ }
+ elsif ($cmd eq 'DEERSEARCH') {
+ if (length($args[0]) < 2) {
+ main::msg($target, 'deerearch: specify 2 or more characters');
+ }
+ else {
+ my ($ret, $count, $deertring) = searchdeer($args[0]);
+
+ if ($ret == 1) {
+ main::msg($target, 'deerearch: database error');
+ }
+ elsif ($ret == 2) {
+ main::msg($target, 'deerearch: no result');
+ }
+ else {
+ unless ($count) {
+ main::msg($target, 'no deer');
+ }
+ elsif ($count == 1) {
+ main::msg($target, 'one happy deer: %s', $deertring);
+ }
+ elsif ($count >= $maxsearch) {
+ main::msg($target, 'too many deer, have some: %s', $deertring);
+ }
+ else {
+ main::msg($target, '%u deer: %s', $count, $deertring);
+ }
+ }
+ }
+ }
+ elsif ($cmd eq 'DEERKILL') {
+ return unless($who ~~ @cankill);
+
+ my ($ret, $killed) = killdeer($args[0]);
+
+ if ($ret == 1) {
+ main::msg($target, 'deerkill: database error');
+ }
+ elsif ($ret == 2) {
+ main::msg($target, 'no deer killed');
+ }
+ else {
+ main::msg($target, '%d deer killed', $killed);
+ }
+ }
+ elsif ($cmd eq 'DEERCOUNT') {
+ my ($ret, $count) = countdeer();
+
+ if ($count == 1) {
+ main::msg($target, 'deercount: database error');
+ }
+ elsif ($count == 2) {
+ main::msg($target, 'deercount: no result');
+ }
+ else {
+ main::msg($target, 'there are %u deer', $count);
+ }
+ }
+ elsif ($cmd eq 'PREVDEER') {
+ main::msg($target, q{'%s'%s}, $prevdeer{$target}{deer}, $prevdeer{$target}{creator} eq 'You' || $prevdeer{$target}{creator} eq 'n/a' ? '' : " by $prevdeer{$target}{creator}") if (exists $prevdeer{$target});
+ }
+}
+
+1;
View
88 modules/deerkill.pm
@@ -1,88 +0,0 @@
-package deerkill;
-
-use utf8;
-use strict;
-use warnings;
-
-use DBI;
-
-my $mytrigger;
-
-my $dbh;
-
-### start config
-
-my %sql = (
- host => '',
- db => '',
- table => '',
- user => '',
- pass => '',
-);
-
-### end config
-
-### functions
-
-sub new {
- my ($package, %self) = @_;
- my $self = bless(\%self, $package);
-
- $mytrigger = $self->{mytrigger};
-
- return $self;
-}
-
-sub mysql_connect {
- unless ($dbh = DBI->connect("DBI:mysql:$sql{db}:$sql{host}", $sql{user}, $sql{pass}, {mysql_auto_reconnect => 1, mysql_enable_utf8 => 1})) {
- printf("[%s] !!! modules::%s: %s\n", scalar localtime, __PACKAGE__, $DBI::errstr);
- return 1;
- }
- else {
- return 0;
- }
-}
-
-sub mysql_disconnect {
- $dbh->disconnect;
-}
-
-### hooks
-
-sub on_privmsg {
- my ($self, $target, $msg, $ischan, $nick, undef, undef, $who) = @_;
-
- if (substr($msg, 0, 1) eq $$mytrigger) {
- my @args = split(' ', $msg);
- my $cmd = uc(substr(shift(@args), 1));
-
- return unless (main::isadmin($who));
-
- $target = $nick unless ($ischan);
-
- # cmds
- if ($cmd eq 'DEERKILL' || $cmd eq 'DK') {
- if ($args[0]) {
- unless (mysql_connect() == 0) { main::err($target, 'database error');
- return 1; }
-
- my $rows = $dbh->do("DELETE FROM $sql{table} WHERE deer = ?", {}, "@args");
-
- mysql_disconnect();
-
- unless ($rows == 0) {
- printf("[%s] === modules::%s: deer killed [%s] on %s by %s\n", scalar localtime, __PACKAGE__, $args[0], $target, $nick);
- main::msg($target, '%d deer killed', $rows);
- }
- else {
- main::msg($target, 'no deers killed');
- }
- }
- else {
- main::hlp($target, 'syntax: DEERKILL(DK) <deer name>');
- }
- }
- }
-}
-
-1;
View
115 modules/deerme.pm
@@ -1,115 +0,0 @@
-package deerme;
-
-use utf8;
-use strict;
-use warnings;
-
-use DBI;
-
-my $mytrigger;
-
-my $dbh;
-my %prevdeers;
-
-### start config
-
-my $deeritor = 'http://example.com/deeritor';
-
-my %sql = (
- host => '',
- db => '',
- table => '',
- user => '',
- pass => '',
-);
-
-### end config
-
-### functions
-
-sub new {
- my ($package, %self) = @_;
- my $self = bless(\%self, $package);
-
- $mytrigger = $self->{mytrigger};
-
- return $self;
-}
-
-sub mysql_connect {
- unless ($dbh = DBI->connect("DBI:mysql:$sql{db}:$sql{host}", $sql{user}, $sql{pass}, {mysql_auto_reconnect => 1, mysql_enable_utf8 => 1})) {
- printf("[%s] !!! modules::%s: %s\n", scalar localtime, __PACKAGE__, $DBI::errstr);
- return 1;
- }
- else {
- return 0;
- }
-}
-
-sub mysql_disconnect {
- $dbh->disconnect;
-}
-
-sub fetchdeer {
- my $deer = shift || return;
- my $ucdeer = uc($deer);
- my ($stmt, @bind, $special, $creator);
-
- if ($ucdeer eq 'RANDOM') {
- $special = 1;
- $stmt = "SELECT creator,irccode,deer FROM $sql{table} ORDER BY RAND() DESC LIMIT 1";
- }
- elsif ($ucdeer eq 'LATEST') {
- $special = 1;
- $stmt = "SELECT creator,irccode,deer FROM $sql{table} ORDER BY date DESC LIMIT 1";
- }
- else {
- $stmt = "SELECT creator,irccode,deer FROM $sql{table} WHERE deer = ? ORDER BY id DESC LIMIT 1";
- @bind = $deer;
- }
-
- return 2 unless (mysql_connect() == 0);
-
- my $result = $dbh->selectrow_arrayref($stmt, {}, @bind);
-
- mysql_disconnect();
-
- unless ($result) {
- return 1;
- }
- else {
- return @$result[0], @$result[1], @$result[2], $special;
- }
-}
-
-### hooks
-
-sub on_privmsg {
- my ($self, $target, $msg, $ischan, $nick, undef, undef, undef) = @_;
-
- # cmds
- if ($msg =~ /^deer (.+)/) {
- my ($creator, $irccode, $deer, $special) = fetchdeer($1);
-
- if ($creator eq 2) {
- main::err($target, 'database error');
- }
- elsif ($creator eq 1) {
- main::msg($target, '404 Deer Not Found. Go to %s and create it.', $deeritor);
- }
- else {
- printf("[%s] === modules::%s: Deer [%s] on %s for %s\n", scalar localtime, __PACKAGE__, $1, $target, $nick);
-
- main::msg($target, $irccode);
- main::msg($target, '%s by %s', $deer, $creator) if ($special);
-
- $prevdeers{$target}{deer} = $deer;
- $prevdeers{$target}{creator} = $creator;
- }
- }
- elsif (lc($msg) eq "$$mytrigger prevdeer") {
- main::msg($target, 'The previous deer to walk the earth was %s by %s', $prevdeers{$target}{deer}, $prevdeers{$target}{creator}) if exists $prevdeers{$target};
- }
-}
-
-1;

0 comments on commit 5f47a58

Please sign in to comment.