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> |
|
25 |
|
|
26 |
#include "histolog.h" |
|
27 |
#include "screen.h" |
|
28 |
|
|
29 |
static guint UseFileLogging; |
|
30 |
static char *RootDir; |
|
31 |
|
|
32 |
|
|
33 |
// user_histo_file() |
|
34 |
// Returns history filename for the given jid |
|
35 |
// Note: the caller *must* free the filename after use (if not null). |
111
|
36 |
static char *user_histo_file(const char *jid) |
110
|
37 |
{ |
|
38 |
char *filename; |
|
39 |
if (!UseFileLogging) |
|
40 |
return NULL; |
|
41 |
|
|
42 |
filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
|
43 |
strcpy(filename, RootDir); |
|
44 |
strcat(filename, jid); |
|
45 |
return filename; |
|
46 |
} |
|
47 |
|
|
48 |
// write() |
|
49 |
// Adds a history (multi-)line to the jid's history logfile |
111
|
50 |
static void write(const char *jid, |
|
51 |
time_t timestamp, guchar type, guchar info, char *data) |
110
|
52 |
{ |
|
53 |
guint len = 0; |
|
54 |
time_t ts; |
|
55 |
char *p; |
|
56 |
char *filename = user_histo_file(jid); |
|
57 |
|
|
58 |
if (!filename) |
|
59 |
return; |
|
60 |
|
|
61 |
// If timestamp is null, get current date |
|
62 |
if (timestamp) |
|
63 |
ts = timestamp; |
|
64 |
else |
|
65 |
time(&ts); |
|
66 |
|
|
67 |
if (!data) |
|
68 |
data = ""; |
|
69 |
|
|
70 |
// Count number of extra lines |
|
71 |
for (p=data ; *p ; p++) |
|
72 |
if (*p == '\n') len++; |
|
73 |
|
|
74 |
/* Line format: "T I DDDDDDDDDD LLL [data]" |
|
75 |
* T=Type, I=Info, D=date, LLL=0-padded-len |
|
76 |
* |
|
77 |
* Types: |
|
78 |
* - M message Info: S (send) R (receive) |
|
79 |
* - S status Info: [oaifdcn] |
|
80 |
* We don't check them, we'll trust the caller. |
|
81 |
*/ |
|
82 |
|
|
83 |
scr_LogPrint("Log to [%s]:", filename); |
|
84 |
scr_LogPrint("%c %c %10d %03d %s", type, info, ts, len, data); |
|
85 |
} |
|
86 |
|
|
87 |
// hlog_enable() |
|
88 |
// Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
|
89 |
void hlog_enable(guint enable, char *root_dir) |
|
90 |
{ |
|
91 |
UseFileLogging = enable; |
|
92 |
|
|
93 |
if (enable) { |
|
94 |
if (root_dir) { |
|
95 |
int l = strlen(root_dir); |
|
96 |
if (l < 1) { |
|
97 |
scr_LogPrint("root_dir too short"); |
|
98 |
UseFileLogging = FALSE; |
|
99 |
return; |
|
100 |
} |
|
101 |
// RootDir must be slash-terminated |
|
102 |
if (root_dir[l-1] == '/') |
|
103 |
RootDir = g_strdup(root_dir); |
|
104 |
else { |
|
105 |
RootDir = g_new(char, l+2); |
|
106 |
strcpy(RootDir, root_dir); |
|
107 |
strcat(RootDir, "/"); |
|
108 |
} |
|
109 |
} else { |
|
110 |
char *home = getenv("HOME"); |
|
111 |
char *dir = "/.mcabber/"; |
|
112 |
RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
|
113 |
strcpy(RootDir, home); |
|
114 |
strcat(RootDir, dir); |
|
115 |
} |
|
116 |
// FIXME |
|
117 |
// We should check the directory actually exists |
|
118 |
} else // Disable history logging |
|
119 |
if (RootDir) { |
|
120 |
g_free(RootDir); |
|
121 |
} |
|
122 |
} |
|
123 |
|