forked from APE-Project/APE_Server
/
channel_history.c
160 lines (123 loc) · 4.1 KB
/
channel_history.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright (C) 2010 Philipp Fuehrer <pf@netzbeben.de>
This file is part of APE Server.
APE is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
APE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with APE ; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* channel_history.c */
#include "channel_history.h"
#include "utils.h"
int get_channel_max_history_size(CHANNEL *chan) {
char *hs = get_property_val((chan)->properties, "max_history_size");
return (hs != NULL) ? strtol(hs, NULL, 10) : 0;
}
void history_append_element(CHANNEL_HISTORY_DEQUE *deque, RAW *raw) {
CHANNEL_HISTORY_NODE *node = xmalloc(sizeof(*node));
node->value = raw;
++deque->length;
if (deque->head != NULL) {
node->next = deque->head;
deque->tail = deque->tail->next = node;
return;
}
node->next = node;
deque->head = deque->tail = node;
}
void history_push_element(CHANNEL_HISTORY_DEQUE *deque, RAW *raw) {
free_raw(deque->head->value);
deque->head->value = raw;
deque->tail = deque->head;
deque->head = deque->head->next;
}
void history_free_node(CHANNEL_HISTORY_NODE *node) {
free_raw(node->value);
free(node);
}
void deque_truncate_buffer(CHANNEL_HISTORY_DEQUE *deque, const int size) {
int i = size;
for (; i < deque->length; ++i) {
deque->head = deque->head->next;
history_free_node(deque->tail->next);
deque->tail->next = deque->head;
}
if (size == 0 && i > size) {
free(deque);
deque = NULL;
}
}
void free_channel_history(CHANNEL_HISTORY_DEQUE *deque) {
if (deque == NULL) return;
deque_truncate_buffer(deque, 0);
}
void update_chan_history_size(CHANNEL *chan) {
if (chan->history == NULL) return;
{
const int max_history_size = get_channel_max_history_size(chan);
if (max_history_size < chan->history->length) {
deque_truncate_buffer(chan->history, max_history_size);
}
}
}
CHANNEL_HISTORY_DEQUE *new_channel_history(void) {
struct CHANNEL_HISTORY_DEQUE *deque = xmalloc(sizeof(*deque));
deque->length = 0;
deque->filter = NULL;
deque->head = deque->tail = NULL;
return deque;
}
CHANNEL_HISTORY_DEQUE *init_channel_history(CHANNEL *chan, apeconfig *config) {
char *config_val = CONFIG_VAL(Channels, history_default_max_size, config);
if (config_val != NULL && atoi(config_val) > 0) {
CHANNEL_HISTORY_DEQUE *deque = new_channel_history();
add_property(&chan->properties, "max_history_size", config_val, EXTEND_STR, EXTEND_ISPRIVATE);
config_val = CONFIG_VAL(Channels, history_default_raw_filter, config);
if (config_val != NULL) {
S_TOUPPER(config_val);
if (strcmp(config_val, "ALL") != 0) {
deque->filter = config_val;
}
}
return deque;
}
return NULL;
}
/* preconditions: memory for raw has been allocated and will not be freed later elsewhere, maxsize>0 */
void push_raw_to_channel_history(CHANNEL *chan, RAW *raw, apeconfig *config) {
if (chan == NULL || chan->history == NULL) {
return;
}
if (chan->history->filter != NULL) {
char *type = strstr(raw->data, "\"raw\":\"");
ITERATE_CONFIG_VAL(chan->history->filter, " ", {
if (type == NULL || strncmp(type+7, token, strlen(token)) != 0) {
free_raw(raw);
return;
}
});
}
if (chan->history->length == get_channel_max_history_size(chan)) {
history_push_element(chan->history, raw);
return;
}
history_append_element(chan->history, raw);
}
void post_channel_history(CHANNEL *chan, USERS *user, acetables *g_ape) {
if (get_channel_max_history_size(chan) > 0) {
CHANNEL_HISTORY_NODE *node = chan->history->head;
if (node != NULL) {
do {
post_raw(copy_raw_z(node->value), user, g_ape);
node = node->next;
} while (node != chan->history->head);
}
}
}