shithub: masto9

ref: 8d24acf30776c1bee6b09af285d417bc66d87953
dir: /masto9.c/

View raw version
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include <json.h>
#include <auth.h>
#include <bio.h>

#include "masto9.h"

UserPasswd *
getcredentials(char *host)
{
	UserPasswd *p;

	if((p = auth_getuserpasswd(
			auth_getkey, "proto=pass service=mastodon server=%s", host)) == nil)
		sysfatal("getcredentials: failed to retrieve token: %r");

	return p;
}

void
gethome(char *token, char *host, Toot toots[], char *beforeid)
{
	JSON *obj, *id, *content, *reblogcontent, *account, *reblogaccount,
		*handle, *rebloghandle, *displayname, *avatar, *reblog,
		*mediaattachments, *type, *previewurl, *remoteurl;
	char *endpoint;
	int i = 0;

	if(beforeid != nil) {
		endpoint = esmprint("timelines/home?max_id=%s", beforeid);
	} else {
		endpoint = "timelines/home";
	}

	obj = mastodonget(token, host, endpoint);
	if(obj->t != JSONArray)
		sysfatal("gethome: jsonparse: not an array");

	for(JSONEl *p = obj->first; p != nil; p = p->next) {
		JSON *tootjson = p->val;

		id = getjsonkey(tootjson, "id");
		content = getjsonkey(tootjson, "content");
		account = getjsonkey(tootjson, "account");
		handle = getjsonkey(account, "acct");
		displayname = getjsonkey(account, "display_name");
		avatar = getjsonkey(account, "avatar_static");
		reblog = getjsonkey(tootjson, "reblog");
		mediaattachments = getjsonkey(tootjson, "media_attachments");

		Toot *toot = emalloc(sizeof(Toot));
		toot->id = estrdup((char *)id->s);

		if(reblog->s == nil) {
			toot->reblogged = 0;
			toot->content = estrdup((char *)content->s);
		} else {
			reblogcontent = getjsonkey(reblog, "content");
			reblogaccount = getjsonkey(reblog, "account");
			rebloghandle = getjsonkey(reblogaccount, "acct");

			toot->content = estrdup((char *)reblogcontent->s);
			toot->rebloggedhandle = estrdup((char *)rebloghandle->s);
			toot->reblogged = 1;

			mediaattachments = getjsonkey(reblog, "media_attachments");
		};
		toot->handle = estrdup((char *)handle->s);
		toot->displayname = estrdup((char *)displayname->s);
		toot->avatarurl = estrdup((char *)avatar->s);
		toot->attachmentscount = 0;

		if(mediaattachments->s != nil) {
			int j = 0;
			for(JSONEl *at = mediaattachments->first; at != nil;
				at = at->next) {
				JSON *attachmentjson = at->val;
				Attachment *attachment = emalloc(sizeof(Attachment));
				type = getjsonkey(attachmentjson, "type");
				previewurl = getjsonkey(attachmentjson, "preview_url");
				remoteurl = getjsonkey(attachmentjson, "remote_url");
				attachment->type = estrdup((char *)type->s);

				if(strcmp(type->s, "image") == 0) {
					attachment->url = estrdup((char *)previewurl->s);
				} else {
					attachment->url = estrdup((char *)remoteurl->s);
				}

				toot->mediaattachments[j] = attachment;
				toot->attachmentscount++;
				j++;
			}
		}
		toots[i] = *toot;
		i++;
	}
	jsonfree(obj);
}

void
getnotifications(char *token, char *host, Notification *notifs, char *filter)
{
	JSON *obj, *id, *content, *displayname, *handle, *type, *account, *status;
	char *endpoint;
	int i = 0;

	if(strlen(filter) > 0) {
		endpoint = esmprint("notifications?types[]=%s", filter);
	} else {
		endpoint = "notifications";
	}
	obj = mastodonget(token, host, endpoint);

	if(obj->t != JSONArray)
		sysfatal("getnotifications: jsonparse: not an array");

	for(JSONEl *p = obj->first; p != nil; p = p->next) {
		JSON *notifjson = p->val;

		id = getjsonkey(notifjson, "id");
		type = getjsonkey(notifjson, "type");
		if(strcmp(type->s, "follow") != 0) {
			status = getjsonkey(notifjson, "status");
			content = getjsonkey(status, "content");
		} else {
			content = jsonparse("");
		}
		account = getjsonkey(notifjson, "account");
		displayname = 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->displayname = estrdup((char *)displayname->s);
		notif->handle = estrdup((char *)handle->s);

		notifs[i] = *notif;
		i++;
	}
	jsonfree(obj);
}

void
posttoot(char *token, char *host, char *text)
{
	char *url;
	url = esmprint("https://%s/api/v1/statuses", host);

	httppost(token, url, esmprint("status=%s", text));
	print("Posted:\n %s\n", text);
}

char *
tootauthor(char *token, char *host, char *id)
{
	JSON *obj, *account, *reblog;
	char *endpoint, *response;

	endpoint = esmprint("statuses/%s", id);
	obj = mastodonget(token, host, endpoint);

	reblog = getjsonkey(obj, "reblog");
	if(reblog->s != nil) {
		account = getjsonkey(reblog, "account");
	} else {
		account = getjsonkey(obj, "account");
	}

	response = estrdup((char *)getjsonkey(account, "acct")->s);

	free(account);
	free(reblog);
	free(obj);

	return response;
}

void
postattachment(char *token, char *host, char *text, char *filepath)
{
	JSON *obj, *id;
	char *url, *response, *body;

	url = esmprint("https://%s/api/v1/media", host);
	response = upload(token, url, filepath);

	if((obj = jsonparse(response)) == nil)
		sysfatal("postattachment: jsonparse: not json");

	id = getjsonkey(obj, "id");

	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);
	}
	httppost(token, url, body);
	print("Posted toot\n");
}

void
perform(char *token, char *host, char *id, char *action)
{
	char *url;
	url = esmprint("https://%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.");
}

void
unboost(char *token, char *host, char *id)
{
	perform(token, host, id, "unreblog");
	print("Unboosted toot.");
}

void
fav(char *token, char *host, char *id)
{
	perform(token, host, id, "favourite");
	print("Favorited toot.");
}

void
unfav(char *token, char *host, char *id)
{
	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;

	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);

			httppost(token, url, content);
			print("\nReply sent.\n");
			free(t);
		} else {
			fprint(2, "%r\n");
		}
		close(fd);
	} else {
		fprint(2, "%r\n");
	}
}

char *
fmthtml(char *msg)
{
	int wr[2], rd[2], n;
	char buf[TOOTBUFSIZE];

	if(pipe(wr) == -1 || pipe(rd) == -1)
		sysfatal("fmthtml: pipe: %r");
	switch(fork()) {
	case -1:
		sysfatal("fmthtml: fork: %r");
		break;
	case 0:
		close(wr[0]);
		close(rd[1]);
		dup(wr[1], 0);
		dup(rd[0], 1);
		execl("/bin/htmlfmt", "htmlfmt -cutf-8 -j", nil);
		sysfatal("fmthtml: exec: %r");
		break;
	default:
		close(wr[1]);
		close(rd[0]);
		write(wr[0], msg, strlen(msg));
		close(wr[0]);
		n = readn(rd[1], buf, sizeof(buf));
		close(rd[1]);
		if(n == -1)
			sysfatal("fmthtml: read: %r\n");
		buf[n] = 0;
		return buf;
	}
	return buf;
}

char *
cleanup(char *str)
{
	removetag(str, "<span");
	removesubstring(str, "</span>");

	return str;
}

JSON *
mastodonget(char *token, char *host, char *endpoint)
{
	JSON *obj;
	char *response, *url;

	url = esmprint("https://%s/api/v1/%s", host, endpoint);
	response = httpget(token, url);

	if((obj = jsonparse(response)) == nil)
		sysfatal("mastodonget: jsonparse: not json");

	return (obj);
}

void
usage(void)
{
	sysfatal("usage: masto9 url");
}

void
displaytoots(Toot toots[], char *server)
{
	Biobuf out;
	Binit(&out, 1, OWRITE);

	for(int i = 0; i < TOOTSCOUNT; i++) {
		Toot toot = toots[i];
		char *username;

		username = esmprint("%s (%s)", toot.displayname, toot.handle);

		Bprint(&out, "\n\n——————————\n");
		if(toot.reblogged == 1) {
			Bprint(&out, "⊙ %s retooted %s:\n", username,
				   toot.rebloggedhandle);
		} else {
			Bprint(&out, "⊙ %s:\n", username);
		}
		Bprint(&out, "\n%s", fmthtml(cleanup(toot.content)));
		if(toot.attachmentscount > 0) {
			for(int j = 0; j < toot.attachmentscount; j++) {
				Attachment *attachment = toot.mediaattachments[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);

	for(int i = 0; i < NOTIFSCOUNT; i++) {
		Notification notif = notifs[i];
		char *username;

		username = esmprint("%s (%s)", notif.displayname, 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);
}

void
debug(char *token, char *host, char *id)
{
	JSON *obj;
	char *endpoint;

	endpoint = esmprint("statuses/%s", id);
	obj = mastodonget(token, host, endpoint);
	print("%J\n", obj);
	jsonfree(obj);
}

void
main(int argc, char **argv)
{
	UserPasswd *p;
	char *token, *host, *command, *text, *id, *filepath;

	if(argc < 2)
		usage();

	JSONfmtinstall();

	host = argv[1];
	command = argv[2];

	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);
	} else if(strcmp(command, "mentions") == 0) {
		Notification notifs[NOTIFSCOUNT];
		getnotifications(token, host, notifs, "mention");
		displaynotifications(notifs);
	}

	free(p);
	exits(nil);
}