<?php

namespace App\Classes\Control;

use App\Classes\Control;

/**
 * Class Telnet
 *
 * This class looks after any telnet session commands
 *
 * TELNET http://pcmicro.com/netfoss/telnet.html
 *
 * @package App\Classes\Control
 */
class Telnet extends Control
{
	private $option = FALSE;
	private $note = '';
	private $terminal = '';

	public function handle(string $read)
	{
		$this->so->log('debug',sprintf('Session Char (%s)',ord($read)),['complete'=>$this->complete,'option'=>$this->option]);

		switch ($read) {
			// Command being sent.
			case TCP_IAC:
				$this->complete = FALSE;
				$this->note = 'IAC ';

				break;

			case TCP_SB:
				$this->option = TRUE;

				break;

			case TCP_SE:
				$this->option = FALSE;
				$this->complete = TRUE;
				$this->so->log('debug',sprintf('Session Terminal: %s',$this->terminal));

				break;

			case TCP_DO:
				$this->note .= 'DO ';

				break;

			case TCP_WILL:
				$this->note .= 'WILL ';

				break;

			case TCP_WONT:
				$this->note .= 'WONT ';

				break;

			case TCP_OPT_TERMTYPE:

				break;

			case TCP_OPT_ECHO:
				$this->note .= 'ECHO';
				$this->complete = TRUE;

				$this->so->log('debug',sprintf('Session Note: %s',$this->note));

				break;

			case TCP_OPT_SUP_GOAHEAD:
				$this->note .= 'SUPPRESS GO AHEAD';
				$this->complete = TRUE;

				$this->so->log('debug',sprintf('Session Note: %s',$this->note));

				break;

			case TCP_OPT_WINDOWSIZE:
				$this->note .= 'WINDOWSIZE';
				$this->complete = TRUE;

				$this->so->log('debug',sprintf('Session Note: %s',$this->note));

				break;

			default:
				if ($this->option AND $read) {
					$this->terminal .= $read;

				} else {
					$this->so->log('debug',sprintf('Unhandled char in session_init: %s (%s)',$read,ord($read)));
				}
		}

		return '';
	}
}