40 // Returns history filename for the given jid |
41 // Returns history filename for the given jid |
41 // Note: the caller *must* free the filename after use (if not null). |
42 // Note: the caller *must* free the filename after use (if not null). |
42 static char *user_histo_file(const char *jid) |
43 static char *user_histo_file(const char *jid) |
43 { |
44 { |
44 char *filename; |
45 char *filename; |
45 if (!UseFileLogging) |
46 if (!UseFileLogging && !FileLoadLogs) return NULL; |
46 return NULL; |
|
47 |
47 |
48 filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
48 filename = g_new(char, strlen(RootDir) + strlen(jid) + 1); |
49 strcpy(filename, RootDir); |
49 strcpy(filename, RootDir); |
50 strcat(filename, jid); |
50 strcat(filename, jid); |
51 return filename; |
51 return filename; |
52 } |
52 } |
53 |
53 |
54 // write() |
54 // write_histo_line() |
55 // Adds a history (multi-)line to the jid's history logfile |
55 // Adds a history (multi-)line to the jid's history logfile |
56 static void write_histo_line(const char *jid, |
56 static void write_histo_line(const char *jid, |
57 time_t timestamp, guchar type, guchar info, const char *data) |
57 time_t timestamp, guchar type, guchar info, const char *data) |
58 { |
58 { |
59 guint len = 0; |
59 guint len = 0; |
60 FILE *fp; |
60 FILE *fp; |
61 time_t ts; |
61 time_t ts; |
62 const char *p; |
62 const char *p; |
63 char *filename = user_histo_file(jid); |
63 char *filename; |
64 |
64 |
65 if (!filename) |
65 if (!UseFileLogging) return; |
66 return; |
66 |
|
67 filename = user_histo_file(jid); |
67 |
68 |
68 // If timestamp is null, get current date |
69 // If timestamp is null, get current date |
69 if (timestamp) |
70 if (timestamp) |
70 ts = timestamp; |
71 ts = timestamp; |
71 else |
72 else |
87 * We don't check them, we'll trust the caller. |
88 * We don't check them, we'll trust the caller. |
88 */ |
89 */ |
89 |
90 |
90 fp = fopen(filename, "a"); |
91 fp = fopen(filename, "a"); |
91 g_free(filename); |
92 g_free(filename); |
92 if (!fp) |
93 if (!fp) return; |
93 return; |
94 |
94 fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data); |
95 fprintf(fp, "%c%c %10u %03d %s\n", type, info, (unsigned int)ts, len, data); |
95 fclose(fp); |
96 fclose(fp); |
|
97 } |
|
98 |
|
99 // hlog_read_history() |
|
100 // Reads the jid's history logfile |
|
101 void hlog_read_history(const char *jid, GList **p_buddyhbuf, guint width) |
|
102 { |
|
103 char *filename; |
|
104 time_t timestamp; |
|
105 guchar type, info; |
|
106 char *data, *tail; |
|
107 guint len; |
|
108 FILE *fp; |
|
109 char prefix[32]; |
|
110 |
|
111 if (!FileLoadLogs) return; |
|
112 |
|
113 data = g_new(char, HBB_BLOCKSIZE+32); |
|
114 if (!data) { |
|
115 scr_LogPrint("Not enough memory to read history file"); |
|
116 return; |
|
117 } |
|
118 |
|
119 filename = user_histo_file(jid); |
|
120 |
|
121 fp = fopen(filename, "r"); |
|
122 g_free(filename); |
|
123 if (!fp) { g_free(data); return; } |
|
124 |
|
125 /* See write_histo_line() for line format... */ |
|
126 while (!feof(fp)) { |
|
127 if (fgets(data, HBB_BLOCKSIZE+24, fp) == NULL) break; |
|
128 |
|
129 for (tail = data; *tail; tail++) ; |
|
130 |
|
131 type = data[0]; |
|
132 info = data[1]; |
|
133 if ((type != 'M' && type != 'S') || |
|
134 (data[13] != ' ') || (data[17] != ' ')) { |
|
135 scr_LogPrint("Error in history file format"); |
|
136 break; |
|
137 } |
|
138 data[13] = data[17] = 0; |
|
139 timestamp = (unsigned long) atol(&data[3]); |
|
140 len = (unsigned long) atol(&data[14]); |
|
141 |
|
142 // Some checks |
|
143 if (((type == 'M') && (info != 'S' && info != 'R')) || |
|
144 ((type == 'I') && (!strchr("oaifdcn", info)))) { |
|
145 scr_LogPrint("Error in history file format"); |
|
146 break; |
|
147 } |
|
148 |
|
149 while (len--) { |
|
150 if (fgets(tail, HBB_BLOCKSIZE+24 - (tail-data), fp) == NULL) break; |
|
151 |
|
152 while (*tail) tail++; |
|
153 } |
|
154 if ((tail > data+18) && (*(tail-1) == '\n')) |
|
155 *(tail-1) = 0; |
|
156 |
|
157 if (type == 'M') { |
|
158 strftime(prefix, 12, "[%H:%M] ", localtime(×tamp)); |
|
159 if (info == 'S') |
|
160 strcat(prefix, "--> "); |
|
161 else |
|
162 strcat(prefix, "<== "); |
|
163 hbuf_add_line(p_buddyhbuf, &data[18], prefix, width); |
|
164 } |
|
165 } |
|
166 fclose(fp); |
|
167 g_free(data); |
96 } |
168 } |
97 |
169 |
98 // hlog_enable() |
170 // hlog_enable() |
99 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
171 // Enable logging to files. If root_dir is NULL, then $HOME/.mcabber is used. |
100 // If loadfiles is TRUE, we will try to load buddies history logs from file. |
172 // If loadfiles is TRUE, we will try to load buddies history logs from file. |