Update away state regularly

This commit is contained in:
HanzZ 2012-12-04 19:21:59 +01:00
parent 2d3069e602
commit 11a084deb0
2 changed files with 49 additions and 7 deletions

View file

@ -36,6 +36,14 @@ MyIrcSession::MyIrcSession(const std::string &user, IRCNetworkPlugin *np, const
connect(this, SIGNAL(socketError(QAbstractSocket::SocketError)), SLOT(on_socketError(QAbstractSocket::SocketError)));
connect(this, SIGNAL(connected()), SLOT(on_connected()));
connect(this, SIGNAL(messageReceived(IrcMessage*)), this, SLOT(onMessageReceived(IrcMessage*)));
m_awayTimer = new QTimer(this);
connect(m_awayTimer, SIGNAL(timeout()), this, SLOT(awayTimeout()));
m_awayTimer->start(10*1000);
}
MyIrcSession::~MyIrcSession() {
delete m_awayTimer;
}
void MyIrcSession::on_connected() {
@ -210,14 +218,23 @@ void MyIrcSession::on_numericMessageReceived(IrcMessage *message) {
}
np->handleSubject(user, TO_UTF8(m->parameters().value(1)) + suffix, m_topicData, nick);
break;
case 352:
case 352: {
channel = m->parameters().value(1);
nick = TO_UTF8(m->parameters().value(5));
IRCBuddy &buddy = getIRCBuddy(TO_UTF8(channel), nick);
if (m->parameters().value(6).toUpper().startsWith("G")) {
channel = m->parameters().value(1);
nick = TO_UTF8(m->parameters().value(5));
IRCBuddy &buddy = getIRCBuddy(TO_UTF8(channel), nick);
np->handleParticipantChanged(user, nick, TO_UTF8(channel) + suffix, buddy.isOp(), pbnetwork::STATUS_AWAY);
if (!buddy.isAway()) {
buddy.setAway(true);
np->handleParticipantChanged(user, nick, TO_UTF8(channel) + suffix, buddy.isOp(), pbnetwork::STATUS_AWAY);
}
}
else if (buddy.isAway()) {
buddy.setAway(false);
np->handleParticipantChanged(user, nick, TO_UTF8(channel) + suffix, buddy.isOp(), pbnetwork::STATUS_ONLINE);
}
break;
}
case 353:
channel = m->parameters().value(2);
members = m->parameters().value(3).split(" ");
@ -260,6 +277,15 @@ void MyIrcSession::on_numericMessageReceived(IrcMessage *message) {
//qDebug() << "numeric message received:" << receiver() << origin << code << params;
}
void MyIrcSession::awayTimeout() {
for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) {
if (it->second->shouldAskWho()) {
LOG4CXX_INFO(logger, "The time has come. Asking /who " << it->second->getChannel() << " again to get current away states.");
sendCommand(IrcCommand::createWho(FROM_UTF8(it->second->getChannel())));
}
}
}
void MyIrcSession::onMessageReceived(IrcMessage *message) {
LOG4CXX_INFO(logger, user << ": " << TO_UTF8(message->toString()));
switch (message->type()) {

View file

@ -15,6 +15,7 @@
#include <transport/networkplugin.h>
#include "Swiften/Swiften.h"
#include <boost/smart_ptr/make_shared.hpp>
#include <QTimer>
using namespace Transport;
@ -27,14 +28,26 @@ class MyIrcSession : public IrcSession
public:
class AutoJoinChannel {
public:
AutoJoinChannel(const std::string &channel = "", const std::string &password = "") : m_channel(channel), m_password(password) {}
AutoJoinChannel(const std::string &channel = "", const std::string &password = "", int awayCycle = 12) : m_channel(channel), m_password(password),
m_awayCycle(awayCycle), m_currentAwayTick(0) {}
virtual ~AutoJoinChannel() {}
const std::string &getChannel() { return m_channel; }
const std::string &getPassword() { return m_password; }
bool shouldAskWho() {
if (m_currentAwayTick == m_awayCycle) {
m_currentAwayTick = 0;
return true;
}
m_currentAwayTick++;
return false;
}
private:
std::string m_channel;
std::string m_password;
int m_awayCycle;
int m_currentAwayTick;
};
class IRCBuddy {
@ -55,11 +68,12 @@ public:
typedef std::map<std::string, std::map<std::string, IRCBuddy> > IRCBuddyMap;
MyIrcSession(const std::string &user, IRCNetworkPlugin *np, const std::string &suffix = "", QObject* parent = 0);
virtual ~MyIrcSession();
std::string suffix;
int rooms;
void addAutoJoinChannel(const std::string &channel, const std::string &password) {
m_autoJoin[channel] = boost::make_shared<AutoJoinChannel>(channel, password);
m_autoJoin[channel] = boost::make_shared<AutoJoinChannel>(channel, password, 12 + m_autoJoin.size());
}
void removeAutoJoinChannel(const std::string &channel) {
@ -104,6 +118,7 @@ protected Q_SLOTS:
void on_socketError(QAbstractSocket::SocketError error);
void onMessageReceived(IrcMessage* message);
void awayTimeout();
protected:
IRCNetworkPlugin *np;
@ -115,6 +130,7 @@ protected:
std::list<std::string> m_rooms;
std::list<std::string> m_names;
IRCBuddyMap m_buddies;
QTimer *m_awayTimer;
};
#endif // SESSION_H