/
IRCConnection.m
257 lines (207 loc) · 5.19 KB
/
IRCConnection.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
// LimeChat is copyrighted free software by Satoshi Nakagawa <psychs AT limechat DOT net>.
// You can redistribute it and/or modify it under the terms of the GPL version 2 (see the file GPL.txt).
#import "IRCConnection.h"
#include <SystemConfiguration/SystemConfiguration.h>
#import "IRC.h"
#import "NSData+Kana.h"
#define TIMER_INTERVAL 2
#define PENALTY_THREASHOLD 5
@interface IRCConnection (Private)
- (void)updateTimer;
- (void)tryToSend;
@end
@implementation IRCConnection
@synthesize delegate;
@synthesize host;
@synthesize port;
@synthesize useSSL;
@synthesize encoding;
@synthesize useSystemSocks;
@synthesize useSocks;
@synthesize socksVersion;
@synthesize proxyHost;
@synthesize proxyPort;
@synthesize proxyUser;
@synthesize proxyPassword;
@synthesize loggedIn;
- (id)init
{
self = [super init];
if (self) {
encoding = NSUTF8StringEncoding;
sendQueue = [NSMutableArray new];
timer = [Timer new];
timer.delegate = self;
}
return self;
}
- (void)dealloc
{
[host release];
[proxyHost release];
[proxyUser release];
[proxyPassword release];
[conn close];
[conn autorelease];
[sendQueue release];
[timer stop];
[timer release];
[super dealloc];
}
- (void)open
{
[self close];
conn = [TCPClient new];
conn.delegate = self;
conn.host = host;
conn.port = port;
conn.useSSL = useSSL;
if (useSystemSocks) {
// check if system socks proxy is enabled
CFDictionaryRef proxyDic = SCDynamicStoreCopyProxies(NULL);
NSNumber* num = (NSNumber*)CFDictionaryGetValue(proxyDic, kSCPropNetProxiesSOCKSEnable);
BOOL systemSocksEnabled = [num intValue] != 0;
CFRelease(proxyDic);
conn.useSocks = systemSocksEnabled;
conn.useSystemSocks = systemSocksEnabled;
}
else {
conn.useSocks = useSocks;
conn.socksVersion = socksVersion;
}
conn.proxyHost = proxyHost;
conn.proxyPort = proxyPort;
conn.proxyUser = proxyUser;
[conn open];
}
- (void)close
{
loggedIn = NO;
[timer stop];
[sendQueue removeAllObjects];
[conn close];
[conn autorelease];
conn = nil;
}
- (BOOL)active
{
return [conn active];
}
- (BOOL)connecting
{
return [conn connecting];
}
- (BOOL)connected
{
return [conn connected];
}
- (BOOL)readyToSend
{
return !sending && penalty == 0;
}
- (void)clearSendQueue
{
[sendQueue removeAllObjects];
[self updateTimer];
}
- (void)sendLine:(NSString*)line
{
[sendQueue addObject:line];
[self tryToSend];
[self updateTimer];
}
- (NSData*)convertToCommonEncoding:(NSString*)s
{
NSData* data = [s dataUsingEncoding:encoding];
if (!data) {
data = [s dataUsingEncoding:encoding allowLossyConversion:YES];
if (!data) {
data = [s dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
}
}
if (encoding == NSISO2022JPStringEncoding) {
if (data) {
data = [data convertKanaFromISO2022ToNative];
}
}
return data;
}
- (void)tryToSend
{
if ([sendQueue count] == 0) return;
if (sending) return;
if (penalty > PENALTY_THREASHOLD) return;
NSString* s = [sendQueue objectAtIndex:0];
s = [s stringByAppendingString:@"\r\n"];
[sendQueue removeObjectAtIndex:0];
NSData* data = [self convertToCommonEncoding:s];
if (data) {
sending = YES;
if (loggedIn) {
penalty += IRC_PENALTY_NORMAL;
}
[conn write:data];
if ([delegate respondsToSelector:@selector(ircConnectionWillSend:)]) {
[delegate ircConnectionWillSend:s];
}
}
}
- (void)updateTimer
{
if (!sendQueue.count && penalty <= 0) {
if (timer.isActive) {
[timer stop];
}
}
else {
if (!timer.isActive) {
[timer start:TIMER_INTERVAL];
}
}
}
- (void)timerOnTimer:(id)sender
{
if (penalty > 0) penalty -= TIMER_INTERVAL;
if (penalty < 0) penalty = 0;
[self tryToSend];
[self updateTimer];
}
- (void)tcpClientDidConnect:(TCPClient*)sender
{
[sendQueue removeAllObjects];
if ([delegate respondsToSelector:@selector(ircConnectionDidConnect:)]) {
[delegate ircConnectionDidConnect:self];
}
}
- (void)tcpClient:(TCPClient*)sender error:(NSString*)error
{
[timer stop];
[sendQueue removeAllObjects];
if ([delegate respondsToSelector:@selector(ircConnectionDidError:)]) {
[delegate ircConnectionDidError:error];
}
}
- (void)tcpClientDidDisconnect:(TCPClient*)sender
{
[timer stop];
[sendQueue removeAllObjects];
if ([delegate respondsToSelector:@selector(ircConnectionDidDisconnect:)]) {
[delegate ircConnectionDidDisconnect:self];
}
}
- (void)tcpClientDidReceiveData:(TCPClient*)sender
{
while (1) {
NSData* data = [conn readLine];
if (!data) break;
if ([delegate respondsToSelector:@selector(ircConnectionDidReceive:)]) {
[delegate ircConnectionDidReceive:data];
}
}
}
- (void)tcpClientDidSendData:(TCPClient*)sender
{
sending = NO;
[self tryToSend];
}
@end