File: kickrejoin.cpp

package info (click to toggle)
znc 0.092-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,328 kB
  • ctags: 3,878
  • sloc: cpp: 26,913; sh: 2,892; perl: 358; makefile: 250; tcl: 204
file content (111 lines) | stat: -rw-r--r-- 2,681 bytes parent folder | download
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
/*
 * 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.
 *
 * This was originally written by cycomate.
 *
 * Autorejoin module
 * rejoin channel (after a delay) when kicked
 * Usage: LoadModule = rejoin [delay]
 *
 */

#include "Chan.h"
#include "User.h"

class CRejoinJob: public CTimer {
public:
	CRejoinJob(CModule* pModule, unsigned int uInterval, unsigned int uCycles, const CString& sLabel, const CString& sDescription)
	: CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {
	}

	virtual ~CRejoinJob() {}

protected:
	virtual void RunJob() {
		CUser* user = m_pModule->GetUser();
		CChan* pChan = user->FindChan(GetName().Token(1, true));

		if (pChan) {
			pChan->Enable();
			GetModule()->PutIRC("JOIN " + pChan->GetName() + " " + pChan->GetKey());
		}
	}
};

class CRejoinMod : public CModule {
private:
	unsigned int delay;

public:
	MODCONSTRUCTOR(CRejoinMod) {}
	virtual ~CRejoinMod() {}

	virtual bool OnLoad(const CString& sArgs, CString& sErrorMsg) {
		if (sArgs.empty()) {
			CString sDelay = GetNV("delay");

			if (sDelay.empty())
				delay = 10;
			else
				delay = sDelay.ToUInt();
		} else {
			int i = sArgs.ToInt();
			if ((i == 0 && sArgs == "0") || i > 0)
				delay = i;
			else {
				sErrorMsg = "Illegal argument, "
					"must be a positive number or 0";
				return false;
			}
		}

		return true;
	}

	virtual void OnModCommand(const CString& sCommand) {
		CString sCmdName = sCommand.Token(0).AsLower();

		if (sCmdName == "setdelay") {
			int i;
			i = sCommand.Token(1).ToInt();

			if (i < 0) {
				PutModule("Negative delays don't make any sense!");
				return;
			}

			delay = i;
			SetNV("delay", CString(delay));

			if (delay)
				PutModule("Rejoin delay set to " + CString(delay) + " seconds");
			else
				PutModule("Rejoin delay disabled");
		} else if (sCmdName == "showdelay") {
			if (delay)
				PutModule("Rejoin delay enabled, " + CString(delay) + " seconds");
			else
				PutModule("Rejoin delay disabled");
		} else {
			PutModule("Commands: setdelay <secs>, showdelay");
		}
	}

	virtual void OnKick(const CNick& OpNick, const CString& sKickedNick, CChan& pChan, const CString& sMessage) {
		if (m_pUser->GetCurNick().Equals(sKickedNick)) {
			if (!delay) {
				PutIRC("JOIN " + pChan.GetName() + " " + pChan.GetKey());
				pChan.Enable();
				return;
			}
			AddTimer(new CRejoinJob(this, delay, 1, "Rejoin " + pChan.GetName(),
						"Rejoin channel after a delay"));
		}
	}
};

MODULEDEFS(CRejoinMod, "Autorejoin on kick")