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