Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.82 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8. import java.math.BigInteger;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import java.nio.ByteBuffer;
  12. import java.nio.channels.Channels;
  13. import java.nio.channels.ReadableByteChannel;
  14. import java.nio.channels.WritableByteChannel;
  15. import java.nio.charset.Charset;
  16. import java.sql.Timestamp;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.LinkedHashMap;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.Random;
  25. import java.util.concurrent.Executors;
  26. import java.util.concurrent.ScheduledExecutorService;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.concurrent.locks.Lock;
  29. import java.util.concurrent.locks.ReentrantLock;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32. import java.util.*;
  33.  
  34. /**
  35. * @author Jon Rollins Synaptic Studios Jp - 2015
  36. * Chatango Room Client
  37. * Screen name DigitalClay
  38. * Bot name MentalEncryption
  39. */
  40. public class Room {
  41.  
  42.  
  43. /**
  44. * Server, port, and socket
  45. */
  46. String host = "";
  47. private String uid = "";
  48. int port = 5228;
  49. static Socket socket;
  50.  
  51. /**
  52. * Default Room name
  53. */
  54. String roomame = "projectmath";
  55.  
  56.  
  57. /**
  58. * Input and output - for reading and writing to and from server
  59. */
  60. static ByteArrayOutputStream baos;
  61. static DataOutputStream sockOutput;
  62. static WritableByteChannel channel;
  63. BufferedReader inputReader;
  64.  
  65. /**
  66. * Write Lock - One Write or read at a time
  67. */
  68. private final static Lock writeLock = new ReentrantLock();
  69.  
  70.  
  71. /**
  72. * Used for the ping task when bot is idle
  73. */
  74. private final ScheduledExecutorService executorService =
  75. Executors.newSingleThreadScheduledExecutor();
  76.  
  77.  
  78. /**
  79. * default credentals
  80. */
  81. private String user_id = "Ashurei";
  82. private String password = "3236730112aA";
  83.  
  84.  
  85. /**
  86. * used in logging in as anon or with account
  87. * 0 = default , 1 = anon , 2 = account
  88. */
  89. int x = 0;
  90.  
  91. /**
  92. * First Command boolean
  93. */
  94. static boolean firstCommand = true;
  95.  
  96. /**
  97. * Server weights
  98. */
  99. static LinkedHashMap<String,Integer> _chatangoTagserver=new LinkedHashMap<String, Integer>();
  100. private int sv10 = 110;
  101. private int sv12=116;
  102. private int w12=75;
  103. private int sv8=101;
  104. private int sv6=104;
  105. private int sv4=110;
  106. private int sv2=95;
  107.  
  108.  
  109. /**
  110. * Chat room info
  111. */
  112. private static ArrayList<String> mods = new ArrayList<String>();
  113. private static String owner;
  114. private static ArrayList<String> participants = new ArrayList<String>();
  115. private static String count;
  116. static ArrayList<String> banlist = new ArrayList<String>();
  117. private boolean useBG = true;
  118.  
  119.  
  120. /**
  121. * Font face, color, size
  122. * Name color
  123. */
  124. private static String fontFace = "Typewriter";
  125. private static String fontSize = "12";
  126. //private static String fontColor = "33CCFF";
  127. private static String fontColor = "FFFFFF";
  128. //private static String nameColor = "093";
  129. //private static String nameColor = "FF6666";
  130. private static String nameColor = "FFFFFF";
  131.  
  132. /**
  133. * Used when flood ban / warning
  134. */
  135. static boolean quiet = false;
  136.  
  137.  
  138. /**
  139. * Default constructor ***unused***
  140. * @throws UnknownHostException
  141. * @throws IOException
  142. */
  143. public Room() throws UnknownHostException, IOException {
  144. connect();
  145. }
  146.  
  147.  
  148. /**
  149. * Constructor to login as anon
  150. * @param roomS
  151. * @throws UnknownHostException
  152. * @throws IOException
  153. */
  154. public Room(String roomS) throws UnknownHostException, IOException{
  155. roomame = roomS;
  156. x = 1;
  157. connect();
  158.  
  159.  
  160. }
  161.  
  162.  
  163. /**
  164. * Constructor to login with account
  165. * @param roomS
  166. * @param userS
  167. * @param passS
  168. * @throws UnknownHostException
  169. * @throws IOException
  170. */
  171. public Room(String roomS, String userS, String passS) throws UnknownHostException, IOException{
  172. roomame = roomS;
  173. user_id = userS;
  174. password = passS;
  175. x = 2;
  176. connect();
  177. }
  178.  
  179. /**
  180. * Connect to chatroom
  181. * @throws UnknownHostException
  182. * @throws IOException
  183. */
  184. private void connect() throws UnknownHostException, IOException{
  185.  
  186. //Tag server weights//
  187. _chatangoTagserver.put("5", w12);
  188. _chatangoTagserver.put("6", w12);
  189. _chatangoTagserver.put("7", w12);
  190. _chatangoTagserver.put("8", w12);
  191. _chatangoTagserver.put("16", w12);
  192. _chatangoTagserver.put("17", w12);
  193. _chatangoTagserver.put("18", w12);
  194. _chatangoTagserver.put("9", sv2);
  195. _chatangoTagserver.put("11", sv2);
  196. _chatangoTagserver.put("12", sv2);
  197. _chatangoTagserver.put("13", sv2);
  198. _chatangoTagserver.put("14", sv2);
  199. _chatangoTagserver.put("15", sv2);
  200. _chatangoTagserver.put("19", sv4);
  201. _chatangoTagserver.put("23", sv4);
  202. _chatangoTagserver.put("24", sv4);
  203. _chatangoTagserver.put("25", sv4);
  204. _chatangoTagserver.put("26", sv4);
  205. _chatangoTagserver.put("28", sv6);
  206. _chatangoTagserver.put("29", sv6);
  207. _chatangoTagserver.put("30", sv6);
  208. _chatangoTagserver.put("31", sv6);
  209. _chatangoTagserver.put("32", sv6);
  210. _chatangoTagserver.put("33", sv6);
  211. _chatangoTagserver.put("35", sv8);
  212. _chatangoTagserver.put("36", sv8);
  213. _chatangoTagserver.put("37", sv8);
  214. _chatangoTagserver.put("38", sv8);
  215. _chatangoTagserver.put("39", sv8);
  216. _chatangoTagserver.put("40", sv8);
  217. _chatangoTagserver.put("41", sv8);
  218. _chatangoTagserver.put("42", sv8);
  219. _chatangoTagserver.put("43", sv8);
  220. _chatangoTagserver.put("44", sv8);
  221. _chatangoTagserver.put("45", sv8);
  222. _chatangoTagserver.put("46", sv8);
  223. _chatangoTagserver.put("47", sv8);
  224. _chatangoTagserver.put("48", sv8);
  225. _chatangoTagserver.put("49", sv8);
  226. _chatangoTagserver.put("50", sv8);
  227. _chatangoTagserver.put("52", sv10);
  228. _chatangoTagserver.put("53", sv10);
  229. _chatangoTagserver.put("55", sv10);
  230. _chatangoTagserver.put("57", sv10);
  231. _chatangoTagserver.put("58", sv10);
  232. _chatangoTagserver.put("59", sv10);
  233. _chatangoTagserver.put("60", sv10);
  234. _chatangoTagserver.put("61", sv10);
  235. _chatangoTagserver.put("62", sv10);
  236. _chatangoTagserver.put("63", sv10);
  237. _chatangoTagserver.put("64", sv10);
  238. _chatangoTagserver.put("65", sv10);
  239. _chatangoTagserver.put("66", sv10);
  240. _chatangoTagserver.put("68", sv2);
  241. _chatangoTagserver.put("71", sv12);
  242. _chatangoTagserver.put("72", sv12);
  243. _chatangoTagserver.put("73", sv12);
  244. _chatangoTagserver.put("74", sv12);
  245. _chatangoTagserver.put("75", sv12);
  246. _chatangoTagserver.put("76", sv12);
  247. _chatangoTagserver.put("77", sv12);
  248. _chatangoTagserver.put("78", sv12);
  249. _chatangoTagserver.put("79", sv12);
  250. _chatangoTagserver.put("80", sv12);
  251. _chatangoTagserver.put("81", sv12);
  252. _chatangoTagserver.put("82", sv12);
  253. _chatangoTagserver.put("83", sv12);
  254. _chatangoTagserver.put("84", sv12);
  255.  
  256. System.out.println("[Room]Tagserver Weights : "+_chatangoTagserver);
  257.  
  258.  
  259. //user id//
  260. uid = generateUID();
  261.  
  262. //socket//
  263. host = "s"+getServerId(roomame)+".chatango.com";
  264. System.out.println("[Room] Host : "+host);
  265. Bot.print("[Room] Host : "+host);
  266. socket = createSocket(host, port);
  267.  
  268. //start reading loop - response from server//
  269. readResponse();
  270.  
  271. //Start login//
  272. String loginStr = "";
  273.  
  274. //Login as anon//
  275. if(x == 1){
  276. loginStr = "bauth:"+roomame+":"+ uid;
  277. }
  278. //login with account//
  279. if(x == 2){
  280. loginStr = "bauth:"+roomame+":"+ uid+":"+ user_id+":"+ password;
  281. }
  282.  
  283. //default *unused*//
  284. if(x == 0){
  285. loginStr = "bauth:"+roomame+":"+ uid+":"+ user_id+":"+ password;
  286. }
  287.  
  288. //Send login command//
  289. sendLoginCommand(loginStr);
  290.  
  291. //Start idle ping//
  292. startIdlePing();
  293.  
  294. //Delay before posting msg to chatroom//
  295. try {
  296. TimeUnit.SECONDS.sleep(1);
  297. } catch (InterruptedException e) {
  298. e.printStackTrace();
  299. }
  300.  
  301. //Post login msg to chatroom//
  302. //sendMsg("Hello, I've logged in successfully. ");
  303.  
  304. }
  305.  
  306.  
  307. /**
  308. * Read response from server - Loop
  309. * @throws IOException
  310. */
  311. private void readResponse() throws IOException {
  312. //Read Loop Thread//
  313. Thread readThread = new Thread(new Runnable() {
  314. @Override
  315. public void run() {
  316. try {
  317. //Innput reader and channel/
  318. DataInputStream sockInput = new DataInputStream(socket.getInputStream());
  319. ReadableByteChannel readChannel = Channels.newChannel(sockInput);
  320.  
  321. while (true) {
  322. // see if any message has been received//
  323. ByteBuffer bufferA = ByteBuffer.allocate(10000000);
  324.  
  325. @SuppressWarnings("unused")
  326. int count = 0;
  327.  
  328. //empty message//
  329. String message = "";
  330.  
  331. //Read bytes//
  332. while ((count = readChannel.read(bufferA)) > 0) {
  333. // flip the byte buffer to start reading//
  334. bufferA.flip();
  335. //decode bytes and add each char-construct message//
  336. message += Charset.defaultCharset().decode(bufferA);
  337.  
  338. }
  339. //check if message had data//
  340. if (message.length() > 0 && !message.trim().isEmpty()) {
  341.  
  342. ///////////////////////////////////////
  343. //SERVER COMMANDS/////////////////////
  344.  
  345. //on login//
  346. if(message.trim().toLowerCase().startsWith("ok:")){
  347. onConnectRcv(message);
  348. }
  349.  
  350. //on login fail//
  351. if(message.trim().toLowerCase().startsWith("denied:")){
  352. onConnectFailRcv();
  353. }
  354.  
  355. //On init//
  356. if(message.trim().toLowerCase().contains("inited")){
  357. onInitRcv(message);
  358. }
  359.  
  360. //message//
  361. if(message.trim().toLowerCase().startsWith("b:")){
  362. String clean = cleanMSG(message);
  363. String sender = getSender(message);
  364. Bot.print("[Room message] "+sender+" : "+clean);
  365. System.out.println("[Room message] "+sender+" : "+clean);
  366.  
  367.  
  368.  
  369. String flooder = Bot.flooderName.getText().toString();
  370. String counterMsg = Bot.counterMsg.getText().toString();
  371. //boolean contains = s.matches(".*\\http\\b.*");
  372.  
  373. //if(sender.equalsIgnoreCase("ChainSawLipStick")||sender.equalsIgnoreCase("TrainToForever")||sender.equalsIgnoreCase(flooder)){
  374. // if(sender.equalsIgnoreCase("ChainSawLipStick")||sender.equalsIgnoreCase("TrainToForever")||sender.equals(null)||sender.equals("")||sender.equalsIgnoreCase(flooder)){
  375.  
  376.  
  377.  
  378.  
  379. try{
  380.  
  381.  
  382. TimeUnit.SECONDS.sleep(3);
  383. Room.sendMsg(counterMsg);
  384.  
  385.  
  386. //Room.sendMsg(proper_noun[index]);
  387.  
  388.  
  389.  
  390. // Room.sendMsg( "Makaiinsultong salita tulad ng bobo, pwede raw ikakulong ng netizen sa ilalim ng online libel http://www.gmanetwork.com/news/story/349217/news/ulatfilipino/makaiinsultong-salita-tulad-ng-bobo-pwede-raw-ikakulong-ng-netizen-sa-ilalim-ng-online-libel");
  391. String flood = "Oppa Gangnam Style \r\r Gangnam Style \r\r Najelu dasun ono igajagi yoja \r\r Kopi hanjanui yeoyureul aneun pumgyeok inneun yeoja \r\r Bami om ya shimjangi bdeugeowojineun yeoja \r\r Geureon banjeon inneun yeoja \r\r Naneun zono hai \r\r Najeneun loma kuftasano oncho zono hao \r\r Keopi sikgido jeone woyshyat ttaerineun zono hai \r\r Bami omyeon shimjangi oeojyeobeorineun zono hai \r\r oncho zono hai \r\r Areumdawo sarangseureowo \r\r Geurae neo hey geurae baro neo hey \r\r Areumdawo sarangseureowo \r\r Geurae neo hey geurae baro neo hey \r\r Jigeumbuteo gal dekkaji gabolkka \r\r Oppa Gangnam Style \r\r Gangnam Style \r\r Op, op, op, op \r\r Oppa Gangnam Style \r\r Gangnam Style \r\r Op, op, op, o \r\r Oppa Gangnam Style \r\r Eh, sexy lady \r\r Op, op, op, op \r\r Oppa Gangnam Style \r\r Eh, sexy lady \r\r Op, op, op, op \r\r Eh-eh-eh, eh-eh-eh \r\r Jeongsukhae boijiman nol ttaen noneun yeoja \r\r Ittaeda sipeumyeon mukkeotdeon meori puneun yeoja \r\r Garyeotjiman wenmanhan nochulboda yahan yeoja \r\r Geureon gamgakjeogin yeoja \r\r Naneun sanai \r\r Jeomjanha boijiman nol ttaen noneun sanai \r\r Ttaega doemyeon wanjeon michyeobeorineun sanai \r\r Geunyukboda sasangi ultungbultunghan sanai \r\r Geureon sanai \r\r Areumdawo sarangseureowo \r\r Geurae neo, hey, geurae baro neo, hey \r\r Areumdawo sarangseureowo \r\r Geurae baro neo, hey \r\r Jigeumbuteo gal dekkaji gabolkka \r\r Oppa Gangnam Style \r\r Gangnam Style \r\r Op, op, op, op \r\r Oppa Gangnam Style \r\r Gangnam Style \r\r Op, op, op, op \r\r Oppa Gangnam Style \r\r Eh, sexy lady \r\r Op, op, op, op \r\r Oppa Gangnam Style \r\r Eh, sexy lady \r\r Op, op, op, op \r\r Eh-eh-eh, eh-eh-eh \r\r Read more: PSY - Gangnam Style Lyrics | MetroLyrics";
  392. // Room.sendMsg(randomString);
  393. Room.sendMsg(flood);
  394.  
  395. }catch(Exception e){
  396. e.printStackTrace();
  397. }
  398.  
  399.  
  400.  
  401. checkCmd(clean,sender);
  402. }
  403. //group count
  404. if(message.trim().toLowerCase().startsWith("n:")){
  405. onCountRcv(message);
  406. }
  407. //mod names
  408. if(message.trim().toLowerCase().startsWith("mods:")){
  409. onModsRcv(message);
  410. }
  411. //on leave
  412. if(message.trim().toLowerCase().startsWith("participant:0")){
  413. onLeaveRcv(message);
  414. }
  415. //on join
  416. if(message.trim().toLowerCase().startsWith("participant:1")){
  417. onJoinRcv(message);
  418. }
  419. //group names
  420. if(message.trim().toLowerCase().startsWith("g_participants:")){
  421. onRoomNamesRcv(message);
  422. }
  423. //banned
  424. if(message.trim().toLowerCase().startsWith("blocked:")){
  425. onBanRcv(message);
  426. }
  427. //unbanned
  428. if(message.trim().toLowerCase().startsWith("unblocked:")){
  429. onUnbanRcv(message);
  430. }
  431. //past msgs
  432. if(message.trim().toLowerCase().startsWith("i:")){
  433. onPastMsgRcv(message);
  434. }
  435. //Ban List
  436. if(message.trim().toLowerCase().startsWith("blocklist:")){
  437. onBlocklistRcv(message);
  438. }
  439. //message deleted
  440. if(message.trim().toLowerCase().startsWith("delete:")){
  441. onMsgDeleteRcv(message);
  442. }
  443. //Flood Warning//
  444. if(message.trim().toLowerCase().startsWith("show_fw:")){
  445. onFloodWarningRcv(message);
  446. }
  447. //Flood Ban//
  448. if(message.trim().toLowerCase().startsWith("show_tb:")){
  449. onFloodBanRcv(message);
  450. }
  451. //Flood Ban repeat//
  452. if(message.trim().toLowerCase().startsWith("tb:")){
  453. onFloodBanRepeatRcv(message);
  454. }
  455. //premium//
  456. if(message.trim().toLowerCase().startsWith("premium:")){
  457. onPremiumRcv(message);
  458. }
  459. //on all messages delete//
  460. if(message.trim().toLowerCase().startsWith("deleteall:")){
  461. onAllMsgDeleteRcv(message);
  462. }
  463.  
  464. //System.out.println("[Room] Room debug : "+message);
  465. //Bot.print("[Room] Room debug : "+message);
  466.  
  467. //Reset message//
  468. message = "";
  469.  
  470.  
  471. }
  472. }
  473. } catch (IOException e) {
  474. e.printStackTrace();
  475. }
  476. }
  477.  
  478.  
  479. });
  480. readThread.start();
  481. }
  482.  
  483. ////////////////////////////////////////
  484. //SERVER COMMAND EVENTS////////////////
  485.  
  486.  
  487. /**
  488. * Called When all of a users messages are deleted
  489. * @param message
  490. * @throws IOException
  491. */
  492. protected void onAllMsgDeleteRcv(String message) throws IOException {
  493. System.out.println("[Room] All MSG Deleted : "+message);
  494. Bot.print("[Room] All MSG Deleted : "+message);
  495. //sendMsg("All Clear ^.^");
  496. }
  497.  
  498.  
  499. /**
  500. * On Premium
  501. * @param message
  502. * @throws IOException
  503. */
  504. protected void onPremiumRcv(String message) throws IOException {
  505.  
  506. String bgTime = message.split(":")[2];
  507.  
  508. Timestamp currentTimeMil = new Timestamp(System.currentTimeMillis());
  509.  
  510. long g = Long.parseLong(bgTime.trim());
  511. Timestamp bgEnd = new Timestamp(g*1000);
  512.  
  513. //test if user accounnt has time left on premium//
  514. if(bgEnd.getTime() > currentTimeMil.getTime() &&useBG ){
  515. System.out.println("[Room] Using premium : "+message);
  516. Bot.print("[Room] Using premium : "+message);
  517. setBG();//set bg//
  518. setMedia();//set media//
  519. }else{
  520. System.out.println("[Room] Not using premium : ");
  521. Bot.print("[Room] Not using premium : "+message);
  522. }
  523. }
  524.  
  525. /**
  526. * On Flood Ban Repeat
  527. * @param message
  528. * @throws FileNotFoundException
  529. * @throws UnsupportedEncodingException
  530. */
  531. protected void onFloodBanRepeatRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  532. System.out.println("[Room] Flood Ban Repeat : "+message);
  533. Bot.print("[Room] Flood Ban Repeat : "+message);
  534. if(quiet == false){
  535. quiet = true;
  536. Thread t = new Thread(new Runnable() {
  537. @Override
  538. public void run() {
  539. try {
  540. Thread.sleep(120000);
  541. } catch (InterruptedException e) {
  542. e.printStackTrace();
  543. }finally{
  544. quiet = false;
  545. System.out.println("[Room] Flood Banned : Over ");
  546. try {
  547. Bot.print("[Room] Flood Banned : Over");
  548. } catch (FileNotFoundException e) {
  549. e.printStackTrace();
  550. } catch (UnsupportedEncodingException e) {
  551. e.printStackTrace();
  552. }
  553. }
  554.  
  555. }
  556. });
  557. t.start();
  558. }
  559. }
  560.  
  561. /**
  562. * On Flood ban
  563. * @param message
  564. * @throws FileNotFoundException
  565. * @throws UnsupportedEncodingException
  566. */
  567. protected void onFloodBanRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  568. System.out.println("[Room] Flood Ban : "+message);
  569. Bot.print("[Room] Flood Ban : "+message);
  570. if(quiet == false){
  571. quiet = true;
  572. Thread t = new Thread(new Runnable() {
  573. @Override
  574. public void run() {
  575. try {
  576. Thread.sleep(500000);
  577. } catch (InterruptedException e) {
  578. e.printStackTrace();
  579. }finally{
  580. quiet = false;
  581. System.out.println("[Room] Flood Banned : Over ");
  582. try {
  583. Bot.print("[Room] Flood Banned : Over");
  584. } catch (FileNotFoundException e) {
  585. e.printStackTrace();
  586. } catch (UnsupportedEncodingException e) {
  587. e.printStackTrace();
  588. }
  589. }
  590.  
  591. }
  592. });
  593. t.start();
  594. }
  595. }
  596.  
  597. /**
  598. * On Flood Ban Warning
  599. * @param message
  600. * @throws FileNotFoundException
  601. * @throws UnsupportedEncodingException
  602. */
  603. protected void onFloodWarningRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  604. System.out.println("[Room] Flood Warning : "+message);
  605. Bot.print("[Room] Flood Warning : "+message);
  606. if(quiet == false){
  607. quiet = true;
  608. Thread t = new Thread(new Runnable() {
  609. @Override
  610. public void run() {
  611. try {
  612. Thread.sleep(120000);
  613. } catch (InterruptedException e) {
  614. e.printStackTrace();
  615. }finally{
  616. quiet = false;
  617. System.out.println("[Room] Flood Warning : Over ");
  618. try {
  619. Bot.print("[Room] Flood Warning : Over");
  620. } catch (FileNotFoundException e) {
  621. e.printStackTrace();
  622. } catch (UnsupportedEncodingException e) {
  623. e.printStackTrace();
  624. }
  625. }
  626.  
  627. }
  628. });
  629. t.start();
  630. }
  631. }
  632.  
  633. /**
  634. * On Message delete
  635. * @param message
  636. * @throws FileNotFoundException
  637. * @throws UnsupportedEncodingException
  638. */
  639. protected void onMsgDeleteRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  640. System.out.println("[Room] Message Deleted : "+message);
  641. Bot.print("[Room] Message Deleted : "+message);
  642. }
  643.  
  644. /**
  645. * On User Join
  646. * @param message
  647. * @throws IOException
  648. */
  649. protected void onJoinRcv(String message) throws IOException {
  650. String user = message.split(":")[4];
  651. System.out.println("[Room] Joined : "+user);
  652. Bot.print("[Room] Joined : "+user);
  653. //sendMsg("Welcome back "+user);
  654. participants.add(user);
  655. }
  656.  
  657. /**
  658. * On User Leave
  659. * @param message
  660. * @throws IOException
  661. */
  662. private void onLeaveRcv(String message) throws IOException {
  663. if(message.contains(":")){
  664. String user = message.split(":")[4];
  665. System.out.println("[Room] Left : "+user);
  666. Bot.print("[Room] left : "+user);
  667. //sendMsg("Bye "+user);
  668. participants.remove(user);
  669. }
  670.  
  671. }
  672.  
  673. /**
  674. * On Block List Recive
  675. * @param message
  676. * @throws FileNotFoundException
  677. * @throws UnsupportedEncodingException
  678. */
  679. protected void onBlocklistRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  680. message = message.replace("blocklist:", "");
  681. //check if list is empty//
  682. if(message.contains(";")&&message.contains(":")){
  683. String[] parts = message.split(";");
  684. for(int i = 0;i < parts.length;i++){
  685. banlist.add(parts[i].split(":")[2]);
  686. }
  687. }
  688. System.out.println("[Room] Block List : "+banlist);
  689. Bot.print("[Room] Block List : "+banlist);
  690. }
  691.  
  692. /**
  693. * On Init
  694. * @param message
  695. * @throws IOException
  696. */
  697. protected void onInitRcv(String message) throws IOException {
  698. getPremium();
  699. getRoomUsers();
  700. getBlockList();
  701. }
  702.  
  703. /**
  704. * On Connect Fail
  705. */
  706. protected void onConnectFailRcv() {
  707. System.out.println("[Room] Error : Connect failled");
  708. }
  709.  
  710. /**
  711. * On Login Fail
  712. */
  713. protected void onLoginFail() {
  714. System.out.println("[Room] Error : login failled");
  715. }
  716.  
  717.  
  718. /**
  719. * On Connect
  720. * @param message
  721. * @throws UnsupportedEncodingException
  722. * @throws FileNotFoundException
  723. */
  724. protected void onConnectRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  725. //check if empty//
  726. if(message.contains(":")){
  727. //Get Owner//
  728. owner = message.split(":")[1];
  729. System.out.println("[Room] Owner : "+owner);
  730. Bot.print("[Room] Owner : "+owner);
  731. }
  732. //Get Mods//
  733. if(!message.contains("M:")){
  734. onLoginFail();
  735. return;
  736. }
  737. try{
  738. String[] msgPartsB = message.split("M:");
  739. String[] msgPartsB2 = msgPartsB[1].split(":");
  740. String[] modsFull = msgPartsB2[3].split(";");
  741. for(int m=0; m < modsFull.length;m++){
  742. mods.add(modsFull[m].split(",")[0]);
  743. }
  744. }catch(Exception e){
  745. e.printStackTrace();
  746. }
  747. System.out.println("[Room] Mods : "+mods);
  748. Bot.print("[Room] Mods : "+mods);
  749. }
  750.  
  751. /**
  752. * On Past Messages
  753. * @param message
  754. */
  755. protected void onPastMsgRcv(String message) {
  756. //Currently Unused//
  757. }
  758.  
  759.  
  760. /**
  761. * On Unban
  762. * @param message
  763. * @throws IOException
  764. */
  765. protected void onUnbanRcv(String message) throws IOException {
  766. String banned = message.split(":")[3];
  767. String banner = message.split(":")[4];
  768. banlist.remove(banned);
  769. System.out.println("[Room] Unban : "+banned+" By : "+banner);
  770. Bot.print("[Room] Unban : "+banned+" By : "+banner);
  771. sendMsg("UnBan : "+banned+" __ By :"+banner);
  772. }
  773.  
  774. /**
  775. * On Ban
  776. * @param message
  777. * @throws IOException
  778. */
  779. protected void onBanRcv(String message) throws IOException {
  780. String banned = message.split(":")[3];
  781. String banner = message.split(":")[4];
  782. banlist.add(banned);
  783. System.out.println("[Room] Ban : "+banned+" By : "+banner);
  784. Bot.print("[Room] Ban : "+banned+" By : "+banner);
  785. sendMsg("Banned : "+banned+" __ By :"+banner);
  786. }
  787.  
  788. /**
  789. * A mod has been added or removed
  790. * @param message
  791. * @throws IOException
  792. */
  793. protected void onModsRcv(String message) throws IOException {
  794.  
  795. message = message.replace("mods:", "");
  796. if(!message.contains(":")){
  797. return;
  798. }
  799. String[] parts = message.split(":");
  800.  
  801. ArrayList<String> j = new ArrayList<String>();
  802.  
  803. for(int m=0; m < parts.length;m++){
  804. j.add(parts[m].split(",")[0]);
  805. }
  806. if(mods.size() < j.size()){
  807. mods = j;
  808. sendMsg("A mod has been removed ___ Mods : "+mods);
  809. System.out.println("[Room] Mod Removed : "+mods);
  810. Bot.print("[Room] Mod Removed : "+mods);
  811. }
  812. if(mods.size() > j.size()){
  813. mods= j;
  814. sendMsg("A mod has been added ___ Mods : "+mods);
  815. System.out.println("[Room] Mod added : "+mods);
  816. Bot.print("[Room] Mod added : "+mods);
  817. }
  818.  
  819.  
  820. }
  821.  
  822.  
  823. /**
  824. * Chat room participants
  825. * @param message
  826. * @throws FileNotFoundException
  827. * @throws UnsupportedEncodingException
  828. */
  829. protected void onRoomNamesRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  830. message = message.replace("g_participants:","");
  831.  
  832. String[] parts = message.split(";");
  833.  
  834. for(int i = 0;i<parts.length;i++){
  835. participants.add(parts[i].split(":")[3]);
  836. }
  837. System.out.println("[Room] Participants : "+participants);
  838. Bot.print("[Room] Participants : "+participants);
  839. }
  840.  
  841.  
  842.  
  843. /**
  844. * Chat room count
  845. * @param message
  846. * @throws FileNotFoundException
  847. * @throws UnsupportedEncodingException
  848. */
  849. protected void onCountRcv(String message) throws FileNotFoundException, UnsupportedEncodingException {
  850. count = message.split("n:")[1];
  851. System.out.println("[Room message] Room Count : "+count);
  852. Bot.print("[Room message] Room Count : "+count);
  853. }
  854.  
  855.  
  856. //////////////////////////////
  857. //Bot Commands///////////////
  858.  
  859. /**
  860. * Commands
  861. *
  862. * @param clean
  863. * @param sender
  864. * @throws IOException
  865. */
  866. private void checkCmd(String clean, String sender) throws IOException {
  867.  
  868. //Check if ~ is used and return if not//
  869. if(!clean.startsWith("~")){
  870. return;
  871. }
  872.  
  873. //Trim and lowercase message//
  874. String cleanB = clean.toLowerCase().trim();
  875.  
  876. //Getting cmd and arguments//
  877. String cmd = "";
  878. String args = "";
  879. String msg = cleanB;
  880. if(cleanB.contains(" ")){
  881. String[] msgArray = cleanB.split(" ");
  882. if(msgArray.length > 1){
  883. cmd = msgArray[0].trim();
  884. args = msgArray[1].trim();
  885. }
  886. }else{
  887. cmd = cleanB;
  888. args= "";
  889. }
  890.  
  891.  
  892.  
  893. // end of if-else
  894. // if(cmd.equals("~join") && !args.equals("")){
  895. // Bot.joinRoom(args, user_id, password);
  896. // }
  897. }
  898.  
  899.  
  900. /////////////////////////////
  901. //Message Utilities/////////
  902.  
  903. /**
  904. * Get message sender.
  905. *
  906. * @param message
  907. * @return
  908. */
  909. protected String getSender(String message) {
  910. String[] parts = message.split("b:");
  911. String a = parts[1];
  912. parts = a.split(":");
  913. return parts[1];
  914. }
  915.  
  916. /**
  917. * Clean message
  918. *
  919. * @param in
  920. * @return
  921. */
  922. private String cleanMSG(String in) {
  923. String patt = "<.*?>";
  924. Pattern pattern = Pattern.compile(patt);
  925. Matcher match = pattern.matcher(in);
  926. in = match.replaceAll("");
  927. String[] parts = in.split(":");
  928. String b = parts[parts.length-1];
  929. String patt2 = "¦";
  930. Pattern pattern2 = Pattern.compile(patt2);
  931. Matcher match2 = pattern2.matcher(b);
  932. b = match2.replaceAll("");
  933. String patt3 = "¦";
  934. Pattern pattern3 = Pattern.compile(patt3);
  935. Matcher match3 = pattern3.matcher(b);
  936. b = match3.replaceAll("");
  937. b = b.toLowerCase().trim();
  938. b = b.replace("&lt;", "<");
  939. b = b.replace("&gt;", ">");
  940. b = b.replace("&quot;", "\"");
  941. b = b.replace("&apos;", "'");
  942. b = b.replace("&amp;", "&");
  943. return b;
  944. }
  945.  
  946.  
  947.  
  948. /////////////////////////////////
  949. //Connection Utilities//////////
  950.  
  951. /**
  952. * Create a new Socket.
  953. *
  954. * @param host2
  955. * @param port2
  956. * @return
  957. * @throws UnknownHostException
  958. * @throws IOException
  959. */
  960. private Socket createSocket(String host2, int port2) throws UnknownHostException, IOException {
  961. socket = new Socket(host2,port2);
  962. socket.setKeepAlive(true);
  963. return socket;
  964. }
  965.  
  966. /**
  967. * Get the server Id
  968. * @param name
  969. * @return
  970. */
  971. private static String getServerId(String name) {
  972.  
  973. //replace [^0-9a-z] with q
  974. name = name.toLowerCase().replace("-", "q").replace("_","q");
  975.  
  976.  
  977.  
  978. //get the minimum number _ 5 or if name.length() _ whatever is smallest
  979. int mins = Math.min(5, name.length());
  980.  
  981. //get a substring starting at 0 place and ending at mins
  982. //example: 0 - 5 of string digitalmasterminds = digit
  983. String substr = name.substring(0, mins);
  984.  
  985. //cast big int .intvalue() with string value
  986. //of substr and radix of 36 to float
  987. float fnv = (float) new BigInteger(substr, 36).intValue();
  988.  
  989. int lnv = 1000;
  990. //if name length larger than 6
  991. if(name.length() > 6) {
  992. //basicly the same as above in one line
  993. lnv = new BigInteger(name.substring(6, 6 + Math.min(3, name.length() - 5)), 36).intValue();
  994. //get the max number _ 1000 or lnv _ whatever is largest
  995. lnv = Math.max(lnv, 1000);
  996. }
  997.  
  998. //num = mod of fnv and lnv divided by lnv
  999. float num = (fnv % lnv) / lnv;
  1000.  
  1001.  
  1002. //variable to iterate map
  1003. Iterator<Entry<String, Integer>> it = _chatangoTagserver.entrySet().iterator();
  1004.  
  1005. //maxnum starting point
  1006. int maxnum=0;
  1007.  
  1008. //loop while map has data left
  1009. while (it.hasNext()) {
  1010. //set entry
  1011. Map.Entry<String, Integer> pair = it.next();
  1012. //add up entry values
  1013. maxnum = maxnum + (int)pair.getValue();
  1014.  
  1015. }
  1016.  
  1017. float sumfreq = 0;//sum
  1018. String sn = "";//serv num
  1019.  
  1020. //map iterator
  1021. Iterator<Entry<String, Integer>> it2 = _chatangoTagserver.entrySet().iterator();
  1022.  
  1023. //loop map
  1024. while(it2.hasNext()) {
  1025.  
  1026. //set entry
  1027. Map.Entry<String, Integer> pair2 = it2.next();
  1028.  
  1029. //add up pair value / maxnum
  1030. sumfreq += ((float) pair2.getValue()) / maxnum;
  1031.  
  1032. //if num <= sumfreq
  1033. if(num <= sumfreq) {
  1034. //parse key
  1035. sn = pair2.getKey();
  1036. break;
  1037. }
  1038. }
  1039. //return server number
  1040. return sn;
  1041. }
  1042.  
  1043.  
  1044.  
  1045. /**
  1046. * GENERATE UID
  1047. *
  1048. * -Generate a UID with 16 characters
  1049. *
  1050. * @return id
  1051. */
  1052. private String generateUID() {
  1053. String id = "";
  1054. Random random = new Random();
  1055. while(id.length() < 16) {
  1056. id += random.nextInt(10);
  1057. }
  1058. return id;
  1059. }
  1060.  
  1061.  
  1062. //////////////////////////////////
  1063. //Send To Server Utilities///////
  1064.  
  1065. /**
  1066. * Send data to server.
  1067. *
  1068. * @param data
  1069. * @throws IOException
  1070. */
  1071. private static void writeToServer(String data) throws IOException{
  1072. writeLock.lock();
  1073. baos = new ByteArrayOutputStream();
  1074.  
  1075. if (firstCommand) {
  1076. baos.write(data.getBytes());
  1077. baos.write(0x00);
  1078.  
  1079. } else {
  1080. baos.write(data.getBytes());
  1081. }
  1082. sockOutput = new DataOutputStream(socket.getOutputStream());
  1083. channel = Channels.newChannel(sockOutput);
  1084. channel.write(ByteBuffer.wrap(baos.toByteArray()));
  1085. sockOutput.flush();
  1086. writeLock.unlock();
  1087. }
  1088.  
  1089. /**
  1090. * Idle ping
  1091. *
  1092. * @throws IOException
  1093. */
  1094. private void startIdlePing() throws IOException {
  1095. executorService.scheduleWithFixedDelay(new Runnable() {
  1096. @Override
  1097. public void run() {
  1098. try {
  1099. sendPing();
  1100. } catch (IOException e) {
  1101. e.printStackTrace();
  1102. }
  1103. }
  1104. }, 20L, 20L, TimeUnit.SECONDS);
  1105. }
  1106.  
  1107. /**
  1108. * Send Ping.
  1109. *
  1110. * @throws IOException
  1111. */
  1112. private void sendPing() throws IOException {
  1113. writeToServer("\r\n");
  1114. }
  1115.  
  1116. /**
  1117. * Send login command.
  1118. *
  1119. * @param login
  1120. * @throws IOException
  1121. */
  1122. private void sendLoginCommand(String login) throws IOException {
  1123. writeToServer(login);
  1124. Bot.print("[Room] Login sent to server : "+baos);
  1125. System.out.println("[Room] Login sent to server : "+baos);
  1126. firstCommand = false;
  1127. }
  1128.  
  1129. /**
  1130. * Post a message to the chat.
  1131. *
  1132. * @param msg
  1133. * @throws IOException
  1134. */
  1135. private static void sendMsg(String msg) throws IOException {
  1136. if(quiet){
  1137. return;
  1138. }
  1139. if(!quiet){
  1140. msg = "bmsg:tl2r:"+"<n"+nameColor+"/><f x"+fontSize+fontColor+"=\""+fontFace+"\">"+msg+"\r\n";
  1141. writeToServer(msg);
  1142. }
  1143. }
  1144.  
  1145. /**
  1146. * Get Blocklist
  1147. * @throws IOException
  1148. */
  1149. private void getBlockList() throws IOException {
  1150. writeToServer("blocklist:block::next:500\r\n");
  1151. }
  1152.  
  1153. /**
  1154. * Get Premium
  1155. * @throws IOException
  1156. */
  1157. private void getPremium() throws IOException {
  1158. writeToServer("getpremium:1\r\n");
  1159. }
  1160.  
  1161.  
  1162. /**
  1163. * Set Media - Video/Audio Recording
  1164. * @throws IOException
  1165. */
  1166. private void setMedia() throws IOException {
  1167. writeToServer("msgmedia:1\r\n");
  1168. }
  1169.  
  1170. /**
  1171. * Get Room Users
  1172. * @throws IOException
  1173. */
  1174. private void getRoomUsers() throws IOException {
  1175. writeToServer("g_participants:start\r\n");
  1176. }
  1177.  
  1178. /**
  1179. * Set to use the BG
  1180. * @throws IOException
  1181. */
  1182. private void setBG() throws IOException {
  1183. writeToServer("msgbg:1\r\n");
  1184. }
  1185.  
  1186. /**
  1187. * Add Mod
  1188. * @param name
  1189. * @throws IOException
  1190. */
  1191. private void addMod(String name) throws IOException{
  1192. writeToServer("addmod:"+name+"\r\n");
  1193. }
  1194.  
  1195. /**
  1196. * Remove Mod
  1197. * @param name
  1198. * @throws IOException
  1199. */
  1200. private void removeMod(String name) throws IOException{
  1201. writeToServer("removemod:"+name+"\r\n");
  1202. }
  1203.  
  1204. /**
  1205. * Ban a User
  1206. * @param userID
  1207. * @param ip
  1208. * @param name
  1209. * @throws IOException
  1210. */
  1211. private void ban(String userID,String ip, String name) throws IOException{
  1212. writeToServer("block:"+userID+":"+ip+":"+name+"\r\n");
  1213. }
  1214.  
  1215. /**
  1216. * Unban a User
  1217. * @param userID
  1218. * @param ip
  1219. * @param name
  1220. * @throws IOException
  1221. */
  1222. private void unBan(String userID,String ip, String name) throws IOException{
  1223. writeToServer("removeblock:"+userID+":"+ip+":"+name+"\r\n");
  1224. }
  1225.  
  1226. /**
  1227. * Delete a message
  1228. * @param msgId
  1229. * @throws IOException
  1230. */
  1231. private void deleteMsg(String msgId) throws IOException{
  1232. writeToServer("delmsg:"+msgId+"\r\n");
  1233. }
  1234.  
  1235. /**
  1236. * Delete all a users messages
  1237. * @param userId
  1238. * @throws IOException
  1239. */
  1240. private void deleteUserAllMsg(String userId) throws IOException{
  1241. writeToServer("delallmsg:"+userId+"\r\n");
  1242. }
  1243.  
  1244. /**
  1245. * Flag a user
  1246. * @param msgId
  1247. * @throws IOException
  1248. */
  1249. private void flag(String msgId) throws IOException{
  1250. writeToServer("g_flag:"+msgId+"\r\n");
  1251. }
  1252.  
  1253. /**
  1254. * Flag a user
  1255. * @param msgId
  1256. * @throws IOException
  1257. */
  1258. private void clearAllMsg() throws IOException{
  1259. writeToServer("clearall:\r\n");
  1260. }
  1261.  
  1262. /**
  1263. * Send a message from the GUI or use a console command
  1264. * @param text
  1265. * @throws IOException
  1266. */
  1267. public static void sendFieldText(String text) throws IOException {
  1268. //If text starts with /cmd: - use a console command//
  1269. if(text.toLowerCase().trim().startsWith("/cmd")){
  1270. //Use console command//
  1271. consoleCommands(text);
  1272. }else{
  1273. //If not a console cmd post msg in all chatrooms//
  1274. sendMsg(text);
  1275. }
  1276. }
  1277.  
  1278.  
  1279. /**
  1280. * Console Commands - commands entered into the bots GUI.
  1281. * All console commands must start with /cmd:
  1282. * followed by the command:
  1283. * and then the single or multiple arguments all parts seperated by :
  1284. *
  1285. * @param text
  1286. * @throws FileNotFoundException
  1287. * @throws UnsupportedEncodingException
  1288. */
  1289. private static void consoleCommands(String text) throws FileNotFoundException, UnsupportedEncodingException {
  1290.  
  1291. //lower and trim text//
  1292. text = text.toLowerCase().trim();
  1293.  
  1294. //check if valid cmd - contains ://
  1295. if(!text.contains(":")){
  1296. Bot.print("[Console CMD] Error : invalid console cmd");
  1297. System.out.println("[Console CMD] Error : invalid console cmd");
  1298. return;//return//
  1299. }
  1300.  
  1301. //split text at : after checking if it contains ://
  1302. String[] parts = text.split(":");
  1303.  
  1304. //cmd and args//
  1305. String conCmd = "";
  1306. String conArgs = "";
  1307. String conArgs2 = "";
  1308.  
  1309. System.out.println("[Console CMD Debug] Room;: "+Arrays.toString(parts)+parts.length);
  1310.  
  1311. //check if valid cmd - lenght of cmd less than 3 parts//
  1312. if(parts.length < 2){
  1313. Bot.print("[Console CMD] Error : invalid console cmd");
  1314. System.out.println("[Console CMD] Error : invalid console cmd");
  1315. return;//return//
  1316. }
  1317.  
  1318. //if command is a no args command//
  1319. if(parts.length == 2){
  1320. conCmd = parts[1];//command//
  1321. }
  1322.  
  1323. //if command is a single args command//
  1324. if(parts.length == 3){
  1325. conCmd = parts[1];//command//
  1326. conArgs = parts[2];//first arg//
  1327. }
  1328.  
  1329. //if command is a multi args command//
  1330. if(parts.length == 4){
  1331. conCmd = parts[1];//command//
  1332. conArgs = parts[2];//first args//
  1333. conArgs2 = parts[3];//second args//
  1334. }
  1335.  
  1336. ////////////////////////////////////////////
  1337. //Console commands/////////////////////////
  1338.  
  1339. //set quiet//
  1340. if(conCmd.trim().equals("quiet") && !conArgs.equals("")){
  1341. if(conArgs.equals("true")){
  1342. quiet = true;
  1343. Bot.print("[Console CMD] Quiet : " + quiet);
  1344. System.out.println("[Console CMD] Quiet : " + quiet);
  1345. }else if(conArgs.equals("false")){
  1346. quiet = false;
  1347. Bot.print("[Console CMD] Quiet : " + quiet);
  1348. System.out.println("[Console CMD] Quiet : " + quiet);
  1349. }
  1350. }
  1351. //set font color//
  1352. if(conCmd.trim().equals("fontcolor") && !conArgs.equals("")){
  1353. fontColor = conArgs;
  1354. }
  1355. //set font face//
  1356. if(conCmd.trim().equals("fontface") && !conArgs.equals("")){
  1357. fontFace = conArgs;
  1358. }
  1359. //set fornt size//
  1360. if(conCmd.trim().equals("fontsize") && !conArgs.equals("")){
  1361. fontSize = conArgs;
  1362. }
  1363. //set name color//
  1364. if(conCmd.trim().equals("namecolor")&& !conArgs.equals("")){
  1365. nameColor = conArgs;
  1366. }
  1367. //add a mod//
  1368. if(conCmd.trim().equals("addmod")&& !conArgs.equals("")){
  1369.  
  1370. }
  1371. //remove a mod//
  1372. if(conCmd.trim().equals("removemod")&& !conArgs.equals("")){
  1373.  
  1374. }
  1375. //ban a user//
  1376. if(conCmd.trim().equals("ban")&& !conArgs.equals("")){
  1377.  
  1378. }
  1379. //unban a user//
  1380. if(conCmd.trim().equals("unban")&& !conArgs.equals("")){
  1381.  
  1382. }
  1383. if(conCmd.trim().equals("join")&& !conArgs.equals("")){
  1384.  
  1385. }
  1386. if(conCmd.trim().equals("leave")&& !conArgs.equals("")){
  1387.  
  1388. }
  1389. if(conCmd.trim().equals("post")&& !conArgs.equals("") && !conArgs2.equals("")){
  1390.  
  1391. }
  1392. //get banlist//
  1393. if(conCmd.trim().equals("getbanlist")){
  1394. Bot.print("[Console CMD] BanList : " + banlist);
  1395. System.out.println("[Console CMD] BanList : " + banlist);
  1396. }
  1397. //get mods//
  1398. if(conCmd.trim().equals("getmods")){
  1399. Bot.print("[Console CMD] Mods : " + mods);
  1400. System.out.println("[Console CMD] Mods : " + mods);
  1401. }
  1402. //get owner//
  1403. if(conCmd.trim().equals("getowner")){
  1404. Bot.print("[Console CMD] Owner : " + owner);
  1405. System.out.println("[Console CMD] Owner : " + owner);
  1406. }
  1407. //get count//
  1408. if(conCmd.trim().equals("getcount")){
  1409. Bot.print("[Console CMD] Count : " + count);
  1410. System.out.println("[Console CMD] Count : " + count);
  1411. }
  1412. //get chatlist//
  1413. if(conCmd.trim().equals("getchatlist")){
  1414. Bot.print("[Console CMD] Chat List : " + participants);
  1415. System.out.println("[Console CMD] Chat List : " + participants);
  1416. }
  1417. }
  1418.  
  1419.  
  1420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement