annotate mcabber/mcabber/histolog.c @ 2205:1177b33d4550

Do not hardcode the default logging location The current configuration directory is used as a base.
author Mikael Berthe <mikael@lilotux.net>
date Sat, 17 Oct 2015 20:19:58 +0200
parents 09508eeb81dc
children 736f9323d701
rev   line source
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
1 /*
699
ee03b56b93ee Update Copyright (2006)
Mikael Berthe <mikael@lilotux.net>
parents: 635
diff changeset
2 * histolog.c -- File history handling
395
3e4f2f98c0bf Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents: 394
diff changeset
3 *
1729
e6e89b1d7831 Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents: 1684
diff changeset
4 * Copyright (C) 2005-2010 Mikael Berthe <mikael@lilotux.net>
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
5 *
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or (at
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
9 * your option) any later version.
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
10 *
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful, but
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
14 * General Public License for more details.
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
15 *
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
19 * USA
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
20 */
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
21
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
22 #include <ctype.h>
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
23 #include <errno.h>
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
24 #include <fcntl.h>
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
25 #include <stdlib.h>
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
26 #include <string.h>
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
27 #include <sys/types.h>
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
28 #include <sys/stat.h>
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
29 #include <time.h>
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
30 #include <unistd.h>
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
31
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
32 #include "histolog.h"
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
33 #include "hbuf.h"
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
34 #include "utils.h"
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
35 #include "screen.h"
635
d4119cb85aeb New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
36 #include "settings.h"
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
37 #include "utils.h"
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
38 #include "roster.h"
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
39 #include "xmpp.h"
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
40
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
41 static guint UseFileLogging;
177
a51ce78a0e2a [/trunk] Changeset 189 by mikael
mikael
parents: 169
diff changeset
42 static guint FileLoadLogs;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
43 static char *RootDir;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
44
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
45
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
46 // user_histo_file(jid)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
47 // Returns history filename for the given jid
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
48 // Note: the caller *must* free the filename after use (if not null).
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
49 static char *user_histo_file(const char *bjid)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
50 {
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
51 char *filename;
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
52 char *lowerid;
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
53
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
54 if (!(UseFileLogging || FileLoadLogs))
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
55 return NULL;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
56
1864
27c42ee4b6f0 Add a safety check to user_histo_file()
Mikael Berthe <mikael@lilotux.net>
parents: 1815
diff changeset
57 if (!g_strcmp0(bjid, ".") || !g_strcmp0(bjid, "..") ||
1865
e9773bd9dd2e Fix parent (27c42ee4b6f0) :-/
Mikael Berthe <mikael@lilotux.net>
parents: 1864
diff changeset
58 strchr(bjid, '/'))
1864
27c42ee4b6f0 Add a safety check to user_histo_file()
Mikael Berthe <mikael@lilotux.net>
parents: 1815
diff changeset
59 return NULL;
27c42ee4b6f0 Add a safety check to user_histo_file()
Mikael Berthe <mikael@lilotux.net>
parents: 1815
diff changeset
60
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
61 lowerid = g_strdup(bjid);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
62 if (!lowerid)
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
63 return NULL;
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
64 mc_strtolower(lowerid);
186
888ad9f15346 [/trunk] Changeset 198 by mikael
mikael
parents: 184
diff changeset
65
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
66 filename = g_strdup_printf("%s%s", RootDir, lowerid);
186
888ad9f15346 [/trunk] Changeset 198 by mikael
mikael
parents: 184
diff changeset
67 g_free(lowerid);
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
68 return filename;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
69 }
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
70
1155
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
71 char *hlog_get_log_jid(const char *bjid)
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
72 {
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
73 struct stat bufstat;
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
74 char *path;
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
75 char *log_jid = NULL;
1303
b6fdbfa6b219 Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
76
1155
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
77 path = user_histo_file(bjid);
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
78 while (path) {
1303
b6fdbfa6b219 Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
79 if (lstat(path, &bufstat) != 0)
1155
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
80 break;
1303
b6fdbfa6b219 Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
81 if (S_ISLNK(bufstat.st_mode)) {
1155
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
82 g_free(log_jid);
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
83 log_jid = g_new0(char, bufstat.st_size+1);
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
84 if (readlink(path, log_jid, bufstat.st_size) < 0) return NULL;
1155
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
85 g_free(path);
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
86 path = user_histo_file(log_jid);
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
87 } else
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
88 break;
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
89 }
1155
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
90
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
91 g_free(path);
1155
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
92 return log_jid;
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
93 }
3b9bbf6c4c93 Added hlog_get_log_jid(): returns the value of a symlinked history file or NULL if it isn't symlinked
Frank Zschockelt
parents: 1144
diff changeset
94
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
95 // write_histo_line()
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
96 // Adds a history (multi-)line to the jid's history logfile
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
97 static void write_histo_line(const char *bjid,
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
98 time_t timestamp, guchar type, guchar info, const char *data)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
99 {
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
100 guint len = 0;
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
101 FILE *fp;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
102 time_t ts;
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
103 const char *p;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
104 char *filename;
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
105 char str_ts[20];
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
106 int err;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
107
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
108 if (!UseFileLogging)
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
109 return;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
110
1144
b8f5fe2d666a Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
111 // Do not log status messages when 'logging_ignore_status' is set
b8f5fe2d666a Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
112 if (type == 'S' && settings_opt_get_int("logging_ignore_status"))
b8f5fe2d666a Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
113 return;
b8f5fe2d666a Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
114
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
115 filename = user_histo_file(bjid);
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
116
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
117 // If timestamp is null, get current date
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
118 if (timestamp)
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
119 ts = timestamp;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
120 else
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
121 time(&ts);
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
122
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
123 if (!data)
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
124 data = "";
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
125
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
126 // Count number of extra lines
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
127 for (p=data ; *p ; p++)
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
128 if (*p == '\n') len++;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
129
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
130 /* Line format: "TI yyyymmddThh:mm:ssZ LLL [data]"
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
131 * T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
132 *
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
133 * Types:
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
134 * - M message Info: S (send) R (receive) I (info)
1311
0dda8238af21 Implement "/roster display"
Mikael Berthe <mikael@lilotux.net>
parents: 1303
diff changeset
135 * - S status Info: [_ofdnai]
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
136 * We don't check them, we trust the caller.
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
137 * (Info messages are not sent nor received, they're generated
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
138 * locally by mcabber.)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
139 */
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
140
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
141 fp = fopen(filename, "a");
155
8a54d46e889a [/trunk] Changeset 167 by mikael
mikael
parents: 118
diff changeset
142 g_free(filename);
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
143 if (!fp) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
144 scr_LogPrint(LPRINT_LOGNORM, "Unable to write history "
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
145 "(cannot open logfile)");
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
146 return;
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
147 }
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
148
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
149 to_iso8601(str_ts, ts);
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
150 err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
151 fclose(fp);
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
152 if (err < 0) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
153 scr_LogPrint(LPRINT_LOGNORM, "Error while writing to log file: %s",
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
154 strerror(errno));
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
155 }
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
156 }
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
157
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
158 // hlog_read_history()
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
159 // Reads the jid's history logfile
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
160 void hlog_read_history(const char *bjid, GList **p_buddyhbuf, guint width)
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
161 {
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
162 char *filename;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
163 guchar type, info;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
164 char *data, *tail;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
165 guint data_size;
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
166 char *xtext;
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 183
diff changeset
167 time_t timestamp;
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 183
diff changeset
168 guint prefix_flags;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
169 guint len;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
170 FILE *fp;
197
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
171 struct stat bufstat;
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
172 guint err = 0;
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
173 guint ln = 0; // line number
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
174 time_t starttime;
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
175 int max_num_of_blocks;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
176
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
177 if (!FileLoadLogs)
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
178 return;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
179
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
180 if ((roster_gettype(bjid) & ROSTER_TYPE_ROOM) &&
635
d4119cb85aeb New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
181 (settings_opt_get_int("load_muc_logs") != 1))
d4119cb85aeb New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
182 return;
d4119cb85aeb New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
183
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
184 data_size = HBB_BLOCKSIZE+32;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
185 data = g_new(char, data_size);
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
186 if (!data) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
187 scr_LogPrint(LPRINT_LOGNORM, "Not enough memory to read history file");
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
188 return;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
189 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
190
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
191 filename = user_histo_file(bjid);
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
192
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
193 fp = fopen(filename, "r");
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
194 g_free(filename);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
195 if (!fp) {
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
196 g_free(data);
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
197 return;
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
198 }
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
199
394
4617d0f029ea Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents: 378
diff changeset
200 // If file is large (> 3MB here), display a message to inform the user
197
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
201 // (it can take a while...)
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
202 if (!fstat(fileno(fp), &bufstat)) {
1274
c33b5b6fbf08 Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
203 if (bufstat.st_size > 3145728) {
c33b5b6fbf08 Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
204 scr_LogPrint(LPRINT_NORMAL, "Reading <%s> history file...", bjid);
1780
e4378fbab5d7 Major API cleanup - bump API to 4
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
205 scr_do_update();
1274
c33b5b6fbf08 Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
206 }
197
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
207 }
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
208
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
209 max_num_of_blocks = get_max_history_blocks();
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
210
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
211 starttime = 0L;
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
212 if (settings_opt_get_int("max_history_age") > 0) {
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
213 int maxdays = settings_opt_get_int("max_history_age");
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
214 time(&starttime);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
215 if (maxdays >= starttime/86400L)
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
216 starttime = 0L;
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
217 else
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
218 starttime -= maxdays * 86400L;
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
219 }
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
220
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
221 /* See write_histo_line() for line format... */
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
222 while (!feof(fp)) {
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
223 guint dataoffset = 25;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
224 guint noeol;
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
225
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
226 if (fgets(data, data_size-1, fp) == NULL)
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
227 break;
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
228 ln++;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
229
2182
09508eeb81dc Fix reading of history log files w/o LF character (Sam Whited)
Mikael Berthe <mikael@lilotux.net>
parents: 2073
diff changeset
230 while (!feof(fp)) {
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
231 for (tail = data; *tail; tail++) ;
2073
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
232 if (tail == data) {
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
233 // That would happen if the log file has NUL characters...
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
234 scr_LogPrint(LPRINT_LOGNORM, "Corrupted history file! Trying to recover.");
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
235 err = 1;
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
236 break;
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
237 }
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
238 noeol = (*(tail-1) != '\n');
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
239 if (!noeol)
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
240 break;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
241 /* TODO: duplicated code... could do better... */
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
242 if (tail == data + data_size-2) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
243 // The buffer is too small to contain the whole line.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
244 // Let's allocate some more space.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
245 if (!max_num_of_blocks ||
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
246 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
247 guint toffset = tail - data;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
248 // Allocate one more block.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
249 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
250 data = g_renew(char, data, data_size);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
251 // Update the tail pointer, as the data may have been moved.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
252 tail = data + toffset;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
253 if (fgets(tail, data_size-1 - (tail-data), fp) == NULL)
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
254 break;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
255 } else {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
256 scr_LogPrint(LPRINT_LOGNORM, "Line too long in history file!");
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
257 ln--;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
258 break;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
259 }
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
260 }
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
261 }
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
262
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
263 type = data[0];
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
264 info = data[1];
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
265
395
3e4f2f98c0bf Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents: 394
diff changeset
266 if ((type != 'M' && type != 'S') ||
259
594e73bc9610 Remove support for old logfile format
mikael@frmp8452
parents: 251
diff changeset
267 ((data[11] != 'T') || (data[20] != 'Z') ||
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
268 (data[21] != ' ') ||
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
269 (data[25] != ' ' && data[26] != ' '))) {
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
270 if (!err) {
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
271 scr_LogPrint(LPRINT_LOGNORM,
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
272 "Error in history file format (%s), l.%u", bjid, ln);
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
273 err = 1;
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
274 }
186
888ad9f15346 [/trunk] Changeset 198 by mikael
mikael
parents: 184
diff changeset
275 continue;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
276 }
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
277 // The number of lines can be written with 3 or 4 bytes.
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
278 if (data[25] != ' ') dataoffset = 26;
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
279 data[21] = data[dataoffset] = 0;
259
594e73bc9610 Remove support for old logfile format
mikael@frmp8452
parents: 251
diff changeset
280 timestamp = from_iso8601(&data[3], 1);
594e73bc9610 Remove support for old logfile format
mikael@frmp8452
parents: 251
diff changeset
281 len = (guint) atoi(&data[22]);
395
3e4f2f98c0bf Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents: 394
diff changeset
282
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
283 // Some checks
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
284 if (((type == 'M') && (info != 'S' && info != 'R' && info != 'I')) ||
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
285 ((type == 'S') && (!strchr("_OFDNAI", info)))) {
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
286 if (!err) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
287 scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u",
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
288 bjid, ln);
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
289 err = 1;
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
290 }
186
888ad9f15346 [/trunk] Changeset 198 by mikael
mikael
parents: 184
diff changeset
291 continue;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
292 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
293
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
294 while (len--) {
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
295 ln++;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
296 if (fgets(tail, data_size-1 - (tail-data), fp) == NULL)
249
c3e6e78d1ab4 [/trunk] Changeset 262 by mikael
mikael
parents: 248
diff changeset
297 break;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
298
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
299 while (*tail) tail++;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
300 noeol = (*(tail-1) != '\n');
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
301 if (tail == data + data_size-2 && (len || noeol)) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
302 // The buffer is too small to contain the whole message.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
303 // Let's allocate some more space.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
304 if (!max_num_of_blocks ||
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
305 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
306 guint toffset = tail - data;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
307 // If the line hasn't been read completely and we reallocate the
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
308 // buffer, we want to read one more time.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
309 if (noeol)
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
310 len++;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
311 // Allocate one more block.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
312 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
313 data = g_renew(char, data, data_size);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
314 // Update the tail pointer, as the data may have been moved.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
315 tail = data + toffset;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
316 } else {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
317 // There will probably be a parse error on next read, because
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
318 // this message hasn't been read entirely.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
319 scr_LogPrint(LPRINT_LOGNORM, "Message too big in history file!");
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
320 }
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
321 }
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
322 }
251
806fcdd3b5da [/trunk] Changeset 264 by mikael
mikael
parents: 250
diff changeset
323 // Remove last CR (we keep it if the line is empty, too)
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
324 if ((tail > data+dataoffset+1) && (*(tail-1) == '\n'))
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
325 *(tail-1) = 0;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
326
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
327 // Check if the data is older than max_history_age
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
328 if (starttime) {
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
329 if (timestamp > starttime)
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
330 starttime = 0L; // From now on, load everything
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
331 else
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
332 continue;
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
333 }
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
334
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
335 if (type == 'M') {
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
336 char *converted;
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
337 if (info == 'S') {
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
338 prefix_flags = HBB_PREFIX_OUT | HBB_PREFIX_HLIGHT_OUT;
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
339 } else {
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 183
diff changeset
340 prefix_flags = HBB_PREFIX_IN;
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
341 if (info == 'I')
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
342 prefix_flags = HBB_PREFIX_INFO;
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
343 }
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
344 converted = from_utf8(&data[dataoffset+1]);
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
345 if (converted) {
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
346 xtext = ut_expand_tabs(converted); // Expand tabs
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
347 hbuf_add_line(p_buddyhbuf, xtext, timestamp, prefix_flags, width,
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
348 max_num_of_blocks, 0, NULL);
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
349 if (xtext != converted)
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
350 g_free(xtext);
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
351 g_free(converted);
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
352 }
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
353 err = 0;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
354 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
355 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
356 fclose(fp);
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
357 g_free(data);
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
358 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
359
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
360 // hlog_enable()
2205
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
361 // Enable logging to files. If root_dir is NULL, then the subdirectory "histo"
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
362 // in mcabber configuration directory is used.
177
a51ce78a0e2a [/trunk] Changeset 189 by mikael
mikael
parents: 169
diff changeset
363 // If loadfiles is TRUE, we will try to load buddies history logs from file.
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 277
diff changeset
364 void hlog_enable(guint enable, const char *root_dir, guint loadfiles)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
365 {
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
366 UseFileLogging = enable;
177
a51ce78a0e2a [/trunk] Changeset 189 by mikael
mikael
parents: 169
diff changeset
367 FileLoadLogs = loadfiles;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
368
177
a51ce78a0e2a [/trunk] Changeset 189 by mikael
mikael
parents: 169
diff changeset
369 if (enable || loadfiles) {
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
370 if (root_dir) {
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
371 char *xp_root_dir;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
372 int l = strlen(root_dir);
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
373 if (l < 1) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
374 scr_LogPrint(LPRINT_LOGNORM, "Error: logging dir name too short");
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
375 UseFileLogging = FileLoadLogs = FALSE;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
376 return;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
377 }
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
378 xp_root_dir = expand_filename(root_dir);
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
379 // RootDir must be slash-terminated
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
380 if (root_dir[l-1] == '/') {
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
381 RootDir = xp_root_dir;
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
382 } else {
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
383 RootDir = g_strdup_printf("%s/", xp_root_dir);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
384 g_free(xp_root_dir);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
385 }
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
386 } else {
2205
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
387 const char *cfgdir = settings_get_mcabber_config_dir();
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
388 const char *hdir = "/histo/";
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
389 RootDir = g_strdup_printf("%s%s", cfgdir, hdir);
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
390 }
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
391 // Check directory permissions (should not be readable by group/others)
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
392 if (checkset_perm(RootDir, TRUE) == -1) {
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
393 // The directory does not actually exists
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
394 g_free(RootDir);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
395 RootDir = NULL;
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
396 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access "
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
397 "history log directory, logging DISABLED");
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
398 UseFileLogging = FileLoadLogs = FALSE;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
399 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
400 } else { // Disable history logging
774
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
401 g_free(RootDir);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
402 RootDir = NULL;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
403 }
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
404 }
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
405
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
406 guint hlog_is_enabled(void)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
407 {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
408 return UseFileLogging;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
409 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
410
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
411 inline void hlog_write_message(const char *bjid, time_t timestamp, int sent,
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
412 const char *msg)
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
413 {
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
414 guchar info;
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
415 /* sent=1 message sent by mcabber
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
416 * sent=0 message received by mcabber
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
417 * sent=-1 local info message
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
418 */
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
419 if (sent == 1)
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
420 info = 'S';
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
421 else if (sent == 0)
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
422 info = 'R';
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
423 else
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
424 info = 'I';
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
425 write_histo_line(bjid, timestamp, 'M', info, msg);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
426 }
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
427
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
428 inline void hlog_write_status(const char *bjid, time_t timestamp,
221
73f6ce668ba8 [/trunk] Changeset 233 by mikael
mikael
parents: 197
diff changeset
429 enum imstatus status, const char *status_msg)
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
430 {
1205
2de8f8ba1f34 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 1204
diff changeset
431 // XXX Check status value?
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
432 write_histo_line(bjid, timestamp, 'S', toupper(imstatus2char[status]),
221
73f6ce668ba8 [/trunk] Changeset 233 by mikael
mikael
parents: 197
diff changeset
433 status_msg);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
434 }
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
435
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
436
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
437 // hlog_save_state()
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
438 // If enabled, save the current state of the roster
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
439 // (i.e. pending messages) to a temporary file.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
440 void hlog_save_state(void)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
441 {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
442 gpointer unread_ptr, first_unread;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
443 const char *bjid;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
444 char *statefile_xp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
445 FILE *fp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
446 const char *statefile = settings_opt_get("statefile");
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
447
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
448 if (!statefile || !UseFileLogging)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
449 return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
450
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
451 statefile_xp = expand_filename(statefile);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
452 fp = fopen(statefile_xp, "w");
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
453 if (!fp) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
454 scr_LogPrint(LPRINT_NORMAL, "Cannot open state file [%s]",
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
455 strerror(errno));
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
456 goto hlog_save_state_return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
457 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
458
1684
95df4ea512c8 Provide xmpp_is_online()
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1682
diff changeset
459 if (!xmpp_is_online()) {
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
460 // We're not connected. Let's use the unread_jids hash.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
461 GList *unread_jid = unread_jid_get_list();
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
462 unread_ptr = unread_jid;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
463 for ( ; unread_jid ; unread_jid = g_list_next(unread_jid))
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
464 fprintf(fp, "%s\n", (char*)unread_jid->data);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
465 g_list_free(unread_ptr);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
466 goto hlog_save_state_return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
467 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
468
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
469 if (!current_buddy) // Safety check -- shouldn't happen.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
470 goto hlog_save_state_return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
471
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
472 // We're connected. Let's use unread_msg().
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
473 unread_ptr = first_unread = unread_msg(NULL);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
474 if (!first_unread)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
475 goto hlog_save_state_return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
476
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
477 do {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
478 guint type = buddy_gettype(unread_ptr);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
479 if (type & (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT)) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
480 bjid = buddy_getjid(unread_ptr);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
481 if (bjid)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
482 fprintf(fp, "%s\n", bjid);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
483 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
484 unread_ptr = unread_msg(unread_ptr);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
485 } while (unread_ptr && unread_ptr != first_unread);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
486
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
487 hlog_save_state_return:
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
488 if (fp) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
489 long filelen = ftell(fp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
490 fclose(fp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
491 if (!filelen)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
492 unlink(statefile_xp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
493 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
494 g_free(statefile_xp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
495 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
496
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
497 // hlog_load_state()
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
498 // If enabled, load the current state of the roster
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
499 // (i.e. pending messages) from a temporary file.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
500 // This function adds the JIDs to the unread_jids hash table,
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
501 // so it should only be called at startup.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
502 void hlog_load_state(void)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
503 {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
504 char bjid[1024];
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
505 char *statefile_xp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
506 FILE *fp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
507 const char *statefile = settings_opt_get("statefile");
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
508
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
509 if (!statefile || !UseFileLogging)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
510 return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
511
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
512 statefile_xp = expand_filename(statefile);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
513 fp = fopen(statefile_xp, "r");
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
514 if (fp) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
515 char *eol;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
516 while (!feof(fp)) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
517 if (fgets(bjid, sizeof bjid, fp) == NULL)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
518 break;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
519 // Let's remove the trailing newline.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
520 // Also remove whitespace, if the file as been (badly) manually modified.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
521 for (eol = bjid; *eol; eol++) ;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
522 for (eol--; eol >= bjid && (*eol == '\n' || *eol == ' '); *eol-- = 0) ;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
523 // Safety checks...
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
524 if (!bjid[0])
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
525 continue;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
526 if (check_jid_syntax(bjid)) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
527 scr_LogPrint(LPRINT_LOGNORM,
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
528 "ERROR: Invalid JID in state file. Corrupted file?");
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
529 break;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
530 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
531 // Display a warning if there are pending messages but the user
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
532 // won't see them because load_log isn't set.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
533 if (!FileLoadLogs) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
534 scr_LogPrint(LPRINT_LOGNORM, "WARNING: unread message from <%s>.",
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
535 bjid);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
536 scr_setmsgflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE);
1815
6abca6000762 Make use of ROSTER_UI_PRIO_STATUS_WIN_MESSAGE
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
537 scr_setattentionflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE,
6abca6000762 Make use of ROSTER_UI_PRIO_STATUS_WIN_MESSAGE
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
538 ROSTER_UI_PRIO_STATUS_WIN_MESSAGE, prio_max);
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
539 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
540 // Add the JID to unread_jids. It will be used when the contact is
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
541 // added to the roster.
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
542 unread_jid_add(bjid);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
543 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
544 fclose(fp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
545 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
546 g_free(statefile_xp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
547 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
548
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
549 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */