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