function PostChannelMessage($Channel, $Msg)
 {
     $success = $this->connection->PutData(array('type' => 'post', 'to' => $Channel, 'message' => $Msg));
     if (!$success) {
         return false;
     } else {
         $message = array('type' => 'PRIVMSG', 'sender' => $_SESSION[IrcClientManager::$SessionName]['nick'], 'to' => $Channel, 'channel' => $Channel, 'content' => $Msg, 'received' => time());
         IrcClientManager::ProcessPrivMsg($message);
         return $message;
     }
 }
 /**
  * @todo Handle when not logged in nicely
  */
 function ajax($variation = NULL)
 {
     if ('embeddedlive' === $variation) {
         show_404();
         if (!CheckPermissions('public', false)) {
             return;
         }
         if (!isset($_POST['username']) || !isset($_POST['fullname'])) {
             show_404();
         }
         $username = $_POST['username'];
         $fullname = $_POST['fullname'];
     } else {
         if (!CheckPermissions('office', false)) {
             return;
         }
         $username = $this->user_auth->username;
         $fullname = $this->user_auth->firstname . ' ' . $this->user_auth->surname;
     }
     $nick = str_replace(' ', '', $fullname);
     $data = array('Errors' => array(), 'Messages' => array());
     if (isset($_POST['cmd'])) {
         $this->load->library('irc_client');
         if ($_POST['cmd'] == 'connect') {
             // Start a new client server, don't terminate until client server is finished.
             // The script will continue to run, timeout will be disabled.
             // However the connection to the browser will be closed
             // Only have one client server running at a time
             if (IrcClientManager::IsConnected()) {
                 $this->irc_client->Attach();
                 if ($this->irc_client->Attached()) {
                     $this->irc_client->ForceDisconnect();
                 }
             }
             // This is the main client server object
             $server = new IrcClientManager('irc.afsmg.co.uk');
             if ($server->IsConnected()) {
                 // Use the user's username, and fullnames in irc names
                 $server->Login($username, $nick, $fullname);
                 $server->Join('#theyorkeroffice');
                 // Output empty XML document and close the connection with
                 // the browser
                 @ob_end_clean();
                 header('content-type: text/xml');
                 header("Connection: close");
                 ignore_user_abort();
                 ob_start();
                 echo '<?xml version="1.0" encoding="UTF-8"?' . '><irc></irc>' . "\n";
                 $size = ob_get_length();
                 header('Content-Length: ' . $size);
                 ob_end_flush();
                 flush();
                 // Now start listening to the sockets.
                 return $server->listen();
             } else {
                 $data['Errors'][] = array('_attr' => array('code' => 1, 'retry' => 1), "Connection failed");
             }
         } else {
             $this->irc_client->Attach();
             session_commit();
             if (!$this->irc_client->Attached()) {
                 $data['Errors'][] = array('_attr' => array('code' => 1, 'retry' => 0), "Unable to process message, IPC failure.");
             } else {
                 $new_message = NULL;
                 $get_messages = false;
                 switch ($_POST['cmd']) {
                     // This command lets the server know that the interface is still alive
                     // it also waits a while for any new messages which it can return
                     case 'ping':
                         $this->irc_client->Ping();
                         $get_messages = true;
                         break;
                         // A query has been written in one of the channels
                     // A query has been written in one of the channels
                     case 'msg':
                         if (isset($_POST['channel']) && isset($_POST['msg'])) {
                             $new_message = $this->irc_client->InterpretQuery($_POST['channel'], $_POST['msg']);
                             if (false === $new_message) {
                                 $data['Errors'][] = array('_attr' => array('code' => 1, 'retry' => 0), "Unable to send message, IPC failure.");
                             }
                         }
                         break;
                         // Disconnect and end any client servers.
                     // Disconnect and end any client servers.
                     case 'disconnect':
                         if ($this->irc_client->Attached()) {
                             $this->irc_client->Disconnect();
                         } else {
                             $data['Errors'][] = array('_attr' => array('code' => 1, 'retry' => 0), "Unable to disconnect, IPC failure.");
                         }
                         $continue = false;
                         break;
                 }
                 if (is_array($new_message)) {
                     $new_message['content'] = $this->_autolinkify(xml_escape($new_message['content']));
                     $data['Messages'][] = $new_message;
                 }
                 if ($get_messages) {
                     // wait up to 20 seconds for messages before returning.
                     $messages = $this->irc_client->WaitForMessages(20 * 1000);
                     if (is_array($messages)) {
                         foreach ($messages as $message) {
                             $message['content'] = $this->_autolinkify(xml_escape($message['content']));
                             $data['Messages'][] = $message;
                         }
                     }
                     // Stop receiving messages
                     $this->irc_client->Unping();
                 }
             }
         }
     }
     // Write the xml of any messages.
     $this->load->view('office/irc/xml.php', $data);
 }