-rw-r--r-- | noncore/net/opieirc/ircmessage.cpp | 27 | ||||
-rw-r--r-- | noncore/net/opieirc/ircmessage.h | 3 |
2 files changed, 24 insertions, 6 deletions
diff --git a/noncore/net/opieirc/ircmessage.cpp b/noncore/net/opieirc/ircmessage.cpp index d19e9e6..1b7072e 100644 --- a/noncore/net/opieirc/ircmessage.cpp +++ b/noncore/net/opieirc/ircmessage.cpp | |||
@@ -1,154 +1,169 @@ | |||
1 | #include <opie2/odebug.h> | 1 | #include <opie2/odebug.h> |
2 | 2 | ||
3 | using namespace Opie::Core; | 3 | using namespace Opie::Core; |
4 | 4 | ||
5 | 5 | ||
6 | #include <qtextstream.h> | 6 | #include <qtextstream.h> |
7 | #include <qstring.h> | 7 | #include <qstring.h> |
8 | #include <qstringlist.h> | 8 | #include <qstringlist.h> |
9 | #include <qregexp.h> | 9 | #include <qregexp.h> |
10 | 10 | ||
11 | #include "ircmessage.h" | 11 | #include "ircmessage.h" |
12 | 12 | ||
13 | /* | 13 | /* |
14 | * Create a new IRCMessage by evaluating | 14 | * Create a new IRCMessage by evaluating |
15 | * a received string | 15 | * a received string |
16 | */ | 16 | */ |
17 | 17 | ||
18 | IRCMessage::IRCMessage(QString line) { | 18 | IRCMessage::IRCMessage(QString line) { |
19 | /* Remove CRs from the message */ | 19 | /* Remove CRs from the message */ |
20 | while((line.right(1) == "\n") || (line.right(1) == "\r")) | 20 | while((line.right(1) == "\n") || (line.right(1) == "\r")) |
21 | line = line.left(line.length() - 1); | 21 | line = line.left(line.length() - 1); |
22 | QTextIStream stream(&line); | 22 | QTextIStream stream(&line); |
23 | QString temp; | 23 | QString temp; |
24 | 24 | ||
25 | stream >> temp; | 25 | stream >> temp; |
26 | if (temp.startsWith(":")) { | 26 | if (temp.startsWith(":")) { |
27 | /* extract the prefix */ | 27 | /* extract the prefix */ |
28 | m_prefix = temp.right(temp.length()-1); | 28 | m_prefix = temp.right(temp.length()-1); |
29 | stream >> temp; | 29 | stream >> temp; |
30 | m_command = temp.upper(); | 30 | m_command = temp.upper(); |
31 | m_allParameters = line.right(line.length() - m_prefix.length() - m_command.length() - 3); | 31 | m_allParameters = line.right(line.length() - m_prefix.length() - m_command.length() - 3); |
32 | } else { | 32 | } else { |
33 | m_command = temp.upper(); | 33 | m_command = temp.upper(); |
34 | m_allParameters = line.right(line.length() - m_command.length() - 1); | 34 | m_allParameters = line.right(line.length() - m_command.length() - 1); |
35 | } | 35 | } |
36 | 36 | ||
37 | /* Create a list of all parameters */ | 37 | /* Create a list of all parameters */ |
38 | while(!(stream.atEnd())) { | 38 | while(!(stream.atEnd())) { |
39 | stream >> temp; | 39 | stream >> temp; |
40 | if (temp.startsWith(":")) { | 40 | if (temp.startsWith(":")) { |
41 | /* last parameter */ | 41 | /* last parameter */ |
42 | m_trailing = line.right(line.length() - line.find(QChar(':'), 1) - 1); | 42 | m_trailing = line.right(line.length() - line.find(QChar(':'), 1) - 1); |
43 | m_parameters << m_trailing; | 43 | m_parameters << m_trailing; |
44 | break; | 44 | break; |
45 | } else { | 45 | } else { |
46 | m_parameters << temp; | 46 | m_parameters << temp; |
47 | } | 47 | } |
48 | } | 48 | } |
49 | 49 | ||
50 | 50 | ||
51 | m_commandNumber = m_command.toInt(&m_isNumerical); | 51 | m_commandNumber = m_command.toInt(&m_isNumerical); |
52 | /* Is this a CTCP command */ | 52 | /* Is this a CTCP command */ |
53 | if ((m_command == "PRIVMSG" || m_command == "NOTICE") && m_trailing.length()>0 && m_trailing.left(1) == QChar(1)) { | 53 | if ((m_command == "PRIVMSG" || m_command == "NOTICE") && m_trailing.length()>0 && m_trailing.left(1) == QChar(1)) { |
54 | m_ctcp = TRUE; | 54 | m_ctcp = TRUE; |
55 | if (m_command == "PRIVMSG") { | ||
56 | m_ctcpRequest = TRUE; | ||
57 | } | ||
58 | else { | ||
59 | m_ctcpRequest = FALSE; | ||
60 | } | ||
61 | |||
55 | /* Strip CTCP \001 characters */ | 62 | /* Strip CTCP \001 characters */ |
56 | m_allParameters = m_allParameters.replace(QRegExp(QChar(1)), ""); | 63 | m_allParameters = m_allParameters.replace(QRegExp(QChar(1)), ""); |
57 | QTextIStream ctcpStream(&m_allParameters); | 64 | QTextIStream ctcpStream(&m_allParameters); |
58 | if (m_command == "PRIVMSG") | 65 | ctcpStream >> m_ctcpDestination; |
59 | ctcpStream >> m_ctcpDestination; | ||
60 | ctcpStream >> temp; | 66 | ctcpStream >> temp; |
61 | m_ctcpCommand = temp.upper().right(temp.length()-1); | 67 | m_ctcpCommand = temp.upper().right(temp.length()-1); |
62 | m_parameters.clear(); | 68 | m_parameters.clear(); |
63 | int length = m_allParameters.length() - m_ctcpCommand.length() - 1; | 69 | int length = m_allParameters.length() - m_ctcpCommand.length() - 1; |
64 | if (m_command == "PRIVMSG") | 70 | length -= m_ctcpDestination.length() + 1; |
65 | length -= m_ctcpDestination.length() + 1; | ||
66 | if (length <= 0) { | 71 | if (length <= 0) { |
67 | m_allParameters = ""; | 72 | m_allParameters = ""; |
68 | } else { | 73 | } |
74 | else { | ||
69 | m_allParameters = m_allParameters.right(length); | 75 | m_allParameters = m_allParameters.right(length); |
70 | m_parameters << m_allParameters; | 76 | m_parameters << m_allParameters; |
71 | } | 77 | } |
72 | } else { | 78 | } |
79 | else { | ||
73 | m_ctcp = FALSE; | 80 | m_ctcp = FALSE; |
74 | } | 81 | } |
75 | 82 | ||
76 | 83 | ||
77 | odebug << "Parsed: " << line << oendl; | 84 | odebug << "Parsed: " << line << oendl; |
78 | odebug << "Prefix: " << m_prefix << oendl; | 85 | odebug << "Prefix: " << m_prefix << oendl; |
79 | odebug << "Command: " << m_command << oendl; | 86 | odebug << "Command: " << m_command << oendl; |
80 | odebug << "Allparameters: " << m_allParameters << oendl; | 87 | odebug << "Allparameters: " << m_allParameters << oendl; |
81 | for (unsigned int i=0; i<m_parameters.count(); i++) { | 88 | for (unsigned int i=0; i<m_parameters.count(); i++) { |
82 | odebug << "Parameter " << i << ":" << m_parameters[i] << oendl; | 89 | odebug << "Parameter " << i << ":" << m_parameters[i] << oendl; |
83 | } | 90 | } |
84 | odebug << "CTCP Command: " << m_ctcpCommand << oendl; | 91 | odebug << "CTCP Command: " << m_ctcpCommand << oendl; |
85 | odebug << "CTCP Destination: " << m_ctcpDestination << oendl; | 92 | odebug << "CTCP Destination: " << m_ctcpDestination << oendl; |
86 | odebug << "CTCP param count is: " << m_parameters.count() << oendl; | 93 | odebug << "CTCP param count is: " << m_parameters.count() << oendl; |
87 | 94 | ||
88 | } | 95 | } |
89 | 96 | ||
90 | QString IRCMessage::param(int param) { | 97 | QString IRCMessage::param(int param) { |
91 | return m_parameters[param]; | 98 | return m_parameters[param]; |
92 | } | 99 | } |
93 | 100 | ||
94 | QStringList IRCMessage::params(const QString ¶mstring) const { | 101 | QStringList IRCMessage::params(const QString ¶mstring) const { |
95 | QStringList params, retvalue; | 102 | QStringList params, retvalue; |
96 | params = QStringList::split(',', paramstring); | 103 | params = QStringList::split(',', paramstring); |
97 | QStringList::Iterator end = params.end(); | 104 | QStringList::Iterator end = params.end(); |
98 | 105 | ||
99 | for (QStringList::Iterator it = params.begin(); it != end; ++it) { | 106 | for (QStringList::Iterator it = params.begin(); it != end; ++it) { |
100 | int pos = (*it).find(':'); | 107 | int pos = (*it).find(':'); |
101 | if(pos < 0) { | 108 | if(pos < 0) { |
102 | if((*it).toUInt() < m_parameters.count()) | 109 | if((*it).toUInt() < m_parameters.count()) |
103 | retvalue << m_parameters[(*it).toUInt()]; | 110 | retvalue << m_parameters[(*it).toUInt()]; |
104 | } | 111 | } |
105 | 112 | ||
106 | else { | 113 | else { |
107 | unsigned int start, end; | 114 | unsigned int start, end; |
108 | start = (*it).left(pos).toUInt(); | 115 | start = (*it).left(pos).toUInt(); |
109 | end = (*it).mid(pos+1).toUInt(); | 116 | end = (*it).mid(pos+1).toUInt(); |
110 | for (unsigned int i=start;i<=end && i < m_parameters.count() ;++i) { | 117 | for (unsigned int i=start;i<=end && i < m_parameters.count() ;++i) { |
111 | retvalue << m_parameters[i]; | 118 | retvalue << m_parameters[i]; |
112 | } | 119 | } |
113 | } | 120 | } |
114 | } | 121 | } |
115 | 122 | ||
116 | return retvalue; | 123 | return retvalue; |
117 | } | 124 | } |
118 | 125 | ||
119 | QString IRCMessage::prefix() { | 126 | QString IRCMessage::prefix() { |
120 | return m_prefix; | 127 | return m_prefix; |
121 | } | 128 | } |
122 | 129 | ||
123 | QString IRCMessage::command() { | 130 | QString IRCMessage::command() { |
124 | return m_command; | 131 | return m_command; |
125 | } | 132 | } |
126 | 133 | ||
127 | QString IRCMessage::ctcpCommand() { | 134 | QString IRCMessage::ctcpCommand() { |
128 | return m_ctcpCommand; | 135 | return m_ctcpCommand; |
129 | } | 136 | } |
130 | 137 | ||
131 | QString IRCMessage::ctcpDestination() { | 138 | QString IRCMessage::ctcpDestination() { |
132 | return m_ctcpDestination; | 139 | return m_ctcpDestination; |
133 | } | 140 | } |
134 | 141 | ||
135 | unsigned short IRCMessage::commandNumber() { | 142 | unsigned short IRCMessage::commandNumber() { |
136 | return m_commandNumber; | 143 | return m_commandNumber; |
137 | } | 144 | } |
138 | 145 | ||
139 | bool IRCMessage::isNumerical() { | 146 | bool IRCMessage::isNumerical() { |
140 | return m_isNumerical; | 147 | return m_isNumerical; |
141 | } | 148 | } |
142 | 149 | ||
143 | bool IRCMessage::isCTCP() { | 150 | bool IRCMessage::isCTCP() { |
144 | return m_ctcp; | 151 | return m_ctcp; |
145 | } | 152 | } |
146 | 153 | ||
154 | bool IRCMessage::isCTCPRequest() { | ||
155 | return m_ctcpRequest; | ||
156 | } | ||
157 | |||
158 | bool IRCMessage::isCTCPReply() { | ||
159 | return !m_ctcpRequest; | ||
160 | } | ||
161 | |||
147 | QString IRCMessage::trailing() { | 162 | QString IRCMessage::trailing() { |
148 | return m_trailing; | 163 | return m_trailing; |
149 | } | 164 | } |
150 | 165 | ||
151 | QString IRCMessage::allParameters() { | 166 | QString IRCMessage::allParameters() { |
152 | return m_allParameters; | 167 | return m_allParameters; |
153 | } | 168 | } |
154 | 169 | ||
diff --git a/noncore/net/opieirc/ircmessage.h b/noncore/net/opieirc/ircmessage.h index 10ba450..40bb210 100644 --- a/noncore/net/opieirc/ircmessage.h +++ b/noncore/net/opieirc/ircmessage.h | |||
@@ -1,70 +1,73 @@ | |||
1 | /* | 1 | /* |
2 | OpieIRC - An embedded IRC client | 2 | OpieIRC - An embedded IRC client |
3 | Copyright (C) 2002 Wenzel Jakob | 3 | Copyright (C) 2002 Wenzel Jakob |
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify | 5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | 6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or | 7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. | 8 | (at your option) any later version. |
9 | 9 | ||
10 | This program is distributed in the hope that it will be useful, | 10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. | 13 | GNU General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | 16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | 18 | ||
19 | */ | 19 | */ |
20 | 20 | ||
21 | #ifndef __IRCMESSAGE_H | 21 | #ifndef __IRCMESSAGE_H |
22 | #define __IRCMESSAGE_H | 22 | #define __IRCMESSAGE_H |
23 | 23 | ||
24 | class QString; | 24 | class QString; |
25 | class QStringList; | 25 | class QStringList; |
26 | 26 | ||
27 | /* IRCMessage objects are used to encapsulate information | 27 | /* IRCMessage objects are used to encapsulate information |
28 | which the IRC server sent to us. */ | 28 | which the IRC server sent to us. */ |
29 | 29 | ||
30 | class IRCMessage { | 30 | class IRCMessage { |
31 | public: | 31 | public: |
32 | /* Parse an IRC message and create the IRCMessage object */ | 32 | /* Parse an IRC message and create the IRCMessage object */ |
33 | IRCMessage(QString line); | 33 | IRCMessage(QString line); |
34 | 34 | ||
35 | /* Return the IRC message prefix (usually sender etc) */ | 35 | /* Return the IRC message prefix (usually sender etc) */ |
36 | QString prefix(); | 36 | QString prefix(); |
37 | /* Check if this IRCMessage's command is literal or numerical */ | 37 | /* Check if this IRCMessage's command is literal or numerical */ |
38 | bool isNumerical(); | 38 | bool isNumerical(); |
39 | /* CHeck if this IRCMessage is a CTCP message */ | 39 | /* CHeck if this IRCMessage is a CTCP message */ |
40 | bool isCTCP(); | 40 | bool isCTCP(); |
41 | bool isCTCPRequest(); | ||
42 | bool isCTCPReply(); | ||
41 | /* Return the IRC command (literal commands) */ | 43 | /* Return the IRC command (literal commands) */ |
42 | QString command(); | 44 | QString command(); |
43 | /* Return the CTCP command */ | 45 | /* Return the CTCP command */ |
44 | QString ctcpCommand(); | 46 | QString ctcpCommand(); |
45 | /* Return the CTCP destination if applicable (channel/person) */ | 47 | /* Return the CTCP destination if applicable (channel/person) */ |
46 | QString ctcpDestination(); | 48 | QString ctcpDestination(); |
47 | /* Return the IRC command (numerical commands) */ | 49 | /* Return the IRC command (numerical commands) */ |
48 | unsigned short commandNumber(); | 50 | unsigned short commandNumber(); |
49 | /* Return the trailing parameter string */ | 51 | /* Return the trailing parameter string */ |
50 | QString trailing(); | 52 | QString trailing(); |
51 | /* Return the complete parameter string */ | 53 | /* Return the complete parameter string */ |
52 | QString allParameters(); | 54 | QString allParameters(); |
53 | /* Return one parameter */ | 55 | /* Return one parameter */ |
54 | QString param(int param); | 56 | QString param(int param); |
55 | /* Return some parameters */ | 57 | /* Return some parameters */ |
56 | QStringList params(const QString ¶mstring) const; | 58 | QStringList params(const QString ¶mstring) const; |
57 | protected: | 59 | protected: |
58 | QString m_prefix; | 60 | QString m_prefix; |
59 | QString m_command; | 61 | QString m_command; |
60 | QString m_ctcpCommand; | 62 | QString m_ctcpCommand; |
61 | QString m_ctcpDestination; | 63 | QString m_ctcpDestination; |
62 | unsigned short m_commandNumber; | 64 | unsigned short m_commandNumber; |
63 | QString m_allParameters; | 65 | QString m_allParameters; |
64 | QString m_trailing; | 66 | QString m_trailing; |
65 | QStringList m_parameters; | 67 | QStringList m_parameters; |
66 | bool m_isNumerical; | 68 | bool m_isNumerical; |
67 | bool m_ctcp; | 69 | bool m_ctcp; |
70 | bool m_ctcpRequest; | ||
68 | }; | 71 | }; |
69 | 72 | ||
70 | #endif | 73 | #endif |