int i = 0, lastIndex, firstIndex = 1;
int i = 1, lastIndex, firstIndex = 1;
	public void messagePrivMsg(String nick, String host, String recipient, String msg) {		boolean pm = false;		String log;		if (iequals(recipient, name)) {			recipient = nick;			pm = true;		}		log = "<" + nick + "> ";		log += msg.substring(0, msg.length());		logfile(recipient, log);		msg = msg.trim();		checkForMessages(nick, host, recipient);		checkForBlacklist(nick, recipient);		updateHistory(nick, msg);				//Bot has been mentioned?		if (iregex(name, msg)) {			if (gamesurge()) {				if (iregex("fuck you", msg)) {					if (System.currentTimeMillis() > nextWho) {	//!spam						ircio.privmsg(recipient, "Fuck you too, buddy.");						nextWho = System.currentTimeMillis() + 5000;						return;					}				} else if (iregex("screw you", msg)) {					if (System.currentTimeMillis() > nextWho) {	//!spam						ircio.privmsg(recipient, "Screw you too, buddy.");						nextWho = System.currentTimeMillis() + 5000;						return;					}				} else if (iregex("you suck", msg)) {					if (System.currentTimeMillis() > nextWho) {	//!spam						ircio.privmsg(recipient, "I suck, but you swallow, bitch.");						nextWho = System.currentTimeMillis() + 5000;						return;					}				}			}			if (iregex("(thank( ?(yo)?u|[sz])\\b|\\bt(y|hn?x)\\b)", msg)) {				if (System.currentTimeMillis() > nextWho) { //!spam					ircio.privmsg(recipient, "No problem.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			}		}		//Say hello!		int nameEnd = name.length() < 4 ? name.length() : 4;		if (iregex(name.substring(0, nameEnd), msg)) {			for (int i = 0; i < hellos.length; i++) {				if (iregex("^"+hellos[i], msg)) {					if (System.currentTimeMillis() > nextHi) {	//!spam						ircio.privmsg(recipient, helloreplies[new Random().nextInt(helloreplies.length)]);						nextHi = System.currentTimeMillis() + 500;						return;					}				}			}		}		StringTokenizer tok = new StringTokenizer(msg, ",: ");		String botname;		if (!pm && tok.hasMoreElements()) {			botname = tok.nextToken();		} else {			botname = "";		}		if (pm || talkingToMe(msg)) {			//Remove the bot's name			if (!pm)				msg = msg.substring(msg.indexOf(" ")+1);			if (iregex("bring out the strapon", msg)) {				ircio.privemote(recipient, "steps forward with a large strapon and begins mashing potatoes.");				return;			}			//Remove punctuation from the end			while (msg.endsWith(".") || msg.endsWith("?") || msg.endsWith("!")){				msg = msg.substring(0, msg.length()-1);			}			//BEGIN COLLOQUIAL COMMANDS			//These commands can be used anywhere if the bot's name is spoken first.			if (iequals("who are you", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privmsg(recipient, "I am an advanced SephiaBot channel bot.");					ircio.privmsg(recipient, "I'll kick your ass in days that end in 'y'.");					ircio.privmsg(recipient, "I was written by Vino. Vino rocks.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (gamesurge() && iregex("what does marsellus wallace look like", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privmsg(recipient, "He's black.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("who wrote you", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privmsg(recipient, "I was written by Vino. Vino rocks.");					ircio.privmsg(recipient, "Nilbus helped too.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("who is here", msg) && nick.equals("Nilbus")) {				if (System.currentTimeMillis() > nextWho) {	//!spam					int channum = channelNumber(recipient);					if (channum == -1 || pm) {						ircio.privmsg(recipient, "It's just you and me in a PM, buddy.");						nextWho = System.currentTimeMillis() + 5000;						return;					} else {						StringBuffer buf = new StringBuffer("Users in this channel:");						IRCUser current = server.channels[channum].users;						for (int i = 0; i < server.channels[channum].numusers; i++) {							buf.append(" " + current.name);							current = current.next;						}						//ircio.privmsg(recipient, buf.toString());						ircio.privmsg("Nilbus", buf.toString()); //XXX: debug						nextWho = System.currentTimeMillis() + 5000;						return;					}				}			} else if (iregex("who is vino", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privmsg(recipient, "A dirty cuban.");					ircio.privmsg(recipient, "And my daddy.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("who is remy", msg)					|| iregex("who is luckyremy",msg)) {				if (System.currentTimeMillis() > nextWho) {     //!spam					ircio.privmsg(recipient, "Father of the Black Sheep.");					ircio.privmsg(recipient, "Harbinger of Doom.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("who is", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					User target = getUserByNick(msg.substring(msg.lastIndexOf(' ')+1, msg.length()));					if (target == null)						ircio.privmsg(recipient, "Nobody important.");					else						ircio.privmsg(recipient, "He's a cool guy.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (gamesurge() && iregex("are you (sexy|hot)", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privmsg(recipient, "Fuck yes.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (gamesurge() && iregex("want to cyber", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					if (!isVino(host)) {						ircio.privmsg(recipient, "Fuck no.");					} else {						ircio.privmsg(recipient, "Take me, " + nick + "!");					}					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("wh?[aeu]re?('?[sz]| i[sz]| si| be?)( m(a[ih]|y))? vino", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					if (vino.away == null) {						ircio.privmsg(recipient, "If he's not here, I dunno. He hasn't told me he's gone.");					} else {						ircio.privmsg(recipient, "He's " + vino.away + ". He's been gone for " + makeTime(vino.leavetime) + ".");					}					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("wh?[aeu]re?('?[sz]| i[sz]| si| be?)( m(a[ih]|y))?", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					String targetName = msg.substring(msg.lastIndexOf(' ')+1, msg.length());					while (targetName.endsWith("!") || targetName.endsWith("?") || targetName.endsWith(","))						targetName = targetName.substring(0, targetName.length()-1);					if (iregex("eve?ry ?(b(o|ud)dy|(1|one?))", targetName)) {						//Find out where everybody is and tell the channel.						for (int i = 0; i < users.length; i++) {							User user = users[i];							if (user.away != null)								ircio.privmsg(recipient, user.userName + " has been " + user.away + " for " + makeTime(user.leavetime) + ".");						}						if (vino.away != null)							ircio.privmsg(recipient, vino.userName + " has been " + vino.away + " for " + makeTime(vino.leavetime) + ".");						return;					}					if (iequals(targetName, botname))						return;					User target = getUserByNick(targetName);					if (target == null) {						ircio.privmsg(recipient, "Like I know.");						return;					} else						targetName = target.userName; //use correct caps					if (target.away == null) {						ircio.privmsg(recipient, "I don't know.");					} else {						ircio.privmsg(recipient, targetName + " is " + target.away + ".  " + targetName + " has been gone for " + makeTime(target.leavetime) + ".");					}					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("who am i", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					User target = getUserByHost(host);					if (isVino(host)) {						ircio.privmsg(recipient, "Daddy!");						ircio.privemote(recipient, "hugs " + nick + ".");					} else if (target != null) {						ircio.privmsg(recipient, "Someone I know.");					} else {						ircio.privmsg(recipient, "Nobody important.");					}					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("who('| i)?s your daddy", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privmsg(recipient, "Vino's my daddy, ugh! Spank me again Vino!");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("knock knock", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privmsg(recipient, "Who's there?");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (gamesurge() && iregex("i suck dick", msg)) {				if (System.currentTimeMillis() > nextWho) { //!spam					ircio.privmsg(recipient, "Yeah, we know you do.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (gamesurge() && iregex("words of wisdom", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					String phrase = randomPhrase(new File(sephiadir, "wordsofwisdom.txt"));					if (phrase != null)						ircio.privmsg(recipient, phrase);					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("roll (the )?dice", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					Random rand = new Random();					int dice = rand.nextInt(5)+2;					int sides = rand.nextInt(5)+6;					ircio.privemote(recipient, "rolls " + dice + "d" + sides + " dice and gets " + (dice*sides+1) + ".");					if (!isVino(host)) {						ircio.privemote(recipient, "kills " + nick + ".");					} else {						ircio.privemote(recipient, "hugs " + nick + ".");					}					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("do a little dance", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					ircio.privemote(recipient, "makes a little love.");					nextWho = System.currentTimeMillis() + 5000;					return;				}			} else if (iregex("excuse", msg)) {				if (System.currentTimeMillis() > nextWho) {	//!spam					String excuse = randomPhrase(new File(sephiadir, "excuses.txt"));					if (excuse != null)						ircio.privmsg(recipient, "Your excuse is: " + excuse);					nextWho = System.currentTimeMillis() + 5000;					return;				}							//BEGIN COMMAND SECTION.			//These are one-word commands only. The command is the first thing you say after the bot name. botname, command arguments.			} else if (tok.hasMoreElements()) {				String cmd = tok.nextToken(" ");				if (tok.hasMoreElements() && (cmd.startsWith(",") || cmd.startsWith(":"))) { 					cmd = tok.nextToken(" ");				}				if (iequals(cmd, "kill")) {					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "KILL! KILL! KILL!");						return;					}					String killed = tok.nextToken(" ");					User killerUser = getUserByHost(host);					User killedUser = getUserByNick(killed);					if ((killerUser == null || (killedUser != null && killedUser.memberType > killerUser.memberType)) && !isVino(host)) {						ircio.privemote(recipient, "giggles at " + nick);					} else if (iequals(killed, botname)) {						ircio.privmsg(recipient, ":(");					} else {						if (isVino(host))							killerUser = vino;						int killedAccess = getAccess(killed, channelNumber(recipient));						if (killedAccess != -1) {							ircio.privmsg(recipient, "It would be my pleasure.");							ircio.kick(recipient, killed, "This kick was compliments of " + killerUser.userName + ". Have a nice day.");						} else if (killed.equalsIgnoreCase("message")) {							if (!tok.hasMoreElements()) {								ircio.privmsg(recipient, "Which message? I need a number");								return;							}							try {								int msgIndex = Integer.parseInt(tok.nextToken());								int i = 1;								Message last = null;								User user = getUserByHost(host);								for (Message curr = firstMessage; curr != null; curr = curr.next) {									if (curr.sender.equals(nick) || (user != null && curr.sender.equals(user.userName))) {										if (i++ == msgIndex) {											if (last == null)												firstMessage = curr.next;											else												last.next = curr.next;											ircio.privmsg(recipient, "Message removed from " + curr.sender + " to " + curr.target + " " + makeTime(curr.time) + " ago:" + curr.message);											writeData();											return;										}									}									last = curr;								}								ircio.privmsg(recipient, "What message?");							} catch (NumberFormatException nfe) {								ircio.privmsg(recipient, "...if you can call that a number.");							}							return;						} else							ircio.privmsg(recipient, "Kill who what now?"); 						return;					}				} else if (iequals(cmd, "tell")) {					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "Tell who what?");						return;					}					String target = tok.nextToken(" ");					while (target.endsWith(".") || target.endsWith("!") || target.endsWith(","))						target = target.substring(0, target.length()-1);					//If the target is logged in, send the message to his username instead so he will always get it if he is logged in.					User user = getUserByNick(target);					if (user != null)						target = user.userName;					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "Tell " + target + " what?");						return;					}					String message = tok.nextToken("");					//If the target was "everybody" or "everyone" then send the message to every user.					if (getUserByHost(host) != null && (iequals(target, "everybody") || iequals(target, "everyone"))) {						for (int i = 0; i < users.length; i++) {							if (firstMessage == null) {								firstMessage = new Message(users[i].userName, message, nick);							} else {								Message currMsg = firstMessage;								while (currMsg.next != null)									currMsg = currMsg.next;								currMsg.next = new Message(users[i].userName, message, nick);							}						}						//Fall through here! Send one last message to Vino!						target = "Vino";					}					if (firstMessage == null) {						firstMessage = new Message(target, message, nick);					} else {						Message currMsg = firstMessage;						while (currMsg.next != null)							currMsg = currMsg.next;						currMsg.next = new Message(target, message, nick);					}					writeData();					ircio.privmsg(recipient, "OK, I'll make sure to let them know.");				} else if (iregex("^(butt?)?se(x|cks)$", cmd)) {					if (!tok.hasMoreElements()) {						ircio.privemote(recipient, "anally rapes " + nick + ".");						return;					}					String sexed = tok.nextToken(" ");					if (iregex("^vino", sexed)) {						ircio.privemote(recipient, "screams as Vino penetrates every orifice of her body!");					} else if (iequals(botname, sexed)) {						ircio.privemote(recipient, "furiously works the potato masher!");					} else {						int sexedAccess = getAccess(sexed, channelNumber(recipient));						if (sexedAccess == -1) {							sexed = nick;						}						ircio.privemote(recipient, "anally rapes " + sexed + ".");					}				} else if (iequals(cmd, "reboot")) {					if (isAdmin(host)) {						ircio.privmsg(recipient, "Be right back.");						System.exit(1);					} else						ircio.privmsg(recipient, "No.");					return;				} else if (iequals(cmd, "shutdown")) {					if (isVino(host)) {						ircio.privmsg(recipient, "Goodbye everybody!");						System.exit(0);					} else {						ircio.privmsg(recipient, "No.");					}					return;				} else if (iequals(cmd, "reload")) {					if (isAdmin(host)) {						parseConfig(config);						ircio.privmsg(recipient, "Done.");					} else {						ircio.privmsg(recipient, "No.");					}					return;				} else if (iequals(cmd, "save")) {					if (isAdmin(host)) {						writeData();						ircio.privmsg(recipient, "Done.");					} else {						ircio.privmsg(recipient, "No.");					}					return;				} else if (iequals(cmd, "listhosts")) {					User user = getUserByHost(host);					if (user == null)						return;					String buffer = "Hosts logged in as " + user.userName + ":";					for (int i = 0; i < 10; i++)						if (user.hosts[i] != null && user.hosts[i].length() > 0)							buffer += " " + (i+1) + ": " + user.hosts[i];					ircio.privmsg(nick, buffer);				} else if (iequals(cmd, "logout")) {					User user = getUserByHost(host);					if (user == null)						return;					if (!tok.hasMoreElements()) {						for (int i = 0; i < 10; i++) {							if (user.hosts[i] != null && user.hosts[i].equals(host)) {								user.hosts[i] = null;								ircio.privmsg(nick, "It's too bad things couldn't work out.");								writeData();								return;							}						}						ircio.privmsg(nick, "Your host is not logged in.");						return;					}					try {						int i = Integer.parseInt(tok.nextToken("").trim())-1;						if (i < 0 || i >= 10)							return;						if (user.hosts[i] == null || user.hosts[i].length() <= 0)							ircio.privmsg(nick, "That host is not logged in.");						else {							ircio.privmsg(nick, "It's too bad things couldn't work out.");							user.hosts[i] = null;						}					} catch (NumberFormatException nfe) {					}					writeData();				} else if (iequals(cmd, "login")) {					if (tok.countTokens() < 2) {						ircio.privmsg(nick, "Yeah. Sure. Whatever.");						return;					}					String login = tok.nextToken(" ").trim();					String passwd = tok.nextToken("").trim();					if (login.trim().equals("vino") && passwd.trim().equals("xxxxx")) {						boolean foundSpot = false;						int i;						for (i = 0; i < 10; i++) {							if (vino.hosts[i] != null && vino.hosts[i].equals(host)) {								ircio.privmsg(nick, "Silly you. You're already logged in.");								return;							}						}						for (i = 0; i < 10; i++) {							if (vino.hosts[i] == null || vino.hosts[i].length() <= 0) {								vino.hosts[i] = host;								foundSpot = true;								break;							}						}						if (!foundSpot)							ircio.privmsg(nick, "No spots left.");						else if (iequals(nick, "nilbus"))							ircio.privmsg(nick, "What's up Nilbus?");						else							ircio.privmsg(nick, "Hi, daddy! :D");						writeData();					} else if (validateLogin(login, passwd)) {						boolean foundSpot = false;						int i, userID = -1;						for (i = 0; i < users.length; i++)							if (users[i].userName.equalsIgnoreCase(login))								userID = i;						if (userID == -1) {	//WTFException							ircio.privmsg(nick, "WTF? Tell Vino you saw this.");							return;						}						for (i = 0; i < 10; i++) {							if (users[userID].hosts[i] != null && users[userID].hosts[i].equals(host)) {								ircio.privmsg(nick, "Silly you. You're already logged in.");								return;							}						}						for (i = 0; i < 10; i++) {							if (users[userID].hosts[i] == null || users[userID].hosts[i].length() <= 0) {								users[userID].hosts[i] = host;								foundSpot = true;								break;							}						}						if (!foundSpot)							ircio.privmsg(nick, "No spots left.");						else							ircio.privmsg(nick, "What's up " + users[userID].userName + "?");						writeData();					} else {						ircio.privmsg(nick, "No cigar.");						log("Failed login attempt by " + nick + "!" + host + " with " + login + "/" + passwd + ".");					}				} else if (iequals(cmd, "i'm")) {					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "You're what?");						return;					}					User user = getUserByHost(host);					if (user == null) {						ircio.privmsg(recipient, "I don't care.");						return;					}					String location = tok.nextToken("").trim();					if (iregex("^back", location)) {						if (user.away == null) {							ircio.privmsg(recipient, "Of course you are honey.");						} else {							ircio.privmsg(recipient, "Welcome back! You've been away for " + makeTime(user.leavetime) + ".");							user.away = null;						}					} else {						ircio.privmsg(recipient, "Have fun!");						//Remove punctuation from the end						while (location.endsWith(".") || location.endsWith("!") || location.endsWith(","))							location = location.substring(0, location.length()-1);						user.away = location.replaceAll("\"", "'");						user.leavetime = System.currentTimeMillis();					}					writeData();					return;				} else if (iequals(cmd, "messages")) {					User user = getUserByHost(host);					boolean foundMessage = false;					int i = 0, lastIndex, firstIndex = 1;					if (tok.hasMoreElements()) {						try {							firstIndex = Integer.parseInt(tok.nextToken());							if (firstIndex < 1)								firstIndex = 1;						} catch (NumberFormatException nfe) {						}					}					lastIndex = firstIndex + 5;					for (Message curr = firstMessage; curr != null; curr = curr.next) {						if (curr.sender.equals(nick) || (user != null && curr.sender.equals(user.userName))) {							if (i >= firstIndex) {								if (!foundMessage) {									ircio.privmsg(recipient, "You have sent the following messages:");									foundMessage = true;								}								ircio.privmsg(recipient, "Message " + i + ": To " + curr.target + " " + makeTime(curr.time) + " ago:" + curr.message);							}							i++;						}						if (i >= lastIndex)							break;					}					if (!foundMessage)						ircio.privmsg(recipient, "You havent sent any messages.");				} else if (iequals(cmd, "say")) {					if (!isAdmin(host)) {						ircio.privmsg(recipient, "No.");						return;					}					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "Say what?!?");						return;					}					String inchannel = recipient;					if (iequals("in", tok.nextToken())) {						inchannel = tok.nextToken();					}					ircio.privmsg(inchannel, tok.nextToken("").substring(1));					return;				//TODO: Make mode setting colloquial				} else if (iequals(cmd, "mode")) {					if (!isAdmin(host)) {						ircio.privmsg(recipient, "No.");						return;					}					String inchannel = recipient;					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "What channel?");						return;					}					inchannel = tok.nextToken();					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "Who?");						return;					}					String who = tok.nextToken();					String mode;					if (!tok.hasMoreElements()) {						ircio.privmsg(recipient, "What mode?");						return;					} else mode = tok.nextToken();					System.out.println("MODE " + inchannel + " " + mode + " " + who + "\n");					ircio.setMode(who, inchannel, mode);					return;				}			}		} else if (spelledMyNameWrong(botname)) {//			ircio.privmsg(recipient, nick + ", " + spell);		} else {			//Act like a parrot only if the message isn't a command			//Only repeat when 2 people said the same thing, but not the 3rd time.			if (iequals(historyText[0], historyText[1]) && !iequals(historyText[0], this.lastRepeat) && !iequals(historyNick[0], historyNick[1]) && !historyText[0].trim().startsWith("!")) {				this.lastRepeat = historyText[0];				ircio.privmsg(recipient, historyText[0]);			}		}	}