Skip to content

Instantly share code, notes, and snippets.

@EspressoCake
Created September 19, 2023 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EspressoCake/7c22aa939bd9785c9951e1a79b543205 to your computer and use it in GitHub Desktop.
Save EspressoCake/7c22aa939bd9785c9951e1a79b543205 to your computer and use it in GitHub Desktop.
# Author: Justin Lucas
# Date: September 19, 2023
# Info: A script to colleagues' files starting at the current directory it is running
# Where found, it will attempt to receive the file as a redirected byte stream
beacon_command_register("peerseek", "Seek a file from your peers to grab instantly.", "peerseek FILENAME");
alias peerseek
{
generateRequestEvent(@_);
}
sub generateRequestEvent
{
local('%data');
if (size(@_) == 1)
{
%data = %(filename => @_[0][1], beaconid => @_[0][0]);
#custom_event('requestfiles', %data);
blog(@_[0][0], "Initiating search for: " . %data['filename']);
custom_event('requestfiles', %data);
}
}
command generateRequestEvent
{
local('%data');
if (size(@_) == 1)
{
%data = %(filename => $1);
custom_event('requestfiles', %data);
}
}
on custom_event_requestfiles
{
# Do not acknowledge it if originating from ourselves
if (mynick() !ismatch $1)
{
local('$currentPath');
$currentPath = script_resource("/");
local('@filesToIterate');
@filesToIterate = `find $currentPath -type f -name $2['filename']`;
if (size(@filesToIterate) >= 1)
{
# Take only the first target in a depth-first search
local('$fileNameToSend');
$fileNameToSend = @filesToIterate[0];
local('$handle $fileData');
$handle = openf($fileNameToSend);
$fileData = readb($handle, -1);
closef($handle);
if ('beaconid' in keys($2))
{
blog($2['beaconid'], mynick() . ": I have the file you're looking for " . $1 . "!");
custom_event_private($1, "sendfile", %(filename => $2['filename'], filedata => $fileData, beaconid => $2['beaconid']));
}
else
{
custom_event_private($1, "sendfile", %(filename => $2['filename'], filedata => $fileData));
}
}
}
}
on custom_event_sendfile
{
if (@_[0] !ismatch mynick())
{
local('$currentPath');
$currentPath = script_resource("/");
local('$outputFilePath');
$outputFilePath = $currentPath . $2['filename'];
local('$outputHandle');
println($outputFilePath);
$outputHandle = openf(">" . $outputFilePath);
writeb($outputHandle, $2['filedata']);
closef($outputHandle);
if ('beaconid' in keys($2))
{
blog($2['beaconid'], mynick() . ": I am done receiving the file from " . @_[0]. " (" . $outputFilePath . ").");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment