function on_read($event) { $read = "on_read: "; bufferevent_read($event, $read, 100); bufferevent_write($event, $read); }
function on_read($event) { global $CLIENTS; global $COMMANDS; $time = date('H:i:s'); bufferevent_read($event, $msg, 100); $msg = rtrim($msg); // determine sender $sender = ''; if (($ip = findClientByResource($event)) == false) { echo "somethin f****d up - couldn't find sender"; return; } $client = $CLIENTS[$ip]; $sender = $client['nick']; echo $time . ' ' . $sender . ': ' . $msg . "\n"; $toUser = $toOthers = null; if (substr(trim($msg), 0, 1) == '/') { $msg = trim($msg); $params = explode(' ', $msg); $cmd = strtolower(substr($params[0], 1)); unset($params[0]); $paramStr = trim(implode(' ', $params)); switch ($cmd) { case 'away': $toUser = "******"; break; case 'help': $paramStr = strtolower($paramStr); if (strlen($paramStr) == 0) { $toUser = "******" . implode(', ', array_keys($COMMANDS)); } else { if (in_array($paramStr, array_keys($COMMANDS))) { $toUser = $COMMANDS[$paramStr]; } else { $toUser = "******"; } } break; case 'ison': $toUser = "******"; foreach ($params as $user) { $user = trim($user); if (findClientByNick($user) != false) { $toUser .= "{$user} "; } } break; case 'join': $toUser = "******"; break; case 'kick': case 'kill': if (($ip = findClientByNick($paramStr)) != false) { $toUser = $toOthers = $client['nick'] . " has kicked " . $CLIENTS[$ip]['nick'] . "."; fclose($CLIENTS[$ip]['con']); unset($CLIENTS[$ip]); } break; case 'list': $toUser = "******"; break; case 'motd': $toUser = "******"; break; case 'names': case 'users': $toUser = "******"; foreach ($CLIENTS as $client) { $toUser .= $client['nick'] . ' '; } break; case 'nick': $old = $client['nick']; $new = trim($paramStr); // TODO: maybe use some sort of Zend_Validator to handle this // TODO: make sure username is unique if (strlen($new) == 0) { $toUser = "******"; } else { $CLIENTS[$ip]['nick'] = $new; $toUser = "******"; $toOthers = "{$old} has changed their nick to '{$new}'."; } break; case 'privmsg': $toUser = "******"; break; case 'quit': case 'exit': $toOthers = $client['nick'] . " has disconnected."; fclose($client['con']); unset($CLIENTS[$ip]); break; case 'time': $toUser = "******" . date('H:i:s') . "."; break; default: $toUser = "******"; break; } } else { $toUser = $toOthers = "{$sender}: " . $msg; } // send message to all other clients foreach ($CLIENTS as $ip => $d) { if ($event == $d['client']) { if ($toUser != null) { bufferevent_write($d['client'], $time . ' ' . $toUser . "\n"); } } else { if ($toOthers != null) { bufferevent_write($d['client'], $time . ' ' . $toOthers . "\n"); } } } }