shithub: masto9

Download patch

ref: 6c7016b5b7119d833af543fe5ad707b1b0db5c7e
parent: d7047b022e63c014469877be1faa54a977e51e89
author: Julien Blanchard <jblanchard@makemusic.com>
date: Fri Mar 31 13:48:23 EDT 2023

Use esmprint, move getjsonkey to util

--- a/masto9.c
+++ b/masto9.c
@@ -19,15 +19,6 @@
 	return p;
 }
 
-static JSON *
-getjsonkey(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
 gethome(char *token, char *host, Toot toots[], char *after)
 {
@@ -35,9 +26,8 @@
 	char *endpoint;
 	int i = 0;
 
-  endpoint = emalloc(sizeof(char)*1024);
   if(after != nil) {
-    snprintf(endpoint, 1024, "timelines/home?max_id=%s", after);
+    endpoint = esmprint("timelines/home?max_id=%s", after);
   } else {
     endpoint = "timelines/home";
   }
@@ -108,7 +98,7 @@
 }
 
 void
-notifications(char *token, char *host, Notification notifs[])
+getnotifications(char *token, char *host, Notification notifs[])
 {
 	JSON *obj, *id, *content, *display_name, *handle, *type, *account, *status;
 	int i = 0;
@@ -152,8 +142,8 @@
 void
 posttoot(char *token, char *host, char *text)
 {
-  char url[MAXURL];
-  snprintf(url, MAXURL, "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);
@@ -181,8 +171,8 @@
 void
 perform(char *token, char *host, char *id, char *action)
 {
-  char url[MAXURL];
-  snprintf(url, sizeof(url), "%s/api/v1/statuses/%s/%s", host, id, action);
+  char *url;
+  url = esmprint("%s/api/v1/statuses/%s/%s", host, id, action);
   httppost(token, url, "");
 }
 
@@ -217,9 +207,9 @@
 void
 reply(char *token, char *host, char *id)
 {
-  char content[TOOTBUFSIZE];
+  char *content;
   int fd, wait;
-  char *s, *t, *u, url[MAXURL];
+  char *s, *t, *u, *url;
   Biobuf body;
 
   wait = 0;
@@ -233,11 +223,11 @@
     print("Reply to %s\n", u);
     Binit(&body, 0, OREAD);
     if((s = Brdstr(&body, 0, 1)) != nil)
-      t = smprint("%s", s);
+      t = esmprint("%s", s);
     free(s);
     if(t != nil){
-      snprintf(url, MAXURL, "%s/api/v1/statuses", host);
-      snprintf(content, TOOTBUFSIZE, "in_reply_to_id=%s&status=@%s %s", id, u, t);
+      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");
@@ -299,9 +289,9 @@
 mastodonget(char *token, char *host, char *endpoint)
 {
 	JSON *obj;
-	char *response, url[MAXURL];
+	char *response, *url;
 
-	snprintf(url, MAXURL, "https://%s/api/v1/%s", host, endpoint);
+	url = esmprint("https://%s/api/v1/%s", host, endpoint);
 	response = httpget(token, url);
 
 	obj = jsonparse(response);
@@ -327,8 +317,7 @@
     Toot toot = toots[i];
     char *username;
 
-    username = emalloc(sizeof(char)*256);
-    snprintf(username, 256, "%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) {
@@ -361,8 +350,7 @@
     Notification notif = notifs[i];
     char *username;
 
-    username = emalloc(sizeof(char)*256);
-    snprintf(username, 256, "%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)));
@@ -427,7 +415,7 @@
     displaytoots(toots, host);
   } else if(strcmp(command, "notifications") == 0) {
     Notification notifs[NOTIFSCOUNT];
-    notifications(token, host, notifs);
+    getnotifications(token, host, notifs);
     displaynotifications(notifs);
   }
 
--- a/masto9.h
+++ b/masto9.h
@@ -51,7 +51,6 @@
 /* utils */
 char *concat(char *s1, char *s2);
 void *emalloc(ulong);
-void *erealloc(void*, ulong);
 char *estrdup(char*);
 char *estrjoin(char **strings, char *sep);
 char *esmprint(char*, ...);
@@ -59,3 +58,4 @@
 u32int strhash(char *);
 void removesubstring(char *, char *);
 void removetag(char *, char *);
+JSON *getjsonkey(JSON *, char *);
--- a/util.c
+++ b/util.c
@@ -41,29 +41,6 @@
 	return s;
 }
 
-/* char* */
-/* estrjoin(char *s, ...) */
-/* { */
-/* 	va_list ap; */
-/* 	char *r, *t, *p, *e; */
-/* 	int n; */
-
-/* 	va_start(ap, s); */
-/* 	n = strlen(s) + 1; */
-/* 	while((p = va_arg(ap, char*)) != nil) */
-/* 		n += strlen(p); */
-/* 	va_end(ap); */
-
-/* 	r = emalloc(n); */
-/* 	e = r + n; */
-/* 	va_start(ap, s); */
-/* 	t = strecpy(r, e, s); */
-/* 	while((p = va_arg(ap, char*)) != nil) */
-/* 		t = strecpy(t, e, p); */
-/* 	va_end(ap); */
-/* 	return r; */
-/* } */
-
 char *
 concat(char *s1, char *s2)
 {
@@ -158,4 +135,13 @@
 
     start = strstr(start, tag);
   }
+}
+
+JSON *
+getjsonkey(JSON *obj, char *key)
+{
+	JSON *value = jsonbyname(obj, key);
+	if (value == nil)
+		sysfatal("jsonbyname: key %s not found in %J", key, obj);
+	return value;
 }