00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <iostream>
00012
00013 #include <qsocket.h>
00014 #include <qstring.h>
00015 #include <qstringlist.h>
00016 #include <qregexp.h>
00017
00018 #include "ChatSession.h"
00019 #include "telnetmanager.h"
00020
00021 using namespace std;
00022
00023 TelnetManager::TelnetManager(QWidget *parent, const char *name)
00024 : QObject(parent, name) {
00025 mtp = (ChatSession *)parent;
00026 socket = new QSocket(this);
00027
00028 IAC = QChar(char(255));
00029
00030 connect(socket, SIGNAL(readyRead()),
00031 this, SLOT(readStdout()));
00032 connect(socket, SIGNAL(connectionClosed()),
00033 this, SLOT(slotProcessExited()));
00034 connect(socket, SIGNAL(error(int)),
00035 this, SLOT(slotError(int)));
00036 }
00037
00038
00039 TelnetManager::~TelnetManager() {
00040 socket->close();
00041 delete socket;
00042 }
00043
00044 void TelnetManager::setArgs(const QString& host, const QString& port) {
00045 this->host = host;
00046 this->port = port.toInt();
00047 }
00048
00049 void TelnetManager::start() {
00050 socket->connectToHost(host,port);
00051 }
00052
00053 void TelnetManager::readStdout() {
00054 int available = socket->bytesAvailable();
00055
00056 char * buffer = new char[available+1];
00057 socket->readBlock(buffer,available);
00058 buffer[available] = 0;
00059
00060 QString text(buffer);
00061 delete[] buffer;
00062
00063
00064 text = text.replace(QRegExp(QString("^") + IAC + QString("..")),"").replace(QRegExp(QString(IAC) + IAC),QString(IAC));
00065
00066
00067 text = text.replace(QRegExp(QString("\r\n")),"\n");
00068
00069
00070 if (text.endsWith("Login: ")) {
00071 text += "\n";
00072 if (login != "")
00073 writeStdin(login);
00074 login = "";
00075 }
00076
00077 if (text.endsWith("Password: ")) {
00078 text += "\n";
00079 if (password != "")
00080 writeStdin(password);
00081 password = "";
00082 }
00083
00084 text = suspended + text;
00085 QStringList list = QStringList::split("\n",text);
00086 QStringList::Iterator end_it;
00087
00088 if (!text.endsWith("\n")) {
00089 end_it = list.end();
00090 end_it--;
00091 suspended = *end_it;
00092 } else {
00093 suspended = "";
00094 end_it = list.end();
00095 }
00096 for (QStringList::Iterator it = list.begin(); it != end_it; ++it) {
00097 mtp->displayStdout(*it);
00098 }
00099 }
00100
00101 void TelnetManager::writeStdin(const QString& msg) {
00102 QString to_write(msg);
00103 to_write = to_write.replace(QRegExp("\n"),"\r\n") + "\r\n";
00104
00105 int index = 0;
00106 while ((index = to_write.find(IAC,index)) != -1) {
00107 to_write.insert(index,IAC);
00108 index += 2;
00109 }
00110
00111 socket->writeBlock(to_write.latin1(),to_write.length());
00112 }
00113
00114 void TelnetManager::slotProcessExited() {
00115 emit processExited();
00116 }
00117
00118 void TelnetManager::setLogin(const QString& login) {
00119 this->login = login;
00120 }
00121
00122 void TelnetManager::setPassword(const QString& password) {
00123 this->password = password;
00124 }
00125
00126 void TelnetManager::slotError(int error_code) {
00127 switch (error_code) {
00128 case QSocket::ErrConnectionRefused:
00129 cerr << "QSocket::ErrConnectionRefused" << endl;
00130 break;
00131 case QSocket::ErrHostNotFound:
00132 cerr << "QSocket::ErrHostNotFound" << endl;
00133 break;
00134 case QSocket::ErrSocketRead:
00135 cerr << "QSocket::ErrSocketRead" << endl;
00136 break;
00137 default:
00138 cerr << "Unknown QSocket error" << endl;
00139 break;
00140 }
00141 }