replaced Client::fakeInput() with Client::userInpt() (now static but no longer a...
[quassel.git] / src / common / message.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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) version 3.                                           *
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('\x0f', "%O");
73   mirc.replace('\x12', "%R");
74   mirc.replace('\x16', "%R");
75   mirc.replace('\x1d', "%S");
76   mirc.replace('\x1f', "%U");
77
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).
83   int pos = 0;
84   for(;;) {
85     pos = mirc.indexOf('\x03', pos);
86     if(pos < 0) break; // no more mirc color codes
87     QString ins, num;
88     int l = mirc.length();
89     int i = pos + 1;
90     // check for fg color
91     if(i < l && mirc[i].isDigit()) {
92       num = mirc[i++];
93       if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
94       else num.prepend('0');
95       ins = QString("%Dcf%1").arg(num);
96
97       if(i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) {
98         i++;
99         num = mirc[i++];
100         if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
101         else num.prepend('0');
102         ins += QString("%Dcb%1").arg(num);
103       }
104     } else {
105       ins = "%Dc-";
106     }
107     mirc.replace(pos, i-pos, ins);
108   }
109   return mirc;
110 }
111
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();
120
121   _formattedTimestamp = tr("%DT[%1]").arg(timestamp().toLocalTime().toString("hh:mm:ss"));
122
123   QString s, t;
124   switch(type()) {
125     case Message::Plain:
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;
129     case Message::Error:
130       s = tr("%De*"); t = tr("%De%1").arg(txt); break;
131     case Message::Join:
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;
133     case Message::Part:
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);
136       break;
137     case Message::Quit:
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);
140       break;
141     case Message::Kick:
142     { s = tr("%Dk<-*");
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);
148     }
149     break;
150     case Message::Nick:
151       s = tr("%Dr<->");
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);
154       break;
155     case Message::Mode:
156       s = tr("%Dm***");
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);
159       break;
160     case Message::Action:
161       s = tr("%Da-*-");
162       t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(txt);
163       break;
164     default:
165       s = tr("%De%1").arg(sender());
166       t = tr("%De[%1]").arg(txt);
167   }
168   _formattedSender = s;
169   _formattedText = t;
170 }
171
172 QString Message::formattedTimestamp() {
173   format();
174   return _formattedTimestamp;
175 }
176
177 QString Message::formattedSender() {
178   format();
179   return _formattedSender;
180 }
181
182 QString Message::formattedText() {
183   format();
184   return _formattedText;
185 }
186
187 /*
188 QString Message::formattedToHtml(const QString &f) {
189
190
191   return f;
192 }
193 */
194
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();
198   return out;
199 }
200
201 QDataStream &operator>>(QDataStream &in, Message &msg) {
202   quint8 t, f;
203   quint32 ts;
204   QByteArray s, m;
205   BufferInfo buf;
206   in >> ts >> t >> f >> buf >> s >> m;
207   msg._type = (Message::Type)t;
208   msg._flags = (quint8)f;
209   msg._buffer = buf;
210   msg._timestamp = QDateTime::fromTime_t(ts);
211   msg._sender = QString::fromUtf8(s);
212   msg._text = QString::fromUtf8(m);
213   return in;
214 }
215