Пример #1
0
/* Sets the default config */
void set_defaults()
{

  struct stat st;
  char cwd[PATH_MAX];
  /*
   * Zero the structure which holds the config data 
   */
  memset(&rt, 0, sizeof(rt));

  if (getcwd(cwd, sizeof(cwd)) == NULL){
    proz_debug("Failed to get the current working directory");
    strcpy(cwd, ".");
  }

  if (rt.home_dir != 0)
    free(rt.home_dir);
  rt.home_dir = strdup(libprozrtinfo.home_dir);

  /*TODO  what to do if the homedir is NULL */
  if (rt.config_dir != 0)
    free(rt.config_dir);
  rt.config_dir = (char *) malloc(PATH_MAX);
  snprintf(rt.config_dir, PATH_MAX, "%s/%s", rt.home_dir, PRZCONFDIR);
  /* Make the ~/.prozilla directory if necessary */

  if (stat(rt.config_dir, &st) == -1){
    /*error has hapenned */
    if (errno == ENOENT){
      /*Create the dir then */
      if (mkdir(rt.config_dir, S_IRWXU) != 0){
		  perror(_("unable to create the directory to store the config info in"));
		  exit(0);
      }
    } else
      perror(_("Error while stating the config info directory"));
  }

  /*Output the file to the directory , cwd by default */

  if (rt.output_dir != 0)
    free(rt.output_dir);
  rt.output_dir = strdup(cwd);

  if (rt.dl_dir != 0)
    free(rt.dl_dir);
  rt.dl_dir = strdup(cwd);

  if (rt.logfile_dir != 0)
    free(rt.logfile_dir);
  rt.logfile_dir = strdup(rt.config_dir);


  /*
   * The default no of connections and maximum redirections allowed 
   */
  rt.num_connections = 4;
  rt.max_redirections = 10;
  /* Uses PASV by default 
   */
  rt.ftp_use_pasv = libprozrtinfo.ftp_use_pasv;
  /*
   * The force option, off by default when enabled 
   * cause Prozilla not to prompt the user about overwriting existent
   * files etc..
   */
  rt.force_mode = FALSE;
  /*
   * .netrc options 
   */
  rt.use_netrc = TRUE;


  /*
   * The max number of trys and the delay between each 
   */
  rt.max_attempts = 0; /* TODO : It is currently unlimited */
  rt.retry_delay = 15;	/* delay in seconds */

  /*Default is to not log any debug info */
  rt.debug_mode = FALSE;
  rt.quiet_mode = TRUE;
  rt.libdebug_mode = TRUE;
  rt.ftp_search = FALSE;
  rt.min_search_size=128;
  rt.max_simul_pings = 5;
  rt.max_ping_wait = 8;
  rt.ftps_mirror_req_n = 40;

  rt.max_bps_per_dl = 0;	/* 0= No throttling */
 

  if (rt.http_proxy != 0)
    free(rt.http_proxy);
  rt.http_proxy = (proxy_info *) malloc(sizeof(proxy_info));
  rt.http_proxy->username = strdup("");
  rt.http_proxy->passwd = strdup("");
  rt.http_proxy->type = HTTPPROXY;
  proz_parse_url("localhost:3128", &rt.http_proxy->proxy_url, 0);
  rt.use_http_proxy = FALSE;

  if (rt.ftp_proxy != 0)
    free(rt.ftp_proxy);
  rt.ftp_proxy = (proxy_info *) malloc(sizeof(proxy_info));
  rt.ftp_proxy->username = strdup("");
  rt.ftp_proxy->passwd = strdup("");
  rt.ftp_proxy->type = HTTPPROXY;
  proz_parse_url("localhost:3128", &rt.ftp_proxy->proxy_url, 0);
  rt.use_ftp_proxy = FALSE;
  rt.http_no_cache = FALSE;
  rt.timeout.tv_sec = 90;
  rt.timeout.tv_usec = 0;
  rt.ftpsearch_server_id = 1;
  /*Set the values necessary for libprozilla */
  rt.display_mode = DISP_STDOUT;
  set_runtime_values();

  return;
}
Пример #2
0
int
main (int argc, char **argv)
{
	int c;
	int ret;
	proz_init (argc, argv);	//init libprozilla
	set_defaults ();	//set some reasonable defaults
	load_prefs ();		//load values from the config file

	while ((c =
		getopt_long (argc, argv, "?hvrfk:1Lt:VgsP:", long_opts,
			     NULL)) != EOF)
	{
		switch (c)
		{
		case 'L':
			license ();
			exit (0);
		case 'h':
			help ();
			exit (0);
		case 'V':
			version ();
			exit (0);
		case 'r':
			rt.resume_mode = RESUME;
			break;
		case 'f':
			rt.force_mode = TRUE;
			break;
		case 'k':
			if (setargval (optarg, &rt.num_connections) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the -k option\n" "Please type proz --help for help\n"));
				exit (0);
			}

			if (rt.num_connections == 0)
			{
				printf (_("Hey! How can I download anything with 0 (Zero)" " connections!?\n" "Please type proz --help for help\n"));
				exit (0);
			}

			break;
		case 't':
			if (setargval (optarg, &rt.max_attempts) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the -t or --tries option(s)\n" "Please type proz --help for help\n"));
				exit (0);
			}
			break;
		case 'n':
			/*
			 * Don't use ~/.netrc" 
			 */
			rt.use_netrc = FALSE;
			break;

		case 'P':
			/*
			 * Save the downloaded file to DIR 
			 */
			rt.output_dir = kstrdup (optarg);
			break;
		case '?':
			help ();
			exit (0);
			break;
		case '1':
			rt.num_connections = 1;
			break;

		case 'g':
			/*
			 * TODO solve this soon 
			 */
			printf ("Error: GTK interface is not supported in "
				"the development version currently\n");
			exit (0);
			break;

		case 129:
			/*
			 * lets use PORT as the default then 
			 */
			rt.ftp_use_pasv = FALSE;
			break;
		case 130:
			/*
			 * retry-delay option 
			 */
			if (setargval (optarg, &rt.retry_delay) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --retry-delay option\n" "Please type proz --help for help\n"));
				exit (0);
			}
			break;
		case 131:
	    /*--timout option */
			if (setargval (optarg, &rt.itimeout) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --timeout option\n" "Please type proz --help for help\n"));
				exit (0);
			}
			break;
		case 132:
			/* --no-getch option */
			rt.dont_prompt = TRUE;
			break;

		case 133:
			/* --debug option */
			rt.debug_mode = TRUE;
			rt.libdebug_mode=TRUE;
			break;

		case 'v':
			/* --verbose option */
			rt.quiet_mode = FALSE;
			break;

		case 's':
			/* --ftpsearch option */
			rt.ftp_search = TRUE;
			break;

		case 135:
			/* --no-search option */
			rt.ftp_search = FALSE;
			break;

		case 136:
			/* --pt option */
			if (setargval (optarg, &rt.max_ping_wait) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --pt option\n" "Please type proz --help for help\n"));
				exit (0);
			}

			if (rt.max_ping_wait == 0)
			{
				printf (_("Hey! Does waiting for a server response for Zero(0)" " seconds make and sense to you!?\n" "Please type proz --help for help\n"));
				exit (0);
			}

			break;
		case 137:
			/* --pao option */
			if (setargval (optarg, &rt.max_simul_pings) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --pao option\n" "Please type proz --help for help\n"));
				exit (0);
			}

			if (rt.max_simul_pings == 0)
			{
				printf (_("Hey you! Will pinging Zero(0) servers at once" " achive anything for me!?\n" "Please type proz --help for help\n"));
				exit (0);
			}

			break;

		case 138:
			/* --max-ftp-servers option */
			if (setargval (optarg, &rt.ftps_mirror_req_n) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --pao option\n" "Please type proz --help for help\n"));
				exit (0);
			}

			if (rt.ftps_mirror_req_n == 0)
			{
				printf (_("Hey! Will requesting Zero(0) servers at once" "from the ftpearch achive anything!?\n" "Please type proz --help for help\n"));
				exit (0);
			}

			break;
		case 139:
			/* --max-bps */
			if (setlongargval (optarg, &rt.max_bps_per_dl) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --max-bps option\n" "Please type proz --help for help\n"));
				exit (0);
			}
			break;
		case 140:
      rt.display_mode = DISP_STDOUT;
      break;
		case 141:
			/* --min-size */
			if (setlongargval (optarg, &rt.min_search_size) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --min-size option\n" "Please type proz --help for help\n"));
				exit (0);
			}
			break;

		case 142:
			/* --ftpsid */
		
			if (setargval (optarg, &rt.ftpsearch_server_id) != 1)
			{
				/*
				 * The call failed  due to a invalid arg
				 */
				printf (_("Error: Invalid arguments for the --ftpsid option\n" "Please type proz --help for help\n"));
				exit (0);
			}

			if (rt.ftpsearch_server_id < 0 || rt.ftpsearch_server_id >1)
			{
				printf (_("The available servers are (0) filesearching.com and (1) ftpsearch.elmundo.es\n" "Please type proz --help for help\n"));
				exit (0);
			}

			break;



		default:
			printf (_("Error: Invalid  option\n"));
			exit (0);
		}
	}

	set_runtime_values ();	//tell libprozilla about any changed settings

	if (optind == argc)
	{
		help ();
	}
	else
	{
		/* Gettext stuff */
		setlocale (LC_ALL, "");
		bindtextdomain (PACKAGE, LOCALEDIR);
		textdomain (PACKAGE);

		/*delete the ~/.prozilla/debug.log file if present at the start of each run */
		proz_debug_delete_log ();

    if (rt.display_mode == DISP_CURSES)
      initCurses();
    
		/* we will now see whether the user has specfied any urls in the command line  and add them */
		for (int i = optind; i < argc; i++)
		{
			uerr_t err;
			urlinfo *url_data;
			url_data = (urlinfo *) malloc (sizeof (urlinfo));
			memset (url_data, 0, sizeof (urlinfo));

			//parses and validates the command-line parm
			err = proz_parse_url (argv[i], url_data, 0);
			if (err != URLOK)
			{
				PrintMessage (_
					("%s does not seem to be a valid URL"),
					argv[optind]);
				proz_debug
					("%s does not seem to be a valid URL",
					 argv[optind]);
				exit (0);
			}

			PrintMessage("Starting.....");
	//In to %s\n",url_data->host);
			// start the download
			ret=open_new_dl_win (url_data, rt.ftp_search);
			/*If the download failed the return -1 */
			if(ret==-1)
			  {
			    free(url_data);
			    delete(dl_win);
			    shutdown();
			    return -1;
			  }
			delete(dl_win);
			free (url_data);
		}
	}

  shutdown();

}