1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Development Team *
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) any later version. *
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 ***************************************************************************/
23 #include <QDataStream>
25 Message::Message(BufferId __buffer, Type __type, QString __text, QString __sender, quint8 __flags)
26 : _buffer(__buffer), _text(__text), _sender(__sender), _type(__type), _flags(__flags) {
27 _timeStamp = QDateTime::currentDateTime().toUTC();
30 Message::Message(QDateTime __ts, BufferId __buffer, Type __type, QString __text, QString __sender, quint8 __flags)
31 : _timeStamp(__ts), _buffer(__buffer), _text(__text), _sender(__sender), _type(__type), _flags(__flags) {
35 MsgId Message::msgId() const {
39 void Message::setMsgId(MsgId _id) {
43 BufferId Message::buffer() const {
47 QString Message::text() const {
51 QString Message::sender() const {
55 Message::Type Message::type() const {
59 quint8 Message::flags() const {
63 QDateTime Message::timeStamp() const {
67 QString Message::mircToInternal(QString mirc) {
68 mirc.replace('%', "%%"); // escape % just to be sure
69 mirc.replace('\x02', "%B");
70 mirc.replace('\x03', "%C");
71 mirc.replace('\x0f', "%O");
72 mirc.replace('\x12', "%R");
73 mirc.replace('\x16', "%R");
74 mirc.replace('\x1d', "%S");
75 mirc.replace('\x1f', "%U");
79 void Message::format() {
80 if(!_formattedText.isNull()) return; // already done
81 QString user = userFromMask(sender());
82 QString host = hostFromMask(sender());
83 QString nick = nickFromMask(sender());
84 QString txt = mircToInternal(text());
85 QString networkName = buffer().network();
86 QString bufferName = buffer().buffer();
88 _formattedTimeStamp = tr("%DT[%1]").arg(timeStamp().toLocalTime().toString("hh:mm:ss"));
93 s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(txt); break;
95 s = tr("%Ds*"); t = tr("%Ds%1").arg(txt); break;
97 s = tr("%De*"); t = tr("%De%1").arg(txt); break;
99 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;
101 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);
102 if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
105 s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
106 if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
110 QString victim = txt.section(" ", 0, 0);
111 //if(victim == ui.ownNick->currentText()) victim = tr("you");
112 QString kickmsg = txt.section(" ", 1);
113 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);
114 if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
119 if(nick == text()) t = tr("%DrYou are now known as %DN%1%DN").arg(text());
120 else t = tr("%Dr%DN%1%DN is now known as %DN%DU%2%DU%DN").arg(nick, text());
124 if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(text());
125 else t = tr("%DmMode %DM%1%DM by %DN%DU%2%DU%DN").arg(text(), nick);
127 case Message::Action:
129 t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(text());
132 s = tr("%De%1").arg(sender());
133 t = tr("%De[%1]").arg(text());
135 _formattedSender = s;
139 QString Message::formattedTimeStamp() {
141 return _formattedTimeStamp;
144 QString Message::formattedSender() {
146 return _formattedSender;
149 QString Message::formattedText() {
151 return _formattedText;
154 QDataStream &operator<<(QDataStream &out, const Message &msg) {
155 out << (quint32)msg.timeStamp().toTime_t() << (quint8)msg.type() << (quint8)msg.flags()
156 << msg.buffer() << msg.sender().toUtf8() << msg.text().toUtf8();
160 QDataStream &operator>>(QDataStream &in, Message &msg) {
165 in >> ts >> t >> f >> buf >> s >> m;
166 msg._type = (Message::Type)t;
167 msg._flags = (quint8)f;
169 msg._timeStamp = QDateTime::fromTime_t(ts);
170 msg._sender = QString::fromUtf8(s);
171 msg._text = QString::fromUtf8(m);