darkmagex6

Untitled

Oct 25th, 2020
926
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define BUFFER_SIZE 1024
  6. #define MAIL_FROM_PREFIX_SIZE 10
  7. #define RCPT_TO_PREFIX_SIZE 8
  8.  
  9. void handle_mailfrom(char *line, char *from_user);
  10. void handle_receipt_to(char *line, int *num_recipients, char *recipients[]) ;
  11.  
  12. int main(int argc, char *argv[]) {
  13.  
  14.     int mailfrom_read_in = 0;
  15.     char from_user[1024] = {0};
  16.  
  17.     int num_recipients = 0;
  18.     char *recipients[BUFFER_SIZE] = {0};
  19.  
  20.     char line[BUFFER_SIZE] = {0};
  21.     // read first line
  22.     char *fgets_result = fgets(line, BUFFER_SIZE, stdin);
  23.     // continue reading lines until we have no more
  24.     while (fgets_result != NULL) {
  25.         // just print out the line
  26.         printf("%s", line);
  27.        
  28.         if ( ! mailfrom_read_in ) {
  29.             handle_mailfrom(line, from_user);  
  30.             printf("%s\n", from_user);
  31.             mailfrom_read_in = 1;
  32.         }
  33.         else {
  34.             handle_receipt_to(line, &num_recipients, recipients);
  35.         }
  36.        
  37.         fgets_result = fgets(line, BUFFER_SIZE, stdin);
  38.     }
  39.  
  40.     printf("Num recipients: %d\n", num_recipients);
  41.  
  42.     for (int i =0 ; i < num_recipients; i++) {
  43.  
  44.         printf("%s\n", recipients[i]);
  45.  
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
  51. void handle_mailfrom(char *line, char *from_user){
  52.     // check to see if the first 10 chars are "MAIL FROM:"
  53.     int r = strncmp(line, "MAIL FROM:", MAIL_FROM_PREFIX_SIZE);
  54.     if (r==0) {
  55.         // read in "MAIL FROM", handle appropriately
  56.         // copy just the part of the line after "MAIL FROM:"
  57.         strcpy(from_user, line + MAIL_FROM_PREFIX_SIZE );
  58.         // print the user out just as proof we've successfully parsed the line
  59.     }
  60.     else {
  61.         printf("Error, expecting MAIL FROM, exiting...\n");
  62.         exit(-1);
  63.         //handle_receipt_to(line);
  64.     }
  65. }
  66.  
  67. void handle_receipt_to(char *line, int *num_recipients, char *recipients[]) {
  68.     int r = strncmp(line, "RCPT TO:", RCPT_TO_PREFIX_SIZE);
  69.     if (r==0) {
  70.         //printf("--is receipt line--\n");  
  71.  
  72.         int index = *num_recipients;
  73.        
  74.         recipients[index] = (char *)calloc(sizeof(char), BUFFER_SIZE);
  75.         if (recipients[index]==NULL) {
  76.             printf("Error callocing recipients[index], exiting...\n");
  77.             exit(-1);
  78.         }
  79.  
  80.         strcpy(recipients[index], line + RCPT_TO_PREFIX_SIZE);
  81.  
  82.         (*num_recipients)++;
  83.     }
  84.     else {
  85.         printf("Error, expecting RCPT TO, exiting...\n");
  86.         exit(-1);
  87.     }
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
RAW Paste Data