package mx; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Arrays; import java.util.Date; import java.util.Timer; import java.util.regex.Matcher; import java.util.regex.Pattern; public class BnReg { private static String sUrl = "http://bbs.nju.edu.cn/"; private static String sLogin = sUrl + "bbslogin?type=2&id=百合账号&pw=百合密码"; private static String sMailList = sUrl + "bbsmail"; private static String sSendMail = sUrl + "bbssndmail"; private static String sLogUrl = sUrl + "blogdopost?perm=101&text=&title="; private static String usersDir = "/usr/local/pvpgn/var/users/"; private static String reservedAccts[] = {"disable", "bnm", "ap1", "ap2", "rd", "stop"}; private static String cookie = null; public static void main(String[] args) { Arrays.sort(reservedAccts); Timer timer = new Timer(); timer.schedule(new MyTask(), 1000, 20000); } static class MyTask extends java.util.TimerTask { public void run() { BnReg.execute(); } } public static void execute() { System.out.println("execute now" + new Date()); String res; // get mail list res = getContent(sMailList); // System.out.println(res); if (res.indexOf("错误! 您尚未登录, 请先登录! <br><br>") != -1) { System.out.println("need login"); doLogin(); doLog("re_login"); res = getContent(sMailList); } // System.out.println(res); Pattern Regex = Pattern .compile( "<input[^>]+type=checkbox[^>]+name=([^> ]+)><td>(<img src="/image/unread_mail//.gif" mce_src="image/unread_mail//.gif">|[^>]*)<td><a[^>]*>[^<]*</a><td>[^<]*<td><a[^>]*href=([^> ]+)[^>]*>★//s+apply//s+</a>", Pattern.CANON_EQ); Matcher RegexMatcher = Regex.matcher(res); while (RegexMatcher.find()) { String sMailLink = sUrl + RegexMatcher.group(3); System.out.println(sMailLink); res = getContent(sMailLink); // System.out.println(res); // get user Regex = Pattern.compile("寄信人: ([^//s]+)"); Matcher matcherUser = Regex.matcher(res); String user = null; if (matcherUser.find()) { user = matcherUser.group(1); } else { // error.... } // check if user valid if(Arrays.binarySearch(reservedAccts, user) > 0) { sendMail(user, "fail", "you cannot use this account"); continue; } /* * bnpassword:yourpassword */ Regex = Pattern .compile("bnpassword:([a-z0-9_]+)", Pattern.CANON_EQ); Matcher matcherPasswd = Regex.matcher(res); if (!matcherPasswd.find()) { sendMail(user, "fail", "illformat"); } else { String passwd = matcherPasswd.group(1); // generate user generateUser(user, passwd); // send password to user. sendMail(user, "successfull", "your password is: " + passwd); } // delete mail String sDelMail = sUrl + "bbsman2?" + RegexMatcher.group(1) + "=on"; getContent(sDelMail); } System.out.println("---end execute---"); } private static void sendMail(String user, String title, String msg) { String sMail = sSendMail + "?userid=" + user + "&title=" + encodeUrl(title) + "&text=" + encodeUrl(msg); System.out.println(sMail); getContent(sMail); // System.out.println(res); } private static void generateUser(String user, String passwd) { try { // check if user exists String userFile = usersDir + user; File f = new File(userFile); // update password if(f.exists()) { Pattern Regex = Pattern.compile("^(/"BNETacctpasshash1/"=)/"[0-9a-f]+/"", Pattern.CANON_EQ); BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(userFile))); StringBuffer sb = new StringBuffer(); String str = br.readLine(); while (str != null) { Matcher RegexMatcher = Regex.matcher(str); if(RegexMatcher.find()) { str = RegexMatcher.replaceAll("$1/"" + PvPGN.pvpgn_hash(passwd) + "/""); } sb.append(str + "/n"); str = br.readLine(); } br.close(); String s = sb.toString(); FileWriter fw = new FileWriter(userFile); fw.write(s,0,s.length()); fw.flush(); fw.close(); doLog("USER UPDATE: " + user); } else { // create new account BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(usersDir + "maxid.txt"))); String data = null; data = br.readLine(); br.close(); int id = parseInt(data); String str = "/"BNETacctusername/"=/"" + user + "/"/n" + "/"BNETacctpasshash1/"=/"" + PvPGN.pvpgn_hash(passwd) + "/"/n" + "/"BNETacctuserid/"=/"" + id + "/"/n"; FileWriter fw = new FileWriter(usersDir + "maxid.txt"); String s = (id + 1) + ""; fw.write(s,0,s.length()); fw.flush(); fw.close(); fw = new FileWriter(userFile); fw.write(str, 0, str.length()); fw.flush(); fw.close(); doLog("USER CREATE: " + user); } } catch (Exception e) { e.printStackTrace(); } } private static String encodeUrl(String s) { return s.replaceAll(" ", "%20"); } private static int parseInt(String s) { int r = 0; for (int i = 0; i != s.length(); ++i) { char ch = s.charAt(i); if ('0' <= ch && ch <= '9') { r = r * 10 + (ch - '0'); } else { return r; } } return r; } private static void doLog(String str) { getContent(sLogUrl + encodeUrl(str)); } private static void doLogin() { // process login String res = getContent(sLogin); // System.out.println(res); String sStart = "Net.BBS.setCookie('"; String sEnd = "')"; int iStart = res.indexOf(sStart) + sStart.length(); int iEnd = res.indexOf(sEnd, iStart); String sC = res.substring(iStart, iEnd); int iNum = parseInt(sC) + 2; String sNum = "" + iNum; // System.out.println(sNum); String sNid = sC.substring(sNum.length() + 1, sC.indexOf("+")); // System.out.println(sNid); String sKey = "" + (parseInt(sC.substring(sC.indexOf("+") + 1)) - 2); // System.out.println(sKey); cookie = "_U_KEY=" + sKey + ";_U_NUM=" + sNum + ";_U_UID=" + sNid; } private static String getContent(String sUrl) { try { InputStream in; URL url = new URL(sUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Cookie", cookie); connection.connect(); in = connection.getInputStream(); BufferedReader breader = new BufferedReader(new InputStreamReader( in, "GBK")); StringBuffer sb = new StringBuffer(); String str = breader.readLine(); while (str != null) { sb.append(str); str = breader.readLine(); } breader.close(); connection.disconnect(); return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }