shithub: masto9

ref: fd3391f84165bf8d637bec678142f07044d52f1d
dir: /display.c/

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

#include "masto9.h"

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

	return str;
}

static 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;
}

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");
		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, "\n→ Reply[%s] ∙ Boost[%s] ∙ Favorite[%s]\n", toot.id, toot.id,
			   toot.id);
	}
	Bprint(&out, "\n\n⇒ Send the next line to load more");
	Bprint(&out, "\nmasto9 %s more %s\n\n", server, toots[TOOTSCOUNT - 1].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;

		if(strlen(notif.displayname) > 0) {
			username = esmprint("%s (%s)", notif.displayname, notif.handle);
		} else {
			username = notif.handle;
		}

		Bprint(&out, "\n——————————\n");
		if(strcmp(notif.type, "reblog") == 0) {
			Bprint(&out, "∞ %s retooted:\n\n%s", username,
				   fmthtml(cleanup(notif.content)));
		} else if(strcmp(notif.type, "favourite") == 0) {
			Bprint(&out, "★ %s favorited:\n\n%s", username,
				   fmthtml(cleanup(notif.content)));
		} else if(strcmp(notif.type, "mention") == 0) {
			Bprint(&out, "⊙ %s mentioned you:\n\n%s", username,
				   fmthtml(cleanup(notif.content)));
			Bprint(&out, "\n→ Reply[%s] ∙ Boost[%s] ∙ Favorite[%s]\n",
				   notif.statusid, notif.statusid, notif.statusid);
		} else if(strcmp(notif.type, "follow") == 0) {
			Bprint(&out, "↔ %s followed you.\n", username);
		} else if(strcmp(notif.type, "poll") == 0) {
			Bprint(&out, "∴ %s poll ended:\n\n%s", username,
				   fmthtml(cleanup(notif.content)));
		}
	}
	Bprint(&out, "\n\n");
	Bflush(&out);
}