aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorqorg11 <qorg@vxempire.xyz>2021-06-29 13:49:52 +0200
committerqorg11 <qorg@vxempire.xyz>2021-06-29 13:49:52 +0200
commitd7b6807fab6f2d0ee4a0ac3ba40ed581bf1b9600 (patch)
treec780711b599caf0e96cf2fb05430d6f3e98aab03
parentecea60640413d856b2e78acba83f99723d4a6b8c (diff)
downloaddemiurge-d7b6807fab6f2d0ee4a0ac3ba40ed581bf1b9600.tar.gz
demiurge-d7b6807fab6f2d0ee4a0ac3ba40ed581bf1b9600.zip
add format
-rw-r--r--src/.clang-format4
-rw-r--r--src/asprintf.c24
-rw-r--r--src/login.c97
-rw-r--r--src/main.c44
-rw-r--r--src/post.c62
-rw-r--r--src/upload_file.c42
-rw-r--r--src/util.c42
7 files changed, 166 insertions, 149 deletions
diff --git a/src/.clang-format b/src/.clang-format
index 5fca9a3..ea78709 100644
--- a/src/.clang-format
+++ b/src/.clang-format
@@ -10,8 +10,8 @@ AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
-AllowAllArgumentsOnNextLine: true
-AllowAllConstructorInitializersOnNextLine: true
+AllowAllArgumentsOnNextLine: false
+AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
diff --git a/src/asprintf.c b/src/asprintf.c
index 28b7f8f..5120335 100644
--- a/src/asprintf.c
+++ b/src/asprintf.c
@@ -6,31 +6,29 @@
/* Implementation of asprintf() */
int
-asprintf(char **restrict strp, const char *restrict fmt,... )
+asprintf(char **restrict strp, const char *restrict fmt, ...)
{
va_list args;
int size = 0;
- va_start(args,fmt);
+ va_start(args, fmt);
va_end(args);
- return size = vasprintf(strp,fmt,args);
-
+ return size = vasprintf(strp, fmt, args);
}
int
-vasprintf(char **restrict strp, const char *restrict fmt,
- va_list ap)
+vasprintf(char **restrict strp, const char *restrict fmt, va_list ap)
{
va_list args;
- va_copy(args,ap);
- int size = vsnprintf(NULL,0,fmt,args);
-
+ va_copy(args, ap);
+ int size = vsnprintf(NULL, 0, fmt, args);
+
/* if negative number is returned return error */
- if (size < 0)
+ if(size < 0)
return -1;
- *strp = (char*)malloc(size + 1);
+ *strp = (char *)malloc(size + 1);
if(*strp == NULL)
return -1;
-
+
va_end(args);
- return size = vsprintf(*strp,fmt,ap);
+ return size = vsprintf(*strp, fmt, ap);
}
diff --git a/src/login.c b/src/login.c
index 028bc18..25e2f1c 100644
--- a/src/login.c
+++ b/src/login.c
@@ -9,15 +9,17 @@
#include "util.h"
void
-store_config(const char *instance, const char *client_id,
- const char *client_secret, const char *access_token)
+store_config(const char *instance,
+ const char *client_id,
+ const char *client_secret,
+ const char *access_token)
{
- FILE *fp = fopen(".demiurgerc","w+");
-
- fprintf(fp,"%s=%s\n","instance",instance);
- fprintf(fp,"%s=%s\n","client_id",client_id);
- fprintf(fp,"%s=%s\n","client_secret",client_secret);
- fprintf(fp,"%s=%s\n","access_token",access_token);
+ FILE *fp = fopen(".demiurgerc", "w+");
+
+ fprintf(fp, "%s=%s\n", "instance", instance);
+ fprintf(fp, "%s=%s\n", "client_id", client_id);
+ fprintf(fp, "%s=%s\n", "client_secret", client_secret);
+ fprintf(fp, "%s=%s\n", "access_token", access_token);
fclose(fp);
}
@@ -28,89 +30,100 @@ int
setup()
{
char *instance = NULL;
- instance = readline("Enter your instance (e.g. https://social.fnord.tld) ");
-
+ instance =
+ readline("Enter your instance (e.g. https://social.fnord.tld) ");
+
char *api_url = "/api/v1/apps";
-
+
CURL *curl = curl_easy_init();
struct json_object *parsed_json;
struct json_object *json_client_id;
struct json_object *json_client_secret;
struct json_object *json_access_token;
-
+
if(curl == NULL) {
- fprintf(stderr,"Error creating curl (what?)\n");
+ fprintf(stderr, "Error creating curl (what?)\n");
return -1;
}
-
+
char buf[8192];
char *post_url = NULL;
- asprintf(&post_url,"%s%s",instance,api_url);
- curl_easy_setopt(curl,CURLOPT_URL, post_url);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,buf);
-
+ asprintf(&post_url, "%s%s", instance, api_url);
+ curl_easy_setopt(curl, CURLOPT_URL, post_url);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
+
/* TODO: Better way lol */
/* If someone wants to do it, use curl_mime thing, I am to lazy
* to do this */
-
- curl_easy_setopt(curl,CURLOPT_POSTFIELDS,
- "client_name=demiurge&redirect_uris=urn:ietf:wg:oauth:2.0:oob&scope=read write");
-
+
+ curl_easy_setopt(curl,
+ CURLOPT_POSTFIELDS,
+ "client_name=demiurge&redirect_uris=urn:ietf:wg:oauth:2."
+ "0:oob&scope=read write");
+
curl_easy_perform(curl);
curl_easy_cleanup(curl);
parsed_json = json_tokener_parse(buf);
if(parsed_json == NULL) {
- fprintf(stderr,"error\n");
+ fprintf(stderr, "error\n");
return -1;
}
-
+
json_object_object_get_ex(parsed_json, "client_id", &json_client_id);
- json_object_object_get_ex(parsed_json, "client_secret", &json_client_secret);
+ json_object_object_get_ex(parsed_json,
+ "client_secret",
+ &json_client_secret);
const char *client_id = json_object_get_string(json_client_id);
const char *client_secret = json_object_get_string(json_client_secret);
-
- char *fmt = "%s%sresonse_type=code&client_id=%s&redirect_uri=urn:ietf:wg:oauth:2.0:oob&force_login&scope=read write";
+
+ char *fmt = "%s%sresonse_type=code&client_id=%s&redirect_uri=urn:ietf:wg:"
+ "oauth:2.0:oob&force_login&scope=read write";
api_url = "/oauth/authorize?";
-
+
free(post_url);
post_url = NULL;
- asprintf(&post_url,fmt,instance,api_url,client_id);
+ asprintf(&post_url, fmt, instance, api_url, client_id);
puts(post_url);
curl = curl_easy_init();
-
+
char *code = NULL;
code = readline("Please log in in that url and paste the code here: ");
- char *access_token_fmt = "client_id=%s&client_secret=%s&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code&code=%s&scope=read write";
+ char *access_token_fmt =
+ "client_id=%s&client_secret=%s&redirect_uri=urn:ietf:wg:oauth:2.0:"
+ "oob&grant_type=authorization_code&code=%s&scope=read write";
api_url = "/oauth/token";
-
char *post_token_url = NULL;
- asprintf(&post_token_url, access_token_fmt,client_id,client_secret,code);
+ asprintf(&post_token_url,
+ access_token_fmt,
+ client_id,
+ client_secret,
+ code);
post_url = NULL;
-
- asprintf(&post_url,"%s%s",instance,api_url);
- curl_easy_setopt(curl, CURLOPT_URL,post_url);
- curl_easy_setopt(curl,CURLOPT_POSTFIELDS, post_token_url);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,buf);
+ asprintf(&post_url, "%s%s", instance, api_url);
+
+ curl_easy_setopt(curl, CURLOPT_URL, post_url);
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_token_url);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
curl_easy_perform(curl);
parsed_json = json_tokener_parse(buf);
json_object_object_get_ex(parsed_json, "access_token", &json_access_token);
const char *access_token = json_object_get_string(json_access_token);
- store_config(instance,client_id,client_secret,access_token);
-
+ store_config(instance, client_id, client_secret, access_token);
+
free(code);
free(post_url);
return 0;
diff --git a/src/main.c b/src/main.c
index 854895d..6de525b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -15,60 +15,58 @@
void
usage(const char *progname)
{
- printf("Usage: %s -s=status [-v=visibility] [-F=filename]\n",progname);
+ printf("Usage: %s -s=status [-v=visibility] [-F=filename]\n", progname);
return;
}
/* prints a string to stderr */
-
int
main(int argc, char **argv)
{
- if(access(".demiurgerc", F_OK ))
+ if(access(".demiurgerc", F_OK))
setup();
int c;
char *status = NULL;
char *visibility = NULL;
char *id_ptr = NULL;
-
+
/* TODO: Support filename and visibility */
-
+
if(!isatty(0)) {
eputs("Not a terminal, reading from stdin");
char *status = readline("");
- post_status(status,NULL,NULL);
+ post_status(status, NULL, NULL);
return 0;
}
-
+
if(argc == 1) {
usage(argv[0]);
return -1;
}
-
- while((c = getopt(argc,argv,"s:v:F:")) != -1) {
+
+ while((c = getopt(argc, argv, "s:v:F:")) != -1) {
switch(c) {
- case 's':
- status = optarg;
- break;
- case 'v':
- visibility = optarg;
- break;
- case 'F':
- upload_file(optarg,basename(optarg),&id_ptr);
- break;
+ case 's':
+ status = optarg;
+ break;
+ case 'v':
+ visibility = optarg;
+ break;
+ case 'F':
+ upload_file(optarg, basename(optarg), &id_ptr);
+ break;
}
-
}
-
+
if(status == NULL) {
eputs("Enter a status (-s)");
return -1;
}
-
+
if(visibility == NULL) {
visibility = "public";
}
-
- post_status(status,visibility,id_ptr);
+
+ post_status(status, visibility, id_ptr);
}
diff --git a/src/post.c b/src/post.c
index e70578e..c4683c1 100644
--- a/src/post.c
+++ b/src/post.c
@@ -9,7 +9,6 @@
/* This function post "status" with the visibility "visibility" */
-
int
post_status(const char *status, const char *scope, const char *media_id)
{
@@ -17,78 +16,81 @@ post_status(const char *status, const char *scope, const char *media_id)
char client_id[50];
char client_secret[50];
char access_token[50];
-
- get_tokens_from_file(".demiurgerc",instance,client_id,client_secret,access_token);
+
+ get_tokens_from_file(".demiurgerc",
+ instance,
+ client_id,
+ client_secret,
+ access_token);
CURL *curl = curl_easy_init();
if(curl == NULL) {
- fprintf(stderr,"Error creating libcurl thing\n");
+ fprintf(stderr, "Error creating libcurl thing\n");
return -1;
}
-
+
char *api_url = "/api/v1/statuses";
char *url = NULL;
-
- asprintf(&url,"%s%s",instance,api_url);
+
+ asprintf(&url, "%s%s", instance, api_url);
char *header_fmt = "Authorization: Bearer %s";
char *authorization_header = NULL;
struct curl_slist *header_list = NULL;
-
- asprintf(&authorization_header,header_fmt,access_token);
+
+ asprintf(&authorization_header, header_fmt, access_token);
header_list = curl_slist_append(header_list, authorization_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
/* Write the thing */
-
- struct memory chunk = {0};
+
+ struct memory chunk = { 0 };
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
-
+
curl_mime *mime;
mime = curl_mime_init(curl);
curl_mimepart *status_part, *scope_part, *media_part;
/* Is there a better way to do this? */
-
+
status_part = curl_mime_addpart(mime);
scope_part = curl_mime_addpart(mime);
media_part = curl_mime_addpart(mime);
-
+
/* Status */
-
- curl_mime_data(status_part,status,CURL_ZERO_TERMINATED);
- curl_mime_name(status_part,"status");
-
+
+ curl_mime_data(status_part, status, CURL_ZERO_TERMINATED);
+ curl_mime_name(status_part, "status");
+
/* Visibility */
- curl_mime_data(scope_part,scope,CURL_ZERO_TERMINATED);
- curl_mime_name(scope_part,"visibility");
+ curl_mime_data(scope_part, scope, CURL_ZERO_TERMINATED);
+ curl_mime_name(scope_part, "visibility");
/* Media */
-
+
if(media_id != NULL) {
- curl_mime_data(media_part,media_id,CURL_ZERO_TERMINATED);
- curl_mime_name(media_part,"media_ids[]");
+ curl_mime_data(media_part, media_id, CURL_ZERO_TERMINATED);
+ curl_mime_name(media_part, "media_ids[]");
}
-
+
/* post */
-
- curl_easy_setopt(curl,CURLOPT_URL,url);
- curl_easy_setopt(curl,CURLOPT_MIMEPOST,mime);
+
+ curl_easy_setopt(curl, CURLOPT_URL, url);
+ curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_perform(curl);
/* The the url */
struct json_object *parsed_json;
struct json_object *json_url;
-
+
parsed_json = json_tokener_parse(chunk.response);
json_object_object_get_ex(parsed_json, "url", &json_url);
puts(json_object_get_string(json_url));
-
+
free(url);
free(authorization_header);
free(chunk.response);
return 0;
-
}
diff --git a/src/upload_file.c b/src/upload_file.c
index 402ec6f..e64f450 100644
--- a/src/upload_file.c
+++ b/src/upload_file.c
@@ -16,30 +16,34 @@ upload_file(const char *path, const char *description, char **id_ptr)
struct json_object *parsed_json;
struct json_object *json_media_id;
char buf[8192];
-
- get_tokens_from_file(".demiurgerc",instance,client_id,client_secret,access_token);
+
+ get_tokens_from_file(".demiurgerc",
+ instance,
+ client_id,
+ client_secret,
+ access_token);
CURL *curl = curl_easy_init();
if(curl == NULL) {
- fprintf(stderr,"Error creating libcurl thing\n");
+ fprintf(stderr, "Error creating libcurl thing\n");
return -1;
}
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,buf);
-
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
+
char *url_to_post = NULL;
- asprintf(&url_to_post,"%s/api/v1/media",instance);
+ asprintf(&url_to_post, "%s/api/v1/media", instance);
/* Don't repeat yourself, so they say, it's the root of all evil
* today */
-
+
char *header_fmt = "Authorization: Bearer %s";
struct curl_slist *header_list = NULL;
char *authorization_header = NULL;
- asprintf(&authorization_header,header_fmt,access_token);
+ asprintf(&authorization_header, header_fmt, access_token);
header_list = curl_slist_append(header_list, authorization_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
-
+
curl_mime *mime;
mime = curl_mime_init(curl);
curl_mimepart *image_part;
@@ -47,19 +51,19 @@ upload_file(const char *path, const char *description, char **id_ptr)
curl_mimepart *description_part;
description_part = curl_mime_addpart(mime);
/* Upload file */
-
+
curl_mime_filedata(image_part, path);
- curl_mime_name(image_part,"file");
-
+ curl_mime_name(image_part, "file");
+
/* Description */
-
- curl_mime_data(description_part,description,CURL_ZERO_TERMINATED);
- curl_mime_name(description_part,"description");
+
+ curl_mime_data(description_part, description, CURL_ZERO_TERMINATED);
+ curl_mime_name(description_part, "description");
/* Post */
-
- curl_easy_setopt(curl,CURLOPT_MIMEPOST,mime);
- curl_easy_setopt(curl,CURLOPT_URL,url_to_post);
+
+ curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
+ curl_easy_setopt(curl, CURLOPT_URL, url_to_post);
/* Free the things */
curl_easy_perform(curl);
diff --git a/src/util.c b/src/util.c
index a791688..21a9067 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2,12 +2,12 @@
#include <string.h>
#include <stdlib.h>
-struct memory {
+struct memory
+{
char *response;
size_t size;
};
-
/* This function fucking sucks and should use something else. I'll
* have to rewrite this shit function some day. But for now, it will
* do the job.
@@ -16,45 +16,47 @@ struct memory {
/* Be aware with buffer overflows, fscanf() */
-
int
-get_tokens_from_file(char *filename, char *instance, char *client_id,
- char *client_secret, char *access_token)
+get_tokens_from_file(char *filename,
+ char *instance,
+ char *client_id,
+ char *client_secret,
+ char *access_token)
{
- FILE *fp = fopen(filename,"r");
+ FILE *fp = fopen(filename, "r");
if(fp == NULL)
return -1;
- fscanf(fp,"instance=%s\n",instance);
- fscanf(fp,"client_id=%s\n",client_id);
- fscanf(fp,"client_secret=%s\n",client_secret);
- fscanf(fp,"access_token=%s\n",access_token);
-
+ fscanf(fp, "instance=%s\n", instance);
+ fscanf(fp, "client_id=%s\n", client_id);
+ fscanf(fp, "client_secret=%s\n", client_secret);
+ fscanf(fp, "access_token=%s\n", access_token);
+
return 0;
}
-
-size_t cb(void *data, size_t size, size_t nmemb, void *userp)
+
+size_t
+cb(void *data, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct memory *mem = (struct memory *)userp;
-
+
char *ptr = realloc(mem->response, mem->size + realsize + 1);
if(ptr == NULL)
- return 0; /* out of memory! */
-
+ return 0; /* out of memory! */
+
mem->response = ptr;
memcpy(&(mem->response[mem->size]), data, realsize);
mem->size += realsize;
mem->response[mem->size] = 0;
-
+
return realsize;
}
-
size_t
write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
- memcpy(userp,buffer,nmemb*size);
+ memcpy(userp, buffer, nmemb * size);
return 0;
}
@@ -63,5 +65,5 @@ write_data(void *buffer, size_t size, size_t nmemb, void *userp)
void
eputs(const char *s)
{
- fprintf(stderr,"%s\n",s);
+ fprintf(stderr, "%s\n", s);
}