Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CharlieBravo.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.charliebravo;
  23. import com.dmdirc.parser.common.MyInfo;
  24. import com.dmdirc.parser.interfaces.Parser;
  25. import com.dmdirc.parser.interfaces.callbacks.DataInListener;
  26. import com.dmdirc.parser.interfaces.callbacks.DataOutListener;
  27. import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
  28. import com.dmdirc.parser.interfaces.callbacks.Post005Listener;
  29. import com.dmdirc.parser.irc.IRCParser;
  30. import com.dmdirc.parser.irc.ServerInfo;
  31. import java.util.Date;
  32. /**
  33. *
  34. * @author chris
  35. */
  36. public class CharlieBravo implements Runnable, Post005Listener,
  37. DebugInfoListener, DataInListener, DataOutListener {
  38. protected final Config config = new Config();
  39. protected final InputHandler handler = new InputHandler(config);
  40. protected final String[] servers = {"irc.quakenet.org","83.140.172.211"};
  41. public void run() {
  42. int server = 0;
  43. while (true) {
  44. final MyInfo myinfo = new MyInfo();
  45. myinfo.setNickname("CharlieBravo");
  46. myinfo.setRealname("Charlie Bravo");
  47. myinfo.setUsername("charliebravo");
  48. final IRCParser parser = new IRCParser(myinfo, new ServerInfo(servers[server], 6667, ""));
  49. handler.setParser(parser);
  50. parser.getCallbackManager().addCallback(Post005Listener.class, this);
  51. parser.getCallbackManager().addCallback(DebugInfoListener.class, this);
  52. parser.getCallbackManager().addCallback(DataInListener.class, this);
  53. parser.getCallbackManager().addCallback(DataOutListener.class, this);
  54. parser.run();
  55. server = ++server % servers.length;
  56. try {
  57. Thread.sleep(5000);
  58. } catch (InterruptedException ex) {
  59. // Don't care!
  60. }
  61. }
  62. }
  63. @Override
  64. public void onPost005(final Parser tParser, final Date date) {
  65. tParser.joinChannel("#MD87");
  66. tParser.joinChannel("#DMDirc");
  67. tParser.joinChannel("#DMDirc.dev");
  68. tParser.joinChannel("#MDbot");
  69. }
  70. @Override
  71. public void onDebugInfo(final Parser tParser, final Date date, final int nLevel, final String sData) {
  72. System.out.println(nLevel + ": " + sData);
  73. }
  74. public static void main(final String ... args) throws Exception {
  75. new Thread(new CharlieBravo()).start();
  76. }
  77. @Override
  78. public void onDataIn(final Parser tParser, final Date date, final String sData) {
  79. System.out.println("<<< " + sData);
  80. }
  81. @Override
  82. public void onDataOut(final Parser tParser, final Date date, final String sData, final boolean bFromParser) {
  83. System.out.println(">>> " + sData);
  84. }
  85. /**
  86. * Tests for and adds one component of the duration format.
  87. *
  88. * @param builder The string builder to append text to
  89. * @param current The number of seconds in the duration
  90. * @param duration The number of seconds in this component
  91. * @param name The name of this component
  92. * @return The number of seconds used by this component
  93. */
  94. private static int doDuration(final StringBuilder builder, final int current,
  95. final int duration, final String name) {
  96. int res = 0;
  97. if (current >= duration) {
  98. final int units = current / duration;
  99. res = units * duration;
  100. if (builder.length() > 0) {
  101. builder.append(", ");
  102. }
  103. builder.append(units);
  104. builder.append(' ');
  105. builder.append(name + (units != 1 ? 's' : ""));
  106. }
  107. return res;
  108. }
  109. /**
  110. * Formats the specified number of seconds as a string containing the
  111. * number of days, hours, minutes and seconds.
  112. *
  113. * @param duration The duration in seconds to be formatted
  114. * @return A textual version of the duration
  115. */
  116. public static String formatDuration(final int duration) {
  117. final StringBuilder buff = new StringBuilder();
  118. int seconds = duration;
  119. seconds -= doDuration(buff, seconds, 60*60*24, "day");
  120. seconds -= doDuration(buff, seconds, 60*60, "hour");
  121. seconds -= doDuration(buff, seconds, 60, "minute");
  122. seconds -= doDuration(buff, seconds, 1, "second");
  123. return buff.length() == 0 ? "0 seconds" : buff.toString();
  124. }
  125. }