Jump to content

simple oo issue..


tomfmason

Recommended Posts

I have been playin around with a bot for phpfreaks irc channel.. Well my first bot worked fine but it was a bit of a mess. So I decided to clean up the code a bit and place it in a couple classes..

Well now I am not able to send any commands.. I am sure that it is from the way that I am calling the commands from the parent class..

Here is the irc commands class..

[code]
<?php
class irc_commands{
  function message($type, $destination, $message) {
      switch ($type) {
  case "ME":
      return 'PRIVMSG ' . $destination . ' :' . chr(1) . 'ACTION' . $message . chr(1);
  break;
  case "REG":
    return 'PRIVMSG ' . $destination . ' :' . $message;
  break;
  }
  }
 
  function joinChannel($channels) {
      if (is_array($channels)) {
      $commands = array();
      foreach ($channels as $channel) {
      $commands[] = 'JOIN' . $channel;
  }
  return $commands;
  }else{
    return 'JOIN ' . $channels;
  }
  }

  function partChannel($channels) {
      if (is_array($channels)) {
      $commands = array();
      foreach($channels as $channel) {
    $commands[] = 'PART ' . $channel;
  }
  return $commands;
  } else {
      return 'PART ' . $channels;
  }
  }
 
  function kick($channel, $nicks, $reason) {
      if (is_array($nicks)) {
$commands = array();
foreach ($nicks as $nick) {
    $commands[] = 'KICK ' . $channel . ' ' . $nick . ' :' . $reason;
}
return $commands;
      } else {
return 'KICK' . $channel . ' ' . $nicks . ' :' . $reason;
      }
  }
 
  function ident($password) {
      return 'NICKSERV IDENTIFY ' . $password;
  }
 
  function quit($message) {
      return 'QUIT ' . $message;
  }    
}
?>
[/code]

Now I will post the entire irbot class and then I will break will show what I am having the issue..

[code]
<?php
class ircBot extends irc_commands{
  var $config = array('server' => 'irc.freenode.net',
                      'port' => 6667,
                      'host' => '',
                      'channel' => '#phpfreaks',
                      'nick' => '',
                      'name' => '',
                      'password' => '');

 
  var $keyCommands = array('quit','end','shut up','stfu','terminate','talk',
                      'time','version');

  var $con = array();

  function cmd_send($command, $con) {
      $put = fputs($con['sock'], "$command\r\n");
      if (!$put) {
      $error = 'Error : Unable to write to the socket';
          print $error . '\n\r';
  fwrite($con['fp'], $error, 4096);
        } else {
          print "$command\n\r";
  fwrite($con['fp'], $command . "\n", 4096);
      }
  }
 
  function startBot() {
      $this->con['fp'] = fopen('path\to\response.txt', 'r+b');
      $this->con['sock'] = fsockopen($this->config['server'], $this->config['port']);
  if (!$this->con['sock']) {
      $error = "Error: unable to connect to " . $this->config['server'] . '\n\r';
  print $error;
  fwrite($this->fp, $error, 4096);
  }else{
      while (!feof($this->con['sock'])) {
  $this->con['buffer']['all'] = trim(fgets($this->con['sock'], 4096));
  if (strpos($this->con['buffer']['all'], 'NOTICE AUTH :*** Checking ident')) {
      $this->cmd_send('NICK ' . $this->config['nick'], $this->con);
      $this->cmd_send('USER ' . $this->config['nick'] . ' ' . $this->config['host'] . ' ' . $this->config['server'] . ' :' . $this->config['name'], $this->con);
          } else if ($this->con['buffer']['all'] == ':NickServ!NickServ@services. NOTICE ' . $this->config['nick'] . ' :If this is your nickname, type /msg NickServ IDENTIFY <password>') {
    $command = parent::ident($this->config['password']);
    $this->cmd_send($command, $this->con);
  } else if ($this->con['buffer']['all'] == ':services. MODE ' . $this->config['nick'] . ' :+e') {
    $command = parent::joinChannels($this->config['channel']);
    $this->cmd_send($command, $this->con);
  } else if (strpos($this->con['buffer']['all'], $this->config['nick'] . ' !quit')) {
    $command = parent::quit("Don't hate me because I am bad ass");
    $this->cmd_send($command, $this->con);
  }
  print date("[d/m @ H:i]")."<- ". $this->con['buffer']['all'] . "\n";
  fwrite($this->con['fp'], $this->con['buffer']['all'] . "\n", 4096);
  if (substr($this->con['buffer']['all'], 0, 6) == 'PING :') {
      $this->cmd_send('PONG :'.substr($this->con['buffer']['all'], 0, 6), $this->con);
  }  
      }
}    
  }
}
?>[/code]

I am thinking that the problem is with way that I call the functions from the irc_commands class.. It will not send the iden function.. However, I know that cmd_send function works fine.. as I am able to respond to a ping from the server...

here is how I call the functions from the irc_commands class

[code=php:0]
$command = parent::joinChannels($this->config['channel']);
$this->cmd_send($command, $this->con);
[/code]

Any ideas as to what I am doing wrong..

Thanks,
Tom
Link to comment
https://forums.phpfreaks.com/topic/26907-simple-oo-issue/
Share on other sites

$con will not be changed in the class variable, it doesn't work like that, since your passing $con by value not reference...


Just make your function read the $con params directly from the class var...

It should be something like:

[code]$command = parent::joinChannels($this->config['channel']);
$this->cmd_send($command);[/code]

[code]  function cmd_send($command) {
      $put = fputs($this->con['sock'], "$command\r\n");
      if (!$put) {
      $error = 'Error : Unable to write to the socket';
          print $error . '\n\r';
  fwrite($con['fp'], $error, 4096);
        } else {
          print "$command\n\r";
  fwrite($con['fp'], $command . "\n", 4096);
      }
  }[/code]

Anyway I don't like OOP, but I think im on the right track...

Also for debugging purposes do a var-dump of $con

hth
Link to comment
https://forums.phpfreaks.com/topic/26907-simple-oo-issue/#findComment-123046
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.