Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PipSqueek::Plugin::Freezer;
- use base qw(PipSqueek::Plugin);
- use DBI;
- use DBD::mysql;
- use Time::localtime;
- sub plugin_initialize {
- my $self = shift;
- # CONFIG VARIABLES
- my $platform = "mysql";
- my $database = "orbiter_radio";
- my $host = "localhost";
- my $port = "33 something";
- my $tablename = "freezer";
- my $user = "Radiobot";
- my $pass = "Yeah, right...";
- # A few variables used in all functions.
- my $lock = "unlocked";
- my $temp = "200";
- # set the data source name
- my $dsn = "dbi:$platform:$database:$host:$port";
- # now connect and get a database handle
- $dbh = DBI->connect($dsn, $user, $pass)
- or die $DBI::errstr;
- # Array holds comments for when someone tries to pull the penguin out
- $Penguin[1] = "The Penguin Resists!";
- $Penguin[2] = "Uhh... I don't think he wants to move right now.";
- $Penguin[3] = "Yeah, he's WAY too busy fapping for that.";
- $Penguin[4] = "Not a chance in hell. He'd just try to buttfuck me or something.";
- $Penguin[5] = "He'll come out when he's ready... or something.";
- $Penguin[6] = "I'm... I'm scared he'll hurt me";
- $Penguin[7] = "It's too warm out here. He'd melt. Best leave him in there.";
- # Commands we accept for the freezer and the source we accept them from
- $self->plugin_handlers({
- 'multi_freezer' => 'freezer', # list items in freezer. Needs limiting to avoid floods.
- 'multi_freezer+' => 'freezer_add', # add item to freezer. May need an item-exists check.
- 'multi_freezer-' => 'freezer_rem', # remove item from freezer.
- 'multi_tape' => 'freezer_tape', # Taping people and things to the outside of the freezer.
- });
- }
- sub freezer {
- my ($self, $message) = @_;
- #check to see if we got an argument for this
- my @check = split(/\s+/, $message->command_input()); # Splits on a space to deliniate args
- my $nick = $message->{nick}; # Grab the nick who sent us the message
- if ( lc($check[0]) =~ /^[+-]?\d+$/) # Check for a number, if so give item details list
- {
- # Query db for info on item number
- my $query = 'SELECT * FROM freezer WHERE listnum = ?';
- my $sth = $dbh->prepare($query);
- #run the query
- $sth->execute($check[0]);
- # Format and output to buffer for dumping.
- @string = ("Item $check[0] details");
- while(@row = $sth->fetchrow_array())
- {
- my $buffer = "Name: $row[2]; Added by: $row[3] on $row[4]";
- @string = (@string, $buffer);
- }
- # Print string to message and exit done.
- my $output = join ('; ', @string);
- return $self->respond($message, $output);
- }
- # Not a number, some other command.
- else {
- if ( lc ($check[0]) eq "nuke") # Freezer Nuke!
- {
- if ($nick eq "Xyon") # Only useable by this nick. Not much use on servers with no nickserv protection.
- {
- # The nuke query, delete all but the first two (TSPenguin & Jolene) from freezer.
- my $query = 'DELETE FROM freezer WHERE listnum > 2';
- my $sth = $dbh->prepare($query);
- # Run the query.
- $sth->execute();
- # Output confimation and exit.
- return $self->respond($message, "Freezer nuked. Only the Penguin and his fappage material survived.");
- }
- # If they're not the authorised nuke user tell them they can't use the command
- else {
- return $self->respond($message, "Sorry, you lack the access to use this command.");
- }
- }
- if ( lc($check[0]) eq "lock") # Freezer lock command, toggle.
- {
- if ($nick eq "Xyon") # Again, check for access based on username
- {
- if ($lock eq "unlocked") # Toggling, so we need to poll the current state.
- {
- $lock = "locked"; # If it's not locked, lock it and tell the channel we did it
- return $self->respond($message, "\x01ACTION locks the freezer\x01");
- }
- else
- {
- $lock = "unlocked"; # And vice versa.
- return $self->respond($message, "\x01ACTION unlocks the freezer\x01");
- }
- }
- else # Unauthorised user does not have key.
- {
- return $self->respond($message, "You don't have the key.");
- }
- }
- if (lc($check[0]) eq "temp") # Freezer Temperature command.
- {
- if (lc($check[1]) eq "") # No number catch, returns to default temp.
- {
- $temp = "200";
- return $self->respond($message, "Freezer temperature returned to default.");
- }
- if (lc($check[1]) =~ /^[+-]?\d+$/) # Catch the number and parse it.
- {
- if (lc($check[1]) > 255) # Temp upper limit...
- {
- return $self->respond($message, "What? Freezers don't go that high, fuckwit.");
- }
- if (lc($check[1]) < 0) # ... and lower. Could use config file vars here for less hardcoding.
- {
- return $self->respond($message, "Brush up on your physics. Especially the bit about absolute zero.");
- }
- $temp = $check[1]; # If the number is good, use it.
- return $self->respond($message, "Freezer temperature now $temp K.");
- }
- else { # If not, spit out and die.
- return $self->respond($message, "Not a valid temperature.");
- }
- }
- }
- # None of the above match or we didn't get an arg, spew the freezer list.
- # Query db for items and who added them (We use rows 2, 3, and 4 for this list).
- my $query = 'SELECT * FROM freezer';
- my $sth = $dbh->prepare($query);
- # run the query
- $sth->execute()
- or die "Fail."; # REALLY informative error :P
- # format and display query result.
- @string = ("Items in Xyon's freezer at the moment include"); # List header.
- while(@row = $sth->fetchrow_array()) # Dump the information to a buffer variable.
- {
- my $buffer = "$row[1]: $row[2] ($row[3])"; # Machines count from 0! I keep forgetting...
- @string = (@string, $buffer); # append the buffer to the array which holds temp output...
- }
- @string = (@string, "And whoever's duct-taped to the outside. (Freezer is $lock and is at $temp K.)"); # List tail
- my $output = join ('; ', @string); # Glue the multiple lines into one big one and spew to channel
- return $self->respond($message, $output); # Done.
- }
- sub freezer_add {
- my ($self,$message)=@_;
- my $item = $message->command_input(); # Takes the item they want to add.
- $item =~ tr/a-zA-Z0-9//cd; # Checking for stupid chars; Blame Lambo.
- my $check = lc($item); # Copy to second var for checking.
- my $nick = $message->{nick}; # For the "who added this" data
- my $tm = localtime; # Get the date and time. Works in whatever timezone the bot runs in.
- my ($DAY, $MONTH, $YEAR, $HOUR, $MINUTE, $SECOND) = ($tm->mday, $tm->mon+1, $tm->year+1900, $tm->hour, $tm->min, $tm->sec);
- # Check if the freezer is locked.
- if ($lock eq "locked")
- {
- return $self->respond($message, "Sorry, the freezer is locked.");
- }
- # list number sorting
- my $query = 'SELECT listnum FROM freezer ORDER BY listnum desc limit 1';
- my $sth = $dbh->prepare($query);
- # A bit of a hackjob, but it works...
- $sth->execute();
- my $listnumtemp = $sth->fetchrow();
- my $listnum = ($listnumtemp)+1;
- #First, make sure they gave us an item to add
- if ($item eq "") {
- return $self->respond($message, "You have to actually give me an item to add to the freezer, dummy.");
- }
- # Test for sanity, stupidity, or both
- if ($check eq "freezer") {
- return $self->respond($message, "Sorry, but you don't have the authority to buttfuck physics like that, $nick");
- }
- if ($check eq "tspenguin")
- {
- return $self->respond($message, "What? He's already in there...");
- }
- if ($check eq "xyon")
- {
- return $self->respond($message, "... I can't do that. If he's in the freezer, who will play with my subroutines?");
- }
- if ($check eq "radiobotproto")
- {
- return $self->respond($message, "I don't want to get in there. It's full of penguin shit.");
- }
- # Make sure the freezer isn't full.
- $check = 'SELECT * FROM freezer WHERE listnum > 9';
- my $sthcheck = $dbh->prepare($check);
- # Run the query
- $sthcheck->execute();
- # Another hackjob, but it works...
- my $freezerfull = $sthcheck->fetchrow();
- if ($freezerfull != NULL)
- {
- return $self->respond($message, "Sorry, the freezer is full!"); # Can't add !
- }
- # freezer isn't full, carry on
- else {
- my $query = 'INSERT INTO freezer(listnum, item_name, added_by, added_on, removable) values (?,?,?,?,?)';
- my $sth = $dbh->prepare($query) or die "WTF $DBI::errstr;";
- $sth->execute("$listnum", "$item", "$nick", "$YEAR-$MONTH-$DAY $HOUR:$MINUTE:$SECOND", 1)
- or die "Failed to access database properly. Prod Xyon until he fixes it. $DBI::errstr\n";
- # Generally see that die string when someone tries to input strings too long for the field
- return $self->respond($message, "\x01ACTION shoves $item into the freezer.\x01"); # confirm.
- }
- }
- sub freezer_rem {
- my ($self,$message)=@_;
- my $item = $message->command_input(); # Takes the item they want to remove.
- # check for freezer lock
- if ($lock eq "locked")
- {
- return $self->respond($message, "Sorry, the freezer is locked.");
- }
- # number check
- if ( $item =~ /^[\+-]*[0-9]*\.*[0-9]*$/ && $item !~ /^[\. ]*$/ ) {
- # check for the penguin!
- if ($item eq "1")
- {
- my $range = 8;
- my $num = int(rand($range));
- return $self->respond($message, $Penguin[$num]);
- }
- # Check for Jolene
- if ($item eq "2")
- {
- return $self->respond($message, "The Penguin reminds you that removing his fappage material is unwise.");
- }
- # If they didn't tell us to take anything out...
- if ($item eq "") {
- return $self->respond($message, "Take it out? Take what out? WHAT?");
- }
- # Prepare the query; Just delete the row, it's easiest.
- my $query = 'SELECT * FROM freezer WHERE listnum = ?';
- my $sth = $dbh->prepare($query);
- my ($itemname, $itemexists);
- $sth->execute($item);
- while (@namecheck = $sth->fetchrow()) {
- $itemname = $namecheck[2]; # Pull the item name from the DB - since we can't after it's deleted and the arg is just a number.
- $itemexists = $namecheck[1]; # Checking to see if the item they want to take out is actually IN there
- }
- # If it doesn't...
- if ($itemexists eq "") {
- return $self->respond($message, "Sorry, I can't find that in the freezer.");
- }
- # Now we can run the actual delete.
- my $deljob = 'DELETE FROM freezer WHERE listnum = ?';
- my $sth1 = $dbh->prepare($deljob);
- # Run the query... lalalala
- $sth1->execute($item);
- # Shuffle the index to move the gap
- my $gapfind = 'SELECT listnum FROM freezer WHERE listnum > ?'; # Find everything with a listnumber higher than the one we just took out
- my $sth3 = $dbh->prepare($gapfind);
- $sth3->execute($item) or die "query fail";
- while (@row = $sth3->fetchrow()){
- my $oldnum = $row[0];
- my $newnum = ($oldnum)-1; # Decrement each listnum sequentially. The less there are the less this takes... with a big freezer this could cripple.
- my $gapkiller = 'UPDATE freezer SET listnum=? WHERE listnum=?';
- my $qr1 = $dbh->prepare($gapkiller);
- $qr1->execute($newnum, $oldnum);
- }
- # Confirm remove and finish
- return $self->respond($message, "\x01ACTION grabs $itemname and drags it out of the freezer.\x01");
- }
- # If it wasn't a number, they're probably trying to use the name of the item instead.
- else
- {
- return $self->respond($message, "Please use the number of the item.");
- }
- }
- sub freezer_tape {
- my ($self,$message)=@_;
- my $victim = $message->command_input(); # Takes the victim to be taped to the freezer.
- return $self->respond($message, "\x01ACTION tapes $victim to the freezer.\x01"); # confirm.
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement