24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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
|
-
+
+
+
+
|
#include <stdarg.h>
#import <ObjFW/OFString.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFMutableDictionary.h>
#import <ObjFW/OFTCPSocket.h>
#import <ObjFW/OFAutoreleasePool.h>
#import <ObjFW/OFInvalidEncodingException.h>
#import <ObjFW/macros.h>
#import "IRCConnection.h"
#import "IRCUser.h"
@implementation IRCConnection
@synthesize server = _server, port = _port;
@synthesize nickname = _nickname, username = _username, realname = _realname;
@synthesize delegate = _delegate, socket = _socket;
+ (instancetype)connection
{
return [[[self alloc] init] autorelease];
}
- init
{
|
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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
-
-
+
+
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
|
[_username release];
[_realname release];
[_channels release];
[super dealloc];
}
- (void)setServer: (OFString*)server
{
OF_SETTER(_server, server, true, 1)
}
- (OFString*)server
{
OF_GETTER(_server, true)
}
- (void)setPort: (uint16_t)port
{
_port = port;
}
- (uint16_t)port
{
return _port;
}
- (void)setNickname: (OFString*)nickname
{
OF_SETTER(_nickname, nickname, true, 1)
}
- (OFString*)nickname
{
OF_GETTER(_nickname, true)
}
- (void)setUsername: (OFString*)username
{
OF_SETTER(_username, username, true, 1)
}
- (OFString*)username
{
OF_GETTER(_username, true)
}
- (void)setRealname: (OFString*)realname
{
OF_SETTER(_realname, realname, true, 1)
}
- (OFString*)realname
{
OF_GETTER(_realname, true)
}
- (void)setDelegate: (id <IRCConnectionDelegate>)delegate
{
_delegate = delegate;
}
- (id <IRCConnectionDelegate>)delegate
{
OF_GETTER(_delegate, false)
}
- (OFTCPSocket*)socket
{
OF_GETTER(_socket, true)
}
- (void)connect
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
void *pool = objc_autoreleasePoolPush();
if (_socket != nil)
@throw [OFAlreadyConnectedException exception];
_socket = [[OFTCPSocket alloc] init];
[_socket connectToHost: _server
port: _port];
[self sendLineWithFormat: @"NICK %@", _nickname];
[self sendLineWithFormat: @"USER %@ * 0 :%@", _username, _realname];
[pool release];
objc_autoreleasePoolPop(pool);
}
- (void)disconnect
{
[self disconnectWithReason: nil];
}
- (void)disconnectWithReason: (OFString*)reason
{
void *pool = objc_autoreleasePoolPush();
reason = [[reason componentsSeparatedByString: @"\n"] firstObject];
if (reason == nil)
[self sendLine: @"QUIT"];
else
[self sendLineWithFormat: @"QUIT :%@", reason];
objc_autoreleasePoolPop(pool);
}
- (void)joinChannel: (OFString*)channel
{
void *pool = objc_autoreleasePoolPush();
channel = [[channel componentsSeparatedByString: @"\n"] firstObject];
[self sendLineWithFormat: @"JOIN %@", channel];
objc_autoreleasePoolPop(pool);
}
- (void)leaveChannel: (OFString*)channel
{
[self leaveChannel: channel
reason: nil];
}
- (void)leaveChannel: (OFString*)channel
reason: (OFString*)reason
{
void *pool = objc_autoreleasePoolPush();
channel = [[channel componentsSeparatedByString: @"\n"] firstObject];
reason = [[reason componentsSeparatedByString: @"\n"] firstObject];
if (reason == nil)
[self sendLineWithFormat: @"PART %@", channel];
else
[self sendLineWithFormat: @"PART %@ :%@", channel, reason];
[_channels removeObjectForKey: channel];
objc_autoreleasePoolPop(pool);
}
- (void)sendLine: (OFString*)line
{
if ([_delegate respondsToSelector: @selector(connection:didSendLine:)])
[_delegate connection: self
didSendLine: line];
[_socket writeLine: line];
}
- (void)sendLineWithFormat: (OFConstantString*)format, ...
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
void *pool = objc_autoreleasePoolPush();
OFString *line;
va_list args;
va_start(args, format);
line = [[[OFString alloc] initWithFormat: format
arguments: args] autorelease];
va_end(args);
[self sendLine: line];
[pool release];
objc_autoreleasePoolPop(pool);
}
- (void)sendMessage: (OFString*)msg
to: (OFString*)to
{
OFArray *lines = [msg componentsSeparatedByString: @"\n"];
void *pool = objc_autoreleasePoolPush();
OFEnumerator *enumerator = [lines objectEnumerator];
OFString *line;
while ((line = [enumerator nextObject]) != nil)
for (OFString *line in [msg componentsSeparatedByString: @"\n"])
[self sendLineWithFormat: @"PRIVMSG %@ :%@", to, line];
objc_autoreleasePoolPop(pool);
}
- (void)sendNotice: (OFString*)notice
to: (OFString*)to
{
OFArray *lines = [notice componentsSeparatedByString: @"\n"];
void *pool = objc_autoreleasePoolPush();
OFEnumerator *enumerator = [lines objectEnumerator];
OFString *line;
while ((line = [enumerator nextObject]) != nil)
for (OFString *line in [notice componentsSeparatedByString: @"\n"])
[self sendLineWithFormat: @"NOTICE %@ :%@", to, line];
objc_autoreleasePoolPop(pool);
}
- (void)kickUser: (OFString*)user
channel: (OFString*)channel
reason: (OFString*)reason
{
void *pool = objc_autoreleasePoolPush();
reason = [[reason componentsSeparatedByString: @"\n"] firstObject];
[self sendLineWithFormat: @"KICK %@ %@ :%@", channel, user, reason];
objc_autoreleasePoolPop(pool);
}
- (void)changeNicknameTo: (OFString*)nickname
{
void *pool = objc_autoreleasePoolPush();
nickname = [[nickname componentsSeparatedByString: @"\n"]
firstObject];
[self sendLineWithFormat: @"NICK %@", nickname];
objc_autoreleasePoolPop(pool);
}
- (void)IRC_processLine: (OFString*)line
{
OFArray *components;
OFString *action = nil;
|
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
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
|
-
-
-
+
-
|
/* NAMES reply */
if ([action isEqual: @"353"] && [components count] >= 6) {
OFString *where;
OFMutableSet *channel;
OFArray *users;
size_t pos;
OFEnumerator *enumerator;
OFString *user;
where = [components objectAtIndex: 4];
if ((channel = [_channels objectForKey: where]) == nil) {
/* We did not request that */
return;
}
pos = [[components objectAtIndex: 0] length] +
[[components objectAtIndex: 1] length] +
[[components objectAtIndex: 2] length] +
[[components objectAtIndex: 3] length] +
[[components objectAtIndex: 4] length] + 6;
users = [[line substringWithRange:
of_range(pos, [line length] - pos)]
componentsSeparatedByString: @" "];
enumerator = [users objectEnumerator];
for (OFString *user in users) {
while ((user = [enumerator nextObject]) != nil) {
if ([user hasPrefix: @"@"] || [user hasPrefix: @"+"] ||
[user hasPrefix: @"%"] || [user hasPrefix: @"*"])
user = [user substringWithRange:
of_range(1, [user length] - 1)];
[channel addObject: user];
}
|
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
-
-
-
+
-
-
-
-
+
-
|
/* QUIT */
if ([action isEqual: @"QUIT"] && [components count] >= 2) {
OFString *who = [components objectAtIndex: 0];
IRCUser *user;
OFString *reason = nil;
size_t pos = [who length] + 1 +
[[components objectAtIndex: 1] length];
OFEnumerator *enumerator;
OFMutableSet *channel;
who = [who substringWithRange: of_range(1, [who length] - 1)];
user = [IRCUser IRCUserWithString: who];
if ([components count] > 2)
reason = [line substringWithRange:
of_range(pos + 2, [line length] - pos - 2)];
enumerator = [_channels objectEnumerator];
for (OFMutableSet *channel in _channels)
while ((channel = [enumerator nextObject]) != nil)
[channel removeObject: [user nickname]];
if ([_delegate respondsToSelector:
@selector(connection:didSeeUserQuit:reason:)])
[_delegate connection: self
didSeeUserQuit: user
reason: reason];
return;
}
/* NICK */
if ([action isEqual: @"NICK"] && [components count] == 3) {
OFString *who = [components objectAtIndex: 0];
OFString *nickname = [components objectAtIndex: 2];
IRCUser *user;
OFEnumerator *enumerator;
OFMutableSet *channel;
who = [who substringWithRange: of_range(1, [who length] - 1)];
nickname = [nickname substringWithRange:
of_range(1, [nickname length] - 1)];
user = [IRCUser IRCUserWithString: who];
if ([[user nickname] isEqual: _nickname]) {
[_nickname release];
_nickname = [nickname copy];
}
enumerator = [_channels objectEnumerator];
for (OFMutableSet *channel in _channels) {
while ((channel = [enumerator nextObject]) != nil) {
if ([channel containsObject: [user nickname]]) {
[channel removeObject: [user nickname]];
[channel addObject: nickname];
}
}
if ([_delegate respondsToSelector:
|