-rw-r--r-- | noncore/net/opieirc/ircmessage.cpp | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/noncore/net/opieirc/ircmessage.cpp b/noncore/net/opieirc/ircmessage.cpp index d0b2652..41386ee 100644 --- a/noncore/net/opieirc/ircmessage.cpp +++ b/noncore/net/opieirc/ircmessage.cpp | |||
@@ -1,151 +1,152 @@ | |||
1 | #include <qtextstream.h> | 1 | #include <qtextstream.h> |
2 | #include <qstring.h> | 2 | #include <qstring.h> |
3 | #include <qstringlist.h> | 3 | #include <qstringlist.h> |
4 | #include <qregexp.h> | ||
4 | 5 | ||
5 | #include "ircmessage.h" | 6 | #include "ircmessage.h" |
6 | 7 | ||
7 | /* | 8 | /* |
8 | * Create a new IRCMessage by evaluating | 9 | * Create a new IRCMessage by evaluating |
9 | * a received string | 10 | * a received string |
10 | */ | 11 | */ |
11 | 12 | ||
12 | IRCMessage::IRCMessage(QString line) { | 13 | IRCMessage::IRCMessage(QString line) { |
13 | /* Remove CRs from the message */ | 14 | /* Remove CRs from the message */ |
14 | while((line.right(1) == "\n") || (line.right(1) == "\r")) | 15 | while((line.right(1) == "\n") || (line.right(1) == "\r")) |
15 | line = line.left(line.length() - 1); | 16 | line = line.left(line.length() - 1); |
16 | QTextIStream stream(&line); | 17 | QTextIStream stream(&line); |
17 | QString temp; | 18 | QString temp; |
18 | 19 | ||
19 | stream >> temp; | 20 | stream >> temp; |
20 | if (temp.startsWith(":")) { | 21 | if (temp.startsWith(":")) { |
21 | /* extract the prefix */ | 22 | /* extract the prefix */ |
22 | m_prefix = temp.right(temp.length()-1); | 23 | m_prefix = temp.right(temp.length()-1); |
23 | stream >> temp; | 24 | stream >> temp; |
24 | m_command = temp.upper(); | 25 | m_command = temp.upper(); |
25 | m_allParameters = line.right(line.length() - m_prefix.length() - m_command.length() - 3); | 26 | m_allParameters = line.right(line.length() - m_prefix.length() - m_command.length() - 3); |
26 | } else { | 27 | } else { |
27 | m_command = temp.upper(); | 28 | m_command = temp.upper(); |
28 | m_allParameters = line.right(line.length() - m_command.length() - 1); | 29 | m_allParameters = line.right(line.length() - m_command.length() - 1); |
29 | } | 30 | } |
30 | 31 | ||
31 | /* Create a list of all parameters */ | 32 | /* Create a list of all parameters */ |
32 | while(!(stream.atEnd())) { | 33 | while(!(stream.atEnd())) { |
33 | stream >> temp; | 34 | stream >> temp; |
34 | if (temp.startsWith(":")) { | 35 | if (temp.startsWith(":")) { |
35 | /* last parameter */ | 36 | /* last parameter */ |
36 | m_trailing = line.right(line.length() - line.find(QChar(':'), 1) - 1); | 37 | m_trailing = line.right(line.length() - line.find(QChar(':'), 1) - 1); |
37 | m_parameters << m_trailing; | 38 | m_parameters << m_trailing; |
38 | break; | 39 | break; |
39 | } else { | 40 | } else { |
40 | m_parameters << temp; | 41 | m_parameters << temp; |
41 | } | 42 | } |
42 | } | 43 | } |
43 | 44 | ||
44 | 45 | ||
45 | m_commandNumber = m_command.toInt(&m_isNumerical); | 46 | m_commandNumber = m_command.toInt(&m_isNumerical); |
46 | /* Is this a CTCP command */ | 47 | /* Is this a CTCP command */ |
47 | if ((m_command == "PRIVMSG" || m_command == "NOTICE") && m_trailing.length()>0 && m_trailing.left(1) == QChar(1)) { | 48 | if ((m_command == "PRIVMSG" || m_command == "NOTICE") && m_trailing.length()>0 && m_trailing.left(1) == QChar(1)) { |
48 | m_ctcp = TRUE; | 49 | m_ctcp = TRUE; |
49 | /* Strip CTCP \001 characters */ | 50 | /* Strip CTCP \001 characters */ |
50 | m_allParameters = m_allParameters.replace(QRegExp(QChar(1)), ""); | 51 | m_allParameters = m_allParameters.replace(QRegExp(QChar(1)), ""); |
51 | QTextIStream ctcpStream(&m_allParameters); | 52 | QTextIStream ctcpStream(&m_allParameters); |
52 | if (m_command == "PRIVMSG") | 53 | if (m_command == "PRIVMSG") |
53 | ctcpStream >> m_ctcpDestination; | 54 | ctcpStream >> m_ctcpDestination; |
54 | ctcpStream >> temp; | 55 | ctcpStream >> temp; |
55 | m_ctcpCommand = temp.upper().right(temp.length()-1); | 56 | m_ctcpCommand = temp.upper().right(temp.length()-1); |
56 | m_parameters.clear(); | 57 | m_parameters.clear(); |
57 | int length = m_allParameters.length() - m_ctcpCommand.length() - 1; | 58 | int length = m_allParameters.length() - m_ctcpCommand.length() - 1; |
58 | if (m_command == "PRIVMSG") | 59 | if (m_command == "PRIVMSG") |
59 | length -= m_ctcpDestination.length() + 1; | 60 | length -= m_ctcpDestination.length() + 1; |
60 | if (length <= 0) { | 61 | if (length <= 0) { |
61 | m_allParameters = ""; | 62 | m_allParameters = ""; |
62 | } else { | 63 | } else { |
63 | m_allParameters = m_allParameters.right(length); | 64 | m_allParameters = m_allParameters.right(length); |
64 | m_parameters << m_allParameters; | 65 | m_parameters << m_allParameters; |
65 | } | 66 | } |
66 | } else { | 67 | } else { |
67 | m_ctcp = FALSE; | 68 | m_ctcp = FALSE; |
68 | } | 69 | } |
69 | 70 | ||
70 | /* | 71 | |
71 | //-- Uncomment to debug -- | 72 | //-- Uncomment to debug -- |
72 | 73 | /* | |
73 | printf("Parsed : '%s'\n", line.ascii()); | 74 | printf("Parsed : '%s'\n", line.ascii()); |
74 | printf("Prefix : '%s'\n", m_prefix.ascii()); | 75 | printf("Prefix : '%s'\n", m_prefix.ascii()); |
75 | printf("Command : '%s'\n", m_command.ascii()); | 76 | printf("Command : '%s'\n", m_command.ascii()); |
76 | printf("Allparameters : '%s'\n", m_allParameters.ascii()); | 77 | printf("Allparameters : '%s'\n", m_allParameters.ascii()); |
77 | for (unsigned int i=0; i<m_parameters.count(); i++) { | 78 | for (unsigned int i=0; i<m_parameters.count(); i++) { |
78 | printf("Parameter %i : '%s'\n", i, m_parameters[i].ascii()); | 79 | printf("Parameter %i : '%s'\n", i, m_parameters[i].ascii()); |
79 | } | 80 | } |
80 | printf("CTCP Command : '%s'\n", m_ctcpCommand.latin1()); | 81 | printf("CTCP Command : '%s'\n", m_ctcpCommand.latin1()); |
81 | printf("CTCP Destination : '%s'\n", m_ctcpDestination.latin1()); | 82 | printf("CTCP Destination : '%s'\n", m_ctcpDestination.latin1()); |
82 | printf("CTCP param count is : '%i'\n", m_parameters.count()); | 83 | printf("CTCP param count is : '%i'\n", m_parameters.count()); |
83 | |||
84 | */ | 84 | */ |
85 | |||
85 | } | 86 | } |
86 | 87 | ||
87 | QString IRCMessage::param(int param) { | 88 | QString IRCMessage::param(int param) { |
88 | return m_parameters[param]; | 89 | return m_parameters[param]; |
89 | } | 90 | } |
90 | 91 | ||
91 | QStringList IRCMessage::params(const QString ¶mstring) const { | 92 | QStringList IRCMessage::params(const QString ¶mstring) const { |
92 | QStringList params, retvalue; | 93 | QStringList params, retvalue; |
93 | params = QStringList::split(',', paramstring); | 94 | params = QStringList::split(',', paramstring); |
94 | QStringList::Iterator end = params.end(); | 95 | QStringList::Iterator end = params.end(); |
95 | 96 | ||
96 | for (QStringList::Iterator it = params.begin(); it != end; ++it) { | 97 | for (QStringList::Iterator it = params.begin(); it != end; ++it) { |
97 | int pos = (*it).find(':'); | 98 | int pos = (*it).find(':'); |
98 | if(pos < 0) { | 99 | if(pos < 0) { |
99 | if((*it).toInt() < m_parameters.count()) | 100 | if(static_cast<unsigned int>((*it).toInt()) < m_parameters.count()) |
100 | retvalue << m_parameters[(*it).toInt()]; | 101 | retvalue << m_parameters[(*it).toInt()]; |
101 | } | 102 | } |
102 | 103 | ||
103 | else { | 104 | else { |
104 | int start, end; | 105 | int start, end; |
105 | start = (*it).left(pos).toInt(); | 106 | start = (*it).left(pos).toInt(); |
106 | end = (*it).mid(pos+1).toInt(); | 107 | end = (*it).mid(pos+1).toInt(); |
107 | for (int i=start;i<=end && i < m_parameters.count() ;++i) { | 108 | for (int i=start;i<=end && i < static_cast<int>(m_parameters.count()) ;++i) { |
108 | retvalue << m_parameters[i]; | 109 | retvalue << m_parameters[i]; |
109 | } | 110 | } |
110 | } | 111 | } |
111 | } | 112 | } |
112 | 113 | ||
113 | return retvalue; | 114 | return retvalue; |
114 | } | 115 | } |
115 | 116 | ||
116 | QString IRCMessage::prefix() { | 117 | QString IRCMessage::prefix() { |
117 | return m_prefix; | 118 | return m_prefix; |
118 | } | 119 | } |
119 | 120 | ||
120 | QString IRCMessage::command() { | 121 | QString IRCMessage::command() { |
121 | return m_command; | 122 | return m_command; |
122 | } | 123 | } |
123 | 124 | ||
124 | QString IRCMessage::ctcpCommand() { | 125 | QString IRCMessage::ctcpCommand() { |
125 | return m_ctcpCommand; | 126 | return m_ctcpCommand; |
126 | } | 127 | } |
127 | 128 | ||
128 | QString IRCMessage::ctcpDestination() { | 129 | QString IRCMessage::ctcpDestination() { |
129 | return m_ctcpDestination; | 130 | return m_ctcpDestination; |
130 | } | 131 | } |
131 | 132 | ||
132 | unsigned short IRCMessage::commandNumber() { | 133 | unsigned short IRCMessage::commandNumber() { |
133 | return m_commandNumber; | 134 | return m_commandNumber; |
134 | } | 135 | } |
135 | 136 | ||
136 | bool IRCMessage::isNumerical() { | 137 | bool IRCMessage::isNumerical() { |
137 | return m_isNumerical; | 138 | return m_isNumerical; |
138 | } | 139 | } |
139 | 140 | ||
140 | bool IRCMessage::isCTCP() { | 141 | bool IRCMessage::isCTCP() { |
141 | return m_ctcp; | 142 | return m_ctcp; |
142 | } | 143 | } |
143 | 144 | ||
144 | QString IRCMessage::trailing() { | 145 | QString IRCMessage::trailing() { |
145 | return m_trailing; | 146 | return m_trailing; |
146 | } | 147 | } |
147 | 148 | ||
148 | QString IRCMessage::allParameters() { | 149 | QString IRCMessage::allParameters() { |
149 | return m_allParameters; | 150 | return m_allParameters; |
150 | } | 151 | } |
151 | 152 | ||