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 *      http://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 * The TelnetOptionHandler class is the base class to be used for implementing handlers for telnet options.
022 * <p>
023 * TelnetOptionHandler implements basic option handling functionality and defines abstract methods that must be implemented to define subnegotiation behavior.
024 * </p>
025 */
026public abstract class TelnetOptionHandler {
027    /**
028     * Option code
029     */
030    private int optionCode = -1;
031
032    /**
033     * true if the option should be activated on the local side
034     */
035    private boolean initialLocal;
036
037    /**
038     * true if the option should be activated on the remote side
039     */
040    private boolean initialRemote;
041
042    /**
043     * true if the option should be accepted on the local side
044     */
045    private boolean acceptLocal;
046
047    /**
048     * true if the option should be accepted on the remote side
049     */
050    private boolean acceptRemote;
051
052    /**
053     * true if the option is active on the local side
054     */
055    private boolean doFlag;
056
057    /**
058     * true if the option is active on the remote side
059     */
060    private boolean willFlag;
061
062    /**
063     * Constructor for the TelnetOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
064     * local/remote activation request for this option is received.
065     *
066     * @param optcode      - Option code.
067     * @param initlocal    - if set to true, a {@code WILL} is sent upon connection.
068     * @param initremote   - if set to true, a {@code DO} is sent upon connection.
069     * @param acceptlocal  - if set to true, any {@code DO} request is accepted.
070     * @param acceptremote - if set to true, any {@code WILL} request is accepted.
071     */
072    public TelnetOptionHandler(final int optcode, final boolean initlocal, final boolean initremote, final boolean acceptlocal, final boolean acceptremote) {
073        optionCode = optcode;
074        initialLocal = initlocal;
075        initialRemote = initremote;
076        acceptLocal = acceptlocal;
077        acceptRemote = acceptremote;
078    }
079
080    /**
081     * Method called upon reception of a subnegotiation for this option coming from the other end.
082     * <p>
083     * This implementation returns null, and must be overridden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
084     * request.
085     * </p>
086     *
087     * @param suboptionData   - the sequence received, without IAC SB &amp; IAC SE
088     * @param suboptionLength - the length of data in suboption_data
089     * @return response to be sent to the subnegotiation sequence. TelnetClient will add IAC SB &amp; IAC SE. null means no response
090     */
091    public int[] answerSubnegotiation(final int suboptionData[], final int suboptionLength) {
092        return null;
093    }
094
095    /**
096     * Gets a boolean indicating whether to accept a DO request coming from the other end.
097     *
098     * @return true if a {@code DO} request shall be accepted.
099     */
100    public boolean getAcceptLocal() {
101        return acceptLocal;
102    }
103
104    /**
105     * Gets a boolean indicating whether to accept a WILL request coming from the other end.
106     *
107     * @return true if a {@code WILL} request shall be accepted.
108     */
109    public boolean getAcceptRemote() {
110        return acceptRemote;
111    }
112
113    /**
114     * Gets a boolean indicating whether a {@code DO} request sent to the other side has been acknowledged.
115     *
116     * @return true if a {@code DO} sent to the other side has been acknowledged.
117     */
118    boolean getDo() {
119        return doFlag;
120    }
121
122    /**
123     * Gets a boolean indicating whether to send a WILL request to the other end upon connection.
124     *
125     * @return true if a {@code WILL} request shall be sent upon connection.
126     */
127    public boolean getInitLocal() {
128        return initialLocal;
129    }
130
131    /**
132     * Gets a boolean indicating whether to send a DO request to the other end upon connection.
133     *
134     * @return true if a {@code DO} request shall be sent upon connection.
135     */
136    public boolean getInitRemote() {
137        return initialRemote;
138    }
139
140    /**
141     * Gets the option code for this option.
142     *
143     * @return Option code.
144     */
145    public int getOptionCode() {
146        return optionCode;
147    }
148
149    /**
150     * Gets a boolean indicating whether a {@code WILL} request sent to the other side has been acknowledged.
151     *
152     * @return true if a {@code WILL} sent to the other side has been acknowledged.
153     */
154    boolean getWill() {
155        return willFlag;
156    }
157
158    /**
159     * Sets behavior of the option for DO requests coming from the other end.
160     *
161     * @param accept - if true, subsequent DO requests will be accepted.
162     */
163    public void setAcceptLocal(final boolean accept) {
164        acceptLocal = accept;
165    }
166
167    /**
168     * Sets behavior of the option for {@code WILL} requests coming from the other end.
169     *
170     * @param accept - if true, subsequent {@code WILL} requests will be accepted.
171     */
172    public void setAcceptRemote(final boolean accept) {
173        acceptRemote = accept;
174    }
175
176    /**
177     * Sets this option whether a {@code DO} request sent to the other side has been acknowledged (invoked by TelnetClient).
178     *
179     * @param state - if true, a {@code DO} request has been acknowledged.
180     */
181    void setDo(final boolean state) {
182        doFlag = state;
183    }
184
185    /**
186     * Sets this option whether to send a {@code WILL} request upon connection.
187     *
188     * @param init - if true, a {@code WILL} request will be sent upon subsequent connections.
189     */
190    public void setInitLocal(final boolean init) {
191        initialLocal = init;
192    }
193
194    /**
195     * Sets this option whether to send a {@code DO} request upon connection.
196     *
197     * @param init - if true, a {@code DO} request will be sent upon subsequent connections.
198     */
199    public void setInitRemote(final boolean init) {
200        initialRemote = init;
201    }
202
203    /**
204     * Sets this option whether a {@code WILL} request sent to the other side has been acknowledged (invoked by TelnetClient).
205     *
206     * @param state - if true, a {@code WILL} request has been acknowledged.
207     */
208    void setWill(final boolean state) {
209        willFlag = state;
210    }
211
212    /**
213     * This method is invoked whenever this option is acknowledged active on the local end (TelnetClient sent a WILL, remote side sent a DO). The method is used
214     * to specify a subnegotiation sequence that will be sent by TelnetClient when the option is activated.
215     * <p>
216     * This implementation returns null, and must be overriden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
217     * request.
218     * </p>
219     *
220     * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient will add IAC SB &amp; IAC SE. null means no subnegotiation.
221     */
222    public int[] startSubnegotiationLocal() {
223        return null;
224    }
225
226    /**
227     * This method is invoked whenever this option is acknowledged active on the remote end (TelnetClient sent a DO, remote side sent a WILL). The method is
228     * used to specify a subnegotiation sequence that will be sent by TelnetClient when the option is activated.
229     * <p>
230     * This implementation returns null, and must be overridden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
231     * request.
232     * </p>
233     *
234     * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient will add IAC SB &amp; IAC SE. null means no subnegotiation.
235     */
236    public int[] startSubnegotiationRemote() {
237        return null;
238    }
239}