-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMPPChatLog.m
325 lines (305 loc) · 12.5 KB
/
XMPPChatLog.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// XMPPChatLog.m
// Jabber
//
// Created by David Chisnall on 25/11/2004.
// Copyright 2004 __MyCompanyName__. All rights reserved.
//
#import "XMPPChatLog.h"
#import "XMPPError.h"
#import <EtoileFoundation/EtoileCompatibility.h>
NSString * logBasePath;
NSMutableDictionary * chatLogs;
static NSDictionary * ERROR_STYLE;
@implementation XMPPChatLog
+ (void)initialize
{
//TODO: Set this using portable filesystem functions
NSError *error;
NSString * processName = [[[NSProcessInfo processInfo] arguments] objectAtIndex:0];
NSRange lastSlash = [processName rangeOfString:@"/" options:NSBackwardsSearch];
if(lastSlash.location != NSNotFound)
{
processName = [processName substringFromIndex:lastSlash.location + 1];
}
//Set the base path to be ~/Library/Logs/AppName - This allows multiple apps to use the same library but store their logs in different places
logBasePath = [NSString stringWithFormat:@"%@/logs/%@",
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0],
processName];
if(![[NSFileManager defaultManager] fileExistsAtPath:logBasePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:logBasePath withIntermediateDirectories:YES attributes:nil error:&error];
}
logBasePath = [logBasePath stringByAppendingString:@"/"];
chatLogs = [[NSMutableDictionary alloc] init];
ERROR_STYLE = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor colorWithCalibratedRed:1.0f
green:0.0f
blue:0.0f
alpha:1.0f],
NSForegroundColorAttributeName,
nil];
}
+ (void) setLogBasePath:(NSString*)aPath
{
if([aPath characterAtIndex:[aPath length]-1] == '/')
{
logBasePath = aPath;
}
else
{
logBasePath = [aPath stringByAppendingString:@"/"];
}
}
+ (id) chatLogWithPerson:(XMPPPerson*)aPerson
{
return [[XMPPChatLog alloc] initWithPerson:aPerson];
}
- (void) initLog
{
NSError *error;
NSString * logFolder = [[NSString alloc] initWithFormat:@"%@%@/%@",
logBasePath,
[remoteEntity group],
[remoteEntity name]];
//If the log folder doesn't exist, create it
if(![[NSFileManager defaultManager] fileExistsAtPath:logFolder])
{
NSString * groupLogPath = [NSString stringWithFormat:@"%@%@",
logBasePath,
[remoteEntity group]];
//We can't create the log folder if the folder it's supposed to be in doesn't exist.
if(![[NSFileManager defaultManager] fileExistsAtPath:groupLogPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:groupLogPath withIntermediateDirectories:YES attributes:nil error:&error];
}
[[NSFileManager defaultManager] createDirectoryAtPath:logFolder withIntermediateDirectories:YES attributes:nil error:&error];
}
if(isXML)
{
logFileName = [[NSString alloc] initWithFormat:@"%@/%@.xml",
logFolder,
[today descriptionWithCalendarFormat:@"%y-%m-%d (%a)"]];
//TODO: Initialise log
}
else
{
logFileName = [[NSString alloc] initWithFormat:@"%@/%@.rtf",
logFolder,
[today descriptionWithCalendarFormat:@"%y-%m-%d (%a)"]];
}
//Check if file exists and load log if it does.
NSFileHandle * logFile = [NSFileHandle fileHandleForReadingAtPath:logFileName];
if(logFile != nil)
{
if(isXML)
{
//TODO: Read XML file
}
else
{
log = [[NSMutableAttributedString alloc] initWithRTF:[logFile readDataToEndOfFile] documentAttributes:NULL];
}
}
else
{
if(isXML)
{
//TODO: Read XML file
}
else
{
log = [[NSMutableAttributedString alloc] init];
}
}
}
- (id) initWithPerson:(XMPPPerson*)person useXMLFormatLog:(BOOL)_xml
{
self = [self init];
if(self == nil)
{
return nil;
}
isXML = _xml;
remoteEntity = person;
[self initLog];
return self;
}
+ (id) chatLogWithPerson:(XMPPPerson*)person useXMLFormatLog:(BOOL)_xml
{
return [[XMPPChatLog alloc] initWithPerson:person useXMLFormatLog:_xml];
}
- (id) initWithPerson:(XMPPPerson*)aPerson
{
return [self initWithPerson:(XMPPPerson*)aPerson useXMLFormatLog:NO];
}
- (id) init
{
self = [super init];
if(self == nil)
{
return nil;
}
isXML = NO;
today = [[NSCalendarDate alloc] init];
logFileName = nil;
remoteEntity = nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(autoSave:)
name:@"NSApplicationWillTerminateNotification"
object:NSApp];
return self;
}
- (BOOL) update
{
//Check if today is stil today
if([today dayOfCommonEra] != [[NSCalendarDate date] dayOfCommonEra])
{
#ifndef DNDEBG
ETLog(@"Rolling over chat logs");
#endif
[self save];
today = [[NSCalendarDate alloc] init];
[self initLog];
return YES;
}
return NO;
}
- (void) autoSave:(NSTimer*)_sender
{
autoSaveTimer = nil;
if(![self update])
{
[self save];
}
}
- (id) logErrorMessage:(XMPPMessage*)aMessage
{
XMPPError * error = [aMessage error];
NSCalendarDate * timestamp = [[aMessage timestamp] time];
if(timestamp == nil)
{
timestamp = [NSCalendarDate calendarDate];
}
NSString * errorString = [NSString stringWithFormat:@"(%@) Error %d:\n%@\n",
[timestamp descriptionWithCalendarFormat:@"%H:%M:%S"],
[error errorCode],
[[error errorMessage] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSAttributedString * errorMessage = [[NSAttributedString alloc] initWithString:errorString
attributes:ERROR_STYLE];
[log appendAttributedString:errorMessage];
return errorMessage;
}
- (id) logMessage:(XMPPMessage*)aMessage
{
if(isXML)
{
//TODO: Implement this
return nil;
}
else
{
if(autoSaveTimer == nil)
{
autoSaveTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)15.0
target:self
selector:@selector(autoSave:)
userInfo:nil
repeats:NO];
}
if([aMessage type] == MESSAGE_TYPE_ERROR)
{
return [self logErrorMessage:aMessage];
}
//TODO: Make `you / he says' message colours configurable.
//TODO: Localise messages
BOOL emote = ([[aMessage body] length] >= 3 && [[[aMessage body] substringToIndex:3] isEqualToString:@"/me"]);
NSMutableAttributedString * messageText;
NSColor * headerColour;
NSCalendarDate * timestamp = [[aMessage timestamp] time];
if(timestamp == nil)
{
timestamp = [NSCalendarDate calendarDate];
}
NSMutableString * headerString = [NSMutableString stringWithFormat:@"(%@) ",[timestamp descriptionWithCalendarFormat:@"%H:%M:%S"]];
if([aMessage in])
{
headerColour = [NSColor colorWithCalibratedRed:0.0f
green:0.0f
blue:1.0f
alpha:1.0f];
if(emote)
{
[headerString appendString:[NSString stringWithFormat:@"%@ ", [remoteEntity name]]];
}
else
{
[headerString appendString:[NSString stringWithFormat:@"%@ says:\n", [remoteEntity name]]];
}
}
else
{
headerColour = [NSColor colorWithCalibratedRed:1.0f
green:0.0f
blue:0.0f
alpha:1.0f];
if(emote)
{
[headerString appendString:@"Your Avatar "];
}
else
{
[headerString appendString:@"You say:\n"];
}
}
messageText = [[NSMutableAttributedString alloc] initWithString:headerString];
NSAttributedString * attributedMessageBody = [aMessage HTMLBody];
if(emote)
{
attributedMessageBody = [attributedMessageBody attributedSubstringFromRange:NSMakeRange(3,[[aMessage body] length]-3)];
//messageBody = [messageBody stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
[messageText addAttribute:NSForegroundColorAttributeName value:headerColour range:NSMakeRange(0,[messageText length])];
[messageText appendAttributedString:attributedMessageBody];
[messageText appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
[log appendAttributedString:messageText];
return messageText;
}
}
- (BOOL) isXML
{
return isXML;
}
- (BOOL) save
{
NS_DURING
{
if(isXML)
{
//TODO: Implement this
}
else
{
#ifndef DNDEBUG
ETLog(@"Saving log: \"%@\"", logFileName);
#endif
NSFileHandle * logFile = [[NSFileHandle alloc] initWithFileDescriptor:fileno(fopen([logFileName UTF8String], "w")) closeOnDealloc:YES];
[logFile writeData:[log RTFFromRange:NSMakeRange(0,[(NSAttributedString*)log length]) documentAttributes:nil]];
}
}
NS_HANDLER
{
return NO;
}
NS_ENDHANDLER
return YES;
}
+ (NSString*) logPath
{
return logBasePath;
}
- (id) getLogForToday
{
return log;
}
@end