Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.40 KB | None | 0 0
  1. var fs = require('fs');
  2. var crypto = require('crypto');
  3. var config = require('config');
  4. var moment = require('moment');
  5.  
  6. var SteamWeb = require('./steamweb');
  7.  
  8. var Steam = require('steam');
  9. var getSteamAPIKey = require('steam-web-api-key');
  10. var SteamWebLogOn = require('steam-weblogon');
  11.  
  12. var steamClient = new Steam.SteamClient();
  13. var steamUser = new Steam.SteamUser(steamClient);
  14. var steamFriends = new Steam.SteamFriends(steamClient);
  15. var steamWebLogOn = new SteamWebLogOn(steamClient, steamUser);
  16. var sw;
  17.  
  18. if (!config.get('steam.username')) {
  19. console.log('Either you have not set your steam username in the config or you are using default.json!');
  20. console.log('You\'ll need to go into /config and create the file production.json');
  21. console.log('Copy all of default.json and paste it in production.json and then fill it out.');
  22. console.log('');
  23. console.log('Finally you\'ll need to type in your console:');
  24. console.log('')
  25. console.log('export NODE_ENV=production');
  26. console.log('');
  27. console.log('Then trying running it again.');
  28. process.exit(0);
  29. }
  30.  
  31. // Login, self explanatory
  32. var logOnOptions = {
  33. account_name: config.get('steam.username'),
  34. password: config.get('steam.password')
  35. };
  36.  
  37. var authCode = process.argv[2];
  38.  
  39. // Check if sentry exists
  40. if (authCode == '2fa') logOnOptions.two_factor_code = process.argv[3]
  41. else {
  42. try {
  43. logOnOptions.sha_sentryfile = getSHA1(fs.readFileSync('sentry'));
  44. } catch (e) {
  45. if (authCode !== '') {
  46. logOnOptions.auth_code = authCode;
  47. }
  48. }
  49. }
  50.  
  51. // If we've said a server list, use it
  52. if (fs.existsSync('servers')) {
  53. Steam.servers = JSON.parse(fs.readFileSync('servers'));
  54. }
  55.  
  56. // Login
  57. steamClient.connect();
  58. steamClient.on('connected', function() {
  59. steamUser.logOn(logOnOptions);
  60. });
  61.  
  62. // Logged in response
  63. steamClient.on('logOnResponse', function(logonResp) {
  64. if (logonResp.eresult === Steam.EResult.OK) {
  65. console.log('Logged In!');
  66. steamFriends.setPersonaState(Steam.EPersonaState.Online);
  67. steamFriends.setPersonaName(config.get('name'));
  68. steamWebLogOn.webLogOn(function(sessionID, newCookie) {
  69. getSteamAPIKey({
  70. sessionID: sessionID,
  71. webCookie: newCookie
  72. }, function(err, APIKey) {
  73. if (err) console.log('[ERR STEAM API]',err);
  74. sw = SteamWeb(APIKey);
  75. console.log('Connected to Steam Web API');
  76. steamFriends.joinChat(config.get('groupId'));
  77. });
  78. });
  79. } else {
  80. if (logonResp.eresult == 85) {
  81. console.log('You need 2FA for this account!');
  82. } else if (logonResp.eresult == 63) {
  83. console.log('Code sent to your email (XXXXXX.'+logonResp.email_domain+').');
  84. console.log('You\'ll need to run:');
  85. console.log('node index <code>');
  86. console.log('Once in order to save the computer');
  87. console.log('IF YOU\'VE DONE ALL THIS: You may need to delete your "sentry" file.');
  88. } else {
  89. console.log('Houston, we have a problem! Info:',logonResp);
  90. }
  91. process.exit(0);
  92. }
  93. });
  94.  
  95. steamFriends.on('chatEnter', (chatId) => {
  96. console.log('Entered chat ('+chatId+')');
  97. Object.keys(roomUsers()).forEach(id => uniqueUsers.push(id));
  98. });
  99.  
  100. steamFriends.on('chatStateChange', (state, id, chatId, actionId) => { // Join/leave/kick/etc messages
  101. getName(id, (name) => {
  102. getName(actionId, (actionName) => {
  103. if (state == Steam.EChatMemberStateChange.Entered) {
  104. console.log(name + ' entered');
  105. chatLogs.push({ msg: name + ' entered' });
  106. if (uniqueUsers.indexOf(id) == -1) uniqueUsers.push(id);
  107. } else if (state = Steam.EChatMemberStateChange.Left) {
  108. console.log(name + ' left');
  109. chatLogs.push({ msg: name + ' left' });
  110. } else if (state = Steam.EChatMemberStateChange.Disconnected) {
  111. console.log(name + ' disconnected');
  112. chatLogs.push({ msg: name + ' disconnected' });
  113. } else if (state = Steam.EChatMemberStateChange.Kicked) {
  114. console.log(name + ' kicked by ' + actionName);
  115. chatLogs.push({ msg: name + ' kicked by ' + actionName });
  116. } else if (state = Steam.EChatMemberStateChange.Banned) {
  117. console.log(name + ' banned by ' + actionName);
  118. chatLogs.push({ msg: name + ' banned by ' + actionName });
  119. }
  120. });
  121. })
  122. });
  123.  
  124. var cacheNames = {}; // Cache names so we don't always have to ask the steam api
  125. var kickCount = {}; // Numbers of times kicked (track for 3 kick = ban)
  126. var chatLogs = []; // Stores logs, compiled in saveLog()
  127. var uniqueUsers = []; // Unique users, added to end of log
  128. var messageCount = {}; // Keeps track of messages in last 3 seconds (for spam kick)
  129.  
  130. steamFriends.on('chatMsg', (roomId, msg, type, id) => {
  131. if (roomId != config.get('groupId')) return;
  132. getName(id, (name) => {
  133. parseMessage(msg, id, name);
  134. });
  135. });
  136.  
  137. ////// BASIC ACTIONS
  138. function roomUsers() { return steamFriends.chatRooms[config.get('groupId')]; }
  139. function chat(msg) { steamFriends.sendMessage(config.get('groupId'), msg); }
  140. function kick(id, reason) {
  141. if (isAdmin(id)) return;
  142. console.log('Kicking '+id+' ('+reason+')');
  143. steamFriends.kick(config.get('groupId'), id);
  144. getName(id, (name) => {
  145. chatLogs.push({ msg: '*KICKED ' + name + ' (Reason: ' + reason + ')'});
  146. });
  147. kickCount[id] = (kickCount[id] || 0) + 1;
  148. if (kickCount[id] >= 3) ban(id, 'Three kicks');
  149. }
  150. function ban(id, reason) {
  151. if (isAdmin(id)) return;
  152. console.log('Banning '+id+' ('+reason+')')
  153. steamFriends.ban(config.get('groupId'), id);
  154. getName(id, (name) => {
  155. chatLogs.push({ msg: '***BANNED ' + name + ' (Reason: ' + reason + ')'});
  156. });
  157. }
  158. function unban(id) { steamFriends.unban(config.get('groupId'), id); }
  159. function lock() { steamFriends.lockChat(config.get('groupId')); }
  160. function unlock() { steamFriends.unlockChat(config.get('groupId')); }
  161. function moderated() { steamFriends.setModerated(config.get('groupId')); }
  162. function unmoderated() { steamFriends.setUnmoderated(config.get('groupId')); }
  163. function isAdmin(id) { return config.get('admins').indexOf(id) > -1 }
  164. //////////
  165.  
  166. function getName(id, cb) { // Either instantly (cached) or a long time (steam api to get name)
  167. if (!id) {
  168. cb(null);
  169. return;
  170. } else if (cacheNames[id]) {
  171. cb(cacheNames[id]);
  172. } else {
  173. sw.summary([id], (err, resp) => { // Get their data + name
  174. if (err) console.log('[STEAM API ERR]',err);
  175. else {
  176. if (!resp || !resp.players || !resp.players[0]) cb(null);
  177. else cb(resp.players[0].personaname);
  178. }
  179. });
  180. }
  181. }
  182.  
  183. function saveLog(cb) { // Called at end when closing bot
  184. var fileName = 'logs/log-'+moment().format('MMM[-]Do[-]h[_]mma')+'.txt';
  185. var userCount = {};
  186. uniqueUsers.forEach(u => userCount[u] = 0);
  187. var str = 'Logs - Saved at '+moment().format('LLLL');
  188. str += '\r\n\r\n';
  189. for (var c in chatLogs) {
  190. if (!chatLogs[c].msg) continue;
  191. if (chatLogs[c].name) {
  192. str += chatLogs[c].name + ': ' + chatLogs[c].msg + '\r\n';
  193. if (userCount[chatLogs[c].id] != undefined) userCount[chatLogs[c].id]++;
  194. } else {
  195. str += chatLogs[c].msg + '\r\n';
  196. }
  197. }
  198. str += '\r\n\r\n\r\nUsers:\r\n\r\n';
  199. var userCountArr = Object.keys(userCount).map(k => { return { id: k, count: userCount[k] } });
  200. userCountArr = userCountArr.sort((a, b) => a.count < b.count);
  201. var done = () => {
  202. totalDone++;
  203. if (totalDone == userCountArr.length) finish();
  204. else {
  205. var user = userCountArr[totalDone];
  206. getName(user.id, (name) => {
  207. str += name + ' - ' + user.id + ' (' + user.count + ')' + '\r\n';
  208. setTimeout(done, 0);
  209. });
  210. }
  211. };
  212. var totalDone = -1;
  213. var finish = () => fs.writeFile(fileName, str, cb);
  214. setTimeout(finish, 5*1000);
  215. done();
  216. }
  217.  
  218. setInterval(() => { // Check every 3 seconds, user said >= 5 messages? Kick
  219. Object.keys(messageCount).forEach(id => {
  220. if (messageCount[id] >= 5) kick(id, 'Spam');
  221.  
  222. });
  223. messageCount = {};
  224. }, 3*1000);
  225.  
  226. function parseMessage(msg, id, name) { // Deal with message
  227. console.log(name+': '+msg);
  228. chatLogs.push({
  229. name: name,
  230. id: id,
  231. msg: msg
  232. });
  233. messageCount[id] = (messageCount[id] || 0) + 1;
  234. var penalty = parseLanguage(msg, id);
  235. parseCommands(msg, id);
  236. }
  237.  
  238. function parseLanguage(msg, id) { // parse language or links
  239. if (isAdmin(id)) return;
  240. var urlChecker = /[^ ]+ ?(?:dot|\.) ?(?:com|org|net)/gi;
  241. if (msg.match(urlChecker)) { // Check to make sure not an illegal URL
  242. var m;
  243. do {
  244. m = urlChecker.exec(msg);
  245. if (!m) return;
  246. var url = m[0].replace(/(https?:\/\/)?(.+\.)?(?=.+\..+)/g, '');
  247. if (config.get('options.sites').split(',').indexOf(url) == -1) {
  248. kick(id, 'Unapproved Website');
  249. return;
  250. }
  251. } while (m);
  252. } else if (config.get('options.keywords').split(',').hasSame(msg.split(' '))) { // Keywords = inappropriate language
  253. kick(id, 'Language');
  254. }
  255. }
  256.  
  257. function parseCommands(msg, id) { // Parse commands (only one atm)
  258. if (msg.indexOf('!random' == 0) && isAdmin(id)) {
  259. var userList = Object.keys(roomUsers()).filter(user => !isAdmin(user));
  260. var rand = userList[Math.floor(Math.random() * userList.length)];
  261. getName(rand, (name) => {
  262. chat('Random User: ' + name + '!');
  263. });
  264. }
  265. }
  266.  
  267. // Save server list
  268. steamClient.on('servers', function(servers) {
  269. fs.writeFile('servers', JSON.stringify(servers));
  270. });
  271.  
  272. // Save sentry
  273. steamUser.on('updateMachineAuth', function(sentry, callback) {
  274. fs.writeFileSync('sentry', sentry.bytes);
  275. callback({ sha_file: getSHA1(sentry.bytes) });
  276. });
  277.  
  278. function getSHA1(bytes) { // Convert sentry
  279. var shasum = crypto.createHash('sha1');
  280. shasum.end(bytes);
  281. return shasum.read();
  282. }
  283.  
  284. steamFriends.on('relationships', () => { // "node index id" to see this
  285. if (process.argv[2] == 'id') {
  286. for (var g in steamFriends.groups) {
  287. console.log(g);
  288. }
  289. }
  290. });
  291.  
  292. Array.prototype.hasSame = function(arr2) {
  293. for (var i = 0; i < arr2.length; i++) {
  294. if (this.indexOf(arr2[i]) != -1) return true;
  295. }
  296. return false;
  297. }
  298.  
  299.  
  300. process.on('SIGINT', function() { // When closing, save (or don't if no logs)
  301. console.log('');
  302. if (!chatLogs.length) {
  303. console.log('No chat logs to save!');
  304. process.exit(0);
  305. }
  306. console.log('Saving Log (If this takes more than 20 seconds it will end early)');
  307. steamFriends.leaveChat(config.get('groupId'));
  308. saveLog(() => {
  309. console.log('Saved Log!');
  310.  
  311. process.exit(0);
  312. });
  313. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement