commit 0d8a040

default  ·  2023-05-24 09:49:16 +0000 UTC
parent 9bc4825
Added some experimental 'Question' posting code.
4 files changed,  +54, -25
M html.c
M main.c
M snac.h
+14, -6
 1@@ -678,14 +678,22 @@ d_char *msg_actor(snac *snac)
 2 }
 3 
 4 
 5-d_char *msg_create(snac *snac, char *object)
 6+xs_dict *msg_create(snac *snac, const xs_dict *object)
 7 /* creates a 'Create' message */
 8 {
 9-    d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
10+    xs_dict *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
11+    xs_val *v;
12+
13+    if ((v = xs_dict_get(object, "attributedTo")))
14+        msg = xs_dict_append(msg, "attributedTo", v);
15+
16+    if ((v = xs_dict_get(object, "cc")))
17+        msg = xs_dict_append(msg, "cc", v);
18 
19-    msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo"));
20-    msg = xs_dict_append(msg, "to",           xs_dict_get(object, "to"));
21-    msg = xs_dict_append(msg, "cc",           xs_dict_get(object, "cc"));
22+    if ((v = xs_dict_get(object, "to")))
23+        msg = xs_dict_append(msg, "to", v);
24+    else
25+        msg = xs_dict_append(msg, "to", public_address);
26 
27     return msg;
28 }
29@@ -951,7 +959,7 @@ xs_dict *msg_question(snac *user, const char *content, const xs_list *opts, int
30     time_t t = time(NULL) + end_secs;
31     xs *et = xs_str_utctime(t, "%Y-%m-%dT%H:%M:%SZ");
32 
33-    msg = xs_dict_append(msg, "endTime", msg);
34+    msg = xs_dict_append(msg, "endTime", et);
35 
36     return msg;
37 }
M html.c
+3, -1
 1@@ -852,7 +852,9 @@ xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
 2 #endif
 3 
 4     {
 5-        xs *c  = sanitize(xs_dict_get(msg, "content"));
 6+        const char *content = xs_dict_get(msg, "content");
 7+
 8+        xs *c  = sanitize(xs_is_null(content) ? "" : content);
 9         char *p, *v;
10 
11         /* do some tweaks to the content */
M main.c
+36, -17
  1@@ -30,6 +30,7 @@ int usage(void)
  2     printf("note {basedir} {uid} {'text'}    Sends a note to followers\n");
  3     printf("resetpwd {basedir} {uid}         Resets the password of a user\n");
  4     printf("ping {basedir} {uid} {actor}     Pings an actor\n");
  5+/*    printf("question {basedir} {uid} 'opts'  Generates a poll (;-separated opts)\n");*/
  6 
  7     return 1;
  8 }
  9@@ -63,7 +64,7 @@ int main(int argc, char *argv[])
 10     if ((cmd = GET_ARGV()) == NULL)
 11         return usage();
 12 
 13-    if (strcmp(cmd, "init") == 0) {
 14+    if (strcmp(cmd, "init") == 0) { /** **/
 15         /* initialize the data storage */
 16         /* ... */
 17         basedir = GET_ARGV();
 18@@ -71,7 +72,7 @@ int main(int argc, char *argv[])
 19         return snac_init(basedir);
 20     }
 21 
 22-    if (strcmp(cmd, "upgrade") == 0) {
 23+    if (strcmp(cmd, "upgrade") == 0) { /** **/
 24         int ret;
 25 
 26         /* upgrade */
 27@@ -84,7 +85,7 @@ int main(int argc, char *argv[])
 28         return ret;
 29     }
 30 
 31-    if (strcmp(cmd, "markdown") == 0) {
 32+    if (strcmp(cmd, "markdown") == 0) { /** **/
 33         /* undocumented, for testing only */
 34         xs *c = xs_readall(stdin);
 35         xs *fc = not_really_markdown(c, NULL);
 36@@ -101,7 +102,7 @@ int main(int argc, char *argv[])
 37         return 1;
 38     }
 39 
 40-    if (strcmp(cmd, "adduser") == 0) {
 41+    if (strcmp(cmd, "adduser") == 0) { /** **/
 42         user = GET_ARGV();
 43 
 44         return adduser(user);
 45@@ -109,13 +110,13 @@ int main(int argc, char *argv[])
 46         return 0;
 47     }
 48 
 49-    if (strcmp(cmd, "httpd") == 0) {
 50+    if (strcmp(cmd, "httpd") == 0) { /** **/
 51         httpd();
 52         srv_free();
 53         return 0;
 54     }
 55 
 56-    if (strcmp(cmd, "purge") == 0) {
 57+    if (strcmp(cmd, "purge") == 0) { /** **/
 58         purge_all();
 59         return 0;
 60     }
 61@@ -123,7 +124,7 @@ int main(int argc, char *argv[])
 62     if ((user = GET_ARGV()) == NULL)
 63         return usage();
 64 
 65-    if (strcmp(cmd, "webfinger") == 0) {
 66+    if (strcmp(cmd, "webfinger") == 0) { /** **/
 67         xs *actor = NULL;
 68         xs *uid = NULL;
 69         int status;
 70@@ -146,16 +147,16 @@ int main(int argc, char *argv[])
 71 
 72     lastlog_write(&snac, "cmdline");
 73 
 74-    if (strcmp(cmd, "resetpwd") == 0) {
 75+    if (strcmp(cmd, "resetpwd") == 0) { /** **/
 76         return resetpwd(&snac);
 77     }
 78 
 79-    if (strcmp(cmd, "queue") == 0) {
 80+    if (strcmp(cmd, "queue") == 0) { /** **/
 81         process_user_queue(&snac);
 82         return 0;
 83     }
 84 
 85-    if (strcmp(cmd, "timeline") == 0) {
 86+    if (strcmp(cmd, "timeline") == 0) { /** **/
 87 #if 0
 88         xs *list = local_list(&snac, XS_ALL);
 89         xs *body = html_timeline(&snac, list, 1);
 90@@ -178,7 +179,7 @@ int main(int argc, char *argv[])
 91     if ((url = GET_ARGV()) == NULL)
 92         return usage();
 93 
 94-    if (strcmp(cmd, "announce") == 0) {
 95+    if (strcmp(cmd, "announce") == 0) { /** **/
 96         xs *msg = msg_admiration(&snac, url, "Announce");
 97 
 98         if (msg != NULL) {
 99@@ -193,7 +194,7 @@ int main(int argc, char *argv[])
100         return 0;
101     }
102 
103-    if (strcmp(cmd, "follow") == 0) {
104+    if (strcmp(cmd, "follow") == 0) { /** **/
105         xs *msg = msg_follow(&snac, url);
106 
107         if (msg != NULL) {
108@@ -212,7 +213,7 @@ int main(int argc, char *argv[])
109         return 0;
110     }
111 
112-    if (strcmp(cmd, "unfollow") == 0) {
113+    if (strcmp(cmd, "unfollow") == 0) { /** **/
114         xs *object = NULL;
115 
116         if (valid_status(following_get(&snac, url, &object))) {
117@@ -230,7 +231,7 @@ int main(int argc, char *argv[])
118         return 0;
119     }
120 
121-    if (strcmp(cmd, "ping") == 0) {
122+    if (strcmp(cmd, "ping") == 0) { /** **/
123         xs *actor_o = NULL;
124 
125         if (valid_status(actor_request(&snac, url, &actor_o))) {
126@@ -251,7 +252,25 @@ int main(int argc, char *argv[])
127         return 0;
128     }
129 
130-    if (strcmp(cmd, "request") == 0) {
131+    if (strcmp(cmd, "question") == 0) { /** **/
132+        xs *opts = xs_split(url, ";");
133+
134+        xs *msg = msg_question(&snac, "Poll", opts, 0, 5 * 60);
135+        xs *c_msg = msg_create(&snac, msg);
136+
137+        if (dbglevel) {
138+            xs *j = xs_json_dumps_pp(c_msg, 4);
139+            printf("%s\n", j);
140+        }
141+
142+        enqueue_message(&snac, c_msg);
143+
144+        timeline_add(&snac, xs_dict_get(msg, "id"), msg);
145+
146+        return 0;
147+    }
148+
149+    if (strcmp(cmd, "request") == 0) { /** **/
150         int status;
151         xs *data = NULL;
152 
153@@ -267,7 +286,7 @@ int main(int argc, char *argv[])
154         return 0;
155     }
156 
157-    if (strcmp(cmd, "actor") == 0) {
158+    if (strcmp(cmd, "actor") == 0) { /** **/
159         int status;
160         xs *data = NULL;
161 
162@@ -283,7 +302,7 @@ int main(int argc, char *argv[])
163         return 0;
164     }
165 
166-    if (strcmp(cmd, "note") == 0) {
167+    if (strcmp(cmd, "note") == 0) { /** **/
168         xs *content = NULL;
169         xs *msg = NULL;
170         xs *c_msg = NULL;
M snac.h
+1, -1
1@@ -193,7 +193,7 @@ int webfinger_get_handler(xs_dict *req, char *q_path,
2 const char *default_avatar_base64(void);
3 
4 d_char *msg_admiration(snac *snac, char *object, char *type);
5-d_char *msg_create(snac *snac, char *object);
6+xs_dict *msg_create(snac *snac, const xs_dict *object);
7 xs_dict *msg_follow(snac *snac, const char *actor);
8 
9 xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,