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