annotate mcabber/mcabber/histolog.c @ 2224:736f9323d701

Update comments to hlog_get_log_jid()
author Mikael Berthe <mikael@lilotux.net>
date Sat, 07 Nov 2015 12:16:15 +0100
parents 1177b33d4550
children c06488852cdc
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 *
2224
736f9323d701 Update comments to hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 2205
diff changeset
4 * Copyright (C) 2005-2015 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
2224
736f9323d701 Update comments to hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 2205
diff changeset
71 // hlog_get_log_jid(bare_jid)
736f9323d701 Update comments to hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 2205
diff changeset
72 // Returns the real JID used for a symlinked history log file,
736f9323d701 Update comments to hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 2205
diff changeset
73 // or NULL if there's no symbolic link.
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
74 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
75 {
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
76 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
77 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
78 char *log_jid = NULL;
1303
b6fdbfa6b219 Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
79
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 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
81 while (path) {
1303
b6fdbfa6b219 Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
82 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
83 break;
1303
b6fdbfa6b219 Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
84 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
85 g_free(log_jid);
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
86 log_jid = g_new0(char, bufstat.st_size+1);
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1560
diff changeset
87 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
88 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
89 path = user_histo_file(log_jid);
2224
736f9323d701 Update comments to hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 2205
diff changeset
90 } else {
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
91 break;
2224
736f9323d701 Update comments to hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 2205
diff changeset
92 }
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
93 }
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
94
1560
ec55cdf44335 Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents: 1437
diff changeset
95 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
96 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
97 }
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
98
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
99 // write_histo_line()
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
100 // Adds a history (multi-)line to the jid's history logfile
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
101 static void write_histo_line(const char *bjid,
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
102 time_t timestamp, guchar type, guchar info, const char *data)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
103 {
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
104 guint len = 0;
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
105 FILE *fp;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
106 time_t ts;
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
107 const char *p;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
108 char *filename;
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
109 char str_ts[20];
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
110 int err;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
111
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
112 if (!UseFileLogging)
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
113 return;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
114
1144
b8f5fe2d666a Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
115 // 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
116 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
117 return;
b8f5fe2d666a Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
118
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
119 filename = user_histo_file(bjid);
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
120
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
121 // If timestamp is null, get current date
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
122 if (timestamp)
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
123 ts = timestamp;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
124 else
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
125 time(&ts);
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
126
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
127 if (!data)
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
128 data = "";
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
129
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
130 // Count number of extra lines
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
131 for (p=data ; *p ; p++)
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
132 if (*p == '\n') len++;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
133
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
134 /* Line format: "TI yyyymmddThh:mm:ssZ LLL [data]"
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
135 * T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
136 *
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
137 * Types:
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
138 * - M message Info: S (send) R (receive) I (info)
1311
0dda8238af21 Implement "/roster display"
Mikael Berthe <mikael@lilotux.net>
parents: 1303
diff changeset
139 * - 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
140 * 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
141 * (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
142 * locally by mcabber.)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
143 */
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
144
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
145 fp = fopen(filename, "a");
155
8a54d46e889a [/trunk] Changeset 167 by mikael
mikael
parents: 118
diff changeset
146 g_free(filename);
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
147 if (!fp) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
148 scr_LogPrint(LPRINT_LOGNORM, "Unable to write history "
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
149 "(cannot open logfile)");
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
150 return;
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
151 }
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
152
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
153 to_iso8601(str_ts, ts);
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
154 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
155 fclose(fp);
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
156 if (err < 0) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
157 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
158 strerror(errno));
273
fe438fe67b6f Error checking when writing log files
mikael@frmp8452
parents: 259
diff changeset
159 }
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
160 }
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
161
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
162 // hlog_read_history()
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
163 // Reads the jid's history logfile
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
164 void hlog_read_history(const char *bjid, GList **p_buddyhbuf, guint width)
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
165 {
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
166 char *filename;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
167 guchar type, info;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
168 char *data, *tail;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
169 guint data_size;
727
1c3620668857 Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
170 char *xtext;
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 183
diff changeset
171 time_t timestamp;
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 183
diff changeset
172 guint prefix_flags;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
173 guint len;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
174 FILE *fp;
197
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
175 struct stat bufstat;
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
176 guint err = 0;
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
177 guint ln = 0; // line number
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
178 time_t starttime;
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
179 int max_num_of_blocks;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
180
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
181 if (!FileLoadLogs)
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
182 return;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
183
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
184 if ((roster_gettype(bjid) & ROSTER_TYPE_ROOM) &&
635
d4119cb85aeb New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
185 (settings_opt_get_int("load_muc_logs") != 1))
d4119cb85aeb New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
186 return;
d4119cb85aeb New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents: 580
diff changeset
187
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
188 data_size = HBB_BLOCKSIZE+32;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
189 data = g_new(char, data_size);
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
190 if (!data) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
191 scr_LogPrint(LPRINT_LOGNORM, "Not enough memory to read history file");
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
192 return;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
193 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
194
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
195 filename = user_histo_file(bjid);
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
196
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
197 fp = fopen(filename, "r");
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
198 g_free(filename);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
199 if (!fp) {
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
200 g_free(data);
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
201 return;
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
202 }
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
203
394
4617d0f029ea Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents: 378
diff changeset
204 // 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
205 // (it can take a while...)
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
206 if (!fstat(fileno(fp), &bufstat)) {
1274
c33b5b6fbf08 Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
207 if (bufstat.st_size > 3145728) {
c33b5b6fbf08 Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
208 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
209 scr_do_update();
1274
c33b5b6fbf08 Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
210 }
197
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
211 }
c289e3c39c48 [/trunk] Changeset 209 by mikael
mikael
parents: 193
diff changeset
212
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
213 max_num_of_blocks = get_max_history_blocks();
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
214
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
215 starttime = 0L;
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
216 if (settings_opt_get_int("max_history_age") > 0) {
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
217 int maxdays = settings_opt_get_int("max_history_age");
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
218 time(&starttime);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
219 if (maxdays >= starttime/86400L)
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
220 starttime = 0L;
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
221 else
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
222 starttime -= maxdays * 86400L;
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
223 }
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
224
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
225 /* See write_histo_line() for line format... */
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
226 while (!feof(fp)) {
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
227 guint dataoffset = 25;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
228 guint noeol;
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
229
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
230 if (fgets(data, data_size-1, fp) == NULL)
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
231 break;
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
232 ln++;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
233
2182
09508eeb81dc Fix reading of history log files w/o LF character (Sam Whited)
Mikael Berthe <mikael@lilotux.net>
parents: 2073
diff changeset
234 while (!feof(fp)) {
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
235 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
236 if (tail == data) {
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
237 // 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
238 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
239 err = 1;
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
240 break;
990cad5123f3 Try to recover when a history log file is damaged
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
241 }
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
242 noeol = (*(tail-1) != '\n');
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
243 if (!noeol)
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
244 break;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
245 /* TODO: duplicated code... could do better... */
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
246 if (tail == data + data_size-2) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
247 // 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
248 // Let's allocate some more space.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
249 if (!max_num_of_blocks ||
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
250 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
251 guint toffset = tail - data;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
252 // Allocate one more block.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
253 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
254 data = g_renew(char, data, data_size);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
255 // 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
256 tail = data + toffset;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
257 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
258 break;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
259 } else {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
260 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
261 ln--;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
262 break;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
263 }
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
264 }
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
265 }
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
266
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
267 type = data[0];
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
268 info = data[1];
241
8584f919d9b9 [/trunk] Changeset 254 by mikael
mikael
parents: 221
diff changeset
269
395
3e4f2f98c0bf Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents: 394
diff changeset
270 if ((type != 'M' && type != 'S') ||
259
594e73bc9610 Remove support for old logfile format
mikael@frmp8452
parents: 251
diff changeset
271 ((data[11] != 'T') || (data[20] != 'Z') ||
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
272 (data[21] != ' ') ||
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
273 (data[25] != ' ' && data[26] != ' '))) {
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
274 if (!err) {
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
275 scr_LogPrint(LPRINT_LOGNORM,
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
276 "Error in history file format (%s), l.%u", bjid, ln);
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
277 err = 1;
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
278 }
186
888ad9f15346 [/trunk] Changeset 198 by mikael
mikael
parents: 184
diff changeset
279 continue;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
280 }
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
281 // 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
282 if (data[25] != ' ') dataoffset = 26;
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
283 data[21] = data[dataoffset] = 0;
259
594e73bc9610 Remove support for old logfile format
mikael@frmp8452
parents: 251
diff changeset
284 timestamp = from_iso8601(&data[3], 1);
594e73bc9610 Remove support for old logfile format
mikael@frmp8452
parents: 251
diff changeset
285 len = (guint) atoi(&data[22]);
395
3e4f2f98c0bf Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents: 394
diff changeset
286
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
287 // 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
288 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
289 ((type == 'S') && (!strchr("_OFDNAI", info)))) {
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
290 if (!err) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
291 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
292 bjid, ln);
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
293 err = 1;
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
294 }
186
888ad9f15346 [/trunk] Changeset 198 by mikael
mikael
parents: 184
diff changeset
295 continue;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
296 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
297
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
298 while (len--) {
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
299 ln++;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
300 if (fgets(tail, data_size-1 - (tail-data), fp) == NULL)
249
c3e6e78d1ab4 [/trunk] Changeset 262 by mikael
mikael
parents: 248
diff changeset
301 break;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
302
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
303 while (*tail) tail++;
1436
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
304 noeol = (*(tail-1) != '\n');
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
305 if (tail == data + data_size-2 && (len || noeol)) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
306 // 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
307 // Let's allocate some more space.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
308 if (!max_num_of_blocks ||
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
309 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
310 guint toffset = tail - data;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
311 // 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
312 // buffer, we want to read one more time.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
313 if (noeol)
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
314 len++;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
315 // Allocate one more block.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
316 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
317 data = g_renew(char, data, data_size);
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
318 // 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
319 tail = data + toffset;
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
320 } else {
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
321 // 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
322 // this message hasn't been read entirely.
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
323 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
324 }
6fb141142388 Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents: 1414
diff changeset
325 }
248
701651393076 [/trunk] Changeset 261 by mikael
mikael
parents: 241
diff changeset
326 }
251
806fcdd3b5da [/trunk] Changeset 264 by mikael
mikael
parents: 250
diff changeset
327 // 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
328 if ((tail > data+dataoffset+1) && (*(tail-1) == '\n'))
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
329 *(tail-1) = 0;
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
330
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
331 // 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
332 if (starttime) {
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
333 if (timestamp > starttime)
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
334 starttime = 0L; // From now on, load everything
899
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
335 else
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
336 continue;
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
337 }
a833f3d6119a Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents: 795
diff changeset
338
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
339 if (type == 'M') {
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
340 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
341 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
342 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
343 } else {
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 183
diff changeset
344 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
345 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
346 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
347 }
795
b16acadd7d53 Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
348 converted = from_utf8(&data[dataoffset+1]);
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
349 if (converted) {
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
350 xtext = ut_expand_tabs(converted); // Expand tabs
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1106
diff changeset
351 hbuf_add_line(p_buddyhbuf, xtext, timestamp, prefix_flags, width,
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
352 max_num_of_blocks, 0, NULL);
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
353 if (xtext != converted)
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
354 g_free(xtext);
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
355 g_free(converted);
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
356 }
193
5cbdcccfab29 [/trunk] Changeset 205 by mikael
mikael
parents: 186
diff changeset
357 err = 0;
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
358 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
359 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
360 fclose(fp);
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
361 g_free(data);
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
362 }
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 177
diff changeset
363
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
364 // hlog_enable()
2205
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
365 // 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
366 // in mcabber configuration directory is used.
177
a51ce78a0e2a [/trunk] Changeset 189 by mikael
mikael
parents: 169
diff changeset
367 // 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
368 void hlog_enable(guint enable, const char *root_dir, guint loadfiles)
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
369 {
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
370 UseFileLogging = enable;
177
a51ce78a0e2a [/trunk] Changeset 189 by mikael
mikael
parents: 169
diff changeset
371 FileLoadLogs = loadfiles;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
372
177
a51ce78a0e2a [/trunk] Changeset 189 by mikael
mikael
parents: 169
diff changeset
373 if (enable || loadfiles) {
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
374 if (root_dir) {
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
375 char *xp_root_dir;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
376 int l = strlen(root_dir);
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
377 if (l < 1) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
378 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
379 UseFileLogging = FileLoadLogs = FALSE;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
380 return;
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
381 }
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
382 xp_root_dir = expand_filename(root_dir);
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
383 // RootDir must be slash-terminated
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
384 if (root_dir[l-1] == '/') {
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
385 RootDir = xp_root_dir;
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
386 } else {
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
387 RootDir = g_strdup_printf("%s/", xp_root_dir);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
388 g_free(xp_root_dir);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1155
diff changeset
389 }
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
390 } else {
2205
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
391 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
392 const char *hdir = "/histo/";
1177b33d4550 Do not hardcode the default logging location
Mikael Berthe <mikael@lilotux.net>
parents: 2182
diff changeset
393 RootDir = g_strdup_printf("%s%s", cfgdir, hdir);
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
394 }
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
395 // 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
396 if (checkset_perm(RootDir, TRUE) == -1) {
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
397 // The directory does not actually exists
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
398 g_free(RootDir);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
399 RootDir = NULL;
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
400 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access "
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 366
diff changeset
401 "history log directory, logging DISABLED");
362
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
402 UseFileLogging = FileLoadLogs = FALSE;
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
403 }
d8f147d6e872 Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents: 352
diff changeset
404 } else { // Disable history logging
774
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
405 g_free(RootDir);
1106
8e12137fab20 Cosmetics and cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
406 RootDir = NULL;
110
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
407 }
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
408 }
3d41aca3d878 [/trunk] Changeset 124 by mikael
mikael
parents:
diff changeset
409
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
410 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
411 {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
412 return UseFileLogging;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
413 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
414
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
415 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
416 const char *msg)
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
417 {
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
418 guchar info;
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
419 /* 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
420 * 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
421 * 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
422 */
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
423 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
424 info = 'S';
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
425 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
426 info = 'R';
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
427 else
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
428 info = 'I';
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
429 write_histo_line(bjid, timestamp, 'M', info, msg);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
430 }
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
431
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
432 inline void hlog_write_status(const char *bjid, time_t timestamp,
221
73f6ce668ba8 [/trunk] Changeset 233 by mikael
mikael
parents: 197
diff changeset
433 enum imstatus status, const char *status_msg)
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
434 {
1205
2de8f8ba1f34 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 1204
diff changeset
435 // XXX Check status value?
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 899
diff changeset
436 write_histo_line(bjid, timestamp, 'S', toupper(imstatus2char[status]),
221
73f6ce668ba8 [/trunk] Changeset 233 by mikael
mikael
parents: 197
diff changeset
437 status_msg);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
438 }
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents: 111
diff changeset
439
1437
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
440
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
441 // hlog_save_state()
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
442 // 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
443 // (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
444 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
445 {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
446 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
447 const char *bjid;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
448 char *statefile_xp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
449 FILE *fp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
450 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
451
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
452 if (!statefile || !UseFileLogging)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
453 return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
454
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
455 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
456 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
457 if (!fp) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
458 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
459 strerror(errno));
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
460 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
461 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
462
1684
95df4ea512c8 Provide xmpp_is_online()
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1682
diff changeset
463 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
464 // 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
465 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
466 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
467 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
468 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
469 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
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
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
473 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
474 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
475
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
476 // 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
477 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
478 if (!first_unread)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
479 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
480
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
481 do {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
482 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
483 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
484 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
485 if (bjid)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
486 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
487 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
488 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
489 } 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
490
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
491 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
492 if (fp) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
493 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
494 fclose(fp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
495 if (!filelen)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
496 unlink(statefile_xp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
497 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
498 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
499 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
500
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
501 // hlog_load_state()
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
502 // 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
503 // (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
504 // 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
505 // 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
506 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
507 {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
508 char bjid[1024];
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
509 char *statefile_xp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
510 FILE *fp;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
511 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
512
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
513 if (!statefile || !UseFileLogging)
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
514 return;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
515
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
516 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
517 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
518 if (fp) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
519 char *eol;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
520 while (!feof(fp)) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
521 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
522 break;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
523 // 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
524 // 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
525 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
526 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
527 // Safety checks...
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
528 if (!bjid[0])
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
529 continue;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
530 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
531 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
532 "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
533 break;
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
534 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
535 // 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
536 // 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
537 if (!FileLoadLogs) {
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
538 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
539 bjid);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
540 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
541 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
542 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
543 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
544 // 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
545 // 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
546 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
547 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
548 fclose(fp);
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
549 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
550 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
551 }
071c8170b7de Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents: 1436
diff changeset
552
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
553 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */