    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.net.telnet;
    019
    020/**
    021 * Implements the Telnet terminal type option RFC 1091.
    022 */
    023public class TerminalTypeOptionHandler extends TelnetOptionHandler {
    024
    025    /**
    026     * Terminal type option
    027     */
    028    protected static final int TERMINAL_TYPE = 24;
    029
    030    /**
    031     * Send (for subnegotiation)
    032     */
    033    protected static final int TERMINAL_TYPE_SEND = 1;
    034
    035    /**
    036     * Is (for subnegotiation)
    037     */
    038    protected static final int TERMINAL_TYPE_IS = 0;
    039
    040    /**
    041     * Terminal type
    042     */
    043    private final String termType;
    044
    045    /**
    046     * Constructor for the TerminalTypeOptionHandler. Initial and accept behavior flags are set to false
    047     *
    048     * @param termtype   terminal type that will be negotiated.
    049     */
    050    public TerminalTypeOptionHandler(final String termtype) {
    051        super(TelnetOption.TERMINAL_TYPE, false, false, false, false);
    052        termType = termtype;
    053    }
    054
    055    /**
    056     * Constructor for the TerminalTypeOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
    057     * local/remote activation request for this option is received.
    058     *
    059     * @param termtype       terminal type that will be negotiated.
    060     * @param initlocal      if set to true, a {@code WILL} is sent upon connection.
    061     * @param initremote     if set to true, a {@code DO} is sent upon connection.
    062     * @param acceptlocal    if set to true, any {@code DO} request is accepted.
    063     * @param acceptremote   if set to true, any {@code WILL} request is accepted.
    064     */
    065    public TerminalTypeOptionHandler(final String termtype, final boolean initlocal, final boolean initremote, final boolean acceptlocal,
    066            final boolean acceptremote) {
    067        super(TelnetOption.TERMINAL_TYPE, initlocal, initremote, acceptlocal, acceptremote);
    068        termType = termtype;
    069    }
    070
    071    /**
    072     * Implements the abstract method of TelnetOptionHandler.
    073     *
    074     * @param suboptionData     the sequence received, without IAC SB &amp; IAC SE
    075     * @param suboptionLength   the length of data in suboption_data
    076     * @return terminal type information
    077     */
    078    @Override
    079    public int[] answerSubnegotiation(final int suboptionData[], final int suboptionLength) {
    080        if (suboptionData != null && suboptionLength > 1 && termType != null && suboptionData[0] == TERMINAL_TYPE && suboptionData[1] == TERMINAL_TYPE_SEND) {
    081            final int[] response = new int[termType.length() + 2];
    082            response[0] = TERMINAL_TYPE;
    083            response[1] = TERMINAL_TYPE_IS;
    084            for (int ii = 0; ii < termType.length(); ii++) {
    085                response[ii + 2] = termType.charAt(ii);
    086            }
    087            return response;
    088        }
    089        return null;
    090    }
    091}