Remove superfluous (and warning-producing) semicolons.
[quassel.git] / src / common / message.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Development Team             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "message.h"
22
23 #include "util.h"
24
25 #include <QDataStream>
26
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();
30 }
31
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) {
34
35 }
36
37 MsgId Message::msgId() const {
38   return _msgId;
39 }
40
41 void Message::setMsgId(MsgId _id) {
42   _msgId = _id;
43 }
44
45 BufferInfo Message::buffer() const {
46   return _buffer;
47 }
48
49 QString Message::text() const {
50   return _text;
51 }
52
53 QString Message::sender() const {
54   return _sender;
55 }
56
57 Message::Type Message::type() const {
58    return _type;
59 }
60
61 quint8 Message::flags() const {
62    return _flags;
63 }
64
65 QDateTime Message::timeStamp() const {
66   return _timeStamp;
67 }
68
69 QString Message::mircToInternal(QString mirc) {
70   mirc.replace('%', "%%");      // escape % just to be sure
71   mirc.replace('\x02', "%B");
72   mirc.replace('\x03', "%C");
73   mirc.replace('\x0f', "%O");
74   mirc.replace('\x12', "%R");
75   mirc.replace('\x16', "%R");
76   mirc.replace('\x1d', "%S");
77   mirc.replace('\x1f', "%U");
78   return mirc;
79 }
80
81 void Message::format() {
82   if(!_formattedText.isNull()) return; // already done
83   QString user = userFromMask(sender());
84   QString host = hostFromMask(sender());
85   QString nick = nickFromMask(sender());
86   QString txt = mircToInternal(text());
87   QString networkName = buffer().network();
88   QString bufferName = buffer().buffer();
89
90   _formattedTimeStamp = tr("%DT[%1]").arg(timeStamp().toLocalTime().toString("hh:mm:ss"));
91
92   QString s, t;
93   switch(type()) {
94     case Message::Plain:
95       s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(txt); break;
96     case Message::Server:
97       s = tr("%Ds*"); t = tr("%Ds%1").arg(txt); break;
98     case Message::Error:
99       s = tr("%De*"); t = tr("%De%1").arg(txt); break;
100     case Message::Join:
101       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;
102     case Message::Part:
103       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);
104       if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
105       break;
106     case Message::Quit:
107       s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
108       if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
109       break;
110     case Message::Kick:
111     { s = tr("%Dk<-*");
112     QString victim = txt.section(" ", 0, 0);
113         //if(victim == ui.ownNick->currentText()) victim = tr("you");
114     QString kickmsg = txt.section(" ", 1);
115     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);
116     if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
117     }
118     break;
119     case Message::Nick:
120       s = tr("%Dr<->");
121       if(nick == text()) t = tr("%DrYou are now known as %DN%1%DN").arg(text());
122       else t = tr("%Dr%DN%1%DN is now known as %DN%DU%2%DU%DN").arg(nick, text());
123       break;
124     case Message::Mode:
125       s = tr("%Dm***");
126       if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(text());
127       else t = tr("%DmMode %DM%1%DM by %DN%DU%2%DU%DN").arg(text(), nick);
128       break;
129     case Message::Action:
130       s = tr("%Da-*-");
131       t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(text());
132       break;
133     default:
134       s = tr("%De%1").arg(sender());
135       t = tr("%De[%1]").arg(text());
136   }
137   _formattedSender = s;
138   _formattedText = t;
139 }
140
141 QString Message::formattedTimeStamp() {
142   format();
143   return _formattedTimeStamp;
144 }
145
146 QString Message::formattedSender() {
147   format();
148   return _formattedSender;
149 }
150
151 QString Message::formattedText() {
152   format();
153   return _formattedText;
154 }
155
156 QDataStream &operator<<(QDataStream &out, const Message &msg) {
157   out << (quint32)msg.timeStamp().toTime_t() << (quint8)msg.type() << (quint8)msg.flags()
158       << msg.buffer() << msg.sender().toUtf8() << msg.text().toUtf8();
159   return out;
160 }
161
162 QDataStream &operator>>(QDataStream &in, Message &msg) {
163   quint8 t, f;
164   quint32 ts;
165   QByteArray s, m;
166   BufferInfo buf;
167   in >> ts >> t >> f >> buf >> s >> m;
168   msg._type = (Message::Type)t;
169   msg._flags = (quint8)f;
170   msg._buffer = buf;
171   msg._timeStamp = QDateTime::fromTime_t(ts);
172   msg._sender = QString::fromUtf8(s);
173   msg._text = QString::fromUtf8(m);
174   return in;
175 }
176