~ubuntu-branches/ubuntu/trusty/znc/trusty

« back to all changes in this revision

Viewing changes to modules/extra/autovoice.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-05-06 09:18:27 UTC
  • mfrom: (21.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20130506091827-08sixjiyy3hjfx6b
Tags: 1.0-4
* Change section from znc-tcl to interpreters.
* Uploading to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2004-2011  See the AUTHORS file for details.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 2 as published
6
 
 * by the Free Software Foundation.
7
 
 */
8
 
 
9
 
#include "Chan.h"
10
 
#include "User.h"
11
 
 
12
 
class CAutoVoiceUser {
13
 
public:
14
 
        CAutoVoiceUser() {}
15
 
 
16
 
        CAutoVoiceUser(const CString& sLine) {
17
 
                FromString(sLine);
18
 
        }
19
 
 
20
 
        CAutoVoiceUser(const CString& sUsername, const CString& sHostmask, const CString& sChannels) :
21
 
                        m_sUsername(sUsername),
22
 
                        m_sHostmask(sHostmask) {
23
 
                AddChans(sChannels);
24
 
        }
25
 
 
26
 
        virtual ~CAutoVoiceUser() {}
27
 
 
28
 
        const CString& GetUsername() const { return m_sUsername; }
29
 
        const CString& GetHostmask() const { return m_sHostmask; }
30
 
 
31
 
        bool ChannelMatches(const CString& sChan) const {
32
 
                for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
33
 
                        if (sChan.AsLower().WildCmp(*it)) {
34
 
                                return true;
35
 
                        }
36
 
                }
37
 
 
38
 
                return false;
39
 
        }
40
 
 
41
 
        bool HostMatches(const CString& sHostmask) {
42
 
                return sHostmask.WildCmp(m_sHostmask);
43
 
        }
44
 
 
45
 
        CString GetChannels() const {
46
 
                CString sRet;
47
 
 
48
 
                for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
49
 
                        if (!sRet.empty()) {
50
 
                                sRet += " ";
51
 
                        }
52
 
 
53
 
                        sRet += *it;
54
 
                }
55
 
 
56
 
                return sRet;
57
 
        }
58
 
 
59
 
        void DelChans(const CString& sChans) {
60
 
                VCString vsChans;
61
 
                sChans.Split(" ", vsChans);
62
 
 
63
 
                for (unsigned int a = 0; a < vsChans.size(); a++) {
64
 
                        m_ssChans.erase(vsChans[a].AsLower());
65
 
                }
66
 
        }
67
 
 
68
 
        void AddChans(const CString& sChans) {
69
 
                VCString vsChans;
70
 
                sChans.Split(" ", vsChans);
71
 
 
72
 
                for (unsigned int a = 0; a < vsChans.size(); a++) {
73
 
                        m_ssChans.insert(vsChans[a].AsLower());
74
 
                }
75
 
        }
76
 
 
77
 
        CString ToString() const {
78
 
                CString sChans;
79
 
 
80
 
                for (set<CString>::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) {
81
 
                        if (!sChans.empty()) {
82
 
                                sChans += " ";
83
 
                        }
84
 
 
85
 
                        sChans += *it;
86
 
                }
87
 
 
88
 
                return m_sUsername + "\t" + m_sHostmask + "\t" + sChans;
89
 
        }
90
 
 
91
 
        bool FromString(const CString& sLine) {
92
 
                m_sUsername = sLine.Token(0, false, "\t");
93
 
                m_sHostmask = sLine.Token(1, false, "\t");
94
 
                sLine.Token(2, false, "\t").Split(" ", m_ssChans);
95
 
 
96
 
                return !m_sHostmask.empty();
97
 
        }
98
 
private:
99
 
protected:
100
 
        CString      m_sUsername;
101
 
        CString      m_sHostmask;
102
 
        set<CString> m_ssChans;
103
 
};
104
 
 
105
 
class CAutoVoiceMod : public CModule {
106
 
public:
107
 
        MODCONSTRUCTOR(CAutoVoiceMod) {}
108
 
 
109
 
        virtual bool OnLoad(const CString& sArgs, CString& sMessage) {
110
 
                // Load the chans from the command line
111
 
                unsigned int a = 0;
112
 
                VCString vsChans;
113
 
                sArgs.Split(" ", vsChans, false);
114
 
 
115
 
                for (VCString::const_iterator it = vsChans.begin(); it != vsChans.end(); ++it) {
116
 
                        CString sName = "Args";
117
 
                        sName += CString(a);
118
 
                        AddUser(sName, "*", *it);
119
 
                }
120
 
 
121
 
                // Load the saved users
122
 
                for (MCString::iterator it = BeginNV(); it != EndNV(); it++) {
123
 
                        const CString& sLine = it->second;
124
 
                        CAutoVoiceUser* pUser = new CAutoVoiceUser;
125
 
 
126
 
                        if (!pUser->FromString(sLine) || FindUser(pUser->GetUsername().AsLower())) {
127
 
                                delete pUser;
128
 
                        } else {
129
 
                                m_msUsers[pUser->GetUsername().AsLower()] = pUser;
130
 
                        }
131
 
                }
132
 
 
133
 
                return true;
134
 
        }
135
 
 
136
 
        virtual ~CAutoVoiceMod() {
137
 
                for (map<CString, CAutoVoiceUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
138
 
                        delete it->second;
139
 
                }
140
 
 
141
 
                m_msUsers.clear();
142
 
        }
143
 
 
144
 
        virtual void OnJoin(const CNick& Nick, CChan& Channel) {
145
 
                // If we have ops in this chan
146
 
                if (Channel.HasPerm(CChan::Op) || Channel.HasPerm(CChan::HalfOp)) {
147
 
                        for (map<CString, CAutoVoiceUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
148
 
                                // and the nick who joined is a valid user
149
 
                                if (it->second->HostMatches(Nick.GetHostMask()) && it->second->ChannelMatches(Channel.GetName())) {
150
 
                                        PutIRC("MODE " + Channel.GetName() + " +v " + Nick.GetNick());
151
 
                                        break;
152
 
                                }
153
 
                        }
154
 
                }
155
 
        }
156
 
 
157
 
        virtual void OnModCommand(const CString& sLine) {
158
 
                CString sCommand = sLine.Token(0).AsUpper();
159
 
 
160
 
                if (sCommand.Equals("HELP")) {
161
 
                        PutModule("Commands are: ListUsers, AddChans, DelChans, AddUser, DelUser");
162
 
                } else if (sCommand.Equals("ADDUSER") || sCommand.Equals("DELUSER")) {
163
 
                        CString sUser = sLine.Token(1);
164
 
                        CString sHost = sLine.Token(2);
165
 
 
166
 
                        if (sCommand.Equals("ADDUSER")) {
167
 
                                if (sHost.empty()) {
168
 
                                        PutModule("Usage: " + sCommand + " <user> <hostmask> [channels]");
169
 
                                } else {
170
 
                                        CAutoVoiceUser* pUser = AddUser(sUser, sHost, sLine.Token(3, true));
171
 
 
172
 
                                        if (pUser) {
173
 
                                                SetNV(sUser, pUser->ToString());
174
 
                                        }
175
 
                                }
176
 
                        } else {
177
 
                                DelUser(sUser);
178
 
                                DelNV(sUser);
179
 
                        }
180
 
                } else if (sCommand.Equals("LISTUSERS")) {
181
 
                        if (m_msUsers.empty()) {
182
 
                                PutModule("There are no users defined");
183
 
                                return;
184
 
                        }
185
 
 
186
 
                        CTable Table;
187
 
 
188
 
                        Table.AddColumn("User");
189
 
                        Table.AddColumn("Hostmask");
190
 
                        Table.AddColumn("Channels");
191
 
 
192
 
                        for (map<CString, CAutoVoiceUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
193
 
                                Table.AddRow();
194
 
                                Table.SetCell("User", it->second->GetUsername());
195
 
                                Table.SetCell("Hostmask", it->second->GetHostmask());
196
 
                                Table.SetCell("Channels", it->second->GetChannels());
197
 
                        }
198
 
 
199
 
                        PutModule(Table);
200
 
                } else if (sCommand.Equals("ADDCHANS") || sCommand.Equals("DELCHANS")) {
201
 
                        CString sUser = sLine.Token(1);
202
 
                        CString sChans = sLine.Token(2, true);
203
 
 
204
 
                        if (sChans.empty()) {
205
 
                                PutModule("Usage: " + sCommand + " <user> <channel> [channel] ...");
206
 
                                return;
207
 
                        }
208
 
 
209
 
                        CAutoVoiceUser* pUser = FindUser(sUser);
210
 
 
211
 
                        if (!pUser) {
212
 
                                PutModule("No such user");
213
 
                                return;
214
 
                        }
215
 
 
216
 
                        if (sCommand.Equals("ADDCHANS")) {
217
 
                                pUser->AddChans(sChans);
218
 
                                PutModule("Channel(s) added to user [" + pUser->GetUsername() + "]");
219
 
                        } else {
220
 
                                pUser->DelChans(sChans);
221
 
                                PutModule("Channel(s) Removed from user [" + pUser->GetUsername() + "]");
222
 
                        }
223
 
 
224
 
                        SetNV(pUser->GetUsername(), pUser->ToString());
225
 
                } else {
226
 
                        PutModule("Unknown command, try HELP");
227
 
                }
228
 
        }
229
 
 
230
 
        CAutoVoiceUser* FindUser(const CString& sUser) {
231
 
                map<CString, CAutoVoiceUser*>::iterator it = m_msUsers.find(sUser.AsLower());
232
 
 
233
 
                return (it != m_msUsers.end()) ? it->second : NULL;
234
 
        }
235
 
 
236
 
        CAutoVoiceUser* FindUserByHost(const CString& sHostmask, const CString& sChannel = "") {
237
 
                for (map<CString, CAutoVoiceUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
238
 
                        CAutoVoiceUser* pUser = it->second;
239
 
 
240
 
                        if (pUser->HostMatches(sHostmask) && (sChannel.empty() || pUser->ChannelMatches(sChannel))) {
241
 
                                return pUser;
242
 
                        }
243
 
                }
244
 
 
245
 
                return NULL;
246
 
        }
247
 
 
248
 
        void DelUser(const CString& sUser) {
249
 
                map<CString, CAutoVoiceUser*>::iterator it = m_msUsers.find(sUser.AsLower());
250
 
 
251
 
                if (it == m_msUsers.end()) {
252
 
                        PutModule("That user does not exist");
253
 
                        return;
254
 
                }
255
 
 
256
 
                delete it->second;
257
 
                m_msUsers.erase(it);
258
 
                PutModule("User [" + sUser + "] removed");
259
 
        }
260
 
 
261
 
        CAutoVoiceUser* AddUser(const CString& sUser, const CString& sHost, const CString& sChans) {
262
 
                if (m_msUsers.find(sUser) != m_msUsers.end()) {
263
 
                        PutModule("That user already exists");
264
 
                        return NULL;
265
 
                }
266
 
 
267
 
                CAutoVoiceUser* pUser = new CAutoVoiceUser(sUser, sHost, sChans);
268
 
                m_msUsers[sUser.AsLower()] = pUser;
269
 
                PutModule("User [" + sUser + "] added with hostmask [" + sHost + "]");
270
 
                return pUser;
271
 
        }
272
 
 
273
 
private:
274
 
        map<CString, CAutoVoiceUser*> m_msUsers;
275
 
};
276
 
 
277
 
MODULEDEFS(CAutoVoiceMod, "Auto voice the good guys")