Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* General C header, needed for scanf, and printf */
- #include <stdlib.h> /* Needed for exit() */
- #include <string.h> /* Needed for strcpy, and strcat */
- #include <time.h> /* Needed for our wait() function */
- #include <curl/curl.h> /* The CURL include */
- /*PROTOTYPES */
- /* Since C is a top-down language, the compiler has no idea this exists, until it reaches it. And because our functions we are calling in 'Main' are AFTER 'main()', we have to tell
- * The compiler that they really do exist later on in the code and we aren't just insane. We could avoid these by putting our functions BEFORE 'main()', it's a matter of style and
- * preferance, really. */
- void write(const char * path, const char * phrase); /* Prototype for 'write' */
- void search(const char * word, const char * path, const char * cS); /* Prototype for 'search' */
- void parse(const char * reading, const char * writing, const char * phrase); /* Prototype for 'parse' */
- void postCheck(); /* Prototype for 'postCheck' */
- void wait(); /* Prototype for 'wait' */
- /* GLOBAL VARIALBE */
- int searchReturn = 0; /* Whether our search was true or not */
- int postCount = 0; /* How many posts we have */
- int original = 0; /* If this is the first time lanuched */
- int upExists = 0; /* If the User/Pass exists... */
- char posts[256]; /* Raw posts */
- char username[80]; /* HTS Username */
- char password[80]; /* HTS Password */
- char data[256]; /* Combining both the username, and pass, into a POST header for logging in */
- int main(void) /* Our main function */
- {
- if(original == 0) /* If you just lanuched the program, otherwise there's no need to re-enter information */
- {
- original = 1; /* We no longer just launched the program... */
- FILE * fCheck;
- if((fCheck = fopen("UP.txt", "r")) == NULL) /* If there is no file containing a username and password... */
- {
- /*USER INPUT*/
- printf("Please insert your username: ");
- scanf("%s", (char *)&username); /* Scanf requires that the second argument be a char *, thus the cast */
- printf("Please insert your password: ");
- scanf("%s", (char *)&password);
- /*POST DATA CONCATENATION */
- strcpy(data, "username="); /* In-order to concatenate you must have an orginal string, hence why strcpy is first */
- strcat(data, username); /* Combining the inputed username with the current string */
- strcat(data, "&password=");
- strcat(data, password);
- strcat(data, "&btn_submit=Login"); /*Submit button for logging in (Required in the POST header) */
- printf("Writing the username, and password to a textfile...\n");
- FILE * upWrite = fopen("UP.txt", "w"); /* Opening and creating the User/Pass file */
- strcat(username, "\n"); /* Adding a newline indicator to it, so that they're both on different lines, for fget */
- fwrite(username, strlen(username), 1, upWrite); /* Writing the username to the file */
- fwrite(password, strlen(password), 1, upWrite); /* Writing the password to the file */
- fclose(upWrite); /* Closing the file, as writing it done */
- }
- else /* If there is a file named 'UP.txt' */
- {
- FILE * upRead = fopen("UP.txt", "r"); /* We're going to open it, for reading */
- char tmpArray[256]; /* Temporary array for fget*/
- while(upRead != NULL && fgets(tmpArray, sizeof(tmpArray), upRead) != NULL) /* While the file is there, and isn't empty... */
- {
- tmpArray[strcspn ( tmpArray, "\n" )] = '\0'; /* Remember we added that newline, character? Well we need to remove that!
- ^ So, strcspn finds the first instance of "\n" in tmpArray, and then we're setting that to '\0' or a blank space. */
- printf("Would you like to login with username %s? (Y/N) ", tmpArray); /* Prompt asking if you would like to use the username that you logged into before */
- char answer[256]; /* Where the answer will be held */
- scanf("%s", answer); /* Getting the user input and storing it in 'answer' */
- if(!strcmp(answer, "Y") || !strcmp(answer, "y")) /* If the answer equals y or Y, then they must mean yes. */
- {
- strcpy(username, tmpArray); /* Copying the first line, i.e. the username, to our variable username */
- fgets(tmpArray, sizeof(tmpArray), upRead); /* Now, we want to get the next line (it has no '\n' in it) */
- strcpy(password, tmpArray); /* Copying that to our variable password */
- strcpy(data, "username="); /* In-order to concatenate you must have an orginal string, hence why strcpy is first */
- strcat(data, username); /* Combining the inputed username with the current string */
- strcat(data, "&password=");
- strcat(data, password);
- strcat(data, "&btn_submit=Login"); /*Submit button for logging in (Required in the POST header) */
- break; /* Get me out of this 'while' statment yo! */
- }
- if(!strcmp(answer, "N") || !strcmp(answer, "n")) /* If the answer equals 'N' or 'n', then they must mean no. */
- {
- remove("UP.txt"); /* Then we want to remove our User/Pass text file, as it was either coppied wrong, or they were in a different account */
- main(); /* Call our main function */
- break; /* ... and get out of the 'while' statement. */
- }
- }
- }
- }
- /* CURL VARIABLES */
- CURL * easyHandle; /* Creating the handle for CURL */
- FILE *outfile; /* Need an output file for searching/parsing */
- outfile = fopen("outfile.txt", "w"); /* Opening the output file with a 'write' property */
- easyHandle = curl_easy_init(); /* Assining our 'easyHandle' variable to curl_easy_init(), where all our functions are */
- /* The meat and potatoes */
- if(easyHandle) /* Essentially, if it's assigned. */
- {
- /* LOGIN */
- printf("Logging into hackthissite.org as %s...\n", username);
- curl_easy_setopt(easyHandle, CURLOPT_URL, "www.hackthissite.org/user/login"); /* Setting up the URL where the data is posted */
- curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, outfile); /* Writing all the HTML data to our output file, so we can use it in further processing */
- curl_easy_setopt(easyHandle, CURLOPT_POST, 1); /* Setting up the POST header */
- curl_easy_setopt(easyHandle, CURLOPT_POSTFIELDS, data); /* Posting our POST header to the URL above */
- curl_easy_setopt(easyHandle, CURLOPT_COOKIEJAR, "cookie.txt"); /* Making it act a bit more like a browser, and capturing cookies */
- curl_easy_setopt(easyHandle, CURLOPT_REFERER, "http://hackthissite.org/"); /* Bypassing the invalid referer error */
- curl_easy_perform(easyHandle); /* Actually performing everything we set above. Without this command, we would just have formed a POST header, not submitted it. */
- /* Login check */
- search("Invalid Password", "outfile.txt", "You used an invalid username, or password! You must now sign in at http://www.hackthissite.org/ to solve this issue.");
- /* ^ Checking to see if login was successful, if it wasn't, then this search will turn true because 'Invalid Password' will be displayed in the HTML. */
- if(searchReturn == 0) /* If it was false, then the login was successful */
- {
- /* FORUMS */
- printf("Login was successful! Logging into the forums...\n");
- curl_easy_setopt(easyHandle, CURLOPT_URL, "http://www.hackthissite.org/forums/ucp.php?mode=login"); /* Logging into the forums */
- curl_easy_setopt(easyHandle, CURLOPT_FOLLOWLOCATION, 1); /* HTS automatically directs you to the forums, so we want to allow the re-direction */
- curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, outfile); /* Writing all the HTML data to our output file, so we can use it in further processing */
- curl_easy_perform(easyHandle); /* Once again, preforming all requested operations */
- search("Please login", "outfile.txt", "Error occured when trying to login into the forums!"); /* Checking for forum login failure */
- if(searchReturn == 0) /* If it was false, then login was successful */
- {
- printf("Forum login was successful! Attempting to get posts...\n");
- curl_easy_setopt(easyHandle, CURLOPT_URL, "http://www.hackthissite.org/forums/search.php?search_id=egosearch"); /*View your posts */
- curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, outfile); /* Writing 'view posts' data to file */
- curl_easy_perform(easyHandle); /* Performin all requested operations */
- printf("Posts were gotten successfully!\n");
- printf("Attempting first parse pass...\n");
- write("outfile.txt", "topictitle"); /* Calling our write function, to write each instance of 'topictitle' to a new file */
- printf("First parse pass successful!\n");
- printf("Attempting second parse pass...\n");
- parse("write.tmp", "posts.tmp", ">"); /* Parsing our written file, and writing the newly parsed information to 'posts.tmp' */
- printf("Second parse pass successful!\n");
- FILE * temp;
- if(!(temp = fopen("original.txt", "r"))) /* If an 'original.txt' already exists, there's no need to make a new one*/
- {
- printf("Making baseline...\n");
- parse("write.tmp", "original.txt", ">"); /* Making the baseline */
- printf("Baseline made successfully!\n");
- }
- printf("Checking posts...\n");
- postCheck(); /* Check our posts */
- printf("Checking again in thirty minutes...\n");
- wait(); /* Waiting for a half an hour */
- }
- curl_easy_cleanup(easyHandle); /* Ending CURL, and closing all connections. */
- }
- }
- return 0; /* End the program */
- }
- void wait() /* Waits for 30 minutes, before restarting */
- {
- clock_t timeCount; /* Making a new clock variable */
- timeCount = clock () + 1800 * CLOCKS_PER_SEC; /* Setting timeCount to however many cycles 1800 seconds is */
- while(clock() < timeCount) /* While the clock is less than the set amount of time */
- { /* Do nothing... i.e. wait */
- }
- remove("posts.tmp"); /* Deleting 'posts.tmp' */
- remove("write.tmp"); /* Deleting 'write.tmp' */
- remove("outfile.tmp"); /* Deleting 'outfile.tmp' */
- main(); /* Now that we're done waiting, we loop back to the start to start over. */
- }
- void postCheck() /* Where we'll check the posts */
- {
- FILE * atRfp = fopen("original.txt", "r"); /* Handler for reading our 'original.txt' (Accronym = Another temporary reading file path) */
- FILE * tRfp = fopen("posts.tmp", "r"); /* Handler for reading 'posts.tmp' (Accronym = temporary Reading file path) */
- FILE * yatRfp = fopen("write.tmp", "r"); /* Handler for reading our 'write.tmp' (Accronym = Yet another temporary Reading file path) */
- FILE * yatWfp = fopen("newPost.txt", "w"); /* Handler for WRITING our 'new.tmp' (Accronym = Yet another temporary Writing file path) */
- char tmpArray[256]; /* A temp array to hold the lines from our files */
- char tmpArray2[256]; /* A temp array to hold the lines from our files */
- fgets(tmpArray, sizeof(tmpArray), tRfp); /* Getting the first line of the file */
- fgets(tmpArray2, sizeof(tmpArray2), tRfp); /* Getting the second line of the file */
- if(!strcmp(tmpArray, tmpArray2)) /* If the first line equals the second line... */
- {
- printf("New post ond %s!\n", tmpArray); /* Then there was a new post! */
- parse("write.tmp", "original.txt", ">"); /* Because of the new post, our baseline is no longer correct. So, we need to make it again */
- }
- else /* If the first line doesn't equal the second line, then there may still be a new post if they're out of original order */
- {
- while(fgets(tmpArray, sizeof(tmpArray), tRfp) != NULL && fgets(tmpArray2, sizeof(tmpArray2), atRfp) != NULL) /* If both of the files aren't empty... */
- {
- if(strcmp(tmpArray, tmpArray2) == 0) /* If the original and the new aren't equal, a new post must have been made! */
- {
- printf("New post on %s!\n", tmpArray2);
- parse("write.tmp", "original.txt", ">"); /* Because of the new post, our baseline is no longer correct. So, we need to make it again */
- }
- char tokStr[LINE_MAX];
- fgets(tokStr, sizeof(tokStr), yatRfp); /* Opening write.tmp for parsing */
- if(strstr(tokStr, "unread"))/* When there is an unread post, there is an extra line in the HTML that conatins 'unread' in it. Because of this, it will require extra
- parsing to get the name. */
- {
- char * blah;
- blah = strstr(tokStr, "topictitle"); /* Getting the line that has 'topictitle' in it */
- char * fool;
- fool = strchr(blah+1, '>'); /* Parsing our line for the character AFTER the FIRST '>' */
- char * bars;
- bars = strtok(fool+1, "<"); /* Grabbing ALL characters BEFORE the first '<' */
- if(strstr(tmpArray2, bars)) /* If this is already in original.txt, it's old news, and doesn't need to be restated again */
- {
- break; /* So, we're getting out of this bad boy */
- }
- printf("SUCCESS: NEW POST MADE ON %s \n", bars); /* If it isn't in original.txt yet, then it must be new! */
- strcat(bars, "\n"); /* Adding a newline character to bars */
- char atmpArray[LINE_MAX];
- strcpy(atmpArray, bars); /* Copying bars to 'atmpArray' for writing */
- fwrite(atmpArray, strlen(atmpArray), 1, yatWfp); /* Writing 'atmpArray' to newPost.txt */
- parse("write.tmp", "original.txt", ">"); /* Now that our original is wrong, we have to update it */
- /* New post jazz, down here */
- }
- else /* If not, then no post was made */
- {
- printf("No posts right now...\n");
- break; /* So we break our while statement */
- }
- }
- fclose(yatWfp);
- }
- }
- void parse(const char * reading, const char * writing, const char * phrase)
- /* ^ This is where we'll parse the file. We only need the file we're reading from, writing to, and what we're parsing */
- {
- FILE *tRfp = fopen(reading, "r"); /* Temporary read file path */
- FILE *tWfp = fopen(writing, "w"); /* Temporary write file path */
- char tokStr[LINE_MAX]; /* Random array name, just used to hold our file for parsing. */
- int i = 0; /* Array counter */
- char tmpArray[LINE_MAX]; /* Another temporary array, only needed to hold some things */
- while(tRfp != NULL && fgets(tokStr, sizeof(tokStr), tRfp) != NULL) /* While the file exists, and isn't empty */
- {
- if(strstr(tokStr, phrase)) /* Comparing everything in tokStr with phrase. If it's true, it will grab the entire line */
- {
- if(strstr(tokStr, "unread"))
- {
- char * foo;
- foo = strstr(tokStr, "topictitle");
- char * fool;
- fool = strchr(foo+1, '>');
- char * bar;
- bar = strtok(fool+1, "<");
- strcat(bar, "\n");
- strcpy(tmpArray, bar);
- }
- else
- {
- char * foo;
- char * bar;
- foo = strchr(tokStr, '>');
- /* ^ When strchr encounters '>' for the first time, it'll stop looking, and put that character and everything AFTER (until the line ends) into foo */
- bar = strtok(foo+1, "<");
- /* When strtok first encounters '<' it'll put everything BEFORE that characcter (until the line ends) into bar.
- * But, because we don't want the extra '>' that foo still contains, we're going one character forward in foo. */
- strcat(bar, "\n"); /* Appending a '\n', or new line, to our parsed text */
- strcpy(tmpArray, bar); /* Copying it to tmpArray, for writing */
- }
- }
- fwrite(tmpArray, sizeof(tmpArray[i]), strlen(tmpArray), tWfp); /* Writing tmpArray, that's the size of tmpArray, and as long as tmpArray, to 'tWfp' */
- i++; /* Adding one to 'i' */
- }
- fclose(tWfp); /* Closing file, after we're done with execution */
- fclose(tRfp); /* Closing file, after we're done with execution */
- }
- void write(const char * path, const char * phrase) /*Searches for phrase, and writes each found line to a temporary file.*/
- {
- FILE *fp = fopen(path, "r"); /* File path */
- FILE *tFp = fopen("write.tmp", "w"); /* Temporary file path */
- char tmpArray[LINE_MAX]; /* A temporary array that is needed for our comparison, below. It's as large as a line in a text file is allowed in Linux */
- int i = 0; /* Counter for the posts */
- while(fp != NULL && fgets(tmpArray, sizeof(tmpArray), fp) != NULL) /* If the file does exist and it isn't empty, get the first line of the file and store it in tmpArray */
- {
- if(strstr(tmpArray, phrase)) /* Comparing tmpArray to our phrase. */
- {
- strcpy(posts, tmpArray); /* Copying what is in our tmpArray, to our global posts array */
- fwrite(posts, sizeof(posts[i]), strlen(posts), tFp);
- /* ^ Writing our posts array that is as big as posts array, and contains as much as the posts array. We're writing this to tFp, or write.tmp . */
- ++i;
- /* ^ Each time we find a "topictitle", it must mean another topic. This code will only get executed if we find another "topictitle". So, we add one to get postCount */
- }
- }
- postCount = i; /* Setting our global variable, 'postCount' to our posts */
- if(postCount == 0) /* If you have no posts */
- {
- printf("You have no posts to check!");
- exit(EXIT_FAILURE); /* If you have no posts, there's no reason for this to run. Thus, the exiting out. */
- }
- if(fp != NULL) /* If the file doesn't exist... */
- {
- fclose(fp); /* Close the file 'fp' */
- fclose(tFp); /* Close the file 'tFp' */
- }
- }
- void search(const char * word, const char * path, const char * cS) /* Writing our own search function, to search through our HTML data recieved in the forums. */
- {
- FILE *fp = fopen(path,"r"); /* Our filePath (fp) */
- char tmpArray[LINE_MAX]; /* Temporary array, to hold our words */
- while(fp != NULL && fgets(tmpArray, sizeof(tmpArray),fp) != NULL) /* While our filepath is still good, and the file isn't empty.. */
- {
- if (strstr(tmpArray, word)) /* If tmpArray matches the word */
- {
- searchReturn = 1; /* Found the word, so it's true or 1 */
- printf("%s", cS); /* Closing statements. Errors or comments would go here. */
- exit(EXIT_FAILURE); /* It didn't work ,so we exit with a failure */
- }
- }
- if(fp != NULL) /* Does not exist in the file */
- {
- fclose(fp); /* Close it. */
- searchReturn = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement