diff --git a/Makefile b/Makefile index ab0e30e..e496076 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,10 @@ LDLIBS= PREFIX=/usr/local +SRC = src/journal/main.o \ + src/journal/journal.o \ + src/journal/emotion.o + all: ensure_bin journal ensure_bin: bin @@ -22,10 +26,10 @@ uninstall: rm $(DESTDIR)$(PREFIX)/bin/journal # currently we only have one journal util, others may come later -journal: src/journal/main.o src/journal/emotion.o - $(CC) $(LDFLAGS) -o bin/journal \ - src/journal/main.o src/journal/emotion.o $(LDLIBS) +journal: $(SRC) + $(CC) $(LDFLAGS) -o bin/journal $(SRC) $(LDLIBS) +src/journal/journal.o: src/journal/journal.c src/journal/emotion.o: src/journal/emotion.c src/journal/main.o: src/journal/main.c diff --git a/src/journal/emotion.c b/src/journal/emotion.c index 31382c2..a316ec7 100644 --- a/src/journal/emotion.c +++ b/src/journal/emotion.c @@ -1,7 +1,10 @@ #include -void handle_emotion() +#include "journal.h" + +void handle_emotion(FILE *journal, char* msg) { - printf("test\n"); + // TODO: add newline to msg + journal_write(journal, msg); return; } diff --git a/src/journal/emotion.h b/src/journal/emotion.h index 4f2cf78..b9b6393 100644 --- a/src/journal/emotion.h +++ b/src/journal/emotion.h @@ -1,6 +1,8 @@ #ifndef __EMOTION_H__ #define __EMOTION_H__ -void handle_emotion(); +#include + +void handle_emotion(FILE*, char*); #endif diff --git a/src/journal/journal.c b/src/journal/journal.c new file mode 100644 index 0000000..5aaee17 --- /dev/null +++ b/src/journal/journal.c @@ -0,0 +1,38 @@ +// functions to deal with the journal file +#include +#include +#include +#include + +FILE *journal_open(char *topic) +{ + char *home_path = getenv("HOME"); + + // combine w/ home path + char *journal_dir = (char*)malloc(1024); + snprintf(journal_dir, 1024, "%s/.lunabot", home_path); + + // construct a 744 + mkdir(journal_dir, S_IRWXU | S_IRGRP | S_IROTH); + + char *journal_path = (char*)malloc(1024); + snprintf(journal_path, 1024, "%s/%s", journal_dir, topic); + + FILE* res = fopen(journal_path, "a"); + + free(journal_path); + free(journal_dir); + + return res; +} + +void journal_write(FILE* journal_fd, char* message) +{ + // TODO: strlen()? + fwrite(message, strlen(message), 1, journal_fd); +} + +void journal_close(FILE* journal_fd) +{ + fclose(journal_fd); +} diff --git a/src/journal/journal.h b/src/journal/journal.h new file mode 100644 index 0000000..5151fef --- /dev/null +++ b/src/journal/journal.h @@ -0,0 +1,10 @@ +#ifndef __JOURNAL_H__ +#define __JOURNAL_H__ + +#include + +FILE *journal_open(char *topic); +void journal_write(FILE*, char* message); +void journal_close(FILE*); + +#endif diff --git a/src/journal/main.c b/src/journal/main.c index 6674c24..6c243e1 100644 --- a/src/journal/main.c +++ b/src/journal/main.c @@ -2,6 +2,7 @@ #include #include "emotion.h" +#include "journal.h" #define TOPICS 10 @@ -20,17 +21,23 @@ int main(int argc, char** argv) "emotion" }; - void (*handlers[])() = { + void (*handlers[])(FILE*, char*) = { handle_emotion, }; for(int i = 0; i < TOPICS; i++) { const char* cur_topic = topics[i]; + if(strcmp(topic, cur_topic) == 0) { - void (*fun_ptr)() = handlers[i]; - fun_ptr(); + void (*fun_ptr)(FILE*, char*) = handlers[i]; + + FILE* journal_file = journal_open(topic); + fun_ptr(journal_file, "hello world"); + journal_close(journal_file); + + printf("done\n"); return 0; } }