Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/histolog.c @ 1869:fe385f2cfb93
Partial update for XEP-0184 v1.1
Added 'id' attribute to <received/> element
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 04 Apr 2010 10:31:38 +0200 |
parents | e9773bd9dd2e |
children | d41623dd88ee |
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++) ; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
232 noeol = (*(tail-1) != '\n'); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
233 if (!noeol) |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
234 break; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
235 /* TODO: duplicated code... could do better... */ |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
236 if (tail == data + data_size-2) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
237 // 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
|
238 // Let's allocate some more space. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
239 if (!max_num_of_blocks || |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
240 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
241 guint toffset = tail - data; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
242 // Allocate one more block. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
243 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
244 data = g_renew(char, data, data_size); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
245 // 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
|
246 tail = data + toffset; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
247 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
|
248 break; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
249 } else { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
250 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
|
251 ln--; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
252 break; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
253 } |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
254 } |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
255 } |
178 | 256 |
257 type = data[0]; | |
258 info = data[1]; | |
241 | 259 |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
260 if ((type != 'M' && type != 'S') || |
259 | 261 ((data[11] != 'T') || (data[20] != 'Z') || |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
262 (data[21] != ' ') || |
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
263 (data[25] != ' ' && data[26] != ' '))) { |
193 | 264 if (!err) { |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
265 scr_LogPrint(LPRINT_LOGNORM, |
1058 | 266 "Error in history file format (%s), l.%u", bjid, ln); |
193 | 267 err = 1; |
268 } | |
186 | 269 continue; |
178 | 270 } |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
271 // 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
|
272 if (data[25] != ' ') dataoffset = 26; |
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
273 data[21] = data[dataoffset] = 0; |
259 | 274 timestamp = from_iso8601(&data[3], 1); |
275 len = (guint) atoi(&data[22]); | |
395
3e4f2f98c0bf
Whitespace cleanup (histolog.c)
Mikael Berthe <mikael@lilotux.net>
parents:
394
diff
changeset
|
276 |
178 | 277 // 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
|
278 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
|
279 ((type == 'S') && (!strchr("_OFDNAI", info)))) { |
193 | 280 if (!err) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
281 scr_LogPrint(LPRINT_LOGNORM, "Error in history file format (%s), l.%u", |
1058 | 282 bjid, ln); |
193 | 283 err = 1; |
284 } | |
186 | 285 continue; |
178 | 286 } |
287 | |
288 while (len--) { | |
248 | 289 ln++; |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
290 if (fgets(tail, data_size-1 - (tail-data), fp) == NULL) |
249 | 291 break; |
178 | 292 |
293 while (*tail) tail++; | |
1436
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
294 noeol = (*(tail-1) != '\n'); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
295 if (tail == data + data_size-2 && (len || noeol)) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
296 // 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
|
297 // Let's allocate some more space. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
298 if (!max_num_of_blocks || |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
299 data_size/HBB_BLOCKSIZE < 5U*max_num_of_blocks) { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
300 guint toffset = tail - data; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
301 // 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
|
302 // buffer, we want to read one more time. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
303 if (noeol) |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
304 len++; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
305 // Allocate one more block. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
306 data_size = HBB_BLOCKSIZE * (1 + data_size/HBB_BLOCKSIZE); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
307 data = g_renew(char, data, data_size); |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
308 // 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
|
309 tail = data + toffset; |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
310 } else { |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
311 // 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
|
312 // this message hasn't been read entirely. |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
313 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
|
314 } |
6fb141142388
Improve reading of history files
Mikael Berthe <mikael@lilotux.net>
parents:
1414
diff
changeset
|
315 } |
248 | 316 } |
251 | 317 // 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
|
318 if ((tail > data+dataoffset+1) && (*(tail-1) == '\n')) |
178 | 319 *(tail-1) = 0; |
320 | |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
321 // 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
|
322 if (starttime) { |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
323 if (timestamp > starttime) |
1106 | 324 starttime = 0L; // From now on, load everything |
899
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
325 else |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
326 continue; |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
327 } |
a833f3d6119a
Add option 'max_history_age'
Mikael Berthe <mikael@lilotux.net>
parents:
795
diff
changeset
|
328 |
178 | 329 if (type == 'M') { |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
330 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
|
331 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
|
332 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
|
333 } else { |
184 | 334 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
|
335 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
|
336 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
|
337 } |
795
b16acadd7d53
Improve support for long messages
Mikael Berthe <mikael@lilotux.net>
parents:
774
diff
changeset
|
338 converted = from_utf8(&data[dataoffset+1]); |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
339 if (converted) { |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
340 xtext = ut_expand_tabs(converted); // Expand tabs |
1141
5be2408a6534
Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents:
1106
diff
changeset
|
341 hbuf_add_line(p_buddyhbuf, xtext, timestamp, prefix_flags, width, |
1602 | 342 max_num_of_blocks, 0, NULL); |
772
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
343 if (xtext != converted) |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
344 g_free(xtext); |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
345 g_free(converted); |
464be13343a9
Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents:
729
diff
changeset
|
346 } |
193 | 347 err = 0; |
178 | 348 } |
349 } | |
350 fclose(fp); | |
351 g_free(data); | |
352 } | |
353 | |
110 | 354 // hlog_enable() |
355 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. | |
177 | 356 // 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
|
357 void hlog_enable(guint enable, const char *root_dir, guint loadfiles) |
110 | 358 { |
359 UseFileLogging = enable; | |
177 | 360 FileLoadLogs = loadfiles; |
110 | 361 |
177 | 362 if (enable || loadfiles) { |
110 | 363 if (root_dir) { |
1167
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
364 char *xp_root_dir; |
110 | 365 int l = strlen(root_dir); |
366 if (l < 1) { | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
367 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
|
368 UseFileLogging = FileLoadLogs = FALSE; |
110 | 369 return; |
370 } | |
1167
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
371 xp_root_dir = expand_filename(root_dir); |
110 | 372 // RootDir must be slash-terminated |
1167
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
373 if (root_dir[l-1] == '/') { |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
374 RootDir = xp_root_dir; |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
375 } else { |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
376 RootDir = g_strdup_printf("%s/", xp_root_dir); |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
377 g_free(xp_root_dir); |
9726c78a91f3
Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents:
1155
diff
changeset
|
378 } |
110 | 379 } else { |
380 char *home = getenv("HOME"); | |
1106 | 381 const char *dir = "/.mcabber/histo/"; |
382 RootDir = g_strdup_printf("%s%s", home, dir); | |
110 | 383 } |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
384 // 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
|
385 if (checkset_perm(RootDir, TRUE) == -1) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
386 // The directory does not actually exists |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
387 g_free(RootDir); |
1106 | 388 RootDir = NULL; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
389 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Cannot access " |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
366
diff
changeset
|
390 "history log directory, logging DISABLED"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
391 UseFileLogging = FileLoadLogs = FALSE; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
392 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
352
diff
changeset
|
393 } else { // Disable history logging |
774
46304b773a44
Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents:
772
diff
changeset
|
394 g_free(RootDir); |
1106 | 395 RootDir = NULL; |
110 | 396 } |
397 } | |
398 | |
1437
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
399 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
|
400 { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
401 return UseFileLogging; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
402 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
403 |
1058 | 404 inline void hlog_write_message(const char *bjid, time_t timestamp, int sent, |
113 | 405 const char *msg) |
406 { | |
1342
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
407 guchar info; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
408 /* 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
|
409 * 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
|
410 * 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
|
411 */ |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
412 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
|
413 info = 'S'; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
414 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
|
415 info = 'R'; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
416 else |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
417 info = 'I'; |
06441b6cc23a
History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents:
1311
diff
changeset
|
418 write_histo_line(bjid, timestamp, 'M', info, msg); |
113 | 419 } |
420 | |
1058 | 421 inline void hlog_write_status(const char *bjid, time_t timestamp, |
221 | 422 enum imstatus status, const char *status_msg) |
113 | 423 { |
1205 | 424 // XXX Check status value? |
1058 | 425 write_histo_line(bjid, timestamp, 'S', toupper(imstatus2char[status]), |
221 | 426 status_msg); |
113 | 427 } |
428 | |
1437
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
429 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
430 // hlog_save_state() |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
431 // 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
|
432 // (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
|
433 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
|
434 { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
435 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
|
436 const char *bjid; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
437 char *statefile_xp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
438 FILE *fp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
439 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
|
440 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
441 if (!statefile || !UseFileLogging) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
442 return; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
443 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
444 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
|
445 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
|
446 if (!fp) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
447 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
|
448 strerror(errno)); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
449 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
|
450 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
451 |
1684
95df4ea512c8
Provide xmpp_is_online()
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1682
diff
changeset
|
452 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
|
453 // 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
|
454 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
|
455 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
|
456 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
|
457 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
|
458 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
|
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 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
462 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
|
463 goto hlog_save_state_return; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
464 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
465 // 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
|
466 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
|
467 if (!first_unread) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
468 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
|
469 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
470 do { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
471 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
|
472 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
|
473 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
|
474 if (bjid) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
475 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
|
476 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
477 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
|
478 } 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
|
479 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
480 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
|
481 if (fp) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
482 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
|
483 fclose(fp); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
484 if (!filelen) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
485 unlink(statefile_xp); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
486 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
487 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
|
488 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
489 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
490 // hlog_load_state() |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
491 // 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
|
492 // (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
|
493 // 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
|
494 // 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
|
495 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
|
496 { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
497 char bjid[1024]; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
498 char *statefile_xp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
499 FILE *fp; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
500 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
|
501 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
502 if (!statefile || !UseFileLogging) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
503 return; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
504 |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
505 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
|
506 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
|
507 if (fp) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
508 char *eol; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
509 while (!feof(fp)) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
510 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
|
511 break; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
512 // 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
|
513 // 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
|
514 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
|
515 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
|
516 // Safety checks... |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
517 if (!bjid[0]) |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
518 continue; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
519 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
|
520 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
|
521 "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
|
522 break; |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
523 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
524 // 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
|
525 // 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
|
526 if (!FileLoadLogs) { |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
527 scr_LogPrint(LPRINT_LOGNORM, "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
|
528 bjid); |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
529 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
|
530 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
|
531 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
|
532 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
533 // 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
|
534 // 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
|
535 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
|
536 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
537 fclose(fp); |
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 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
|
540 } |
071c8170b7de
Add option 'statefile' to keep track of unread messages across restarts
Mikael Berthe <mikael@lilotux.net>
parents:
1436
diff
changeset
|
541 |
1811 | 542 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ |