summaryrefslogtreecommitdiffstats
path: root/src/kvilib/ext/kvi_regchan.cpp
blob: 434ebd9f79c8f9e70e8f183ae4ef873b685e6e67 (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
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
//=============================================================================
//
//   File : kvi_regchan.cpp
//   Creation date : Sat Jun 29 01:01:16 2002 GMT by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2001-2007 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#define __KVILIB__

#include "kvi_regchan.h"
#include "kvi_config.h"
#include "kvi_qstring.h"

KviRegisteredChannel::KviRegisteredChannel(const KviStr &name,const KviStr &nettqmask)
{
	m_szName = name;
	m_szNetMask = nettqmask;
	m_pPropertyDict = new KviPointerHashTable<const char *,KviStr>(7,false,true);
	m_pPropertyDict->setAutoDelete(true);
}

KviRegisteredChannel::~KviRegisteredChannel()
{
	delete m_pPropertyDict;
}



KviRegisteredChannelDataBase::KviRegisteredChannelDataBase()
{
	m_pChannelDict = new KviPointerHashTable<const char *,KviRegisteredChannelList>(17,false,true);
	m_pChannelDict->setAutoDelete(true);
}

KviRegisteredChannelDataBase::~KviRegisteredChannelDataBase()
{
	delete m_pChannelDict;
}

void KviRegisteredChannelDataBase::load(const char * filename)
{
	KviConfig cfg(filename,KviConfig::Read);
	m_pChannelDict->clear();
	KviConfigIterator it(*(cfg.dict()));
	while(KviConfigGroup * d = it.current())
	{
		KviStr szMask = it.currentKey();
		KviStr szChan = szMask.leftToLast('@');
		szMask.cutToLast('@');
		KviRegisteredChannel * c = new KviRegisteredChannel(szChan,szMask);
		add(c);
		KviConfigGroupIterator sit(*d);
		while(TQString * s = sit.current())
		{
			c->setProperty(KviTQString::toUtf8(sit.currentKey()).data(),new KviStr(*s));
			++sit;
		}
		++it;
	}
}

void KviRegisteredChannelDataBase::save(const char * filename)
{
	KviConfig cfg(filename,KviConfig::Write);
	cfg.clear();

	KviPointerHashTableIterator<const char *,KviRegisteredChannelList> it(*m_pChannelDict);
	while(KviRegisteredChannelList * l = it.current())
	{
		for(KviRegisteredChannel * c = l->first();c;c = l->next())
		{
			KviStr szGrp(KviStr::Format,"%s@%s",c->name().ptr(),c->netMask().ptr());
			cfg.setGroup(szGrp.ptr());
			KviPointerHashTableIterator<const char *,KviStr> pit(*(c->propertyDict()));
			while(KviStr * s = pit.current())
			{
				cfg.writeEntry(pit.currentKey(),s->ptr());
				++pit;
			}
		}
		++it;
	}
}

KviRegisteredChannel * KviRegisteredChannelDataBase::find(const char * name,const char * net)
{
	KviRegisteredChannelList * l = m_pChannelDict->find(name);
	if(!l)return 0;
	for(KviRegisteredChannel * c = l->first();c;c = l->next())
	{
		if(kvi_matchString(c->netMask().ptr(),net))return c;
	}
	
	return 0;
}

KviRegisteredChannel * KviRegisteredChannelDataBase::findExact(const char * name,const char * nettqmask)
{
	KviRegisteredChannelList * l = m_pChannelDict->find(name);
	if(!l)return 0;
	for(KviRegisteredChannel * c = l->first();c;c = l->next())
	{
		if(kvi_strEqualCI(c->netMask().ptr(),nettqmask))return c;
	}
	return 0;
}

void KviRegisteredChannelDataBase::remove(KviRegisteredChannel * c)
{
	KviRegisteredChannelList * l = m_pChannelDict->find(c->name().ptr());
	if(!l)return;
	for(KviRegisteredChannel * ch = l->first();ch;ch = l->next())
	{
		if(ch == c)
		{
			if(l->count() <= 1)
			{
				m_pChannelDict->remove(c->name().ptr());
			} else {
				l->removeRef(c);
			}
			return;
		}
	}
}


void KviRegisteredChannelDataBase::add(KviRegisteredChannel * c)
{
	KviRegisteredChannel * old = findExact(c->name().ptr(),c->netMask().ptr());
	if(old)
	{
		KviPointerHashTableIterator<const char *,KviStr> pit(*(old->propertyDict()));
		while(KviStr *s = pit.current())
		{
			if(!c->property(pit.currentKey()))
				c->setProperty(pit.currentKey(),new KviStr(*s));
			++pit;
		}
		remove(old);
	}
	KviRegisteredChannelList * l = m_pChannelDict->find(c->name().ptr());
	if(!l)
	{
		l = new KviRegisteredChannelList;
		l->setAutoDelete(true);
		m_pChannelDict->insert(c->name().ptr(),l);
	}
	// insert where there are less wildcards
	int o = c->netMask().occurences('*');
	int idx = 0;
	for(KviRegisteredChannel * rc = l->first();rc;rc = l->next())
	{
		if(rc->netMask().occurences('*') > o)
		{
			// the existing has more wildcards , insert here!
			l->insert(idx,c);
			return;
		}
		idx++;
	}
	l->append(c);
}