summaryrefslogtreecommitdiffstats
path: root/src/modules/sharedfile/libkvisharedfile.cpp
blob: adbf7af11c962acd62eeed5ddf9ca5d9ef85d69b (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
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
//=============================================================================
//
//   File : libkvisharedfile.cpp (orig : libkvioffer.cpp)
//   Creation date : Wed Sep 27 2000 20:59:02 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2000-2005 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.
//
//=============================================================================

#include "kvi_module.h"

#include "kvi_sharedfiles.h"
#include "kvi_ircmask.h"
#include "kvi_fileutils.h"
#include "kvi_locale.h"

#include "kvi_out.h"
#include "kvi_mirccntrl.h"
#include "kvi_window.h"
#include "kvi_frame.h"


#include <time.h>
#include "kvi_pointerhashtable.h"

extern KVIRC_API KviSharedFilesManager * g_pSharedFilesManager;


/*
	@doc: sharedfile.add
	@type:
		command
	@title:
		sharedfile.add
	@keyterms:
		trading files by dcc
	@short:
		Adds a file sharedfile
	@syntax:
		sharedfile.add [-t=<timeout:integer>] [-n=<visible name>] <filename> [user_tqmask]
	@switches:
		!sw: -t=<timeout> | --timeout=<timeout>
		Specified the timeout after that the share will be automatically removed
		!sw: -n=<visible name> | --name=<visible name>
		Explicitly specifies the visible name of the share
	@description:
		Adds <filename> to the list of the active shared files.
		The users will be able to request the file via [cmd]dcc.get[/cmd].
		If [user_tqmask] is specified , the access to the file is limited
		to the users that match this tqmask, otherwise the tqmask will
		be automatically set to '*!*@*'.[br]
		If the 't' switch is used, the sharedfile will be removed after
		<timeout> seconds. If the 'n' switch is used, the sharedfile
		will be visible to the oter users as <visible name> instead
		of the real <filename> (stripped of the leading path).
		<filename> must be an absolute path.
	@seealso:
		[cmd]sharedfile.remove[/cmd], [cmd]sharedfile.list[/cmd],
		[cmd]dcc.get[/cmd]
*/

static bool sharedfile_kvs_cmd_add(KviKvsModuleCommandCall * c)
{
	TQString szFileName,szUserMask;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szFileName)
		KVSM_PARAMETER("user_tqmask",KVS_PT_NONEMPTYSTRING,KVS_PF_OPTIONAL,szUserMask)
	KVSM_PARAMETERS_END(c)

	if(!KviFileUtils::isReadable(szFileName))
	{
		c->warning(__tr2qs_ctx("The file '%Q' is not readable","sharedfile"),&szFileName);
		return true;
	}

	if(szUserMask.isEmpty())szUserMask="*!*@*";
	KviIrcMask u(szUserMask);
	
	TQString szm;
	u.tqmask(szm);

	kvs_int_t timeout = 0;

	if(KviKvsVariant * v = c->switches()->tqfind('t',"timeout"))
	{
		if(!v->asInteger(timeout))
		{
			c->warning(__tr2qs_ctx("Invalid timeout, ignoring","sharedfile"));
			timeout = 0;
		}
	}

	TQString szVisibleName = szFileName;
	KviTQString::cutToLast(szVisibleName,'/');

	if(KviKvsVariant * n = c->switches()->tqfind('n',"name"))
	{
		TQString tmp;
		n->asString(tmp);
		if(tmp.isEmpty())
		{
			c->warning(__tr2qs_ctx("Invalid visible name: using default","sharedfile"));
		} else szVisibleName = tmp;
	}

	if(!g_pSharedFilesManager->addSharedFile(szVisibleName,szFileName,szUserMask,timeout))
	{
		c->warning(__tr2qs_ctx("Ops..failed to add the sharedfile...","sharedfile"));
	}

	return true;
}


/*
	@doc: sharedfile.remove
	@type:
		command
	@title:
		sharedfile.remove
	@keyterms:
		trading files by dcc
	@short:
		Removes a shared file
	@syntax:
		sharedfile.remove <visible name:string> <user tqmask:string> [filesize:integer]
	@description:
		Removes the shared file that matches <visible name> and <user tqmask>.
		If [filesize] is specified, then it must be matched by the entry
		to be removed.
	@seealso:
		[cmd]sharedfile.add[/cmd], [cmd]sharedfile.list[/cmd], [cmd]sharedfile.clear[/cmd]
*/

static bool sharedfile_kvs_cmd_remove(KviKvsModuleCommandCall * c)
{
	TQString szVisibleName,szUserMask;
	kvs_uint_t uSize;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("visible_name",KVS_PT_NONEMPTYSTRING,0,szVisibleName)
		KVSM_PARAMETER("user_tqmask",KVS_PT_NONEMPTYSTRING,0,szUserMask)
		KVSM_PARAMETER("filesize",KVS_PT_UINT,KVS_PF_OPTIONAL,uSize)
	KVSM_PARAMETERS_END(c)

	if(!g_pSharedFilesManager->removeSharedFile(szVisibleName,szUserMask,uSize))
		c->warning(__tr2qs_ctx("No sharedfile with visible name '%Q' and user tqmask '%Q'","sharedfile"),&szVisibleName,&szUserMask);

	return true;
}



/*
	@doc: sharedfile.clear
	@type:
		command
	@title:
		sharedfile.clear
	@keyterms:
		trading files by dcc, shared files
	@short:
		Clears the shared files list
	@syntax:
		sharedfile.clear
	@description:
		Clears the shared files list
	@seealso:
		[cmd]sharedfile.add[/cmd], [cmd]sharedfile.list[/cmd], [cmd]sharedfile.remove[/cmd]
*/

static bool sharedfile_kvs_cmd_clear(KviKvsModuleCommandCall * c)
{
	g_pSharedFilesManager->clear();
	return true;
}


/*
	@doc: sharedfile.list
	@type:
		command
	@title:
		sharedfile.list
	@keyterms:
		trading files by dcc
	@short:
		Lists the active file sharedfile
	@syntax:
		sharedfile.list
	@description:
		Lists the active file sharedfile.
	@seealso:
		[cmd]sharedfile.add[/cmd], [cmd]sharedfile.remove[/cmd]
*/

static bool sharedfile_kvs_cmd_list(KviKvsModuleCommandCall * c)
{
	KviPointerHashTableIterator<TQString,KviSharedFileList> it(*(g_pSharedFilesManager->sharedFileListDict()));

	int idx = 0;

	while(KviSharedFileList * l = it.current())
	{
		for(KviSharedFile * o = l->first();o;o = l->next())
		{
			c->window()->output(KVI_OUT_NONE,"%c%d. %s",
				KVI_TEXT_BOLD,idx + 1,it.currentKey().utf8().data());
			c->window()->output(KVI_OUT_NONE,__tr2qs_ctx("    File: %s (%u bytes)","sharedfile"),
				o->absFilePath().utf8().data(),o->fileSize());
			c->window()->output(KVI_OUT_NONE,__tr2qs_ctx("    Mask: %s","sharedfile"),
				o->userMask().utf8().data());
			if(o->expireTime() > 0)
			{
				int secs = ((int)(o->expireTime())) - ((int)(time(0)));
				int hour = secs / 3600;
				secs = secs % 3600;
				int mins = secs / 60;
				secs = secs % 60;
				c->window()->output(KVI_OUT_NONE,__tr2qs_ctx("    Expires in %d hours %d minutes %d seconds","sharedfile"),
					hour,mins,secs);
			}
			++idx;
		}
		++it;
	}

//#warning "FIND A BETTER KVI_OUT_*"

	if(idx == 0)c->window()->outputNoFmt(KVI_OUT_NONE,__tr2qs_ctx("No active file sharedfile","sharedfile"));
	else c->window()->output(KVI_OUT_NONE,__tr2qs_ctx("Total: %d sharedfile","sharedfile"),idx);

	return true;
}


static bool sharedfile_module_init(KviModule * m)
{

	KVSM_REGISTER_SIMPLE_COMMAND(m,"add",sharedfile_kvs_cmd_add);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"remove",sharedfile_kvs_cmd_remove);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"list",sharedfile_kvs_cmd_list);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"clear",sharedfile_kvs_cmd_clear);

	return true;
}

static bool sharedfile_module_can_unload(KviModule *m)
{
	return true;
}

static bool sharedfile_module_cleanup(KviModule *m)
{
	return true;
}

KVIRC_MODULE(
	"Offer",                                                // module name
	"1.0.0",                                                // module version
	"Copyright (C) 2000-2003 Szymon Stefanek (pragma at kvirc dot net)", // author & (C)
	"User interface to the file sharing system",
	sharedfile_module_init,
	sharedfile_module_can_unload,
	0,
	sharedfile_module_cleanup
)