foxygit / Torlinkc Log in
commits tags

/include/torlinkc/sources/magnet.hpp · 1.14 KB

raw
#pragma once

#include <optional>
#include <string>
#include <vector>

namespace torlinkc {

// extraTrackers come first and win on duplicates: a torrent that carries its
// own announce list means that list, and the public defaults are only a
// fallback. Ported from sources/magnet.ts.
std::string buildMagnet(const std::string& infoHash, const std::string& name,
                         const std::vector<std::string>& extraTrackers = {});

std::string normalizeInfoHash(const std::string& raw);

struct ParsedMagnet {
  std::string infoHash;
  std::string name;
  std::string magnet;
};

std::optional<ParsedMagnet> parseMagnet(const std::string& input);

// Anchored to the whole input so an ordinary search query is never mistaken
// for a hash: only a string that is *nothing but* a 40-char hex or 32-char
// base32 info hash counts.
bool isInfoHash(const std::string& input);

// Accepts either a magnet URI or a bare info hash. A bare hash is normalized
// and wrapped with the default public trackers via buildMagnet. Returns
// nullopt for anything that is neither.
std::optional<ParsedMagnet> parseInput(const std::string& input);

}  // namespace torlinkc