-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.c
138 lines (116 loc) · 2.77 KB
/
store.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#define MODULE_NAME "store"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "store.h"
#include "state.h"
#include "sett.h"
#include "log.h"
void store_init(void)
{
INF("Initializing...");
struct settings *s = sett_get();
util_mkdir_r(s->spath); //TODO: Implement this in platform.c
}
void store_msg(char *msg)
{
struct settings *s = sett_get();
char *t = util_strdup(msg);
struct irc_message m = util_irc_message_parse(t);
if(!strcmp(m.tokarr[m.cmd], "PRIVMSG")){
char path[1024], buf[1024];
store_format_parse(path, sizeof path, s->sfmt, m.tokarr[m.middle]);
util_mkdir_r(path);
util_irc_prefix_construct(buf, sizeof buf, m.prefix);
strcat(buf, ": ");
strcat(buf, m.tokarr[m.trailing]);
store_store(path, buf);
}
else if(s->sjoin && (!strcmp(m.tokarr[m.cmd], "JOIN") || !strcmp(m.tokarr[m.cmd], "PART"))){
char path[1024], buf[1024];
store_format_parse(path, sizeof path, s->sfmt, m.tokarr[m.middle]);
util_mkdir_r(path);
util_irc_prefix_construct(buf, sizeof buf, m.prefix);
strcat(buf, " ");
strcat(buf, (m.tokarr[m.cmd][1] == 'J') ? "joined." : "left.");
store_store(path, buf);
}
if(state_is_away())
state_buffer(m.tokarr[m.middle], msg);
free(t);
}
bool store_store(char *path, char *msg)
{
char buf[2048];
time_t t = time(NULL);
struct tm *tm = localtime(&t);
strftime(buf, sizeof buf, "%T ", tm);
strcat(buf, msg);
FILE *f = fopen(path, "a");
if(!f)
return false;
fputs(buf, f);
fclose(f);
return true;
}
bool store_format_parse(char *buf, size_t bufsz, const char *fmt, const char *channel)
{
char *temp = malloc(sizeof *temp * bufsz);
size_t sz = bufsz;
char *a = temp;
struct settings *s = sett_get();
strcpy(a, s->spath);
a += strlen(s->spath);
*a++ = '/';
while(sz-- > 0){
if(*fmt == '%'){
switch(*(fmt + 1)){
case 'D':
case 'M':
case 'Y':
*a++ = *fmt++;
continue;
case 'h':
if(sz < strlen(s->server.host))
return false;
strcpy(a, s->server.host);
a += strlen(s->server.host);
sz -= strlen(s->server.host);
break;
case 'c':
if(sz < strlen(channel))
return false;
strcpy(a, channel);
a += strlen(channel);
sz -= strlen(channel);
break;
case 'E': ;
char tmp[32];
snprintf(tmp, sizeof tmp, "%ld", s->rawepoch);
if(sz < strlen(tmp))
return false;
strcpy(a, tmp);
a += strlen(tmp);
sz -= strlen(tmp);
break;
default:
ERR("Unknown format specifier in store format.");
return false;
}
fmt += 2;
}
else if(!(*a++ = *fmt++))
goto fuckthisshit;
}
if(!sz)
return false;
fuckthisshit: ;
time_t t = time(NULL);
struct tm *tm = localtime(&t);
strftime(buf, bufsz, temp, tm);
free(temp);
return true;
}
#undef MODULE_NAME