Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/histolog.c @ 2223:965e0282c128
Backed out changeset fa516ef22145
Turns out that in this hash the value pointer is the same as the
key pointer, so there's no need to free both the key and the value.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Fri, 06 Nov 2015 22:31:40 +0100 |
parents | 1177b33d4550 |
children | 736f9323d701 |
rev | line source |
---|---|
110 | 1 /* |
699 | 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 | 5 * |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or (at | |
9 * your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 * USA | |
20 */ | |
21 | |
1598 | 22 #include <ctype.h> |
23 #include <errno.h> | |
24 #include <fcntl.h> | |
110 | 25 #include <stdlib.h> |
1598 | 26 #include <string.h> |
113 | 27 #include <sys/types.h> |
28 #include <sys/stat.h> | |
1598 | 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 | 31 |
32 #include "histolog.h" | |
178 | 33 #include "hbuf.h" |
241 | 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 | 39 #include "xmpp.h" |
110 | 40 |
41 static guint UseFileLogging; | |
177 | 42 static guint FileLoadLogs; |
110 | 43 static char *RootDir; |
44 | |
45 | |
1058 | 46 // user_histo_file(jid) |
110 | 47 // Returns history filename for the given jid |
48 // Note: the caller *must* free the filename after use (if not null). | |
1058 | 49 static char *user_histo_file(const char *bjid) |
110 | 50 { |
51 char *filename; | |
1106 | 52 char *lowerid; |
53 | |
54 if (!(UseFileLogging || FileLoadLogs)) | |
55 return NULL; | |
110 | 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 | 61 lowerid = g_strdup(bjid); |
1106 | 62 if (!lowerid) |
63 return NULL; | |
64 mc_strtolower(lowerid); | |
186 | 65 |
1106 | 66 filename = g_strdup_printf("%s%s", RootDir, lowerid); |
186 | 67 g_free(lowerid); |
110 | 68 return filename; |
69 } | |
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 | 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 | 95 // write_histo_line() |
110 | 96 // Adds a history (multi-)line to the jid's history logfile |
1058 | 97 static void write_histo_line(const char *bjid, |
113 | 98 time_t timestamp, guchar type, guchar info, const char *data) |
110 | 99 { |
100 guint len = 0; | |
113 | 101 FILE *fp; |
110 | 102 time_t ts; |
113 | 103 const char *p; |
178 | 104 char *filename; |
241 | 105 char str_ts[20]; |
273 | 106 int err; |
110 | 107 |
1106 | 108 if (!UseFileLogging) |
109 return; | |
178 | 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 | 115 filename = user_histo_file(bjid); |
110 | 116 |
117 // If timestamp is null, get current date | |
118 if (timestamp) | |
119 ts = timestamp; | |
120 else | |
121 time(&ts); | |
122 | |
123 if (!data) | |
124 data = ""; | |
125 | |
126 // Count number of extra lines | |
127 for (p=data ; *p ; p++) | |
128 if (*p == '\n') len++; | |
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 | 131 * T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len |
110 | 132 * |
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 | 139 */ |
140 | |
113 | 141 fp = fopen(filename, "a"); |
155 | 142 g_free(filename); |
273 | 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 | 146 return; |
147 } | |
178 | 148 |
241 | 149 to_iso8601(str_ts, ts); |
273 | 150 err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); |
113 | 151 fclose(fp); |
273 | 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 | 155 } |
110 | 156 } |
157 | |
178 | 158 // hlog_read_history() |
159 // Reads the jid's history logfile | |
1058 | 160 void hlog_read_history(const char *bjid, GList **p_buddyhbuf, guint width) |
178 | 161 { |
162 char *filename; | |
163 guchar type, info; | |
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 | 167 time_t timestamp; |
168 guint prefix_flags; | |
178 | 169 guint len; |
170 FILE *fp; | |
197 | 171 struct stat bufstat; |
193 | 172 guint err = 0; |
248 | 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 | 176 |
1106 | 177 if (!FileLoadLogs) |
178 return; | |
178 | 179 |
1058 | 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 | 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 | 188 return; |
189 } | |
190 | |
1058 | 191 filename = user_histo_file(bjid); |
178 | 192 |
193 fp = fopen(filename, "r"); | |
194 g_free(filename); | |
1106 | 195 if (!fp) { |
196 g_free(data); | |
197 return; | |
198 } | |
178 | 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 | 201 // (it can take a while...) |
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 | 207 } |
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 | 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 | 215 if (maxdays >= starttime/86400L) |
216 starttime = 0L; | |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
217 else |
1106 | 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 | 221 /* See write_histo_line() for line format... */ |
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 | 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 | 227 break; |
248 | 228 ln++; |
178 | 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 | 262 |
263 type = data[0]; | |
264 info = data[1]; | |
241 | 265 |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
266 if ((type != 'M' && type != 'S') || |
259 | 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 | 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 | 272 "Error in history file format (%s), l.%u", bjid, ln); |
193 | 273 err = 1; |
274 } | |
186 | 275 continue; |
178 | 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 | 280 timestamp = from_iso8601(&data[3], 1); |
281 len = (guint) atoi(&data[22]); | |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
282 |
178 | 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 | 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 | 288 bjid, ln); |
193 | 289 err = 1; |
290 } | |
186 | 291 continue; |
178 | 292 } |
293 | |
294 while (len--) { | |
248 | 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 | 297 break; |
178 | 298 |
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 | 322 } |
251 | 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 | 325 *(tail-1) = 0; |
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 | 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 | 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 | 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 | 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 | 353 err = 0; |
178 | 354 } |
355 } | |
356 fclose(fp); | |
357 g_free(data); | |
358 } | |
359 | |
110 | 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 | 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 | 365 { |
366 UseFileLogging = enable; | |
177 | 367 FileLoadLogs = loadfiles; |
110 | 368 |
177 | 369 if (enable || loadfiles) { |
110 | 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 | 372 int l = strlen(root_dir); |
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 | 376 return; |
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 | 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 | 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 | 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 | 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 | 402 RootDir = NULL; |
110 | 403 } |
404 } | |
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 | 411 inline void hlog_write_message(const char *bjid, time_t timestamp, int sent, |
113 | 412 const char *msg) |
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 | 426 } |
427 | |
1058 | 428 inline void hlog_write_status(const char *bjid, time_t timestamp, |
221 | 429 enum imstatus status, const char *status_msg) |
113 | 430 { |
1205 | 431 // XXX Check status value? |
1058 | 432 write_histo_line(bjid, timestamp, 'S', toupper(imstatus2char[status]), |
221 | 433 status_msg); |
113 | 434 } |
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 | 549 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ |