summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/ircsession.cpp
blob: d87ff80ee134f1a93d4588944bac677912e66400 (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

#include "ircsession.h"
#include "ircmessageparser.h"
#include "ircchannelperson.h"
#include "ircversion.h"

IRCSession::IRCSession(QObject *parent, IRCServer *server)
    : QObject(parent)
{
    m_server = server;
    m_connection = new IRCConnection(m_server);
    m_parser     = new IRCMessageParser(this);
    connect(m_connection, SIGNAL(messageArrived(IRCMessage*)), this, SLOT(handleMessage(IRCMessage*)));
    connect(m_parser, SIGNAL(outputReady(IRCOutput)), this, SIGNAL(outputReady(IRCOutput)));
    connect(m_connection, SIGNAL(outputReady(IRCOutput)), this, SIGNAL(outputReady(IRCOutput)));
}

IRCSession::~IRCSession() {
    /* We want this to get deleted automatically */
    m_channels.setAutoDelete(TRUE);
    m_people.setAutoDelete(TRUE);
        
    delete m_parser;
    delete m_connection;
}

void IRCSession::beginSession() {
    m_connection->doConnect();
}

void IRCSession::join(QString channelname) {
    m_connection->sendLine("JOIN " + channelname);
}

void IRCSession::quit(){
    m_connection->sendLine("QUIT :[OI] I'm too good to need a reason");
}

void IRCSession::quit(QString message){
    m_connection->sendLine("QUIT :" + message);
}

void IRCSession::topic(IRCChannel *channel, QString message){
    m_connection->sendLine("TOPIC :" + channel->channelname() + " " + message);
}

void IRCSession::mode(IRCChannel *channel, QString message){
    m_connection->sendLine("MODE " + channel->channelname() + " " + message);
}

void IRCSession::mode(IRCPerson *person, QString message){
    m_connection->sendLine("MODE " + person->nick() + " " + message);
}

void IRCSession::mode(QString message){
    m_connection->sendLine("MODE " + message);
}

void IRCSession::raw(QString message){
    m_connection->sendLine(message);
}

void IRCSession::kick(IRCChannel *channel, IRCPerson *person) {
    m_connection->sendLine("KICK " + channel->channelname() + " " + person->nick() +" :0wn3d - no reason");
}

void IRCSession::op(IRCChannel *channel, IRCPerson *person) {
    m_connection->sendLine("MODE " + channel->channelname() + " +ooo " + person->nick());
}

void IRCSession::kick(IRCChannel *channel, IRCPerson *person, QString message) {
    m_connection->sendLine("KICK " + channel->channelname() + " " + person->nick() +" :" + message);
} 

void IRCSession::sendMessage(IRCPerson *person, QString message) {
    m_connection->sendLine("PRIVMSG " + person->nick() + " :" + message);
}

void IRCSession::sendMessage(IRCChannel *channel, QString message) {
    m_connection->sendLine("PRIVMSG " + channel->channelname() + " :" + message);
}

void IRCSession::sendAction(IRCChannel *channel, QString message) {
    m_connection->sendLine("PRIVMSG " + channel->channelname() + " :\001ACTION " + message + "\001");
}

void IRCSession::sendAction(IRCPerson *person, QString message) {
    m_connection->sendLine("PRIVMSG " + person->nick() + " :\001ACTION " + message + "\001");
}

bool IRCSession::isSessionActive() {
    return m_connection->isConnected(); 
}

bool IRCSession::isLoggedIn() {
    return m_connection->isLoggedIn();
}

void IRCSession::endSession() {
    if (m_connection->isLoggedIn())
        quit(APP_VERSION);
    else
        m_connection->close();
}

void IRCSession::part(IRCChannel *channel) {
    m_connection->sendLine("PART " + channel->channelname() + " :" + APP_VERSION);
}

void IRCSession::setValidUsermodes(const QString &modes) {
    m_validUsermodes = modes;
}

void IRCSession::setValidChannelmodes(const QString &modes) {
    m_validChannelmodes = modes;
}

void IRCSession::updateNickname(const QString &oldNickname, const QString &newNickname) {
    QList<IRCChannel> channels;
    IRCOutput output;
    
    if (oldNickname == m_server->nick()) {
        m_server->setNick(newNickname);
        output = IRCOutput(OUTPUT_NICKCHANGE, tr("You are now known as %1").arg(newNickname));
        channels = m_channels;
    }

    else {
        IRCPerson *person = getPerson(oldNickname);
        
        if(!person) {
            emit outputReady(IRCOutput(OUTPUT_ERROR, tr("Nickname change of an unknown person")));
            return;
        }
        
        getChannelsByPerson(person, channels);
        output = IRCOutput(OUTPUT_NICKCHANGE, tr("%1 is now known as %2").arg(oldNickname).arg(newNickname));
    }

    QListIterator<IRCChannel> it(channels);
    for (;it.current(); ++it) {
        IRCChannelPerson *chanperson = it.current()->getPerson(oldNickname);
        if (chanperson) {
            it.current()->removePerson(chanperson);
            chanperson->setNick(newNickname);
            it.current()->addPerson(chanperson);
        }
    }

    emit updateChannels();
    output.addParam(new QString(newNickname));
    emit outputReady(output);
}

IRCChannel *IRCSession::getChannel(QString channelname) {
    QListIterator<IRCChannel> it(m_channels);
    for (; it.current(); ++it) {
        if (it.current()->channelname() == channelname) {
            return it.current();
        }
    }
    return 0;
}

IRCPerson *IRCSession::getPerson(QString nickname) {
    QListIterator<IRCPerson> it(m_people);
    for (; it.current(); ++it) {
        if (it.current()->nick() == nickname) {
            return it.current();
        }
    }
    return 0;
}

void IRCSession::getChannelsByPerson(IRCPerson *person, QList<IRCChannel> &channels) {
    QListIterator<IRCChannel> it(m_channels);
    for (; it.current(); ++it) {
        if (it.current()->getPerson(person->nick()) != 0) {
            channels.append(it.current());
        }
    }
}

void IRCSession::addPerson(IRCPerson *person) {
    m_people.append(person);
}

void IRCSession::addChannel(IRCChannel *channel) {
    m_channels.append(channel);
}

void IRCSession::removeChannel(IRCChannel *channel) {
    m_channels.remove(channel);
}

void IRCSession::removePerson(IRCPerson *person) {
    m_people.remove(person);
}

void IRCSession::handleMessage(IRCMessage *message) {
    m_parser->parse(message);
}

void IRCSession::whois(const QString &nickname) {
    m_connection->whois(nickname);
}

void IRCSession::sendCTCPPing(const QString &nickname) {
    m_connection->sendCTCPPing(nickname);
}

void IRCSession::sendCTCPRequest(const QString &nickname, const QString &type, const QString &args) {
    m_connection->sendCTCPRequest(nickname, type, args);
}

void IRCSession::sendCTCPReply(const QString &nickname, const QString &type, const QString &args) {
    m_connection->sendCTCPReply(nickname, type, args);
}