Compare commits
52 Commits
Author | SHA1 | Date |
---|---|---|
tastytea | c04a1b8926 | |
tastytea | 7baaae1abc | |
tastytea | 64f78e0300 | |
tastytea | a4988d72f2 | |
tastytea | 087ae68136 | |
tastytea | 217a4bb7f0 | |
tastytea | 1ba5d4e444 | |
tastytea | 32fbdf7375 | |
tastytea | d938959598 | |
tastytea | 0f471e860f | |
tastytea | cade0f8a4a | |
tastytea | 3808a89a13 | |
tastytea | 3a44361bf7 | |
tastytea | 73ddaf2776 | |
tastytea | b744fb0903 | |
tastytea | f42bff3918 | |
tastytea | 52604d536a | |
tastytea | 23ad1917f9 | |
tastytea | 07629de436 | |
tastytea | 8c91751fbe | |
tastytea | a25672c039 | |
tastytea | c4b4522dc7 | |
tastytea | 0b218fd472 | |
tastytea | 46f7ddc575 | |
tastytea | c602278dc5 | |
tastytea | ef7067a169 | |
tastytea | 764e84cefd | |
tastytea | 209743fd55 | |
tastytea | f70b5f1a82 | |
tastytea | cfec84d455 | |
tastytea | f5a8f04d73 | |
tastytea | 42d1898ce3 | |
tastytea | bf5fffca3a | |
tastytea | a6f37098f2 | |
tastytea | 6463f8ef1f | |
tastytea | 5d34c938e1 | |
tastytea | b147b42b79 | |
tastytea | bf608f5145 | |
tastytea | e528cf6baa | |
tastytea | 46aa2b89c3 | |
tastytea | f3db10dc0a | |
tastytea | a1ef65b71c | |
tastytea | 50d3ab6bba | |
tastytea | 2c955a2a18 | |
tastytea | 7938cfb30b | |
tastytea | 6dbddb6df2 | |
tastytea | 992f6373ab | |
tastytea | 0a380dbab4 | |
tastytea | 1273f72e4b | |
tastytea | ed537c850f | |
tastytea | 575fb5b4e2 | |
tastytea | 95c9eea3df |
|
@ -0,0 +1,87 @@
|
||||||
|
pipeline:
|
||||||
|
download:
|
||||||
|
image: plugins/download
|
||||||
|
pull: true
|
||||||
|
source: https://schlomp.space/tastytea/mastodon-cpp/releases/download/0.106.0/libmastodon-cpp_0.106.0-0_amd64.deb
|
||||||
|
destination: mastodon-cpp.deb
|
||||||
|
|
||||||
|
gcc6:
|
||||||
|
image: debian:stretch-slim
|
||||||
|
pull: true
|
||||||
|
environment:
|
||||||
|
- LANG=C.utf8
|
||||||
|
- CXX=g++-6
|
||||||
|
- CXXFLAGS=-pipe -O2
|
||||||
|
commands:
|
||||||
|
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
|
||||||
|
- echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
|
||||||
|
- apt-get update -q
|
||||||
|
- apt-get install -qy build-essential cmake pkg-config
|
||||||
|
- apt-get install -qy libjsoncpp-dev libcurl4-openssl-dev libxdg-basedir-dev asciidoc
|
||||||
|
- apt-get install -qy -t sid libcurlpp-dev
|
||||||
|
- dpkg -i mastodon-cpp.deb
|
||||||
|
- rm -rf build && mkdir -p build && cd build
|
||||||
|
- cmake ..
|
||||||
|
- make VERBOSE=1
|
||||||
|
- make install DESTDIR=install
|
||||||
|
|
||||||
|
gcc7:
|
||||||
|
image: debian:stretch-slim
|
||||||
|
pull: true
|
||||||
|
environment:
|
||||||
|
- LANG=C.utf8
|
||||||
|
- CXX=g++-7
|
||||||
|
- CXXFLAGS=-pipe -O2
|
||||||
|
commands:
|
||||||
|
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
|
||||||
|
- echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
|
||||||
|
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
|
||||||
|
- apt-get update -q
|
||||||
|
- apt-get install -qy gnupg
|
||||||
|
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
|
||||||
|
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
|
||||||
|
- apt-get update -q
|
||||||
|
- apt-get install -qy build-essential cmake pkg-config
|
||||||
|
- apt-get install -qy -t xenial g++-7
|
||||||
|
- apt-get install -qy libjsoncpp-dev libcurl4-openssl-dev libxdg-basedir-dev asciidoc
|
||||||
|
- apt-get install -qy -t sid libcurlpp-dev
|
||||||
|
- dpkg -i mastodon-cpp.deb
|
||||||
|
- rm -rf build && mkdir -p build && cd build
|
||||||
|
- cmake ..
|
||||||
|
- make VERBOSE=1
|
||||||
|
- make install DESTDIR=install
|
||||||
|
|
||||||
|
gcc8:
|
||||||
|
image: debian:stretch-slim
|
||||||
|
pull: true
|
||||||
|
environment:
|
||||||
|
- LANG=C.utf8
|
||||||
|
- CXX=g++-8
|
||||||
|
- CXXFLAGS=-pipe -O2
|
||||||
|
commands:
|
||||||
|
- echo "APT::Default-Release \"stretch\";" >> /etc/apt/apt.conf.d/00default_release
|
||||||
|
- echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list.d/sid.list
|
||||||
|
- echo "deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main" >> /etc/apt/sources.list.d/ubuntu-toolchain-r.list
|
||||||
|
- apt-get update -q
|
||||||
|
- apt-get install -qy gnupg
|
||||||
|
- gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x60c317803a41ba51845e371a1e9377a2ba9ef27f
|
||||||
|
- gpg --armor --export 0x60c317803a41ba51845e371a1e9377a2ba9ef27f | apt-key add -
|
||||||
|
- apt-get update -q
|
||||||
|
- apt-get install -qy build-essential cmake pkg-config
|
||||||
|
- apt-get install -qy -t xenial g++-8
|
||||||
|
- apt-get install -qy libjsoncpp-dev libcurl4-openssl-dev libxdg-basedir-dev asciidoc
|
||||||
|
- apt-get install -qy -t sid libcurlpp-dev
|
||||||
|
- dpkg -i mastodon-cpp.deb
|
||||||
|
- rm -rf build && mkdir -p build && cd build
|
||||||
|
- cmake ..
|
||||||
|
- make VERBOSE=1
|
||||||
|
- make install DESTDIR=install
|
||||||
|
|
||||||
|
notify:
|
||||||
|
image: drillster/drone-email
|
||||||
|
pull: true
|
||||||
|
host: cryptoparty-celle.de
|
||||||
|
secrets: [ email_username, email_password ]
|
||||||
|
from: drone@tzend.de
|
||||||
|
when:
|
||||||
|
status: [ changed, failure ]
|
|
@ -1,46 +1,58 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project (expandurl-mastodon
|
project (expandurl-mastodon
|
||||||
VERSION 0.6.4
|
VERSION 0.9.14
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
find_package(CURL REQUIRED)
|
find_package(CURL REQUIRED)
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
pkg_check_modules(CURLPP REQUIRED curlpp)
|
pkg_check_modules(CURLPP REQUIRED curlpp)
|
||||||
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
|
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
|
||||||
|
pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
|
set(CMAKE_CXX_FLAGS_DEBUG
|
||||||
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
|
"${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -Wpedantic -ftrapv \
|
||||||
# uint_fast16_t can be bigger than 16 bit, but that doesn't matter because
|
-fsanitize=undefined -g -Og -fno-omit-frame-pointer")
|
||||||
# everything but the last 16 bit is padded with zeroes.
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
include_directories(${PROJECT_SOURCE_DIR}/src)
|
|
||||||
include_directories(${PROJECT_BINARY_DIR})
|
include_directories(${PROJECT_BINARY_DIR})
|
||||||
|
|
||||||
include_directories(${CURL_INCLUDE_DIRS})
|
include_directories(${CURL_INCLUDE_DIRS})
|
||||||
include_directories(${CURLPP_INCLUDE_DIRS})
|
include_directories(${CURLPP_INCLUDE_DIRS})
|
||||||
include_directories(${JSONCPP_INCLUDE_DIRS})
|
include_directories(${JSONCPP_INCLUDE_DIRS})
|
||||||
|
include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
|
||||||
|
|
||||||
link_directories(${CURL_LIBRARY_DIRS})
|
link_directories(${CURL_LIBRARY_DIRS})
|
||||||
link_directories(${CURLPP_LIBRARY_DIRS})
|
link_directories(${CURLPP_LIBRARY_DIRS})
|
||||||
link_directories(${JSONCPP_LIBRARY_DIRS})
|
link_directories(${JSONCPP_LIBRARY_DIRS})
|
||||||
|
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
|
||||||
|
|
||||||
# Write version in header
|
# Write version in header
|
||||||
configure_file (
|
configure_file (
|
||||||
"${PROJECT_SOURCE_DIR}/src/version.hpp.in"
|
"${PROJECT_SOURCE_DIR}/src/version.hpp.in"
|
||||||
"${PROJECT_BINARY_DIR}/version.hpp"
|
"${PROJECT_BINARY_DIR}/version.hpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
file(GLOB sources src/*.cpp)
|
file(GLOB sources src/*.cpp)
|
||||||
add_executable(expandurl-mastodon ${sources})
|
add_executable(expandurl-mastodon ${sources})
|
||||||
target_link_libraries(expandurl-mastodon
|
target_link_libraries(expandurl-mastodon
|
||||||
${CURLPP_LIBRARIES} ${JSONCPP_LIBRARIES}
|
${CURLPP_LIBRARIES} ${JSONCPP_LIBRARIES} ${LIBXDG_BASEDIR_LIBRARIES}
|
||||||
mastodon-cpp pthread)
|
mastodon-cpp pthread stdc++fs)
|
||||||
install(TARGETS expandurl-mastodon DESTINATION ${CMAKE_INSTALL_BINDIR})
|
install(TARGETS expandurl-mastodon DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||||
|
|
||||||
|
set(WITH_MAN "YES" CACHE STRING "WITH_MAN defaults to \"YES\"")
|
||||||
|
if (WITH_MAN)
|
||||||
|
add_custom_command(OUTPUT "${PROJECT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.1"
|
||||||
|
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
|
||||||
|
DEPENDS "${CMAKE_SOURCE_DIR}/${CMAKE_PROJECT_NAME}.1.adoc"
|
||||||
|
COMMAND ${CMAKE_SOURCE_DIR}/build_manpage.sh
|
||||||
|
ARGS ${PROJECT_VERSION})
|
||||||
|
add_custom_target(run ALL
|
||||||
|
DEPENDS "${PROJECT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.1")
|
||||||
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.1
|
||||||
|
DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
|
||||||
|
endif()
|
||||||
|
|
65
README.md
65
README.md
|
@ -3,7 +3,7 @@
|
||||||
If you want the bot to expand an URL, reply to the post with the URL in it and
|
If you want the bot to expand an URL, reply to the post with the URL in it and
|
||||||
mention the bot account (`@expandurl@botsin.space` for example).
|
mention the bot account (`@expandurl@botsin.space` for example).
|
||||||
|
|
||||||
![Example screenshot](https://user-images.githubusercontent.com/3681516/39963736-908e3eea-5663-11e8-9a9c-55ca74279235.jpg)
|
![Example screenshot](https://doc.schlomp.space/expandurl-mastodon/expandurl_screenshot.jpg)
|
||||||
|
|
||||||
This bot uses the same visibility as you, but posts unlisted instead of public.
|
This bot uses the same visibility as you, but posts unlisted instead of public.
|
||||||
It retains the sensitive flag and spoiler warnings.
|
It retains the sensitive flag and spoiler warnings.
|
||||||
|
@ -14,60 +14,59 @@ to rewrite [AMP](https://en.wikipedia.org/wiki/Accelerated_Mobile_Pages) URLs to
|
||||||
point at the real webpages.
|
point at the real webpages.
|
||||||
|
|
||||||
Please report any bugs via the
|
Please report any bugs via the
|
||||||
[issue tracker on GitHub](https://github.com/tastytea/expandurl-mastodon/issues)
|
[issue tracker on schlomp.space](https://schlomp.space/tastytea/expandurl-mastodon/issues)
|
||||||
or to [@tastytea@soc.ialis.me](https://soc.ialis.me/@tastytea).
|
or to [@tastytea@soc.ialis.me](https://soc.ialis.me/@tastytea).
|
||||||
|
|
||||||
# Install
|
# Install
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
* Tested OS: Linux
|
* Tested OS: Linux
|
||||||
* C++ compiler (tested: gcc 6.4)
|
* C++ compiler (tested: gcc 6/7/8)
|
||||||
* [cmake](https://cmake.org/) (tested: 3.9.6)
|
* [cmake](https://cmake.org/) (tested: 3.9 / 3.11)
|
||||||
* [curlpp](http://www.curlpp.org/) (tested: 0.8.1)
|
* [curlpp](http://www.curlpp.org/) (tested: 0.8)
|
||||||
* [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.13.1)
|
* [mastodon-cpp](https://schlomp.space/tastytea/mastodon-cpp) (at least: 0.106)
|
||||||
* [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.4)
|
* [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8 / 1.7)
|
||||||
|
* [libxdg-basedir](http://repo.or.cz/w/libxdg-basedir.git) (tested: 1.2)
|
||||||
|
* Optional:
|
||||||
|
* Manpage: [asciidoc](http://asciidoc.org/) (tested: 8.6)
|
||||||
|
|
||||||
## Get sourcecode
|
## Get sourcecode
|
||||||
|
|
||||||
### Latest release
|
### Latest release
|
||||||
|
|
||||||
https://github.com/tastytea/expandurl-mastodon/releases/latest
|
https://schlomp.space/tastytea/expandurl-mastodon/releases
|
||||||
|
|
||||||
### Development version
|
### Development version
|
||||||
|
|
||||||
git clone https://github.com/tastytea/expandurl-mastodon.git
|
```SH
|
||||||
|
git clone https://schlomp.space/tastytea/expandurl-mastodon.git
|
||||||
|
```
|
||||||
|
|
||||||
## Compile
|
## Compile
|
||||||
|
|
||||||
mkdir build
|
```SH
|
||||||
cd build/
|
mkdir build
|
||||||
cmake ..
|
cd build/
|
||||||
make
|
cmake ..
|
||||||
|
make
|
||||||
|
```
|
||||||
|
|
||||||
|
cmake options:
|
||||||
|
* `-DCMAKE_BUILD_TYPE=Debug` for a debug build
|
||||||
|
* `-DWITH_MAN=NO` to not compile the manpage
|
||||||
|
|
||||||
Install with `make install`.
|
Install with `make install`.
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
**The config file has changed from cfg to JSON in 0.4.0.**
|
Have a look at the [manpage](https://schlomp.space/tastytea/expandurl-mastodon/src/branch/master/expandurl-mastodon.1.adoc).
|
||||||
|
|
||||||
Start expandurl-mastodon without parameters.
|
|
||||||
|
|
||||||
If no config file is found, you will be asked to provide your account address
|
|
||||||
and an access token is generated. The config file can be found in
|
|
||||||
`${HOME}/.config/expandurl-mastodon.json` and looks like this:
|
|
||||||
|
|
||||||
{
|
|
||||||
"account": "expandurl@example.social",
|
|
||||||
"access_token": "abc123"
|
|
||||||
}
|
|
||||||
|
|
||||||
After the configuration file is generated, you can start expandurl-mastodon as
|
|
||||||
daemon.
|
|
||||||
|
|
||||||
# Copyright
|
# Copyright
|
||||||
|
|
||||||
Copyright © 2018 tastytea <tastytea@tastytea.de>.
|
```PLAIN
|
||||||
License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.
|
Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>.
|
||||||
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
|
License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.
|
||||||
and you are welcome to redistribute it under certain conditions.
|
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
|
||||||
|
and you are welcome to redistribute it under certain conditions.
|
||||||
|
```
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
name="expandurl-mastodon"
|
||||||
|
|
||||||
|
if [ -n "${1}" ]; then
|
||||||
|
dir="$(dirname ${0})"
|
||||||
|
version="${1}"
|
||||||
|
cp -vf "${dir}/${name}.1.adoc" .
|
||||||
|
sed -Ei "s/(Revision: +)[0-9]+\.[0-9]\.[0-9]/\1${version}/" "${name}.1.adoc"
|
||||||
|
a2x --doctype manpage --format manpage --no-xmllint "${name}.1.adoc"
|
||||||
|
else
|
||||||
|
echo "usage: ${0} VERSION" >&2
|
||||||
|
fi
|
|
@ -0,0 +1,75 @@
|
||||||
|
= expandurl-mastodon(1)
|
||||||
|
:Author: tastytea
|
||||||
|
:Email: tastytea@tastytea.de
|
||||||
|
:Date: 2019-04-12
|
||||||
|
:Revision: 0.0.0
|
||||||
|
:man source: expandurl-mastodon
|
||||||
|
:man version: {revision}
|
||||||
|
:man manual: General Commands Manual
|
||||||
|
|
||||||
|
== NAME
|
||||||
|
|
||||||
|
expandurl-mastodon - Mastodon bot that expands shortened URLs.
|
||||||
|
|
||||||
|
== SYNOPSIS
|
||||||
|
|
||||||
|
*expandurl-mastodon*
|
||||||
|
|
||||||
|
== DESCRIPTION
|
||||||
|
|
||||||
|
If you want the bot to expand an URL, reply to the post with the URL in it and
|
||||||
|
mention the bot account.
|
||||||
|
|
||||||
|
This bot uses the same visibility as you, but posts unlisted instead of public.
|
||||||
|
It retains the sensitive flag and spoiler warnings.
|
||||||
|
|
||||||
|
Some tracking parameters, like those beginning with
|
||||||
|
https://en.wikipedia.org/wiki/UTM_parameters[utm_] are stripped. It also tries
|
||||||
|
to rewrite https://en.wikipedia.org/wiki/Accelerated_Mobile_Pages[AMP] URLs to
|
||||||
|
point at the real webpages.
|
||||||
|
|
||||||
|
== CONFIGURATION
|
||||||
|
|
||||||
|
If no config file is found, you will be asked to provide your account address
|
||||||
|
and an access token is generated. The config file can be found in
|
||||||
|
`${XDG_CONFIG_HOME}/expandurl-mastodon.json` and looks like this:
|
||||||
|
|
||||||
|
[source,json]
|
||||||
|
----
|
||||||
|
{
|
||||||
|
"account": "expandurl@example.social",
|
||||||
|
"access_token": "abc123",
|
||||||
|
"proxy":
|
||||||
|
{
|
||||||
|
"url": "socks5h://[::1]:1080/",
|
||||||
|
"user": "user23",
|
||||||
|
"password": "supersecure"
|
||||||
|
},
|
||||||
|
"replace" :
|
||||||
|
{
|
||||||
|
"//amp\\." : "//",
|
||||||
|
"[\\?&]__twitter_impression=[^&]+" : "",
|
||||||
|
"[\\?&]utm_[^&]+" : "",
|
||||||
|
"[\\?&]wt_zmc=[^&]+" : "",
|
||||||
|
"[\\?&]wtmc=[^&]+" : ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to use a proxy or define your own replacements, you have to edit the
|
||||||
|
configuration file manually. After the configuration file is generated, you can
|
||||||
|
start expandurl-mastodon as daemon.
|
||||||
|
|
||||||
|
== FILES
|
||||||
|
|
||||||
|
- *Configuration file*: `${XDG_CONFIG_HOME}/expandurl-mastodon.json`
|
||||||
|
|
||||||
|
`${XDG_CONFIG_HOME}` is usually `~/.config`.
|
||||||
|
|
||||||
|
== REPORTING BUGS
|
||||||
|
|
||||||
|
Bugtracker: https://schlomp.space/tastytea/expandurl-mastodon/issues
|
||||||
|
|
||||||
|
E-mail: tastytea@tastytea.de
|
||||||
|
|
||||||
|
Fediverse: https://soc.ialis.me/@tastytea
|
|
@ -6,14 +6,15 @@ EAPI=6
|
||||||
inherit git-r3 cmake-utils
|
inherit git-r3 cmake-utils
|
||||||
|
|
||||||
DESCRIPTION="Mastodon bot that expands shortened URLs."
|
DESCRIPTION="Mastodon bot that expands shortened URLs."
|
||||||
HOMEPAGE="https://github.com/tastytea/expandurl-mastodon"
|
HOMEPAGE="https://git.schlomp.space/tastytea/expandurl-mastodon"
|
||||||
EGIT_REPO_URI="https://github.com/tastytea/expandurl-mastodon.git"
|
EGIT_REPO_URI="https://git.schlomp.space/tastytea/expandurl-mastodon.git"
|
||||||
LICENSE="GPL-3"
|
LICENSE="GPL-3"
|
||||||
SLOT="0"
|
SLOT="0"
|
||||||
KEYWORDS=""
|
KEYWORDS=""
|
||||||
IUSE=""
|
IUSE=""
|
||||||
RDEPEND=">=dev-cpp/mastodon-cpp-9999
|
RDEPEND=">=dev-cpp/mastodon-cpp-9999
|
||||||
>=dev-cpp/curlpp-0.8.1"
|
>=dev-cpp/curlpp-0.8.1
|
||||||
|
>=dev-libs/libxdg-basedir-1.2.0-r1"
|
||||||
DEPEND=">=dev-util/cmake-3.9.6
|
DEPEND=">=dev-util/cmake-3.9.6
|
||||||
${RDEPEND}"
|
${RDEPEND}"
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
/* This file is part of expandurl-mastodon.
|
||||||
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <experimental/filesystem>
|
||||||
|
#include <basedir.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include "configjson.hpp"
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
namespace fs = std::experimental::filesystem;
|
||||||
|
|
||||||
|
ConfigJSON::ConfigJSON(const string &filename, const string &subdir)
|
||||||
|
: _json()
|
||||||
|
{
|
||||||
|
xdgHandle xdg;
|
||||||
|
xdgInitHandle(&xdg);
|
||||||
|
_filepath = xdgConfigHome(&xdg);
|
||||||
|
xdgWipeHandle(&xdg);
|
||||||
|
|
||||||
|
if (!subdir.empty())
|
||||||
|
{
|
||||||
|
_filepath += '/' + subdir;
|
||||||
|
if (!fs::exists(_filepath))
|
||||||
|
{
|
||||||
|
fs::create_directory(_filepath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_filepath += '/' + filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConfigJSON::read()
|
||||||
|
{
|
||||||
|
std::ifstream file(_filepath);
|
||||||
|
if (file.is_open())
|
||||||
|
{
|
||||||
|
std::stringstream config;
|
||||||
|
config << file.rdbuf();
|
||||||
|
file.close();
|
||||||
|
config >> _json;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConfigJSON::write()
|
||||||
|
{
|
||||||
|
std::ofstream file(_filepath);
|
||||||
|
if (file.is_open())
|
||||||
|
{
|
||||||
|
const string config = _json.toStyledString();
|
||||||
|
file.write(config.c_str(), config.length());
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Json::Value &ConfigJSON::get_json()
|
||||||
|
{
|
||||||
|
return _json;
|
||||||
|
}
|
||||||
|
|
||||||
|
const string ConfigJSON::get_filepath() const
|
||||||
|
{
|
||||||
|
return _filepath;
|
||||||
|
}
|
|
@ -0,0 +1,82 @@
|
||||||
|
/* This file is part of expandurl-mastodon.
|
||||||
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CONFIGJSON_HPP
|
||||||
|
#define CONFIGJSON_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <jsoncpp/json/json.h>
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
class ConfigJSON
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/*!
|
||||||
|
* @brief Checks if subdir is present, creates it if necessary
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* @code
|
||||||
|
* ConfigJSON config("test.json", "subdirectory");
|
||||||
|
* @endcode
|
||||||
|
*
|
||||||
|
* @param filename The name of the file, including extension
|
||||||
|
* @param subdir The subdir (optional)
|
||||||
|
*/
|
||||||
|
explicit ConfigJSON(const string &filename, const string &subdir = "");
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Read the file
|
||||||
|
*
|
||||||
|
* @return `true` on success
|
||||||
|
*/
|
||||||
|
bool read();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Write the file
|
||||||
|
*
|
||||||
|
* @return `true` on success
|
||||||
|
*/
|
||||||
|
bool write();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Returns a reference to the config as Json::Value
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* @code
|
||||||
|
* Json::Value &json = config.get_json();
|
||||||
|
* @endcode
|
||||||
|
*/
|
||||||
|
Json::Value &get_json();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Gets the complete filepath
|
||||||
|
*/
|
||||||
|
const string get_filepath() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/*!
|
||||||
|
* Holds the contents of the JSON file
|
||||||
|
*/
|
||||||
|
Json::Value _json;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Complete filepath
|
||||||
|
*/
|
||||||
|
string _filepath;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CONFIGJSON_HPP
|
|
@ -1,5 +1,5 @@
|
||||||
/* This file is part of expandurl-mastodon.
|
/* This file is part of expandurl-mastodon.
|
||||||
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -25,14 +25,17 @@
|
||||||
#include <mastodon-cpp/mastodon-cpp.hpp>
|
#include <mastodon-cpp/mastodon-cpp.hpp>
|
||||||
#include <mastodon-cpp/easy/all.hpp>
|
#include <mastodon-cpp/easy/all.hpp>
|
||||||
#include <jsoncpp/json/json.h>
|
#include <jsoncpp/json/json.h>
|
||||||
|
#include "configjson.hpp"
|
||||||
|
|
||||||
|
using namespace Mastodon;
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using Mastodon::API;
|
using Mastodon::API;
|
||||||
using Mastodon::Easy;
|
|
||||||
|
extern ConfigJSON configfile;
|
||||||
|
|
||||||
void signal_handler(int signum);
|
void signal_handler(int signum);
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief Extract URLs from HTML
|
* @brief Extract URLs from HTML
|
||||||
*
|
*
|
||||||
|
@ -60,6 +63,15 @@ const string expand(const string &url);
|
||||||
*/
|
*/
|
||||||
const string strip(const string &url);
|
const string strip(const string &url);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief Initialize replacements for URLs
|
||||||
|
*
|
||||||
|
* If no replacements are found in the config file, a default list is
|
||||||
|
* inserted.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void init_replacements();
|
||||||
|
|
||||||
|
|
||||||
class Listener
|
class Listener
|
||||||
{
|
{
|
||||||
|
@ -70,34 +82,37 @@ public:
|
||||||
/*!
|
/*!
|
||||||
* @brief Starts listening on Mastodon
|
* @brief Starts listening on Mastodon
|
||||||
*/
|
*/
|
||||||
const void start();
|
void start();
|
||||||
/*!
|
/*!
|
||||||
* @brief Stops listening on Mastodon
|
* @brief Stops listening on Mastodon
|
||||||
*/
|
*/
|
||||||
const void stop();
|
void stop();
|
||||||
|
|
||||||
const std::vector<Easy::Notification> get_new_messages();
|
const std::vector<Easy::Notification> get_new_messages();
|
||||||
const std::vector<Easy::Notification> catchup();
|
const std::vector<Easy::Notification> catchup();
|
||||||
Easy::Status get_status(const std::uint_fast64_t &id);
|
Easy::Status get_status(const string &id);
|
||||||
const bool send_reply(const Easy::Status &to_status, const string &message);
|
bool send_reply(const Easy::Status &to_status, const string &message);
|
||||||
const std::uint_fast64_t get_parent_id(const Easy::Notification ¬if);
|
const string get_parent_id(const Easy::Notification ¬if);
|
||||||
|
|
||||||
const bool stillrunning() const;
|
bool stillrunning() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string _instance;
|
string _instance;
|
||||||
string _access_token;
|
string _access_token;
|
||||||
std::unique_ptr<Easy> _masto;
|
std::unique_ptr<Easy::API> _masto;
|
||||||
string _stream;
|
string _stream;
|
||||||
std::unique_ptr<API::http> _ptr;
|
std::unique_ptr<API::http> _ptr;
|
||||||
std::thread _thread;
|
std::thread _thread;
|
||||||
bool _running;
|
bool _running;
|
||||||
Json::Value _config;
|
string _proxy;
|
||||||
const string _configfilepath;
|
string _proxy_user;
|
||||||
|
string _proxy_password;
|
||||||
|
Json::Value &_config;
|
||||||
|
|
||||||
const bool read_config();
|
void read_config();
|
||||||
const bool write_config();
|
bool write_config();
|
||||||
const bool register_app();
|
bool register_app();
|
||||||
|
void set_proxy(Easy::API &masto);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // EXPANDURL_MASTODON_HPP
|
#endif // EXPANDURL_MASTODON_HPP
|
||||||
|
|
40
src/main.cpp
40
src/main.cpp
|
@ -1,5 +1,5 @@
|
||||||
/* This file is part of expandurl-mastodon.
|
/* This file is part of expandurl-mastodon.
|
||||||
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -18,15 +18,19 @@
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
#include <numeric>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <unistd.h> // getuid()
|
#include <unistd.h> // getuid()
|
||||||
#include <curlpp/cURLpp.hpp>
|
#include <curlpp/cURLpp.hpp>
|
||||||
|
#include "configjson.hpp"
|
||||||
#include "expandurl-mastodon.hpp"
|
#include "expandurl-mastodon.hpp"
|
||||||
|
|
||||||
|
using namespace Mastodon;
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using Mastodon::Easy;
|
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
ConfigJSON configfile("expandurl-mastodon.json");
|
||||||
|
|
||||||
void signal_handler(int signum)
|
void signal_handler(int signum)
|
||||||
{
|
{
|
||||||
|
@ -48,10 +52,18 @@ void signal_handler(int signum)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main()
|
||||||
{
|
{
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
signal(SIGTERM, signal_handler);
|
signal(SIGTERM, signal_handler);
|
||||||
|
|
||||||
|
if (!configfile.read())
|
||||||
|
{
|
||||||
|
syslog(LOG_WARNING, "Could not open %s.",
|
||||||
|
configfile.get_filepath().c_str());
|
||||||
|
}
|
||||||
|
init_replacements();
|
||||||
|
|
||||||
curlpp::initialize();
|
curlpp::initialize();
|
||||||
openlog("expandurl-mastodon", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_LOCAL1);
|
openlog("expandurl-mastodon", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_LOCAL1);
|
||||||
syslog(LOG_NOTICE, "Program started by user %d", getuid());
|
syslog(LOG_NOTICE, "Program started by user %d", getuid());
|
||||||
|
@ -62,13 +74,12 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
while (running)
|
while (running)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||||
if (!listener.stillrunning())
|
if (!listener.stillrunning())
|
||||||
{
|
{
|
||||||
listener.stop();
|
listener.stop();
|
||||||
syslog(LOG_DEBUG, "Reestablishing connection...");
|
syslog(LOG_DEBUG, "Reestablishing connection...");
|
||||||
listener.start();
|
listener.start();
|
||||||
syslog(LOG_NOTICE, "Reestablished connection.");
|
|
||||||
new_messages = listener.catchup();
|
new_messages = listener.catchup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,25 +92,26 @@ int main(int argc, char *argv[])
|
||||||
for (Easy::Notification ¬if : new_messages)
|
for (Easy::Notification ¬if : new_messages)
|
||||||
{
|
{
|
||||||
syslog(LOG_DEBUG, "new message");
|
syslog(LOG_DEBUG, "new message");
|
||||||
const std::uint_fast64_t id = listener.get_parent_id(notif);
|
const string id = listener.get_parent_id(notif);
|
||||||
syslog(LOG_DEBUG, "in_reply_to_id: %lu", id);
|
syslog(LOG_DEBUG, "in_reply_to_id: %s", id.c_str());
|
||||||
Easy::Status status;
|
Easy::Status status;
|
||||||
|
|
||||||
if (id > 0)
|
if (!id.empty())
|
||||||
{
|
{
|
||||||
status = listener.get_status(id);
|
status = listener.get_status(id);
|
||||||
if (status.valid())
|
if (status.valid())
|
||||||
{
|
{
|
||||||
string message = "";
|
const std::vector<string> vec = get_urls(status.content());
|
||||||
for (const string &url : get_urls(status.content()))
|
const string message =
|
||||||
{
|
std::accumulate(vec.begin(), vec.end(), string(),
|
||||||
message += url + " \n";
|
[](const string &s1, const string s2)
|
||||||
}
|
{ return s1 + s2 + " \n"; });
|
||||||
if (!message.empty())
|
if (!message.empty())
|
||||||
{
|
{
|
||||||
if (!listener.send_reply(notif.status(), message))
|
if (!listener.send_reply(notif.status(), message))
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "could not send reply to %lu", id);
|
syslog(LOG_ERR, "could not send reply to %s",
|
||||||
|
id.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
305
src/masto.cpp
305
src/masto.cpp
|
@ -1,5 +1,5 @@
|
||||||
/* This file is part of expandurl-mastodon.
|
/* This file is part of expandurl-mastodon.
|
||||||
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -20,11 +20,13 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
#include <chrono>
|
||||||
#include "version.hpp"
|
#include "version.hpp"
|
||||||
#include "expandurl-mastodon.hpp"
|
#include "expandurl-mastodon.hpp"
|
||||||
|
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
using std::uint8_t;
|
||||||
|
|
||||||
Listener::Listener()
|
Listener::Listener()
|
||||||
: _instance("")
|
: _instance("")
|
||||||
|
@ -32,26 +34,22 @@ Listener::Listener()
|
||||||
, _stream("")
|
, _stream("")
|
||||||
, _ptr(nullptr)
|
, _ptr(nullptr)
|
||||||
, _running(false)
|
, _running(false)
|
||||||
, _configfilepath(static_cast<const string>(getenv("HOME")) +
|
, _proxy("")
|
||||||
"/.config/expandurl-mastodon.json")
|
, _proxy_user("")
|
||||||
|
, _proxy_password("")
|
||||||
|
, _config(configfile.get_json())
|
||||||
{
|
{
|
||||||
|
read_config();
|
||||||
if (read_config())
|
if (_config["access_token"].isNull())
|
||||||
{
|
{
|
||||||
_masto = std::make_unique<Easy>(_instance, _access_token);
|
|
||||||
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
|
|
||||||
global::version);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
syslog(LOG_WARNING, "Could not open %s.", _configfilepath.c_str());
|
|
||||||
syslog(LOG_INFO, "Attempting to register application and write config file.");
|
syslog(LOG_INFO, "Attempting to register application and write config file.");
|
||||||
if (register_app())
|
if (register_app())
|
||||||
{
|
{
|
||||||
syslog(LOG_INFO, "Registration successful.");
|
syslog(LOG_INFO, "Registration successful.");
|
||||||
if (!write_config())
|
if (!configfile.write())
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "Could not write %s.", _configfilepath.c_str());
|
syslog(LOG_ERR, "Could not write %s.",
|
||||||
|
configfile.get_filepath().c_str());
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,77 +59,49 @@ Listener::Listener()
|
||||||
std::exit(2);
|
std::exit(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_masto = std::make_unique<Easy::API>(_instance, _access_token);
|
||||||
|
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
|
||||||
|
global::version);
|
||||||
|
set_proxy(*_masto);
|
||||||
}
|
}
|
||||||
|
|
||||||
Listener::~Listener()
|
Listener::~Listener()
|
||||||
{
|
{
|
||||||
if (!write_config())
|
|
||||||
{
|
|
||||||
syslog(LOG_ERR, "Could not write %s.", _configfilepath.c_str());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool Listener::read_config()
|
void Listener::read_config()
|
||||||
{
|
{
|
||||||
std::ifstream file(_configfilepath);
|
_instance = _config["account"].asString();
|
||||||
|
_instance = _instance.substr(_instance.find('@') + 1);
|
||||||
if (file.is_open())
|
_access_token = _config["access_token"].asString();
|
||||||
{
|
_proxy = _config["proxy"]["url"].asString();
|
||||||
std::stringstream json;
|
_proxy_user = _config["proxy"]["user"].asString();
|
||||||
json << file.rdbuf();
|
_proxy_password = _config["proxy"]["password"].asString();
|
||||||
file.close();
|
|
||||||
json >> _config;
|
|
||||||
|
|
||||||
_instance = _config["account"].asString();
|
|
||||||
_instance = _instance.substr(_instance.find('@') + 1);
|
|
||||||
_access_token = _config["access_token"].asString();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool Listener::write_config()
|
void Listener::start()
|
||||||
{
|
{
|
||||||
std::ofstream outfile(_configfilepath);
|
_running = true;
|
||||||
if (outfile.is_open())
|
_masto->get_stream(API::v1::streaming_user, _ptr, _stream);
|
||||||
{
|
|
||||||
outfile.write(_config.toStyledString().c_str(),
|
|
||||||
_config.toStyledString().length());
|
|
||||||
outfile.close();
|
|
||||||
|
|
||||||
return true;
|
syslog(LOG_NOTICE, "Connecting to %s ...", _instance.c_str());
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const void Listener::start()
|
void Listener::stop()
|
||||||
{
|
{
|
||||||
_thread = std::thread([=]
|
if (!configfile.write())
|
||||||
{
|
{
|
||||||
_running = true;
|
syslog(LOG_ERR, "Could not write %s.",
|
||||||
Easy masto(_instance, _access_token);
|
configfile.get_filepath().c_str());
|
||||||
masto.set_useragent(static_cast<const string>("expandurl-mastodon/") +
|
|
||||||
global::version);
|
|
||||||
masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr);
|
|
||||||
syslog(LOG_DEBUG, "Connection lost.");
|
|
||||||
_running = false;
|
|
||||||
});
|
|
||||||
while (_ptr == nullptr)
|
|
||||||
{
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
||||||
}
|
}
|
||||||
syslog(LOG_NOTICE, "Connected to %s", _instance.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
const void Listener::stop()
|
|
||||||
{
|
|
||||||
if (_ptr)
|
if (_ptr)
|
||||||
{
|
{
|
||||||
_ptr->cancel_stream();
|
_ptr->cancel_stream();
|
||||||
_thread.join();
|
_thread.join();
|
||||||
_ptr.reset();
|
_ptr.reset();
|
||||||
|
_ptr = nullptr;
|
||||||
_stream = "";
|
_stream = "";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -142,35 +112,45 @@ const void Listener::stop()
|
||||||
|
|
||||||
const std::vector<Easy::Notification> Listener::get_new_messages()
|
const std::vector<Easy::Notification> Listener::get_new_messages()
|
||||||
{
|
{
|
||||||
|
using namespace std::chrono;
|
||||||
|
|
||||||
std::vector<Easy::Notification> v;
|
std::vector<Easy::Notification> v;
|
||||||
static std::uint_fast8_t count_empty = 0;
|
static system_clock::time_point lastping = system_clock::now();
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(_ptr->get_mutex());
|
std::lock_guard<std::mutex> lock(_ptr->get_mutex());
|
||||||
if (!_stream.empty())
|
if (!_stream.empty())
|
||||||
{
|
{
|
||||||
const string buffer = _stream;
|
for (const Easy::stream_event_type &event : Easy::parse_stream(_stream))
|
||||||
_stream.clear();
|
|
||||||
|
|
||||||
for (const Easy::stream_event &event : Easy::parse_stream(buffer))
|
|
||||||
{
|
{
|
||||||
if (event.first == Easy::event_type::Notification)
|
if (event.type == Easy::event_type::Notification)
|
||||||
{
|
{
|
||||||
Easy::Notification notif(event.second);
|
Easy::Notification notif(event.data);
|
||||||
if (notif.type() == Easy::notification_type::Mention)
|
if (notif.type() == Easy::notification_type::Mention)
|
||||||
{
|
{
|
||||||
v.push_back(notif);
|
v.push_back(notif);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (event.type == Easy::event_type::Error)
|
||||||
|
{
|
||||||
|
constexpr uint8_t delay_after_error = 120;
|
||||||
|
syslog(LOG_DEBUG, "Connection lost.");
|
||||||
|
const Json::Value err;
|
||||||
|
syslog(LOG_ERR, "Connection terminated: Error %u",
|
||||||
|
err["error_code"].asUInt());
|
||||||
|
syslog(LOG_INFO, "Waiting for %u seconds", delay_after_error);
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(delay_after_error));
|
||||||
|
_running = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
count_empty = 0;
|
_stream.clear();
|
||||||
|
lastping = system_clock::now();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// If we got an empty message 5 times, set state to not running
|
// If the last keep-alive packet was received 25 seconds or more ago
|
||||||
++count_empty;
|
if (duration_cast<seconds>(system_clock::now() - lastping).count() >= 25)
|
||||||
if (count_empty > 5)
|
|
||||||
{
|
{
|
||||||
count_empty = 0;
|
lastping = system_clock::now();
|
||||||
syslog(LOG_NOTICE, "Detected broken connection.");
|
syslog(LOG_NOTICE, "Detected broken connection.");
|
||||||
_running = false;
|
_running = false;
|
||||||
}
|
}
|
||||||
|
@ -186,158 +166,152 @@ const std::vector<Easy::Notification> Listener::catchup()
|
||||||
if (last_id != "")
|
if (last_id != "")
|
||||||
{
|
{
|
||||||
syslog(LOG_DEBUG, "Catching up...");
|
syslog(LOG_DEBUG, "Catching up...");
|
||||||
API::parametermap parameter =
|
parameters parameter =
|
||||||
{
|
{
|
||||||
{ "since_id", { last_id } },
|
{ "since_id", { last_id } },
|
||||||
{ "exclude_types", { "follow", "favourite", "reblog" } }
|
{ "exclude_types", { "follow", "favourite", "reblog" } }
|
||||||
};
|
};
|
||||||
string answer;
|
return_call ret;;
|
||||||
std::uint_fast16_t ret;
|
|
||||||
|
|
||||||
ret = _masto->get(API::v1::notifications, parameter, answer);
|
ret = _masto->get(API::v1::notifications, parameter);
|
||||||
|
|
||||||
if (ret == 0)
|
if (ret)
|
||||||
{
|
{
|
||||||
for (const string str : Easy::json_array_to_vector(answer))
|
for (const string str : Easy::json_array_to_vector(ret.answer))
|
||||||
{
|
{
|
||||||
v.push_back(Easy::Notification(str));
|
v.push_back(Easy::Notification(str));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
syslog(LOG_ERR, "Could not catch up: Error %u", ret.error_code);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id)
|
Mastodon::Easy::Status Listener::get_status(const string &id)
|
||||||
{
|
{
|
||||||
std::uint_fast16_t ret;
|
return_call ret;
|
||||||
string answer;
|
|
||||||
|
|
||||||
ret = _masto->get(API::v1::statuses_id, {{ "id", { std::to_string(id) }}},
|
ret = _masto->get(API::v1::statuses_id, {{ "id", { id }}});
|
||||||
answer);
|
if (ret)
|
||||||
if (ret == 0)
|
|
||||||
{
|
{
|
||||||
return Easy::Status(answer);
|
return Easy::Status(ret.answer);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "Error %u in %s.", ret, __FUNCTION__);
|
syslog(LOG_ERR, "Error %u in %s.", ret.error_code, __FUNCTION__);
|
||||||
return Easy::Status();
|
return Easy::Status();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool Listener::send_reply(const Easy::Status &to_status,
|
bool Listener::send_reply(const Easy::Status &to_status,
|
||||||
const string &message)
|
const string &message)
|
||||||
{
|
{
|
||||||
std::uint_fast16_t ret;
|
Easy::return_entity<Easy::Status> ret;
|
||||||
string answer;
|
|
||||||
const string id = std::to_string(to_status.id());
|
|
||||||
string strvisibility;
|
|
||||||
|
|
||||||
switch (to_status.visibility())
|
Easy::Status new_status;
|
||||||
|
if (to_status.visibility() == Easy::visibility_type::Public)
|
||||||
{
|
{
|
||||||
case Easy::visibility_type::Private:
|
new_status.visibility(Easy::visibility_type::Unlisted);
|
||||||
strvisibility = "private";
|
|
||||||
break;
|
|
||||||
case Easy::visibility_type::Direct:
|
|
||||||
strvisibility = "direct";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
strvisibility = "unlisted";
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
Easy::parametermap parameters =
|
|
||||||
{
|
{
|
||||||
{ "in_reply_to_id", { id } },
|
new_status.visibility(to_status.visibility());
|
||||||
{ "visibility", { strvisibility } },
|
|
||||||
{ "status", { '@' + to_status.account().acct() + ' ' + message } }
|
|
||||||
};
|
|
||||||
|
|
||||||
if (to_status.sensitive())
|
|
||||||
{
|
|
||||||
parameters.insert({ "sensitive", { "true" } });
|
|
||||||
}
|
}
|
||||||
|
new_status.in_reply_to_id(to_status.id());
|
||||||
|
new_status.content('@' + to_status.account().acct() + ' ' + message);
|
||||||
|
new_status.sensitive(to_status.sensitive());
|
||||||
|
new_status.spoiler_text(to_status.spoiler_text());
|
||||||
|
|
||||||
if (!to_status.spoiler_text().empty())
|
ret = _masto->send_post(new_status);
|
||||||
{
|
|
||||||
parameters.insert({ "spoiler_text", { to_status.spoiler_text() } });
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = _masto->post(API::v1::statuses, parameters, answer);
|
if (ret)
|
||||||
|
|
||||||
if (ret == 0)
|
|
||||||
{
|
{
|
||||||
syslog(LOG_DEBUG, "Sent reply");
|
syslog(LOG_DEBUG, "Sent reply");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "Error %u in %s.", ret, __FUNCTION__);
|
syslog(LOG_ERR, "Error %u in %s.", ret.error_code, __FUNCTION__);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::uint_fast64_t Listener::get_parent_id(const Easy::Notification ¬if)
|
const string Listener::get_parent_id(const Easy::Notification ¬if)
|
||||||
{
|
{
|
||||||
string answer;
|
return_call ret;
|
||||||
std::uint_fast16_t ret;
|
|
||||||
|
|
||||||
// Fetch full status
|
// Retry up to 2 times
|
||||||
ret = _masto->get(API::v1::search, {{ "q", { notif.status().url() }}},
|
for (std::uint_fast8_t retries = 1; retries <= 2; ++retries)
|
||||||
answer);
|
|
||||||
if (ret > 0 || !Easy::Status(answer).valid())
|
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "Error %u: Could not fetch status (in %s).",
|
// Fetch full status
|
||||||
ret, __FUNCTION__);
|
ret = _masto->get(API::v1::search, {{ "q", { notif.status().url() }}});
|
||||||
return 0;
|
if (!ret)
|
||||||
|
{
|
||||||
|
syslog(LOG_ERR, "Error %u: Could not fetch status (in %s).",
|
||||||
|
ret.error_code, __FUNCTION__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = _masto->get(API::v1::statuses_id,
|
||||||
|
{{ "id", { notif.status().id() }}});
|
||||||
|
|
||||||
|
if (!ret)
|
||||||
|
{
|
||||||
|
syslog(LOG_ERR, "Error %u: Could not get status (in %s).",
|
||||||
|
ret.error_code, __FUNCTION__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_config["last_id"] = notif.id();
|
||||||
|
const Easy::Status s(ret.answer);
|
||||||
|
|
||||||
|
// If parent is found, return ID; else retry
|
||||||
|
if (!s.in_reply_to_id().empty())
|
||||||
|
{
|
||||||
|
return s.in_reply_to_id();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
syslog(LOG_WARNING, "Could not get ID of replied-to post");
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = _masto->get(API::v1::statuses_id,
|
|
||||||
{{ "id", { std::to_string(notif.status().id()) }}},
|
|
||||||
answer);
|
|
||||||
|
|
||||||
if (ret > 0)
|
|
||||||
{
|
|
||||||
syslog(LOG_ERR, "Error %u: Could not get status (in %s).",
|
|
||||||
ret, __FUNCTION__);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_config["last_id"] = std::to_string(notif.id());
|
|
||||||
Easy::Status s(answer);
|
|
||||||
return s.in_reply_to_id();
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool Listener::stillrunning() const
|
bool Listener::stillrunning() const
|
||||||
{
|
{
|
||||||
return _running;
|
return _running;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool Listener::register_app()
|
bool Listener::register_app()
|
||||||
{
|
{
|
||||||
cout << "Account (username@instance): ";
|
cout << "Account (username@instance): ";
|
||||||
std::cin >> _instance;
|
std::cin >> _instance;
|
||||||
_config["account"] = _instance;
|
_config["account"] = _instance;
|
||||||
_instance = _instance.substr(_instance.find('@') + 1);
|
_instance = _instance.substr(_instance.find('@') + 1);
|
||||||
|
|
||||||
_masto = std::make_unique<Easy>(_instance, "");
|
_masto = std::make_unique<Easy::API>(_instance, "");
|
||||||
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
|
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
|
||||||
global::version);
|
global::version);
|
||||||
|
|
||||||
std::uint_fast16_t ret;
|
return_call ret;
|
||||||
string client_id, client_secret, url;
|
string client_id, client_secret, url;
|
||||||
ret = _masto->register_app1("expandurl-mastodon",
|
ret = _masto->register_app1("expandurl-mastodon",
|
||||||
"urn:ietf:wg:oauth:2.0:oob",
|
"urn:ietf:wg:oauth:2.0:oob",
|
||||||
"read write",
|
"read write",
|
||||||
"https://github.com/tastytea/expandurl-mastodon",
|
"https://schlomp.space/tastytea/expandurl-mastodon",
|
||||||
client_id,
|
client_id,
|
||||||
client_secret,
|
client_secret,
|
||||||
url);
|
url);
|
||||||
if (ret == 0)
|
if (ret)
|
||||||
{
|
{
|
||||||
string code;
|
string code;
|
||||||
cout << "Visit " << url << " to authorize this application.\n";
|
cout << "Visit " << url << " to authorize this application.\n";
|
||||||
|
@ -348,20 +322,35 @@ const bool Listener::register_app()
|
||||||
"urn:ietf:wg:oauth:2.0:oob",
|
"urn:ietf:wg:oauth:2.0:oob",
|
||||||
code,
|
code,
|
||||||
_access_token);
|
_access_token);
|
||||||
if (ret == 0)
|
if (ret)
|
||||||
{
|
{
|
||||||
_config["access_token"] = _access_token;
|
_config["access_token"] = _access_token;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "register_app2(): %u", ret);
|
syslog(LOG_ERR, "register_app2(): %u", ret.error_code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "register_app1(): %u", ret);
|
syslog(LOG_ERR, "register_app1(): %u", ret.error_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Listener::set_proxy(Easy::API &masto)
|
||||||
|
{
|
||||||
|
if (!_proxy.empty())
|
||||||
|
{
|
||||||
|
if (!_proxy_user.empty())
|
||||||
|
{
|
||||||
|
masto.set_proxy(_proxy, _proxy_user + ':' + _proxy_password);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
masto.set_proxy(_proxy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
63
src/url.cpp
63
src/url.cpp
|
@ -1,5 +1,5 @@
|
||||||
/* This file is part of expandurl-mastodon.
|
/* This file is part of expandurl-mastodon.
|
||||||
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -31,15 +31,19 @@ namespace curlopts = curlpp::options;
|
||||||
|
|
||||||
const std::vector<string> get_urls(const string &html)
|
const std::vector<string> get_urls(const string &html)
|
||||||
{
|
{
|
||||||
const std::regex re_url("href=\"([^\"]+)\" rel");
|
const std::regex re_url("href=\\\\?\"([^\"\\\\]+)\\\\?\"([^>]+)");
|
||||||
std::smatch match;
|
std::smatch match;
|
||||||
string buffer = html;
|
string buffer = html;
|
||||||
std::vector<string> v;
|
std::vector<string> v;
|
||||||
|
|
||||||
while (std::regex_search(buffer, match, re_url))
|
while (std::regex_search(buffer, match, re_url))
|
||||||
{
|
{
|
||||||
string url = Easy::unescape_html(match[1].str());
|
// Add URL to vector if it is not a mention.#
|
||||||
v.push_back(strip(expand(url)));
|
if (match[2].str().find("mention") == std::string::npos)
|
||||||
|
{
|
||||||
|
string url = unescape_html(match[1].str());
|
||||||
|
v.push_back(strip(expand(url)));
|
||||||
|
}
|
||||||
buffer = match.suffix().str();
|
buffer = match.suffix().str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +65,7 @@ const string expand(const string &url)
|
||||||
"Connection: close",
|
"Connection: close",
|
||||||
});
|
});
|
||||||
request.setOpt<curlopts::FollowLocation>(true);
|
request.setOpt<curlopts::FollowLocation>(true);
|
||||||
|
request.setOpt(curlopts::Timeout(30));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -69,6 +74,7 @@ const string expand(const string &url)
|
||||||
catch (const std::exception &e)
|
catch (const std::exception &e)
|
||||||
{
|
{
|
||||||
syslog(LOG_ERR, "%s", e.what());
|
syslog(LOG_ERR, "%s", e.what());
|
||||||
|
// TODO: Do something when: "Couldn't resolve host …"
|
||||||
syslog(LOG_NOTICE, "The previous error is ignored.");
|
syslog(LOG_NOTICE, "The previous error is ignored.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,23 +83,48 @@ const string expand(const string &url)
|
||||||
|
|
||||||
const string strip(const string &url)
|
const string strip(const string &url)
|
||||||
{
|
{
|
||||||
using replace_pair = std::pair<const std::regex, const std::string>;
|
|
||||||
using namespace std::regex_constants;
|
using namespace std::regex_constants;
|
||||||
|
Json::Value &config = configfile.get_json();
|
||||||
string newurl = url;
|
string newurl = url;
|
||||||
|
|
||||||
const std::array<const replace_pair, 5> replace_array =
|
for (auto it = config["replace"].begin(); it != config["replace"].end();
|
||||||
{{
|
++it)
|
||||||
{ std::regex("[\\?&]utm_[^&]+", icase), "" }, // Google
|
|
||||||
{ std::regex("[\\?&]wtmc=[^&]+", icase), "" }, // Twitter?
|
|
||||||
{ std::regex("[\\?&]__twitter_impression=[^&]+", icase), "" }, // Twitter?
|
|
||||||
{ std::regex("[\\?&]wt_zmc=[^&]+", icase), "" }, // Twitter?
|
|
||||||
{ std::regex("//amp\\.", icase), "//" } // AMP
|
|
||||||
}};
|
|
||||||
|
|
||||||
for (const replace_pair &pair : replace_array)
|
|
||||||
{
|
{
|
||||||
newurl = std::regex_replace(newurl, pair.first, pair.second);
|
newurl = std::regex_replace(newurl,
|
||||||
|
std::regex(it.name(), icase),
|
||||||
|
(*it).asString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// If '&' is found in the new URL, but no '?'
|
||||||
|
if (newurl.find('&') != std::string::npos &&
|
||||||
|
newurl.find('?') == std::string::npos)
|
||||||
|
{
|
||||||
|
size_t pos = newurl.find('&');
|
||||||
|
newurl.replace(pos, 1, "?");
|
||||||
}
|
}
|
||||||
|
|
||||||
return newurl;
|
return newurl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void init_replacements()
|
||||||
|
{
|
||||||
|
using replace_pair = std::pair<const std::string, const std::string>;
|
||||||
|
Json::Value &config = configfile.get_json();
|
||||||
|
if (config["replace"].isNull())
|
||||||
|
{
|
||||||
|
const std::array<const replace_pair, 6> replace_array =
|
||||||
|
{{
|
||||||
|
{ "[\\?&]utm_[^&]+", "" }, // Google
|
||||||
|
{ "[\\?&]wt_?[^&]+", "" }, // Twitter?
|
||||||
|
{ "[\\?&]__twitter_impression=[^&]+", "" }, // Twitter?
|
||||||
|
{ "//amp\\.", "//" }, // AMP
|
||||||
|
{ "/amp/", "" }, // AMP
|
||||||
|
{ "[\\?&]service=amp", "" } // AMP
|
||||||
|
}};
|
||||||
|
|
||||||
|
for (const replace_pair &pair : replace_array)
|
||||||
|
{
|
||||||
|
config["replace"][pair.first] = pair.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue