File indexing completed on 2024-04-21 15:08:10

0001 /***************************************************************************
0002                           ctelnet.h  -  handles telnet connection
0003     This file is a part of KMuddy distribution.
0004                              -------------------
0005     begin                : Pi Jun 14 2002
0006     copyright            : (C) 2002-2008 by Tomas Mecir
0007     email                : kmuddy@kmuddy.com
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 
0019 #ifndef CTELNET_H
0020 #define CTELNET_H
0021 
0022 
0023 #include <list>
0024 #include <string>
0025 
0026 #include <config-mxp.h>
0027 #include "cactionbase.h"
0028 #include <QObject>
0029 
0030 struct cTelnetPrivate;
0031 
0032 class cProfileSettings;
0033 
0034 /**
0035 cTelnet handles the connection and telnet commands
0036 However, as this is a MUD client and not a telnet, only a small subset of
0037 telnet commands is handled.
0038 
0039 List of direct telnet commands and their support in this class:
0040 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
0041 240 - SE (subcommand end) - supported
0042 241 - NOP (no operation) - ignored ;-)
0043 242 - DM (data mark) - NOT USED - Synch is not implemented (well,
0044   maybe I'll implement this later if reall necessary...)
0045 243 - B (break) - not used (only client may use this, so the server won't bother with this)
0046 244 - IP (interrupt process) - not applicable (only useful for shell processes)
0047 245 - AO (about output) - not applicable (see IP)
0048 246 - AYT (are you there) - I do not send this; "I'm here" is sent
0049   in an unlikely event that AYT is received
0050 247 - EC (erase char) - not applicable (line editing fully handled by client)
0051 248 - EL (erase line) - not applicable (line editing fully handled by client)
0052 249 - GA (go ahead) - ignored; suppress-go-ahead is negotiated if possible
0053 250 - SB (subcommand begin) - supported
0054 251 - WILL - supported
0055 252 - WON'T - supported
0056 253 - DO - supported
0057 254 - DON'T - supported
0058 255 - IAC - supported
0059 
0060 Extended telnet commands:
0061 =-=-=-=-=-=-=-=-=-=-=-=-=
0062   these are handled with the IAC DO/DON'T/WILL/WON'T and SB/SE mechanism;
0063   see RFC 854 and RFCs listed here for further information.
0064 
0065 a) FULLY SUPPORTED COMMANDS:
0066 - STATUS (cmd 5, RFC 859) - sending/receiving option status
0067 - TIMING-MARK (cmd 6, RFC 860) - timing marks, used by auto-mapper
0068 - TERMINAL-TYPE (cmd 24, RFC 1091) - terminal type sending
0069 - NAWS (cmd 31, RFC 1073) - negotiate about window size - used to inform
0070   the server about window size (cols x rows)
0071 - MCCP v1 (cmd 85) - compression protocol version 1; handled by cMCCP class
0072 - MCCP v2 (cmd 86) - compression protocol version 2; handled by cMCCP class
0073   
0074 b) PARTIALLY SUPPORTED COMMANDS
0075 - SUPPRESS-GO-AHEAD (cmd 3, RFC 858) - we try to suppress GA's if possible.
0076   If we fail, we just ignore all GA's, hoping for no problems...
0077 
0078 c) COMMANDS THAT ARE NOT SUPPORTED
0079 The following commands are not supported at all - they are all disabled,
0080 if the server asks us to enable it, we respond with DON'T or WON'T (this
0081 is a standard behaviour described in RFC 854)
0082 - TRANSMIT-BINARY (cmd 0, RFC 856) (see note below)
0083 - ECHO (cmd 1, RFC 857) (see note below)
0084 - EXTENDED-OPTIONS-LIST (cmd 255, RFC 861) (see note below)
0085 - RCTE (cmd 7, RFC 726)
0086 - NAOCRD (cmd 10, RFC 652)
0087 - NAOHTS (cmd 11, RFC 653)
0088 - NAOHTD (cmd 12, RFC 654)
0089 - NAOFFD (cmd 13, RFC 655)
0090 - NAOVTS (cmd 14, RFC 656)
0091 - NAOVTD (cmd 15, RFC 657)
0092 - NAOLFD (cmd 16, RFC 658)
0093 - EXTEND-ASCII (cmd 17, RFC 698)
0094 - LOGOUT (cmd 18, RFC 727)
0095 - BM (cmd 19, RFC 735)
0096 - DET (cmd 20, RFC 1043)
0097 - SUPDUP (cmd 21, RFC 736)
0098 - SUPDUP-OUTPUT (cmd 22, RFC 749)
0099 - SEND-LOCATION (cmd 23, RFC 779) (well, maybe one day I'll support this)
0100 - END-OF-RECORD (cmd 25, RFC 885)
0101 - TUID (cmd 26, RFC 927)
0102 - OUTMARK (cmd 27, RFC 933)
0103 - TTYLOC (cmd 28, RFC 946)
0104 - 3270-REGIME (cmd 29, RFC 1041)
0105 - X.3-PAD (cmd 30, RFC 1053)
0106 - TERMINAL-SPEED (cmd 32, RFC 1079)  (not needed, let the server decide; we can
0107   accept data at (almost) any speed, only limited by connection speed)
0108 - TOGGLE-FLOW-CONTROL (cmd 33, RFC 1372) (maybe I'll support it, if it
0109   proves to be necessary/useful)
0110 - LINEMODE (cmd 34, RFC 1184) - linemode is not needed, as we send the entire
0111   line either...
0112 - NEW-ENVIRON (cmd 39, RFC 1572)
0113 - TN3270E (cmd 40, RFC 1647)
0114 
0115 Note: the first three commands are internet standards and each telnet application
0116 should support them. However, this is NOT a telnet application and those
0117 commands have no use for a MUD client, so I leave those commands unsupported.
0118 Who knows, maybe one day I'll change my opinion and implement them (or somebody
0119 else does it ;))
0120 
0121   *@author Tomas Mecir
0122   */
0123 
0124 
0125 //telnet command codes (prefixed with TN_ to prevent duplicit #defines
0126 #define TN_SE (unsigned char) 240
0127 #define TN_NOP (unsigned char) 241
0128 #define TN_DM (unsigned char) 242
0129 #define TN_B (unsigned char) 243
0130 #define TN_IP (unsigned char) 244
0131 #define TN_AO (unsigned char) 245
0132 #define TN_AYT (unsigned char) 246
0133 #define TN_EC (unsigned char) 247
0134 #define TN_EL (unsigned char) 248
0135 #define TN_GA (unsigned char) 249
0136 #define TN_SB (unsigned char) 250
0137 #define TN_WILL (unsigned char) 251
0138 #define TN_WONT (unsigned char) 252
0139 #define TN_DO (unsigned char) 253
0140 #define TN_DONT (unsigned char) 254
0141 #define TN_IAC (unsigned char) 255
0142 
0143 //telnet option codes (supported options only)
0144 #define OPT_ECHO (unsigned char) 1
0145 #define OPT_SUPPRESS_GA (unsigned char) 3
0146 #define OPT_STATUS (unsigned char) 5
0147 #define OPT_TIMING_MARK (unsigned char) 6
0148 #define OPT_TERMINAL_TYPE (unsigned char) 24
0149 #define OPT_NAWS (unsigned char) 31
0150 #define OPT_COMPRESS (unsigned char) 85
0151 #define OPT_COMPRESS2 (unsigned char) 86
0152 #define OPT_MSP (unsigned char) 90
0153 #define OPT_MXP (unsigned char) 91
0154 
0155 //telnet SB suboption types
0156 #define TNSB_IS (char) 0
0157 #define TNSB_SEND (unsigned char) 1
0158 
0159 class cTelnet : public QObject, public cActionBase {
0160 Q_OBJECT
0161 public: 
0162   cTelnet (int sess);
0163   ~cTelnet () override;
0164   /** attempt to establish a new connection */
0165   void connectIt (const QString &address, int port, cProfileSettings *sett = nullptr);
0166   /** closes connection */
0167   void disconnect ();
0168   /** returns whether we're connected to some other host */
0169   bool isConnected ();
0170 
0171   /** Prepares data, doubles IACs, sends it using doSendData. */
0172   bool sendData (const QString &data);
0173 
0174   /** these two functions control waiting for data from server */
0175   void waitingForData ();
0176   bool newData ();
0177   void setOffLineConnection (bool type);
0178   bool isOffLineConnection ();
0179   int compressedBytes ();
0180   int uncompressedBytes ();
0181   int sentBytes ();
0182   bool usingMCCP ();
0183   int MCCPVer ();
0184 
0185   /** Command echo setting */
0186   void setCommandEcho(bool cmdEcho);
0187 
0188   /** LPMud prompt style */
0189   void setLPMudStyle (bool lpmustyle);
0190   
0191   /** Start-up telnet negotiation */
0192   void setNegotiateOnStartup (bool startupneg);
0193   
0194   /** are we currently using MSP? */
0195   bool usingMSP ();
0196   void setMSPAllowed (bool allow);
0197   void setDownloadAllowed (bool allow);
0198   void setMSPGlobalPaths (const QStringList &paths);
0199   
0200   void processSoundRequest (bool isSOUND, QString fName, int volume, int repeats, int priority,
0201       QString type, QString url);
0202 
0203   #ifdef HAVE_MXP
0204   bool usingMXP ();
0205   /** how are we using MXP?
0206   1 = Never
0207   2 = If negotiated
0208   3 = Auto-detect (if a MXP mode change occurs)
0209   4 = Always on (for zMUD compatibility)
0210   */
0211   int MXPAllowed ();
0212   void setMXPAllowed (int allow);
0213   #endif
0214   
0215   /** window size has changed - informs the server about it */
0216   void windowSizeChanged (int x, int y);
0217 protected slots:
0218   /** called by socket when it connects */
0219   void socketHostFound ();
0220   void socketConnected ();
0221   /** called by socket when connection fails */
0222   void socketFailed ();
0223   void socketRead ();
0224   void socketClosed ();
0225 protected:
0226   void eventIntHandler (QString event, int session, int par1, int par2) override;
0227   void eventNothingHandler (QString event, int session) override;
0228 
0229   void reset ();
0230   void setupSocketHandlers ();
0231   void setupEncoding ();
0232 
0233   /** Send out the data. Does not double IACs, this must be done by caller
0234   if needed. This function is suitable for sending telnet sequences. */
0235   bool doSendData (const std::string &data);
0236   
0237   /** processes a telnet command (IAC ...) */
0238   void processTelnetCommand (const std::string &command);
0239 
0240   /** send a telnet option */
0241   void sendTelnetOption (unsigned char type, unsigned char option);
0242 
0243   cTelnetPrivate *d;
0244 };
0245 
0246 #endif