1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/*
* Copyright (C) 2004-2010 See the AUTHORS file for details.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* Quiet Away and message logger
* Author: imaginos <imaginos@imaginos.net>
*/
#define REQUIRESSL
#include "User.h"
#include <sys/stat.h>
#define CRYPT_VERIFICATION_TOKEN "::__:AWAY:__::"
class CAway;
class CAwayJob : public CTimer
{
public:
CAwayJob(CModule* pModule, unsigned int uInterval, unsigned int uCycles, const CString& sLabel, const CString& sDescription)
: CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {}
virtual ~CAwayJob() {}
protected:
virtual void RunJob();
};
class CAway : public CModule
{
public:
MODCONSTRUCTOR(CAway)
{
Ping();
m_bIsAway = false;
m_bBootError = false;
SetAwayTime(300);
AddTimer(new CAwayJob(this, 60, 0, "AwayJob", "Checks for idle and saves messages every 1 minute"));
}
virtual ~CAway()
{
if (!m_bBootError)
SaveBufferToDisk();
}
virtual bool OnLoad(const CString& sArgs, CString& sMessage)
{
CString sMyArgs = sArgs;
if (sMyArgs.Token(0) == "-notimer")
{
SetAwayTime(0);
sMyArgs = sMyArgs.Token(1, true);
} else if (sMyArgs.Token(0) == "-timer")
{
SetAwayTime(sMyArgs.Token(1).ToInt());
sMyArgs = sMyArgs.Token(2, true);
}
if (!sMyArgs.empty())
{
m_sPassword = CBlowfish::MD5(sMyArgs);
} else {
sMessage = "This module needs as an argument a keyphrase used for encryption";
return false;
}
if (!BootStrap())
{
sMessage = "Failed to decrypt your saved messages - "
"Did you give the right encryption key as an argument to this module?";
m_bBootError = true;
return false;
}
return true;
}
virtual void OnIRCConnected()
{
if (m_bIsAway)
Away(true); // reset away if we are reconnected
else
Back(); // ircd seems to remember your away if you killed the client and came back
}
bool BootStrap()
{
CString sFile;
if (DecryptMessages(sFile))
{
VCString vsLines;
VCString::iterator it;
sFile.Split("\n", vsLines);
for (it = vsLines.begin(); it != vsLines.end(); ++it) {
CString sLine(*it);
sLine.Trim();
AddMessage(sLine);
}
} else {
m_sPassword = "";
CUtils::PrintError("[" + GetModName() + ".so] Failed to Decrypt Messages");
return(false);
}
return(true);
}
void SaveBufferToDisk()
{
if (!m_sPassword.empty())
{
CString sFile = CRYPT_VERIFICATION_TOKEN;
for (u_int b = 0; b < m_vMessages.size(); b++)
sFile += m_vMessages[b] + "\n";
CBlowfish c(m_sPassword, BF_ENCRYPT);
sFile = c.Crypt(sFile);
CString sPath = GetPath();
if (!sPath.empty())
{
CFile File(sPath);
if (File.Open(O_WRONLY | O_CREAT | O_TRUNC, 0600)) {
File.Chmod(0600);
File.Write(sFile);
}
File.Close();
}
}
}
virtual void OnClientLogin()
{
Back(true);
}
virtual void OnClientDisconnect()
{
Away();
}
virtual void OnModCommand(const CString& sCommand)
{
CString sCmdName = sCommand.Token(0);
if (sCmdName == "away")
{
CString sReason;
if (sCommand.Token(1) != "-quiet")
{
sReason = sCommand.Token(1, true);
PutModNotice("You have been marked as away", "away");
}
else
sReason = sCommand.Token(2, true);
Away(false, sReason);
}
else if (sCmdName == "back")
{
if ((m_vMessages.empty()) && (sCommand.Token(1) != "-quiet"))
PutModNotice("Welcome Back!", "away");
Back();
}
else if (sCmdName == "messages")
{
for (u_int a = 0; a < m_vMessages.size(); a++)
PutModule(m_vMessages[a], "away");
}
else if (sCmdName == "delete")
{
CString sWhich = sCommand.Token(1);
if (sWhich == "all")
{
PutModNotice("Deleted " + CString(m_vMessages.size()) + " Messages.", "away");
for (u_int a = 0; a < m_vMessages.size(); a++)
m_vMessages.erase(m_vMessages.begin() + a--);
}
else if (sWhich.empty())
{
PutModNotice("USAGE: delete <num|all>", "away");
return;
} else
{
u_int iNum = sWhich.ToUInt();
if (iNum >= m_vMessages.size())
{
PutModNotice("Illegal Message # Requested", "away");
return;
}
else
{
m_vMessages.erase(m_vMessages.begin() + iNum);
PutModNotice("Message Erased.", "away");
}
SaveBufferToDisk();
}
}
else if (sCmdName == "save")
{
SaveBufferToDisk();
PutModNotice("Messages saved to disk.", "away");
}
else if (sCmdName == "ping")
{
Ping();
if (m_bIsAway)
Back();
}
else if (sCmdName == "pass")
{
m_sPassword = sCommand.Token(1);
PutModNotice("Password Updated to [" + m_sPassword + "]");
}
else if (sCmdName == "show")
{
map< CString, vector< CString> > msvOutput;
for (u_int a = 0; a < m_vMessages.size(); a++)
{
CString sTime = m_vMessages[a].Token(0, false, ":");
CString sWhom = m_vMessages[a].Token(1, false, ":");
CString sMessage = m_vMessages[a].Token(2, true, ":");
if ((sTime.empty()) || (sWhom.empty()) || (sMessage.empty()))
{
// illegal format
PutModule("Corrupt message! [" + m_vMessages[a] + "]", "away");
m_vMessages.erase(m_vMessages.begin() + a--);
continue;
}
time_t iTime = sTime.ToULong();
char szFormat[64];
struct tm t;
localtime_r(&iTime, &t);
size_t iCount = strftime(szFormat, 64, "%F %T", &t);
if (iCount <= 0)
{
PutModule("Corrupt time stamp! [" + m_vMessages[a] + "]", "away");
m_vMessages.erase(m_vMessages.begin() + a--);
continue;
}
CString sTmp = " " + CString(a) + ") [";
sTmp.append(szFormat, iCount);
sTmp += "] ";
sTmp += sMessage;
msvOutput[sWhom].push_back(sTmp);
}
for (map< CString, vector< CString> >::iterator it = msvOutput.begin(); it != msvOutput.end(); ++it)
{
PutModule(it->first, "away");
for (u_int a = 0; a < it->second.size(); a++)
PutModule(it->second[a]);
}
PutModule("#--- End Messages", "away");
} else if (sCmdName == "enabletimer")
{
SetAwayTime(300);
PutModule("Timer set to 300 seconds");
} else if (sCmdName == "disabletimer")
{
SetAwayTime(0);
PutModule("Timer disabled");
} else if (sCmdName == "settimer")
{
int iSetting = sCommand.Token(1).ToInt();
SetAwayTime(iSetting);
if (iSetting == 0)
PutModule("Timer disabled");
else
PutModule("Timer set to " + CString(iSetting) + " seconds");
} else if (sCmdName == "timer")
{
PutModule("Current timer setting: " + CString(GetAwayTime()) + " seconds");
} else
{
PutModule("Commands: away [-quiet], back [-quiet], delete <num|all>, ping, show, save, enabletimer, disabletimer, settimer <secs>, timer", "away");
}
}
CString GetPath()
{
CString sBuffer = m_pUser->GetUserName();
CString sRet = GetSavePath();
sRet += "/.znc-away-" + CBlowfish::MD5(sBuffer, true);
return(sRet);
}
virtual void Away(bool bForce = false, const CString & sReason = "")
{
if ((!m_bIsAway) || (bForce))
{
if (!bForce)
m_sReason = sReason;
else if (!sReason.empty())
m_sReason = sReason;
time_t iTime = time(NULL);
char *pTime = ctime(&iTime);
CString sTime;
if (pTime)
{
sTime = pTime;
sTime.Trim();
}
if (m_sReason.empty())
m_sReason = "Auto Away at " + sTime;
PutIRC("AWAY :" + m_sReason);
m_bIsAway = true;
}
}
virtual void Back(bool bUsePrivMessage = false)
{
PutIRC("away");
m_bIsAway = false;
if (!m_vMessages.empty())
{
if (bUsePrivMessage)
{
PutModule("Welcome Back!", "away");
PutModule("You have " + CString(m_vMessages.size()) + " messages!", "away");
}
else
{
PutModNotice("Welcome Back!", "away");
PutModNotice("You have " + CString(m_vMessages.size()) + " messages!", "away");
}
}
m_sReason = "";
}
virtual EModRet OnPrivMsg(CNick& Nick, CString& sMessage)
{
if (m_bIsAway)
AddMessage(time(NULL), Nick, sMessage);
return(CONTINUE);
}
virtual EModRet OnUserNotice(CString& sTarget, CString& sMessage)
{
Ping();
if (m_bIsAway)
Back();
return(CONTINUE);
}
virtual EModRet OnUserMsg(CString& sTarget, CString& sMessage)
{
Ping();
if (m_bIsAway)
Back();
return(CONTINUE);
}
time_t GetTimeStamp() const { return(m_iLastSentData); }
void Ping() { m_iLastSentData = time(NULL); }
time_t GetAwayTime() { return m_iAutoAway; }
void SetAwayTime(time_t u) { m_iAutoAway = u; }
bool IsAway() { return(m_bIsAway); }
private:
CString m_sPassword;
bool m_bBootError;
bool DecryptMessages(CString & sBuffer)
{
CString sMessages = GetPath();
CString sFile;
sBuffer = "";
CFile File(sMessages);
if (sMessages.empty() || !File.Open() || !File.ReadFile(sFile))
{
PutModule("Unable to find buffer");
return(true); // gonna be successful here
}
File.Close();
if (!sFile.empty())
{
CBlowfish c(m_sPassword, BF_DECRYPT);
sBuffer = c.Crypt(sFile);
if (sBuffer.Left(strlen(CRYPT_VERIFICATION_TOKEN)) != CRYPT_VERIFICATION_TOKEN)
{
// failed to decode :(
PutModule("Unable to decode Encrypted messages");
return(false);
}
sBuffer.erase(0, strlen(CRYPT_VERIFICATION_TOKEN));
}
return(true);
}
void AddMessage(time_t iTime, const CNick & Nick, CString & sMessage)
{
if (m_pUser && Nick.GetNick() == m_pUser->GetIRCNick().GetNick())
return; // ignore messages from self
AddMessage(CString(iTime) + ":" + Nick.GetNickMask() + ":" + sMessage);
}
void AddMessage(const CString & sText)
{
m_vMessages.push_back(sText);
}
time_t m_iLastSentData;
bool m_bIsAway;
time_t m_iAutoAway;
vector<CString> m_vMessages;
CString m_sReason;
};
void CAwayJob::RunJob()
{
CAway *p = (CAway *)m_pModule;
p->SaveBufferToDisk();
if (!p->IsAway())
{
time_t iNow = time(NULL);
if ((iNow - p->GetTimeStamp()) > p->GetAwayTime() && p->GetAwayTime() != 0)
p->Away();
}
}
MODULEDEFS(CAway, "Stores messages while away, also auto away")
|