php - IRC相关帮助

标签 php irc

现在我的机器人可以在机器人加入时发送消息。但是,如何制作一个可以发布数据的表单,以便机器人向 channel 说出消息?

这是我的脚本(已修改):

<?php
set_time_limit(0);

$socket = fsockopen("//", 6667) or die();

$msg = $_POST['message'];
$pr = $_POST['percentage'];
$pr /= 100;

fputs($socket,"USER BOT 0 zo :ZH bot\n");
// Set the bots nickname
fputs($socket,"NICK BOT1\n");

fputs($socket,"JOIN #bots\n");

while(1) {

   while($data = fgets($socket, 128)) {
      // echo the data received to page
      echo nl2br($data);
      // flush old data, it isn't needed any longer.
      flush();

      $ex = explode(' ', $data);

      if($ex[0] == "PING") fputs($socket, "PONG ".$ex[1]."\n");

      $search_string = "/^:([A-Za-z0-9_\-]+)[@!~a-zA-Z0-9@\.\-]+\s*([A-Z]+)\s*[:]*([\#a-zA-Z0-9\-]+)*\s*[:]*([!\#\-\.A-Za-z0-9 ]+)*/";

      $do = preg_match($search_string, $data, $matches);

      // check that there is a command received
      if(isset($matches['2'])) {
         switch($matches['2']) {
            case "PRIVMSG":
               $user = $matches['1'];
               $channel = $matches['3'];
               $chat_text = isset($matches['4']) ? $matches['4'] : "";

               // check chat for !time
               if(strtolower($chat_text) == "!time") { 
                  $output = "::::: " . date('l jS \of F Y h:i:s A') . " :::::";
                  fputs($socket, "PRIVMSG " . $channel . " :" . $output . "\n");
               } elseif(strtolower($chat_text) == "!hello") {
                  fputs($socket, "PRIVMSG " . $channel . " :Hello!\n");
               }
            break;

            case "JOIN":
               $user = $matches['1'];
               $channel = $matches['3'];
               fputs($socket, "PRIVMSG " . $channel . " :Welcome " . $user . " to " . $channel . "\n");
            break;
         }

      }
   }
}
?>

例如制作一个将数据发送到 IRC channel 的表单。输出将是“wget file info port”<-- 这将是发送到 IRC channel 的文本。

以下是相关部分:

fputs($socket, "PRIVMSG " . $channel . " :Welcome " . $user . " to " . $channel ."\n");

希望有人能帮忙。

最佳答案

好的,这是一个更好的答案。第一部分仍然存在。每次您想要启动新脚本时都会调用一个新的 PHP 进程。因此,您需要某种方法来进行 IPC。

以下是 PHP 在 *nix(但不是 Windows)上的实现方式:

接收者:

<?php
$queueKey = 123321;

$queue = false;
if(msg_queue_exists($queueKey)) {
        echo "Queue Exists.\n";
}

// Join the queue
$queue = msg_get_queue($queueKey);

while(!($queue == false)) {
        // Note: This function could block if you feel like threading
        $msgRec = msg_receive(
                $queue,        // I: Queue to get messages from
                0,             // I: Message type (0 = first on queue)
                $msgType,      // O: Type of message received
                1024,          // I: Max message size
                $msgData,      // O: Data in the message
                true,          // I: Unserialize data
                MSG_IPC_NOWAIT // I: Don't block
        );

        if($msgRec) {
                echo "Message received:\n";
                echo "Type = $msgType\n";
                echo "Data = \n";
                print_r($msgData);
        }
}
?>

发件人:

<?php
$queueKey = 123321;

$queue = false;
if(msg_queue_exists($queueKey)) {
        echo "Queue Exists.\n";
} else {
        echo "WARNING: Queue does not exist. Maybe no listeners?\n";
}

$queue = msg_get_queue($queueKey);

$abc["something"] = "something value";
$abc["hello"] = "world";
$abc["fu"] = "bar";
msg_send(
        $queue, // Queue to send on
        1,      // Message type
        $abc,   // Data to send
        true,   // Serialize data?
        true    // Block
);

?>

这应该产生(在接收器循环中)类似的东西:

Message received:
Type = 1 
Data =
Array 
(
    [something] => something value
    [hello] => world
    [fu] => bar 
)

你的脚本可能看起来像这样

postToMe.php:

<?php
$queueKey = 123321;

$queue = false;
if(msg_queue_exists($queueKey)) {
        echo "Queue Exists.\n";
} else {
        echo "WARNING: Queue does not exist. Maybe no listeners?\n";
}

$queue = msg_get_queue($queueKey);

msg_send(
        $queue, // Queue to send on
        1,      // Message type
        $_POST, // Data to send
        true,   // Serialize data?
        true    // Block
);

?>

bot.php:

<?php 
set_time_limit(0); 

$socket = fsockopen("//", 6667) or die(); 

$msg = $_POST['message']; 
$pr = $_POST['percentage']; 
$pr /= 100; 

fputs($socket,"USER BOT 0 zo :ZH bot\n"); 
// Set the bots nickname 
fputs($socket,"NICK BOT1\n"); 

fputs($socket,"JOIN #bots\n"); 


$queueKey = 123321;
$queue = false;

// Join the IPC queue
$queue = msg_get_queue($queueKey);
if(!$queue) echo "ERROR: Could not join IPC queue. Form data will not be received";

while(1) { 
   // Handle new post info
   // You may want to increase the message size from 1024 if post data is large
   if(msg_receive($queue, 0, $msgType, 1024, $msgData, true, MSG_IPC_NOWAIT)) {
      // Handle data here. Post data is stored in $msgData
   }

   while($data = fgets($socket, 128)) { 
      // echo the data received to page 
      echo nl2br($data); 
      // flush old data, it isn't needed any longer. 
      flush(); 

      $ex = explode(' ', $data); 

      if($ex[0] == "PING") fputs($socket, "PONG ".$ex[1]."\n"); 

      $search_string = "/^:([A-Za-z0-9_\-]+)[@!~a-zA-Z0-9@\.\-]+\s*([A-Z]+)\s*[:]*([\#a-zA-Z0-9\-]+)*\s*[:]*([!\#\-\.A-Za-z0-9 ]+)*/"; 

      $do = preg_match($search_string, $data, $matches); 

      // check that there is a command received 
      if(isset($matches['2'])) { 
         switch($matches['2']) { 
            case "PRIVMSG": 
               $user = $matches['1']; 
               $channel = $matches['3']; 
               $chat_text = isset($matches['4']) ? $matches['4'] : ""; 

               // check chat for !time 
               if(strtolower($chat_text) == "!time") {  
                  $output = "::::: " . date('l jS \of F Y h:i:s A') . " :::::"; 
                  fputs($socket, "PRIVMSG " . $channel . " :" . $output . "\n"); 
               } elseif(strtolower($chat_text) == "!hello") { 
                  fputs($socket, "PRIVMSG " . $channel . " :Hello!\n"); 
               } 
            break; 

            case "JOIN": 
               $user = $matches['1']; 
               $channel = $matches['3']; 
               fputs($socket, "PRIVMSG " . $channel . " :Welcome " . $user . " to " . $channel . "\n"); 
            break; 
         } 

      } 
   } 
} 
?> 

关于php - IRC相关帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4220905/

相关文章:

使用 HTML 表格的 PHP 循环

php - 在 php 中,我可以使对象行为与数组有多相似?我该怎么做?

php - 使用 SET 减少列值

python - Twisted IRC Bot 线程

c# - tcpClient 和 IRC 客户端问题

php - 是否有一个 PHP 函数可以在不切割单词的情况下将字符串切割成多个部分?

javascript - 如何使用 jQuery 在每个 Ajax 请求上重新生成 session token ?

Java irc 库

python - 将方法从文件导入到类中

java - 如何过滤打印流