~skiqqy/baal

8304b2d7f42f60e990aab65c24d387bfc61b3237 — Stephen Cochrane 2 years ago 84e7435
Some refactor and tidying up
3 files changed, 20 insertions(+), 5 deletions(-)

M include/baal.h
M src/baalserver.c
M test.sh
M include/baal.h => include/baal.h +1 -1
@@ 80,7 80,7 @@ enum commmands {

// JSON Queries
#define REGISTER_QUERY  "register"
#define SEND_MAIL_QUERY "send mail"
#define SEND_MAIL_QUERY "send"

// Commands
int server(int argc, char **argv); // Start a server

M src/baalserver.c => src/baalserver.c +18 -3
@@ 57,6 57,7 @@ void init_threads();
cJSON *
fetchUser(char *user)
{
	if (!user) return NULL;
	cJSON *t;

	cJSON_ArrayForEach(t, cJSON_GetObjectItemCaseSensitive(serv.users, "users")) {


@@ 120,6 121,8 @@ debug(int argc, char **argv)

		if (!strcmp(REGISTER_QUERY, argv[1])) {
			op = REGISTER_HANDLER;
		} else if (!strcmp(SEND_MAIL_QUERY, argv[1])) {
			op = SEND_MAIL_HANDLER;
		} else {
			bwarn("Handler '%s' not yet implemented", argv[1]);
			return EXIT_FAILURE;


@@ 256,7 259,9 @@ send_mail_handler(WorkerArg *arg, cJSON *data)
{
	cJSON *resp = NULL, *resp_details = NULL, *user = NULL;
	cJSON *to = cJSON_GetObjectItemCaseSensitive(data, "to");
	cJSON *from = cJSON_GetObjectItemCaseSensitive(data, "from");
	int resp_code = SUCCESS, client = (arg) ? arg->client : -1;
	char *tok;
	dprintf("send_mail_handler invoked...\n", NULL);
	dprintJSON(data);



@@ 270,9 275,19 @@ send_mail_handler(WorkerArg *arg, cJSON *data)
		goto fail;
	}

	// TODO. Find user, if found, store mail.
	// TODO. strtok on @ to get the username
	if ((user = fetchUser(to->valuestring)) == NULL) {
	if (!from) {
		resp_code = ERR_FIELD;
		resp_details = cJSON_Parse("{\"err\":\"Missing 'from' Field\"}");
		goto fail;
	} else if (!cJSON_IsString(from)) {
		resp_code = ERR_FIELD;
		resp_details = cJSON_Parse("{\"err\":\"'from' field must be a string\"}");
		goto fail;
	}

	// Find user, if found, store mail.
	// TODO: Fix possible mem leak.
	if ((user = fetchUser(strtok_r(to->valuestring, "@", &tok))) == NULL) {
		dprintf("Mail for %s recieved, this user DNE, dropping.\n", to->valuestring);
		resp_code = ERR_USER_DNE;
		goto fail;

M test.sh => test.sh +1 -1
@@ 114,7 114,7 @@ send_mail()
{
	# Simulate a send mail request, and assert the response code
	# Usage: test MSG_JSON CODE
	run() { assert_json_code "$($clientsh "$(query_json "send mail" "$1" )")" "$2" "send mail" 2; }
	run() { assert_json_code "$($clientsh "$(query_json send "$1" )")" "$2" "send mail" 2; }

	# Run the tests
	$server > /dev/null 2>&1 & # Start the server