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.

IdentdServer.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2010 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.addons.identd;
  23. import com.dmdirc.config.IdentityManager;
  24. import com.dmdirc.logger.Logger;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.plugins.PluginInfo;
  27. import com.dmdirc.plugins.PluginManager;
  28. import java.net.Socket;
  29. import java.net.ServerSocket;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. /**
  34. * The IdentdServer watches over the ident port when required
  35. *
  36. * @author Shane "Dataforce" Mc Cormack
  37. */
  38. public final class IdentdServer implements Runnable {
  39. /** The Thread in use for this server */
  40. private volatile Thread myThread = null;
  41. /** The current socket in use for this server */
  42. private ServerSocket serverSocket;
  43. /** Arraylist of all the clients we have */
  44. private final List<IdentClient> clientList = new ArrayList<IdentClient>();
  45. /** The plugin that owns us. */
  46. private final IdentdPlugin myPlugin;
  47. /**
  48. * Create the IdentdServer.
  49. */
  50. public IdentdServer(final IdentdPlugin plugin) {
  51. super();
  52. myPlugin = plugin;
  53. }
  54. /**
  55. * Run this IdentdServer.
  56. */
  57. @Override
  58. public void run() {
  59. final Thread thisThread = Thread.currentThread();
  60. while (myThread == thisThread) {
  61. try {
  62. final Socket clientSocket = serverSocket.accept();
  63. final IdentClient client = new IdentClient(this, clientSocket, myPlugin);
  64. addClient(client);
  65. } catch (IOException e) {
  66. if (myThread == thisThread) {
  67. Logger.userError(ErrorLevel.HIGH, "Accepting client failed: " + e.getMessage());
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * Add an IdentClient to the clientList
  74. *
  75. * @param client Client to add
  76. */
  77. public void addClient(final IdentClient client) {
  78. synchronized (clientList) {
  79. clientList.add(client);
  80. }
  81. }
  82. /**
  83. * Remove an IdentClient from the clientList
  84. *
  85. * @param client Client to remove
  86. */
  87. public void delClient(final IdentClient client) {
  88. synchronized (clientList) {
  89. for (int i = 0; i < clientList.size(); ++i) {
  90. if (clientList.get(i) == client) {
  91. clientList.remove(i);
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * Check if the server is currently running
  99. *
  100. * @return True if the server is running
  101. */
  102. public boolean isRunning() {
  103. return (myThread != null);
  104. }
  105. /**
  106. * Start the ident server
  107. */
  108. public void startServer() {
  109. if (myThread == null) {
  110. try {
  111. final int identPort = IdentityManager.getGlobalConfig().getOptionInt(myPlugin.getDomain(), "advanced.port");
  112. serverSocket = new ServerSocket(identPort);
  113. myThread = new Thread(this);
  114. myThread.start();
  115. } catch (IOException e) {
  116. Logger.userError(ErrorLevel.MEDIUM, "Unable to start identd server: " + e.getMessage());
  117. if (e.getMessage().equals("Permission denied")) {
  118. final PluginInfo plugin = PluginManager.getPluginManager().getPluginInfoByName("identd");
  119. if (plugin != null) {
  120. if (PluginManager.getPluginManager().delPlugin(plugin.getRelativeFilename())) {
  121. PluginManager.getPluginManager().updateAutoLoad(plugin);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * Stop the ident server
  130. */
  131. public void stopServer() {
  132. if (myThread != null) {
  133. final Thread tmpThread = myThread;
  134. myThread = null;
  135. if (tmpThread != null) {
  136. tmpThread.interrupt();
  137. }
  138. try {
  139. serverSocket.close();
  140. } catch (IOException e) {
  141. }
  142. synchronized (clientList) {
  143. for (int i = 0; i < clientList.size(); ++i) {
  144. clientList.get(i).close();
  145. }
  146. clientList.clear();
  147. }
  148. }
  149. }
  150. }