ref: 454703e94b801ebe25b61f964d52ad4fbb7bd3d9
parent: 22b180670881db7c4850e7fd9786ffcb70498057
author: Julien Blanchard <julien@typed-hole.org>
date: Fri Apr 7 07:11:58 EDT 2023
Some formatting
--- /dev/null
+++ b/.clang-format
@@ -1,0 +1,11 @@
+BasedOnStyle: LLVM
+IndentWidth: 4
+TabWidth: 4
+UseTab: Always
+AllowShortFunctionsOnASingleLine: InlineOnly
+AlwaysBreakAfterReturnType: TopLevelDefinitions
+BreakBeforeBraces: Allman
+SpaceBeforeParens: false
+PointerAlignment: Right
+AlignTrailingComments: true
+SortIncludes: false
\ No newline at end of file
--- a/http.c
+++ b/http.c
@@ -2,11 +2,10 @@
#include <libc.h>
#include <stdio.h>
#include <json.h>
-#include <bio.h>
#include "masto9.h"
-#define BOUNDARY "---------------------------328018649918767126933410246249"
+#define BOUNDARY "--------HJBOUNDARY"
int
webclone(int *c)
@@ -16,7 +15,7 @@
if((fd = open("/mnt/web/clone", ORDWR)) < 0)
sysfatal("couldn't open %s: %r", buf);
- if((n = read(fd, buf, sizeof buf-1)) < 0)
+ if((n = read(fd, buf, sizeof buf - 1)) < 0)
sysfatal("reading clone: %r");
if(n == 0)
sysfatal("short read on clone");
@@ -29,34 +28,34 @@
char *
httpget(char *token, char *url)
{
- int ctlfd, bodyfd, conn, n;
+ int ctlfd, bodyfd, conn, n;
char buf[1024];
char body[TLBUFSIZE];
- char *bearer_token;
+ char *bearer_token;
- ctlfd = webclone(&conn);
+ ctlfd = webclone(&conn);
snprint(buf, sizeof buf, "url %s", url);
if(write(ctlfd, buf, n = strlen(buf)) != n)
sysfatal("post: write: %r");
- /* Request */
- bearer_token = concat("Authorization: Bearer ", token);
- if (fprint(ctlfd, "headers %s", bearer_token) <= 0)
+ /* Request */
+ bearer_token = concat("Authorization: Bearer ", token);
+ if(fprint(ctlfd, "headers %s", bearer_token) <= 0)
sysfatal("write ctl failed 'headers'");
snprint(buf, sizeof(buf), "/mnt/web/%d/body", conn);
- /* Response */
- if ((bodyfd = open(buf, OREAD)) < 0)
+ /* Response */
+ if((bodyfd = open(buf, OREAD)) < 0)
sysfatal("open %s: %r", buf);
- if (readn(bodyfd, body, TLBUFSIZE) <= 0)
+ if(readn(bodyfd, body, TLBUFSIZE) <= 0)
sysfatal("readn: %r");
close(bodyfd);
close(ctlfd);
- return body;
+ return body;
}
char *
@@ -64,54 +63,61 @@
{
int n, ctlfd, bodyfd, conn;
char buf[TOOTBUFSIZE];
- char *bearer_token;
+ char *bearer_token;
- ctlfd = webclone(&conn);
+ ctlfd = webclone(&conn);
snprint(buf, sizeof buf, "url %s", url);
if(write(ctlfd, buf, n = strlen(buf)) != n)
sysfatal("post: write: %r");
- /* Request */
- bearer_token = concat("Authorization: Bearer ", token);
- if (fprint(ctlfd, "headers %s", bearer_token) <= 0)
+ /* Request */
+ bearer_token = concat("Authorization: Bearer ", token);
+ if(fprint(ctlfd, "headers %s", bearer_token) <= 0)
sysfatal("write ctl failed 'headers'");
snprint(buf, TOOTBUFSIZE, "/mnt/web/%d/postbody", conn);
bodyfd = open(buf, OWRITE);
- if (bodyfd < 0)
+ if(bodyfd < 0)
sysfatal("open bodyfd %s: %r", buf);
- if (write(bodyfd, text, strlen(text)) < 0)
- sysfatal("write: %r");
+ if(write(bodyfd, text, strlen(text)) < 0)
+ sysfatal("write: %r");
close(bodyfd);
- /* Response */
- snprint(buf, TOOTBUFSIZE, "/mnt/web/%d/body", conn);
+ /* Response */
+ snprint(buf, TOOTBUFSIZE, "/mnt/web/%d/body", conn);
if((bodyfd = open(buf, OREAD)) < 0)
sysfatal("post: opening body: %r");
- if (readn(bodyfd, buf, TOOTBUFSIZE) <= 0)
+ if(readn(bodyfd, buf, TOOTBUFSIZE) <= 0)
sysfatal("readn: %r");
close(bodyfd);
close(ctlfd);
- return buf;
+ return buf;
}
-char
-*mime_type(char *filename)
+char *
+mime_type(char *filename)
{
- char *ext = strrchr(filename, '.');
- if (!ext) return "application/octet-stream";
- if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0) return "image/jpeg";
- if (strcmp(ext, ".gif") == 0) return "image/gif";
- if (strcmp(ext, ".png") == 0) return "image/png";
- if (strcmp(ext, ".mp3") == 0) return "audio/mp3";
- if (strcmp(ext, ".mp4") == 0) return "video/mp4";
- if (strcmp(ext, ".webm") == 0) return "video/webm";
- return "application/octet-stream";
+ char *ext = strrchr(filename, '.');
+ if(!ext)
+ return "application/octet-stream";
+ if(strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0)
+ return "image/jpeg";
+ if(strcmp(ext, ".gif") == 0)
+ return "image/gif";
+ if(strcmp(ext, ".png") == 0)
+ return "image/png";
+ if(strcmp(ext, ".mp3") == 0)
+ return "audio/mp3";
+ if(strcmp(ext, ".mp4") == 0)
+ return "video/mp4";
+ if(strcmp(ext, ".webm") == 0)
+ return "video/webm";
+ return "application/octet-stream";
}
char *
@@ -119,63 +125,65 @@
{
char buf[BUFSIZE];
int n, conn, ctlfd, bodyfd;
- char *bearer_token, *multipart_header;
- FileAttachment *fa;
+ char *bearer_token, *multipart_header;
+ FileAttachment *fa;
ctlfd = open("/mnt/web/clone", ORDWR);
- if (ctlfd < 0)
+ if(ctlfd < 0)
sysfatal("open: %r");
n = read(ctlfd, buf, sizeof(buf));
- if (n < 0)
+ if(n < 0)
sysfatal("read: %r");
buf[n] = '\0';
conn = atoi(buf);
- fa = readfile(filepath);
+ fa = readfile(filepath);
snprint(buf, sizeof buf, "url %s", url);
if(write(ctlfd, buf, n = strlen(buf)) != n)
sysfatal("post: write: %r");
- bearer_token = esmprint("Authorization: Bearer %s", token);
+ bearer_token = esmprint("Authorization: Bearer %s", token);
snprint(buf, sizeof buf, "headers %s", bearer_token);
if(write(ctlfd, buf, n = strlen(buf)) != n)
sysfatal("post: write headers: %r");
- snprint(buf, sizeof buf, "contenttype multipart/form-data; boundary=%s", BOUNDARY);
+ snprint(buf, sizeof buf, "contenttype multipart/form-data; boundary=%s",
+ BOUNDARY);
if(write(ctlfd, buf, n = strlen(buf)) != n)
sysfatal("post: write contenttype: %r");
- snprint(buf, sizeof buf, "/mnt/web/%d/postbody", conn);
- if ((bodyfd = open(buf, OWRITE)) < 0)
+ snprint(buf, sizeof buf, "/mnt/web/%d/postbody", conn);
+ if((bodyfd = open(buf, OWRITE)) < 0)
sysfatal("open bodyfd %s: %r", buf);
- /* Write multipart body */
- write(bodyfd, "--", 2);
- write(bodyfd, BOUNDARY, strlen(BOUNDARY));
- write(bodyfd, "\r\n", 2);
+ /* Write multipart body */
+ write(bodyfd, "--", 2);
+ write(bodyfd, BOUNDARY, strlen(BOUNDARY));
+ write(bodyfd, "\r\n", 2);
- multipart_header = esmprint("Content-Disposition: form-data; \
+ multipart_header = esmprint("Content-Disposition: form-data; \
name=\"file\"; \
- filename=\"blob\"\r\nContent-Type: %s\r\n\r\n", mime_type(basename(filepath)));
- write(bodyfd, multipart_header, strlen(multipart_header));
+ filename=\"blob\"\r\nContent-Type: %s\r\n\r\n",
+ mime_type(basename(filepath)));
+ write(bodyfd, multipart_header, strlen(multipart_header));
- write(bodyfd, fa->buf, fa->size);
+ write(bodyfd, fa->buf, fa->size);
- write(bodyfd, "\r\n", 2);
- write(bodyfd, "--", 2);
- write(bodyfd, BOUNDARY, strlen(BOUNDARY));
- write(bodyfd, "--\r\n", 4);
+ write(bodyfd, "\r\n", 2);
+ write(bodyfd, "--", 2);
+ write(bodyfd, BOUNDARY, strlen(BOUNDARY));
+ write(bodyfd, "--\r\n", 4);
- close(bodyfd);
+ close(bodyfd);
/* Response */
snprint(buf, sizeof buf, "/mnt/web/%d/body", conn);
if((bodyfd = open(buf, OREAD)) < 0)
sysfatal("post: opening body: %r");
- if (readn(bodyfd, buf, BUFSIZE) <= 0)
+ if(readn(bodyfd, buf, BUFSIZE) <= 0)
sysfatal("readn: %r");
close(bodyfd);
close(ctlfd);
- return buf;
+ return buf;
}
--- a/masto9.c
+++ b/masto9.c
@@ -10,9 +10,10 @@
UserPasswd *
getcredentials(char *host)
{
- UserPasswd* p;
+ UserPasswd *p;
- p = auth_getuserpasswd(auth_getkey, "proto=pass service=mastodon server=%s", host);
+ p = auth_getuserpasswd(auth_getkey, "proto=pass service=mastodon server=%s",
+ host);
if(p == nil)
sysfatal("masto9: failed to retrieve token: %r");
@@ -22,21 +23,27 @@
void
gethome(char *token, char *host, Toot toots[], char *after)
{
- JSON *obj, *id, *content, *reblog_content, *account, *reblog_account, *handle, *reblog_handle, *display_name, *avatar, *reblog, *media_attachments, *type, *preview_url, *remote_url;
+ JSON *obj, *id, *content, *reblog_content, *account, *reblog_account,
+ *handle, *reblog_handle, *display_name, *avatar, *reblog,
+ *media_attachments, *type, *preview_url, *remote_url;
char *endpoint;
int i = 0;
- if(after != nil) {
- endpoint = esmprint("timelines/home?max_id=%s", after);
- } else {
- endpoint = "timelines/home";
- }
+ if(after != nil)
+ {
+ endpoint = esmprint("timelines/home?max_id=%s", after);
+ }
+ else
+ {
+ endpoint = "timelines/home";
+ }
- obj = mastodonget(token, host, endpoint);
- if (obj->t != JSONArray)
+ obj = mastodonget(token, host, endpoint);
+ if(obj->t != JSONArray)
sysfatal("jsonparse: not an array");
- for(JSONEl *p = obj->first; p != nil; p = p->next) {
+ for(JSONEl *p = obj->first; p != nil; p = p->next)
+ {
JSON *toot_json = p->val;
id = getjsonkey(toot_json, "id");
@@ -51,10 +58,13 @@
Toot *toot = emalloc(sizeof(Toot));
toot->id = estrdup((char *)id->s);
- if(reblog->s == nil) {
+ if(reblog->s == nil)
+ {
toot->reblogged = 0;
toot->content = estrdup((char *)content->s);
- } else {
+ }
+ else
+ {
reblog_content = getjsonkey(reblog, "content");
reblog_account = getjsonkey(reblog, "account");
reblog_handle = getjsonkey(reblog_account, "acct");
@@ -70,9 +80,11 @@
toot->avatar_url = estrdup((char *)avatar->s);
toot->attachments_count = 0;
- if(media_attachments->s != nil) {
+ if(media_attachments->s != nil)
+ {
int j = 0;
- for(JSONEl *at = media_attachments->first; at != nil; at = at->next) {
+ for(JSONEl *at = media_attachments->first; at != nil; at = at->next)
+ {
JSON *attachment_json = at->val;
Attachment *attachment = emalloc(sizeof(Attachment));
type = getjsonkey(attachment_json, "type");
@@ -80,11 +92,14 @@
remote_url = getjsonkey(attachment_json, "remote_url");
attachment->type = estrdup((char *)type->s);
- if(strcmp(type->s, "image") == 0){
- attachment->url = estrdup((char *)preview_url->s);
- } else {
- attachment->url = estrdup((char *)remote_url->s);
- }
+ if(strcmp(type->s, "image") == 0)
+ {
+ attachment->url = estrdup((char *)preview_url->s);
+ }
+ else
+ {
+ attachment->url = estrdup((char *)remote_url->s);
+ }
toot->media_attachments[j] = attachment;
toot->attachments_count++;
@@ -103,33 +118,38 @@
JSON *obj, *id, *content, *display_name, *handle, *type, *account, *status;
int i = 0;
- obj = mastodonget(token, host, "notifications");
+ obj = mastodonget(token, host, "notifications?types[]=mention");
- if (obj->t != JSONArray)
+ if(obj->t != JSONArray)
sysfatal("jsonparse: not an array");
- for(JSONEl *p = obj->first; p != nil; p = p->next) {
+ for(JSONEl *p = obj->first; p != nil; p = p->next)
+ {
JSON *notif_json = p->val;
id = getjsonkey(notif_json, "id");
type = getjsonkey(notif_json, "type");
- if(strcmp(type->s, "follow") != 0) {
- status = getjsonkey(notif_json, "status");
- content = getjsonkey(status, "content");
- } else {
- content = jsonparse("");
- }
- account = getjsonkey(notif_json, "account");
- display_name = getjsonkey(account, "display_name");
- handle = getjsonkey(account, "acct");
+ if(strcmp(type->s, "follow") != 0)
+ {
+ status = getjsonkey(notif_json, "status");
+ content = getjsonkey(status, "content");
+ }
+ else
+ {
+ content = jsonparse("");
+ }
+ account = getjsonkey(notif_json, "account");
+ display_name = getjsonkey(account, "display_name");
+ handle = getjsonkey(account, "acct");
Notification *notif = emalloc(sizeof(Notification));
notif->id = estrdup((char *)id->s);
- notif->type = estrdup((char *)type->s);
- if(strcmp(type->s, "follow") != 0) {
- notif->content = estrdup((char *)content->s);
- }
+ notif->type = estrdup((char *)type->s);
+ if(strcmp(type->s, "follow") != 0)
+ {
+ notif->content = estrdup((char *)content->s);
+ }
notif->display_name = estrdup((char *)display_name->s);
notif->handle = estrdup((char *)handle->s);
@@ -142,136 +162,148 @@
void
posttoot(char *token, char *host, char *text)
{
- char *url;
- url = esmprint("https://%s/api/v1/statuses", host);
+ char *url;
+ url = esmprint("https://%s/api/v1/statuses", host);
httppost(token, url, concat("status=", text));
- print("Posted:\n %s\n", text);
+ print("Posted:\n %s\n", text);
}
char *
tootauthor(char *token, char *host, char *id)
{
- JSON *obj, *account, *reblog;
- char *endpoint, *response;
+ JSON *obj, *account, *reblog;
+ char *endpoint, *response;
- endpoint = concat("statuses/", id);
+ endpoint = concat("statuses/", id);
obj = mastodonget(token, host, endpoint);
- reblog = getjsonkey(obj, "reblog");
- if(reblog->s != nil) {
- account = getjsonkey(reblog, "account");
- } else {
- account = getjsonkey(obj, "account");
- }
+ reblog = getjsonkey(obj, "reblog");
+ if(reblog->s != nil)
+ {
+ account = getjsonkey(reblog, "account");
+ }
+ else
+ {
+ account = getjsonkey(obj, "account");
+ }
- response = estrdup((char *)getjsonkey(account, "acct")->s);
+ response = estrdup((char *)getjsonkey(account, "acct")->s);
- free(account);
- free(reblog);
- free(obj);
+ free(account);
+ free(reblog);
+ free(obj);
- return response;
+ return response;
}
void
postattachment(char *token, char *host, char *text, char *filepath)
{
- JSON *obj, *id;
- char *url, *response, *body;
+ JSON *obj, *id;
+ char *url, *response, *body;
url = esmprint("https://%s/api/v1/media", host);
response = upload(token, url, filepath);
- obj = jsonparse(response);
- if (obj == nil)
+ obj = jsonparse(response);
+ if(obj == nil)
sysfatal("jsonparse: not json");
- print("%J\n", obj);
- id = getjsonkey(obj, "id");
+ print("%J\n", obj);
+ id = getjsonkey(obj, "id");
- url = esmprint("https://%s/api/v1/statuses", host);
+ url = esmprint("https://%s/api/v1/statuses", host);
- if (strlen(text) >0) {
- body = esmprint("status=%s&media_ids[]=%s", text, id->s);
- } else {
- body = esmprint("media_ids[]=%s", id->s);
- }
+ if(strlen(text) > 0)
+ {
+ body = esmprint("status=%s&media_ids[]=%s", text, id->s);
+ }
+ else
+ {
+ body = esmprint("media_ids[]=%s", id->s);
+ }
httppost(token, url, body);
- print("Posted toot\n");
+ print("Posted toot\n");
}
void
perform(char *token, char *host, char *id, char *action)
{
- char *url;
- url = esmprint("%s/api/v1/statuses/%s/%s", host, id, action);
- httppost(token, url, "");
+ char *url;
+ url = esmprint("%s/api/v1/statuses/%s/%s", host, id, action);
+ httppost(token, url, "");
}
void
boost(char *token, char *host, char *id)
{
- perform(token, host, id, "reblog");
- print("Boosted toot.");
+ perform(token, host, id, "reblog");
+ print("Boosted toot.");
}
void
unboost(char *token, char *host, char *id)
{
- perform(token, host, id, "unreblog");
- print("Unboosted toot.");
+ perform(token, host, id, "unreblog");
+ print("Unboosted toot.");
}
void
fav(char *token, char *host, char *id)
{
- perform(token, host, id, "favourite");
- print("Favorited toot.");
+ perform(token, host, id, "favourite");
+ print("Favorited toot.");
}
void
unfav(char *token, char *host, char *id)
{
- perform(token, host, id, "unfavourite");
- print("Unfavorited toot.");
+ perform(token, host, id, "unfavourite");
+ print("Unfavorited toot.");
}
void
reply(char *token, char *host, char *id)
{
- char *content;
- int fd, wait;
- char *s, *t, *u, *url;
- Biobuf body;
+ char *content;
+ int fd, wait;
+ char *s, *t, *u, *url;
+ Biobuf body;
- wait = 0;
- t = nil;
+ wait = 0;
+ t = nil;
- if(wait)
- close(open("/dev/text", OWRITE|OTRUNC|OCEXEC));
- if((fd = open("/dev/consctl", OWRITE|OCEXEC)) >= 0){
- write(fd, "holdon", 6);
- u = tootauthor(token, host, id);
- print("Reply to %s\n", u);
- Binit(&body, 0, OREAD);
- if((s = Brdstr(&body, 0, 1)) != nil)
- t = esmprint("%s", s);
- free(s);
- if(t != nil){
- url = esmprint("%s/api/v1/statuses", host);
- content = esmprint("in_reply_to_id=%s&status=@%s %s", id, u, t);
+ if(wait)
+ close(open("/dev/text", OWRITE | OTRUNC | OCEXEC));
+ if((fd = open("/dev/consctl", OWRITE | OCEXEC)) >= 0)
+ {
+ write(fd, "holdon", 6);
+ u = tootauthor(token, host, id);
+ print("Reply to %s\n", u);
+ Binit(&body, 0, OREAD);
+ if((s = Brdstr(&body, 0, 1)) != nil)
+ t = esmprint("%s", s);
+ free(s);
+ if(t != nil)
+ {
+ url = esmprint("%s/api/v1/statuses", host);
+ content = esmprint("in_reply_to_id=%s&status=@%s %s", id, u, t);
- httppost(token, url, content);
- print("\nReply sent.\n");
- free(t);
- }else{
- fprint(2, "%r\n");
- }
- close(fd);
- }else{
- fprint(2, "%r\n");
- }
+ httppost(token, url, content);
+ print("\nReply sent.\n");
+ free(t);
+ }
+ else
+ {
+ fprint(2, "%r\n");
+ }
+ close(fd);
+ }
+ else
+ {
+ fprint(2, "%r\n");
+ }
}
char *
@@ -282,7 +314,8 @@
if(pipe(wr) == -1 || pipe(rd) == -1)
sysfatal("pipe: %r");
- switch(fork()){
+ switch(fork())
+ {
case -1:
sysfatal("fork: %r");
break;
@@ -328,155 +361,201 @@
response = httpget(token, url);
obj = jsonparse(response);
- if (obj == nil)
+ if(obj == nil)
sysfatal("jsonparse: not json");
- return(obj);
+ return (obj);
}
void
usage(void)
{
- sysfatal("usage: masto9 url");
+ sysfatal("usage: masto9 url");
}
void
displaytoots(Toot toots[], char *server)
{
- Biobuf out;
- Binit(&out, 1, OWRITE);
+ Biobuf out;
+ Binit(&out, 1, OWRITE);
- for (int i=0;i<TOOTSCOUNT;i++) {
- Toot toot = toots[i];
- char *username;
+ for(int i = 0; i < TOOTSCOUNT; i++)
+ {
+ Toot toot = toots[i];
+ char *username;
- username = esmprint("%s (%s)", toot.display_name, toot.handle);
+ username = esmprint("%s (%s)", toot.display_name, toot.handle);
- Bprint(&out, "\n\n——————————\n");
- if(toot.reblogged == 1) {
- Bprint(&out, "⊙ %s retooted %s:\n", username, toot.reblogged_handle);
- } else {
- Bprint(&out, "⊙ %s:\n", username);
- }
- Bprint(&out, "\n%s", fmthtml(cleanup(toot.content)));
- if(toot.attachments_count>0) {
- for (int j=0;j<toot.attachments_count;j++) {
- Attachment *attachment = toot.media_attachments[j];
- Bprint(&out, "\n[%s] %s", attachment->type, attachment->url);
- }
- Bprint(&out, "\n");
- }
- Bprint(&out, "\nReply[%s] | Boost[%s] | Favorite[%s]", toot.id, toot.id, toot.id);
- }
- Bprint(&out, "\n\n\n⇒ Send the next line to load more");
- Bprint(&out, "\n6.out %s more %s\n\n", server, toots[19].id);
- Bflush(&out);
+ Bprint(&out, "\n\n——————————\n");
+ if(toot.reblogged == 1)
+ {
+ Bprint(&out, "⊙ %s retooted %s:\n", username,
+ toot.reblogged_handle);
+ }
+ else
+ {
+ Bprint(&out, "⊙ %s:\n", username);
+ }
+ Bprint(&out, "\n%s", fmthtml(cleanup(toot.content)));
+ if(toot.attachments_count > 0)
+ {
+ for(int j = 0; j < toot.attachments_count; j++)
+ {
+ Attachment *attachment = toot.media_attachments[j];
+ Bprint(&out, "\n[%s] %s", attachment->type, attachment->url);
+ }
+ Bprint(&out, "\n");
+ }
+ Bprint(&out, "\nReply[%s] | Boost[%s] | Favorite[%s]", toot.id, toot.id,
+ toot.id);
+ }
+ Bprint(&out, "\n\n\n⇒ Send the next line to load more");
+ Bprint(&out, "\n6.out %s more %s\n\n", server, toots[19].id);
+ Bflush(&out);
}
void
displaynotifications(Notification notifs[])
{
- Biobuf out;
- Binit(&out, 1, OWRITE);
+ Biobuf out;
+ Binit(&out, 1, OWRITE);
- for (int i=0;i<NOTIFSCOUNT;i++) {
- Notification notif = notifs[i];
- char *username;
+ for(int i = 0; i < NOTIFSCOUNT; i++)
+ {
+ Notification notif = notifs[i];
+ char *username;
- username = esmprint("%s (%s)", notif.display_name, notif.handle);
+ username = esmprint("%s (%s)", notif.display_name, notif.handle);
- if (strcmp(notif.type, "reblog") == 0) {
- Bprint(&out, "\n⊙ %s retooted\n %s", username, fmthtml(cleanup(notif.content)));
- } else if (strcmp(notif.type, "favourite") == 0) {
- Bprint(&out, "\n⊙ %s favorited\n %s", username, fmthtml(cleanup(notif.content)));
- } else if (strcmp(notif.type, "mention") == 0) {
- Bprint(&out, "\n⊙ %s mentioned you\n %s", username, fmthtml(cleanup(notif.content)));
- } else if (strcmp(notif.type, "follow") == 0) {
- Bprint(&out, "\n⊙ %s followed you\n", username);
- } else if (strcmp(notif.type, "poll") == 0) {
- Bprint(&out, "\n⊙ %s poll ended\n %s", username, fmthtml(cleanup(notif.content)));
- }
- }
- Bprint(&out, "\n");
- Bflush(&out);
+ if(strcmp(notif.type, "reblog") == 0)
+ {
+ Bprint(&out, "\n⊙ %s retooted\n %s", username,
+ fmthtml(cleanup(notif.content)));
+ }
+ else if(strcmp(notif.type, "favourite") == 0)
+ {
+ Bprint(&out, "\n⊙ %s favorited\n %s", username,
+ fmthtml(cleanup(notif.content)));
+ }
+ else if(strcmp(notif.type, "mention") == 0)
+ {
+ Bprint(&out, "\n⊙ %s mentioned you\n %s", username,
+ fmthtml(cleanup(notif.content)));
+ }
+ else if(strcmp(notif.type, "follow") == 0)
+ {
+ Bprint(&out, "\n⊙ %s followed you\n", username);
+ }
+ else if(strcmp(notif.type, "poll") == 0)
+ {
+ Bprint(&out, "\n⊙ %s poll ended\n %s", username,
+ fmthtml(cleanup(notif.content)));
+ }
+ }
+ Bprint(&out, "\n");
+ Bflush(&out);
}
void
debug(char *token, char *host, char *id)
{
- JSON *obj;
- char *endpoint;
+ JSON *obj;
+ char *endpoint;
- endpoint = esmprint("statuses/%s", id);
+ endpoint = esmprint("statuses/%s", id);
obj = mastodonget(token, host, endpoint);
- print("%J\n", obj);
- jsonfree(obj);
+ print("%J\n", obj);
+ jsonfree(obj);
}
-
-//echo 'proto=pass service=mastodon server=instanceHostName pass=yourToken user=yourUsername' > /mnt/factotum/ctl
+// echo 'proto=pass service=mastodon server=instanceHostName pass=yourToken
+// user=yourUsername' > /mnt/factotum/ctl
void
-main(int argc, char**argv)
+main(int argc, char **argv)
{
- UserPasswd *p;
- char *token, *host, *command, *text, *id, *filepath;
+ UserPasswd *p;
+ char *token, *host, *command, *text, *id, *filepath;
- if(argc < 2)
+ if(argc < 2)
usage();
- JSONfmtinstall();
+ JSONfmtinstall();
- host = argv[1];
- command = argv[2];
+ host = argv[1];
+ command = argv[2];
- p = getcredentials(host);
+ p = getcredentials(host);
token = p->passwd;
- if(command == nil) {
- Toot toots[TOOTSCOUNT];
- gethome(token, host, toots, nil);
- displaytoots(toots, host);
- } else if(strcmp(command, "toot") == 0) {
- text = argv[3];
- posttoot(token, host, text);
- } else if(strcmp(command, "tootfile") == 0) {
- if (argc > 4) {
- text = argv[3];
- filepath = argv[4];
- } else {
- text = "";
- filepath = argv[3];
- }
- postattachment(token, host, text, filepath);
- } else if(strcmp(command, "fav") == 0) {
- id = argv[3];
- fav(token, host, id);
- } else if(strcmp(command, "unfav") == 0) {
- id = argv[3];
- unfav(token, host, id);
- } else if(strcmp(command, "boost") == 0) {
- id = argv[3];
- boost(token, host, id);
- } else if(strcmp(command, "unboost") == 0) {
- id = argv[3];
- unboost(token, host, id);
- } else if(strcmp(command, "reply") == 0) {
- id = argv[3];
- reply(token, host, id);
- } else if(strcmp(command, "debug") == 0) {
- id = argv[3];
- debug(token, host, id);
- } else if(strcmp(command, "more") == 0) {
- id = argv[3];
- Toot toots[TOOTSCOUNT];
- gethome(token, host, toots, id);
- displaytoots(toots, host);
- } else if(strcmp(command, "notifications") == 0) {
- Notification notifs[NOTIFSCOUNT];
- getnotifications(token, host, notifs);
- displaynotifications(notifs);
- }
+ if(command == nil)
+ {
+ Toot toots[TOOTSCOUNT];
+ gethome(token, host, toots, nil);
+ displaytoots(toots, host);
+ }
+ else if(strcmp(command, "toot") == 0)
+ {
+ text = argv[3];
+ posttoot(token, host, text);
+ }
+ else if(strcmp(command, "tootfile") == 0)
+ {
+ if(argc > 4)
+ {
+ text = argv[3];
+ filepath = argv[4];
+ }
+ else
+ {
+ text = "";
+ filepath = argv[3];
+ }
+ postattachment(token, host, text, filepath);
+ }
+ else if(strcmp(command, "fav") == 0)
+ {
+ id = argv[3];
+ fav(token, host, id);
+ }
+ else if(strcmp(command, "unfav") == 0)
+ {
+ id = argv[3];
+ unfav(token, host, id);
+ }
+ else if(strcmp(command, "boost") == 0)
+ {
+ id = argv[3];
+ boost(token, host, id);
+ }
+ else if(strcmp(command, "unboost") == 0)
+ {
+ id = argv[3];
+ unboost(token, host, id);
+ }
+ else if(strcmp(command, "reply") == 0)
+ {
+ id = argv[3];
+ reply(token, host, id);
+ }
+ else if(strcmp(command, "debug") == 0)
+ {
+ id = argv[3];
+ debug(token, host, id);
+ }
+ else if(strcmp(command, "more") == 0)
+ {
+ id = argv[3];
+ Toot toots[TOOTSCOUNT];
+ gethome(token, host, toots, id);
+ displaytoots(toots, host);
+ }
+ else if(strcmp(command, "notifications") == 0)
+ {
+ Notification notifs[NOTIFSCOUNT];
+ getnotifications(token, host, notifs);
+ displaynotifications(notifs);
+ }
- free(p);
+ free(p);
exits(nil);
}
--- a/util.c
+++ b/util.c
@@ -31,7 +31,7 @@
return v;
}
-char*
+char *
estrdup(char *s)
{
s = strdup(s);
@@ -44,15 +44,15 @@
char *
concat(char *s1, char *s2)
{
- char *result;
- result = emalloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
+ char *result;
+ result = emalloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
- strcpy(result, s1);
- strcat(result, s2);
- return result;
+ strcpy(result, s1);
+ strcat(result, s2);
+ return result;
}
-char*
+char *
esmprint(char *fmt, ...)
{
char *s;
@@ -67,7 +67,7 @@
return s;
}
-char*
+char *
fslurp(int fd, int *nbuf)
{
int n, sz, r;
@@ -76,7 +76,8 @@
n = 0;
sz = 128;
buf = emalloc(sz);
- while(1){
+ while(1)
+ {
r = read(fd, buf + n, sz - n);
if(r == 0)
break;
@@ -83,8 +84,9 @@
if(r == -1)
goto error;
n += r;
- if(n == sz){
- sz += sz/2;
+ if(n == sz)
+ {
+ sz += sz / 2;
buf = erealloc(buf, sz);
}
}
@@ -100,30 +102,35 @@
void
removesubstring(char *str, char *sub)
{
- int len = strlen(sub);
+ int len = strlen(sub);
- while ((str = strstr(str, sub))) {
- memmove(str, str + len, strlen(str + len) + 1);
- }
+ while((str = strstr(str, sub)))
+ {
+ memmove(str, str + len, strlen(str + len) + 1);
+ }
}
void
removetag(char *str, char *tag)
{
- char *start = strstr(str, tag);
+ char *start = strstr(str, tag);
- while (start) {
- char *end = strchr(start, '>');
+ while(start)
+ {
+ char *end = strchr(start, '>');
- if (end) {
- memmove(start, end + 1, strlen(end + 1) + 1);
- } else {
- *start = '\0';
- break;
- }
+ if(end)
+ {
+ memmove(start, end + 1, strlen(end + 1) + 1);
+ }
+ else
+ {
+ *start = '\0';
+ break;
+ }
- start = strstr(start, tag);
- }
+ start = strstr(start, tag);
+ }
}
JSON *
@@ -130,7 +137,7 @@
getjsonkey(JSON *obj, char *key)
{
JSON *value = jsonbyname(obj, key);
- if (value == nil)
+ if(value == nil)
sysfatal("jsonbyname: key %s not found in %J", key, obj);
return value;
}
@@ -138,40 +145,42 @@
FileAttachment *
readfile(char *filename)
{
- int fd, nread, size = 0, bufsize = 1024;
- FileAttachment *fa = emalloc(sizeof(FileAttachment));
- char *buf = malloc(bufsize);
- if (!buf)
- sysfatal("malloc failed");
+ int fd, nread, size = 0, bufsize = 1024;
+ FileAttachment *fa = emalloc(sizeof(FileAttachment));
+ char *buf = malloc(bufsize);
+ if(!buf)
+ sysfatal("malloc failed");
- fd = open(filename, OREAD);
- if (fd < 0)
- sysfatal("open %s: %r", filename);
+ fd = open(filename, OREAD);
+ if(fd < 0)
+ sysfatal("open %s: %r", filename);
- while ((nread = read(fd, buf + size, bufsize - size)) > 0) {
- size += nread;
- if (size == bufsize) {
- bufsize *= 2;
- buf = realloc(buf, bufsize);
- if (!buf)
- sysfatal("realloc failed");
- }
- }
- close(fd);
+ while((nread = read(fd, buf + size, bufsize - size)) > 0)
+ {
+ size += nread;
+ if(size == bufsize)
+ {
+ bufsize *= 2;
+ buf = realloc(buf, bufsize);
+ if(!buf)
+ sysfatal("realloc failed");
+ }
+ }
+ close(fd);
- if (nread < 0)
- sysfatal("read %s: %r", filename);
+ if(nread < 0)
+ sysfatal("read %s: %r", filename);
- buf[size] = '\0';
+ buf[size] = '\0';
- fa->buf = buf;
- fa->size = size;
- return fa;
+ fa->buf = buf;
+ fa->size = size;
+ return fa;
}
char *
basename(char *path)
{
- char *base = strrchr(path, '/');
- return base ? base + 1 : path;
+ char *base = strrchr(path, '/');
+ return base ? base + 1 : path;
}