summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/ircservertab.cpp
blob: 5eb1758069c6445d210e569bd84290167f321e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <qtextstream.h>
#include <qwhatsthis.h>

#include "ircservertab.h"
#include "ircmessageparser.h"
#include "ircchannelperson.h"


bool IRCServerTab::containsPing( const QString& text, IRCServerTab* tab ) {
  return (text.contains(IRCMessageParser::tr("Received a CTCP PING from "))) ||
         (text.find("ping") != -1 && text.find( tab->server()->nick() != -1));
}


IRCServerTab::IRCServerTab(IRCServer server, MainWindow *mainWindow, QWidget *parent, const char *name, WFlags f) : IRCTab(parent, name, f) {
    m_server = server;
    m_session = new IRCSession(this, &m_server);
    m_mainWindow = mainWindow;
    m_close = FALSE;
    m_lines = 0;
    m_description->setText(tr("Connecting to")+" <b>" + server.hostname() + ":" + QString::number(server.port()) + "</b>");
    m_textview = new QTextView(this);
    m_textview->setHScrollBarMode(QScrollView::AlwaysOff);
    m_textview->setVScrollBarMode(QScrollView::AlwaysOn);
    m_textview->setTextFormat(RichText);
    QWhatsThis::add(m_textview, tr("Server messages"));
    m_layout->add(m_textview);
    m_field = new IRCHistoryLineEdit(this);
    connect(m_field, SIGNAL(nextTab()), this, SIGNAL(nextTab()));
    connect(m_field, SIGNAL(prevTab()), this, SIGNAL(prevTab()));
    connect(m_field, SIGNAL(closeTab()), this, SIGNAL(closeTab()));
    connect(this, SIGNAL(editFocus()), m_field, SLOT(setEditFocus()));

    QWhatsThis::add(m_field, tr("Type commands here. A list of available commands can be found inside the OpieIRC help"));
    m_layout->add(m_field);
    connect(m_field, SIGNAL(returnPressed()), this, SLOT(processCommand()));
    connect(m_session, SIGNAL(outputReady(IRCOutput)), this, SLOT(display(IRCOutput)));
    connect(m_mainWindow, SIGNAL(updateScroll()), this, SLOT(scrolling()));
    connect(m_session, SIGNAL(updateChannels()), this, SLOT(slotUpdateChannels()));
    settingsChanged();

    m_field->setFocus();
    m_field->setActiveWindow();

}

void IRCServerTab::scrolling(){
  m_textview->ensureVisible(0, m_textview->contentsHeight());
}


void IRCServerTab::appendText(QString text) {
    /* not using append because it creates layout problems */
    QString txt = m_textview->text() + IRCTab::appendTimestamp( text );



    if (m_maxLines > 0 && m_lines >= m_maxLines) {
        int firstBreak = txt.find('\n');
        if (firstBreak != -1) {
            txt = "<qt bgcolor=\"" + m_backgroundColor + "\"/>" + txt.right(txt.length() - (firstBreak + 1));
        }
    } else {
        m_lines++;
    }
    m_textview->setText(txt);
    m_textview->ensureVisible(0, m_textview->contentsHeight());
    emit changed(this);
}

IRCServerTab::~IRCServerTab() {
    delete m_session;
}

void IRCServerTab::removeChannelTab(IRCChannelTab *tab) {
    m_channelTabs.remove(tab);
}

void IRCServerTab::removeQueryTab(IRCQueryTab *tab) {
    m_queryTabs.remove(tab);
}

void IRCServerTab::addQueryTab(IRCQueryTab *tab) {
    m_queryTabs.append(tab);
}

QString IRCServerTab::title() {
    return "Server";
}

IRCSession *IRCServerTab::session() {
    return m_session;
}
/*
QString *IRCServerTab::mynick() {
	return (*m_server->nick());
} */

IRCServer *IRCServerTab::server() {
    return &m_server;
}

void IRCServerTab::settingsChanged() {
    m_textview->setText("<qt bgcolor=\"" + m_backgroundColor + "\"/>");
    m_lines = 0;
}

void IRCServerTab::executeCommand(IRCTab *tab, QString line) {
    QTextIStream stream(&line);
    QString command;
    stream >> command;
    command = command.upper().right(command.length()-1);

   //JOIN
  if (command == "JOIN" || command == "J") {
        QString channel;
        stream >> channel;
        /* According to RFC 1459 */
        if (channel.length() > 0 && channel.length() < 200 &&
            channel.find(",") == -1 && channel.find('\007') == -1) {
            
            if (!channel.startsWith("#") && !channel.startsWith("&") 
		&& !channel.startsWith("+") && !channel.startsWith("!")) {
                channel = channel.prepend("#");
            }
            m_session->join(channel);
        } else {
            tab->appendText("<font color=\"" + m_errorColor + "\">Unknown channel format!</font><br>");
        }
    }

  //KICK
	else if (command == "KICK"){
		QString nickname;
        stream >> nickname;
        if (nickname.length() > 0) {
            if (line.length() > 7 + nickname.length()) {
                QString text = line.right(line.length()-nickname.length()-7);
                IRCPerson person;
                person.setNick(nickname);
                m_session->kick(((IRCChannelTab *)tab)->channel(), &person, text);
            } else {
                IRCPerson person;
                person.setNick(nickname);
                m_session->kick(((IRCChannelTab *)tab)->channel(), &person);
            }
        }
	}

 else if (command == "OP"){
		QString nickname;
        stream >> nickname;
        if (nickname.length() > 0) {
                QString text = line.right(line.length()-nickname.length()-5);
                IRCPerson person;
                person.setNick(nickname);
                m_session->op(((IRCChannelTab *)tab)->channel(), &person);
            }
        }

  //SEND MODES
  else if (command == "MODE"){
     QString text = line.right(line.length()-6);
     if (text.length() > 0) {
       m_session->mode(text);
     } else {
       tab->appendText("<font color=\"" + m_errorColor + "\">/mode channel {[+|-]|o|p|s|i|t|n|b|v} [limit] [user] [ban mask]<br>/mode nickname {[+|-]|i|w|s|o}</font><br>");
     }
	}
  //SEND RAW MESSAGE TO SERVER, COMPLETELY UNCHECKED - anything in the RFC...or really anything you want
  else if (command == "RAW"){
     QString text = line.right(line.length()-5);
     if (text.length() > 0) {
       m_session->raw(text);
     }
  }
  else if (command == "SUSPEND"){
     QString text = line.right(line.length()-9);
      if (text.upper() == "ON") {
          QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable;
      }
      else if (text.upper() == "OFF"){
          QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Disable;
      } else {
          tab->appendText("<font color=\"" + m_errorColor + "\">Line: "+ line +"</font><br>Text: "+text);
      }
  }

  else if (command == "QUIT"){
     QString text = line.right(line.length()-6);
     if (text.length() > 0) {
       m_session->quit(text);
     } else {
       m_session->quit();
     }
	}

  //SEND ACTION
	else if (command == "ME") {
        QString text = line.right(line.length()-4);
        if (text.length() > 0) {
            if (tab->isA("IRCChannelTab")) {
                tab->appendText("<font color=\"" + m_selfColor + "\">*" + IRCOutput::toHTML(m_server.nick()) + " " + IRCOutput::toHTML(text) + "</font><br>");
                m_session->sendAction(((IRCChannelTab *)tab)->channel(), text);
            } else if (tab->isA("IRCQueryTab")) {
                tab->appendText("<font color=\"" + m_selfColor + "\">*" + IRCOutput::toHTML(m_server.nick()) + " " + IRCOutput::toHTML(text) + "</font><br>");
                m_session->sendAction(((IRCQueryTab *)tab)->person(), text);
            } else {
                tab->appendText("<font color=\"" + m_errorColor + "\">Invalid tab for this command</font><br>");
            }
        }
    }
  //SEND PRIVMSG
  else if (command == "MSG") {
        QString nickname;
        stream >> nickname;
        if (nickname.length() > 0) {
            if (line.length() > 6 + nickname.length()) {
                QString text = line.right(line.length()-nickname.length()-6);
                IRCPerson person;
                person.setNick(nickname);
                tab->appendText("<font color=\"" + m_textColor + "\">&gt;</font><font color=\"" + m_otherColor + "\">"+IRCOutput::toHTML(nickname)+"</font><font color=\"" + m_textColor + "\">&lt; "+IRCOutput::toHTML(text)+"</font><br>");
                m_session->sendMessage(&person, text);
            }
        }
    }
	else {
        tab->appendText("<font color=\"" + m_errorColor + "\">Unknown command</font><br>");
    }
}

void IRCServerTab::processCommand() {
    QString text = m_field->text();
    if (text.startsWith("/") && !text.startsWith("//")) {
        /* Command mode */
        executeCommand(this, text);
    }
    m_field->clear();
}

void IRCServerTab::doConnect() {
    m_session->beginSession();
}

void IRCServerTab::remove() {
    /* Close requested */
    if (m_session->isSessionActive()) {
        /* While there is a running session */
        m_close = TRUE;
        m_session->endSession();
    } else {
        /* Session has previously been closed */
        m_channelTabs.first();
        while (m_channelTabs.current() != 0) {
            m_mainWindow->killTab(m_channelTabs.current(), true);
        }
        m_queryTabs.first();
        while (m_queryTabs.current() != 0) {
            m_mainWindow->killTab(m_queryTabs.current(), true);
        }
        m_mainWindow->killTab(this);
    }
}

IRCChannelTab *IRCServerTab::getTabForChannel(IRCChannel *channel) {
    QListIterator<IRCChannelTab> it(m_channelTabs);

    for (; it.current(); ++it) {
        if (it.current()->channel() == channel)
            return it.current();
    }
    return 0;
}

IRCQueryTab *IRCServerTab::getTabForQuery(IRCPerson *person) {
    QListIterator<IRCQueryTab> it(m_queryTabs);

    for (; it.current(); ++it) {
        if (it.current()->person()->nick() == person->nick())
            return it.current();
    }
    return 0;
}

void IRCServerTab::display(IRCOutput output) {

    /* All messages to be displayed inside the GUI get here */
    switch (output.type()) {
        case OUTPUT_CONNCLOSE:
            if (m_close) {
                m_channelTabs.first();
                while (m_channelTabs.current() != 0) {
                    m_mainWindow->killTab(m_channelTabs.current(), true);
                }
                m_queryTabs.first();
                while (m_queryTabs.current() != 0) {
                    m_mainWindow->killTab(m_queryTabs.current(), true);
                }
                m_mainWindow->killTab(this);
            } else {
                appendText("<font color=\"" + m_errorColor + "\">" + output.htmlMessage() +"</font><br>");
                QListIterator<IRCChannelTab> it(m_channelTabs);
                for (; it.current(); ++it) {
                    it.current()->appendText("<font color=\"" + m_serverColor + "\">" + output.htmlMessage() +"</font><br>");
                }
            }
            break;
        case OUTPUT_SELFJOIN: {
                IRCChannelTab *channeltab = new IRCChannelTab((IRCChannel *)output.getParam(0), this, m_mainWindow, (QWidget *)parent());
                m_channelTabs.append(channeltab);
                m_mainWindow->addTab(channeltab);
            }
            break;
        case OUTPUT_CHANPRIVMSG: {
                IRCChannelTab *channelTab = getTabForChannel((IRCChannel *)output.getParam(0));
                channelTab->appendText("<font color=\"" + m_textColor + "\">&lt;</font><font color=\"" + m_otherColor + "\">"+IRCOutput::toHTML(((IRCChannelPerson *)output.getParam(1))->nick())+"</font><font color=\"" + m_textColor + "\">&gt; " + output.htmlMessage()+"</font><br>");
            }
            break;
        case OUTPUT_QUERYACTION:
        case OUTPUT_QUERYPRIVMSG: {
                IRCQueryTab *queryTab = getTabForQuery((IRCPerson *)output.getParam(0));
                if (!queryTab) {
                    queryTab = new IRCQueryTab((IRCPerson *)output.getParam(0), this, m_mainWindow, (QWidget *)parent());
                    m_queryTabs.append(queryTab);
                    m_mainWindow->addTab(queryTab);
                }
                queryTab->display(output);
            }
            break;
        case OUTPUT_SELFPART: {
                IRCChannelTab *channelTab = getTabForChannel((IRCChannel *)output.getParam(0));
                if (channelTab)
                    m_mainWindow->killTab(channelTab, true);
            }
            break;
        case OUTPUT_SELFKICK: {
                appendText("<font color=\"" + m_errorColor + "\">" + output.htmlMessage() + "</font><br>");
                IRCChannelTab *channelTab = getTabForChannel((IRCChannel *)output.getParam(0));
                if (channelTab)
                    m_mainWindow->killTab(channelTab, true);
            }
            break;
        case OUTPUT_CHANACTION: {
                IRCChannelTab *channelTab = getTabForChannel((IRCChannel *)output.getParam(0));
                channelTab->appendText("<font color=\"" + m_otherColor + "\">"+output.htmlMessage()+"</font><br>");
            }
            break;
        case OUTPUT_TOPIC: {
                IRCChannel *channel = (IRCChannel *) output.getParam(0);
                if (channel) {
                    IRCChannelTab *channelTab = getTabForChannel(channel);
                    if (channelTab) {
                        channelTab->appendText("<font color=\"" + m_notificationColor + "\">"+output.htmlMessage()+"</font><br>");
                        return;
                    }
                }
                IRCChannelTab::enqueue(channel->channelname(), "<font color=\"" + m_notificationColor + "\">"+output.htmlMessage()+"</font><br>");
            }
            break;
        case OUTPUT_QUIT: {
                QString nick = ((IRCPerson *)output.getParam(0))->nick();
                QListIterator<IRCChannelTab> it(m_channelTabs);
                for (; it.current(); ++it) {
                    if (it.current()->list()->hasPerson(nick)) {
                        it.current()->appendText("<font color=\"" + m_notificationColor + "\">"+output.htmlMessage()+"</font><br>");
                        it.current()->list()->update();
                    }
                }
            }
            break;
        case OUTPUT_NICKCHANGE: {
                QString *nick = static_cast<QString*>(output.getParam(0));
                if(!nick) {
                    appendText("<font color=\"" + m_notificationColor + "\">"+output.htmlMessage()+"</font><br>");
                    break;
                }
                QListIterator<IRCChannelTab> it(m_channelTabs);
                for (; it.current(); ++it) {
                    if (it.current()->list()->hasPerson(*nick)) {
                        it.current()->appendText("<font color=\"" + m_notificationColor + "\">"+output.htmlMessage()+"</font><br>");
                    }
                }
                delete nick;
            }
            break;
        case OUTPUT_OTHERJOIN:
        case OUTPUT_OTHERKICK:
        case OUTPUT_CHANPERSONMODE:
        case OUTPUT_OTHERPART: {
                IRCChannelTab *channelTab = getTabForChannel((IRCChannel *)output.getParam(0));
                channelTab->appendText("<font color=\"" + m_notificationColor + "\">"+output.htmlMessage()+"</font><br>");
                channelTab->list()->update();
            }
            break;
        case OUTPUT_CTCP:
            appendText("<font color=\"" + m_notificationColor + "\">" + output.htmlMessage() + "</font><br>");
            break;
        case OUTPUT_ERROR:
            appendText("<font color=\"" + m_errorColor + "\">" + output.htmlMessage() + "</font><br>");
            break;
        case OUTPUT_TITLE:
            m_description->setText(output.message());
            break;
        default:
            appendText("<font color=\"" + m_serverColor + "\">" + output.htmlMessage() + "</font><br>");
            break;
    }
}

void IRCServerTab::slotUpdateChannels() {
    QListIterator<IRCChannelTab> it(m_channelTabs);
    for (; it.current(); ++it) {
        it.current()->list()->update();
    }
}

MainWindow *IRCServerTab::mainwindow() {
    return m_mainWindow;
}