package jjvk;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.*;
import javax.net.ssl.HttpsURLConnection;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManagerListener;
/**
*
* @author user
*/
public class JJVK {
HashMap<String, String> cookies = new HashMap<String, String>();
static final String app_id = "2317131";
String remixsid = "";
String user_email;
String user_pass;
String access_token = "";
String user_id = "";
String expires = "";
private boolean logged;
public JJVK(String user_email, String user_pass) {
this.user_email = user_email;
this.user_pass = user_pass;
logged = false;
}
public String getSid () throws Exception {
HashMap<String, String> data = new HashMap<String, String>();
data.put("email", user_email);
data.put("expire", "");
data.put("pass", user_pass);
data.put("vk","");
data.put("act","login");
data.put("try_to_login", "1");
System.out.println("getSid");
URL siteUrl = new URL("http://login.vk.com/");
HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
HttpURLConnection.setFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
Set keys = data.keySet();
Iterator keyIter = keys.iterator();
String content = "";
for(int i=0; keyIter.hasNext(); i++) {
Object key = keyIter.next();
if(i!=0) {
content += "&";
}
content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
}
//System.out.println(content);
out.writeBytes(content);
out.flush();
out.close();
List<String> c = conn.getHeaderFields().get("Set-Cookie");
if (c != null) {
//cookies.clear();
for (String cookie : c) {
String cookie_str = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie_str.substring(0, cookie_str.indexOf("="));
String cookieValue = cookie_str.substring(cookie.indexOf("=") + 1, cookie_str.length());
cookies.put(cookieName, cookieValue);
//System.out.println("C: "+cookieName+" = "+cookieValue);
}
}
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println(conn.getResponseMessage());
String line = "";
String result = "";
while((line=in.readLine())!=null) {
//System.out.println(line);
result += line;
}
in.close();
// PART 2
Pattern p = Pattern.compile("name=\\'(.*?)\\'\\svalue=\\'(.*?)\\'", Pattern.MULTILINE);
Matcher m = p.matcher(result);
data.clear();
while (m.find()) {
data.put(m.group(1), m.group(2));
}
if (data.isEmpty()) {
System.out.println("Error logging in");
throw new Exception("Error logging in");
}
System.out.println("Sending session data");
result = doSubmit("http://vkontakte.ru/login.php", data);
remixsid = cookies.get("remixsid");
if ((remixsid == null) || (remixsid.equals("")))
throw new Exception("Error getting sid");
System.out.println("remixsid: " + remixsid);
return remixsid;
}
public String doSubmit (String url, HashMap<String, String> data) throws Exception {
System.out.println("Do submit");
URL siteUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
HttpURLConnection.setFollowRedirects(false);
if (!cookies.isEmpty()) {
String cookie = "";
int i = 0;
for(String item : cookies.keySet()) {
if(i!=0) {
cookie += "; ";
}
cookie += item + "=" + cookies.get(item);
i++;
}
conn.setRequestProperty("Cookie", cookie);
}
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
Set keys = data.keySet();
Iterator keyIter = keys.iterator();
String content = "";
for(int i=0; keyIter.hasNext(); i++) {
Object key = keyIter.next();
if(i!=0) {
content += "&";
}
content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
}
//System.out.println(content);
out.writeBytes(content);
out.flush();
out.close();
List<String> c = conn.getHeaderFields().get("Set-Cookie");
if (c != null) {
//cookies.clear();
for (String cookie : c) {
String cookie_str = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie_str.substring(0, cookie_str.indexOf("="));
String cookieValue = cookie_str.substring(cookie.indexOf("=") + 1, cookie_str.length());
cookies.put(cookieName, cookieValue);
//System.out.println("C: "+cookieName+" = "+cookieValue);
}
}
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println(conn.getResponseMessage());
String line = "";
String result = "";
while((line=in.readLine())!=null) {
System.out.println(line);
result += line;
}
in.close();
return result;
}
public String getToken () throws Exception {
HashMap<String, String> data = new HashMap<String, String>();
data.put("client_id",app_id);
data.put("redirect_uri", "http://api.vkontakte.ru/blank.html");
data.put("display", "wap");
data.put("scope", "wall");
data.put("response_type", "token");
String url = "http://api.vk.com/oauth/authorize";
System.out.println("Do get perm");
Set keys = data.keySet();
Iterator keyIter = keys.iterator();
String content = "?";
for(int i=0; keyIter.hasNext(); i++) {
Object key = keyIter.next();
if(i!=0) {
content += "&";
}
content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
}
URL siteUrl = new URL(url+content);
HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
HttpURLConnection.setFollowRedirects(false);
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
if (!cookies.isEmpty()) {
String cookie = "";
int i = 0;
for(String item : cookies.keySet()) {
if(i!=0) {
cookie += "; ";
}
cookie += item + "=" + cookies.get(item);
i++;
}
conn.setRequestProperty("Cookie", cookie);
}
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
System.out.println(content);
// out.writeBytes(content);
out.flush();
out.close();
List<String> c = conn.getHeaderFields().get("Set-Cookie");
if (c != null) {
//cookies.clear();
for (String cookie : c) {
String cookie_str = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie_str.substring(0, cookie_str.indexOf("="));
String cookieValue = cookie_str.substring(cookie.indexOf("=") + 1, cookie_str.length());
cookies.put(cookieName, cookieValue);
//System.out.println("C: "+cookieName+" = "+cookieValue);
}
}
Map<String,List<String>> h = conn.getHeaderFields();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println(conn.getResponseMessage());
String line = "";
String result = "";
boolean secure = false;
if (conn.getResponseCode() == 302) {
List<String> loc = conn.getHeaderFields().get("Location");
if (loc != null)
for (String item : loc)
result += item;
result = result.substring(result.indexOf("/oauth"));
System.out.println("302: "+result);
in.close();
secure = true;
//throw new Exception ("Invalid application type");
} else {
while((line=in.readLine())!=null) {
System.out.println(line);
result += line;
}
String parameters = result.substring(result.indexOf("/oauth"));
parameters = parameters.substring(0, parameters.indexOf("\">"));
System.out.println("200 :" + parameters);
in.close();
result = parameters;
}
System.out.println("Location: "+result);
if (!secure)
result = doGetAuth("http://api.vk.com"+result, secure);
else
result = doGetAuth("https://api.vk.com"+result, secure);
String parameters = result.substring(result.indexOf('#')+1);
System.out.println(parameters);
String[] pairs = parameters.split("&");
data.clear();
for (String pair : pairs) {
String[] key_value = pair.split("=");
data.put(key_value[0], key_value[1]);
}
user_id = data.get("user_id");
access_token = data.get("access_token");
if ((access_token == null) || (access_token.equals("")))
throw new Exception("Invalid token");
return result;
}
public String doWallPost (String message) throws Exception {
//String result = "";
System.out.println("Do wall post");
String content = "?";
String url = "https://api.vkontakte.ru/method/";
String format = "JSON";
String method = "wall.post";
url = url + method+"?" + "message=" + URLEncoder.encode(message, "UTF-8") + "&" + "access_token="+access_token;
URL siteUrl = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) siteUrl.openConnection();
HttpsURLConnection.setFollowRedirects(false);
if (!cookies.isEmpty()) {
String cookie = "";
int i = 0;
for(String item : cookies.keySet()) {
if(i!=0) {
cookie += "; ";
}
cookie += item + "=" + cookies.get(item);
i++;
}
conn.setRequestProperty("Cookie", cookie);
}
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
System.out.println(content);
// out.writeBytes(content);
out.flush();
out.close();
// List<String> c = conn.getHeaderFields().get("Set-Cookie");
// if (c != null) {
// //cookies.clear();
// for (String cookie : c) {
// String cookie_str = cookie.substring(0, cookie.indexOf(";"));
// String cookieName = cookie_str.substring(0, cookie_str.indexOf("="));
// String cookieValue = cookie_str.substring(cookie.indexOf("=") + 1, cookie_str.length());
// cookies.put(cookieName, cookieValue);
// //System.out.println("C: "+cookieName+" = "+cookieValue);
// }
// }
// Map<String,List<String>> h = conn.getHeaderFields();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println(conn.getResponseMessage());
String line = "";
String result = "";
while((line=in.readLine())!=null) {
System.out.println(line);
result += line;
}
in.close();
return result;
}
public String doGetAuth (String url, boolean secure) throws Exception {
System.out.println("Do auth");
URL siteUrl = new URL(url);
HttpURLConnection conn;
if (secure) {
conn = (HttpsURLConnection) siteUrl.openConnection();
HttpsURLConnection.setFollowRedirects(false);
} else
{
conn = (HttpURLConnection) siteUrl.openConnection();
HttpURLConnection.setFollowRedirects(false);
}
if (!cookies.isEmpty()) {
String cookie = "";
int i = 0;
for(String item : cookies.keySet()) {
if(i!=0) {
cookie += "; ";
}
cookie += item + "=" + cookies.get(item);
i++;
}
conn.setRequestProperty("Cookie", cookie);
}
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
// out.writeBytes(content);
out.flush();
out.close();
List<String> c = conn.getHeaderFields().get("Set-Cookie");
if (c != null) {
//cookies.clear();
for (String cookie : c) {
String cookie_str = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie_str.substring(0, cookie_str.indexOf("="));
String cookieValue = cookie_str.substring(cookie.indexOf("=") + 1, cookie_str.length());
cookies.put(cookieName, cookieValue);
//System.out.println("C: "+cookieName+" = "+cookieValue);
}
}
Map<String,List<String>> h = conn.getHeaderFields();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println(conn.getResponseMessage());
String result = "";
List<String> loc = conn.getHeaderFields().get("Location");
if (loc != null)
for (String item : loc)
result += item;
in.close();
return result;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JJVK vk = new JJVK("vkontakte_login","vkontakte_password");
HashMap<String, String> data = new HashMap<String, String>();
try {
String result = "";
vk.getSid();
vk.getToken();
// result = vk.doWallPost("test2");
} catch (Exception e) {
System.out.println("Error");
}
if (vk.access_token.isEmpty()) {
System.out.println("Couldn't get token; exiting");
return;
}
final SmackApi c = new SmackApi(vk, "your_jid");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String msg;
// turn on the enhanced debugger
//XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
try {
c.login("bot_login", "bot_password", "bot_server");
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
System.exit(1);
}
ServerSocket socket;
try {
socket = new ServerSocket(5555);
} catch (IOException ex) {
System.out.println("Open socket error");
return;
}
//c.displayBuddyList();
c.connection.getChatManager().addChatListener(
new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
if (!createdLocally)
chat.addMessageListener(c);
}
}
);
Boolean running = true;
while(running) {
try {
Socket s = socket.accept();
running = false;
} catch (IOException ex) {
System.out.println("Create client error");
running = false;
}
}
}
}