summaryrefslogtreecommitdiffstats
path: root/README.adoc
blob: 645fe31414b00a372e57c53c065ccb38d0512a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
= mastodonpp
:toc: preamble
:project: mastodonpp
:uri-base: https://schlomp.space/tastytea/{project}
:uri-branch-main: {uri-base}/src/branch/main
:uri-mastodon-cpp: https://schlomp.space/tastytea/mastodon-cpp
:uri-reference: https://doc.schlomp.space/{project}/
:uri-gcc: https://gcc.gnu.org/
:uti-clang: https://clang.llvm.org/
:uri-cmake: https://cmake.org/
:uri-doxygen: http://www.doxygen.nl/
:uri-catch: https://github.com/catchorg/Catch2
:uri-dpkg: https://packages.qa.debian.org/dpkg
:uri-rpm-build: http://www.rpm.org
:uri-libcurl: https://curl.haxx.se/libcurl/

*{project}* is a C++ wrapper for the Mastodon and Pleroma APIs. It replaces
link:{uri-mastodon-cpp}[mastodon-cpp].

We aim to create a library that is comfortable, yet minimal. All API endpoints
from Mastodon and Pleroma are stored in ``enum class``es, to counteract typos and
make your life easier. The network-facing code is built on
link:{uri-libcurl}[libcurl], a mature and stable library that is available on
virtually every operating system. The library does not parse the responses
itself, but returns to you the raw data, because we know everyone has their
favorite JSON library and we don't want to impose our choice on you!

== Features

This is still a work in progress; here is a rough overview of the features:

* [x]  `GET`, Streaming `GET`, `POST`, `PATCH`, `PUT` and `DELETE` requests.
* [x] Report maximum allowed character per post.
* [ ] Comfortable access to pagination headers.
* [ ] Comfortable function to register a new “app” (get an access token).
* [ ] Report which mime types are allowed for posting statuses.

== Usage

Have a look at the link:{uri-reference}[reference].

=== Example

[source,cpp]
--------------------------------------------------------------------------------
#include "mastodonpp.hpp"
#include <iostream>

int main()
{
    mastodonpp::Instance instance{"example.com", "123AccessToken123"};
    instance.set_proxy("socks4a://[fd14::1:1]:9050");
    mastodonpp::Connection connection{instance};

    const mastodonpp::parametermap parameters
        {
            {"status", "How is the weather?"},
            {"poll[options]", vector<string_view>{"Nice", "not nice"}},
            {"poll[expires_in]", "86400"}
        };
    auto answer{connection.post(mastodonpp::API::v1::statuses, parameters)};

    if (answer)
    {
        std::cout << answer << std::endl;
    }
}
--------------------------------------------------------------------------------

link:{uri-reference}/examples.html[More examples] are included in the reference.

== Install

=== Gentoo

[source,shell]
--------------------------------------------------------------------------------
eselect repository enable tastytea
echo 'dev-cpp/mastodonpp' >> /etc/portage/package.accept_keywords/mastodonpp
emaint sync -r tastytea
emerge -a dev-cpp/mastodonpp
--------------------------------------------------------------------------------

=== Debian and Ubuntu

We automatically generate packages for Debian buster (10) and Ubuntu bionic
(18.04), but only for x86_64 (amd64). Download them at
link:{uri-base}/releases[schlomp.space].

[source,shell]
--------------------------------------------------------------------------------
apt install ./libmastodonpp*.deb
--------------------------------------------------------------------------------

=== CentOS

We automatically generate packages for CentOS 8, but only for x86_64
(amd64). Download them at link:{uri-base}/releases[schlomp.space].

[source,shell]
--------------------------------------------------------------------------------
yum install ./libmastodonpp*.rpm
--------------------------------------------------------------------------------

=== From source

==== Dependencies

* Tested OS: Linux
* C++ compiler (tested: link:{uri-gcc}[GCC] 7/8/9, link:{uri-lang}[clang] 6/7)
* link:{uri-cmake}[CMake] (at least: 3.9)
* link:{uri-libcurl}[libcurl] (at least: 7.56)
* Optional
  ** Documentation: link:{uri-doxygen}[Doxygen] (tested: 1.8)
  ** Tests: link:{uri-catch}[Catch] (tested: 2.5 / 1.2)
  ** DEB package: link:{uri-dpkg}[dpkg] (tested: 1.18)
  ** RPM package: link:{uri-rpm-build}[rpm-build] (tested: 4.11)

==== Get sourcecode

===== Release

Download the current release at link:{uri-base}/releases[schlomp.space].

===== Development version

[source,shell]
--------------------------------------------------------------------------------
git clone https://schlomp.space/tastytea/mastodonpp.git
--------------------------------------------------------------------------------

==== Compile

[source,shell]
--------------------------------------------------------------------------------
mkdir -p build && cd build
cmake ..
cmake --build . -- -j$(nproc --ignore=1)
--------------------------------------------------------------------------------

.CMake options:
* `-DCMAKE_BUILD_TYPE=Debug` for a debug build.
* `-DWITH_TESTS=YES` if you want to compile the tests.
* `-DWITH_EXAMPLES=YES` if you want to compile the examples.
* One of:
  ** `-DWITH_DEB=YES` if you want to be able to generate a deb-package.
  ** `-DWITH_RPM=YES` if you want to be able to generate an rpm-package.

include::{uri-base}/raw/branch/main/CONTRIBUTING.adoc[]
thbuf = STRBUF_INIT; int result = 0; strbuf_addf(&pathbuf, "%s/objects", path); if (stat(pathbuf.buf, &st)) { if (errno != ENOENT) fprintf(stderr, "Error checking path %s: %s (%d)\n", path, strerror(errno), errno); goto out; } if (!S_ISDIR(st.st_mode)) goto out; strbuf_reset(&pathbuf); strbuf_addf(&pathbuf, "%s/HEAD", path); if (stat(pathbuf.buf, &st)) { if (errno != ENOENT) fprintf(stderr, "Error checking path %s: %s (%d)\n", path, strerror(errno), errno); goto out; } if (!S_ISREG(st.st_mode)) goto out; result = 1; out: strbuf_release(&pathbuf); return result; } static struct cgit_repo *repo; static repo_config_fn config_fn; static void scan_tree_repo_config(const char *name, const char *value) { config_fn(repo, name, value); } static int gitconfig_config(const char *key, const char *value, void *cb) { const char *name; if (!strcmp(key, "gitweb.owner")) config_fn(repo, "owner", value); else if (!strcmp(key, "gitweb.description")) config_fn(repo, "desc", value); else if (!strcmp(key, "gitweb.category")) config_fn(repo, "section", value); else if (!strcmp(key, "gitweb.homepage")) config_fn(repo, "homepage", value); else if (skip_prefix(key, "cgit.", &name)) config_fn(repo, name, value); return 0; } static char *xstrrchr(char *s, char *from, int c) { while (from >= s && *from != c) from--; return from < s ? NULL : from; } static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn) { struct stat st; struct passwd *pwd; size_t pathlen; struct strbuf rel = STRBUF_INIT; char *p, *slash; int n; size_t size; if (stat(path->buf, &st)) { fprintf(stderr, "Error accessing %s: %s (%d)\n", path->buf, strerror(errno), errno); return; } strbuf_addch(path, '/'); pathlen = path->len; if (ctx.cfg.strict_export) { strbuf_addstr(path, ctx.cfg.strict_export); if(stat(path->buf, &st)) return; strbuf_setlen(path, pathlen); } strbuf_addstr(path, "noweb"); if (!stat(path->buf, &st)) return; strbuf_setlen(path, pathlen); if (!starts_with(path->buf, base)) strbuf_addbuf(&rel, path); else strbuf_addstr(&rel, path->buf + strlen(base) + 1); if (!strcmp(rel.buf + rel.len - 5, "/.git")) strbuf_setlen(&rel, rel.len - 5); else if (rel.len && rel.buf[rel.len - 1] == '/') strbuf_setlen(&rel, rel.len - 1); repo = cgit_add_repo(rel.buf); config_fn = fn; if (ctx.cfg.enable_git_config) { strbuf_addstr(path, "config"); git_config_from_file(gitconfig_config, path->buf, NULL); strbuf_setlen(path, pathlen); } if (ctx.cfg.remove_suffix) { size_t urllen; strip_suffix(repo->url, ".git", &urllen); strip_suffix_mem(repo->url, &urllen, "/"); repo->url[urllen] = '\0'; } repo->path = xstrdup(path->buf); while (!repo->owner) { if ((pwd = getpwuid(st.st_uid)) == NULL) { fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", path->buf, strerror(errno), errno); break; } if (pwd->pw_gecos) if ((p = strchr(pwd->pw_gecos, ','))) *p = '\0'; repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); } if (repo->desc == cgit_default_repo_desc || !repo->desc) { strbuf_addstr(path, "description"); if (!stat(path->buf, &st)) readfile(path->buf, &repo->desc, &size); strbuf_setlen(path, pathlen); } if (ctx.cfg.section_from_path) { n = ctx.cfg.section_from_path; if (n > 0) { slash = rel.buf - 1; while (slash && n && (slash = strchr(slash + 1, '/'))) n--; } else { slash = rel.buf + rel.len; while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/'))) n++; } if (slash && !n) { *slash = '\0'; repo->section = xstrdup(rel.buf); *slash = '/'; if (starts_with(repo->name, repo->section)) { repo->name += strlen(repo->section); if (*repo->name == '/') repo->name++; } } } strbuf_addstr(path, "cgitrc"); if (!stat(path->buf, &st)) parse_configfile(path->buf, &scan_tree_repo_config); strbuf_release(&rel); } static void scan_path(const char *base, const char *path, repo_config_fn fn) { DIR *dir = opendir(path); struct dirent *ent; struct strbuf pathbuf = STRBUF_INIT; size_t pathlen = strlen(path); struct stat st; if (!dir) { fprintf(stderr, "Error opening directory %s: %s (%d)\n", path, strerror(errno), errno); return; } strbuf_add(&pathbuf, path, strlen(path)); if (is_git_dir(pathbuf.buf)) { add_repo(base, &pathbuf, fn); goto end; } strbuf_addstr(&pathbuf, "/.git"); if (is_git_dir(pathbuf.buf)) { add_repo(base, &pathbuf, fn); goto end; } /* * Add one because we don't want to lose the trailing '/' when we * reset the length of pathbuf in the loop below. */ pathlen++; while ((ent = readdir(dir)) != NULL) { if (ent->d_name[0] == '.') { if (ent->d_name[1] == '\0') continue; if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') continue; if (!ctx.cfg.scan_hidden_path) continue; } strbuf_setlen(&pathbuf, pathlen); strbuf_addstr(&pathbuf, ent->d_name); if (stat(pathbuf.buf, &st)) { fprintf(stderr, "Error checking path %s: %s (%d)\n", pathbuf.buf, strerror(errno), errno); continue; } if (S_ISDIR(st.st_mode)) scan_path(base, pathbuf.buf, fn); } end: strbuf_release(&pathbuf); closedir(dir); } void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) { struct strbuf line = STRBUF_INIT; FILE *projects; int err; projects = fopen(projectsfile, "r"); if (!projects) { fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n", projectsfile, strerror(errno), errno); return; } while (strbuf_getline(&line, projects) != EOF) { if (!line.len) continue; strbuf_insert(&line, 0, "/", 1); strbuf_insert(&line, 0, path, strlen(path)); scan_path(path, line.buf, fn); } if ((err = ferror(projects))) { fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", projectsfile, strerror(err), err); } fclose(projects); strbuf_release(&line); } void scan_tree(const char *path, repo_config_fn fn) { scan_path(path, path, fn); }