duplicate saftey
[KisSync.git] / src / channel / anonymouspost.js
blobe7bb50c56331a40f1f7761b28f53a480ecad46a1
1 "use strict";
3 var ChannelModule = require("./module");
4 var Flags = require("../flags");
6 function makeIPHash(ip) {
7         var numeric = 0;
8         for(var i = 0 ; i < ip.length ; i++){
9                 if(ip.charAt(i) != '.'){
10                         numeric += ip.charAt(i);
11                 }
12         }
13         numeric = parseInt(numeric);
14         return  makeID((numeric * 13) % 1000000);
17 function makeID(id_number){
18         var id_string = "" + id_number;
19         var processed_string = "";
20         for(var i = 0 ; i < id_string.length ; i += 2){
21                 var num = id_string.charAt(i)
22                 if(i + 1 < id_string.length) num += id_string.charAt(i + 1);
23                 num = parseInt(num) % 61;
24                 processed_string += convertNumToIDChar("" + num);
25         }
26         return processed_string;
29 // Xaekai on Jan 14, 2019
30 function convertNumToIDChar(num){
31         const i = parseInt(num, 10);
32         switch(true){
33                 case i > -1 && i < 26: return String.fromCharCode(i+97);
34                 case i > 25 && i < 52: return String.fromCharCode(i+39);
35                 case i > 51 && i < 61: return String.fromCharCode(i-3);
36                 case i === 61: return '0';
37         }
38         console.log('notret');
41 function AnonymousPost(_channel) {
42     ChannelModule.apply(this, arguments);
45 AnonymousPost.prototype = Object.create(ChannelModule.prototype);
47 AnonymousPost.prototype.onUserPreJoin = function (user, data, cb) {
48     const opts = this.channel.modules.options;
49     var anonymousPosting = opts.get("allow_anon_chat");
50         if(anonymousPosting && user.isAnonymous()){
51                 var hash = makeIPHash(user.realip);
52                 var anon_name = "Anonymous-" + hash;
53                 var duplicate_solution = 0;
54                 var duplicate_found = false;
55                 do{
56                         duplicate_found = false;
57                         user.channel.users.forEach((user, i) => {
58                                 if (!duplicated_found && user.account.name == anon_name){
59                                         duplicate_solution++;
60                                         anon_name = "Anonymous-" + hash + duplicate_solution;
61                                         duplicate_found = true;
62                                 }
63                         });
64                 }
65                 while(duplicated_found);
66                 user.guestLogin(anon_name);
67                 cb(null, ChannelModule.PASSTHROUGH);
68         }
69         else{
70                 cb(null, ChannelModule.PASSTHROUGH);
71         }
74 module.exports = AnonymousPost;
75 //# sourceMappingURL=anonymouspost.js.map