Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Spinner.java (Main class)
- package spinner;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.InetSocketAddress;
- import java.net.Proxy;
- import java.net.Socket;
- import java.net.SocketAddress;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.apache.commons.lang3.StringEscapeUtils;
- public class Spinner {
- public static void main(String[] args) {
- if(args.length != 4) {
- System.err.println("usage: java Spinner <BotNick> <BotPass> <Room> <Admin>");
- System.exit(0);
- }
- new Spinner(args[0], args[1], args[2], args[3]);
- }
- public Spinner(String n, String p, String c, String a) {
- botNick = n;
- botPass = p;
- channel = c;
- admin = a;
- ignoreList = new ArrayList<String>();
- System.out.println("---------------------");
- System.out.println("Using Botnick: \""+botNick+"\"");
- System.out.println("Using Password: \""+botPass+"\"");
- System.out.println("With Channel: \""+channel+"\"");
- System.out.println("My Admin: \""+admin+"\"");
- System.out.println("---------------------");
- connect();
- join(channel);
- while(socket.isConnected()) {
- read = readLine();
- parse(read);
- }
- // reconnect if socket broken
- reConnect();
- }
- // parse spin data
- private void parse(String str) {
- char c = str.charAt(0);
- switch(c) {
- case 'e':
- break;
- case '+':
- onChannelActivity(str);
- break;
- case 'g':
- onChannelTalk(str);
- break;
- case 'h':
- onPrivateRecv(str);
- break;
- case 'j':
- onUserlistRecv(str);
- break;
- case 'J':
- break;
- case 'k':
- onUserInfoRecv(str);
- break;
- case 's':
- onServerLoadRecv(str);
- break;
- case 'x':
- onUserOfflineRecv(str);
- break;
- case '|':
- onRoomOp(str);
- break;
- default:
- System.out.println("[SPIN]: " + str);
- }
- }
- private void channelTalk(String msg) {
- send("g" + channel + "#a#" + msg);
- }
- private void onChannelActivity(String str) {
- split = str.split("#");
- chanEvent = split[1];
- user = split[2];
- switch(chanEvent) {
- case "a":
- // room join
- onRoomJoin(str);
- break;
- case "B":
- // room kick
- onRoomKick(str);
- break;
- case "A":
- // room part
- onRoomPart(str);
- break;
- case "L":
- // room idle-out
- onRoomIdleKick(str);
- break;
- case "Q":
- // room logout
- onRoomLogout(str);
- break;
- case "R":
- // room logout
- onRoomLogout(str);
- break;
- default:
- System.out.println("[X] - Received unknown channel event: " + chanEvent);
- return;
- }
- }
- private void onRoomOp(String str) {
- // |Channel#a#LordIkon#Nope.#
- split = str.split("#");
- String mode = split[1];
- if(mode.equals("a")) {
- System.out.println("[" + split[2] + " was opped by " + split[3] + "]");
- }
- else if(mode.equals("A")) {
- System.out.println("[" + split[2] + " was deopped by " + split[3] + "]");
- }
- }
- private void onRoomJoin(String str) {
- split = str.split("#");
- user = split[2];
- System.out.println("[" + user + " joined channel " + channel + "]");
- }
- private void onRoomKick(String str) {
- split = str.split("#");
- user = split[2];
- System.out.println("[" + user + " was kicked from channel " + channel + "]");
- }
- private void onRoomPart(String str) {
- split = str.split("#");
- user = split[2];
- System.out.println("[" + user + " left channel " + channel + "]");
- }
- private void onRoomIdleKick(String str) {
- split = str.split("#");
- user = split[2];
- System.out.println("[" + user + " left channel " + channel + " (idled)]");
- if(user.equals(botNick)) {
- send("c" + channel);
- }
- }
- private void onRoomLogout(String str) {
- split = str.split("#");
- user = split[2];
- System.out.println("[" + user + " left channel " + channel + " (logout)]");
- }
- private void onChannelTalk(String str) {
- split = str.split("#");
- user = split[1];
- text = extract(str);
- System.out.println(user + ": " + text);
- // load command
- if(text.equals("!server") && !isIgnored(user)) {
- send("s");
- new TimerThread(this, user);
- }
- else if(text.startsWith("!join ") && user.equalsIgnoreCase(admin)) {
- String newChan = text.substring(6, text.length());
- send("d" + channel);
- channel = newChan;
- join(channel);
- }
- // admin command
- else if(text.equals("!admin") && !isIgnored(user)) {
- channelTalk("My admin is: " + admin + " - http://www.spinchat.com/hp/" + admin + "/");
- new TimerThread(this, user);
- }
- // info
- else if(text.equals("!info") && !isIgnored(user)) {
- 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.");
- new TimerThread(this, user);
- }
- // quit bot
- else if(text.equals("!quit") && user.equalsIgnoreCase(admin)) {
- channelTalk("Goodbye.");
- send("e");
- System.exit(0);
- }
- // check if user is online
- else if(text.startsWith("!status ") && !isIgnored(user)) {
- String userCheck = text.substring(8, text.length());
- send("k" + userCheck);
- new TimerThread(this, user);
- }
- else if(text.contains("https://www.youtube.com/watch?v=") || text.contains("https://youtu.be/") && !isIgnored(user)) {
- split = text.split(" ");
- for(int i = 0; i < split.length; i++) {
- if(split[i].startsWith("https://www.youtube.com/watch?v=") || split[i].startsWith("https://youtu.be/")) {
- String retTitle = getTitle(split[i]);
- if(!retTitle.equals("#")) {
- channelTalk("Youtube - " + retTitle.substring(0, retTitle.length() - 10));
- new TimerThread(this, user);
- break;
- }
- }
- }
- }
- }
- private String getTitle(String urlStr) {
- Pattern p=null;
- Matcher m=null;
- URL url=null;
- String title = null;
- String html=null;
- StringBuffer buf = new StringBuffer();
- try {
- url = new URL(urlStr);
- BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
- while ((html= in.readLine()) != null) {
- buf.append(html);
- }
- in.close();
- html = buf.toString();
- html = html.replaceAll("\\s+", " ");
- p = Pattern.compile("<title>(.*?)</title>");
- m = p.matcher(html);
- while (m.find() == true) {
- title = m.group(1);
- }
- }
- catch(Exception e) {
- return "#";
- }
- title = StringEscapeUtils.unescapeHtml3(title);
- if(title == null) return "#";
- else if(title.length() > 450) { return "#"; }
- else return title;
- }
- private void onPrivateRecv(String str) {
- }
- private void onUserlistRecv(String str) {
- }
- private void onUserInfoRecv(String str) {
- split = str.split("#");
- String userInfoStr = str.substring(1, split[0].length());
- channelTalk("User '" + userInfoStr + "' is now online.");
- }
- private void onServerLoadRecv(String str) {
- String usersOnline = str.substring(1, str.length());
- channelTalk("Currently " + usersOnline + " people connected to the server.");
- }
- private void onUserOfflineRecv(String str) {
- String userOffline = str.substring(1, str.length());
- channelTalk("User '" + userOffline + "' is offline.");
- }
- private String extract(String s) {
- split = s.split("#");
- int len = split[0].length() + split[1].length() + split[2].length() + split[3].length() + 4;
- return s.substring(len, s.length());
- }
- public void removeUser(String u) {
- u = u.toLowerCase();
- if(ignoreList.contains(u)) {
- ignoreList.remove(u);
- }
- }
- public void addUser(String u) {
- u = u.toLowerCase();
- if(!ignoreList.contains(u)) {
- ignoreList.add(u);
- }
- }
- public boolean isIgnored(String u) {
- u = u.toLowerCase();
- if(ignoreList.contains(u)) return true;
- else return false;
- }
- // connect to spin
- private void connect() {
- try {
- addr = new InetSocketAddress("127.0.0.1", 9050);
- proxy = new Proxy(Proxy.Type.SOCKS, addr);
- socket = new Socket(proxy);
- dest = new InetSocketAddress("www.spinchat.com", 3001);
- socket.connect(dest);
- writer = new PrintWriter(socket.getOutputStream(), true);
- reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- read="";
- while(!read.startsWith("a" + botNick + "#")) {
- send("BI'm a bot.");
- send("a" + botNick + "#" + botPass);
- read = readLine();
- if(read.startsWith("b")) {
- System.err.println("[X] - Login failed: " + read);
- System.exit(0);
- }
- }
- }
- catch(Exception ioe) {
- ioe.printStackTrace();
- reConnect();
- }
- idleThread = new IdleThread(this);
- }
- // reconnect to server
- private void reConnect() {
- try { idleThread.setThreadFlag(false); } catch(NullPointerException n) {
- }
- System.out.println("[X] - Lost connection.");
- System.out.println("[X] - Reconnecting & joining channel (" + channel + ")");
- pauseBot(5000);
- connect();
- join(channel);
- }
- // join room
- private void join(String c) {
- send("c" + c);
- }
- private void opUser(String u) {
- send("|" + channel + "#a#" + u + "#");
- }
- private void deopUser(String u) {
- send("|" + channel + "#A#" + u + "#");
- }
- private void kickBan(String u) {
- send("|" + channel + "#C#" + u + "#");
- }
- private void lockRoom() {
- send("t" + channel + "#d#");
- }
- private void unlockRoom() {
- send("t" + channel + "#D#");
- }
- private void unban(String u) {
- send("|" + channel + "#d#" + u + "#");
- }
- private void unmute(String u) {
- send("|" + channel + "#B#" + u + "#");
- }
- private void mute(String u) {
- send("|" + channel + "#b#" + u + "#");
- }
- private void warnUser(String u) {
- send("g" + channel + "#0#warn#" + u + "#Stop.");
- }
- // send data
- public void send(String str) {
- try { writer.println(str); }
- catch(Exception ioe) {
- reConnect();
- }
- }
- // pause
- private void pauseBot(int t) {
- try { Thread.sleep(t); } catch(InterruptedException ie) {
- new Spinner(botNick, botPass, channel, admin);
- }
- }
- private String readLine() {
- String data="";
- try { data = reader.readLine(); } catch(Exception readline) {
- readline.printStackTrace();
- reConnect();
- }
- return data;
- }
- private Socket socket=null;
- private PrintWriter writer=null;
- private BufferedReader reader=null;
- private InetSocketAddress dest = null;
- private Proxy proxy = null;
- private SocketAddress addr = null;
- private String botNick=null;
- private String botPass=null;
- private String channel=null;
- private String read=null;
- private String user = null;
- private String text = null;
- private String chanEvent = null;
- private String admin = null;
- private String split[];
- private IdleThread idleThread = null;
- private List<String> ignoreList = null;
- }
- // IdleThread.java (prevent idling)
- package spinner;
- public class IdleThread extends Thread {
- public IdleThread(Spinner s) {
- spinner = s;
- start();
- }
- public void run() {
- System.out.println("[X] - IdleThread started.");
- while(flag) {
- spinner.send("Jp");
- try { sleep(35000); } catch(InterruptedException e) {
- e.printStackTrace();
- flag = false;
- }
- }
- }
- public void setThreadFlag(boolean f) {
- flag = f;
- }
- private Spinner spinner = null;
- private boolean flag = true;
- }
- // TimerThread.java (command abuse prevention)
- package spinner;
- public class TimerThread extends Thread {
- public TimerThread(Spinner s, String u) {
- spinner = s;
- user = u;
- start();
- }
- public void run() {
- spinner.addUser(user);
- System.out.println("[X] - Timer STARTED for user " + user + " (" + timer + " seconds.)");
- while(timer != 0) {
- try { sleep(1000); } catch(InterruptedException ie) { }
- timer--;
- }
- spinner.removeUser(user);
- System.out.println("[X] - Timer EXPIRED for user " + user);
- }
- private Spinner spinner = null;
- private String user = null;
- private int timer = 10;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement