Mercurial > hg
annotate mcabber/src/histolog.c @ 1577:e1a642472f4e
Use otr context where possible, saves one mc_strtolower() per received message
author | franky |
---|---|
date | Sun, 12 Apr 2009 20:20:56 +0200 |
parents | ec55cdf44335 |
children | a087125d8fc8 |
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 * |
1414 | 4 * Copyright (C) 2005-2008 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 | |
22 #include <string.h> | |
23 #include <stdlib.h> | |
24 #include <time.h> | |
113 | 25 #include <ctype.h> |
26 #include <sys/types.h> | |
27 #include <sys/stat.h> | |
28 #include <fcntl.h> | |
1437
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
29 #include <unistd.h> |
110 | 30 |
31 #include "histolog.h" | |
178 | 32 #include "hbuf.h" |
113 | 33 #include "jabglue.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" |
110 | 39 |
40 static guint UseFileLogging; | |
177 | 41 static guint FileLoadLogs; |
110 | 42 static char *RootDir; |
43 | |
44 | |
1058 | 45 // user_histo_file(jid) |
110 | 46 // Returns history filename for the given jid |
47 // Note: the caller *must* free the filename after use (if not null). | |
1058 | 48 static char *user_histo_file(const char *bjid) |
110 | 49 { |
50 char *filename; | |
1106 | 51 char *lowerid; |
52 | |
53 if (!(UseFileLogging || FileLoadLogs)) | |
54 return NULL; | |
110 | 55 |
1058 | 56 lowerid = g_strdup(bjid); |
1106 | 57 if (!lowerid) |
58 return NULL; | |
59 mc_strtolower(lowerid); | |
186 | 60 |
1106 | 61 filename = g_strdup_printf("%s%s", RootDir, lowerid); |
186 | 62 g_free(lowerid); |
110 | 63 return filename; |
64 } | |
65 | |
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
|
66 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
|
67 { |
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
|
68 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
|
69 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
|
70 char *log_jid = NULL; |
1303
b6fdbfa6b219
Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents:
1290
diff
changeset
|
71 |
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
|
72 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
|
73 while (path) { |
1303
b6fdbfa6b219
Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents:
1290
diff
changeset
|
74 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
|
75 break; |
1303
b6fdbfa6b219
Minor whitespace change
Mikael Berthe <mikael@lilotux.net>
parents:
1290
diff
changeset
|
76 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
|
77 g_free(log_jid); |
1560
ec55cdf44335
Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents:
1437
diff
changeset
|
78 log_jid = g_new0(char, bufstat.st_size+1); |
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
|
79 readlink(path, log_jid, bufstat.st_size); |
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 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
|
81 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
|
82 } else |
ec55cdf44335
Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents:
1437
diff
changeset
|
83 break; |
ec55cdf44335
Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents:
1437
diff
changeset
|
84 } |
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 |
1560
ec55cdf44335
Fix a memory leak in hlog_get_log_jid()
Mikael Berthe <mikael@lilotux.net>
parents:
1437
diff
changeset
|
86 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
|
87 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
|
88 } |
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 |
178 | 90 // write_histo_line() |
110 | 91 // Adds a history (multi-)line to the jid's history logfile |
1058 | 92 static void write_histo_line(const char *bjid, |
113 | 93 time_t timestamp, guchar type, guchar info, const char *data) |
110 | 94 { |
95 guint len = 0; | |
113 | 96 FILE *fp; |
110 | 97 time_t ts; |
113 | 98 const char *p; |
178 | 99 char *filename; |
241 | 100 char str_ts[20]; |
273 | 101 int err; |
110 | 102 |
1106 | 103 if (!UseFileLogging) |
104 return; | |
178 | 105 |
1144
b8f5fe2d666a
Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents:
1141
diff
changeset
|
106 // 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
|
107 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
|
108 return; |
b8f5fe2d666a
Add option "logging_ignore_status"
Mikael Berthe <mikael@lilotux.net>
parents:
1141
diff
changeset
|
109 |
1058 | 110 filename = user_histo_file(bjid); |
110 | 111 |
112 // If timestamp is null, get current date | |
113 if (timestamp) | |
114 ts = timestamp; | |
115 else | |
116 time(&ts); | |
117 | |
118 if (!data) | |
119 data = ""; | |
120 | |
121 // Count number of extra lines | |
122 for (p=data ; *p ; p++) | |
123 if (*p == '\n') len++; | |
124 | |
1342
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
125 /* Line format: "TI yyyymmddThh:mm:ssZ LLL [data]" |
241 | 126 * T=Type, I=Info, yyyymmddThh:mm:ssZ=date, LLL=0-padded-len |
110 | 127 * |
128 * Types: | |
1342
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
129 * - M message Info: S (send) R (receive) I (info) |
1311
0dda8238af21
Implement "/roster display"
Mikael Berthe <mikael@lilotux.net>
parents:
1303
diff
changeset
|
130 * - 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
|
131 * 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
|
132 * (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
|
133 * locally by mcabber.) |
110 | 134 */ |
135 | |
113 | 136 fp = fopen(filename, "a"); |
155 | 137 g_free(filename); |
273 | 138 if (!fp) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
139 scr_LogPrint(LPRINT_LOGNORM, "Unable to write history " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
140 "(cannot open logfile)"); |
273 | 141 return; |
142 } | |
178 | 143 |
241 | 144 to_iso8601(str_ts, ts); |
273 | 145 err = fprintf(fp, "%c%c %-18.18s %03d %s\n", type, info, str_ts, len, data); |
113 | 146 fclose(fp); |
273 | 147 if (err < 0) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
148 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
|
149 strerror(errno)); |
273 | 150 } |
110 | 151 } |
152 | |
178 | 153 // hlog_read_history() |
154 // Reads the jid's history logfile | |
1058 | 155 void hlog_read_history(const char *bjid, GList **p_buddyhbuf, guint width) |
178 | 156 { |
157 char *filename; | |
158 guchar type, info; | |
159 char *data, *tail; | |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
160 guint data_size; |
727
1c3620668857
Expand tabs when reading history files
Mikael Berthe <mikael@lilotux.net>
parents:
699
diff
changeset
|
161 char *xtext; |
184 | 162 time_t timestamp; |
163 guint prefix_flags; | |
178 | 164 guint len; |
165 FILE *fp; | |
197 | 166 struct stat bufstat; |
193 | 167 guint err = 0; |
248 | 168 guint ln = 0; // line number |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
169 time_t starttime; |
1141
5be2408a6534
Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents:
1106
diff
changeset
|
170 int max_num_of_blocks; |
178 | 171 |
1106 | 172 if (!FileLoadLogs) |
173 return; | |
178 | 174 |
1058 | 175 if ((roster_gettype(bjid) & ROSTER_TYPE_ROOM) && |
635
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
176 (settings_opt_get_int("load_muc_logs") != 1)) |
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
177 return; |
d4119cb85aeb
New "load_muc_logs" option
Mikael Berthe <mikael@lilotux.net>
parents:
580
diff
changeset
|
178 |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
179 data_size = HBB_BLOCKSIZE+32; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
180 data = g_new(char, data_size); |
178 | 181 if (!data) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
182 scr_LogPrint(LPRINT_LOGNORM, "Not enough memory to read history file"); |
178 | 183 return; |
184 } | |
185 | |
1058 | 186 filename = user_histo_file(bjid); |
178 | 187 |
188 fp = fopen(filename, "r"); | |
189 g_free(filename); | |
1106 | 190 if (!fp) { |
191 g_free(data); | |
192 return; | |
193 } | |
178 | 194 |
394
4617d0f029ea
Update "large file" size value to 3MB
Mikael Berthe <mikael@lilotux.net>
parents:
378
diff
changeset
|
195 // If file is large (> 3MB here), display a message to inform the user |
197 | 196 // (it can take a while...) |
197 if (!fstat(fileno(fp), &bufstat)) { | |
1274
c33b5b6fbf08
Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
198 if (bufstat.st_size > 3145728) { |
c33b5b6fbf08
Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
199 scr_LogPrint(LPRINT_NORMAL, "Reading <%s> history file...", bjid); |
c33b5b6fbf08
Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
200 scr_DoUpdate(); |
c33b5b6fbf08
Fix screen refresh issue (spotted by franky)
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
201 } |
197 | 202 } |
203 | |
1141
5be2408a6534
Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents:
1106
diff
changeset
|
204 max_num_of_blocks = get_max_history_blocks(); |
5be2408a6534
Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents:
1106
diff
changeset
|
205 |
1106 | 206 starttime = 0L; |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
207 if (settings_opt_get_int("max_history_age") > 0) { |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
208 int maxdays = settings_opt_get_int("max_history_age"); |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
209 time(&starttime); |
1106 | 210 if (maxdays >= starttime/86400L) |
211 starttime = 0L; | |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
212 else |
1106 | 213 starttime -= maxdays * 86400L; |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
214 } |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
215 |
178 | 216 /* See write_histo_line() for line format... */ |
217 while (!feof(fp)) { | |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
218 guint dataoffset = 25; |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
219 guint noeol; |
1106 | 220 |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
221 if (fgets(data, data_size-1, fp) == NULL) |
1106 | 222 break; |
248 | 223 ln++; |
178 | 224 |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
225 while (1) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
226 for (tail = data; *tail; tail++) ; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
227 noeol = (*(tail-1) != '\n'); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
228 if (!noeol) |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
229 break; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
230 /* TODO: duplicated code... could do better... */ |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
231 if (tail == data + data_size-2) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
232 // 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
|
233 // Let's allocate some more space. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
234 if (!max_num_of_blocks || |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
235 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
236 guint toffset = tail - data; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
237 // Allocate one more block. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
238 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
239 data = g_renew(char, data, data_size); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
240 // 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
|
241 tail = data + toffset; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
242 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
|
243 break; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
244 } else { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
245 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
|
246 ln--; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
247 break; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
248 } |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
249 } |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
250 } |
178 | 251 |
252 type = data[0]; | |
253 info = data[1]; | |
241 | 254 |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
255 if ((type != 'M' && type != 'S') || |
259 | 256 ((data[11] != 'T') || (data[20] != 'Z') || |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
257 (data[21] != ' ') || |
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
258 (data[25] != ' ' && data[26] != ' '))) { |
193 | 259 if (!err) { |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
260 scr_LogPrint(LPRINT_LOGNORM, |
1058 | 261 "Error in history file format (%s), l.%u", bjid, ln); |
193 | 262 err = 1; |
263 } | |
186 | 264 continue; |
178 | 265 } |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
266 // 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
|
267 if (data[25] != ' ') dataoffset = 26; |
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
268 data[21] = data[dataoffset] = 0; |
259 | 269 timestamp = from_iso8601(&data[3], 1); |
270 len = (guint) atoi(&data[22]); | |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
271 |
178 | 272 // 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
|
273 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
|
274 ((type == 'S') && (!strchr("_OFDNAI", info)))) { |
193 | 275 if (!err) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
276 scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u", |
1058 | 277 bjid, ln); |
193 | 278 err = 1; |
279 } | |
186 | 280 continue; |
178 | 281 } |
282 | |
283 while (len--) { | |
248 | 284 ln++; |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
285 if (fgets(tail, data_size-1 - (tail-data), fp) == NULL) |
249 | 286 break; |
178 | 287 |
288 while (*tail) tail++; | |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
289 noeol = (*(tail-1) != '\n'); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
290 if (tail == data + data_size-2 && (len || noeol)) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
291 // 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
|
292 // Let's allocate some more space. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
293 if (!max_num_of_blocks || |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
294 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
295 guint toffset = tail - data; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
296 // 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
|
297 // buffer, we want to read one more time. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
298 if (noeol) |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
299 len++; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
300 // Allocate one more block. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
301 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
302 data = g_renew(char, data, data_size); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
303 // 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
|
304 tail = data + toffset; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
305 } else { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
306 // 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
|
307 // this message hasn't been read entirely. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
308 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
|
309 } |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
310 } |
248 | 311 } |
251 | 312 // 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
|
313 if ((tail > data+dataoffset+1) && (*(tail-1) == '\n')) |
178 | 314 *(tail-1) = 0; |
315 | |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
316 // 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
|
317 if (starttime) { |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
318 if (timestamp > starttime) |
1106 | 319 starttime = 0L; // From now on, load everything |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
320 else |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
321 continue; |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
322 } |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
323 |
178 | 324 if (type == 'M') { |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
325 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
|
326 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
|
327 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
|
328 } else { |
184 | 329 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
|
330 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
|
331 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
|
332 } |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
333 converted = from_utf8(&data[dataoffset+1]); |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
334 if (converted) { |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
335 xtext = ut_expand_tabs(converted); // Expand tabs |
1141
5be2408a6534
Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents:
1106
diff
changeset
|
336 hbuf_add_line(p_buddyhbuf, xtext, timestamp, prefix_flags, width, |
1290
e42f48103609
Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents:
1274
diff
changeset
|
337 max_num_of_blocks, 0); |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
338 if (xtext != converted) |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
339 g_free(xtext); |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
340 g_free(converted); |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
341 } |
193 | 342 err = 0; |
178 | 343 } |
344 } | |
345 fclose(fp); | |
346 g_free(data); | |
347 } | |
348 | |
110 | 349 // hlog_enable() |
350 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. | |
177 | 351 // 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
|
352 void hlog_enable(guint enable, const char *root_dir, guint loadfiles) |
110 | 353 { |
354 UseFileLogging = enable; | |
177 | 355 FileLoadLogs = loadfiles; |
110 | 356 |
177 | 357 if (enable || loadfiles) { |
110 | 358 if (root_dir) { |
1167
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
359 char *xp_root_dir; |
110 | 360 int l = strlen(root_dir); |
361 if (l < 1) { | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
362 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
|
363 UseFileLogging = FileLoadLogs = FALSE; |
110 | 364 return; |
365 } | |
1167
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
366 xp_root_dir = expand_filename(root_dir); |
110 | 367 // RootDir must be slash-terminated |
1167
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
368 if (root_dir[l-1] == '/') { |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
369 RootDir = xp_root_dir; |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
370 } else { |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
371 RootDir = g_strdup_printf("%s/", xp_root_dir); |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
372 g_free(xp_root_dir); |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
373 } |
110 | 374 } else { |
375 char *home = getenv("HOME"); | |
1106 | 376 const char *dir = "/.mcabber/histo/"; |
377 RootDir = g_strdup_printf("%s%s", home, dir); | |
110 | 378 } |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
379 // 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
|
380 if (checkset_perm(RootDir, TRUE) == -1) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
381 // The directory does not actually exists |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
382 g_free(RootDir); |
1106 | 383 RootDir = NULL; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
384 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
385 "history log directory, logging DISABLED"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
386 UseFileLogging = FileLoadLogs = FALSE; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
387 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
388 } else { // Disable history logging |
774
46304b773a44
Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents:
772
diff
changeset
|
389 g_free(RootDir); |
1106 | 390 RootDir = NULL; |
110 | 391 } |
392 } | |
393 | |
1437
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
394 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
|
395 { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
396 return UseFileLogging; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
397 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
398 |
1058 | 399 inline void hlog_write_message(const char *bjid, time_t timestamp, int sent, |
113 | 400 const char *msg) |
401 { | |
1342
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
402 guchar info; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
403 /* 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
|
404 * 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
|
405 * 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
|
406 */ |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
407 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
|
408 info = 'S'; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
409 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
|
410 info = 'R'; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
411 else |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
412 info = 'I'; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
413 write_histo_line(bjid, timestamp, 'M', info, msg); |
113 | 414 } |
415 | |
1058 | 416 inline void hlog_write_status(const char *bjid, time_t timestamp, |
221 | 417 enum imstatus status, const char *status_msg) |
113 | 418 { |
1205 | 419 // XXX Check status value? |
1058 | 420 write_histo_line(bjid, timestamp, 'S', toupper(imstatus2char[status]), |
221 | 421 status_msg); |
113 | 422 } |
423 | |
1437
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
424 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
425 // hlog_save_state() |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
426 // 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
|
427 // (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
|
428 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
|
429 { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
430 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
|
431 const char *bjid; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
432 char *statefile_xp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
433 FILE *fp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
434 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
|
435 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
436 if (!statefile || !UseFileLogging) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
437 return; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
438 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
439 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
|
440 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
|
441 if (!fp) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
442 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
|
443 strerror(errno)); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
444 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
|
445 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
446 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
447 if (!jb_getonline()) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
448 // 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
|
449 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
|
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
456 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
457 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
|
458 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
|
459 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
460 // 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
|
461 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
|
462 if (!first_unread) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
463 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
|
464 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
465 do { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
466 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
|
467 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
|
468 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
|
469 if (bjid) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
470 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
|
471 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
472 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
|
473 } 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
|
474 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
475 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 if (fp) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
477 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
|
478 fclose(fp); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
479 if (!filelen) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
480 unlink(statefile_xp); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
481 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
482 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
|
483 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
484 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
485 // hlog_load_state() |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
486 // 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
|
487 // (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
|
488 // 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
|
489 // 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
|
490 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
|
491 { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
492 char bjid[1024]; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
493 char *statefile_xp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
494 FILE *fp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
495 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
|
496 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
497 if (!statefile || !UseFileLogging) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
498 return; |
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 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
|
501 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
|
502 if (fp) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
503 char *eol; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
504 while (!feof(fp)) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
505 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
|
506 break; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
507 // 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
|
508 // 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
|
509 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
|
510 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
|
511 // Safety checks... |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
512 if (!bjid[0]) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
513 continue; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
514 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
|
515 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
|
516 "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
|
517 break; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
518 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
519 // 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
|
520 // 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
|
521 if (!FileLoadLogs) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
522 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
|
523 bjid); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
524 scr_setmsgflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
525 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
526 // 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
|
527 // 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
|
528 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
|
529 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
530 fclose(fp); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
531 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
532 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
|
533 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
534 |
580 | 535 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |