X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fmessage.cpp;h=46ae4849d262e242aaa45d970c11fe88efd00518;hp=9875473a57b0db60910da53cad1ce20e92858375;hb=a634acadbcf6017474f68a3eaf7cb632660e9e49;hpb=2ee32e7f7707349d12c98181bbe6842f6d19f60e diff --git a/src/common/message.cpp b/src/common/message.cpp index 9875473a..46ae4849 100644 --- a/src/common/message.cpp +++ b/src/common/message.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005/06 by The Quassel Team * + * Copyright (C) 2005-07 by the Quassel IRC Development Team * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -19,14 +19,17 @@ ***************************************************************************/ #include "message.h" + +#include "util.h" + #include -Message::Message(BufferId __buffer, Type __type, QString __text, QString __sender, quint8 __flags) +Message::Message(BufferInfo __buffer, Type __type, QString __text, QString __sender, quint8 __flags) : _buffer(__buffer), _text(__text), _sender(__sender), _type(__type), _flags(__flags) { _timeStamp = QDateTime::currentDateTime().toUTC(); } -Message::Message(QDateTime __ts, BufferId __buffer, Type __type, QString __text, QString __sender, quint8 __flags) +Message::Message(QDateTime __ts, BufferInfo __buffer, Type __type, QString __text, QString __sender, quint8 __flags) : _timeStamp(__ts), _buffer(__buffer), _text(__text), _sender(__sender), _type(__type), _flags(__flags) { } @@ -39,7 +42,7 @@ void Message::setMsgId(MsgId _id) { _msgId = _id; } -BufferId Message::buffer() const { +BufferInfo Message::buffer() const { return _buffer; } @@ -63,6 +66,131 @@ QDateTime Message::timeStamp() const { return _timeStamp; } +QString Message::mircToInternal(QString mirc) { + mirc.replace('%', "%%"); // escape % just to be sure + mirc.replace('\x02', "%B"); + mirc.replace('\x0f', "%O"); + mirc.replace('\x12', "%R"); + mirc.replace('\x16', "%R"); + mirc.replace('\x1d', "%S"); + mirc.replace('\x1f', "%U"); + + // Now we bring the color codes (\x03) in a sane format that can be parsed more easily later. + // %Dcfxx is foreground, %Dcbxx is background color, where xx is a 2 digit dec number denoting the color code. + // %Dc- turns color off. + // Note: We use the "mirc standard" as described in . + // This means that we don't accept something like \x03,5 (even though others, like WeeChat, do). + int pos = 0; + for(;;) { + pos = mirc.indexOf('\x03', pos); + if(pos < 0) break; // no more mirc color codes + QString ins, num; + int l = mirc.length(); + int i = pos + 1; + // check for fg color + if(i < l && mirc[i].isDigit()) { + num = mirc[i++]; + if(i < l && mirc[i].isDigit()) num.append(mirc[i++]); + else num.prepend('0'); + ins = QString("%Dcf%1").arg(num); + + if(i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) { + i++; + num = mirc[i++]; + if(i < l && mirc[i].isDigit()) num.append(mirc[i++]); + else num.prepend('0'); + ins += QString("%Dcb%1").arg(num); + } + } else { + ins = "%Dc-"; + } + mirc.replace(pos, i-pos, ins); + } + return mirc; +} + +void Message::format() { + if(!_formattedText.isNull()) return; // already done + QString user = userFromMask(sender()); + QString host = hostFromMask(sender()); + QString nick = nickFromMask(sender()); + QString txt = mircToInternal(text()); + QString networkName = buffer().network(); + QString bufferName = buffer().buffer(); + + _formattedTimeStamp = tr("%DT[%1]").arg(timeStamp().toLocalTime().toString("hh:mm:ss")); + + QString s, t; + switch(type()) { + case Message::Plain: + s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(txt); break; + case Message::Server: + s = tr("%Ds*"); t = tr("%Ds%1").arg(txt); break; + case Message::Error: + s = tr("%De*"); t = tr("%De%1").arg(txt); break; + case Message::Join: + 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; + case Message::Part: + 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); + if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt); + break; + case Message::Quit: + s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host); + if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt); + break; + case Message::Kick: + { s = tr("%Dk<-*"); + QString victim = txt.section(" ", 0, 0); + //if(victim == ui.ownNick->currentText()) victim = tr("you"); + QString kickmsg = txt.section(" ", 1); + 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); + if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg); + } + break; + case Message::Nick: + s = tr("%Dr<->"); + if(nick == text()) t = tr("%DrYou are now known as %DN%1%DN").arg(text()); + else t = tr("%Dr%DN%1%DN is now known as %DN%DU%2%DU%DN").arg(nick, text()); + break; + case Message::Mode: + s = tr("%Dm***"); + if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(text()); + else t = tr("%DmMode %DM%1%DM by %DN%DU%2%DU%DN").arg(text(), nick); + break; + case Message::Action: + s = tr("%Da-*-"); + t = tr("%Da%DN%DU%1%DU%DN %2").arg(nick).arg(text()); + break; + default: + s = tr("%De%1").arg(sender()); + t = tr("%De[%1]").arg(text()); + } + _formattedSender = s; + _formattedText = t; +} + +QString Message::formattedTimeStamp() { + format(); + return _formattedTimeStamp; +} + +QString Message::formattedSender() { + format(); + return _formattedSender; +} + +QString Message::formattedText() { + format(); + return _formattedText; +} + +/* +QString Message::formattedToHtml(const QString &f) { + + + return f; +} +*/ QDataStream &operator<<(QDataStream &out, const Message &msg) { out << (quint32)msg.timeStamp().toTime_t() << (quint8)msg.type() << (quint8)msg.flags() @@ -74,7 +202,7 @@ QDataStream &operator>>(QDataStream &in, Message &msg) { quint8 t, f; quint32 ts; QByteArray s, m; - BufferId buf; + BufferInfo buf; in >> ts >> t >> f >> buf >> s >> m; msg._type = (Message::Type)t; msg._flags = (quint8)f;