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" |
113
|
31 |
#include "jabglue.h" |
110
|
32 |
#include "screen.h" |
|
33 |
|
|
34 |
static guint UseFileLogging; |
|
35 |
static char *RootDir; |
|
36 |
|
|
37 |
|
|
38 |
// user_histo_file() |
|
39 |
// Returns history filename for the given jid |
|
40 |
// Note: the caller *must* free the filename after use (if not null). |
111
|
41 |
static char *user_histo_file(const char *jid) |
110
|
42 |
{ |
|
43 |
char *filename; |
|
44 |
if (!UseFileLogging) |
|
45 |
return NULL; |
|
46 |
|
|
47 |
filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
|
48 |
strcpy(filename, RootDir); |
|
49 |
strcat(filename, jid); |
|
50 |
return filename; |
|
51 |
} |
|
52 |
|
|
53 |
// write() |
|
54 |
// Adds a history (multi-)line to the jid's history logfile |
113
|
55 |
static void write_histo_line(const char *jid, |
|
56 |
time_t timestamp, guchar type, guchar info, const char *data) |
110
|
57 |
{ |
|
58 |
guint len = 0; |
113
|
59 |
FILE *fp; |
110
|
60 |
time_t ts; |
113
|
61 |
const char *p; |
110
|
62 |
char *filename = user_histo_file(jid); |
|
63 |
|
|
64 |
if (!filename) |
|
65 |
return; |
|
66 |
|
|
67 |
// If timestamp is null, get current date |
|
68 |
if (timestamp) |
|
69 |
ts = timestamp; |
|
70 |
else |
|
71 |
time(&ts); |
|
72 |
|
|
73 |
if (!data) |
|
74 |
data = ""; |
|
75 |
|
|
76 |
// Count number of extra lines |
|
77 |
for (p=data ; *p ; p++) |
|
78 |
if (*p == '\n') len++; |
|
79 |
|
118
|
80 |
/* Line format: "TI DDDDDDDDDD LLL [data]" |
|
81 |
* T=Type, I=Info, DDDDDDDDDD=date, LLL=0-padded-len |
110
|
82 |
* |
|
83 |
* Types: |
|
84 |
* - M message Info: S (send) R (receive) |
|
85 |
* - S status Info: [oaifdcn] |
|
86 |
* We don't check them, we'll trust the caller. |
|
87 |
*/ |
|
88 |
|
113
|
89 |
fp = fopen(filename, "a"); |
155
|
90 |
g_free(filename); |
113
|
91 |
if (!fp) |
|
92 |
return; |
118
|
93 |
fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data); |
113
|
94 |
fclose(fp); |
110
|
95 |
} |
|
96 |
|
|
97 |
// hlog_enable() |
|
98 |
// Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
|
99 |
void hlog_enable(guint enable, char *root_dir) |
|
100 |
{ |
|
101 |
UseFileLogging = enable; |
|
102 |
|
|
103 |
if (enable) { |
|
104 |
if (root_dir) { |
|
105 |
int l = strlen(root_dir); |
|
106 |
if (l < 1) { |
|
107 |
scr_LogPrint("root_dir too short"); |
|
108 |
UseFileLogging = FALSE; |
|
109 |
return; |
|
110 |
} |
|
111 |
// RootDir must be slash-terminated |
|
112 |
if (root_dir[l-1] == '/') |
|
113 |
RootDir = g_strdup(root_dir); |
|
114 |
else { |
|
115 |
RootDir = g_new(char, l+2); |
|
116 |
strcpy(RootDir, root_dir); |
|
117 |
strcat(RootDir, "/"); |
|
118 |
} |
|
119 |
} else { |
|
120 |
char *home = getenv("HOME"); |
|
121 |
char *dir = "/.mcabber/"; |
|
122 |
RootDir = g_new(char, strlen(home) + strlen(dir) + 1); |
|
123 |
strcpy(RootDir, home); |
|
124 |
strcat(RootDir, dir); |
|
125 |
} |
|
126 |
// FIXME |
|
127 |
// We should check the directory actually exists |
|
128 |
} else // Disable history logging |
|
129 |
if (RootDir) { |
|
130 |
g_free(RootDir); |
|
131 |
} |
|
132 |
} |
|
133 |
|
113
|
134 |
inline void hlog_write_message(const char *jid, time_t timestamp, int sent, |
|
135 |
const char *msg) |
|
136 |
{ |
|
137 |
write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg); |
|
138 |
} |
|
139 |
|
|
140 |
inline void hlog_write_status(const char *jid, time_t timestamp, |
|
141 |
enum imstatus status) |
|
142 |
{ |
|
143 |
// #1 XXX Check status value? |
|
144 |
// #2 We could add a user-readable comment |
|
145 |
write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]), |
|
146 |
NULL); |
|
147 |
} |
|
148 |
|