RejoinThread.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main;
  2. import org.jibble.pircbot.PircBot;
  3. public class RejoinThread implements Runnable{
  4. private volatile PircBot pb;
  5. private String chan;
  6. private static long DEFAULT_WAIT_BEFORE_RECONNECT = 10000;
  7. private Thread thread;
  8. private String threadName;
  9. private int failures;
  10. public RejoinThread(PircBot pb, String chan) {
  11. super();
  12. this.pb = pb;
  13. this.chan = chan;
  14. this.threadName = "Rejoin "+chan;
  15. }
  16. @Override
  17. public void run() {
  18. String [] chansWeAreIn;
  19. boolean connected = false;
  20. pb.sendMessage("Marmat", "A l'aide, je viens de me faire expulser de "+chan+" j'attend "+DEFAULT_WAIT_BEFORE_RECONNECT/1000+" secondes et je retente de me connecter");
  21. do {
  22. try {
  23. Thread.sleep(DEFAULT_WAIT_BEFORE_RECONNECT);
  24. // On patiente une minute avant chaque tentative
  25. pb.joinChannel(chan);
  26. chansWeAreIn = pb.getChannels();
  27. if(Main.isDebug()) {
  28. System.out.println("Nous sommes de manière sûre dans les channels :");
  29. }
  30. for(int i=0; i<chansWeAreIn.length; ++i) {
  31. if(Main.isDebug()) {
  32. System.out.println(chansWeAreIn[i]);
  33. }
  34. if(chansWeAreIn[i].equals(chan)) {
  35. connected = true;
  36. }
  37. }
  38. }catch (Exception e) {
  39. System.err.println("Je n'ai pas pu attendre avant de re-rejoindre le channel "+chan+" duquel je me suis fait kicker.");
  40. }
  41. }while(!connected);
  42. pb.sendMessage(chan, "Il semblerai que je me soit fait kicker récement. En cas de problème avec moi, merci de contacter adminsys<at>listes<dot>ffdn<dot>org , root[arobaSe]marmat[point]ovh, ou @Marmat sur l'IRC. ");
  43. pb.sendMessage(chan, "Vous pouvez aussi ouvrir un ticket ici: https://code.ffdn.org/marmat8951/bot-irc2/issues");
  44. }
  45. public void start() {
  46. System.err.println("Je me suis fait kicker de "+chan+" attente de "+DEFAULT_WAIT_BEFORE_RECONNECT/1000+" secondes entre chaque tentative de reconnection");
  47. if(thread == null) {
  48. thread = new Thread(this, this.threadName);
  49. thread.start();
  50. }
  51. }
  52. public int getFailures() {
  53. return failures;
  54. }
  55. /**
  56. * @return the dEFAULT_WAIT_BEFORE_RECONNECT
  57. */
  58. public static long getDEFAULT_WAIT_BEFORE_RECONNECT() {
  59. return DEFAULT_WAIT_BEFORE_RECONNECT;
  60. }
  61. /**
  62. * @param dEFAULT_WAIT_BEFORE_RECONNECT the dEFAULT_WAIT_BEFORE_RECONNECT to set
  63. */
  64. public static void setDEFAULT_WAIT_BEFORE_RECONNECT(long dEFAULT_WAIT_BEFORE_RECONNECT) {
  65. DEFAULT_WAIT_BEFORE_RECONNECT = dEFAULT_WAIT_BEFORE_RECONNECT;
  66. }
  67. }