commit db592a67adec87193339d4c9c4118945b197a906 Author: tastytea Date: Fri May 11 02:21:44 2018 +0200 Expanding URLs on the commandline works diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..37c842e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required (VERSION 3.7) +project (expandurl-mastodon + VERSION 0.0.0 + LANGUAGES CXX +) + +include(GNUInstallDirs) +include(FindCURL) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +include_directories(${PROJECT_SOURCE_DIR}/src) +include_directories(${PROJECT_BINARY_DIR}) + +# Write version in header +configure_file ( + "${PROJECT_SOURCE_DIR}/src/version.hpp.in" + "${PROJECT_BINARY_DIR}/version.hpp" +) + +find_package(CURL REQUIRED) +include_directories(${CURL_INCLUDE_DIR}) + +file(GLOB sources src/*.cpp) +add_executable(expandurl-mastodon ${sources}) +target_link_libraries(expandurl-mastodon mastodon-cpp curl curlpp) +install(TARGETS expandurl-mastodon DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/README.md b/README.md new file mode 100644 index 0000000..bbafc09 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +**expandurl-mastodon** is a Mastodon bot that expands a shortened URL. + + +# Install + +## Dependencies + + * Tested OS: Linux + * C++ compiler (tested: gcc 6.4, clang 5.0) + * [cmake](https://cmake.org/) (tested: 3.9.6) + * [curlpp](http://www.curlpp.org/) (tested: 0.8.4) + * [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.12.0) + +## Get sourcecode + +### Development version + + git clone https://github.com/tastytea/expandurl-mastodon.git + +## Compile + + mkdir build + cd build/ + cmake .. + make + +Install with `make install`. + +# Usage + +## Error codes + +Same as [mastodon-cpp](https://github.com/tastytea/mastodon-cpp/blob/master/README.md#error-codes) + +# Copyright + + Copyright © 2018 tastytea . + License GPLv3: GNU GPL version 3 . + This program comes with ABSOLUTELY NO WARRANTY. This is free software, + and you are welcome to redistribute it under certain conditions. diff --git a/src/expand.cpp b/src/expand.cpp new file mode 100644 index 0000000..d134ef5 --- /dev/null +++ b/src/expand.cpp @@ -0,0 +1,54 @@ +/* This file is part of expandurl-mastodon. + * Copyright © 2018 tastytea + * + * 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, version 3. + * + * 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, see . + */ + +#include +#include +#include +#include +#include +#include "version.hpp" +#include "expandurl-mastodon.hpp" + +using std::cerr; +using std::string; +namespace curlopts = curlpp::options; + +const string expand(const string &url) +{ + curlpp::Easy request; + + request.setOpt("HEAD"); + request.setOpt(url); + request.setOpt + (static_cast("expandurl-mastodon/") + global::version); + request.setOpt( + { + "Connection: close", + }); + request.setOpt(true); + + try + { + request.perform(); + } + catch (const std::exception &e) + { + cerr << "ERROR: " << e.what() << '\n'; + cerr << "The previous error is ignored.\n"; + } + + return curlpp::infos::EffectiveUrl::get(request); +} diff --git a/src/expandurl-mastodon.hpp b/src/expandurl-mastodon.hpp new file mode 100644 index 0000000..5bbeb15 --- /dev/null +++ b/src/expandurl-mastodon.hpp @@ -0,0 +1,26 @@ +/* This file is part of expandurl-mastodon. + * Copyright © 2018 tastytea + * + * 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, version 3. + * + * 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, see . + */ + +#ifndef EXPANDURL_MASTODON_HPP +#define EXPANDURL_MASTODON_HPP + +#include + +using std::string; + +const string expand(const string &url); + +#endif // EXPANDURL_MASTODON_HPP diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..a4e182b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,34 @@ +/* This file is part of expandurl-mastodon. + * Copyright © 2018 tastytea + * + * 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, version 3. + * + * 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, see . + */ + +#include +#include +#include +#include "expandurl-mastodon.hpp" + +using std::cout; +using std::string; + +int main(int argc, char *argv[]) +{ + curlpp::initialize(); + + cout << expand(argv[1]) << '\n'; + + curlpp::Cleanup(); + + return 0; +} diff --git a/src/version.hpp.in b/src/version.hpp.in new file mode 100644 index 0000000..cec7915 --- /dev/null +++ b/src/version.hpp.in @@ -0,0 +1,9 @@ +#ifndef VERSION_HPP +#define VERSION_HPP + +namespace global +{ + static constexpr char version[] = "@PROJECT_VERSION@"; +} + +#endif // VERSION_HPP