Advertisement
Guest User

centip3de's HTS post checker

a guest
Apr 19th, 2012
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.61 KB | None | 0 0
  1. #include <stdio.h> /* General C header, needed for scanf, and printf */
  2. #include <stdlib.h> /* Needed for exit() */
  3. #include <string.h> /* Needed for strcpy, and strcat */
  4. #include <time.h> /* Needed for our wait() function */
  5. #include <curl/curl.h> /* The CURL include */
  6.  
  7. /*PROTOTYPES */
  8. /* 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
  9.  * 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
  10.  * preferance, really. */
  11. void write(const char * path, const char * phrase); /* Prototype for 'write' */
  12. void search(const char * word, const char * path, const char * cS); /* Prototype for 'search' */
  13. void parse(const char * reading, const char * writing, const char * phrase); /* Prototype for 'parse' */
  14. void postCheck(); /* Prototype for 'postCheck' */
  15. void wait(); /* Prototype for 'wait' */
  16.  
  17. /* GLOBAL VARIALBE */
  18. int searchReturn = 0; /* Whether our search was true or not */
  19. int postCount = 0; /* How many posts we have */
  20. int original = 0; /* If this is the first time lanuched */
  21. int upExists = 0; /* If the User/Pass exists... */
  22. char posts[256]; /* Raw posts */
  23. char username[80]; /* HTS Username */
  24. char password[80]; /* HTS Password */
  25. char data[256]; /* Combining both the username, and pass, into a POST header for logging in */
  26.  
  27. int main(void) /* Our main function */
  28. {
  29.     if(original == 0) /* If you just lanuched the program, otherwise there's no need to re-enter information */
  30.     {  
  31.         original = 1; /* We no longer just launched the program... */
  32.         FILE * fCheck;
  33.         if((fCheck = fopen("UP.txt", "r")) == NULL) /* If there is no file containing a username and password... */
  34.         {
  35.             /*USER INPUT*/
  36.             printf("Please insert your username: ");
  37.             scanf("%s", (char *)&username); /* Scanf requires that the second argument be a char *, thus the cast */
  38.             printf("Please insert your password: ");
  39.             scanf("%s", (char *)&password);
  40.            
  41.             /*POST DATA CONCATENATION */
  42.             strcpy(data, "username="); /* In-order to concatenate you must have an orginal string, hence why strcpy is first */
  43.             strcat(data, username); /* Combining the inputed username with the current string */
  44.             strcat(data, "&password=");
  45.             strcat(data, password);
  46.             strcat(data, "&btn_submit=Login"); /*Submit button for logging in (Required in the POST header) */
  47.            
  48.             printf("Writing the username, and password to a textfile...\n");
  49.             FILE * upWrite = fopen("UP.txt", "w"); /* Opening and creating the User/Pass file */
  50.             strcat(username, "\n"); /* Adding a newline indicator to it, so that they're both on different lines, for fget */
  51.             fwrite(username, strlen(username), 1, upWrite); /* Writing the username to the file */
  52.             fwrite(password, strlen(password), 1, upWrite); /* Writing the password to the file */
  53.             fclose(upWrite); /* Closing the file, as writing it done */
  54.         }
  55.         else /* If there is a file named 'UP.txt' */
  56.         {
  57.             FILE * upRead = fopen("UP.txt", "r"); /* We're going to open it, for reading */
  58.             char tmpArray[256]; /* Temporary array for fget*/
  59.             while(upRead != NULL && fgets(tmpArray, sizeof(tmpArray), upRead) != NULL) /* While the file is there, and isn't empty... */
  60.             {
  61.                 tmpArray[strcspn ( tmpArray, "\n" )] = '\0'; /* Remember we added that newline, character? Well we need to remove that!
  62.                 ^ So, strcspn finds the first instance of "\n" in tmpArray, and then we're setting that to '\0' or a blank space. */
  63.                 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 */
  64.                 char answer[256]; /* Where the answer will be held */
  65.                 scanf("%s", answer); /* Getting the user input and storing it in 'answer' */
  66.                 if(!strcmp(answer, "Y") || !strcmp(answer, "y")) /* If the answer equals y or Y, then they must mean yes. */
  67.                 {
  68.                     strcpy(username, tmpArray); /* Copying the first line, i.e. the username, to our variable username */
  69.                     fgets(tmpArray, sizeof(tmpArray), upRead); /* Now, we want to get the next line (it has no '\n' in it) */
  70.                     strcpy(password, tmpArray); /* Copying that to our variable password */
  71.                     strcpy(data, "username="); /* In-order to concatenate you must have an orginal string, hence why strcpy is first */
  72.                     strcat(data, username); /* Combining the inputed username with the current string */
  73.                     strcat(data, "&password=");  
  74.                     strcat(data, password);
  75.                     strcat(data, "&btn_submit=Login"); /*Submit button for logging in (Required in the POST header) */
  76.                     break; /* Get me out of this 'while' statment yo! */
  77.                 }
  78.                 if(!strcmp(answer, "N") || !strcmp(answer, "n")) /* If the answer equals 'N' or 'n', then they must mean no. */
  79.                 {
  80.                     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 */
  81.                     main(); /* Call our main function */
  82.                     break; /* ... and get out of the 'while' statement. */
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     /* CURL VARIABLES */
  88.     CURL * easyHandle; /* Creating the handle for CURL */
  89.     FILE *outfile; /* Need an output file for searching/parsing */
  90.     outfile = fopen("outfile.txt", "w"); /* Opening the output file with a 'write' property */
  91.     easyHandle = curl_easy_init(); /* Assining our 'easyHandle' variable to curl_easy_init(), where all our functions are */
  92.    
  93.     /* The meat and potatoes */
  94.     if(easyHandle) /* Essentially, if it's assigned. */    
  95.     {
  96.         /* LOGIN */
  97.         printf("Logging into hackthissite.org as %s...\n", username);
  98.         curl_easy_setopt(easyHandle, CURLOPT_URL, "www.hackthissite.org/user/login"); /* Setting up the URL where the data is posted */
  99.         curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, outfile); /* Writing all the HTML data to our output file, so we can use it in further processing */
  100.         curl_easy_setopt(easyHandle, CURLOPT_POST, 1); /* Setting up the POST header */
  101.         curl_easy_setopt(easyHandle, CURLOPT_POSTFIELDS, data); /* Posting our POST header to the URL above */
  102.         curl_easy_setopt(easyHandle, CURLOPT_COOKIEJAR, "cookie.txt"); /* Making it act a bit more like a browser, and capturing cookies */
  103.         curl_easy_setopt(easyHandle, CURLOPT_REFERER, "http://hackthissite.org/"); /* Bypassing the invalid referer error */
  104.         curl_easy_perform(easyHandle); /* Actually performing everything we set above. Without this command, we would just have formed a POST header, not submitted it. */
  105.        
  106.             /* Login check */
  107.         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.");
  108.         /* ^ 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. */
  109.         if(searchReturn == 0) /* If it was false, then the login was successful */
  110.         {
  111.             /* FORUMS */
  112.             printf("Login was successful! Logging into the forums...\n");
  113.             curl_easy_setopt(easyHandle, CURLOPT_URL, "http://www.hackthissite.org/forums/ucp.php?mode=login"); /* Logging into the forums */
  114.             curl_easy_setopt(easyHandle, CURLOPT_FOLLOWLOCATION, 1); /* HTS automatically directs you to the forums, so we want to allow the re-direction */
  115.             curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, outfile); /* Writing all the HTML data to our output file, so we can use it in further processing */
  116.             curl_easy_perform(easyHandle); /* Once again, preforming all requested operations */
  117.             search("Please login", "outfile.txt", "Error occured when trying to login into the forums!"); /* Checking for forum login failure */
  118.            
  119.             if(searchReturn == 0) /* If it was false, then login was successful */
  120.             {
  121.                 printf("Forum login was successful! Attempting to get posts...\n");
  122.                 curl_easy_setopt(easyHandle, CURLOPT_URL, "http://www.hackthissite.org/forums/search.php?search_id=egosearch"); /*View your posts */
  123.                 curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, outfile); /* Writing 'view posts' data to file */
  124.                 curl_easy_perform(easyHandle); /* Performin all requested operations */
  125.                 printf("Posts were gotten successfully!\n");
  126.                
  127.                 printf("Attempting first parse pass...\n");
  128.                 write("outfile.txt", "topictitle"); /* Calling our write function, to write each instance of 'topictitle' to a new file */
  129.                 printf("First parse pass successful!\n");
  130.                
  131.                 printf("Attempting second parse pass...\n");
  132.                 parse("write.tmp", "posts.tmp", ">"); /* Parsing our written file, and writing the newly parsed information to 'posts.tmp' */
  133.                 printf("Second parse pass successful!\n");
  134.                
  135.                 FILE * temp;
  136.                 if(!(temp = fopen("original.txt", "r"))) /* If an 'original.txt' already exists, there's no need to make a new one*/
  137.                 {
  138.                     printf("Making baseline...\n");
  139.                     parse("write.tmp", "original.txt", ">"); /* Making the baseline */
  140.                     printf("Baseline made successfully!\n");
  141.                 }
  142.                
  143.                 printf("Checking posts...\n");
  144.                 postCheck(); /* Check our posts */
  145.                
  146.                 printf("Checking again in thirty minutes...\n");
  147.                 wait(); /* Waiting for a half an hour */
  148.             }
  149.             curl_easy_cleanup(easyHandle); /* Ending CURL, and closing all connections. */
  150.         }
  151.     }
  152. return 0; /* End the program */
  153. }
  154.  
  155.  
  156. void wait() /* Waits for 30 minutes, before restarting */
  157. {
  158.     clock_t timeCount; /* Making a new clock variable */
  159.     timeCount = clock () + 1800 * CLOCKS_PER_SEC; /* Setting timeCount to however many cycles 1800 seconds is */
  160.     while(clock() < timeCount) /* While the clock is less than the set amount of time */
  161.     { /* Do nothing... i.e. wait */
  162.     }
  163.     remove("posts.tmp"); /* Deleting 'posts.tmp' */
  164.     remove("write.tmp"); /* Deleting 'write.tmp' */
  165.     remove("outfile.tmp"); /* Deleting 'outfile.tmp' */
  166.     main(); /* Now that we're done waiting, we loop back to the start to start over. */
  167. }
  168.  
  169. void postCheck() /* Where we'll check the posts */
  170. {
  171.     FILE * atRfp = fopen("original.txt", "r"); /* Handler for reading our 'original.txt' (Accronym = Another temporary reading file path) */
  172.     FILE * tRfp = fopen("posts.tmp", "r"); /* Handler for reading 'posts.tmp' (Accronym = temporary Reading file path) */
  173.     FILE * yatRfp = fopen("write.tmp", "r"); /* Handler for reading our 'write.tmp' (Accronym = Yet another temporary Reading file path) */
  174.     FILE * yatWfp = fopen("newPost.txt", "w"); /* Handler for WRITING our 'new.tmp' (Accronym = Yet another temporary Writing file path) */
  175.    
  176.     char tmpArray[256]; /* A temp array to hold the lines from our files */
  177.     char tmpArray2[256]; /* A temp array to hold the lines from our files */
  178.     fgets(tmpArray, sizeof(tmpArray), tRfp); /* Getting the first line of the file */
  179.     fgets(tmpArray2, sizeof(tmpArray2), tRfp); /* Getting the second line of the file */
  180.     if(!strcmp(tmpArray, tmpArray2)) /* If the first line equals the second line... */
  181.     {
  182.         printf("New post ond %s!\n", tmpArray); /* Then there was a new post! */
  183.         parse("write.tmp", "original.txt", ">"); /* Because of the new post, our baseline is no longer correct. So, we need to make it again */
  184.     }
  185.     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 */
  186.     {
  187.         while(fgets(tmpArray, sizeof(tmpArray), tRfp) != NULL && fgets(tmpArray2, sizeof(tmpArray2), atRfp) != NULL) /* If both of the files aren't empty... */
  188.         {
  189.             if(strcmp(tmpArray, tmpArray2) == 0) /* If the original and the new aren't equal, a new post must have been made! */
  190.             {
  191.                 printf("New post on %s!\n", tmpArray2);
  192.                 parse("write.tmp", "original.txt", ">"); /* Because of the new post, our baseline is no longer correct. So, we need to make it again */
  193.             }
  194.             char tokStr[LINE_MAX];
  195.             fgets(tokStr, sizeof(tokStr), yatRfp); /* Opening write.tmp for parsing */
  196.             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
  197.                                             parsing to get the name. */
  198.             {
  199.                 char * blah;
  200.                 blah = strstr(tokStr, "topictitle"); /* Getting the line that has 'topictitle' in it */
  201.                 char * fool;
  202.                 fool = strchr(blah+1, '>'); /* Parsing our line for the character AFTER the FIRST '>' */
  203.                 char * bars;
  204.                 bars = strtok(fool+1, "<"); /* Grabbing ALL characters BEFORE the first '<' */
  205.                 if(strstr(tmpArray2, bars)) /* If this is already in original.txt, it's old news, and doesn't need to be restated again */
  206.                 {
  207.                     break; /* So, we're getting out of this bad boy */
  208.                 }  
  209.                 printf("SUCCESS: NEW POST MADE ON %s \n", bars); /* If it isn't in original.txt yet, then it must be new! */
  210.                 strcat(bars, "\n"); /* Adding a newline character to bars */
  211.                 char atmpArray[LINE_MAX];
  212.                 strcpy(atmpArray, bars); /* Copying bars to 'atmpArray' for writing */
  213.                 fwrite(atmpArray, strlen(atmpArray), 1,  yatWfp); /* Writing 'atmpArray' to newPost.txt */
  214.                 parse("write.tmp", "original.txt", ">"); /* Now that our original is wrong, we have to update it */
  215.                 /* New post jazz, down here */         
  216.             }
  217.             else /* If not, then no post was made */
  218.             {
  219.                 printf("No posts right now...\n");
  220.                 break; /* So we break our while statement */
  221.             }
  222.         }
  223.         fclose(yatWfp);
  224.     }
  225. }
  226.  
  227. void parse(const char * reading, const char * writing, const char * phrase)
  228. /* ^ This is where we'll parse the file. We only need the file we're reading from, writing to, and what we're parsing */
  229. {
  230.     FILE *tRfp = fopen(reading, "r"); /* Temporary read file path */
  231.     FILE *tWfp = fopen(writing, "w"); /* Temporary write file path */
  232.    
  233.     char tokStr[LINE_MAX]; /* Random array name, just used to hold our file for parsing. */
  234.     int i = 0; /* Array counter */
  235.     char tmpArray[LINE_MAX]; /* Another temporary array, only needed to hold some things */
  236.     while(tRfp != NULL && fgets(tokStr, sizeof(tokStr), tRfp) != NULL) /* While the file exists, and isn't empty */
  237.     {
  238.         if(strstr(tokStr, phrase)) /* Comparing everything in tokStr with phrase. If it's true, it will grab the entire line */
  239.         {
  240.             if(strstr(tokStr, "unread"))
  241.             {
  242.                 char * foo;
  243.                 foo = strstr(tokStr, "topictitle");
  244.                 char * fool;
  245.                 fool = strchr(foo+1, '>');
  246.                 char * bar;
  247.                 bar = strtok(fool+1, "<");
  248.                 strcat(bar, "\n");
  249.                 strcpy(tmpArray, bar);
  250.             }
  251.             else
  252.             {              
  253.                 char * foo;
  254.                 char * bar;
  255.                 foo = strchr(tokStr, '>');
  256.                 /* ^ When strchr encounters '>' for the first time, it'll stop looking, and put that character and everything AFTER (until the line ends) into foo */
  257.                 bar = strtok(foo+1, "<");
  258.                 /* When strtok first encounters '<' it'll put everything BEFORE that characcter (until the line ends) into bar.
  259.                  * But, because we don't want the extra '>' that foo still contains, we're going one character forward in foo. */
  260.                 strcat(bar, "\n"); /* Appending a '\n', or new line, to our parsed text */
  261.                 strcpy(tmpArray, bar); /* Copying it to tmpArray, for writing */
  262.             }
  263.         }      
  264.         fwrite(tmpArray, sizeof(tmpArray[i]), strlen(tmpArray), tWfp); /* Writing tmpArray, that's the size of tmpArray, and as long as tmpArray, to 'tWfp' */
  265.         i++; /* Adding one to 'i' */
  266.     }
  267.     fclose(tWfp); /* Closing file, after we're done with execution */
  268.     fclose(tRfp); /* Closing file, after we're done with execution */
  269. }
  270.    
  271.  
  272. void write(const char * path, const char * phrase) /*Searches for phrase, and writes each found line to a temporary file.*/
  273. {
  274.     FILE *fp = fopen(path, "r"); /* File path */
  275.     FILE *tFp = fopen("write.tmp", "w"); /* Temporary file path */
  276.    
  277.     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 */
  278.     int i = 0; /* Counter for the posts */
  279.     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 */
  280.     {
  281.         if(strstr(tmpArray, phrase)) /* Comparing tmpArray to our phrase. */
  282.         {
  283.             strcpy(posts, tmpArray); /* Copying what is in our tmpArray, to our global posts array */
  284.             fwrite(posts, sizeof(posts[i]), strlen(posts), tFp);
  285.             /* ^ 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 . */          
  286.             ++i;
  287.         /*  ^ 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 */
  288.         }
  289.     }
  290.     postCount = i; /* Setting our global variable, 'postCount' to our posts */
  291.     if(postCount == 0) /* If you have no posts */
  292.     {
  293.         printf("You have no posts to check!");
  294.         exit(EXIT_FAILURE); /* If you have no posts, there's no reason for this to run. Thus, the exiting out. */
  295.     }
  296.     if(fp != NULL) /* If the file doesn't exist... */
  297.     {
  298.         fclose(fp); /* Close the file 'fp' */
  299.         fclose(tFp); /* Close the file 'tFp' */
  300.     }
  301. }
  302.    
  303. 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. */
  304. {
  305.     FILE *fp = fopen(path,"r"); /* Our filePath (fp) */
  306.     char tmpArray[LINE_MAX]; /* Temporary array, to hold our words */
  307.     while(fp != NULL && fgets(tmpArray, sizeof(tmpArray),fp) != NULL) /* While our filepath is still good, and the file isn't empty.. */
  308.     {
  309.         if (strstr(tmpArray, word)) /* If tmpArray matches the word */
  310.         {
  311.             searchReturn = 1; /* Found the word, so it's true or 1 */
  312.             printf("%s", cS); /* Closing statements. Errors or comments would go here. */
  313.             exit(EXIT_FAILURE); /* It didn't work ,so we exit with a failure */
  314.         }
  315.     }
  316.    
  317.     if(fp != NULL) /* Does not exist in the file */
  318.     {
  319.         fclose(fp); /* Close it. */
  320.         searchReturn = 0;
  321.     }
  322.    
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement