I'm pretty sure these are most recent, took down my computer and these are the latest from where I was working on it at work.
IRC.java
Code: Select all
// Regex special characters ([{\^-$|]})?*+.
import java.io.*;
import java.net.*;
import java.util.regex.*;
import java.sql.*;
public class IRC implements Runnable{
String NICK="usefulbot";
String USERNAME="useful";
int PORT=6667;
String SERVER="irc.tddirc.com";
String CHANNEL="#test";
boolean isAdmin;
public void run(){
try{
Socket sock=new Socket(SERVER, PORT);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
BufferedReader in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
writeToServer("USER "+ USERNAME +" 8 * : " + USERNAME, out);
writeToServer("NICK "+ NICK, out);
writeToServer("JOIN "+ CHANNEL, out);
readFromServer(out, in, sock);
}catch(UnknownHostException e){
System.out.println("Unknown host exception was thrown: "+e.getMessage());
}catch(IOException e){
System.out.println("An error occurred with i/o socket: "+e.getMessage());
}
}
public void writeToChannel(String text, BufferedWriter out){
try{
out.write("PRIVMSG "+CHANNEL+" :"+text+"\n");
out.flush();
}catch(IOException e){
System.out.println("Failure when trying to write to server: "+e.getMessage());
}
}
public void writeToServer(String text, BufferedWriter out){
try{
out.write(text+"\n");
out.flush();
}catch(IOException e){
System.out.println("Failure when trying to write to server: "+e.getMessage());
}
}
// This method is used to parse the input, so that we can send requests easily to user.
public String parseLine(String line){
Pattern p=Pattern.compile(":\\w*!\\w*@\\w*.* PRIVMSG #\\w* :");
Matcher m=p.matcher(line);
if(m.find())
return line.substring(m.end());
return line;
}
// Consider adding a login check algorithm for verifying user is in channel before getting username.
protected String getIRCUserName(String line){
Pattern userPattern=Pattern.compile("^:\\w*!", Pattern.CASE_INSENSITIVE);
Matcher user=userPattern.matcher(line);
String username=null;
if(user.find()){
username=line.substring(user.start()+1, user.end()-1);
return username;
}
return "";
}
public String getTimeStamp(){
Timestamp st = new Timestamp(System.currentTimeMillis());
String time=st.toString();
return time.substring(0, time.indexOf("."));
}
public static void main(String args[])throws Exception{
IRC irc=new IRC();
Thread t=new Thread(irc);
t.start();
}
public void readFromServer(BufferedWriter out, BufferedReader in, Socket sock){
try{
String line=null;
while((line=in.readLine()) != null){
String parsedLine=parseLine(line);
System.out.println(getTimeStamp()+" "+getIRCUserName(line)+" "+parsedLine);
Actions actions=new Actions();
if(line.matches("^PING.*"))
actions.ping(out);
if(parsedLine.toLowerCase().matches("^!quit$"))
actions.disconnect(sock, out, line);
if(parsedLine.toLowerCase().matches("^!trivia$")) {
Trivia trivia=new Trivia(out, in, sock);
}
if(parsedLine.toLowerCase().matches("!admin"))
writeToChannel("is "+getIRCUserName(line)+" an admin "+isAdmin(getIRCUserName(line)), out);
if(parsedLine.toLowerCase().matches("!nick \\w{1,9}"))
actions.changeNick(parsedLine, out);
}
}catch(IOException e){
System.out.println("Failed to read from server: "+e.getMessage());
}
}
public boolean isAdmin(String username){
ConnectionPool pool=new ConnectionPool();
String user=getIRCUserName(username);
System.out.println("Username "+username.toUpperCase());
try{
Connection conn=DriverManager.getConnection(pool.getUrl(), pool.getUsername(), pool.getPassword());
Statement stmt=conn.createStatement();
stmt.executeQuery("SELECT AUTH FROM USERS WHERE USERNAME='"+username.toLowerCase()+"';");
ResultSet rs=stmt.getResultSet();
String auth=null;
while(rs.next())
auth=rs.getString("AUTH");
rs.close();
stmt.close();
if(auth==null){
isAdmin=false;
return isAdmin;
}
System.out.println("Auth: "+auth);
if((auth.matches("admin")))
isAdmin=true;
}catch(SQLException e){
System.out.println("SQL Exception occurred: "+e.getMessage());
}
return isAdmin;
}
}
Actions.java
Code: Select all
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import java.sql.*;
public class Actions{
IRC irc=new IRC();
public void disconnect(Socket sock, BufferedWriter out, String line){
if(irc.isAdmin(irc.getIRCUserName(line))){
try{
irc.writeToChannel("Goodbye everyone", out);
sock.close();
}catch(IOException e){
System.out.println("An error occurred when trying to close socket: "+e.getMessage());
}
}else{
irc.writeToChannel(irc.getIRCUserName(line)+" is not an admin", out);
}
}
public void ping(BufferedWriter out){
try{
out.write("PONG "+irc.CHANNEL+"\n");
out.flush();
}catch(IOException e){
System.out.println("Error while trying to ping: "+e.getMessage());
}
}
public void changeNick(String parsedLine, BufferedWriter out){
StringTokenizer st=new StringTokenizer(parsedLine, " ");
st.nextToken(); // Tokens should be !NICK, followed by username, we only care about second token.
String username=st.nextToken();
irc.writeToServer("NICK "+username, out);
}
}