shithub: masto9

ref: 25ce6ea9a2e96fc6c056ed752561475a90fc0a05
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"

char *URL = "https://fedi.9til.de/api/v1/timelines/home";
char *POSTURL = "https://fedi.9til.de/api/v1/statuses";
char *NOTIFICATIONSURL = "https://fedi.9til.de/api/v1/notifications";

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

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

	return p;
}

static JSON *
get_json_key(JSON *obj, char *key)
{
	JSON *value = jsonbyname(obj, key);
	if (value == nil)
		sysfatal("jsonbyname: key %s not found in %J", key, obj);
	return value;
}

void
get_timeline(char *token, 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;
	char *response, *tl_url;
	int i = 0;

  tl_url = emalloc(sizeof(char)*1024);
  if(after != nil) {
    snprintf(tl_url, 1024, "%s?max_id=%s", URL, after);
  } else {
    tl_url = URL;
  }
	response = httpget(token, tl_url);

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

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

		id = get_json_key(toot_json, "id");
		content = get_json_key(toot_json, "content");
		account = get_json_key(toot_json, "account");
		handle = get_json_key(account, "acct");
		display_name = get_json_key(account, "display_name");
		avatar = get_json_key(account, "avatar_static");
		reblog = get_json_key(toot_json, "reblog");
		media_attachments = get_json_key(toot_json, "media_attachments");

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

		if(reblog->s == nil) {
			toot.reblogged = 0;
			toot.content = strdup((char *)content->s);
		} else {
			reblog_content = get_json_key(reblog, "content");
			reblog_account = get_json_key(reblog, "account");
			reblog_handle = get_json_key(reblog_account, "acct");

			toot.content = strdup((char *)reblog_content->s);
			toot.reblogged_handle = strdup((char *)reblog_handle->s);
			toot.reblogged = 1;

			media_attachments = get_json_key(reblog, "media_attachments");
		};
		toot.handle = strdup((char *)handle->s);
		toot.display_name = strdup((char *)display_name->s);
		toot.avatar_url = strdup((char *)avatar->s);
		toot.attachments_count = 0;

		if(media_attachments->s != nil) {
			int j = 0;
			for(JSONEl *at = media_attachments->first; at != nil; at = at->next) {
				JSON *attachment_json = at->val;
				Attachment attachment = emalloc(sizeof(Attachment));
				type = get_json_key(attachment_json, "type");
				preview_url = get_json_key(attachment_json, "preview_url");
				remote_url = get_json_key(attachment_json, "remote_url");
				attachment.type = strdup((char *)type->s);

        if(strcmp(type->s, "image") == 0){
          attachment.url = strdup((char *)preview_url->s);
        } else {
          attachment.url = strdup((char *)remote_url->s);
        }

				toot.media_attachments[j] = attachment;
				toot.attachments_count++;
				j++;
			}
		}
		toots[i] = toot;
		i++;
	}
	jsonfree(obj);
}

void
get_notifications(char *token, Notification notifs[])
{
	JSON *obj, *id, *content, *display_name, *handle, *type, *account, *status;
	char *response;
	int i = 0;

	response = httpget(token, NOTIFICATIONSURL);

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

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

		id = get_json_key(notif_json, "id");
		type = get_json_key(notif_json, "type");
    if(strcmp(type->s, "follow") != 0) {
      status = get_json_key(notif_json, "status");
      content = get_json_key(status, "content");
    }
    account = get_json_key(notif_json, "account");
    display_name = get_json_key(account, "display_name");
    handle = get_json_key(account, "acct");

		Notification notif = emalloc(sizeof(Notification));
		notif.id = strdup((char *)id->s);

    notif.type = strdup((char *)type->s);
    if(strcmp(type->s, "follow") != 0) {
      notif.content = strdup((char *)content->s);
    }
		notif.display_name = strdup((char *)display_name->s);
		notif.handle = strdup((char *)handle->s);

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

void
post_toot(char *token, char *text)
{
	httppost(token, POSTURL, concat("status=", text));
  print("Posted:\n %s\n", text);
}

void
action_toot(char *token, char *id, char *action)
{
  char *response;
  char url[1024];
  snprintf(url, sizeof(url), "%s/%s/%s", POSTURL, id, action);
  print("URL %s\n", url);
  response = httppost(token, url, "");
  print("Response:\n %s\n", response);
}

void
boost_toot(char *token, char *id)
{
  action_toot(token, id, "reblog");
}

void
unboost_toot(char *token, char *id)
{
  action_toot(token, id, "unreblog");
}

void
fav_toot(char *token, char *id)
{
  action_toot(token, id, "favourite");
}

void
unfav_toot(char *token, char *id)
{
  action_toot(token, id, "unfavourite");
}

void
reply_toot(char *token, char *id)
{
  char content[TOOTBUFSIZE];
  int fd, wait;
  char *s, *t;
  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);
    print("Reply\n");
    Binit(&body, 0, OREAD);
    if((s = Brdstr(&body, 0, 1)) != nil)
      t = smprint("%s", s);
    free(s);
    if(t != nil){
      snprintf(content, TOOTBUFSIZE, "in_reply_to_id=%s&status=%s", id, t);
      httppost(token, POSTURL, content);
      print("\nReplied!\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("pipe: %r");
	switch(fork()){
	case -1:
		sysfatal("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("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("read: %r\n");
		buf[n] = 0;
		return buf;
	}
	return buf;
}

char *
cleanup(char *str)
{
	remove_tag(str, "<span");
	remove_substring(str, "</span>");

	return str;
}

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

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

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

	return(obj);
}

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

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

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

    username = emalloc(sizeof(char)*256);
    snprintf(username, 256, "%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);
}

void
display_notifications(Notification notifs[])
{
  Biobuf out;
  Binit(&out, 1, OWRITE);

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

    username = emalloc(sizeof(char)*256);
    snprintf(username, 256, "%s (%s)", notif.display_name, notif.handle);

    if (strcmp(notif.type, "reblog") == 0) {
      Bprint(&out, "⊙ %s retooted\n %s\n", username, fmthtml(cleanup(notif.content)));
    } else if (strcmp(notif.type, "favourite") == 0) {
      Bprint(&out, "⊙ %s favorited\n %s\n", username, fmthtml(cleanup(notif.content)));
    } else if (strcmp(notif.type, "mention") == 0) {
      Bprint(&out, "⊙ %s mentioned you\n %s\n", username, fmthtml(cleanup(notif.content)));
    } else if (strcmp(notif.type, "follow") == 0) {
      Bprint(&out, "⊙ %s followed you\n\n", username);
    } else if (strcmp(notif.type, "poll") == 0) {
      Bprint(&out, "⊙ %s poll ended\n %s\n", username, fmthtml(cleanup(notif.content)));
    }
  }
  Bflush(&out);
}

//echo 'proto=pass service=mastodon server=instanceHostName pass=yourToken user=yourUsername' > /mnt/factotum/ctl
void
main(int argc, char**argv)
{
  UserPasswd *p;
  char *token, *server, *command, *text, *id;

  if(argc < 2)
		usage();

  JSONfmtinstall();

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

  p = getcredentials(server);
	token = p->passwd;

  if(command == nil) {
    get_timeline(token, toots, nil);
    display_toots(toots, server);
  } else if(strcmp(command, "toot") == 0) {
    text = argv[3];
    post_toot(token, text);
  } else if(strcmp(command, "fav") == 0) {
    id = argv[3];
    fav_toot(token, id);
  } else if(strcmp(command, "unfav") == 0) {
    id = argv[3];
    unfav_toot(token, id);
  } else if(strcmp(command, "boost") == 0) {
    id = argv[3];
    boost_toot(token, id);
  } else if(strcmp(command, "unboost") == 0) {
    id = argv[3];
    unboost_toot(token, id);
  } else if(strcmp(command, "reply") == 0) {
    id = argv[3];
    reply_toot(token, id);
  } else if(strcmp(command, "more") == 0) {
    id = argv[3];
    get_timeline(token, toots, id);
    display_toots(toots, server);
  } else if(strcmp(command, "notifications") == 0) {
    get_notifications(token, notifs);
    display_notifications(notifs);
  }

	exits(nil);
}