This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Facebook.cpp
126 lines (98 loc) · 3.78 KB
/
Facebook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Facebook.h"
Facebook::Facebook(){
FirstName = "";
Email = "";
Password = "";
if (Email.length() == 0){
std::cout << "you can not use Facebook, no login data specified" << std::endl;
return;
}
curl = curl_easy_init();
}
// This is the writer call back function used by curl
static int facebookWriter(char *data, size_t size, size_t nmemb, std::string *buffer){
// What we will return
int result = 0;
// Is there anything in the buffer?
if (buffer != NULL){
// Append the data to the buffer
buffer->append(data, size * nmemb);
// How much did we write?
result = size * nmemb;
}
return result;
}
void Facebook::post(std::string message){
if (message.size() <= 0) { //if there is no quick post
std::cout << "**your Post: ";
getline (std::cin, message); //not sure if this works
} else { //if there is a quick post
std::cout << "**your Post: " << message << std::endl;
}
LoginPostEmail = "email=";
LoginPostPassword = "&pass=";
LoginPostLogin = "&login=Login";
finalURLp1 = "http://m.facebook.com/";
postFormId = "post_form_id=";
postStatus = "&status=";
postUpdate = "&update=Update Status";
//create the login post message
LoginPost= LoginPostEmail + Email + LoginPostPassword + Password + LoginPostLogin;
//login to facebook mobile
curl_easy_setopt(curl, CURLOPT_URL, "https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, LoginPost.c_str() );
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, facebookWriter);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &loginPageBuffer);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_COOKIE, "~/statsU_cookies.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "~/statsU_cookies.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "~/statsU_cookies.txt");
curl_easy_perform(curl);
//get posting form data from the front page
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, facebookWriter);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &frontPageBuffer);
curl_easy_setopt(curl, CURLOPT_URL, "http://m.facebook.com/home.php");
curl_easy_perform(curl);
//find the actionable page
foundFirst = frontPageBuffer.find("form action=\"/");
for(int i = 0; i < 200; i++){
if(frontPageBuffer.at(foundFirst+i+16) == '"' ){
foundLast = foundFirst+i+16;
foundFirst += 14;
i = 201;
}
}
actionURL = frontPageBuffer.substr(foundFirst, foundLast - foundFirst);
//replace the "&" with just "&" in that string
foundFirst = actionURL.find("&");
foundLast = actionURL.find(";") + 1;
actionURL.replace(foundFirst, foundLast - foundFirst, "&");
finalURL = finalURLp1 + actionURL;
foundFirst = frontPageBuffer.find("name=\"post_form_id\" value=\"");
for(int i = 0; i < 200; i++){
if(frontPageBuffer.at(foundFirst+i+28) == '"'){
foundLast = foundFirst+i+28;
foundFirst += 27; //26 is at the "
i = 201;
}
}
formKey = frontPageBuffer.substr(foundFirst, foundLast - foundFirst);
Post = postFormId + formKey + postStatus + message + postUpdate;
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, facebookWriter);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &finalPageBuffer);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Post.c_str() );
curl_easy_setopt(curl, CURLOPT_URL, finalURL.c_str() );
facebookResult = curl_easy_perform(curl);
// Did we succeed?
if (facebookResult == CURLE_OK){
std::cout << "SUCCESS! message sent to Facebook! (took it's sweet time though...)" << std::endl;
return;
}else {
std::cout << "OUPS! there was an error. The message was not sent to Twitter." << std::endl;
return;
}
}