00001 #include <opie2/odebug.h> 00002 00003 using namespace Opie::Core; 00004 00005 00006 #include <qtextstream.h> 00007 #include <qstring.h> 00008 #include <qstringlist.h> 00009 #include <qregexp.h> 00010 00011 #include "ircmessage.h" 00012 00013 /* 00014 * Create a new IRCMessage by evaluating 00015 * a received string 00016 */ 00017 00018 IRCMessage::IRCMessage(QString line) { 00019 /* Remove CRs from the message */ 00020 while((line.right(1) == "\n") || (line.right(1) == "\r")) 00021 line = line.left(line.length() - 1); 00022 QTextIStream stream(&line); 00023 QString temp; 00024 00025 stream >> temp; 00026 if (temp.startsWith(":")) { 00027 /* extract the prefix */ 00028 m_prefix = temp.right(temp.length()-1); 00029 stream >> temp; 00030 m_command = temp.upper(); 00031 m_allParameters = line.right(line.length() - m_prefix.length() - m_command.length() - 3); 00032 } else { 00033 m_command = temp.upper(); 00034 m_allParameters = line.right(line.length() - m_command.length() - 1); 00035 } 00036 00037 /* Create a list of all parameters */ 00038 while(!(stream.atEnd())) { 00039 stream >> temp; 00040 if (temp.startsWith(":")) { 00041 /* last parameter */ 00042 m_trailing = line.right(line.length() - line.find(" :") - 2); 00043 m_parameters << m_trailing; 00044 break; 00045 } else { 00046 m_parameters << temp; 00047 } 00048 } 00049 00050 00051 m_commandNumber = m_command.toInt(&m_isNumerical); 00052 /* Is this a CTCP command */ 00053 if ((m_command == "PRIVMSG" || m_command == "NOTICE") && m_trailing.length()>0 && m_trailing.left(1) == QChar(1)) { 00054 m_ctcp = TRUE; 00055 00056 m_ctcpRequest = (m_command == "PRIVMSG"); 00057 00058 /* Strip CTCP \001 characters */ 00059 m_allParameters = m_allParameters.replace(QRegExp(QChar(1)), ""); 00060 QTextIStream ctcpStream(&m_allParameters); 00061 ctcpStream >> m_ctcpDestination; 00062 ctcpStream >> temp; 00063 m_ctcpCommand = temp.upper().right(temp.length()-1); 00064 m_parameters.clear(); 00065 int length = m_allParameters.length() - m_ctcpCommand.length() - 1; 00066 length -= m_ctcpDestination.length() + 1; 00067 if (length <= 0) { 00068 m_allParameters = ""; 00069 } 00070 else { 00071 m_allParameters = m_allParameters.right(length); 00072 m_parameters << m_allParameters; 00073 } 00074 } 00075 else { 00076 m_ctcp = FALSE; 00077 } 00078 00079 00080 odebug << "Parsed: " << line << oendl; 00081 odebug << "Prefix: " << m_prefix << oendl; 00082 odebug << "Command: " << m_command << oendl; 00083 odebug << "Allparameters: " << m_allParameters << oendl; 00084 00085 for (unsigned int i=0; i<m_parameters.count(); i++) { 00086 odebug << "Parameter " << i << ":" << m_parameters[i] << oendl; 00087 } 00088 00089 if(m_ctcp) { 00090 odebug << "CTCP " << (m_ctcpRequest? "Request" : "Reply") << ": " << m_ctcpCommand << oendl; 00091 odebug << "CTCP Destination: " << m_ctcpDestination << oendl; 00092 odebug << "CTCP param count is: " << m_parameters.count() << oendl; 00093 } 00094 00095 } 00096 00097 QString IRCMessage::param(int param) { 00098 return m_parameters[param]; 00099 } 00100 00101 QStringList IRCMessage::params(const QString ¶mstring) const { 00102 QStringList params, retvalue; 00103 params = QStringList::split(',', paramstring); 00104 QStringList::Iterator end = params.end(); 00105 00106 for (QStringList::Iterator it = params.begin(); it != end; ++it) { 00107 int pos = (*it).find(':'); 00108 if(pos < 0) { 00109 if((*it).toUInt() < m_parameters.count()) 00110 retvalue << m_parameters[(*it).toUInt()]; 00111 } 00112 00113 else { 00114 unsigned int start, end; 00115 start = (*it).left(pos).toUInt(); 00116 end = (*it).mid(pos+1).toUInt(); 00117 for (unsigned int i=start;i<=end && i < m_parameters.count() ;++i) { 00118 retvalue << m_parameters[i]; 00119 } 00120 } 00121 } 00122 00123 return retvalue; 00124 } 00125 00126 QString IRCMessage::prefix() { 00127 return m_prefix; 00128 } 00129 00130 QString IRCMessage::command() { 00131 return m_command; 00132 } 00133 00134 QString IRCMessage::ctcpCommand() { 00135 return m_ctcpCommand; 00136 } 00137 00138 QString IRCMessage::ctcpDestination() { 00139 return m_ctcpDestination; 00140 } 00141 00142 unsigned short IRCMessage::commandNumber() { 00143 return m_commandNumber; 00144 } 00145 00146 bool IRCMessage::isNumerical() { 00147 return m_isNumerical; 00148 } 00149 00150 bool IRCMessage::isCTCP() { 00151 return m_ctcp; 00152 } 00153 00154 bool IRCMessage::isCTCPRequest() { 00155 return m_ctcpRequest; 00156 } 00157 00158 bool IRCMessage::isCTCPReply() { 00159 return !m_ctcpRequest; 00160 } 00161 00162 QString IRCMessage::trailing() { 00163 return m_trailing; 00164 } 00165 00166 QString IRCMessage::allParameters() { 00167 return m_allParameters; 00168 } 00169
1.4.2