Skip to content

Commit e64657b

Browse files
Some interface changes.
1 parent 22431ad commit e64657b

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

pCloudCC/control_tools.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int stop_crypto(){
4242
if (SendCall(STOPCRYPTO, "", &ret, &errm))
4343
std::cout << "Stop Crypto failed. return is " << ret<< " and message is "<<errm << std::endl;
4444
else
45-
std::cout << "Crypto Stoped. "<< std::endl;
45+
std::cout << "Crypto Stopped. "<< std::endl;
4646
free(errm);
4747
}
4848
int finalize(){
@@ -57,7 +57,7 @@ int finalize(){
5757
}
5858
void process_commands()
5959
{
60-
std::cout<< "Supported commands are:" << std::endl << "startcrypto <crypto pass>, stopcrypt, q, quit" << std::endl;
60+
std::cout<< "Supported commands are:" << std::endl << "startcrypto <crypto pass>, stopcrypto, finalize, q, quit" << std::endl;
6161
std::cout<< "> " ;
6262
for (std::string line; std::getline(std::cin, line);) {
6363
if (!line.compare("finalize")) {
@@ -81,12 +81,12 @@ int daemonize(bool do_commands) {
8181
if (pid < 0)
8282
exit(EXIT_FAILURE);
8383
if (pid > 0) {
84-
std::cout << "Demon process crated. Process id is: " << pid << std::endl;
84+
std::cout << "Daemon process crated. Process id is: " << pid << std::endl;
8585
if (do_commands) {
8686
process_commands();
8787
}
8888
else
89-
std::cout << "sudo kill -9 "<<pid<< std::endl<<"To stop it."<< std::endl;
89+
std::cout << "sudo kill -9 "<<pid<< std::endl<<" To stop it."<< std::endl;
9090
exit(EXIT_SUCCESS);
9191
}
9292
umask(0);

pCloudCC/main.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ int main(int argc, char **argv) {
1515
bool daemon = false;
1616
bool commands = false;
1717
bool commands_only = false;
18+
bool newuser = false;
19+
bool passwordsw = false;
20+
bool save_pass = false;
1821

1922
try {
2023
po::options_description desc("Allowed options");
2124
desc.add_options()
2225
("help,h", "produce help message")
2326
("username,u", po::value<std::string>(&username), "pCloud account name")
24-
("password,p", po::value<std::string>(&password), "pCloud account password")
27+
("password,p", po::bool_switch(&passwordsw), "pCloud account password")
2528
("crypto,c", po::value<std::string>(), "Crypto password")
26-
("passascrypto,s", po::value<std::string>(), "Use user password as crypto password also.")
29+
("passascrypto,y", po::value<std::string>(), "Use user password as crypto password also.")
2730
("daemonize,d", po::bool_switch(&daemon), "Daemonize the process.")
2831
("commands ,o", po::bool_switch(&commands), "Parent stays alive and processes commands. ")
2932
("mountpoint,m", po::value<std::string>(), "Mount point where drive to be mounted.")
3033
("commands_only,k", po::bool_switch(&commands_only),"Daemon already started pass only commands")
34+
("newuser,n", po::bool_switch(&newuser), "Switch if this is a new user to be registered.")
35+
("savepassword,s", po::bool_switch(&save_pass), "Save password in database.")
3136
;
3237

3338
po::variables_map vm;
@@ -52,12 +57,15 @@ int main(int argc, char **argv) {
5257
return 0;
5358
}
5459

55-
if ((!vm.count("username")) || (!vm.count("password"))) {
56-
std::cout << "Username and password options are required!!!" << "\n";
60+
if ((!vm.count("username"))) {
61+
std::cout << "Username option is required!!!" << "\n";
5762
return 1;
5863
}
5964
console_client::clibrary::get_lib().username_ = username;
60-
console_client::clibrary::get_lib().password_ = password;
65+
66+
if (passwordsw) {
67+
console_client::clibrary::get_lib().get_pass_from_console();
68+
}
6169

6270
if ((!vm.count("crypto")) && (!vm.count("passascrypto")) ){
6371
console_client::clibrary::get_lib().setup_crypto_ = false;
@@ -71,6 +79,10 @@ int main(int argc, char **argv) {
7179

7280
if (vm.count("mountpoint"))
7381
console_client::clibrary::get_lib().mount_ = vm["mountpoint"].as<std::string>();
82+
83+
console_client::clibrary::get_lib().newuser_ = newuser;
84+
console_client::clibrary::get_lib().save_pass_ = save_pass;
85+
console_client::clibrary::get_lib().daemon_ = daemon;
7486
}
7587
catch(std::exception& e) {
7688
std::cerr << "error: " << e.what() << "\n";
@@ -84,6 +96,8 @@ int main(int argc, char **argv) {
8496
if (daemon)
8597
ct::daemonize(commands);
8698
else {
99+
if (commands)
100+
std::cout << "Option commnads/o ignored." << "\n";
87101
if (!console_client::clibrary::init())
88102
sleep(360000);
89103
}

pCloudCC/pclsync_lib.cpp

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
#include <iostream>
3232

33+
#include <iostream>
34+
#include <string>
35+
#include <termios.h>
36+
#include <unistd.h>
37+
#include <stdio.h>
38+
3339

3440
namespace cc = console_client;
3541
namespace clib = cc::clibrary;
@@ -38,11 +44,6 @@ static clib::pclsync_lib g_lib;
3844

3945
clib::pclsync_lib& clib::get_lib(){return g_lib;}
4046

41-
#include <string>
42-
#include <iostream>
43-
#include <cstdio>
44-
#include <memory>
45-
4647
static std::string exec(const char* cmd) {
4748
boost::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
4849
if (!pipe) return "ERROR";
@@ -56,6 +57,22 @@ static std::string exec(const char* cmd) {
5657
}
5758

5859

60+
void clib::pclsync_lib::get_pass_from_console()
61+
{
62+
if (g_lib.daemon_) {
63+
g_lib.get_out() << "Not able to read password when started as daemon." << std::endl;
64+
exit(1);
65+
}
66+
termios oldt;
67+
tcgetattr(STDIN_FILENO, &oldt);
68+
termios newt = oldt;
69+
newt.c_lflag &= ~ECHO;
70+
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
71+
g_lib.get_out() << "Please, enter password" << std::endl;
72+
getline(std::cin, password_);
73+
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
74+
}
75+
5976
void event_handler(psync_eventtype_t event, psync_eventdata_t eventdata){
6077
if (event<PEVENT_FIRST_USER_EVENT){
6178
if (event&PEVENT_TYPE_FOLDER)
@@ -90,24 +107,53 @@ static void lib_setup_cripto(){
90107
g_lib.crypto_on_ = true;
91108
}
92109

110+
static char const * status2string (uint32_t status){
111+
switch (status){
112+
case PSTATUS_READY: return "READY";
113+
case PSTATUS_DOWNLOADING: return "DOWNLOADING";
114+
case PSTATUS_UPLOADING: return "UPLOADING";
115+
case PSTATUS_DOWNLOADINGANDUPLOADING: return "DOWNLOADINGANDUPLOADING";
116+
case PSTATUS_LOGIN_REQUIRED: return "LOGIN_REQUIRED";
117+
case PSTATUS_BAD_LOGIN_DATA: return "BAD_LOGIN_DATA";
118+
case PSTATUS_BAD_LOGIN_TOKEN : return "BAD_LOGIN_TOKEN";
119+
case PSTATUS_ACCOUNT_FULL: return "ACCOUNT_FULL";
120+
case PSTATUS_DISK_FULL: return "DISK_FULL";
121+
case PSTATUS_PAUSED: return "PAUSED";
122+
case PSTATUS_STOPPED: return "STOPPED";
123+
case PSTATUS_OFFLINE: return "OFFLINE";
124+
case PSTATUS_CONNECTING: return "CONNECTING";
125+
case PSTATUS_SCANNING: return "SCANNING";
126+
case PSTATUS_USER_MISMATCH: return "USER_MISMATCH";
127+
case PSTATUS_ACCOUT_EXPIRED: return "ACCOUT_EXPIRED";
128+
default :return "Unrecognized status";
129+
}
130+
}
131+
93132
static void status_change(pstatus_t* status) {
94133
static int cryptocheck=0;
95134
static int mount_set=0;
96-
g_lib.get_out() << "Down: " << status->downloadstr << "| Up: " << status->uploadstr <<", status is " << status->status << std::endl;
135+
g_lib.get_out() << "Down: " << status->downloadstr << "| Up: " << status->uploadstr <<", status is " << status2string(status->status) << std::endl;
97136
*g_lib.status_ = *status;
98137
if (status->status==PSTATUS_LOGIN_REQUIRED){
99-
psync_set_user_pass(g_lib.get_username().c_str(), g_lib.get_password().c_str(), 1);
138+
psync_set_user_pass(g_lib.get_username().c_str(), g_lib.get_password().c_str(), (int) g_lib.save_pass_);
100139
g_lib.get_out() << "logging in" << std::endl;
101140
}
102141
else if (status->status==PSTATUS_BAD_LOGIN_DATA){
142+
if (!g_lib.newuser_) {
143+
g_lib.get_pass_from_console();
144+
psync_set_user_pass(g_lib.get_username().c_str(), g_lib.get_password().c_str(), (int) g_lib.save_pass_);
145+
}
146+
else {
103147
g_lib.get_out() << "registering" << std::endl;
104148
if (psync_register(g_lib.get_username().c_str(), g_lib.get_password().c_str(),1, NULL)){
105149
g_lib.get_out() << "both login and registration failed" << std::endl;
106150
exit(1);
107151
}
108152
else{
109153
g_lib.get_out() << "registered, logging in" << std::endl;
110-
psync_set_user_pass(g_lib.get_username().c_str(), g_lib.get_password().c_str(), 1);
154+
psync_set_user_pass(g_lib.get_username().c_str(), g_lib.get_password().c_str(), (int) g_lib.save_pass_);
155+
}
156+
111157
}
112158
}
113159
if (status->status==PSTATUS_READY || status->status==PSTATUS_UPLOADING || status->status==PSTATUS_DOWNLOADING || status->status==PSTATUS_DOWNLOADINGANDUPLOADING){
@@ -158,7 +204,7 @@ int clib::init()//std::string& username, std::string& password, std::string* cry
158204

159205
if (username_old){
160206
if (g_lib.username_.compare(username_old) != 0){
161-
g_lib.get_out() << "logged in with user " << username_old <<", not "<< g_lib.username_ <<", unlinking and exiting"<<std::endl;
207+
g_lib.get_out() << "logged in with user " << username_old <<", not "<< g_lib.username_ <<", unlinking"<<std::endl;
162208
psync_unlink();
163209
psync_free(username_old);
164210
return 2;

pCloudCC/pclsync_lib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace console_client {
5656
const std::string& get_password() {return password_;}
5757
const std::string& get_crypto_pass() {return crypto_pass_;};
5858
const std::string& get_mount() {return mount_;}
59+
void get_pass_from_console();
5960

6061

6162
public:
@@ -70,6 +71,9 @@ namespace console_client {
7071
bool setup_crypto_;
7172
bool to_set_mount_;
7273
bool crypto_on_;
74+
bool newuser_;
75+
bool save_pass_;
76+
bool daemon_;
7377
};
7478

7579
int init();//std::string& username, std::string& password, std::string* crypto_pass, int setup_crypto = 1, int usesrypto_userpass = 0);

0 commit comments

Comments
 (0)