@@ 1,15 1,18 @@
all: index.gmi post postuser
-CFLAGS=-O3 -march=native -Wall -Wextra -pedantic -Wpedantic
+CFLAGS=-O3 -g -march=native -Wall -Wextra -pedantic -Wpedantic
index.gmi: view.c config.h
cc $(CFLAGS) view.c -o index.gmi
+ cp index.gmi ..
post: post.c config.h
cc $(CFLAGS) post.c -o post
+ cp post ..
postuser: postuser.c config.h
cc $(CFLAGS) postuser.c -o postuser
+ cp postuser ..
clean:
rm -f index.gmi post postuser
@@ 1,4 1,7 @@
#pragma once
+/* config */
#define CHAT_NUM "2"
#define BUFF_SIZE 600
+#define MAX_MSG_SIZE 400
+#define COLUMN_SIZE 80 /* how long each line can be */
@@ 1,6 1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/time.h>
#include <time.h>
#include "config.h"
@@ 21,21 22,27 @@ removeExtraChars(char user[9])
}
void
-convert(char *str, char **nstr)
-{ /* free ret after */
+format(char *str, char **nstr)
+{ /* free nstr after */
size_t strsize = strlen(str), retsize = 0;
char *ret = malloc(strsize);
char tmp[2];
- for (unsigned int i = 0; i < strsize; i++) {
+ if(strsize > MAX_MSG_SIZE)
+ strsize = MAX_MSG_SIZE;
+
+ unsigned int cc = 0; /* column counter */
+ for (unsigned int i = 0; i < strsize; i++, cc++) {
+ if (cc >= COLUMN_SIZE) {
+ ret[retsize++] = '\n';
+ cc = 0;
+ }
if (str[i] == '%') {
- ret = realloc(ret, ++retsize);
tmp[0] = str[++i];
tmp[1] = str[++i];
- sscanf(tmp, "%x", (unsigned int *)&ret[retsize - 1]);
+ sscanf(tmp, "%x", (unsigned int *)&ret[retsize++]);
} else ret[retsize++] = str[i];
}
- ret[retsize] = '\0';
*nstr = ret;
}
@@ 54,7 61,6 @@ main()
char *PATH_INFO = getenv("PATH_INFO");
char *QUERY_STRING = getenv("QUERY_STRING");
char user[9];
- FILE *ml;
/* get username */
if (PATH_INFO == NULL || !strcmp(PATH_INFO, ""))
@@ 75,20 81,25 @@ main()
/*
* write message to file
*/
- ml = fopen("message_log", "a");
/* get time */
char *timestr = malloc(19);
getTime(×tr);
- /* replace % with \x */
+ /* decode percent encoding and format message */
char *message;
- convert(QUERY_STRING, &message);
+ format(QUERY_STRING, &message);
+
+ /* generate message id */
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ srand(tv.tv_sec * 1000 + tv.tv_usec / 1000);
/* print to file */
- fprintf(ml, "%s %8s: %s\n", timestr, user, message);
+ char cmd[MAX_MSG_SIZE + 120];
+ sprintf(cmd, "printf \"%s\\n\" | tac | awk '{print substr(\"%x\", 0, 5) \" %s %8s: \" $0}' >> message_log", message, rand(), timestr, user);
+ system(cmd);
free(timestr);
free(message);
- fclose(ml);
/* redirect back to messages */
puts("30 /bin/chat"CHAT_NUM"/\r");