/* ------------------------------------------------------------------------------ */
/* pgpkey-upload.c v1.0 by frank4dd @20060412                                     */
/*                                                                                */
/* This program demonstrates a pgp key file upload using curl by uuencoding       */
/* the file content first.                                                        */
/* ------------------------------------------------------------------------------ */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;
  FILE *fp;
  size_t  len, len2;
  unsigned char *string;
  unsigned char urlstr[BUFSIZ];
  unsigned char buffer[BUFSIZ];
  unsigned char uenc[BUFSIZ];

  if (! (fp = fopen("key.txt", "r")))
     printf("Error cant open key.txt file");

  while (len=fread(buffer,1,sizeof(buffer),fp)) {
    len2 = sizeof(buffer);
    for(;len2--;buffer++) {
      if (*string=='*' || *string=='-' || *string=='.' ||
         '0'<=*string && *string<= '9' || 'A'<=*string && *string<= 'Z' ||
         'a'<=*string && *string<='z' || *string=='_') {
        //printf("%c",*string);
        strcat(uenc,string);
      }
      else if (*string == ' ') {
        //printf("+");
        strcat(uenc,"+");
      }
      else {
        //printf("%%%02X",*string);
        strcat(uenc,"%%%02X");
      }
    }
  }

  printf("192.168.11.101:11371/pks/add?keytext=%s", uenc);
  snprintf(urlstr, BUFSIZ, "192.168.11.101:11371/pks/add?keytext=%s", uenc);

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, urlstr);
    res = curl_easy_perform(curl);
    printf("Return Code:%s\n", res);
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}


