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; |
178
|
46 if (!UseFileLogging && !FileLoadLogs) return NULL; |
110
|
47 |
|
48 filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
|
49 strcpy(filename, RootDir); |
|
50 strcat(filename, jid); |
|
51 return filename; |
|
52 } |
|
53 |
178
|
54 // write_histo_line() |
110
|
55 // Adds a history (multi-)line to the jid's history logfile |
113
|
56 static void write_histo_line(const char *jid, |
|
57 time_t timestamp, guchar type, guchar info, const char *data) |
110
|
58 { |
|
59 guint len = 0; |
113
|
60 FILE *fp; |
110
|
61 time_t ts; |
113
|
62 const char *p; |
178
|
63 char *filename; |
110
|
64 |
178
|
65 if (!UseFileLogging) return; |
|
66 |
|
67 filename = user_histo_file(jid); |
110
|
68 |
|
69 // If timestamp is null, get current date |
|
70 if (timestamp) |
|
71 ts = timestamp; |
|
72 else |
|
73 time(&ts); |
|
74 |
|
75 if (!data) |
|
76 data = ""; |
|
77 |
|
78 // Count number of extra lines |
|
79 for (p=data ; *p ; p++) |
|
80 if (*p == '\n') len++; |
|
81 |
118
|
82 /* Line format: "TI DDDDDDDDDD LLL [data]" |
|
83 * T=Type, I=Info, DDDDDDDDDD=date, LLL=0-padded-len |
110
|
84 * |
|
85 * Types: |
|
86 * - M message Info: S (send) R (receive) |
|
87 * - S status Info: [oaifdcn] |
|
88 * We don't check them, we'll trust the caller. |
|
89 */ |
|
90 |
113
|
91 fp = fopen(filename, "a"); |
155
|
92 g_free(filename); |
178
|
93 if (!fp) return; |
|
94 |
118
|
95 fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data); |
113
|
96 fclose(fp); |
110
|
97 } |
|
98 |
178
|
99 // hlog_read_history() |
|
100 // Reads the jid's history logfile |
|
101 void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width) |
|
102 { |
|
103 char *filename; |
|
104 time_t timestamp; |
|
105 guchar type, info; |
|
106 char *data, *tail; |
|
107 guint len; |
|
108 FILE *fp; |
|
109 char prefix[32]; |
|
110 |
|
111 if (!FileLoadLogs) return; |
|
112 |
|
113 data = g_new(char, HBB_BLOCKSIZE+32); |
|
114 if (!data) { |
|
115 scr_LogPrint("Not enough memory to read history file"); |
|
116 return; |
|
117 } |
|
118 |
|
119 filename = user_histo_file(jid); |
|
120 |
|
121 fp = fopen(filename, "r"); |
|
122 g_free(filename); |
|
123 if (!fp) { g_free(data); return; } |
|
124 |
|
125 /* See write_histo_line() for line format... */ |
|
126 while (!feof(fp)) { |
|
127 if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break; |
|
128 |
|
129 for (tail = data; *tail; tail++) ; |
|
130 |
|
131 type = data[0]; |
|
132 info = data[1]; |
|
133 if ((type != 'M' && type != 'S') || |
|
134 (data[13] != ' ') || (data[17] != ' ')) { |
|
135 scr_LogPrint("Error in history file format"); |
|
136 break; |
|
137 } |
|
138 data[13] = data[17] = 0; |
|
139 timestamp = (unsigned long) atol(&data[3]); |
|
140 len = (unsigned long) atol(&data[14]); |
|
141 |
|
142 // Some checks |
|
143 if (((type == 'M') && (info != 'S' && info != 'R')) || |
183
|
144 ((type == 'I') && (!strchr("OAIFDCN", info)))) { |
178
|
145 scr_LogPrint("Error in history file format"); |
|
146 break; |
|
147 } |
|
148 |
|
149 while (len--) { |
|
150 if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break; |
|
151 |
|
152 while (*tail) tail++; |
|
153 } |
|
154 if ((tail > data+18) && (*(tail-1) == '\n')) |
|
155 *(tail-1) = 0; |
|
156 |
|
157 if (type == 'M') { |
|
158 strftime(prefix, 12, "[%H:%M] ", localtime(×tamp)); |
|
159 if (info == 'S') |
|
160 strcat(prefix, "--> "); |
|
161 else |
|
162 strcat(prefix, "<== "); |
|
163 hbuf_add_line(p_buddyhbuf, &data[18], prefix, width); |
|
164 } |
|
165 } |
|
166 fclose(fp); |
|
167 g_free(data); |
|
168 } |
|
169 |
110
|
170 // hlog_enable() |
|
171 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
177
|
172 // If loadfiles is TRUE, we will try to load buddies history logs from file. |
|
173 void hlog_enable(guint enable, char *root_dir, guint loadfiles) |
110
|
174 { |
|
175 UseFileLogging = enable; |
177
|
176 FileLoadLogs = loadfiles; |
110
|
177 |
177
|
178 if (enable || loadfiles) { |
110
|
179 if (root_dir) { |
|
180 int l = strlen(root_dir); |
|
181 if (l < 1) { |
|
182 scr_LogPrint("root_dir too short"); |
|
183 UseFileLogging = FALSE; |
|
184 return; |
|
185 } |
|
186 // RootDir must be slash-terminated |
|
187 if (root_dir[l-1] == '/') |
|
188 RootDir = g_strdup(root_dir); |
|
189 else { |
|
190 RootDir = g_new(char, l+2); |
|
191 strcpy(RootDir, root_dir); |
|
192 strcat(RootDir, "/"); |
|
193 } |
|
194 } else { |
|
195 char *home = getenv("HOME"); |
169
|
196 char *dir = "/.mcabber/histo/"; |
110
|
197 RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
|
198 strcpy(RootDir, home); |
|
199 strcat(RootDir, dir); |
|
200 } |
|
201 // FIXME |
|
202 // We should check the directory actually exists |
|
203 } else // Disable history logging |
|
204 if (RootDir) { |
|
205 g_free(RootDir); |
|
206 } |
|
207 } |
|
208 |
113
|
209 inline void hlog_write_message(const char *jid, time_t timestamp, int sent, |
|
210 const char *msg) |
|
211 { |
|
212 write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); |
|
213 } |
|
214 |
|
215 inline void hlog_write_status(const char *jid, time_t timestamp, |
|
216 enum imstatus status) |
|
217 { |
|
218 // #1 XXX Check status value? |
|
219 // #2 We could add a user-readable comment |
|
220 write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]), |
|
221 NULL); |
|
222 } |
|
223 |