Bot.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Bot
  2. //
  3. // Douglas Thrift
  4. //
  5. // Bot.java
  6. /* Copyright 2011 Douglas Thrift
  7. *
  8. * This file is part of Big Screen Bot.
  9. *
  10. * Big Screen Bot is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Big Screen Bot is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Big Screen Bot. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package net.douglasthrift.bigscreenbot;
  24. import java.net.InetAddress;
  25. import java.net.UnknownHostException;
  26. import java.util.Collection;
  27. import java.util.regex.Pattern;
  28. import org.apache.oro.text.GlobCompiler;
  29. import org.jibble.pircbot.PircBot;
  30. import org.jibble.pircbot.User;
  31. public abstract class Bot extends PircBot
  32. {
  33. protected boolean isNickInChannels(String nick)
  34. {
  35. for (String channel : getChannels())
  36. for (User user : getUsers(channel))
  37. if (user.equals(nick))
  38. return true;
  39. return false;
  40. }
  41. protected static Pattern compileNickMask(String mask)
  42. {
  43. return Pattern.compile(GlobCompiler.globToPerl5(mask.toCharArray(), GlobCompiler.DEFAULT_MASK), Pattern.CASE_INSENSITIVE);
  44. }
  45. protected static boolean matchNickMasks(String sender, String login, String hostname, Collection<Pattern> masks)
  46. {
  47. String nick = sender + "!" + login + "@" + hostname;
  48. for (Pattern mask : masks)
  49. if (mask.matcher(nick).matches())
  50. return true;
  51. try
  52. {
  53. InetAddress address = InetAddress.getByName(hostname);
  54. String name = address.getCanonicalHostName();
  55. if (!name.equalsIgnoreCase(hostname))
  56. {
  57. nick = sender + "!" + login + "@" + name;
  58. for (Pattern mask : masks)
  59. if (mask.matcher(nick).matches())
  60. return true;
  61. }
  62. }
  63. catch (UnknownHostException exception) {}
  64. return false;
  65. }
  66. }
  67. // vim: expandtab