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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
//=============================================================================
//
// File : kvi_filetrader.cpp
// Creation date : Wed Aug 27 2000 10:33:11 CEST by Szymon Stefanek
//
// This file is part of the KVirc irc client distribution
// Copyright (C) 1999-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_sharedfiles.h"
#include "kvi_config.h"
#include "kvi_fileutils.h"
#include <tqfileinfo.h>
// TODO: Match servers that the file requests come from
// TODO: Max number of downloads ?
// FIXME: MD5SUM ?
/*
@doc: shared_files
@title:
Sharing files with KVIrc
@type:
generic
@short:
Automatically sharing your files with other IRC users
@keyterms:
file sharing
@body:
[big]What is this ?[/big]
The "file offers" are a simple way to share your files with other IRC users.[br]
Basically , you setup an offer by selecting a local file, choosing a "visible name" for it.
Remote users will be able to request you the file and download it automatically by
issuing a simple DCC GET request.[br]
[big]Details[/big]
Each offer refers to an existing file on one of your locally mounted file systems.
The offer is given a visible name that the remote users will effectively request.
To share the file /usr/arch/mp3/SonataArctica_SingInSilence_Live.mp3 you will add a file offer
with /usr/arch/mp3/SonataArctica_SingInSilence_Live.mp3 as real file path , something like
"SonataArctica_SingInSilence.mp3". A remote user will then request you a DCC GET SonataArctica_SingInSilence.mp3
and KVIrc will automatically send the file.[br]
Each file offer has an "user mask" that the requesting remote users must match to
obtain the file: *!*@* matches any user, Pragma!*@* matches any user with nickname pragma,
*!*@*.omnikron.net matches any user coming from the omnikron.net domain.[br]
Each offer can have an expire time: the offer will be automatically removed after
a defined number of seconds. An expire time of '0' seconds means that the offer should never expire.[br]
If you have two file offers with the same name and different file, the remote user can
use an additional "size" parameter in the DCC GET request.[br]
[big]Security issues[/big]
This is a nice but unsecure method of sharing files.[br]
The user mask is a good protection but you have to use it properly!.[br]
Setting the user mask to Nick!*@* can be easily exploited (just by making an user disconnect
in one of the well known ways and then by using his nickname).[br]
On the other side, the remote end must know exactly the visible name of the offer to request
and noone but you will tell him that name.[br]
In sum:[br]
Don't share any really important files: this *might* be like putting it on your webpage :D[br]
Please don't send complains if someone stoles your /etc/passwd : it is because you have permitted that.[br]
*/
KviSharedFile::KviSharedFile(const TQString &szName,const TQString &szAbsPath,const TQString &szUserMask,time_t expireTime,unsigned int uFileSize)
{
m_szName = szName;
m_szAbsFilePath = szAbsPath;
m_szUserMask = szUserMask;
m_expireTime = expireTime;
m_uFileSize = uFileSize;
#ifdef COMPILE_USE_QT4
// QT4ROX: Because they have finally moved the functionality of TQString::contains() to TQString::count(), and TQString::contains() now does the right job
m_uWildCount = m_szUserMask.count('*');
#else
m_uWildCount = m_szUserMask.contains('*');
#endif
m_uNonWildCount = m_szUserMask.length() - m_uWildCount;
}
KviSharedFile::~KviSharedFile()
{
}
KviSharedFilesManager::KviSharedFilesManager()
: TQObject()
{
m_pSharedListDict = new KviPointerHashTable<TQString,KviSharedFileList>();
m_pSharedListDict->setAutoDelete(true);
m_pCleanupTimer = new TQTimer();
connect(m_pCleanupTimer,TQT_SIGNAL(timeout()),this,TQT_SLOT(cleanup()));
}
KviSharedFilesManager::~KviSharedFilesManager()
{
if(m_pCleanupTimer->isActive())m_pCleanupTimer->stop();
delete m_pCleanupTimer;
delete m_pSharedListDict;
}
void KviSharedFilesManager::cleanup()
{
KviPointerHashTableIterator<TQString,KviSharedFileList> it(*m_pSharedListDict);
time_t curTime = time(0);
bool bOtherStuffToCleanup = false;
//bool bChanged = false;
KviPointerList<TQString> lDying;
lDying.setAutoDelete(true);
while(KviSharedFileList * l = it.current())
{
KviPointerList<KviSharedFile> tmp;
tmp.setAutoDelete(false);
for(KviSharedFile * o = l->first();o;o = l->next())
{
if(o->expireTime() > 0)
{
if(((int)o->expireTime()) <= ((int)curTime))
{
tmp.append(o);
//bChanged = true;
} else {
bOtherStuffToCleanup = true;
}
}
}
for(KviSharedFile * fo = tmp.first();fo;fo = tmp.next())
{
l->removeRef(fo);
emit sharedFileRemoved(fo);
}
if(l->count() == 0)
lDying.append(new TQString(it.currentKey()));
++it;
}
for(TQString * pDyingKey = lDying.first();pDyingKey;pDyingKey = lDying.next())
m_pSharedListDict->remove(*pDyingKey);
if(!bOtherStuffToCleanup)m_pCleanupTimer->stop();
//if(bChanged)emit sharedFilesChanged();
}
void KviSharedFilesManager::clear()
{
m_pSharedListDict->clear();
emit sharedFilesChanged();
}
void KviSharedFilesManager::doInsert(KviSharedFileList * l, KviSharedFile * o)
{
int index = 0;
for(KviSharedFile * fo =l->first();fo;fo = l->next())
{
if(o->wildcardCount() > 0)
{
// the new mask has wildcards... if the current one has none, skip it
if(fo->wildcardCount() > 0)
{
// the one in the list has wildcards too...
// the ones with more non-wild chars go first...
if(fo->nonWildcardCount() < o->nonWildcardCount())
{
// ok...the new one has more non-wildcards , insert
l->insert(index,o);
return;
} else {
if(o->nonWildcardCount() == fo->nonWildcardCount())
{
// the same number of non-wildcards
// let the number of wildcards decide (it will be eventually equal)
if(o->wildcardCount() < fo->wildcardCount())
{
// the new one has less wildcards... goes first
l->insert(index,o);
return;
} // else the same number of wildcards and non-wildcards...skip
} // else the existing one has more non-wildcards...skip
}
} // else the current has no wildcards...skip
} else {
// the new mask has no wildcards....
if(fo->wildcardCount() > 0)
{
// current one has wildcards...insert
l->insert(index,o);
return;
}
// the current one has no wildcards...
// the longer masks go first....
if(fo->maskLength() < o->maskLength())
{
// the current one is shorter than the new one...insert
l->insert(index,o);
return;
} // else current one is longer...skip
}
index++;
}
l->append(o);
}
void KviSharedFilesManager::addSharedFile(KviSharedFile * f)
{
// First find the list
KviSharedFileList * l = m_pSharedListDict->find(f->name());
if(!l)
{
l = new KviSharedFileList;
l->setAutoDelete(true);
m_pSharedListDict->replace(f->name(),l);
}
doInsert(l,f);
if(((int)f->expireTime()) > 0)
{
if(!m_pCleanupTimer->isActive())m_pCleanupTimer->start(60000);
}
emit sharedFileAdded(f);
}
KviSharedFile * KviSharedFilesManager::addSharedFile(const TQString &szName,const TQString &szAbsPath,const TQString &szMask,int timeoutInSecs)
{
TQFileInfo inf(szAbsPath);
if(inf.exists() && inf.isFile() && inf.isReadable() && (inf.size() > 0))
{
// First find the list
KviSharedFileList * l = m_pSharedListDict->find(szName);
if(!l)
{
l = new KviSharedFileList;
l->setAutoDelete(true);
m_pSharedListDict->replace(szName,l);
}
// Now insert
KviSharedFile * o = new KviSharedFile(szName,szAbsPath,szMask,timeoutInSecs > 0 ? (((int)(time(0))) + timeoutInSecs) : 0,inf.size());
doInsert(l,o);
if(((int)o->expireTime()) > 0)
{
if(!m_pCleanupTimer->isActive())m_pCleanupTimer->start(60000);
}
emit sharedFileAdded(o);
return o;
} else {
tqDebug("File %s unreadable: can't add offer",KviTQString::toUtf8(szAbsPath).data());
return 0;
}
}
KviSharedFile * KviSharedFilesManager::lookupSharedFile(const TQString &szName,KviIrcMask * mask,unsigned int uFileSize)
{
KviSharedFileList * l = m_pSharedListDict->find(szName);
if(!l)return 0;
for(KviSharedFile * o = l->first();o;o = l->next())
{
bool bMatch;
if(mask)
{
KviIrcMask umask(o->userMask());
bMatch = mask->matchedBy(umask);
} else bMatch = KviTQString::equalCS(o->userMask(),"*!*@*");
if(bMatch)
{
if(uFileSize > 0)
{
if(uFileSize == o->fileSize())return o;
} else return o;
}
}
return 0;
}
bool KviSharedFilesManager::removeSharedFile(const TQString &szName,const TQString &szMask,unsigned int uFileSize)
{
KviSharedFileList * l = m_pSharedListDict->find(szName);
if(!l)return false;
for(KviSharedFile * o = l->first();o;o = l->next())
{
if(KviTQString::equalCI(szMask,o->userMask()))
{
bool bMatch = uFileSize > 0 ? uFileSize == o->fileSize() : true;
if(bMatch)
{
TQString save = szName; // <-- szName MAY Be a pointer to o->name()
l->removeRef(o);
if(l->count() == 0)m_pSharedListDict->remove(save);
emit sharedFileRemoved(o);
return true;
}
}
}
return false;
}
bool KviSharedFilesManager::removeSharedFile(const TQString &szName,KviSharedFile * off)
{
KviSharedFileList * l = m_pSharedListDict->find(szName);
if(!l)return false;
for(KviSharedFile * o = l->first();o;o = l->next())
{
if(off == o)
{
TQString save = szName; // <-- szName MAY Be a pointer to o->name()
l->removeRef(o);
if(l->count() == 0)m_pSharedListDict->remove(save);
emit sharedFileRemoved(off);
return true;
}
}
return false;
}
void KviSharedFilesManager::load(const TQString &filename)
{
KviConfig cfg(filename,KviConfig::Read);
//cfg.clear();
cfg.setGroup("PermanentFileOffers");
int num = cfg.readIntEntry("NEntries",0);
for(int idx=0;idx<num;idx++)
{
TQString tmp;
KviTQString::sprintf(tmp,"%dFName",idx);
TQString szName = cfg.readTQStringEntry(tmp,"");
KviTQString::sprintf(tmp,"%dFilePath",idx);
TQString szPath = cfg.readTQStringEntry(tmp,"");
KviTQString::sprintf(tmp,"%dUserMask",idx);
TQString szMask = cfg.readTQStringEntry(tmp,"");
if(!szMask.isEmpty() && !szPath.isEmpty() && !szName.isEmpty())
addSharedFile(szName,szPath,szMask,0);
}
}
void KviSharedFilesManager::save(const TQString &filename)
{
KviConfig cfg(filename,KviConfig::Write);
cfg.clear();
cfg.setGroup("PermanentFileOffers");
KviPointerHashTableIterator<TQString,KviSharedFileList> it(*m_pSharedListDict);
int idx = 0;
while(KviSharedFileList * l = it.current())
{
for(KviSharedFile * o = l->first();o;o = l->next())
{
if(((int)(o->expireTime())) == 0)
{
TQString tmp;
KviTQString::sprintf(tmp,"%dFName",idx);
cfg.writeEntry(tmp,it.currentKey());
KviTQString::sprintf(tmp,"%dFilePath",idx);
cfg.writeEntry(tmp,o->absFilePath());
KviTQString::sprintf(tmp,"%dUserMask",idx);
cfg.writeEntry(tmp,o->userMask());
++idx;
}
}
++it;
}
cfg.writeEntry("NEntries",idx);
}
|