\r
import java.io.PrintWriter;\r
import java.net.Socket;\r
+import java.util.Hashtable;\r
import java.util.Properties;\r
import java.util.Scanner;\r
+import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
import net.rizon.TrustingSSLSocketFactory;\r
\r
public class BncControlThread extends Thread {\r
private Properties config = new Properties();\r
- \r
+\r
private PrintWriter netOut;\r
\r
+ private Hashtable<String, String> results;\r
+\r
public BncControlThread(Properties props) {\r
this.config = props;\r
+\r
+ results = new Hashtable<String, String>();\r
}\r
- \r
+\r
public void run() {\r
try {\r
Socket socket = new TrustingSSLSocketFactory().createSocket(config.getProperty("server"), Integer.parseInt(config.getProperty("port")));\r
netOut = new PrintWriter(socket.getOutputStream(), true);\r
- \r
+\r
send("PASS :" + config.getProperty("pass"));\r
send("NICK " + config.getProperty("nick"));\r
send("USER " + config.getProperty("ident") + " 1 * :" + config.getProperty("gecos"));\r
- \r
+\r
// TODO - Fix so that scan.nextLine() does not have to return for thread to exit.\r
Scanner scan = new Scanner(socket.getInputStream());\r
Pattern ccStripper = Pattern.compile("[\u0002\u001F\u000F]");\r
Pattern colorStripper = Pattern.compile("\u0003[0-9]{1,2}(,[0-9]{1,2})?");\r
+ Pattern userAddSuccess = Pattern.compile(":\\*admin!.*@.* PRIVMSG .* :User (.*) added!");\r
+ Pattern userDelSuccess = Pattern.compile(":\\*admin!.*@.* PRIVMSG .* :User (.*) deleted!");\r
+ Pattern genericError = Pattern.compile(":\\*admin!.*@.* PRIVMSG .* :Error: (User (.*) .*)");\r
String line;\r
while (BncBot.running && scan.hasNextLine() && !Thread.interrupted()) {\r
line = scan.nextLine();\r
line = ccStripper.matcher(line).replaceAll("");\r
line = colorStripper.matcher(line).replaceAll("");\r
- \r
+\r
if (BncBot.debug)\r
System.out.println("BNC<- " + line);\r
- \r
+\r
// Do stuff with input if necessary.\r
+ Matcher userAddM = userAddSuccess.matcher(line), userDelM = userDelSuccess.matcher(line), errorM = genericError.matcher(line);\r
+ if (userAddM.matches()) {\r
+ synchronized (this.results) {\r
+ this.results.put(userAddM.group(1), "SUCCESS");\r
+ }\r
+ }\r
+ if (userDelM.matches()) {\r
+ synchronized (this.results) {\r
+ this.results.put(userDelM.group(1), "SUCCESS");\r
+ }\r
+ }\r
+ if (errorM.matches()) {\r
+ synchronized (this.results) {\r
+ this.results.put(errorM.group(2), errorM.group(1));\r
+ }\r
+ }\r
}\r
- \r
+\r
scan.close();\r
socket.close();\r
- } catch(Exception e) {\r
+ } catch (Exception e) {\r
e.printStackTrace();\r
}\r
}\r
- \r
+\r
public boolean addUser(String user, String password) {\r
privmsg("*admin", String.format("adduser %s %s irc.rizon.net", user, password));\r
- \r
- // TODO - Make this check to see if it succeeded.\r
- return true;\r
+\r
+ long now = System.currentTimeMillis();\r
+ while (!this.results.containsKey(user) && (now + 1000 > System.currentTimeMillis())) { // One second timeout.\r
+ try {\r
+ Thread.sleep(100);\r
+ } catch (Exception e) {\r
+ }\r
+ }\r
+\r
+ if (!this.results.containsKey(user))\r
+ return true;\r
+ else if (this.results.get(user).equals("SUCCESS"))\r
+ return true;\r
+ else\r
+ return false;\r
}\r
\r
+ public boolean delUser(String user) {\r
+ privmsg("*admin", String.format("deluser %s", user));\r
+\r
+ long now = System.currentTimeMillis();\r
+ while (!this.results.containsKey(user) && (now + 1000 > System.currentTimeMillis())) { // One second timeout.\r
+ try {\r
+ Thread.sleep(100);\r
+ } catch (Exception e) {\r
+ }\r
+ }\r
+\r
+ if (!this.results.containsKey(user))\r
+ return true;\r
+ else if (this.results.get(user).equals("SUCCESS"))\r
+ return true;\r
+ else\r
+ return false;\r
+ }\r
+\r
private void privmsg(String target, String msg) {\r
if (msg.length() > 400) {\r
for (int i = 400; i > 0; i--) {\r
ue.approve(args[0].substring(1));\r
String pass = randomPass();\r
\r
- // TODO - Actually modify the config file here.\r
+ boolean result = BncBot.bncControl.addUser(ue.getNick(), pass);\r
\r
- BncBot.privmsg(BncBot.config.getProperty("adminChannel"), String.format(BncBot.getString("adminBncAction"), BncBot.getString("adminApproved")));\r
- BncBot.sendMemo(ue.getNick(), String.format(BncBot.getString("approvalMemo"), ue.getNick(), pass));\r
+ if (result) {\r
+ BncBot.privmsg(BncBot.config.getProperty("adminChannel"), String.format(BncBot.getString("adminBncAction"), BncBot.getString("adminApproved")));\r
+ BncBot.sendMemo(ue.getNick(), String.format(BncBot.getString("approvalMemo"), ue.getNick(), pass));\r
+ } else {\r
+ BncBot.privmsg(BncBot.config.getProperty("adminChannel"), String.format(BncBot.getString("genericErrorReason"), BncBot.getString("errorConfigFail")));\r
+ }\r
} else {\r
BncBot.privmsg(BncBot.config.getProperty("adminChannel"), String.format(BncBot.getString("adminAlreadyProcessed"), BncBot.getString("adminApproved")));\r
}\r
break;\r
}\r
}\r
- \r
+\r
return sb.toString();\r
}\r
\r
# Generic error with explanation.\r
genericErrorReason = An error occurred while processing your request: %1$s\r
\r
+# =======================\r
+# ERROR\r
+# =======================\r
+errorConfigFail = Updating BNC configuration failed.\r
+\r
# =======================\r
# Admin Strings\r
# =======================\r
# Please wait!\r
userPleaseWait = Please wait...looking up your details...\r
# Rejection appeal message.\r
-userAppeal = To appeal the rejection, contact a user with voice (+v) in #RizonBNC.\r
+userAppeal = To appeal the rejection, contact a user with voice (+v) or higher in #RizonBNC.\r
# Already approved message.\r
# - First argument is the nick of the approving user.\r
# - Second argument is the hostmask (ident@host) of the approving user.\r
# Approval memo\r
# - First argument is the BNC username.\r
# - Second argument is the BNC password.\r
-approvalMemo = It is with great pleasure that we inform you that your BNC application has been APPROVED. To log on to your BNC, connect to bnc.rizon.net with the server password '%1$s:%2$s'. Thank you for flying Rizon!\r
+approvalMemo = It is with great pleasure that we inform you that your BNC application has been APPROVED. To log on to your BNC, connect to rizonbnc.us.rizon.net with the server password '%1$s:%2$s'. Thank you for flying Rizon!\r
# Rejection memo\r
# - First argument is a string containing the reason.\r
# - Second argument is the rejection appeal notice above.\r