summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/libkirc/kircmessage.h
blob: 02631bbf034303d9baf3698d04366bab674849db (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
/*
    kircmessage.h - IRC Client

    Copyright (c) 2003      by Michel Hermier <michel.hermier@wanadoo.fr>

    Kopete    (c) 2003      by the Kopete developers <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#ifndef KIRCMESSAGE_H
#define KIRCMESSAGE_H

#include "kircentity.h"

#include <kbufferedio.h>

#include <tqdict.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqtextcodec.h>
#include <tqregexp.h>

#include <kopetemessage.h>

// Uncoment this if you want a really rfc compliant message handling.
// This is due to some changes of the message encoding with 14 arguments.(not very frequent :)
// #define _IRC_STRICTNESS_

namespace KIRC
{

class Engine;

class Message
{
public:
	/** \brief Sends the message as-is to the server.
	 */
	static void writeRawMessage(KIRC::Engine *engine, const TQTextCodec *codec, const TQString &str);

	static void writeMessage(KIRC::Engine *engine, const TQTextCodec *codec, const TQString &str);

	static void writeMessage(KIRC::Engine *engine, const TQTextCodec *codec,
		const TQString &command, const TQStringList &args, const TQString &suffix);

	static void writeCtcpMessage(KIRC::Engine *engine, const TQTextCodec *codec,
		const TQString &command, const TQString &to,
		const TQString &ctcpMessage);

	static void writeCtcpMessage(KIRC::Engine *engine, const TQTextCodec *codec,
		const TQString &command, const TQString &to, const TQString &suffix,
		const TQString &ctcpCommand, const TQStringList &ctcpArgs = TQStringList(), const TQString &ctcpSuffix = TQString::null );

	Message();
	Message(const KIRC::Message &obj);
	Message(const KIRC::Message *obj);

	~Message();

	inline const TQString nickFromPrefix() const
	{ return Kopete::Message::unescape(KIRC::Entity::userNick(m_prefix)); }

	TQString toString() const;

	/** \brief Returns true if the message command is numeric.
	 */
	bool isNumeric() const;

	/** \brief Message is valid if it was parsed correctly.
	 */
	bool isValid() const;

	/** \brief Writes internal message information about this message through kdDebug().
	 */
	void dump() const;

	/** \brief Re-decodes the message with given codec.
	 */
	void decodeAgain( const TQTextCodec *codec );

	/** \brief The whole message as received.
	 */
	inline const TQCString &raw() const
		{ return m_raw; }

	/** \brief Prefix of this message.
	 *
	 * Returns the prefix of the message. Note that it can be empty.
	 *
	 * Prefix is the server name or the nick name of the sender.
	 *
	 * message    =  [ ":" prefix SPACE ] command [ params ] crlf
	 * prefix     =  servername / ( nickname [ [ "!" user ] "@" host ] )
	 */
	inline const TQString &prefix() const
		{ return m_prefix; }

	/** \brief The command part of this message.
	 *
	 * Returns the command of this message. Can be numerical.
	 *
	 * Examples: "MODE", "PRIVMSG", 303, 001, ...
	 */
	inline const TQString &command() const
		{ return m_command; }

	/** \brief The number of command arguments this message contains.
	 */
	inline size_t argsSize() const
		{ return m_args.size(); }

	/** \brief i:th command argument.
	 */
	inline const TQString &arg(size_t i) const
		{ return m_args[i]; }

	/** \brief All command arguments.
	 */
	inline const TQStringList &args() const
		{ return m_args; }

	/** \brief Message suffix.
	 */
	inline const TQString &suffix() const
		{ return m_suffix; }
	inline const TQString &ctcpRaw() const
		{ return m_ctcpRaw; }

	inline bool hasCtcpMessage() const
		{ return m_ctcpMessage!=0; }
	inline class KIRC::Message &ctcpMessage() const
		{ return *m_ctcpMessage; }

	static KIRC::Message parse(KIRC::Engine *engine, const TQTextCodec *codec, bool *parseSuccess=0);

private:
	/**
	 * Contains the low level dequoted message.
	 */
	TQCString m_raw;

	/**
	 * Contains the completely dequoted prefix.
	 */
	TQString m_prefix;
	/**
	 * Contains the completely dequoted command.
	 */
	TQString m_command;
	/**
	 * Contains the completely dequoted args.
	 */
	TQStringList m_args;
	/**
	 * Contains the completely dequoted suffix.
	 */
	TQString m_suffix;

	/**
	 * If it is a message contains the completely dequoted rawCtcpLine.
	 * If it is a ctcp message contains the completely dequoted rawCtcpArgsLine.
	 */
	TQString m_ctcpRaw;

	// low level quoting, message quoting
	static TQString quote(const TQString &str);
	static TQString unquote(const TQString &str);

	// ctcp level quoting
	static TQString ctcpQuote(const TQString &str);
	static TQString ctcpUnquote(const TQString &str);

	static bool extractCtcpCommand(TQCString &str, TQCString &ctcpline);

	static bool matchForIRCRegExp(const TQCString &line, const TQTextCodec *codec, KIRC::Message &message);
	static bool matchForIRCRegExp(TQRegExp &regexp, const TQTextCodec *codec, const TQCString &line, KIRC::Message &message);

	class KIRC::Message *m_ctcpMessage;

	static TQRegExp m_IRCCommandType1;
  #ifdef _IRC_STRICTNESS_
	static TQRegExp m_IRCCommandType2;
  #endif // _IRC_STRICTNESS_

	static TQRegExp m_IRCNumericCommand;
};

}

#endif // KIRCMESSAGE_H