ref: 45930f4521a779916b87d051065a8d2b751a7c12
parent: 402ed456dc7a1bf16dbcf79e13a566dd57761bf7
author: Julien Blanchard <jblanchard@makemusic.com>
date: Tue Mar 28 14:47:54 EDT 2023
extract display functions
--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,2 @@
+*.6
+6.out
--- a/masto9.c
+++ b/masto9.c
@@ -3,6 +3,7 @@
#include <stdio.h>
#include <json.h>
#include <auth.h>
+#include <bio.h>
#include "masto9.h"
@@ -22,7 +23,6 @@
return p;
}
-
static JSON *
get_json_key(JSON *obj, char *key)
{
@@ -33,13 +33,19 @@
}
void
-get_timeline(char *token, Toot toots[])
+get_timeline(char *token, Toot toots[], char *after)
{
- JSON *obj, *id, *content, *reblog_content, *account, *reblog_account, *username, *reblog_username, *display_name, *avatar, *reblog, *media_attachments, *type, *url;
- char *response;
+ 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;
- response = httpget(token, URL);
+ 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)
@@ -53,7 +59,7 @@
id = get_json_key(toot_json, "id");
content = get_json_key(toot_json, "content");
account = get_json_key(toot_json, "account");
- username = get_json_key(account, "username");
+ 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");
@@ -68,15 +74,15 @@
} else {
reblog_content = get_json_key(reblog, "content");
reblog_account = get_json_key(reblog, "account");
- reblog_username = get_json_key(reblog_account, "username");
+ reblog_handle = get_json_key(reblog_account, "acct");
toot.content = strdup((char *)reblog_content->s);
- toot.reblogged_username = strdup((char *)reblog_username->s);
+ toot.reblogged_handle = strdup((char *)reblog_handle->s);
toot.reblogged = 1;
media_attachments = get_json_key(reblog, "media_attachments");
};
- toot.username = strdup((char *)username->s);
+ 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;
@@ -87,15 +93,21 @@
JSON *attachment_json = at->val;
Attachment attachment = emalloc(sizeof(Attachment));
type = get_json_key(attachment_json, "type");
- url = get_json_key(attachment_json, "preview_url");
+ preview_url = get_json_key(attachment_json, "preview_url");
+ remote_url = get_json_key(attachment_json, "remote_url");
attachment.type = strdup((char *)type->s);
- attachment.url = strdup((char *)url->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++;
}
@@ -103,9 +115,9 @@
}
void
-get_notifications(char *token, Notification toots[])
+get_notifications(char *token, Notification notifs[])
{
- JSON *obj, *id, *content, *username, *type, *account, *status;
+ JSON *obj, *id, *content, *display_name, *handle, *type, *account, *status;
char *response;
int i = 0;
@@ -118,26 +130,29 @@
sysfatal("jsonparse: not an array");
for(JSONEl *p = obj->first; p != nil; p = p->next) {
- JSON *toot_json = p->val;
- content = emalloc(JSONNull);
+ JSON *notif_json = p->val;
- id = get_json_key(toot_json, "id");
- type = get_json_key(toot_json, "type");
+ id = get_json_key(notif_json, "id");
+ type = get_json_key(notif_json, "type");
if(strcmp(type->s, "follow") != 0) {
- status = get_json_key(toot_json, "status");
+ status = get_json_key(notif_json, "status");
content = get_json_key(status, "content");
}
- account = get_json_key(toot_json, "account");
- username = get_json_key(account, "username");
+ account = get_json_key(notif_json, "account");
+ display_name = get_json_key(account, "display_name");
+ handle = get_json_key(account, "acct");
- Notification toot = emalloc(sizeof(Notification));
- toot.id = strdup((char *)id->s);
+ Notification notif = emalloc(sizeof(Notification));
+ notif.id = strdup((char *)id->s);
- toot.type = strdup((char *)type->s);
- toot.content = strdup((char *)content->s);
- toot.username = strdup((char *)username->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);
- toots[i] = toot;
+ notifs[i] = notif;
i++;
}
jsonfree(obj);
@@ -186,11 +201,37 @@
}
void
-reply_toot(char *token, char *id, char *text)
+reply_toot(char *token, char *id)
{
char content[TOOTBUFSIZE];
- snprintf(content, TOOTBUFSIZE, "in_reply_to_id=%s&status=%s", id, text);
- httppost(token, POSTURL, content);
+ 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 *
@@ -261,6 +302,68 @@
sysfatal("usage: masto9 url");
}
+void
+display_toots(Toot toots[])
+{
+ 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");
+ Bprint(&out, "\nMore[%s]\n\n", 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=julienxx' > /mnt/factotum/ctl
void
main(int argc, char**argv)
@@ -280,27 +383,8 @@
token = p->passwd;
if(command == nil) {
- get_timeline(token, toots);
- for (int i=0;i<TOOTS_COUNT;i++) {
- Toot toot = toots[i];
-
- print("\n\n——————————————————————————————————————————————————\n");
- if(toot.reblogged == 1) {
- print("⊙ %s retooted %s:\n", toot.username, toot.reblogged_username);
- } else {
- print("⊙ %s:\n", toot.username);
- }
- print("\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];
- print("\n[%s] %s", attachment.type, attachment.url);
- }
- print("\n");
- }
- print("\nReply[%s] | Boost[%s] | Favorite[%s]", toot.id, toot.id, toot.id);
- }
- print("\n");
+ get_timeline(token, toots, nil);
+ display_toots(toots);
} else if(strcmp(command, "toot") == 0) {
text = argv[3];
post_toot(token, text);
@@ -318,25 +402,14 @@
unboost_toot(token, id);
} else if(strcmp(command, "reply") == 0) {
id = argv[3];
- reply_toot(token, id, "@sirjofri@mastodon.sdf.org now this should be a proper reply");
+ reply_toot(token, id);
+ } else if(strcmp(command, "more") == 0) {
+ id = argv[3];
+ get_timeline(token, toots, id);
+ display_toots(toots);
} else if(strcmp(command, "notifications") == 0) {
get_notifications(token, notifs);
-
- for (int i=0;i<NOTIFS_COUNT;i++) {
- Notification notif = notifs[i];
-
- if (strcmp(notif.type, "reblog") == 0) {
- print("⊙ %s retooted\n %s\n", notif.username, fmthtml(cleanup(notif.content)));
- } else if (strcmp(notif.type, "favourite") == 0) {
- print("⊙ %s favorited\n %s\n", notif.username, fmthtml(cleanup(notif.content)));
- } else if (strcmp(notif.type, "mention") == 0) {
- print("⊙ %s mentioned you\n %s\n", notif.username, fmthtml(cleanup(notif.content)));
- } else if (strcmp(notif.type, "follow") == 0) {
- print("⊙ %s followed you\n", notif.username);
- } else if (strcmp(notif.type, "poll") == 0) {
- print("⊙ %s poll ended\n %s\n", notif.username, fmthtml(cleanup(notif.content)));
- }
- }
+ display_notifications(notifs);
}
exits(nil);
--- a/masto9.h
+++ b/masto9.h
@@ -8,7 +8,8 @@
typedef struct Notification {
char *id;
char *type;
- char *username;
+ char *handle;
+ char *display_name;
char *content;
} Notification;
@@ -15,16 +16,23 @@
typedef struct Toot {
char *id;
char *content;
- char *username;
+ char *handle;
char *display_name;
char *avatar_url;
char *in_reply_to_account_id;
int reblogged;
- char *reblogged_username;
+ char *reblogged_handle;
Attachment media_attachments[10];
int attachments_count;
} Toot;
+typedef struct {
+ char *s1;
+ char *s2;
+} Str2;
+
+#pragma varargck type "E" Str2
+
enum {
TOOTBUFSIZE = 8192,
TLBUFSIZE = 512000,
@@ -34,7 +42,7 @@
};
Toot toots[TOOTS_COUNT];
-Notification notifs[TOOTS_COUNT];
+Notification notifs[40];
/* http */
char *httpget(char *token, char *url);