summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/ircmessage.cpp
blob: f4b09bc67e4844e09d9d8ac7c9f61617d1ac3b2f (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
#include <opie2/odebug.h>

using namespace Opie::Core;


#include <qtextstream.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qregexp.h>

#include "ircmessage.h"

/*
 * Create a new IRCMessage by evaluating
 * a received string 
 */

IRCMessage::IRCMessage(QString line) {
    /* Remove CRs from the message */
    while((line.right(1) == "\n") || (line.right(1) == "\r"))
        line = line.left(line.length() - 1);
    QTextIStream stream(&line);
    QString temp;
    
    stream >> temp;
    if (temp.startsWith(":")) {
        /* extract the prefix */
        m_prefix = temp.right(temp.length()-1);
        stream >> temp;
        m_command = temp.upper();
        m_allParameters = line.right(line.length() - m_prefix.length() - m_command.length() - 3);
    } else {
        m_command = temp.upper();
        m_allParameters = line.right(line.length() - m_command.length() - 1);
    }
    
    /* Create a list of all parameters */
    while(!(stream.atEnd())) {
        stream >> temp;
        if (temp.startsWith(":")) {
            /* last parameter */
            m_trailing = line.right(line.length() - line.find(" :") - 2);
            m_parameters << m_trailing;
            break;
        } else {
            m_parameters << temp;
        }
    }

    
    m_commandNumber = m_command.toInt(&m_isNumerical);
    /* Is this a CTCP command */
    if ((m_command == "PRIVMSG" || m_command == "NOTICE") && m_trailing.length()>0 && m_trailing.left(1) == QChar(1)) {
        m_ctcp = TRUE;
	
        m_ctcpRequest = (m_command == "PRIVMSG");
        
        /* Strip CTCP \001 characters */
        m_allParameters = m_allParameters.replace(QRegExp(QChar(1)), "");
        QTextIStream ctcpStream(&m_allParameters);
        ctcpStream >> m_ctcpDestination;
        ctcpStream >> temp;
        m_ctcpCommand = temp.upper().right(temp.length()-1);
        m_parameters.clear();
        int length = m_allParameters.length() - m_ctcpCommand.length() - 1;
        length -= m_ctcpDestination.length() + 1;
        if (length <= 0) {
            m_allParameters = "";
        } 
        else {
            m_allParameters = m_allParameters.right(length);
            m_parameters << m_allParameters;
        }
    } 
    else {
        m_ctcp = FALSE;
    }

    
    odebug << "Parsed: " << line << oendl;
    odebug << "Prefix: " << m_prefix << oendl;
    odebug << "Command: " << m_command << oendl;
    odebug << "Allparameters: " << m_allParameters << oendl;
    
    for (unsigned int i=0; i<m_parameters.count(); i++) {
        odebug << "Parameter " << i << ":" << m_parameters[i] << oendl;
    }
    
    if(m_ctcp) {
        odebug << "CTCP " << (m_ctcpRequest? "Request" : "Reply")  << ": " << m_ctcpCommand << oendl;
        odebug << "CTCP Destination: " << m_ctcpDestination << oendl;
        odebug << "CTCP param count is: " << m_parameters.count() << oendl;
    }
    
}

QString IRCMessage::param(int param) {
    return m_parameters[param];
}

QStringList IRCMessage::params(const QString &paramstring) const {
    QStringList params, retvalue;
    params = QStringList::split(',', paramstring);
    QStringList::Iterator end = params.end(); 
    
    for (QStringList::Iterator it = params.begin(); it != end; ++it) {
        int pos = (*it).find(':');
        if(pos < 0) {    
            if((*it).toUInt() < m_parameters.count())
                retvalue << m_parameters[(*it).toUInt()];
        }

        else {
            unsigned int start, end;
            start = (*it).left(pos).toUInt();
            end = (*it).mid(pos+1).toUInt();
            for (unsigned int i=start;i<=end && i < m_parameters.count() ;++i) {
                retvalue << m_parameters[i];
            }
        }      
    }
    
    return retvalue;
}

QString IRCMessage::prefix() {
    return m_prefix;
}

QString IRCMessage::command() {
    return m_command;
}

QString IRCMessage::ctcpCommand() {
    return m_ctcpCommand;
}

QString IRCMessage::ctcpDestination() {
    return m_ctcpDestination;
}

unsigned short IRCMessage::commandNumber() {
    return m_commandNumber;
}

bool IRCMessage::isNumerical() {
    return m_isNumerical;
}

bool IRCMessage::isCTCP() {
    return m_ctcp;
}

bool IRCMessage::isCTCPRequest() {
    return m_ctcpRequest;
}

bool IRCMessage::isCTCPReply() {
    return !m_ctcpRequest;
}

QString IRCMessage::trailing() {
    return m_trailing;
}

QString IRCMessage::allParameters() {
    return m_allParameters;
}