-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathawaynickiscool.cpp
208 lines (166 loc) · 4.94 KB
/
awaynickiscool.cpp
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
/*
* Copyright (C) 2004-2015 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* NOTE: This is just a redistributed version of the origin alwaynick
* by ZNC itself. I didn't make the code, I just redistribute it.
*/
#include <znc/IRCSock.h>
#include <znc/IRCNetwork.h>
class CAwayNickMod;
class CAwayNickTimer : public CTimer {
public:
CAwayNickTimer(CAwayNickMod& Module);
private:
virtual void RunJob();
private:
CAwayNickMod& m_Module;
};
class CBackNickTimer : public CTimer {
public:
CBackNickTimer(CModule& Module)
: CTimer(&Module, 3, 1, "BackNickTimer", "Set your nick back when you reattach"),
m_Module(Module) {}
private:
virtual void RunJob() {
CIRCNetwork* pNetwork = m_Module.GetNetwork();
if (pNetwork->IsUserAttached() && pNetwork->IsIRCConnected()) {
CString sConfNick = pNetwork->GetNick();
m_Module.PutIRC("NICK " + sConfNick);
}
}
private:
CModule& m_Module;
};
class CAwayNickMod : public CModule {
public:
MODCONSTRUCTOR(CAwayNickMod) {}
virtual bool OnLoad(const CString& sArgs, CString& sMessage) {
if (!sArgs.empty())
m_sFormat = sArgs;
else
m_sFormat = GetNV("nick");
if (m_sFormat.empty()) {
m_sFormat = "zz_%nick%";
}
SetNV("nick", m_sFormat);
return true;
}
virtual ~CAwayNickMod() {
}
void StartAwayNickTimer() {
RemTimer("AwayNickTimer");
if (FindTimer("BackNickTimer")) {
// Client disconnected before we got set back, so do nothing.
RemTimer("BackNickTimer");
return;
}
AddTimer(new CAwayNickTimer(*this));
}
void StartBackNickTimer() {
CIRCSock* pIRCSock = GetNetwork()->GetIRCSock();
if (pIRCSock) {
CString sConfNick = GetNetwork()->GetNick();
if (pIRCSock->GetNick().Equals(m_sAwayNick.Left(pIRCSock->GetNick().length()))) {
RemTimer("BackNickTimer");
AddTimer(new CBackNickTimer(*this));
}
}
}
virtual EModRet OnIRCRegistration(CString& sPass, CString& sNick,
CString& sIdent, CString& sRealName) {
CIRCNetwork* pNetwork = GetNetwork();
if (pNetwork && !pNetwork->IsUserAttached()) {
m_sAwayNick = m_sFormat;
// ExpandString doesn't know our nick yet, so do it by hand.
m_sAwayNick.Replace("%nick%", sNick);
// We don't limit this to NICKLEN, because we dont know
// NICKLEN yet.
sNick = m_sAwayNick = pNetwork->ExpandString(m_sAwayNick);
}
return CONTINUE;
}
virtual void OnIRCDisconnected() {
RemTimer("AwayNickTimer");
RemTimer("BackNickTimer");
}
virtual void OnClientLogin() {
StartBackNickTimer();
}
virtual void OnClientDisconnect() {
if (!GetNetwork()->IsUserAttached()) {
StartAwayNickTimer();
}
}
virtual void OnModCommand(const CString& sLine) {
CString sCommand = sLine.Token(0);
if (sCommand.Equals("TIMERS")) {
ListTimers();
}
else if (sCommand.Equals("SET")) {
CString sFormat = sLine.Token(1);
if (!sFormat.empty()) {
m_sFormat = sFormat;
SetNV("nick", m_sFormat);
}
if (GetNetwork()) {
CString sExpanded = GetAwayNick();
CString sMsg = "AwayNick is set to [" + m_sFormat + "]";
if (m_sFormat != sExpanded) {
sMsg += " (" + sExpanded + ")";
}
PutModule(sMsg);
}
} else if (sCommand.Equals("SHOW")) {
if (GetNetwork()) {
CString sExpanded = GetAwayNick();
CString sMsg = "AwayNick is set to [" + m_sFormat + "]";
if (m_sFormat != sExpanded) {
sMsg += " (" + sExpanded + ")";
}
PutModule(sMsg);
}
} else if (sCommand.Equals("HELP")) {
PutModule("Commands are: show, timers, set [awaynick]");
}
}
CString GetAwayNick() {
unsigned int uLen = 9;
CIRCSock* pIRCSock = GetNetwork()->GetIRCSock();
if (pIRCSock) {
uLen = pIRCSock->GetMaxNickLen();
}
m_sAwayNick = GetNetwork()->ExpandString(m_sFormat).Left(uLen);
return m_sAwayNick;
}
private:
CString m_sFormat;
CString m_sAwayNick;
};
CAwayNickTimer::CAwayNickTimer(CAwayNickMod& Module)
: CTimer(&Module, 30, 1, "AwayNickTimer", "Set your nick while you're detached"),
m_Module(Module) {}
void CAwayNickTimer::RunJob() {
CIRCNetwork* pNetwork = m_Module.GetNetwork();
if (!pNetwork->IsUserAttached() && pNetwork->IsIRCConnected()) {
m_Module.PutIRC("NICK " + m_Module.GetAwayNick());
}
}
template<> void TModInfo<CAwayNickMod>(CModInfo& Info) {
Info.SetWikiPage("awaynick");
Info.SetHasArgs(true);
Info.SetArgsHelpText("This will be your nickname while you are away. Examples: nick_off or zzz_nick.");
}
NETWORKMODULEDEFS(CAwayNickMod, "Change your nick while you are away")