58 lines
1.2 KiB
C
58 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/stat.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
// functions to deal with the journal file
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
void journal_write_topic(FILE *journal_fd, char *topic, char *message)
|
|
{
|
|
char *tstamp = malloc(128 * sizeof(char));
|
|
char fmt_msg[512];
|
|
|
|
time_t rawtime;
|
|
time(&rawtime);
|
|
const struct tm *cur_time = gmtime(&rawtime);
|
|
strftime(tstamp, 128, "%c", cur_time);
|
|
|
|
sprintf(fmt_msg, "[%s] [%s]: %s\n", tstamp, topic, message);
|
|
free(tstamp);
|
|
|
|
journal_write(journal_fd, fmt_msg);
|
|
}
|