110
|
1 /* |
|
2 * histolog.c -- File history handling |
|
3 * |
|
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License as published by |
|
8 * the Free Software Foundation; either version 2 of the License, or (at |
|
9 * your option) any later version. |
|
10 * |
|
11 * This program is distributed in the hope that it will be useful, but |
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 * General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License |
|
17 * along with this program; if not, write to the Free Software |
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 * USA |
|
20 */ |
|
21 |
|
22 #include <string.h> |
|
23 #include <stdlib.h> |
|
24 #include <time.h> |
113
|
25 #include <ctype.h> |
|
26 #include <sys/types.h> |
|
27 #include <sys/stat.h> |
|
28 #include <fcntl.h> |
110
|
29 |
|
30 #include "histolog.h" |
178
|
31 #include "hbuf.h" |
113
|
32 #include "jabglue.h" |
110
|
33 #include "screen.h" |
|
34 |
|
35 static guint UseFileLogging; |
177
|
36 static guint FileLoadLogs; |
110
|
37 static char *RootDir; |
|
38 |
|
39 |
|
40 // user_histo_file() |
|
41 // Returns history filename for the given jid |
|
42 // Note: the caller *must* free the filename after use (if not null). |
111
|
43 static char *user_histo_file(const char *jid) |
110
|
44 { |
|
45 char *filename; |
186
|
46 char *lowerid, *p; |
178
|
47 if (!UseFileLogging && !FileLoadLogs) return NULL; |
110
|
48 |
186
|
49 lowerid = g_strdup(jid); |
|
50 for (p=lowerid; *p ; p++) |
|
51 *p = tolower(*p); |
|
52 |
110
|
53 filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
|
54 strcpy(filename, RootDir); |
186
|
55 strcat(filename, lowerid); |
|
56 g_free(lowerid); |
110
|
57 return filename; |
|
58 } |
|
59 |
178
|
60 // write_histo_line() |
110
|
61 // Adds a history (multi-)line to the jid's history logfile |
113
|
62 static void write_histo_line(const char *jid, |
|
63 time_t timestamp, guchar type, guchar info, const char *data) |
110
|
64 { |
|
65 guint len = 0; |
113
|
66 FILE *fp; |
110
|
67 time_t ts; |
113
|
68 const char *p; |
178
|
69 char *filename; |
110
|
70 |
178
|
71 if (!UseFileLogging) return; |
|
72 |
|
73 filename = user_histo_file(jid); |
110
|
74 |
|
75 // If timestamp is null, get current date |
|
76 if (timestamp) |
|
77 ts = timestamp; |
|
78 else |
|
79 time(&ts); |
|
80 |
|
81 if (!data) |
|
82 data = ""; |
|
83 |
|
84 // Count number of extra lines |
|
85 for (p=data ; *p ; p++) |
|
86 if (*p == '\n') len++; |
|
87 |
118
|
88 /* Line format: "TI DDDDDDDDDD LLL [data]" |
|
89 * T=Type, I=Info, DDDDDDDDDD=date, LLL=0-padded-len |
110
|
90 * |
|
91 * Types: |
|
92 * - M message Info: S (send) R (receive) |
|
93 * - S status Info: [oaifdcn] |
|
94 * We don't check them, we'll trust the caller. |
|
95 */ |
|
96 |
113
|
97 fp = fopen(filename, "a"); |
155
|
98 g_free(filename); |
178
|
99 if (!fp) return; |
|
100 |
118
|
101 fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data); |
113
|
102 fclose(fp); |
110
|
103 } |
|
104 |
178
|
105 // hlog_read_history() |
|
106 // Reads the jid's history logfile |
|
107 void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width) |
|
108 { |
|
109 char *filename; |
|
110 guchar type, info; |
|
111 char *data, *tail; |
184
|
112 time_t timestamp; |
|
113 guint prefix_flags; |
178
|
114 guint len; |
|
115 FILE *fp; |
193
|
116 guint err = 0; |
178
|
117 |
|
118 if (!FileLoadLogs) return; |
|
119 |
|
120 data = g_new(char, HBB_BLOCKSIZE+32); |
|
121 if (!data) { |
|
122 scr_LogPrint("Not enough memory to read history file"); |
|
123 return; |
|
124 } |
|
125 |
|
126 filename = user_histo_file(jid); |
|
127 |
|
128 fp = fopen(filename, "r"); |
|
129 g_free(filename); |
|
130 if (!fp) { g_free(data); return; } |
|
131 |
|
132 /* See write_histo_line() for line format... */ |
|
133 while (!feof(fp)) { |
|
134 if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break; |
|
135 |
|
136 for (tail = data; *tail; tail++) ; |
|
137 |
|
138 type = data[0]; |
|
139 info = data[1]; |
|
140 if ((type != 'M' && type != 'S') || |
|
141 (data[13] != ' ') || (data[17] != ' ')) { |
193
|
142 if (!err) { |
|
143 scr_LogPrint("Error in history file format (%s)", jid); |
|
144 err = 1; |
|
145 } |
186
|
146 //break; |
|
147 continue; |
178
|
148 } |
|
149 data[13] = data[17] = 0; |
|
150 timestamp = (unsigned long) atol(&data[3]); |
|
151 len = (unsigned long) atol(&data[14]); |
|
152 |
|
153 // Some checks |
|
154 if (((type == 'M') && (info != 'S' && info != 'R')) || |
183
|
155 ((type == 'I') && (!strchr("OAIFDCN", info)))) { |
193
|
156 if (!err) { |
|
157 scr_LogPrint("Error in history file format (%s)", jid); |
|
158 err = 1; |
|
159 } |
186
|
160 //break; |
|
161 continue; |
178
|
162 } |
|
163 |
186
|
164 // FIXME This will fail when a message is too big |
178
|
165 while (len--) { |
|
166 if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break; |
|
167 |
|
168 while (*tail) tail++; |
|
169 } |
|
170 if ((tail > data+18) && (*(tail-1) == '\n')) |
|
171 *(tail-1) = 0; |
|
172 |
|
173 if (type == 'M') { |
|
174 if (info == 'S') |
184
|
175 prefix_flags = HBB_PREFIX_OUT; |
178
|
176 else |
184
|
177 prefix_flags = HBB_PREFIX_IN; |
|
178 hbuf_add_line(p_buddyhbuf, &data[18], timestamp, prefix_flags, width); |
193
|
179 err = 0; |
178
|
180 } |
|
181 } |
|
182 fclose(fp); |
|
183 g_free(data); |
|
184 } |
|
185 |
110
|
186 // hlog_enable() |
|
187 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
177
|
188 // If loadfiles is TRUE, we will try to load buddies history logs from file. |
|
189 void hlog_enable(guint enable, char *root_dir, guint loadfiles) |
110
|
190 { |
|
191 UseFileLogging = enable; |
177
|
192 FileLoadLogs = loadfiles; |
110
|
193 |
177
|
194 if (enable || loadfiles) { |
110
|
195 if (root_dir) { |
|
196 int l = strlen(root_dir); |
|
197 if (l < 1) { |
|
198 scr_LogPrint("root_dir too short"); |
|
199 UseFileLogging = FALSE; |
|
200 return; |
|
201 } |
|
202 // RootDir must be slash-terminated |
|
203 if (root_dir[l-1] == '/') |
|
204 RootDir = g_strdup(root_dir); |
|
205 else { |
|
206 RootDir = g_new(char, l+2); |
|
207 strcpy(RootDir, root_dir); |
|
208 strcat(RootDir, "/"); |
|
209 } |
|
210 } else { |
|
211 char *home = getenv("HOME"); |
169
|
212 char *dir = "/.mcabber/histo/"; |
110
|
213 RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
|
214 strcpy(RootDir, home); |
|
215 strcat(RootDir, dir); |
|
216 } |
|
217 // FIXME |
|
218 // We should check the directory actually exists |
|
219 } else // Disable history logging |
|
220 if (RootDir) { |
|
221 g_free(RootDir); |
|
222 } |
|
223 } |
|
224 |
113
|
225 inline void hlog_write_message(const char *jid, time_t timestamp, int sent, |
|
226 const char *msg) |
|
227 { |
|
228 write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); |
|
229 } |
|
230 |
|
231 inline void hlog_write_status(const char *jid, time_t timestamp, |
|
232 enum imstatus status) |
|
233 { |
|
234 // #1 XXX Check status value? |
|
235 // #2 We could add a user-readable comment |
|
236 write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]), |
|
237 NULL); |
|
238 } |
|
239 |