summaryrefslogtreecommitdiff
path: root/modules/chanserv/main/chanaccess.cpp
blob: 1601260c2ea98330b4e388b584bb08c8807d9d3a (plain)
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
#include "module.h"
#include "chanaccess.h"
#include "chanaccesstype.h"

ChanServ::Channel *ChanAccessImpl::GetChannel()
{
	return Get(&ChanAccessType::ci);
}

void ChanAccessImpl::SetChannel(ChanServ::Channel *ci)
{
	Object::Set(&ChanAccessType::ci, ci);
}

Anope::string ChanAccessImpl::GetCreator()
{
	return Get(&ChanAccessType::creator);
}

void ChanAccessImpl::SetCreator(const Anope::string &c)
{
	Object::Set(&ChanAccessType::creator, c);
}

time_t ChanAccessImpl::GetLastSeen()
{
	return Get(&ChanAccessType::last_seen);
}

void ChanAccessImpl::SetLastSeen(const time_t &t)
{
	Object::Set(&ChanAccessType::last_seen, t);
}

time_t ChanAccessImpl::GetCreated()
{
	return Get(&ChanAccessType::created);
}

void ChanAccessImpl::SetCreated(const time_t &t)
{
	Object::Set(&ChanAccessType::created, t);
}

Anope::string ChanAccessImpl::GetMask()
{
	return Get(&ChanAccessType::mask);
}

void ChanAccessImpl::SetMask(const Anope::string &n)
{
	Object::Set(&ChanAccessType::mask, n);
}

Serialize::Object *ChanAccessImpl::GetObj()
{
	return Get(&ChanAccessType::obj);
}

void ChanAccessImpl::SetObj(Serialize::Object *o)
{
	Object::Set(&ChanAccessType::obj, o);
}

Anope::string ChanAccessImpl::Mask()
{
	if (NickServ::Account *acc = GetAccount())
		return acc->GetDisplay();

	return GetMask();
}

NickServ::Account *ChanAccessImpl::GetAccount()
{
	if (!GetObj() || GetObj()->GetSerializableType() != NickServ::account)
		return nullptr;

	return anope_dynamic_static_cast<NickServ::Account *>(GetObj());
}

bool ChanAccessImpl::Matches(const User *u, NickServ::Account *acc, Path &p)
{
	if (this->GetAccount())
		return this->GetAccount() == acc;

	if (u)
	{
		bool is_mask = this->Mask().find_first_of("!@?*") != Anope::string::npos;
		if (is_mask && Anope::Match(u->nick, this->Mask()))
			return true;
		else if (Anope::Match(u->GetDisplayedMask(), this->Mask()))
			return true;
	}

	if (acc)
		for (NickServ::Nick *na : acc->GetRefs<NickServ::Nick *>(NickServ::nick))
			if (Anope::Match(na->GetNick(), this->Mask()))
				return true;

	if (IRCD->IsChannelValid(this->Mask()))
	{
		ChanServ::Channel *tci = ChanServ::Find(this->Mask());
		if (tci)
		{
			for (unsigned i = 0; i < tci->GetAccessCount(); ++i)
			{
				ChanServ::ChanAccess *a = tci->GetAccess(i);
				std::pair<ChanServ::ChanAccess *, ChanServ::ChanAccess *> pair = std::make_pair(this, a);

				std::pair<Set::iterator, Set::iterator> range = p.first.equal_range(this);
				for (; range.first != range.second; ++range.first)
					if (range.first->first == pair.first && range.first->second == pair.second)
						goto cont;

				p.first.insert(pair);
				if (a->Matches(u, acc, p))
					p.second.insert(pair);

				cont:;
			}

			return p.second.count(this) > 0;
		}
	}

	return false;
}