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");
|
|
90 if (!fp)
|
|
91 return;
|
118
|
92 fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data);
|
113
|
93 fclose(fp);
|
110
|
94 }
|
|
95
|
|
96 // hlog_enable()
|
|
97 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used.
|
|
98 void hlog_enable(guint enable, char *root_dir)
|
|
99 {
|
|
100 UseFileLogging = enable;
|
|
101
|
|
102 if (enable) {
|
|
103 if (root_dir) {
|
|
104 int l = strlen(root_dir);
|
|
105 if (l < 1) {
|
|
106 scr_LogPrint("root_dir too short");
|
|
107 UseFileLogging = FALSE;
|
|
108 return;
|
|
109 }
|
|
110 // RootDir must be slash-terminated
|
|
111 if (root_dir[l-1] == '/')
|
|
112 RootDir = g_strdup(root_dir);
|
|
113 else {
|
|
114 RootDir = g_new(char, l+2);
|
|
115 strcpy(RootDir, root_dir);
|
|
116 strcat(RootDir, "/");
|
|
117 }
|
|
118 } else {
|
|
119 char *home = getenv("HOME");
|
|
120 char *dir = "/.mcabber/";
|
|
121 RootDir = g_new(char, strlen(home) + strlen(dir) + 1);
|
|
122 strcpy(RootDir, home);
|
|
123 strcat(RootDir, dir);
|
|
124 }
|
|
125 // FIXME
|
|
126 // We should check the directory actually exists
|
|
127 } else // Disable history logging
|
|
128 if (RootDir) {
|
|
129 g_free(RootDir);
|
|
130 }
|
|
131 }
|
|
132
|
113
|
133 inline void hlog_write_message(const char *jid, time_t timestamp, int sent,
|
|
134 const char *msg)
|
|
135 {
|
|
136 write_histo_line(jid, timestamp, 'M', ((sent) ? 'S' : 'R'), msg);
|
|
137 }
|
|
138
|
|
139 inline void hlog_write_status(const char *jid, time_t timestamp,
|
|
140 enum imstatus status)
|
|
141 {
|
|
142 // #1 XXX Check status value?
|
|
143 // #2 We could add a user-readable comment
|
|
144 write_histo_line(jid, timestamp, 'S', toupper(imstatus2char[status]),
|
|
145 NULL);
|
|
146 }
|
|
147
|