<commit_before>#include "mailsmtp_oauth2.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "base64.h"
#include "mailsmtp_private.h"

#define SMTP_STRING_SIZE 513

enum {
  XOAUTH2_TYPE_GMAIL,
  XOAUTH2_TYPE_OUTLOOK_COM,
};

static int oauth2_authenticate(mailsmtp * session, int type, const char * auth_user,
    const char * access_token);

LIBETPAN_EXPORT
int mailsmtp_oauth2_authenticate(mailsmtp * session, const char * auth_user,
    const char * access_token)
{
  return oauth2_authenticate(session, XOAUTH2_TYPE_GMAIL, auth_user, access_token);
}

LIBETPAN_EXPORT
int mailsmtp_oauth2_outlook_authenticate(mailsmtp * session, const char * auth_user,
    const char * access_token)
{
  return oauth2_authenticate(session, XOAUTH2_TYPE_OUTLOOK_COM, auth_user, access_token);
}

static int oauth2_authenticate(mailsmtp * session, int type, const char * auth_user,
    const char * access_token)
{
  int r;
  char command[SMTP_STRING_SIZE];
  char * ptr;
  char * full_auth_string;
  char * full_auth_string_b64;
  int auth_user_len;
  int access_token_len;
  int full_auth_string_len;
  int res;
  
  full_auth_string = NULL;
  full_auth_string_b64 = NULL;
  
  /* Build client response string */
  auth_user_len = strlen(auth_user);
  access_token_len = strlen(access_token);
  full_auth_string_len = 5 + auth_user_len + 1 + 12 + access_token_len + 2;
  full_auth_string = malloc(full_auth_string_len + 1);
  if (full_auth_string == NULL) {
    res = MAILSMTP_ERROR_MEMORY;
    goto free;
  }
  
  ptr = memcpy(full_auth_string, "user=", 5);
  ptr = memcpy(ptr + 5, auth_user, auth_user_len);
  ptr = memcpy(ptr + auth_user_len, "\1auth=Bearer ", 13);
  ptr = memcpy(ptr + 13, access_token, access_token_len);
  ptr = memcpy(ptr + access_token_len, "\1\1\0", 3);
  
  /* Convert to base64 */
  full_auth_string_b64 = encode_base64(full_auth_string, full_auth_string_len);
  if (full_auth_string_b64 == NULL) {
    res = MAILSMTP_ERROR_MEMORY;
    goto free;
  }
  
  switch (type) {
    case XOAUTH2_TYPE_GMAIL:
    default:
      snprintf(command, SMTP_STRING_SIZE, "AUTH XOAUTH2 %s\r\n", full_auth_string_b64);
      break;
    case XOAUTH2_TYPE_OUTLOOK_COM:
      snprintf(command, SMTP_STRING_SIZE, "AUTH XOAUTH2\r\n");
      break;
  }
  r = mailsmtp_send_command(session, command);
  if (r == -1) {
    res = MAILSMTP_ERROR_STREAM;
    goto free;
  }
  
  r = mailsmtp_read_response(session);
  switch (r) {
  case 220:
  case 235:
    res = MAILSMTP_NO_ERROR;
    goto free;

  case 334:
    /* AUTH in progress */
    
    switch (type) {
      case XOAUTH2_TYPE_GMAIL:
      default:
        /* There's probably an error, send an empty line as acknowledgement. */
        snprintf(command, SMTP_STRING_SIZE, "\r\n");
        break;
      case XOAUTH2_TYPE_OUTLOOK_COM:
        snprintf(command, SMTP_STRING_SIZE, "%s\r\n", full_auth_string_b64);
        break;
    }
    r = mailsmtp_send_command(session, command);
    if (r == -1) {
      res = MAILSMTP_ERROR_STREAM;
      goto free;
    }
    
    r = mailsmtp_read_response(session);
    switch (r) {
      case 535:
        res = MAILSMTP_ERROR_AUTH_LOGIN;
        goto free;
      
      case 220:
      case 235:
        res = MAILSMTP_NO_ERROR;
        goto free;
        
      default:
        res = MAILSMTP_ERROR_UNEXPECTED_CODE;
        goto free;
    }
    break;

  default:
    res = MAILSMTP_ERROR_UNEXPECTED_CODE;
    goto free;
  }
  
  free:
   free(full_auth_string);
   free(full_auth_string_b64);
   return res;
}<commit_msg>update mailsmtp send command calls outlook & gmail<commit_after>#include "mailsmtp_oauth2.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "base64.h"
#include "mailsmtp_private.h"

#define SMTP_STRING_SIZE 513

enum {
  XOAUTH2_TYPE_GMAIL,
  XOAUTH2_TYPE_OUTLOOK_COM,
};

static int oauth2_authenticate(mailsmtp * session, int type, const char * auth_user,
    const char * access_token);

LIBETPAN_EXPORT
int mailsmtp_oauth2_authenticate(mailsmtp * session, const char * auth_user,
    const char * access_token)
{
  return oauth2_authenticate(session, XOAUTH2_TYPE_GMAIL, auth_user, access_token);
}

LIBETPAN_EXPORT
int mailsmtp_oauth2_outlook_authenticate(mailsmtp * session, const char * auth_user,
    const char * access_token)
{
  return oauth2_authenticate(session, XOAUTH2_TYPE_OUTLOOK_COM, auth_user, access_token);
}

static int oauth2_authenticate(mailsmtp * session, int type, const char * auth_user,
    const char * access_token)
{
  int r;
  char command[SMTP_STRING_SIZE];
  char * ptr;
  char * full_auth_string;
  char * full_auth_string_b64;
  int auth_user_len;
  int access_token_len;
  int full_auth_string_len;
  int res;
  
  full_auth_string = NULL;
  full_auth_string_b64 = NULL;
  
  /* Build client response string */
  auth_user_len = strlen(auth_user);
  access_token_len = strlen(access_token);
  full_auth_string_len = 5 + auth_user_len + 1 + 12 + access_token_len + 2;
  full_auth_string = malloc(full_auth_string_len + 1);
  if (full_auth_string == NULL) {
    res = MAILSMTP_ERROR_MEMORY;
    goto free;
  }
  
  ptr = memcpy(full_auth_string, "user=", 5);
  ptr = memcpy(ptr + 5, auth_user, auth_user_len);
  ptr = memcpy(ptr + auth_user_len, "\1auth=Bearer ", 13);
  ptr = memcpy(ptr + 13, access_token, access_token_len);
  ptr = memcpy(ptr + access_token_len, "\1\1\0", 3);
  
  /* Convert to base64 */
  full_auth_string_b64 = encode_base64(full_auth_string, full_auth_string_len);
  if (full_auth_string_b64 == NULL) {
    res = MAILSMTP_ERROR_MEMORY;
    goto free;
  }
  
  switch (type) {
    case XOAUTH2_TYPE_GMAIL:
    default:
    {
      snprintf(command, SMTP_STRING_SIZE, "AUTH XOAUTH2 ");
      r = mailsmtp_send_command(session, command);
      if (r == -1) {
        res = MAILSMTP_ERROR_STREAM;
        goto free;
      }
      r = mailsmtp_send_command(session, full_auth_string_b64);
      if (r == -1) {
        res = MAILSMTP_ERROR_STREAM;
        goto free;
      }
      snprintf(command, SMTP_STRING_SIZE, "\r\n");
      r = mailsmtp_send_command(session, command);
      if (r == -1) {
        res = MAILSMTP_ERROR_STREAM;
        goto free;
      }
      break;
    }
    case XOAUTH2_TYPE_OUTLOOK_COM:
      snprintf(command, SMTP_STRING_SIZE, "AUTH XOAUTH2\r\n");
      r = mailsmtp_send_command(session, command);
      if (r == -1) {
        res = MAILSMTP_ERROR_STREAM;
        goto free;
      }
      break;
  }
  
  r = mailsmtp_read_response(session);
  switch (r) {
    case 220:
    case 235:
      res = MAILSMTP_NO_ERROR;
      goto free;
      
    case 334:
      /* AUTH in progress */
      
      switch (type) {
        case XOAUTH2_TYPE_GMAIL:
        default:
        {
          /* There's probably an error, send an empty line as acknowledgement. */
          snprintf(command, SMTP_STRING_SIZE, "\r\n");
          r = mailsmtp_send_command(session, command);
          if (r == -1) {
            res = MAILSMTP_ERROR_STREAM;
            goto free;
          }
          break;
        }
        case XOAUTH2_TYPE_OUTLOOK_COM:
        {
          r = mailsmtp_send_command(session, full_auth_string_b64);
          if (r == -1) {
            res = MAILSMTP_ERROR_STREAM;
            goto free;
          }
          snprintf(command, SMTP_STRING_SIZE, "\r\n");
          r = mailsmtp_send_command(session, command);
          if (r == -1) {
            res = MAILSMTP_ERROR_STREAM;
            goto free;
          }
          break;
        }
      }
      
      r = mailsmtp_read_response(session);
      switch (r) {
        case 535:
          res = MAILSMTP_ERROR_AUTH_LOGIN;
          goto free;
          
        case 220:
        case 235:
          res = MAILSMTP_NO_ERROR;
          goto free;
          
        default:
          res = MAILSMTP_ERROR_UNEXPECTED_CODE;
          goto free;
      }
      break;
      
    default:
      res = MAILSMTP_ERROR_UNEXPECTED_CODE;
      goto free;
  }
  
free:
  free(full_auth_string);
  free(full_auth_string_b64);
  return res;
}
{'fraction_non_alphanumeric': 0.09618016672887894, 'fraction_numerical': 0.019783501306457632, 'mean_word_length': 2.802270577105014, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7042005', 'n_tokens_mistral': 3079, 'n_tokens_neox': 2950, 'n_words': 600}