Skip to content

Instantly share code, notes, and snippets.

@fwaggle
Created March 11, 2011 16:44
Show Gist options
  • Save fwaggle/866162 to your computer and use it in GitHub Desktop.
Save fwaggle/866162 to your computer and use it in GitHub Desktop.
Allows temporary channels to hang around for an approximate amount of time (defaults to 10s in this example, obviously default would be zero, and i haven't checked the meta config stuff works it was a crappy copy+paste). i'm aware it probably shouldn't be
diff -ur ../old/src/Channel.cpp ./src/Channel.cpp
--- ../old/src/Channel.cpp 2011-03-10 00:12:59.000000000 -0500
+++ ./src/Channel.cpp 2011-03-11 10:30:14.000000000 -0500
@@ -47,6 +47,9 @@
cParent = qobject_cast<Channel *>(p);
if (cParent)
cParent->addChannel(this);
+
+ iLastUsed = QDateTime::currentDateTime().toTime_t();
+
#ifdef MUMBLE
uiPermissions = 0;
#endif
@@ -182,10 +185,31 @@
p->cChannel->removeUser(p);
p->cChannel = this;
qlUsers << p;
+
+ qWarning("Last Used: %d", iLastUsed);
}
void Channel::removeUser(User *p) {
qlUsers.removeAll(p);
+
+ if (qlUsers.isEmpty())
+ iLastUsed = QDateTime::currentDateTime().toTime_t();
+}
+
+int Channel::lastUsed() {
+ if (qlUsers.isEmpty())
+ return iLastUsed;
+
+ // Still has users in it, return "now".
+ return QDateTime::currentDateTime().toTime_t();
+}
+
+int Channel::lastUsed(const Channel *c) {
+ if (c->qlUsers.isEmpty())
+ return c->iLastUsed;
+
+ // Still has users in it, return "now".
+ return QDateTime::currentDateTime().toTime_t();
}
Channel::operator const QString() const {
diff -ur ../old/src/Channel.h ./src/Channel.h
--- ../old/src/Channel.h 2011-03-10 00:12:59.000000000 -0500
+++ ./src/Channel.h 2011-03-11 10:30:24.000000000 -0500
@@ -45,6 +45,7 @@
Q_DISABLE_COPY(Channel)
private:
QSet<Channel *> qsUnseen;
+ int iLastUsed;
public:
int iId;
int iPosition;
@@ -72,8 +73,8 @@
static QHash<int, Channel *> c_qhChannels;
static QReadWriteLock c_qrwlChannels;
- static Channel *get(int);
static Channel *add(int, const QString &);
+ static Channel *get(int);
static void remove(Channel *);
void addClientUser(ClientUser *p);
@@ -88,6 +89,8 @@
bool isLinked(Channel *c) const;
void link(Channel *c);
void unlink(Channel *c = NULL);
+ int lastUsed();
+ static int lastUsed(const Channel *c);
QSet<Channel *> allLinks();
QSet<Channel *> allChildren();
diff -ur ../old/src/murmur/Meta.cpp ./src/murmur/Meta.cpp
--- ../old/src/murmur/Meta.cpp 2011-03-10 00:12:59.000000000 -0500
+++ ./src/murmur/Meta.cpp 2011-03-10 13:04:57.000000000 -0500
@@ -51,6 +51,7 @@
iMaxImageMessageLength = 131072;
bAllowHTML = true;
iDefaultChan = 0;
+ iTempChannelPersist = 10;
bRememberChan = true;
qsWelcomeText = QString("Welcome to this server");
qsDatabase = QString();
diff -ur ../old/src/murmur/Meta.h ./src/murmur/Meta.h
--- ../old/src/murmur/Meta.h 2011-03-10 00:12:59.000000000 -0500
+++ ./src/murmur/Meta.h 2011-03-10 13:05:07.000000000 -0500
@@ -46,6 +46,7 @@
int iMaxUsers;
int iMaxUsersPerChannel;
int iDefaultChan;
+ int iTempChannelPersist;
bool bRememberChan;
int iMaxTextMessageLength;
int iMaxImageMessageLength;
diff -ur ../old/src/murmur/Murmur.ice ./src/murmur/Murmur.ice
--- ../old/src/murmur/Murmur.ice 2011-03-10 00:12:59.000000000 -0500
+++ ./src/murmur/Murmur.ice 2011-03-10 00:38:30.000000000 -0500
@@ -85,6 +85,8 @@
bool temporary;
/** Position of the channel which is used in Client for sorting. */
int position;
+ /** Seconds since Epoch that the channel was last used. Will return current time if channel has users in it. */
+ int lastUsed;
};
/** A group. Groups are defined per channel, and can inherit members from parent channels.
diff -ur ../old/src/murmur/MurmurIce.cpp ./src/murmur/MurmurIce.cpp
--- ../old/src/murmur/MurmurIce.cpp 2011-03-10 00:12:59.000000000 -0500
+++ ./src/murmur/MurmurIce.cpp 2011-03-10 00:46:18.000000000 -0500
@@ -108,6 +108,7 @@
mc.parent = c->cParent ? c->cParent->iId : -1;
mc.description = u8(c->qsDesc);
mc.position = c->iPosition;
+ mc.lastUsed = ::Channel::lastUsed(c);
mc.links.clear();
foreach(::Channel *chn, c->qsPermLinks)
mc.links.push_back(chn->iId);
diff -ur ../old/src/murmur/Server.cpp ./src/murmur/Server.cpp
--- ../old/src/murmur/Server.cpp 2011-03-10 00:12:59.000000000 -0500
+++ ./src/murmur/Server.cpp 2011-03-11 11:37:23.000000000 -0500
@@ -299,6 +299,7 @@
iMaxImageMessageLength = Meta::mp.iMaxImageMessageLength;
bAllowHTML = Meta::mp.bAllowHTML;
iDefaultChan = Meta::mp.iDefaultChan;
+ iTempChannelPersist = Meta::mp.iTempChannelPersist;
bRememberChan = Meta::mp.bRememberChan;
qsWelcomeText = Meta::mp.qsWelcomeText;
qlBind = Meta::mp.qlBind;
@@ -349,6 +350,7 @@
iMaxTextMessageLength = getConf("textmessagelength", iMaxTextMessageLength).toInt();
iMaxImageMessageLength = getConf("imagemessagelength", iMaxImageMessageLength).toInt();
bAllowHTML = getConf("allowhtml", bAllowHTML).toBool();
+ iTempChannelPersist = getConf("tempchannelpersist", iTempChannelPersist).toInt();
iDefaultChan = getConf("defaultchannel", iDefaultChan).toInt();
bRememberChan = getConf("rememberchannel", bRememberChan).toBool();
qsWelcomeText = getConf("welcometext", qsWelcomeText).toString();
@@ -373,6 +375,8 @@
qsPassword = !v.isNull() ? v : Meta::mp.qsPassword;
else if (key == "timeout")
iTimeout = i ? i : Meta::mp.iTimeout;
+ else if (key == "tempchannelpersist")
+ iTempChannelPersist = i ? i : Meta::mp.iTempChannelPersist;
else if (key == "bandwidth") {
int length = i ? i : Meta::mp.iMaxBandwidth;
if (length != iMaxBandwidth) {
@@ -1086,7 +1090,7 @@
old->removeUser(u);
}
- if (old && old->bTemporary && old->qlUsers.isEmpty())
+ if (old && old->bTemporary && old->qlUsers.isEmpty() && iTempChannelPersist < 1)
QCoreApplication::instance()->postEvent(this, new ExecEvent(boost::bind(&Server::removeChannel, this, old->iId)));
qqIds.enqueue(u->uiSession); // Reinsert session id into pool
@@ -1178,6 +1182,16 @@
qrwlUsers.unlock();
foreach(ServerUser *u, qlClose)
u->disconnectSocket(true);
+
+ // Pretty sure this should be in it's own timeout function
+ // but whatever. :D
+
+ int curTime = QDateTime::currentDateTime().toTime_t();
+
+ foreach(const ::Channel *c, qhChannels) {
+ if ((curTime - ::Channel::lastUsed(c)) > iTempChannelPersist && c->bTemporary)
+ QCoreApplication::instance()->postEvent(this, new ExecEvent(boost::bind(&Server::removeChannel, this, c->iId)));
+ }
}
void Server::tcpTransmitData(QByteArray a, unsigned int id) {
@@ -1338,7 +1352,7 @@
mpus.set_suppress(p->bSuppress);
}
- if (old && old->bTemporary && old->qlUsers.isEmpty()) {
+ if (old && old->bTemporary && old->qlUsers.isEmpty() && iTempChannelPersist < 1) {
QCoreApplication::instance()->postEvent(this, new ExecEvent(boost::bind(&Server::removeChannel, this, old->iId)));
}
diff -ur ../old/src/murmur/Server.h ./src/murmur/Server.h
--- ../old/src/murmur/Server.h 2011-03-10 00:12:59.000000000 -0500
+++ ./src/murmur/Server.h 2011-03-10 12:44:46.000000000 -0500
@@ -108,6 +108,7 @@
int iMaxUsers;
int iMaxUsersPerChannel;
int iDefaultChan;
+ int iTempChannelPersist;
bool bRememberChan;
int iMaxTextMessageLength;
int iMaxImageMessageLength;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment