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
/
Guys.cpp
303 lines (257 loc) · 8.43 KB
/
Guys.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "Guys.h"
Guys::Guys() {
versionNumber = 1.0; //quick way to edit version number
}
bool Guys::folderExists(std::string path) {
FILE *pFile;
char buffer [256];
std::string result;
std::string command = "cd " + path + " && pwd";
pFile = popen(command.c_str(), "r");
if (pFile == NULL) {
std::cout << "handle error" << std::endl;
return false;
} else {
while ( !feof(pFile) ) {
fgets (buffer, 256, pFile);
}
pclose (pFile);
}
result = buffer;
if ( result.length() == 6 ) {
return false; //means the command returned junk data and is not working
} else {
size_t found;
found = result.find(path.substr(2)); //substr(2) specifically for ~/ in path finding, either way it would still resolve for an absolute path (right?)
if (found!=std::string::npos) {
return true;
}
}
return false;
}
bool Guys::configFileExists(std::string path) {
FILE *pFile;
char buffer [256];
std::string result;
std::string command = "cd ~/ && pwd";
std::string file;
pFile = popen(command.c_str(), "r");
if (pFile == NULL) {
std::cout << "handle error" << std::endl;
return false;
} else {
while ( !feof(pFile) ) {
fgets (buffer, 256, pFile);
}
pclose (pFile);
}
result = buffer;
file = result + "/.config/" + path;
std::string::iterator it;
for ( it=file.begin() ; it < file.end(); it++ )
if (*it == 10) file.erase(it); //erase return characters
pFile = fopen (file.c_str() , "r");
if (pFile == NULL) {
std::cout << "config file does not exist" << std::endl;
return false;
} else {
//do things to the file?
fclose(pFile); //clos file
return true;
}
return false;
}
void Guys::makeConfigFile(std::string fileName) {
FILE *pFile;
char buffer [256];
std::string result;
std::string command = "cd ~/ && pwd";
std::string file;
pFile = popen(command.c_str(), "r");
if (pFile == NULL) {
std::cout << "handle error" << std::endl;
return;
} else {
while ( !feof(pFile) ) {
fgets (buffer, 256, pFile);
}
pclose (pFile);
}
result = buffer;
file = result + "/.config/" + fileName;
configPath = result + "/.config/guys/"; //accessible to all functions
std::string::iterator it;
for ( it=file.begin() ; it < file.end(); it++ )
if (*it == 10) file.erase(it); //erase return characters
pFile = fopen (file.c_str() , "w");
if (pFile == NULL) {
std::cout << "file creation error" << std::endl;
return;
} else {
//do things to the file?
fclose(pFile); //close file
return;
}
return;
}
void Guys::makeConfigDir(std::string folderName) {
FILE *pFile;
char buffer [256];
std::string result;
std::string command = "cd ~/ && pwd";
std::string file;
pFile = popen(command.c_str(), "r");
if (pFile == NULL) {
std::cout << "handle error" << std::endl;
return;
} else {
while ( !feof(pFile) ) {
fgets (buffer, 256, pFile);
}
pclose (pFile);
}
result = buffer;
file = result + "/.config/" + folderName;
command = "mkdir " + file;
std::string::iterator it;
for ( it=command.begin() ; it < command.end(); it++ )
if (*it == 10) command.erase(it); //erase return characters
pFile = popen (command.c_str() , "r");
if (pFile == NULL) {
std::cout << "process creation error" << std::endl;
return;
} else {
pclose(pFile); //close file
return;
}
return;
}
void Guys::configurationFileCheck() {
if( configFileExists("guys/twitter") ) {
//std::cout << "file exists" << std::endl;
FILE *pFile;
char buffer [100];
std::string result;
std::string command = "cd ~/ && pwd";
std::string file;
pFile = popen(command.c_str(), "r");
if (pFile == NULL) {
std::cout << "handle error" << std::endl;
return;
} else {
while ( !feof(pFile) ) {
fgets (buffer , 100 , pFile);
}
pclose (pFile);
}
result = buffer;
configPath = result + "/.config/guys/"; //accessible to all functions
std::string::iterator it;
for ( it=configPath.begin() ; it < configPath.end(); it++ )
if (*it == 10) configPath.erase(it); //erase return characters
} else {
if( folderExists("~/.config/guys") ) {
std::cout << "creating config files in ~/.config/guys/" << std::endl; //but not the file
makeConfigFile("guys/twitter"); //create the file
configurationFileCheck();
} else {
makeConfigDir("guys");
configurationFileCheck();
}
}
return;
}
int Guys::interpretArguments(int argc, char* argv[]) {
if (argc < 2) { //if no arguments
displayHelp();
return 0;
}
std::ostringstream unformattedArguments;
std::vector <std::string> arguments;
for (int i = 0; i < argc; i++){ //assign each argument to a string array alled "arguments"
unformattedArguments << argv[i];
arguments.push_back(unformattedArguments.str());
unformattedArguments.str("");
}
if (arguments[1] == "-h" || arguments[1] == "--help") {
displayHelp();
return 0;
}
if (arguments[1] == "-v" || arguments[1] == "--version") {
displayHelp();
return 0;
}
//get the users input
std::ostringstream quickPost;
if (arguments[1] == "-t" || arguments[1] == "--twitter") {
Twitter *twitter = new Twitter;
if (argc > 2) { //if the user inputed his post right after putting -t either by accident or on purpouse
for (int argumentNumber=2; argumentNumber<argc-1; argumentNumber++) { //we start at 2 because we want everything posted after the -t and not before
//we stop at argc-1 because the last input must not have a space after it lest we use an extra character for nothing.
quickPost << arguments[argumentNumber];
quickPost << " "; //add a space after each input or else thewordswillbestucktogether
}
quickPost << arguments[argc-1]; //we do this make sure the last input into the stream doesn't have a space at the end that takes up 1 character for nothing!
}
twitter->post(quickPost.str(), configPath);
}
/*
//facebook now has it's own Oauth implementation which we now need to investigate -_-
if (arguments[1] == "-f" || arguments[1] == "--facebook") {
//std::ostringstream quickPost;
Facebook *facebook = new Facebook;
if (argc > 2) { //if the user inputed his post right after putting -f either by accident or on purpouse
for (int argumentNumber=2; argumentNumber<argc-1; argumentNumber++) { //we start at 2 because we want everything posted after the -t and not before
//we stop at argc-1 because the last input must not have a space after it lest we use an extra character for nothing.
quickPost << arguments[argumentNumber];
quickPost << " "; //add a space after each input or else thewordswillbestucktogether
}
quickPost << arguments[argc-1]; //we do this make sure the last input into the stream doesn't have a space at the end that takes up 1 character for nothing!
}
facebook->post(quickPost.str());
}
*/
if (arguments[1] == "-a" || arguments[1] == "--all") {
/* we will do this one laterrr
std::ostringstream quickPost;
Facebook facebook = new facebook;
Twitter twitter = new Twitter;
if (argc > 2) {
for (int argumentNumber=2; argumentNumber<argc-1; argumentNumber++) {
quickPost << arguments[argumentNumber];
quickPost << " ";
}
quickPost << arguments[argc-1];
}
facebook->post(quickPost.str());
twitter->post(quickPost.str());
*/
}
if (arguments[1] == "-g" || arguments[1] == "--twitter-get") {
Twitter *twitter = new Twitter;
twitter->getTimeline(configPath);
}
return 0;
}
int Guys::displayHelp() {
std::cout << "Guys ********************************************" << std::endl;
std::cout << "**Options:" << std::endl;
std::cout << " -h [ --help ] returns this help message" << std::endl;
std::cout << " -v [ --version ] returns program version" << std::endl;
std::cout << " -f [ --facebook ] [disabled] post to facebook" << std::endl;
std::cout << " -t [ --twitter ] post to twitter" << std::endl;
std::cout << " -a [ --all ] [disabled] post to all services" << std::endl;
std::cout << " -g [ --twitter-get ] get twitter timeline" << std::endl;
std::cout << "*************************************************" << std::endl;
std::cout << "**Notes:" << std::endl;
std::cout << "oauth keys are stored under ~/.config/guys/, if you wish" << std::endl;
std::cout << "to reset your keys, simply delete the files inside or the" << std::endl;
std::cout << "the entire guys directory." << std::endl;
std::cout << "*************************************************" << std::endl;
std::cout << "**Version " << versionNumber << std::endl;
return 0;
}
int Guys::displayVersion() {
std::cout << "Guys updates your status, version: "<< versionNumber << std::endl;
return 0;
}