Advertisement
Guest User

Spinchat bot - source code (unfinished)

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