Skip to content

Instantly share code, notes, and snippets.

@nlf
Created August 22, 2012 22:18
Show Gist options
  • Save nlf/3430010 to your computer and use it in GitHub Desktop.
Save nlf/3430010 to your computer and use it in GitHub Desktop.
znc capslock wednesday module
/* CLW module for ZNC
* Build with znc-buildmod and load with /msg *status loadmod clw
* Module triggers Wednesdays from 2PM to 4PM Pacific time, but uses UTC
* and converts to make sure it works in any time zone. Will ONLY caps
* messages sent to #thezone
*
* Changes:
* 1/4/2012
* added handling of irc connected event so module is properly re-enabled
* 4/30/2011
* added parsing function so URLs beginning with http:, https:, ftp: or www. are not
* sent in caps, though everything else still is
*
* added 'noclw' keyword, which when found at the beginning of a line
* will send the remainder of the line without altering the text at all
*/
#include "Modules.h"
#include "User.h"
#include "IRCSock.h"
class CCLWMod;
class CCLWTimer : public CTimer {
public:
CCLWTimer(CCLWMod* pModule) : CTimer((CModule*) pModule, 1, 0, "CCLWTimer", "Checks to see if it's CLW") {
m_pParent = pModule;
}
virtual ~CCLWTimer() {}
private:
protected:
virtual void RunJob();
CCLWMod* m_pParent;
};
class CCLWMod : public CModule {
public:
MODCONSTRUCTOR(CCLWMod) {}
virtual ~CCLWMod() {}
virtual bool OnLoad(const CString& sArgs, CString& sMessage) {
isCLW = false;
nickChanged = false;
m_pTimer = NULL;
if (m_pUser->IsIRCConnected())
OnIRCConnected();
cszNick = GetNick();
Enable();
return true;
}
CString ProcessText(CString sMessage) {
CString sProcessed = "";
VCString vsMessage;
VCString::iterator it;
if (sMessage.Token(0).CaseCmp("noclw") == 0) {
sProcessed = sMessage.Token(1, true, " ");
return sProcessed;
}
sMessage.Split(" ", vsMessage, false);
for (it = vsMessage.begin(); it != vsMessage.end(); ++it) {
CString sToken = it->Token(0);
if ((sToken.AsLower().Left(5) == "http:") || (sToken.AsLower().Left(4) == "www.") \
|| (sToken.AsLower().Left(6) == "https:") || (sToken.AsLower().Left(4) == "ftp:")) {
sProcessed += sToken + " ";
} else {
sProcessed += sToken.AsUpper() + " ";
}
}
return sProcessed;
}
virtual EModRet OnUserMsg(CString &sTarget, CString &sMessage) {
if (isCLW && sTarget.AsLower() == "#thezone")
sMessage = ProcessText(sMessage);
return CONTINUE;
}
virtual void OnNick(const CNick& Nick, const CString& sNewNick, const vector<CChan*>& vChans) {
if (sNewNick == GetUser()->GetIRCSock()->GetNick())
if (!nickChanged)
cszNick = sNewNick;
}
void CheckCLW() {
if (!m_pTimer)
return;
static char sUTC[] = "TZ=UTC";
static char sPST[] = "TZ=America/Los_Angeles";
isCLW = false;
putenv(sUTC);
tt = time(NULL);
putenv(sPST);
timeval = localtime(&tt);
if (timeval->tm_wday == 3)
if (timeval->tm_hour < 16)
if (timeval->tm_hour >= 14)
isCLW = true;
if (isCLW && !nickChanged)
{
nickChanged = true;
PutIRC("NICK " + cszNick.AsUpper());
}
if (!isCLW && nickChanged)
{
PutIRC("NICK " + cszNick);
nickChanged = false;
}
}
CString GetNick() {
CString sConfNick = m_pUser->GetNick();
CIRCSock* pIRCSock = GetUser()->GetIRCSock();
if (pIRCSock)
sConfNick = sConfNick.Left(pIRCSock->GetMaxNickLen());
return sConfNick;
}
virtual void OnIRCDisconnected() {
Disable();
}
virtual void OnIRCConnected() {
Enable();
}
void Enable() {
if (m_pTimer)
return;
m_pTimer = new CCLWTimer(this);
AddTimer(m_pTimer);
}
void Disable() {
if (!m_pTimer)
return;
m_pTimer->Stop();
RemTimer(m_pTimer->GetName());
m_pTimer = NULL;
}
private:
CCLWTimer* m_pTimer;
CString cszNick;
bool isCLW;
bool nickChanged;
struct tm *timeval;
time_t tt;
};
void CCLWTimer::RunJob() {
m_pParent->CheckCLW();
}
MODULEDEFS(CCLWMod, "Participate in CAPS LOCK WEDNESDAY")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment