Beispiel #1
0
int main(int ac, char* av[])
{
  gc_error_free GError *local_err = NULL;
  mega_reg_state* state = NULL;
  gc_free gchar* signup_key = NULL;
  mega_session* s;

  tool_init_bare(&ac, &av, "LINK - register a new mega.nz account", entries);

  if (opt_verify && opt_register)
  {
    g_printerr("ERROR: You must specify either --register or --verify option\n");
    return 1;
  }

  if (opt_register)
  {
    if (!opt_name)
    {
      g_printerr("ERROR: You must specify name for your new mega.nz account\n");
      return 1;
    }

    if (!opt_email)
    {
      g_printerr("ERROR: You must specify email for your new mega.nz account\n");
      return 1;
    }

    if (!opt_password)
    {
      g_printerr("ERROR: You must specify password for your new mega.nz account\n");
      return 1;
    }

  }
  else if (opt_verify)
  {
    if (ac != 2)
    {
      g_printerr("ERROR: You must specify signup key and a link from the verification email\n");
      return 1;
    }

    gc_regex_unref GRegex* r = g_regex_new("^(?:https?://mega(?:\\.co)?\\.nz/#confirm)?([a-z0-9_-]{80,150})$", G_REGEX_CASELESS, 0, NULL);
    gc_match_info_unref GMatchInfo* m = NULL;

    g_assert(r != NULL);

    if (!g_regex_match(r, av[1], 0, &m))
    {
      g_printerr("ERROR: Invalid verification link or key: '%s'\n", av[1]);
      return 1;
    }

    signup_key = g_match_info_fetch(m, 1);
    state = unserialize_reg_state(opt_verify);
    if (!state)
    {
      g_printerr("ERROR: Failed to decode registration state parameter, make sure you copied it correctly\n");
      return 1;
    }
  }
  else
  {
    g_printerr("ERROR: You must specify either --register or --verify option\n");
    return 1;
  }

  s = mega_session_new();

  if (opt_register)
  {
    if (!mega_session_register(s, opt_email, opt_password, opt_name, &state, &local_err))
    {
      g_printerr("ERROR: Registration failed: %s\n", local_err ? local_err->message : "Unknown error");
      goto err;
    }

    gc_free gchar* serialized_state = serialize_reg_state(state);

    if (opt_script)
      g_print("%s --verify %s @LINK@\n", av[0], serialized_state);
    else
      g_print(
        "Registration email was sent to %s. To complete registration, you must run:\n\n"
        "  %s --verify %s @LINK@\n\n"
        "(Where @LINK@ is registration link from the 'MEGA Signup' email)\n",
        opt_email,
        g_get_prgname(),
        serialized_state
      );
  }
  
  if (opt_verify)
  {
    if (!mega_session_register_verify(s, state, signup_key, &local_err))
    {
      g_printerr("ERROR: Verification failed: %s\n", local_err ? local_err->message : "Unknown error");
      goto err;
    }

    if (!opt_script)
      g_print("Account registered successfully!\n");
  }

  tool_fini(s);
  return 0;

err:
  tool_fini(s);
  return 1;
}
Beispiel #2
0
int main(int ac, char* av[])
{
  GError *local_err = NULL;
  GRegex *file_regex, *folder_regex;
  gchar* key;
  gchar* handle;
  gint i;

  tool_init_bare(&ac, &av, "- download exported files from mega.co.nz", entries);

  if (!strcmp(opt_path, "-"))
    opt_noprogress = opt_stream = TRUE;

  if (ac < 2)
  {
    g_printerr("ERROR: No links specified for download!\n");
    tool_fini(NULL);
    return 1;
  }

  if (opt_stream && ac != 2)
  {
    g_printerr("ERROR: Can't stream from multiple files!\n");
    tool_fini(NULL);
    return 1;
  }

  // prepare link parsers

  file_regex = g_regex_new("^https?://mega.co.nz/#!([a-z0-9_-]{8})!([a-z0-9_-]{43})$", G_REGEX_CASELESS, 0, NULL);
  g_assert(file_regex != NULL);

  folder_regex = g_regex_new("^https?://mega.co.nz/#F!([a-z0-9_-]{8})!([a-z0-9_-]{22})$", G_REGEX_CASELESS, 0, NULL);
  g_assert(folder_regex != NULL);

  // create session

  s = mega_session_new();

  mega_session_watch_status(s, status_callback, NULL);

  // process links
  for (i = 1; i < ac; i++)
  {
    GMatchInfo* m1 = NULL;
    GMatchInfo* m2 = NULL;
    key = NULL;
    handle = NULL;

    if (g_regex_match(file_regex, av[i], 0, &m1))
    {
      handle = g_match_info_fetch(m1, 1);
      key = g_match_info_fetch(m1, 2);

      // perform download
      if (!mega_session_dl(s, handle, key, opt_stream ? NULL : opt_path, &local_err))
      {
        if (!opt_noprogress)
          g_print("\r" ESC_CLREOL "\n");
        g_printerr("ERROR: Download failed for '%s': %s\n", av[i], local_err->message);
        g_clear_error(&local_err);
      }
      else
      {
        if (!opt_noprogress)
          g_print("\r" ESC_CLREOL "Downloaded %s\n", cur_file);
      }
    }
    else if (g_regex_match(folder_regex, av[i], 0, &m2))
    {
      if (opt_stream)
      {
        g_printerr("ERROR: Can't stream from a directory!\n");
        if (m1)
          g_match_info_unref(m1);
        if (m2)
          g_match_info_unref(m2);
        tool_fini(s);
        return 1;
      }

      handle = g_match_info_fetch(m2, 1);
      key = g_match_info_fetch(m2, 2);

      // perform download
      if (!mega_session_open_exp_folder(s, handle, key, &local_err))
      {
        g_printerr("ERROR: Can't open folder '%s': %s\n", av[i], local_err->message);
        g_clear_error(&local_err);
      }
      else
      {
        GSList* l = mega_session_ls(s, "/", FALSE);
        if (g_slist_length(l) == 1)
        {
          mega_node* root_node = l->data;

          GFile* local_dir = g_file_new_for_path(opt_path);
          if (g_file_query_file_type(local_dir, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL) == G_FILE_TYPE_DIRECTORY)
          {
            dl_sync_dir(root_node, local_dir, root_node->path);
          }
          else
          {
            g_printerr("ERROR: %s must be a directory\n", opt_path);
          }
        }
        else
        {
          g_printerr("ERROR: EXP folder fs has multiple toplevel nodes? Weird!\n");
        }
      }
    }
    else
    {
      g_printerr("WARNING: Skipping invalid Mega download link: %s\n", av[i]);
    }

    if (m1)
      g_match_info_unref(m1);
    if (m2)
      g_match_info_unref(m2);
    g_free(handle);
    g_free(key);
  }

  tool_fini(s);
  return 0;
}