Advertisement
Guest User

Spinner - Spinchat bot

a guest
Apr 3rd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.04 KB | None | 0 0
  1. package spinner;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.InetSocketAddress;
  7. import java.net.Proxy;
  8. import java.net.Socket;
  9. import java.net.SocketAddress;
  10. import java.net.URL;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. import org.apache.commons.lang3.StringEscapeUtils;
  16.  
  17. public class Spinner {
  18.  
  19.     public static void main(String[] args) {
  20.        
  21.         if(args.length != 4) {
  22.             System.err.println("usage: java Spinner <BotNick> <BotPass> <Room> <Admin>");
  23.             System.exit(0);
  24.         }
  25.        
  26.         new Spinner(args[0], args[1], args[2], args[3]);
  27.     }
  28.    
  29.     public Spinner(String n, String p, String c, String a) {
  30.        
  31.         botNick = n;
  32.         botPass = p;
  33.         channel = c;
  34.         admin = a;
  35.         ignoreList = new ArrayList<String>();
  36.         idleList = new ArrayList<String>();
  37.        
  38.         connect();
  39.         join(channel);
  40.        
  41.         while(socket.isConnected()) {
  42.             read = readLine();
  43.             parse(read);
  44.         }
  45.        
  46.         // reconnect if socket broken
  47.         reConnect();
  48.     }
  49.    
  50.     // parse spin data
  51.     private void parse(String str) {
  52.        
  53.         char c = str.charAt(0);
  54.         switch(c) {
  55.        
  56.         case 'e':
  57.             break;
  58.        
  59.         case '+':
  60.             onChannelActivity(str);
  61.             break;
  62.        
  63.         case 'g':
  64.             onChannelTalk(str);
  65.             break;
  66.        
  67.         case 'h':
  68.             onPrivateRecv(str);
  69.             break;
  70.        
  71.         case 'j':
  72.             onUserlistRecv(str);
  73.             break;
  74.            
  75.         case 'J':
  76.             break;
  77.        
  78.         case 'k':
  79.             onUserInfoRecv(str);
  80.             break;
  81.        
  82.         case 's':
  83.             onServerLoadRecv(str);
  84.             break;
  85.        
  86.         case 'x':
  87.             onUserOfflineRecv(str);
  88.             break;
  89.         case '|':
  90.             onRoomOp(str);
  91.             break;
  92.             default:
  93.                 System.out.println("[SPIN]: " + str);
  94.         }
  95.     }
  96.    
  97.     private void channelTalk(String msg) {
  98.         send("g" + channel + "#a#" + msg);
  99.     }
  100.    
  101.     private void onChannelActivity(String str) {
  102.         split = str.split("#");
  103.         chanEvent = split[1];
  104.         user = split[2];
  105.         switch(chanEvent) {
  106.         case "a":
  107.             // room join
  108.             onRoomJoin(str);
  109.             break;
  110.         case "B":
  111.             // room kick
  112.             onRoomKick(str);
  113.             break;
  114.         case "A":
  115.             // room part
  116.             onRoomPart(str);
  117.             break;
  118.         case "L":
  119.             // room idle-out
  120.             onRoomIdleKick(str);
  121.             break;
  122.         case "Q":
  123.             // room logout
  124.             onRoomLogout(str);
  125.             break;
  126.         case "R":
  127.             // room logout
  128.             onRoomLogout(str);
  129.             break;
  130.         default:
  131.                 System.out.println("[X] - Received unknown channel event: " + chanEvent);
  132.                 return;
  133.         }
  134.     }
  135.  
  136.     private void onRoomOp(String str) {
  137.         // |Channel#a#LordIkon#Nope.#
  138.         split = str.split("#");
  139.         String mode = split[1];
  140.        
  141.         if(mode.equals("a")) {
  142.             System.out.println("[" + split[2] + " was opped by " + split[3] + "]");
  143.            
  144.             if(!idleListContains(split[3])) {
  145.                 addIdleUser(split[3]);
  146.             }
  147.             else if(idleListContains(split[3])) {
  148.                 resetIdleUser(split[3]);
  149.             }
  150.            
  151.         }
  152.        
  153.         else if(mode.equals("A")) {
  154.             System.out.println("[" + split[2] + " was deopped by " + split[3] + "]");
  155.            
  156.             if(!idleListContains(split[3])) {
  157.                 addIdleUser(split[3]);
  158.             }
  159.             else if(idleListContains(split[3])) {
  160.                 resetIdleUser(split[3]);
  161.             }
  162.         }
  163.     }
  164.  
  165.     private void onRoomJoin(String str) {
  166.         split = str.split("#");
  167.         user = split[2];
  168.         System.out.println("[" + user + " joined channel " + channel + "]");
  169.        
  170.         if(!idleListContains(user)) {
  171.             addIdleUser(user);
  172.         }
  173.        
  174.         else if(idleListContains(user)) {
  175.             resetIdleUser(user);
  176.         }
  177.     }
  178.  
  179.     private void onRoomKick(String str) {
  180.         split = str.split("#");
  181.         user = split[2];
  182.         System.out.println("[" + user + " was kicked from channel " + channel + "]");
  183.        
  184.         if(idleListContains(user)) {
  185.             removeIdleUser(user);
  186.         }
  187.     }
  188.  
  189.     private void onRoomPart(String str) {
  190.         split = str.split("#");
  191.         user = split[2];
  192.         System.out.println("[" + user + " left channel " + channel + "]");
  193.        
  194.         if(idleListContains(user)) {
  195.             removeIdleUser(user);
  196.         }
  197.     }
  198.  
  199.     private void onRoomIdleKick(String str) {
  200.         split = str.split("#");
  201.         user = split[2];
  202.         System.out.println("[" + user + " left channel " + channel + " (idled)]");
  203.        
  204.         if(idleListContains(user)) {
  205.             removeIdleUser(user);
  206.         }
  207.        
  208.         if(user.equals(botNick)) {
  209.             send("c" + channel);
  210.         }
  211.     }
  212.  
  213.     private void onRoomLogout(String str) {
  214.         split = str.split("#");
  215.         user = split[2];
  216.         System.out.println("[" + user + " left channel " + channel + " (logout)]");
  217.        
  218.         if(idleListContains(user)) {
  219.             removeIdleUser(user);
  220.         }
  221.     }
  222.  
  223.     private void onChannelTalk(String str) {
  224.        
  225.         split = str.split("#");
  226.         user = split[1];
  227.         text = extract(str);
  228.        
  229.         System.out.println(user + ": " + text);
  230.        
  231.         if(!idleListContains(user)) {
  232.             addIdleUser(user);
  233.         }
  234.        
  235.         else if(idleListContains(user)) {
  236.             resetIdleUser(user);
  237.         }
  238.        
  239.         // load command
  240.         if(text.equals("!server") && !isIgnored(user)) {
  241.             send("s");
  242.             new TimerThread(this, user);
  243.         }
  244.        
  245.         else if(text.startsWith("!join ") && user.equalsIgnoreCase(admin)) {
  246.             String newChan = text.substring(6, text.length());
  247.             send("d" + channel);
  248.             channel =  newChan;
  249.             idleList.clear();
  250.             join(channel);
  251.         }
  252.        
  253.        
  254.         // admin command
  255.         else if(text.equals("!admin") && !isIgnored(user)) {
  256.             channelTalk("My admin is: " + admin + " - http://www.spinchat.com/hp/" + admin + "/");
  257.             new TimerThread(this, user);
  258.         }
  259.        
  260.         // info
  261.         else if(text.equals("!info") && !isIgnored(user)) {
  262.             channelTalk("Spinner: Spinchat utility bot. Coded by " + admin + ". Available Commands: !server, !idle <user> !admin, !join <room>, !info, !quit, !status <user>. I have a [10] second command timer.");
  263.             new TimerThread(this, user);
  264.         }
  265.        
  266.         else if(text.startsWith("!idle _") && !isIgnored(user)) {
  267.             String userIdle = text.substring(7, text.length());
  268.             String idleTime = getIdleTime(userIdle);
  269.            
  270.             if(idleTime != null) {
  271.                 channelTalk(idleTime);
  272.                 new TimerThread(this, user);
  273.             }
  274.            
  275.             else { send("h" + user + "#0a#a#I have no idletime for " + userIdle+"."); }
  276.         }
  277.        
  278.         else if(text.equals("!list") && user.equalsIgnoreCase(admin)) {
  279.             send("h" + user + "#0#e#" + idleList);
  280.         }
  281.        
  282.         // quit bot
  283.         else if(text.equals("!quit") && user.equalsIgnoreCase(admin)) {
  284.             channelTalk("Goodbye.");
  285.             send("e");
  286.             System.exit(0);
  287.         }
  288.        
  289.         // check if user is online
  290.         else if(text.startsWith("!status ") && !isIgnored(user)) {
  291.             String userCheck = text.substring(8, text.length());
  292.             send("k" + userCheck);
  293.             new TimerThread(this, user);
  294.         }
  295.        
  296.         else if(text.contains("https://www.youtube.com/watch?v=") || text.contains("https://youtu.be/") && !isIgnored(user)) {
  297.             split = text.split(" ");
  298.             for(int i = 0; i < split.length; i++) {
  299.                 if(split[i].startsWith("https://www.youtube.com/watch?v=") || split[i].startsWith("https://youtu.be/")) {
  300.                     String retTitle = getTitle(split[i]);
  301.                     if(!retTitle.equals("#")) {
  302.                         channelTalk("Youtube: " + retTitle.substring(0, retTitle.length() - 10));
  303.                         new TimerThread(this, user);
  304.                         break;
  305.                     }
  306.                 }
  307.             }
  308.         }
  309.     }
  310.    
  311.     private String getTitle(String urlStr) {
  312.         Pattern p=null;
  313.         Matcher m=null;
  314.         URL url=null;
  315.         String title = null;
  316.         String html=null;
  317.         StringBuffer buf = new StringBuffer();
  318.        
  319.         try {
  320.         url = new URL(urlStr);
  321.         BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  322.      while ((html= in.readLine()) != null) {
  323.          buf.append(html);
  324.      }
  325.             html = buf.toString();
  326.             html = html.replaceAll("\\s+", " ");
  327.             p = Pattern.compile("<title>(.*?)</title>");
  328.             m = p.matcher(html);
  329.             while (m.find() == true) {
  330.               title = m.group(1);
  331.             }
  332.         }
  333.            catch(Exception e) {
  334.                return "#";
  335.            }
  336.        
  337.         title = StringEscapeUtils.unescapeHtml3(title);
  338.         if(title == null) return "#";
  339.         else if(title.length() > 450) { return "#"; }
  340.         else return title;
  341.     }
  342.  
  343.     private void onPrivateRecv(String str) {
  344.        
  345.     }
  346.  
  347.     private void onUserlistRecv(String str) {
  348.        
  349.     }
  350.  
  351.     private void onUserInfoRecv(String str) {
  352.         split = str.split("#");
  353.         String userInfoStr = str.substring(1, split[0].length());
  354.         channelTalk("User '" + userInfoStr + "' is now online.");
  355.     }
  356.  
  357.     private void onServerLoadRecv(String str) {
  358.        
  359.         String usersOnline = str.substring(1, str.length());
  360.         channelTalk("Currently " + usersOnline + " people connected to the server.");
  361.     }
  362.  
  363.     private void onUserOfflineRecv(String str) {
  364.         String userOffline = str.substring(1, str.length());
  365.         channelTalk("User '" + userOffline + "' is offline.");
  366.     }
  367.    
  368.     private String extract(String s) {
  369.         split = s.split("#");
  370.         int len = split[0].length() + split[1].length() + split[2].length() + split[3].length() + 4;
  371.         return s.substring(len, s.length());
  372.     }
  373.    
  374.     public void removeUser(String u) {
  375.         u = u.toLowerCase();
  376.         if(ignoreList.contains(u)) {
  377.             ignoreList.remove(u);
  378.         }
  379.     }
  380.    
  381.     public void addUser(String u) {
  382.         u = u.toLowerCase();
  383.         if(!ignoreList.contains(u)) {
  384.             ignoreList.add(u);
  385.         }
  386.     }
  387.    
  388.     public boolean isIgnored(String u) {
  389.         u = u.toLowerCase();
  390.         if(ignoreList.contains(u)) return true;
  391.         else return false;
  392.     }
  393.    
  394.     public String getTimeStamp() {
  395.         long s = System.currentTimeMillis() / 1000L;
  396.         return Long.toString(s);
  397.     }
  398.    
  399.    
  400.    
  401.     // for idling..
  402.     public String getIdleTime(String u) {
  403.         u = u.toLowerCase();
  404.         for(int i = 0; i < idleList.size(); i++) {
  405.                 if(idleList.get(i).contains(u)) {
  406.                     String str = idleList.get(i);
  407.                     String a[] = str.split("#");
  408.                     long setTime = Long.parseLong(a[1]);
  409.                     long currentTime = System.currentTimeMillis() / 1000L;
  410.                     long remaining = currentTime - setTime;
  411.                    
  412.                     long hours = remaining / 3600;
  413.                     long minutes = (remaining % 3600) / 60;
  414.                     long seconds = remaining % 60;
  415.                     if(hours == 0 && minutes == 0) {
  416.                         return String.format("The above user has been idle (in this room) for %2d secs.", seconds);
  417.                     }
  418.                    
  419.                     else if(hours == 0 && minutes > 0) {
  420.                         return String.format("The above user has been idle (in this room) for %2d mins and %2d secs.", minutes, seconds);
  421.                     }
  422.  
  423.                     return String.format("The above user has been idle (in this room) for %2d hrs, %2d mins and %2d secs.", hours, minutes, seconds);
  424.                 }
  425.         }
  426.        
  427.         return null;
  428.     }
  429.    
  430.     public boolean idleListContains(String u) {
  431.         u = u.toLowerCase();
  432.         for(int i = 0; i < idleList.size(); i++) {
  433.                 if(idleList.get(i).contains(u)) {
  434.                         return true;
  435.                 }
  436.         }
  437.        
  438.         return false;
  439.     }
  440.    
  441.     public void resetIdleUser(String u) {
  442.         u = u.toLowerCase();
  443.             for(int i = 0; i < idleList.size(); i++) {
  444.                 if(idleList.get(i).contains(u)) {
  445.                     idleList.remove(i);
  446.                     break;
  447.                 }
  448.             }
  449.            
  450.             String ctime = u + "#" + getTimeStamp();
  451.             idleList.add(ctime);
  452.     }
  453.    
  454.     public void addIdleUser(String u) {
  455.             u = u.toLowerCase();
  456.             String ctime = u + "#" + getTimeStamp();
  457.             idleList.add(ctime);
  458.     }
  459.    
  460.     public void removeIdleUser(String u) {
  461.         u = u.toLowerCase();
  462.         for(int i = 0; i < idleList.size(); i++) {
  463.                 if(idleList.get(i).contains(u)) {
  464.                     idleList.remove(idleList.get(i));
  465.                     break;
  466.                 }
  467.         }
  468.     }
  469.    
  470.    
  471.  
  472.     // connect to spin
  473.     private void connect() {
  474.        
  475.         try {
  476.            
  477.             addr = new InetSocketAddress("127.0.0.1", 9050);
  478.             proxy = new Proxy(Proxy.Type.SOCKS, addr);
  479.             socket = new Socket(proxy);
  480.             dest = new InetSocketAddress("www.spinchat.com", 3001);
  481.             socket.connect(dest);
  482.             writer = new PrintWriter(socket.getOutputStream(), true);
  483.             reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  484.            
  485.             read="";
  486.             while(!read.startsWith("a" + botNick + "#")) {
  487.                 send("BI'm a bot.");
  488.                 send("a" + botNick + "#" + botPass);
  489.                 read = readLine();
  490.                
  491.                 if(read.startsWith("b")) {
  492.                     System.err.println("[X] - Login failed: " + read);
  493.                     System.exit(0);
  494.                 }
  495.             }
  496.         }
  497.         catch(Exception ioe) {
  498.             ioe.printStackTrace();
  499.             reConnect();
  500.         }
  501.        
  502.         idleThread = new IdleThread(this);
  503.     }
  504.    
  505.     // reconnect to server
  506.     private void reConnect() {
  507.        
  508.         try { idleThread.setThreadFlag(false); } catch(NullPointerException n) {
  509.            
  510.         }
  511.        
  512.         System.out.println("[X] - Lost connection.");
  513.         System.out.println("[X] - Reconnecting & joining channel (" + channel + ")");
  514.         pauseBot(5000);
  515.         connect();
  516.         idleList.clear();
  517.         join(channel);
  518.     }
  519.    
  520.     // join room
  521.     private void join(String c) {
  522.         send("c" + c);
  523.     }
  524.    
  525.     private void opUser(String u) {
  526.         send("|" + channel + "#a#" + u + "#");
  527.     }
  528.    
  529.     private void deopUser(String u) {
  530.         send("|" + channel + "#A#" + u + "#");
  531.     }
  532.    
  533.     private void kickBan(String u) {
  534.         send("|" + channel + "#C#" + u + "#");
  535.     }
  536.    
  537.     private void lockRoom() {
  538.         send("t" + channel + "#d#");
  539.     }
  540.    
  541.     private void unlockRoom() {
  542.         send("t" + channel + "#D#");
  543.     }
  544.    
  545.     private void unban(String u) {
  546.         send("|" + channel + "#d#" + u + "#");
  547.     }
  548.    
  549.     private void unmute(String u) {
  550.         send("|" + channel + "#B#" + u + "#");
  551.     }
  552.    
  553.     private void mute(String u) {
  554.         send("|" + channel + "#b#" + u + "#");
  555.     }
  556.    
  557.     private void warnUser(String u) {
  558.         send("g" + channel + "#0#warn#" + u + "#Stop.");
  559.     }
  560.    
  561.     // send data
  562.     public void send(String str) {
  563.         try { writer.println(str); }
  564.         catch(Exception ioe) {
  565.             reConnect();
  566.         }
  567.     }
  568.    
  569.     // pause
  570.     private void pauseBot(int t) {
  571.         try { Thread.sleep(t); } catch(InterruptedException ie) {
  572.             new Spinner(botNick, botPass, channel, admin);
  573.         }
  574.     }
  575.    
  576.     private String readLine() {
  577.         String data="";
  578.         try { data = reader.readLine(); } catch(Exception readline) {
  579.             readline.printStackTrace();
  580.             reConnect();
  581.         }
  582.        
  583.         return data;
  584.     }
  585.    
  586.     private Socket socket=null;
  587.     private PrintWriter writer=null;
  588.     private BufferedReader reader=null;
  589.     private InetSocketAddress dest = null;
  590.     private Proxy proxy = null;
  591.     private SocketAddress addr = null;
  592.     private String botNick=null;
  593.     private String botPass=null;
  594.     private String channel=null;
  595.     private String read=null;
  596.     private String user = null;
  597.     private String text = null;
  598.     private String chanEvent = null;
  599.     private String admin = null;
  600.     private String split[];
  601.     private IdleThread idleThread = null;
  602.     private List<String> ignoreList = null;
  603.     private List<String> idleList = null;
  604. }
  605.  
  606.  
  607. //IdleThread.java (prevent idling)
  608.  
  609. class IdleThread extends Thread {
  610.    
  611.     public IdleThread(Spinner s) {
  612.         spinner = s;
  613.         start();
  614.     }
  615.    
  616.     public void run() {
  617.         System.out.println("[X] - IdleThread started.");
  618.         while(flag) {
  619.             spinner.send("Jp");
  620.             try { sleep(35000); } catch(InterruptedException e) {
  621.                 e.printStackTrace();
  622.                 flag = false;
  623.             }
  624.         }
  625.     }
  626.    
  627.     public void setThreadFlag(boolean f) {
  628.         flag = f;
  629.     }
  630.    
  631.     private Spinner spinner = null;
  632.     private boolean flag = true;
  633. }
  634.  
  635.  
  636. //TimerThread.java (command abuse prevention)
  637.  
  638. class TimerThread extends Thread {
  639.        
  640.     public TimerThread(Spinner s, String u) {
  641.         spinner = s;
  642.         user = u;
  643.         start();
  644.     }
  645.    
  646.     public void run() {
  647.         spinner.addUser(user);
  648.         System.out.println("[X] - Timer STARTED for user " + user + " (" + timer + " seconds.)");
  649.        
  650.         while(timer != 0) {
  651.             try { sleep(1000); } catch(InterruptedException ie) { }
  652.             timer--;
  653.         }
  654.        
  655.         spinner.removeUser(user);
  656.         System.out.println("[X] - Timer EXPIRED for user " + user);
  657.     }
  658.    
  659.     private Spinner spinner = null;
  660.     private String user = null;
  661.     private int timer = 10;
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement