A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.5 KiB

11 years ago
  1. var file = require('fs'),
  2. http = require('http'),
  3. querystring = require('querystring'),
  4. mustache = require('mustache');
  5. function Server(irc, options) {
  6. var __self = this;
  7. __self.options = options || {};
  8. __self.irc = irc;
  9. //config
  10. __self.root = __self.options.root || '';
  11. __self.channel = __self.options.channel.toLowerCase() || '';
  12. __self.bot = __self.options.bot_name || '';
  13. __self.port = __self.options.dashboard_id || '';
  14. }
  15. Server.prototype.start = function () {
  16. var __self = this;
  17. http.createServer(function (request, response) {
  18. var handle, patterns, rest_uri, match, mimes;
  19. mimes = [
  20. {css: 'text/css', encoding: 'utf8'},
  21. {js: 'application/javascript', encoding: 'utf8'},
  22. {html: 'text/html', encoding: 'utf8'},
  23. {jpg: 'image/jpeg', encoding: 'binary'},
  24. {png: 'image/png', encoding: 'binary'},
  25. {gif: 'image/gif', encoding: 'binary'},
  26. {eot: 'application/vnd.ms-fontobject', encoding: 'binary'},
  27. {otf: 'application/octet-stream', encoding: 'binary'},
  28. {ttf: 'application/octet-stream', encoding: 'binary'},
  29. {woff: 'application/x-font-woff', encoding: 'binary'}
  30. ];
  31. // remove start and end slashes
  32. rest_uri = request.url.replace(/^\/|\/$/i, '');
  33. // reg patterns
  34. patterns = {
  35. realtimechat: /get\/chat$/i,
  36. dashboard: /v1\/dashboard$/i,
  37. actions: /actions$/i,
  38. all: /(.*\/)(.*)\.(\w+)$|(.*)\.(\w+)$/i
  39. };
  40. // match with patterns
  41. for (var key in patterns) {
  42. if (patterns[key].test(rest_uri)) {
  43. match = rest_uri.match(patterns[key]);
  44. handle = key;
  45. break;
  46. }
  47. }
  48. // organize requests
  49. if (handle == 'realtimechat') {
  50. response.writeHead(200, {'Content-Type': 'application/json'});
  51. file.exists(__self.log, function (exists) {
  52. if (exists) {
  53. file.readFile(__self.log, 'utf8', function (err, data) {
  54. if (data !== null) {
  55. var json = [];
  56. data = data.split('\r\n');
  57. for (var i = 0; i < data.length - 1; i++) {
  58. json[i] = {id: i, text: data[i]};
  59. }
  60. response.end(JSON.stringify(json));
  61. }
  62. // TODO: fix up the json request so it takes less memory
  63. /*if (data !== null) {
  64. var json = [], max_length;
  65. data = data.split('\r\n');
  66. max_length = data.length > 200 ? 200 : data.length;
  67. for (var i = 0; i < max_length; i++) {
  68. var check_length = data.length > 200 ? (data.length - 200) + i : i;
  69. json[i] = {id: check_length, text: data[check_length]};
  70. }
  71. response.end(JSON.stringify(json));
  72. }*/
  73. });
  74. } else {
  75. response.end('{"Error":"Chat does not exist"}');
  76. }
  77. });
  78. }
  79. if (handle == 'dashboard') {
  80. file.readFile(__self.root + '/dashboard.html', 'utf8', function (err, data) {
  81. var template = {botname: __self.bot, channelname: __self.channel, user: __self.channel.charAt(0).toUpperCase() + __self.channel.slice(0).toLowerCase()};
  82. response.writeHead(200, {'Content-Type': 'text/html'});
  83. response.end(mustache.render(data, template));
  84. });
  85. }
  86. if (handle == 'actions') {
  87. if (request.method == 'POST') {
  88. var post_header = '';
  89. request.on('data', function (chunk) {
  90. post_header += chunk.toString();
  91. });
  92. request.on('end', function () {
  93. var decoded_header = querystring.parse(post_header);
  94. response.writeHead(200, {'Content-Type': 'text/html'});
  95. switch(decoded_header._method) {
  96. case 'reconnect':
  97. __self.irc.reconnect();
  98. response.end();
  99. break;
  100. case 'auction_open':
  101. __self.irc.msg('!arcoins auction open');
  102. response.end();
  103. break;
  104. case 'auction_close':
  105. __self.irc.msg('!arcoins auction close');
  106. response.end();
  107. break;
  108. default:
  109. response.end();
  110. }
  111. });
  112. }
  113. }
  114. if (handle == 'all') {
  115. file.exists(__self.root + '/' + match[0], function (exists) {
  116. if (exists) {
  117. var mime, encoding;
  118. for (var i = 0; i < mimes.length; i++) {
  119. for (var key in mimes[i]) {
  120. if (match[3] == key || match[5] == key) {
  121. mime = mimes[i][key];
  122. encoding = mimes[i].encoding;
  123. break;
  124. }
  125. }
  126. }
  127. file.readFile(__self.root + '/' + match[0], encoding, function (err, data) {
  128. response.writeHead(200, {'Content-Type': mime});
  129. if (encoding == 'binary') {
  130. response.end(data, 'binary');
  131. } else {
  132. response.end(data);
  133. }
  134. });
  135. } else {
  136. fourohfour();
  137. }
  138. });
  139. }
  140. if (handle === undefined) {
  141. fourohfour();
  142. }
  143. function fourohfour() {
  144. file.readFile(__self.root + '/404.html', 'utf8', function (err, data) {
  145. response.writeHead(404, {'Content-Type': 'text/html'});
  146. response.end(data);
  147. });
  148. }
  149. }).listen(__self.port);
  150. console.log('> Dashboard for ' + __self.bot + ' running on http://localhost:' + __self.port + '/v1/dashboard');
  151. };
  152. module.exports = function (irc, options) {
  153. return new Server(irc, options);
  154. };