comparison mcabber/src/histolog.c @ 1106:8e12137fab20

Cosmetics and cleanup
author Mikael Berthe <mikael@lilotux.net>
date Thu, 14 Dec 2006 19:17:50 +0100
parents c0d44a9a99bc
children 5be2408a6534
comparison
equal deleted inserted replaced
1105:265e1d873d3e 1106:8e12137fab20
44 // Returns history filename for the given jid 44 // Returns history filename for the given jid
45 // Note: the caller *must* free the filename after use (if not null). 45 // Note: the caller *must* free the filename after use (if not null).
46 static char *user_histo_file(const char *bjid) 46 static char *user_histo_file(const char *bjid)
47 { 47 {
48 char *filename; 48 char *filename;
49 char *lowerid, *p; 49 char *lowerid;
50 if (!UseFileLogging && !FileLoadLogs) return NULL; 50
51 if (!(UseFileLogging || FileLoadLogs))
52 return NULL;
51 53
52 lowerid = g_strdup(bjid); 54 lowerid = g_strdup(bjid);
53 for (p=lowerid; *p ; p++) 55 if (!lowerid)
54 *p = tolower(*p); 56 return NULL;
55 57 mc_strtolower(lowerid);
56 filename = g_new(char, strlen(RootDir) + strlen(bjid) + 1); 58
57 strcpy(filename, RootDir); 59 filename = g_strdup_printf("%s%s", RootDir, lowerid);
58 strcat(filename, lowerid);
59 g_free(lowerid); 60 g_free(lowerid);
60 return filename; 61 return filename;
61 } 62 }
62 63
63 // write_histo_line() 64 // write_histo_line()
71 const char *p; 72 const char *p;
72 char *filename; 73 char *filename;
73 char str_ts[20]; 74 char str_ts[20];
74 int err; 75 int err;
75 76
76 if (!UseFileLogging) return; 77 if (!UseFileLogging)
78 return;
77 79
78 filename = user_histo_file(bjid); 80 filename = user_histo_file(bjid);
79 81
80 // If timestamp is null, get current date 82 // If timestamp is null, get current date
81 if (timestamp) 83 if (timestamp)
132 struct stat bufstat; 134 struct stat bufstat;
133 guint err = 0; 135 guint err = 0;
134 guint ln = 0; // line number 136 guint ln = 0; // line number
135 time_t starttime; 137 time_t starttime;
136 138
137 if (!FileLoadLogs) return; 139 if (!FileLoadLogs)
140 return;
138 141
139 if ((roster_gettype(bjid) & ROSTER_TYPE_ROOM) && 142 if ((roster_gettype(bjid) & ROSTER_TYPE_ROOM) &&
140 (settings_opt_get_int("load_muc_logs") != 1)) 143 (settings_opt_get_int("load_muc_logs") != 1))
141 return; 144 return;
142 145
148 151
149 filename = user_histo_file(bjid); 152 filename = user_histo_file(bjid);
150 153
151 fp = fopen(filename, "r"); 154 fp = fopen(filename, "r");
152 g_free(filename); 155 g_free(filename);
153 if (!fp) { g_free(data); return; } 156 if (!fp) {
157 g_free(data);
158 return;
159 }
154 160
155 // If file is large (> 3MB here), display a message to inform the user 161 // If file is large (> 3MB here), display a message to inform the user
156 // (it can take a while...) 162 // (it can take a while...)
157 if (!fstat(fileno(fp), &bufstat)) { 163 if (!fstat(fileno(fp), &bufstat)) {
158 if (bufstat.st_size > 3145728) 164 if (bufstat.st_size > 3145728)
159 scr_LogPrint(LPRINT_LOGNORM, "Reading <%s> history file...", bjid); 165 scr_LogPrint(LPRINT_LOGNORM, "Reading <%s> history file...", bjid);
160 } 166 }
161 167
162 starttime = 0; 168 starttime = 0L;
163 if (settings_opt_get_int("max_history_age") > 0) { 169 if (settings_opt_get_int("max_history_age") > 0) {
164 int maxdays = settings_opt_get_int("max_history_age"); 170 int maxdays = settings_opt_get_int("max_history_age");
165 time(&starttime); 171 time(&starttime);
166 if (maxdays >= starttime/86400) 172 if (maxdays >= starttime/86400L)
167 starttime = 0; 173 starttime = 0L;
168 else 174 else
169 starttime -= maxdays * 86400; 175 starttime -= maxdays * 86400L;
170 } 176 }
171 177
172 /* See write_histo_line() for line format... */ 178 /* See write_histo_line() for line format... */
173 while (!feof(fp)) { 179 while (!feof(fp)) {
174 guint dataoffset = 25; 180 guint dataoffset = 25;
175 if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL) break; 181
182 if (fgets(data, HBB_BLOCKSIZE+27, fp) == NULL)
183 break;
176 ln++; 184 ln++;
177 185
178 for (tail = data; *tail; tail++) ; 186 for (tail = data; *tail; tail++) ;
179 187
180 type = data[0]; 188 type = data[0];
228 *(tail-1) = 0; 236 *(tail-1) = 0;
229 237
230 // Check if the data is older than max_history_age 238 // Check if the data is older than max_history_age
231 if (starttime) { 239 if (starttime) {
232 if (timestamp > starttime) 240 if (timestamp > starttime)
233 starttime = 0; // From now on, load everything 241 starttime = 0L; // From now on, load everything
234 else 242 else
235 continue; 243 continue;
236 } 244 }
237 245
238 if (type == 'M') { 246 if (type == 'M') {
273 return; 281 return;
274 } 282 }
275 // RootDir must be slash-terminated 283 // RootDir must be slash-terminated
276 if (root_dir[l-1] == '/') 284 if (root_dir[l-1] == '/')
277 RootDir = g_strdup(root_dir); 285 RootDir = g_strdup(root_dir);
278 else { 286 else
279 RootDir = g_new(char, l+2); 287 RootDir = g_strdup_printf("%s/", root_dir);
280 strcpy(RootDir, root_dir);
281 strcat(RootDir, "/");
282 }
283 } else { 288 } else {
284 char *home = getenv("HOME"); 289 char *home = getenv("HOME");
285 char *dir = "/.mcabber/histo/"; 290 const char *dir = "/.mcabber/histo/";
286 RootDir = g_new(char, strlen(home) + strlen(dir) + 1); 291 RootDir = g_strdup_printf("%s%s", home, dir);
287 strcpy(RootDir, home);
288 strcat(RootDir, dir);
289 } 292 }
290 // Check directory permissions (should not be readable by group/others) 293 // Check directory permissions (should not be readable by group/others)
291 if (checkset_perm(RootDir, TRUE) == -1) { 294 if (checkset_perm(RootDir, TRUE) == -1) {
292 // The directory does not actually exists 295 // The directory does not actually exists
293 g_free(RootDir); 296 g_free(RootDir);
297 RootDir = NULL;
294 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access " 298 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access "
295 "history log directory, logging DISABLED"); 299 "history log directory, logging DISABLED");
296 UseFileLogging = FileLoadLogs = FALSE; 300 UseFileLogging = FileLoadLogs = FALSE;
297 } 301 }
298 } else { // Disable history logging 302 } else { // Disable history logging
299 g_free(RootDir); 303 g_free(RootDir);
304 RootDir = NULL;
300 } 305 }
301 } 306 }
302 307
303 inline void hlog_write_message(const char *bjid, time_t timestamp, int sent, 308 inline void hlog_write_message(const char *bjid, time_t timestamp, int sent,
304 const char *msg) 309 const char *msg)