-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
182 lines (148 loc) · 6.05 KB
/
index.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
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
var Steam = require('steam');
var fs = require('fs');
// if we've saved a server list, use it
if (fs.existsSync('servers')) {
Steam.servers = JSON.parse(fs.readFileSync('servers'));
}
module.exports = function(details) {
var msgFormat = details.msgFormat || '\u000302%s\u000f: %s';
var emoteFormat = details.emoteFormat || '\u000302%s %s';
var msgFormatGame = details.msgFormatGame || details.msgFormat || '\u000303%s\u000f: %s';
var emoteFormatGame = details.emoteFormatGame || details.emoteFormat || '\u000303%s %s';
var queue = [];
function sendMessage(msg) {
if (steam.loggedOn) {
steam.sendMessage(details.chatroom, msg);
} else {
queue.push(msg);
}
}
var irc = new (require('irc')).Client(details.server, details.nick, {
channels: [details.channel]
});
irc.on('error', function(err) {
console.log('IRC error: ', err);
});
irc.on('message' + details.channel, function(from, message) {
sendMessage('<' + from + '> ' + message);
if (!steam.loggedOn)
return;
var parts = message.match(/(\S+)\s+(.*\S)/);
var triggers = {
'.k': 'kick',
'.kb': 'ban',
'.unban': 'unban'
};
if (parts && parts[1] in triggers) {
irc.whois(from, function(info) {
if (info.channels.indexOf('@' + details.channel) == -1)
return; // not OP, go away
Object.keys(steam.users).filter(function(steamID) {
return steam.users[steamID].playerName == parts[2];
}).forEach(function(steamID) {
steam[triggers[parts[1]]](details.chatroom, steamID);
});
});
} else if (message.trim() == '.userlist') {
Object.keys(steam.chatRooms[details.chatroom]).forEach(function(steamID) {
irc.notice(from, steam.users[steamID].playerName + ' http://steamcommunity.com/profiles/' + steamID);
});
}
});
irc.on('action', function(from, to, message) {
if (to == details.channel) {
sendMessage(from + ' ' + message);
}
});
irc.on('+mode', function(channel, by, mode, argument, message) {
if (channel == details.channel && mode == 'b') {
sendMessage(by + ' sets ban on ' + argument);
}
});
irc.on('-mode', function(channel, by, mode, argument, message) {
if (channel == details.channel && mode == 'b') {
sendMessage(by + ' removes ban on ' + argument);
}
});
irc.on('kick' + details.channel, function(nick, by, reason, message) {
sendMessage(by + ' has kicked ' + nick + ' from ' + details.channel + ' (' + reason + ')');
});
irc.on('join' + details.channel, function(nick) {
sendMessage(nick + ' has joined ' + details.channel);
});
irc.on('part' + details.channel, function(nick) {
sendMessage(nick + ' has left ' + details.channel);
});
irc.on('quit', function(nick, reason) {
sendMessage(nick + ' has quit (' + reason + ')');
});
var steam = new Steam.SteamClient();
steam.logOn({
accountName: details.username,
password: details.password,
authCode: details.authCode,
shaSentryfile: require('fs').existsSync('sentry') ? require('fs').readFileSync('sentry') : undefined
});
steam.on('servers', function(servers) {
fs.writeFile('servers', JSON.stringify(servers));
});
steam.on('loggedOn', function(result) {
console.log('Logged on!');
steam.setPersonaState(Steam.EPersonaState.Online);
steam.joinChat(details.chatroom);
queue.forEach(sendMessage);
queue = [];
});
steam.on('chatMsg', function(chatRoom, message, msgType, chatter) {
var game = steam.users[chatter].gameName;
var name = steam.users[chatter].playerName;
if (msgType == Steam.EChatEntryType.ChatMsg) {
irc.say(details.channel, require('util').format(game ? msgFormatGame : msgFormat, name, message));
} else if (msgType == Steam.EChatEntryType.Emote) {
irc.say(details.channel, require('util').format(game ? emoteFormatGame : emoteFormat, name, message));
}
var parts = message.split(/\s+/);
var permissions = steam.chatRooms[chatRoom][chatter].permissions;
if (parts[0] == '.k' && permissions & Steam.EChatPermission.Kick) {
irc.send('KICK', details.channel, parts[1], 'requested by ' + name);
} else if (parts[0] == '.kb' && permissions & Steam.EChatPermission.Ban) {
irc.send('MODE', details.channel, '+b', parts[1]);
irc.send('KICK', details.channel, parts[1], 'requested by ' + name);
} else if (parts[0] == '.unban' && permissions & Steam.EChatPermission.Ban) {
irc.send('MODE', details.channel, '-b', parts[1]);
} else if (parts[0] == '.userlist') {
irc.send('NAMES', details.channel);
irc.once('names' + details.channel, function(nicks) {
steam.sendMessage(chatter, 'Users in ' + details.channel + ':\n' + Object.keys(nicks).map(function(key) {
return nicks[key] + key;
}).join('\n'));
});
}
});
steam.on('chatStateChange', function(stateChange, chatterActedOn, chat, chatterActedBy) {
var name = steam.users[chatterActedOn].playerName + ' (http://steamcommunity.com/profiles/' + chatterActedOn + ')';
switch (stateChange) {
case Steam.EChatMemberStateChange.Entered:
irc.say(details.channel, name + ' entered chat.');
break;
case Steam.EChatMemberStateChange.Left:
irc.say(details.channel, name + ' left chat.');
break;
case Steam.EChatMemberStateChange.Disconnected:
irc.say(details.channel, name + ' disconnected.');
break;
case Steam.EChatMemberStateChange.Kicked:
irc.say(details.channel, name + ' was kicked by ' + steam.users[chatterActedBy].playerName + '.');
break;
case Steam.EChatMemberStateChange.Banned:
irc.say(details.channel, name + ' was banned by ' + steam.users[chatterActedBy].playerName + '.');
}
});
steam.on('loggedOff', function(result) {
console.log("Logged off:", result);
});
steam.on('sentry', function(data) {
require('fs').writeFileSync('sentry', data);
})
steam.on('debug', console.log);
};