shithub: masto9

Download patch

ref: 402ed456dc7a1bf16dbcf79e13a566dd57761bf7
parent: 1d8fc9dd60d8c0ef4e672842c2805b6ba004245c
author: Julien Blanchard <jblanchard@makemusic.com>
date: Sun Mar 26 15:35:30 EDT 2023

Move some utils functions, fix posting a toot

--- a/http.c
+++ b/http.c
@@ -46,7 +46,7 @@
 
 	close(bodyfd);
 	close(ctlfd);
-  print("BODY %d", strlen(body));
+
   return body;
 }
 
@@ -53,7 +53,7 @@
 char *
 httppost(char *token, char *url, char *text)
 {
-	int	ctlfd, bodyfd, conn, n;
+	int	ctlfd, bodyfd, conn;
 	char *buf;
   char *bearer_token;
 
@@ -106,3 +106,120 @@
 
   return buf;
 }
+
+
+/* static void* */
+/* slurp(int fd, int *n) */
+/* { */
+/* 	char *b; */
+/* 	int r, sz; */
+
+/* 	*n = 0; */
+/* 	sz = 32; */
+/* 	if((b = malloc(sz)) == nil) */
+/* 		abort(); */
+/* 	while(1){ */
+/* 		if(*n + 1 == sz){ */
+/* 			sz *= 2; */
+/* 			if((b = realloc(b, sz)) == nil) */
+/* 				abort(); */
+/* 		} */
+/* 		r = read(fd, b + *n, sz - *n - 1); */
+/* 		if(r == 0) */
+/* 			break; */
+/* 		if(r == -1){ */
+/* 			free(b); */
+/* 			return nil; */
+/* 		} */
+/* 		*n += r; */
+/* 	} */
+/* 	b[*n] = 0; */
+/* 	return b; */
+/* } */
+
+/* static int */
+/* webopen(char *url, char *token, char *dir, int ndir) */
+/* { */
+/* 	char buf[16]; */
+/* 	int n, cfd, conn; */
+
+/* 	if((cfd = open("/mnt/web/clone", ORDWR)) == -1) */
+/* 		return -1; */
+/* 	if((n = read(cfd, buf, sizeof(buf)-1)) == -1) */
+/* 		return -1; */
+/* 	buf[n] = 0; */
+/* 	conn = atoi(buf); */
+
+/*     bearer_token = concat("Authorization: Bearer ", token); */
+
+/* 	if(fprint(cfd, "headers %s", bearer_token) <= 0) */
+/* 		goto Error; */
+/* 	if(fprint(cfd, "url %s", url) == -1) */
+/* 		goto Error; */
+/* 	snprint(dir, ndir, "/mnt/web/%d", conn); */
+/* 	return cfd; */
+/* Error: */
+/* 	close(cfd); */
+/* 	return -1; */
+/* } */
+
+/* static char* */
+/* get(char *url, char *token, int *n) */
+/* { */
+/* 	char *r, dir[64], path[80]; */
+/* 	int cfd, dfd; */
+
+/* 	r = nil; */
+/* 	dfd = -1; */
+/* 	if((cfd = webopen(url, token, dir, sizeof(dir))) == -1) */
+/* 		goto Error; */
+/* 	snprint(path, sizeof(path), "%s/%s", dir, "body"); */
+/* 	if((dfd = open(path, OREAD)) == -1) */
+/* 		goto Error; */
+/* 	r = slurp(dfd, n); */
+/* Error: */
+/* 	if(dfd != -1) close(dfd); */
+/* 	if(cfd != -1) close(cfd); */
+/* 	return r; */
+/* } */
+
+/* static char* */
+/* post(char *url, char *buf, int nbuf, int *nret, Hdr *h) */
+/* { */
+/* 	char *r, dir[64], path[80]; */
+/* 	int cfd, dfd, hfd, ok; */
+
+/* 	r = nil; */
+/* 	ok = 0; */
+/* 	dfd = -1; */
+/* 	if((cfd = webopen(url, dir, sizeof(dir))) == -1) */
+/* 		goto Error; */
+/* 	if(write(cfd, Contenttype, strlen(Contenttype)) == -1) */
+/* 		goto Error; */
+/* 	snprint(path, sizeof(path), "%s/%s", dir, "postbody"); */
+/* 	if((dfd = open(path, OWRITE)) == -1) */
+/* 		goto Error; */
+/* 	if(write(dfd, buf, nbuf) != nbuf) */
+/* 		goto Error; */
+/* 	close(dfd); */
+/* 	snprint(path, sizeof(path), "%s/%s", dir, "body"); */
+/* 	if((dfd = open(path, OREAD)) == -1) */
+/* 		goto Error; */
+/* 	if((r = slurp(dfd, nret)) == nil) */
+/* 		goto Error; */
+/* 	if(h != nil){ */
+/* 		snprint(path, sizeof(path), "%s/%s", dir, h->name); */
+/* 		if((hfd = open(path, OREAD)) == -1) */
+/* 			goto Error; */
+/* 		if((h->val = slurp(hfd, &h->nval)) == nil) */
+/* 			goto Error; */
+/* 		close(hfd); */
+/* 	} */
+/* 	ok = 1; */
+/* Error: */
+/* 	if(dfd != -1) close(dfd); */
+/* 	if(cfd != -1) close(cfd); */
+/* 	if(!ok && h != nil) */
+/* 		free(h->val); */
+/* 	return r; */
+/* } */
--- a/masto9.c
+++ b/masto9.c
@@ -15,8 +15,7 @@
 {
 	UserPasswd* p;
 
-	p = auth_getuserpasswd(auth_getkey,
-                         "proto=pass service=mastodon server=%s", host);
+	p = auth_getuserpasswd(auth_getkey, "proto=pass service=mastodon server=%s", host);
 	if(p == nil)
 		sysfatal("mastofs: failed to retrieve token: %r");
 
@@ -67,14 +66,13 @@
 			toot.reblogged = 0;
 			toot.content = strdup((char *)content->s);
 		} else {
-			//print("reblog: %J", toot_json);
 			reblog_content = get_json_key(reblog, "content");
 			reblog_account = get_json_key(reblog, "account");
 			reblog_username = get_json_key(reblog_account, "username");
 
 			toot.content = strdup((char *)reblog_content->s);
-			toot.reblogged = 1;
 			toot.reblogged_username = strdup((char *)reblog_username->s);
+			toot.reblogged = 1;
 
 			media_attachments = get_json_key(reblog, "media_attachments");
 		};
@@ -87,7 +85,6 @@
 			int j = 0;
 			for(JSONEl *at = media_attachments->first; at != nil; at = at->next) {
 				JSON *attachment_json = at->val;
-				//print("att: %J", attachment_json);
 				Attachment attachment = emalloc(sizeof(Attachment));
 				type = get_json_key(attachment_json, "type");
 				url = get_json_key(attachment_json, "preview_url");
@@ -149,9 +146,8 @@
 void
 post_toot(char *token, char *text)
 {
-	char buf[TOOTBUFSIZE];
-	httppost(token, POSTURL, text);
-  print("RESPONSE %s", buf);
+	httppost(token, POSTURL, concat("status=", text));
+  print("Posted:\n %s\n", text);
 }
 
 void
@@ -232,35 +228,6 @@
 	return buf;
 }
 
-void
-remove_substring(char *str, char *sub)
-{
-  int len = strlen(sub);
-
-  while ((str = strstr(str, sub))) {
-    memmove(str, str + len, strlen(str + len) + 1);
-  }
-}
-
-void
-remove_tag(char *str, char *tag)
-{
-  char *start = strstr(str, tag);
-
-  while (start) {
-    char *end = strchr(start, '>');
-
-    if (end) {
-      memmove(start, end + 1, strlen(end + 1) + 1);
-    } else {
-      *start = '\0';
-      break;
-    }
-
-    start = strstr(start, tag);
-  }
-}
-
 char *
 cleanup(char *str)
 {
@@ -269,122 +236,6 @@
 
 	return str;
 }
-
-/* static void* */
-/* slurp(int fd, int *n) */
-/* { */
-/* 	char *b; */
-/* 	int r, sz; */
-
-/* 	*n = 0; */
-/* 	sz = 32; */
-/* 	if((b = malloc(sz)) == nil) */
-/* 		abort(); */
-/* 	while(1){ */
-/* 		if(*n + 1 == sz){ */
-/* 			sz *= 2; */
-/* 			if((b = realloc(b, sz)) == nil) */
-/* 				abort(); */
-/* 		} */
-/* 		r = read(fd, b + *n, sz - *n - 1); */
-/* 		if(r == 0) */
-/* 			break; */
-/* 		if(r == -1){ */
-/* 			free(b); */
-/* 			return nil; */
-/* 		} */
-/* 		*n += r; */
-/* 	} */
-/* 	b[*n] = 0; */
-/* 	return b; */
-/* } */
-
-/* static int */
-/* webopen(char *url, char *token, char *dir, int ndir) */
-/* { */
-/* 	char buf[16]; */
-/* 	int n, cfd, conn; */
-
-/* 	if((cfd = open("/mnt/web/clone", ORDWR)) == -1) */
-/* 		return -1; */
-/* 	if((n = read(cfd, buf, sizeof(buf)-1)) == -1) */
-/* 		return -1; */
-/* 	buf[n] = 0; */
-/* 	conn = atoi(buf); */
-
-/*     bearer_token = concat("Authorization: Bearer ", token); */
-
-/* 	if(fprint(cfd, "headers %s", bearer_token) <= 0) */
-/* 		goto Error; */
-/* 	if(fprint(cfd, "url %s", url) == -1) */
-/* 		goto Error; */
-/* 	snprint(dir, ndir, "/mnt/web/%d", conn); */
-/* 	return cfd; */
-/* Error: */
-/* 	close(cfd); */
-/* 	return -1; */
-/* } */
-
-/* static char* */
-/* get(char *url, char *token, int *n) */
-/* { */
-/* 	char *r, dir[64], path[80]; */
-/* 	int cfd, dfd; */
-
-/* 	r = nil; */
-/* 	dfd = -1; */
-/* 	if((cfd = webopen(url, token, dir, sizeof(dir))) == -1) */
-/* 		goto Error; */
-/* 	snprint(path, sizeof(path), "%s/%s", dir, "body"); */
-/* 	if((dfd = open(path, OREAD)) == -1) */
-/* 		goto Error; */
-/* 	r = slurp(dfd, n); */
-/* Error: */
-/* 	if(dfd != -1) close(dfd); */
-/* 	if(cfd != -1) close(cfd); */
-/* 	return r; */
-/* } */
-
-/* static char* */
-/* post(char *url, char *buf, int nbuf, int *nret, Hdr *h) */
-/* { */
-/* 	char *r, dir[64], path[80]; */
-/* 	int cfd, dfd, hfd, ok; */
-
-/* 	r = nil; */
-/* 	ok = 0; */
-/* 	dfd = -1; */
-/* 	if((cfd = webopen(url, dir, sizeof(dir))) == -1) */
-/* 		goto Error; */
-/* 	if(write(cfd, Contenttype, strlen(Contenttype)) == -1) */
-/* 		goto Error; */
-/* 	snprint(path, sizeof(path), "%s/%s", dir, "postbody"); */
-/* 	if((dfd = open(path, OWRITE)) == -1) */
-/* 		goto Error; */
-/* 	if(write(dfd, buf, nbuf) != nbuf) */
-/* 		goto Error; */
-/* 	close(dfd); */
-/* 	snprint(path, sizeof(path), "%s/%s", dir, "body"); */
-/* 	if((dfd = open(path, OREAD)) == -1) */
-/* 		goto Error; */
-/* 	if((r = slurp(dfd, nret)) == nil) */
-/* 		goto Error; */
-/* 	if(h != nil){ */
-/* 		snprint(path, sizeof(path), "%s/%s", dir, h->name); */
-/* 		if((hfd = open(path, OREAD)) == -1) */
-/* 			goto Error; */
-/* 		if((h->val = slurp(hfd, &h->nval)) == nil) */
-/* 			goto Error; */
-/* 		close(hfd); */
-/* 	} */
-/* 	ok = 1; */
-/* Error: */
-/* 	if(dfd != -1) close(dfd); */
-/* 	if(cfd != -1) close(cfd); */
-/* 	if(!ok && h != nil) */
-/* 		free(h->val); */
-/* 	return r; */
-/* } */
 
 JSON *
 mastodonget(char *token, char *host, char *endpoint)
--- a/masto9.h
+++ b/masto9.h
@@ -48,4 +48,6 @@
 char *estrjoin(char **strings, char *sep);
 char *esmprint(char*, ...);
 char *fslurp(int, int*);
-u32int strhash(char*);
+u32int strhash(char *);
+void remove_substring(char *, char *);
+void remove_tag(char *, char *);
--- a/util.c
+++ b/util.c
@@ -129,3 +129,32 @@
 		h = ((h << 5) + h) + c;
 	return h;
 }
+
+void
+remove_substring(char *str, char *sub)
+{
+  int len = strlen(sub);
+
+  while ((str = strstr(str, sub))) {
+    memmove(str, str + len, strlen(str + len) + 1);
+  }
+}
+
+void
+remove_tag(char *str, char *tag)
+{
+  char *start = strstr(str, tag);
+
+  while (start) {
+    char *end = strchr(start, '>');
+
+    if (end) {
+      memmove(start, end + 1, strlen(end + 1) + 1);
+    } else {
+      *start = '\0';
+      break;
+    }
+
+    start = strstr(start, tag);
+  }
+}