Misc changes.

This commit is contained in:
Jordan Bancino 2023-03-12 03:37:57 +00:00
parent 7fa982a16f
commit 62cd1cdc98
4 changed files with 54 additions and 17 deletions

View file

@ -17,24 +17,24 @@ Milestone: v0.3.0
[ ] Multi-output [ ] Multi-output
[ ] Move UtilStreamCopy() [ ] Move UtilStreamCopy()
[~] HTTP Client API [~] HTTP Client API
[ ] Document HttpParseHeaders() [x] Document HttpParseHeaders()
[ ] HttpClient man page [ ] HttpClient man page
[ ] Uri man page [ ] Uri man page
[x] Test on other platforms [x] Test on other platforms
[~] Option to pretty-print Json [x] Option to pretty-print Json
[ ] Document JsonEncode() and JsonEncodeValue() [x] Document JsonEncode() and JsonEncodeValue()
[ ] Update man page for td [ ] Update man page for td
[ ] Document Telodendria and Main [x] Document Telodendria and Main
[ ] Document tt and http-debug-server [ ] Document tt and http-debug-server
[~] Simple command line tool to make matrix requests [x] Simple command line tool to make matrix requests
[x] Built on HTTP client API [x] Built on HTTP client API
[ ] http man page [x] http man page
[~] Simple command line tool for working with JSON [~] Simple command line tool for working with JSON
[x] Pretty-print Json [x] Pretty-print Json
[x] Query fields for use in shell scripts. [x] Query fields for use in shell scripts.
[x] Encode user-provided JSON strings [x] Encode user-provided JSON strings
[ ] json man page [x] json man page
[ ] Update man pages for tp and send-patch [ ] Update man pages for tp and send-patch
[ ] Move configuration to database [ ] Move configuration to database

View file

@ -49,6 +49,6 @@ extern void
TelodendriaMemoryIterator(MemoryInfo *, void *); TelodendriaMemoryIterator(MemoryInfo *, void *);
extern void extern void
TelodendriaPrintHeader(LogConfig * lc); TelodendriaPrintHeader(LogConfig *);
#endif #endif

View file

@ -6,6 +6,8 @@ PASSWORD=$(json -e 'p@s$w0rd')
ENDPOINT="$1" ENDPOINT="$1"
: "${METH:=GET}"
# Check if user is available. If it is, register it. # Check if user is available. If it is, register it.
user_available=$(http "$BASE/_matrix/client/v3/register/available?username=$USERNAME" | json -s "available") user_available=$(http "$BASE/_matrix/client/v3/register/available?username=$USERNAME" | json -s "available")
if [ "$user_available" = "true" ]; then if [ "$user_available" = "true" ]; then
@ -20,7 +22,7 @@ if [ "$user_available" = "true" ]; then
printf ' "password": %s,' "$PASSWORD" printf ' "password": %s,' "$PASSWORD"
printf ' "inhibit_login": true ' printf ' "inhibit_login": true '
printf '}' printf '}'
) | http -X POST "$BASE/_matrix/client/v3/register" > /dev/null ) | http -X POST -d @- "$BASE/_matrix/client/v3/register" > /dev/null
fi fi
# Log in # Log in
@ -33,7 +35,7 @@ ACCESS_TOKEN=$((
printf ' "type": "m.login.password",' printf ' "type": "m.login.password",'
printf ' "password": %s' "$PASSWORD" printf ' "password": %s' "$PASSWORD"
printf '}' printf '}'
) | http -X POST "$BASE/_matrix/client/v3/login" | json -s "access_token->@decode") ) | http -X POST -d @- "$BASE/_matrix/client/v3/login" | json -s "access_token->@decode")
if [ -z "$ACCESS_TOKEN" ]; then if [ -z "$ACCESS_TOKEN" ]; then
echo "Unable to log in." echo "Unable to log in."
@ -41,7 +43,11 @@ if [ -z "$ACCESS_TOKEN" ]; then
fi fi
# Make request # Make request
http -X POST -H "Authorization: Bearer $ACCESS_TOKEN" "$BASE$ENDPOINT" | json if [ -n "$DATA" ]; then
http -X "$METH" -d "$DATA" -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE}${ENDPOINT}" | json
else
http -X "$METH" -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE}${ENDPOINT}" | json
fi
# Log out # Log out
http -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \ http -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \

View file

@ -26,6 +26,7 @@
#include <ctype.h> #include <ctype.h>
#include <getopt.h> #include <getopt.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <Memory.h> #include <Memory.h>
#include <Str.h> #include <Str.h>
@ -39,7 +40,7 @@
static void static void
usage(char *prog) usage(char *prog)
{ {
fprintf(stderr, "Usage: %s [-i -X method -H header] url\n", prog); fprintf(stderr, "Usage: %s [-i -X method -H header -d data] url\n", prog);
} }
int int
@ -49,6 +50,7 @@ main(int argc, char **argv)
HttpStatus res; HttpStatus res;
HttpRequestMethod method = HTTP_GET; HttpRequestMethod method = HTTP_GET;
Uri *uri; Uri *uri;
char *data = NULL;
HashMap *requestHeaders = HashMapCreate(); HashMap *requestHeaders = HashMapCreate();
char *key; char *key;
@ -59,7 +61,7 @@ main(int argc, char **argv)
int ch; int ch;
while ((ch = getopt(argc, argv, "iH:X:")) != -1) while ((ch = getopt(argc, argv, "iH:X:d:")) != -1)
{ {
switch (ch) switch (ch)
{ {
@ -93,6 +95,9 @@ main(int argc, char **argv)
HashMapSet(requestHeaders, key, StrDuplicate(val)); HashMapSet(requestHeaders, key, StrDuplicate(val));
break; break;
case 'd':
data = optarg;
break;
default: default:
usage(argv[0]); usage(argv[0]);
return 1; return 1;
@ -154,11 +159,37 @@ main(int argc, char **argv)
HttpRequestSendHeaders(cx); HttpRequestSendHeaders(cx);
HashMapFree(requestHeaders); HashMapFree(requestHeaders);
/* Only send stdin if it's not attached to a TTY. This prevents us if (data)
* from blocking if no pipe is provided */
if (!isatty(fileno(stdin)))
{ {
UtilStreamCopy(stdin, HttpClientStream(cx)); if (*data == '@')
{
FILE *in;
data++;
if (strcmp(data, "-") == 0)
{
in = stdin;
}
else
{
in = fopen(data, "r");
}
if (!in)
{
fprintf(stderr, "%s: %s\n", data, strerror(errno));
return 1;
}
UtilStreamCopy(in, HttpClientStream(cx));
fclose(in);
}
else
{
fprintf(HttpClientStream(cx), "%s", data);
}
} }
res = HttpRequestSend(cx); res = HttpRequestSend(cx);