aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/sharedfile/libkvisharedfile.cpp
blob: ba60faf9a3f863347db0ac1572cd4ad81fe008e5 (about) (plain) (blame)
1
2
3
4
5
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888 } /* Comment */
.highlight .err { color: #A61717; background-color: #E3D2D2 } /* Error */
.highlight .k { color: #080; font-weight: bold } /* Keyword */
.highlight .ch { color: #888 } /* Comment.Hashbang */
.highlight .cm { color: #888 } /* Comment.Multiline */
.highlight .cp { color: #C00; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888 } /* Comment.Single */
.highlight .cs { color: #C00; font-weight: bold; background-color: #FFF0F0 } /* Comment.Special */
.highlight .gd { color: #000; background-color: #FDD } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #A00 } /* Generic.Error */
.highlight .gh { color: #333 } /* Generic.Heading */
.highlight .gi { color: #000; background-color: #DFD } /* Generic.Inserted */
.highlight .go { color: #888 } /* Generic.Output */
.highlight .gp { color: #555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666 } /* Generic.Subheading */
.highlight .gt { color: #A00 } /* Generic.Traceback */
.highlight .kc { color: #080; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #080; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #080; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #080 } /* Keyword.Pseudo */
.highlight .kr { color: #080; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #00D; font-weight: bold } /* Literal.Number */
.highlight .s { color: #D20; background-color: #FFF0F0 } /* Literal.String */
.highlight .na { color: #369 } /* Name.Attribute */
.highlight .nb { color: #038 } /* Name.Builtin */
.highlight .nc { color: #B06; font-weight: bold } /* Name.Class */
.highlight .no { color: #036; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555 } /* Name.Decorator */
.highlight .ne { color: #B06; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #06B; font-weight: bold } /* Name.Function */
.highlight .nl { color: #369; font-style: italic } /* Name.Label */
.highlight .nn { color: #B06; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #369; font-weight: bold } /* Name.Property */
.highlight .nt { color: #B06; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #369 } /* Name.Variable */
.highlight .ow { color: #080 } /* Operator.Word */
.highlight .w { color: #BBB } /* Text.Whitespace */
.highlight .mb { color: #00D; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #00D; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #00D; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #00D; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #00D; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #D20; background-color: #FFF0F0 } /* Literal.String.Affix */
.highlight .sb { color: #D20; background-color: #FFF0F0 } /* Literal.String.Backtick */
.highlight .sc { color: #D20; background-color: #FFF0F0 } /* Literal.String.Char */
.highlight .dl { color: #D20; background-color: #FFF0F0 } /* Literal.String.Delimiter */
.highlight .sd { color: #D20; background-color: #FFF0F0 } /* Literal.String.Doc */
.highlight .s2 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Double */
.highlight .se { color: #04D; background-color: #FFF0F0 } /* Literal.String.Escape */
.highlight .sh { color: #D20; background-color: #FFF0F0 } /* Literal.String.Heredoc */
.highlight .si { color: #33B; background-color: #FFF0F0 } /* Literal.String.Interpol */
.highlight .sx { color: #2B2; background-color: #F0FFF0 } /* Literal.String.Other */
.highlight .sr { color: #080; background-color: #FFF0FF } /* Literal.String.Regex */
.highlight .s1 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Single */
.highlight .ss { color: #A60; background-color: #FFF0F0 } /* Literal.String.Symbol */
.highlight .bp { color: #038 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #06B; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #369 } /* Name.Variable.Class */
.highlight .vg { color: #D70 } /* Name.Variable.Global */
.highlight .vi { color: #33B } /* Name.Variable.Instance */
.highlight .vm { color: #369 } /* Name.Variable.Magic */
.highlight .il { color: #00D; font-weight: bold } /* Literal.Number.Integer.Long */
package resolve

import "io/fs"

// Sub returns a new TreeFS rooted at dir.
func (treeFS *TreeFS) Sub(dir string) (fs.FS, error) {
	entry, err := treeFS.resolvePath(treeFSOpSub, dir)
	if err != nil {
		return nil, err
	}

	treeID, err := entry.subtreeID()
	if err != nil {
		return nil, treeFSPathError(treeFSOpSub, dir, fs.ErrInvalid)
	}

	return &TreeFS{
		resolver:  treeFS.resolver,
		rootTree:  treeID,
		rootEntry: entry.treeEntry,
	}, nil
}
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888 } /* Comment */
.highlight .err { color: #A61717; background-color: #E3D2D2 } /* Error */
.highlight .k { color: #080; font-weight: bold } /* Keyword */
.highlight .ch { color: #888 } /* Comment.Hashbang */
.highlight .cm { color: #888 } /* Comment.Multiline */
.highlight .cp { color: #C00; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888 } /* Comment.Single */
.highlight .cs { color: #C00; font-weight: bold; background-color: #FFF0F0 } /* Comment.Special */
.highlight .gd { color: #000; background-color: #FDD } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #A00 } /* Generic.Error */
.highlight .gh { color: #333 } /* Generic.Heading */
.highlight .gi { color: #000; background-color: #DFD } /* Generic.Inserted */
.highlight .go { color: #888 } /* Generic.Output */
.highlight .gp { color: #555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666 } /* Generic.Subheading */
.highlight .gt { color: #A00 } /* Generic.Traceback */
.highlight .kc { color: #080; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #080; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #080; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #080 } /* Keyword.Pseudo */
.highlight .kr { color: #080; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #00D; font-weight: bold } /* Literal.Number */
.highlight .s { color: #D20; background-color: #FFF0F0 } /* Literal.String */
.highlight .na { color: #369 } /* Name.Attribute */
.highlight .nb { color: #038 } /* Name.Builtin */
.highlight .nc { color: #B06; font-weight: bold } /* Name.Class */
.highlight .no { color: #036; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555 } /* Name.Decorator */
.highlight .ne { color: #B06; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #06B; font-weight: bold } /* Name.Function */
.highlight .nl { color: #369; font-style: italic } /* Name.Label */
.highlight .nn { color: #B06; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #369; font-weight: bold } /* Name.Property */
.highlight .nt { color: #B06; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #369 } /* Name.Variable */
.highlight .ow { color: #080 } /* Operator.Word */
.highlight .w { color: #BBB } /* Text.Whitespace */
.highlight .mb { color: #00D; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #00D; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #00D; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #00D; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #00D; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #D20; background-color: #FFF0F0 } /* Literal.String.Affix */
.highlight .sb { color: #D20; background-color: #FFF0F0 } /* Literal.String.Backtick */
.highlight .sc { color: #D20; background-color: #FFF0F0 } /* Literal.String.Char */
.highlight .dl { color: #D20; background-color: #FFF0F0 } /* Literal.String.Delimiter */
.highlight .sd { color: #D20; background-color: #FFF0F0 } /* Literal.String.Doc */
.highlight .s2 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Double */
.highlight .se { color: #04D; background-color: #FFF0F0 } /* Literal.String.Escape */
.highlight .sh { color: #D20; background-color: #FFF0F0 } /* Literal.String.Heredoc */
.highlight .si { color: #33B; background-color: #FFF0F0 } /* Literal.String.Interpol */
.highlight .sx { color: #2B2; background-color: #F0FFF0 } /* Literal.String.Other */
.highlight .sr { color: #080; background-color: #FFF0FF } /* Literal.String.Regex */
.highlight .s1 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Single */
.highlight .ss { color: #A60; background-color: #FFF0F0 } /* Literal.String.Symbol */
.highlight .bp { color: #038 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #06B; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #369 } /* Name.Variable.Class */
.highlight .vg { color: #D70 } /* Name.Variable.Global */
.highlight .vi { color: #33B } /* Name.Variable.Instance */
.highlight .vm { color: #369 } /* Name.Variable.Magic */
.highlight .il { color: #00D; font-weight: bold } /* Literal.Number.Integer.Long */
//=============================================================================
//
//   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-2010 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 option) 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 "KviModule.h"
#include "KviSharedFilesManager.h"
#include "KviIrcMask.h"
#include "KviFileUtils.h"
#include "KviLocale.h"
#include "kvi_out.h"
#include "KviControlCodes.h"
#include "KviWindow.h"
#include "KviMainWindow.h"
#include "KviPointerHashTable.h"

#include <time.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_mask]
	@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_mask] is specified, the access to the file is limited
		to the users that match this mask, otherwise the mask 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 other 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)
{
	QString szFileName, szUserMask;
	KVSM_PARAMETERS_BEGIN(c)
	KVSM_PARAMETER("filename", KVS_PT_NONEMPTYSTRING, 0, szFileName)
	KVSM_PARAMETER("user_mask", 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", "sharedfileswindow"), &szFileName);
		return true;
	}

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

	QString szm;
	u.mask(szm);

	kvs_int_t timeout = 0;

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

	QString szVisibleName = szFileName;
	KviQString::cutToLast(szVisibleName, '/');

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

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

	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 mask:string> [filesize:integer]
	@description:
		Removes the shared file that matches <visible name> and <user mask>.
		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)
{
	QString szVisibleName, szUserMask;
	kvs_uint_t uSize;
	KVSM_PARAMETERS_BEGIN(c)
	KVSM_PARAMETER("visible_name", KVS_PT_NONEMPTYSTRING, 0, szVisibleName)
	KVSM_PARAMETER("user_mask", 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 mask '%Q'", "sharedfileswindow"), &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 *)
{
	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<QString, 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",
			    KviControlCodes::Bold, idx + 1, it.currentKey().toUtf8().data());
			c->window()->output(KVI_OUT_NONE, __tr2qs_ctx("File: %s (%u bytes)", "sharedfileswindow"),
			    o->absFilePath().toUtf8().data(), o->fileSize());
			c->window()->output(KVI_OUT_NONE, __tr2qs_ctx("Mask: %s", "sharedfileswindow"),
			    o->userMask().toUtf8().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", "sharedfileswindow"),
				    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", "sharedfileswindow"));
	else
		c->window()->output(KVI_OUT_NONE, __tr2qs_ctx("Total: %d sharedfile", "sharedfileswindow"), 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 *)
{
	return true;
}

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

KVIRC_MODULE(
    "SharedFiles",                                                       // module name
    "4.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,
    "sharedfileswindow")