Advertisement
Guest User

Untitled

a guest
May 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 11.34 KB | None | 0 0
  1. package PipSqueek::Plugin::Freezer;
  2. use base qw(PipSqueek::Plugin);
  3. use DBI;
  4. use DBD::mysql;
  5. use Time::localtime;
  6.  
  7. sub plugin_initialize {
  8.     my $self = shift;
  9.  
  10.     # CONFIG VARIABLES
  11.     my $platform = "mysql";
  12.     my $database = "orbiter_radio";
  13.     my $host = "localhost";
  14.     my $port = "33 something";
  15.     my $tablename = "freezer";
  16.     my $user = "Radiobot";
  17.     my $pass = "Yeah, right...";
  18.    
  19.     # A few variables used in all functions.
  20.     my $lock = "unlocked";
  21.     my $temp = "200";
  22.    
  23.     # set the data source name
  24.     my $dsn = "dbi:$platform:$database:$host:$port";
  25.  
  26.     # now connect and get a database handle
  27.     $dbh = DBI->connect($dsn, $user, $pass)
  28.             or die $DBI::errstr;
  29.  
  30.     # Array holds comments for when someone tries to pull the penguin out
  31.     $Penguin[1] = "The Penguin Resists!";
  32.     $Penguin[2] = "Uhh... I don't think he wants to move right now.";
  33.     $Penguin[3] = "Yeah, he's WAY too busy fapping for that.";
  34.     $Penguin[4] = "Not a chance in hell. He'd just try to buttfuck me or something.";
  35.     $Penguin[5] = "He'll come out when he's ready... or something.";
  36.     $Penguin[6] = "I'm... I'm scared he'll hurt me";
  37.     $Penguin[7] = "It's too warm out here. He'd melt. Best leave him in there.";
  38.    
  39.     # Commands we accept for the freezer and the source we accept them from
  40.     $self->plugin_handlers({
  41.         'multi_freezer'     =>  'freezer', # list items in freezer. Needs limiting to avoid floods.
  42.         'multi_freezer+'    =>  'freezer_add', # add item to freezer. May need an item-exists check.
  43.         'multi_freezer-'    =>  'freezer_rem', # remove item from freezer.
  44.         'multi_tape'        =>  'freezer_tape', # Taping people and things to the outside of the freezer.
  45.     });
  46. }
  47.  
  48. sub freezer {
  49.     my ($self, $message) = @_;
  50.    
  51.     #check to see if we got an argument for this
  52.     my @check = split(/\s+/, $message->command_input()); # Splits on a space to deliniate args
  53.     my $nick = $message->{nick}; # Grab the nick who sent us the message
  54.    
  55.     if ( lc($check[0]) =~ /^[+-]?\d+$/) # Check for a number, if so give item details list
  56.     {
  57.         # Query db for info on item number
  58.         my $query = 'SELECT * FROM freezer WHERE listnum = ?';
  59.         my $sth = $dbh->prepare($query);
  60.         #run the query
  61.         $sth->execute($check[0]);
  62.        
  63.         # Format and output to buffer for dumping.
  64.         @string = ("Item $check[0] details");
  65.         while(@row = $sth->fetchrow_array())
  66.         {
  67.             my $buffer = "Name: $row[2]; Added by: $row[3] on $row[4]";
  68.             @string = (@string, $buffer);
  69.         }
  70.         # Print string to message and exit done.
  71.         my $output = join ('; ', @string);
  72.         return $self->respond($message, $output);
  73.     }
  74.     # Not a number, some other command.
  75.     else {
  76.         if ( lc ($check[0]) eq "nuke") # Freezer Nuke!
  77.             {
  78.                 if ($nick eq "Xyon") # Only useable by this nick. Not much use on servers with no nickserv protection.
  79.                 {
  80.                     # The nuke query, delete all but the first two (TSPenguin & Jolene) from freezer.
  81.                     my $query = 'DELETE FROM freezer WHERE listnum > 2';
  82.                     my $sth = $dbh->prepare($query);
  83.                     # Run the query.
  84.                     $sth->execute();
  85.                     # Output confimation and exit.
  86.                     return $self->respond($message, "Freezer nuked. Only the Penguin and his fappage material survived.");
  87.                
  88.                 }
  89.                 # If they're not the authorised nuke user tell them they can't use the command
  90.                 else {
  91.                 return $self->respond($message, "Sorry, you lack the access to use this command.");
  92.                 }
  93.         }
  94.    
  95.         if ( lc($check[0]) eq "lock") # Freezer lock command, toggle.
  96.             {
  97.                 if ($nick eq "Xyon") # Again, check for access based on username
  98.                 {
  99.                     if ($lock eq "unlocked") # Toggling, so we need to poll the current state.
  100.                     {
  101.                         $lock = "locked"; # If it's not locked, lock it and tell the channel we did it
  102.                         return $self->respond($message, "\x01ACTION locks the freezer\x01");
  103.                     }
  104.                     else
  105.                     {
  106.                         $lock = "unlocked"; # And vice versa.
  107.                         return $self->respond($message, "\x01ACTION unlocks the freezer\x01");
  108.                     }
  109.                 }
  110.                 else # Unauthorised user does not have key.
  111.                 {
  112.                     return $self->respond($message, "You don't have the key.");
  113.                 }
  114.             }
  115.        
  116.         if (lc($check[0]) eq "temp") # Freezer Temperature command.
  117.         {
  118.             if (lc($check[1]) eq "") # No number catch, returns to default temp.
  119.             {
  120.                     $temp = "200";
  121.                     return $self->respond($message, "Freezer temperature returned to default.");
  122.             }
  123.             if (lc($check[1]) =~ /^[+-]?\d+$/) # Catch the number and parse it.
  124.             {
  125.                 if (lc($check[1]) > 255) # Temp upper limit...
  126.                 {
  127.                     return $self->respond($message, "What? Freezers don't go that high, fuckwit.");
  128.                 }
  129.                 if (lc($check[1]) < 0) # ... and lower. Could use config file vars here for less hardcoding.
  130.                 {
  131.                     return $self->respond($message, "Brush up on your physics. Especially the bit about absolute zero.");
  132.                 }
  133.            
  134.                 $temp = $check[1]; # If the number is good, use it.
  135.                 return $self->respond($message, "Freezer temperature now $temp K.");
  136.             }
  137.             else { # If not, spit out and die.
  138.                 return $self->respond($message, "Not a valid temperature.");
  139.             }
  140.         }
  141.     }
  142.    
  143.     # None of the above match or we didn't get an arg, spew the freezer list.
  144.     # Query db for items and who added them (We use rows 2, 3, and 4 for this list).
  145.     my $query = 'SELECT * FROM freezer';
  146.     my $sth = $dbh->prepare($query);
  147.     # run the query
  148.     $sth->execute()
  149.     or die "Fail."; # REALLY informative error :P
  150.    
  151.     # format and display query result.
  152.     @string = ("Items in Xyon's freezer at the moment include"); # List header.
  153.     while(@row = $sth->fetchrow_array())  # Dump the information to a buffer variable.
  154.     {
  155.         my $buffer = "$row[1]: $row[2] ($row[3])"; # Machines count from 0! I keep forgetting...
  156.         @string = (@string, $buffer); # append the buffer to the array which holds temp output...
  157.     }
  158.     @string = (@string, "And whoever's duct-taped to the outside. (Freezer is $lock and is at $temp K.)"); # List tail
  159.     my $output = join ('; ', @string); # Glue the multiple lines into one big one and spew to channel
  160.     return $self->respond($message, $output); # Done.
  161. }
  162.  
  163. sub freezer_add {
  164.    
  165.     my ($self,$message)=@_;
  166.     my $item = $message->command_input(); # Takes the item they want to add.
  167.     $item =~ tr/a-zA-Z0-9//cd; # Checking for stupid chars; Blame Lambo.
  168.     my $check = lc($item); # Copy to second var for checking.
  169.     my $nick = $message->{nick}; # For the "who added this" data
  170.    
  171.     my $tm = localtime; # Get the date and time. Works in whatever timezone the bot runs in.
  172.     my ($DAY, $MONTH, $YEAR, $HOUR, $MINUTE, $SECOND) = ($tm->mday, $tm->mon+1, $tm->year+1900, $tm->hour, $tm->min, $tm->sec);
  173.    
  174.     # Check if the freezer is locked.
  175.    
  176.     if ($lock eq "locked")
  177.     {
  178.         return $self->respond($message, "Sorry, the freezer is locked.");
  179.     }
  180.    
  181.     # list number sorting
  182.    
  183.     my $query = 'SELECT listnum FROM freezer ORDER BY listnum desc limit 1';
  184.     my $sth = $dbh->prepare($query);
  185.     # A bit of a hackjob, but it works...
  186.     $sth->execute();
  187.     my $listnumtemp = $sth->fetchrow();
  188.     my $listnum = ($listnumtemp)+1;
  189.  
  190.     #First, make sure they gave us an item to add
  191.    
  192.     if ($item eq "") {
  193.         return $self->respond($message, "You have to actually give me an item to add to the freezer, dummy.");
  194.     }
  195.     # Test for sanity, stupidity, or both
  196.     if ($check eq "freezer") {
  197.         return $self->respond($message, "Sorry, but you don't have the authority to buttfuck physics like that, $nick");
  198.     }
  199.    
  200.     if ($check eq "tspenguin")
  201.     {
  202.         return $self->respond($message, "What? He's already in there...");
  203.     }
  204.    
  205.     if ($check eq "xyon")
  206.     {
  207.         return $self->respond($message, "... I can't do that. If he's in the freezer, who will play with my subroutines?");
  208.     }
  209.    
  210.     if ($check eq "radiobotproto")
  211.     {
  212.         return $self->respond($message, "I don't want to get in there. It's full of penguin shit.");
  213.     }
  214.    
  215.     # Make sure the freezer isn't full.
  216.    
  217.     $check = 'SELECT * FROM freezer WHERE listnum > 9';
  218.     my $sthcheck = $dbh->prepare($check);
  219.     # Run the query
  220.     $sthcheck->execute();
  221.     # Another hackjob, but it works...
  222.     my $freezerfull = $sthcheck->fetchrow();
  223.    
  224.     if ($freezerfull != NULL)
  225.     {
  226.         return $self->respond($message, "Sorry, the freezer is full!"); # Can't add !
  227.     }
  228.    
  229.     # freezer isn't full, carry on
  230.     else {
  231.         my $query = 'INSERT INTO freezer(listnum, item_name, added_by, added_on, removable) values (?,?,?,?,?)';
  232.         my $sth = $dbh->prepare($query) or die "WTF $DBI::errstr;";
  233.    
  234.         $sth->execute("$listnum", "$item", "$nick", "$YEAR-$MONTH-$DAY $HOUR:$MINUTE:$SECOND", 1)
  235.         or die "Failed to access database properly. Prod Xyon until he fixes it. $DBI::errstr\n";
  236.         # Generally see that die string when someone tries to input strings too long for the field
  237.         return $self->respond($message, "\x01ACTION shoves $item into the freezer.\x01"); # confirm.
  238.     }
  239. }
  240.  
  241. sub freezer_rem {
  242.     my ($self,$message)=@_;
  243.     my $item = $message->command_input(); # Takes the item they want to remove.
  244.    
  245.     # check for freezer lock
  246.     if ($lock eq "locked")
  247.     {
  248.         return $self->respond($message, "Sorry, the freezer is locked.");
  249.     }
  250.     # number check
  251.     if ( $item =~ /^[\+-]*[0-9]*\.*[0-9]*$/ && $item !~ /^[\. ]*$/ ) {
  252.  
  253.         # check for the penguin!
  254.    
  255.         if ($item eq "1")
  256.         {
  257.             my $range = 8;
  258.    
  259.             my $num = int(rand($range));
  260.             return $self->respond($message, $Penguin[$num]);
  261.         }
  262.         # Check for Jolene
  263.         if ($item eq "2")
  264.         {
  265.             return $self->respond($message, "The Penguin reminds you that removing his fappage material is unwise.");
  266.         }
  267.         # If they didn't tell us to take anything out...
  268.         if ($item eq "") {
  269.             return $self->respond($message, "Take it out? Take what out? WHAT?");
  270.         }
  271.         # Prepare the query; Just delete the row, it's easiest.
  272.         my $query = 'SELECT * FROM freezer WHERE listnum = ?';
  273.         my $sth = $dbh->prepare($query);
  274.         my ($itemname, $itemexists);
  275.         $sth->execute($item);
  276.         while (@namecheck = $sth->fetchrow()) {
  277.             $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.
  278.             $itemexists = $namecheck[1]; # Checking to see if the item they want to take out is actually IN there
  279.         }
  280.         # If it doesn't...
  281.         if ($itemexists eq "") {
  282.             return $self->respond($message, "Sorry, I can't find that in the freezer.");
  283.         }
  284.         # Now we can run the actual delete.
  285.         my $deljob = 'DELETE FROM freezer WHERE listnum = ?';
  286.         my $sth1 = $dbh->prepare($deljob);
  287.         # Run the query... lalalala
  288.         $sth1->execute($item);
  289.  
  290.         # Shuffle the index to move the gap
  291.         my $gapfind = 'SELECT listnum FROM freezer WHERE listnum > ?'; # Find everything with a listnumber higher than the one we just took out
  292.         my $sth3 = $dbh->prepare($gapfind);
  293.         $sth3->execute($item) or die "query fail";
  294.        
  295.         while (@row = $sth3->fetchrow()){
  296.        
  297.             my $oldnum = $row[0];
  298.             my $newnum = ($oldnum)-1; # Decrement each listnum sequentially. The less there are the less this takes... with a big freezer this could cripple.
  299.        
  300.             my $gapkiller = 'UPDATE freezer SET listnum=? WHERE listnum=?';
  301.             my $qr1 = $dbh->prepare($gapkiller);
  302.             $qr1->execute($newnum, $oldnum);
  303.         }
  304.         # Confirm remove and finish
  305.         return $self->respond($message, "\x01ACTION grabs $itemname and drags it out of the freezer.\x01");
  306.     }
  307.    
  308.     # If it wasn't a number, they're probably trying to use the name of the item instead.
  309.     else
  310.     {
  311.         return $self->respond($message, "Please use the number of the item.");
  312.     }
  313. }
  314.  
  315. sub freezer_tape {
  316.     my ($self,$message)=@_;
  317.     my $victim = $message->command_input(); # Takes the victim to be taped to the freezer.
  318.    
  319.     return $self->respond($message, "\x01ACTION tapes $victim to the freezer.\x01"); # confirm.
  320. }
  321.  
  322. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement