Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *   _____       _                 _  _____
  3.  * |  __ \     | |               | |/ ____|
  4.  * | |__) |   _| |__  _   _      | | (___
  5.  * |  _  / | | | '_ \| | | | _   | |\___ \
  6.  * | | \ \ |_| | |_) | |_| || |__| |____) |
  7.  * |_|  \_\__,_|_.__/ \__, (_)____/|_____/
  8.  *                    __/ |
  9.  *                   |___/
  10.  *
  11.  * The ultimate in bullshitting technology
  12.  *
  13.  * "Refactoring is for pussies" edition
  14.  */
  15.  
  16. var socket = require('request');
  17. var fs = require('fs');
  18.  
  19. var cookie_jar = socket.jar();
  20.  
  21. var cred = {
  22.     user: '',
  23.     pass: '',
  24.     wiki: '',
  25.     urll: '',
  26.     apil: ''
  27. };
  28.  
  29. var config = {
  30.     key: '',
  31.     room: '',
  32.     mod: '',
  33.     server: '',
  34.     host: '',
  35.     options: {}
  36. };
  37.  
  38. var http = {
  39.     headers: {
  40.         'User-Agent': 'Ruby.js/v1.0 (screw socket.io)',
  41.         'Content-Type': 'application/octet-stream',
  42.         'Accept': '*/*',
  43.         'Pragma': 'no-cache',
  44.         'Cache-Control': 'no-cache'
  45.     },
  46.     get: function(url, headers, callback) {
  47.         socket.get({url: url, jar: cookie_jar, headers: headers}, function(e, r, b) {
  48.             callback({error: e, response: r, body: b});
  49.         });
  50.     },
  51.     post: function(url, form, headers, callback) {
  52.         socket.post({url: url, form: form, jar: cookie_jar, headers: headers}, function(e, r, b) {
  53.             callback({error: e, response: r, body: b});
  54.         });
  55.     }
  56. };
  57.  
  58. var mediawiki = {
  59.     _callback: null,
  60.     _data: {},
  61.     _loggedin: false,
  62.     login: function(username, password, url, callback) {
  63.         if (this._loggedin === false) {
  64.             if (typeof callback === 'function') {
  65.                 this._callback = callback;
  66.                 this._data.url = url;
  67.                 this._data.form = {
  68.                     action: 'login',
  69.                     lgname: username,
  70.                     lgpassword: password,
  71.                     format: 'json'
  72.                 };
  73.  
  74.                 http.post(this._data.url, this._data.form, {}, function(response) {
  75.                     if (!response.error) {
  76.                         mediawiki._data.form.lgtoken = (JSON.parse(response.body)).login.token;
  77.  
  78.                         http.post(mediawiki._data.url, mediawiki._data.form, {}, function(response) {
  79.                             if (!response.error) {
  80.                                 if ((JSON.parse(response.body)).login.result === 'Success') {
  81.                                     mediawiki._data = {};
  82.                                     mediawiki._loggedin = true;
  83.                                     mediawiki._callback();
  84.                                 } else {
  85.                                     console.log('ERROR: Could not log into the wiki!');
  86.                                     console.log('    -> ' + JSON.stringify(mediawiki._data));
  87.                                     console.log('    -> ' + response.body);
  88.                                 }
  89.                             } else {
  90.                                 console.log('ERROR: Could not log into the wiki!');
  91.                                 console.log('    -> ' + response.error);
  92.                             }
  93.                         });
  94.                     } else {
  95.                         console.log('ERROR: Could not log into the wiki!');
  96.                         console.log('    -> ' + response.error);
  97.                     }
  98.                 });
  99.             } else {
  100.                 console.log('ERROR: Callback is not a function!');
  101.                 console.log('    -> ' + typeof callback);
  102.             }
  103.         } else {
  104.             callback();
  105.         }
  106.     }
  107. };
  108.  
  109. function getChatInfo() {
  110.     http.get(cred.urll + '/wikia.php?controller=Chat&format=json', http.headers, function(response) {
  111.         if (!response.error) {
  112.             var data = JSON.parse(response.body);
  113.  
  114.             config.key = data.chatkey;
  115.             config.room = data.roomId;
  116.             config.mod = data.isChatMod;
  117.             config.host = data.chatServerHost;
  118.  
  119.             http.get(cred.apil + '/api.php?action=query&meta=siteinfo&siprop=wikidesc&format=json', {}, function(response) {
  120.                 if (!response.error) {
  121.                     var data = JSON.parse(response.body);
  122.  
  123.                     config.server = data.query.wikidesc.id;
  124.                     config.options.name: cred.user;
  125.                     config.options.EIO: 2;
  126.                     config.options.transport: 'polling';
  127.                     config.options.key: config.key;
  128.                     config.options.roomId: config.room;
  129.                     config.options.serverId: config.server;
  130.                     config.options.wikiId: config.server;
  131.                 } else {
  132.                     console.log('ERROR: Could not fetch chat config!');
  133.                     console.log('    -> ' + response.error);
  134.                 }
  135.             });
  136.         } else {
  137.             console.log('ERROR: Could not fetch chat config!');
  138.             console.log('    -> ' + response.error);
  139.         }
  140.     });
  141. }
  142.  
  143. function login() {
  144.     mediawiki.login(cred.user, cred.pass, cred.apil, function() {
  145.         getChatInfo();
  146.     });
  147. }
  148.  
  149. function start() {
  150.     try {
  151.         var data = JSON.parse(fs.readFileSync('config.json', 'UTF-8'));
  152.     } catch (e) {
  153.         console.log('ERROR: Could not parse config.json!');
  154.         console.log('    -> ' + e);
  155.     }
  156.  
  157.     if (data.username) {
  158.         if (data.passsword) {
  159.             if (data.wiki) {
  160.                 cred.user = data.username;
  161.                 cred.pass = data.password;
  162.                 cred.wiki = data.wiki;
  163.                 cred.urll = 'http://' + data.wiki + '.wikia.com'
  164.                 cred.apil = cred.urll + '/api.php';
  165.  
  166.                 login();
  167.             } else {
  168.                 console.log('ERROR: No wiki name!');
  169.             }
  170.         } else {
  171.             console.log('ERROR: No password!');
  172.         }
  173.     } else {
  174.         console.log('ERROR: No username!');
  175.     }
  176. }
  177.  
  178. start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement