Java IRC bot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCAuthenticator.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.dmdirc.parser.irc;
  23. import java.net.Authenticator;
  24. import java.net.PasswordAuthentication;
  25. import java.util.Map;
  26. import java.util.HashMap;
  27. /**
  28. * Handles proxy authentication for the parser
  29. *
  30. * @author Shane Mc Cormack
  31. * @see IRCParser
  32. */
  33. public class IRCAuthenticator extends Authenticator {
  34. /**
  35. * A version number for this class. It should be changed whenever the class
  36. * structure is changed (or anything else that would prevent serialized
  37. * objects being unserialized with the new class).
  38. */
  39. private static final long serialVersionUID = 1;
  40. /** Singleton instance of IRCAuthenticator. */
  41. private static IRCAuthenticator me = null;
  42. /** List of authentication replies. */
  43. private final Map<String,PasswordAuthentication> replies = new HashMap<String,PasswordAuthentication>();
  44. /**
  45. * Create a new IRCAuthenticator.
  46. *
  47. * This creates an IRCAuthenticator and registers it as the default
  48. * Authenticator.
  49. */
  50. private IRCAuthenticator() {
  51. /* try {
  52. final Field field = Authenticator.class.getDeclaredField("theAuthenticator");
  53. field.setAccessible(true);
  54. final Object authenticator = field.get(null);
  55. if (authenticator instanceof Authenticator) {
  56. oldAuthenticator = (Authenticator)authenticator;
  57. }
  58. } catch (NoSuchFieldException nsfe) {
  59. } catch (IllegalAccessException iae) {
  60. }*/
  61. Authenticator.setDefault(this);
  62. }
  63. /**
  64. * Get the instance of IRCAuthenticator
  65. */
  66. public static synchronized IRCAuthenticator getIRCAuthenticator() {
  67. if (me == null) {
  68. me = new IRCAuthenticator();
  69. }
  70. return me;
  71. }
  72. /**
  73. * Add a server to authenticate for.
  74. *
  75. * @param server ServerInfo object with proxy details.
  76. */
  77. public void addAuthentication(final ServerInfo server) {
  78. addAuthentication(server.getProxyHost(), server.getProxyPort(), server.getProxyUser(), server.getProxyPass());
  79. }
  80. /**
  81. * Add a host to authenticate for.
  82. *
  83. * @param host Hostname
  84. * @param port Port
  85. * @param username Username to return for authentication
  86. * @param password Password to return for authentication
  87. */
  88. public void addAuthentication(final String host, final int port, final String username, final String password) {
  89. if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
  90. return;
  91. }
  92. final PasswordAuthentication pass = new PasswordAuthentication(username, password.toCharArray());
  93. final String fullhost = host.toLowerCase()+":"+port;
  94. if (replies.containsKey(fullhost)) {
  95. replies.remove(fullhost);
  96. }
  97. replies.put(fullhost, pass);
  98. }
  99. /** {@inheritDoc} */
  100. protected PasswordAuthentication getPasswordAuthentication() {
  101. /*
  102. * getRequestingHost: 85.234.138.2
  103. * getRequestingPort: 1080
  104. * getRequestingPrompt: SOCKS authentication
  105. * getRequestingProtocol: SOCKS5
  106. * getRequestingScheme: null
  107. * getRequestingSite: /85.234.138.2
  108. * getRequestingURL: null
  109. * getRequestorType: SERVER
  110. */
  111. final String fullhost = getRequestingHost().toLowerCase()+":"+getRequestingPort();
  112. return replies.get(fullhost);
  113. }
  114. }