-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkkongchi.js
114 lines (84 loc) · 2.52 KB
/
kkongchi.js
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
var net = require('net');
var sys = require('sys');
var ircServer = {
host: 'irc.ozinger.org',
port: 6667,
channel: [ '#langdev','#hangulize' ]
};
var botConfig = {
nick: '꽁치',
user: 'kkongchi',
verbose: false
};
var sock = net.createConnection(ircServer.port, ircServer.host);
sock.setEncoding('utf8');
packetHandlers = [
[ /^\:[a-zA-Z0-9_\.]+\s001/, function(socket) {
if ( typeof(ircServer.channel) == 'string' ) {
writeln(socket,"JOIN "+ircServer.channel);
} else {
ircServer.channel.forEach(function(ch) {
writeln(socket,"JOIN "+ch);
});
}
}
],
[ /^PING\s\:(.+)/, function(socket,line,m) { writeln(socket, "PONG :" + m[1]);} ],
[ /^\:(.+)\sPRIVMSG\s(.+)\s\:(.+)/, function(socket,line,m) {
var from = m[1];
var ch = m[2];
var text = m[3];
/*if ( /낚지/.test(text) == true ) {
writeln(socket,"PRIVMSG " + ch + " :국산 산낚지는 안전하지 말입니다");
}*/
var hlize = /^\;hlize(\([a-z]{2}\))?\s(.*)/.exec(text);
if ( hlize != null ) {
var ln = hlize[1];
if ( ln != undefined ) {
ln = ln.replace('(','').replace(')','');
}
hangulize(sock, ch, hlize[2],ln);
}
} ]
];
sock.on('connect', function() {
log("connected");
writeln(sock,"USER " + botConfig.user +" 0 * :"+botConfig.user);
writeln(sock,"NICK " + botConfig.nick);
});
sock.on('data', function(data) {
if ( botConfig.verbose == true ) { log(data); }
var lines = data.split("\r\n");
lines.forEach(function(line) {
packetHandlers.forEach(function(obj) {
var exp = obj[0];
var fun = obj[1];
var m = exp.exec(line);
if ( m != null ) {
fun(sock, line, m);
}
});
});
});
function log(text) {
sys.log(text);
};
function writeln(socket,data) {
socket.write(data+"\r\n");
};
function hangulize(sock,ch,data,lang) {
require('./hangulize').hangulize( data, function(result, respData) {
var lang = respData.request.lang;
var word = respData.request.word;
require('sys').log(JSON.stringify(respData));
if ( result == true && respData.response.success == true ) {
writeln(sock, "PRIVMSG " + ch + " :"+word+"("+lang+") => " + respData.response.result);
} else {
if ( respData.response.reason != undefined ) {
writeln(sock, "PRIVMSG " + ch + " :"+word+"("+lang+") => " + respData.response.reason);
} else {
writeln(sock, "PRIVMSG " + ch + " :"+word+"("+lang+") => 오류");
}
}
}, lang);
};