1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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 *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include <QDataStream>
27 Message::Message(BufferInfo __buffer, Type __type, QString __text, QString __sender, quint8 __flags)
28 : _buffer(__buffer), _text(__text), _sender(__sender), _type(__type), _flags(__flags) {
29 _timestamp = QDateTime::currentDateTime().toUTC();
32 Message::Message(QDateTime __ts, BufferInfo __buffer, Type __type, QString __text, QString __sender, quint8 __flags)
33 : _timestamp(__ts), _buffer(__buffer), _text(__text), _sender(__sender), _type(__type), _flags(__flags) {
37 MsgId Message::msgId() const {
41 void Message::setMsgId(MsgId _id) {
45 BufferInfo Message::buffer() const {
49 QString Message::text() const {
53 QString Message::sender() const {
57 Message::Type Message::type() const {
61 quint8 Message::flags() const {
65 QDateTime Message::timestamp() const {
69 QString Message::mircToInternal(QString mirc) {
70 mirc.replace('%', "%%"); // escape % just to be sure
71 mirc.replace('\x02', "%B");
72 mirc.replace('\x0f', "%O");
73 mirc.replace('\x12', "%R");
74 mirc.replace('\x16', "%R");
75 mirc.replace('\x1d', "%S");
76 mirc.replace('\x1f', "%U");
78 // Now we bring the color codes (\x03) in a sane format that can be parsed more easily later.
79 // %Dcfxx is foreground, %Dcbxx is background color, where xx is a 2 digit dec number denoting the color code.
80 // %Dc- turns color off.
81 // Note: We use the "mirc standard" as described in <http://www.mirc.co.uk/help/color.txt>.
82 // This means that we don't accept something like \x03,5 (even though others, like WeeChat, do).
85 pos = mirc.indexOf('\x03', pos);
86 if(pos < 0) break; // no more mirc color codes
88 int l = mirc.length();
91 if(i < l && mirc[i].isDigit()) {
93 if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
94 else num.prepend('0');
95 ins = QString("%Dcf%1").arg(num);
97 if(i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) {
100 if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
101 else num.prepend('0');
102 ins += QString("%Dcb%1").arg(num);
107 mirc.replace(pos, i-pos, ins);
112 void Message::format() {
113 if(!_formattedText.isNull()) return; // already done
114 QString user = userFromMask(sender());
115 QString host = hostFromMask(sender());
116 QString nick = nickFromMask(sender());
117 QString txt = mircToInternal(text());
118 QString networkName = buffer().network();
119 QString bufferName = buffer().buffer();
121 _formattedTimestamp = tr("%DT[%1]").arg(timestamp().toLocalTime().toString("hh:mm:ss"));
126 s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(txt); break;
127 case Message::Server:
128 s = tr("%Ds*"); t = tr("%Ds%1").arg(txt); break;
130 s = tr("%De*"); t = tr("%De%1").arg(txt); break;
132 s = tr("%Dj-->"); t = tr("%Dj%DN%DU%1%DU%DN %DH(%2@%3)%DH has joined %DC%DU%4%DU%DC").arg(nick, user, host, bufferName); break;
134 s = tr("%Dp<--"); t = tr("%Dp%DN%DU%1%DU%DN %DH(%2@%3)%DH has left %DC%DU%4%DU%DC").arg(nick, user, host, bufferName);
135 if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
138 s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
139 if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
143 QString victim = txt.section(" ", 0, 0);
144 //if(victim == ui.ownNick->currentText()) victim = tr("you");
145 QString kickmsg = txt.section(" ", 1);
146 t = tr("%Dk%DN%DU%1%DU%DN has kicked %DN%DU%2%DU%DN from %DC%DU%3%DU%DC").arg(nick).arg(victim).arg(bufferName);
147 if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
152 if(nick == text()) t = tr("%DrYou are now known as %DN%1%DN").arg(txt);
153 else t = tr("%Dr%DN%1%DN is now known as %DN%DU%2%DU%DN").arg(nick, txt);
157 if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(text());
158 else t = tr("%DmMode %DM%1%DM by %DN%DU%2%DU%DN").arg(txt, nick);
160 case Message::Action:
162 t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(txt);
165 s = tr("%De%1").arg(sender());
166 t = tr("%De[%1]").arg(txt);
168 _formattedSender = s;
172 QString Message::formattedTimestamp() {
174 return _formattedTimestamp;
177 QString Message::formattedSender() {
179 return _formattedSender;
182 QString Message::formattedText() {
184 return _formattedText;
188 QString Message::formattedToHtml(const QString &f) {
195 QDataStream &operator<<(QDataStream &out, const Message &msg) {
196 out << (quint32)msg.timestamp().toTime_t() << (quint8)msg.type() << (quint8)msg.flags()
197 << msg.buffer() << msg.sender().toUtf8() << msg.text().toUtf8();
201 QDataStream &operator>>(QDataStream &in, Message &msg) {
206 in >> ts >> t >> f >> buf >> s >> m;
207 msg._type = (Message::Type)t;
208 msg._flags = (quint8)f;
210 msg._timestamp = QDateTime::fromTime_t(ts);
211 msg._sender = QString::fromUtf8(s);
212 msg._text = QString::fromUtf8(m);