1 /**
2  * @file src/megacmdexecuter.h
3  * @brief MEGAcmd: Executer of the commands
4  *
5  * (c) 2013 by Mega Limited, Auckland, New Zealand
6  *
7  * This file is part of the MEGAcmd.
8  *
9  * MEGAcmd is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * @copyright Simplified (2-clause) BSD License.
14  *
15  * You should have received a copy of the license along with this
16  * program.
17  */
18 
19 #ifndef MEGACMDEXECUTER_H
20 #define MEGACMDEXECUTER_H
21 
22 #include "megacmdlogger.h"
23 #include "megacmdsandbox.h"
24 #include "listeners.h"
25 
26 namespace megacmd {
27 class MegaCmdSandbox;
28 class MegaCmdMultiTransferListener;
29 class MegaCmdGlobalTransferListener;
30 
31 class MegaCmdExecuter
32 {
33 private:
34     mega::MegaApi *api;
35     mega::handle cwd;
36     char *session;
37     mega::MegaFileSystemAccess *fsAccessCMD;
38     MegaCMDLogger *loggerCMD;
39     MegaCmdSandbox *sandboxCMD;
40     MegaCmdGlobalTransferListener *globalTransferListener;
41     std::mutex mtxSyncMap;
42     std::mutex mtxWebDavLocations;
43     std::mutex mtxFtpLocations;
44 
45 #ifdef ENABLE_BACKUPS
46     std::recursive_mutex mtxBackupsMap;
47 #endif
48 
49     // login/signup e-mail address
50     std::string login;
51 
52     // signup name
53     std::string name;
54 
55     //delete confirmation
56     std::vector<mega::MegaNode *> nodesToConfirmDelete;
57 
58 
59     std::string getNodePathString(mega::MegaNode *n);
60 
61 public:
62     bool signingup;
63     bool confirming;
64     bool confirmingcancel;
65     // link to confirm
66     std::string link;
67 
68     MegaCmdExecuter(mega::MegaApi *api, MegaCMDLogger *loggerCMD, MegaCmdSandbox *sandboxCMD);
69     ~MegaCmdExecuter();
70 
71     void updateprompt(mega::MegaApi *api = nullptr);
72 
73     // nodes browsing
74     void listtrees();
75     static bool includeIfIsExported(mega::MegaApi* api, mega::MegaNode * n, void *arg);
76     static bool includeIfIsShared(mega::MegaApi* api, mega::MegaNode * n, void *arg);
77     static bool includeIfIsPendingOutShare(mega::MegaApi* api, mega::MegaNode * n, void *arg);
78     static bool includeIfIsSharedOrPendingOutShare(mega::MegaApi* api, mega::MegaNode * n, void *arg);
79     static bool includeIfMatchesPattern(mega::MegaApi* api, mega::MegaNode * n, void *arg);
80     static bool includeIfMatchesCriteria(mega::MegaApi* api, mega::MegaNode * n, void *arg);
81 
82     bool processTree(mega::MegaNode * n, bool(mega::MegaApi *, mega::MegaNode *, void *), void *( arg ));
83 
84     mega::MegaNode* nodebypath(const char* ptr, std::string* user = NULL, std::string* namepart = NULL);
85     std::vector <mega::MegaNode*> * nodesbypath(const char* ptr, bool usepcre, std::string* user = NULL);
86     void getNodesMatching(mega::MegaNode *parentNode, std::deque<std::string> pathParts, std::vector<mega::MegaNode *> *nodesMatching, bool usepcre);
87 
88     std::vector <std::string> * nodesPathsbypath(const char* ptr, bool usepcre, std::string* user = NULL, std::string* namepart = NULL);
89     void getPathsMatching(mega::MegaNode *parentNode, std::deque<std::string> pathParts, std::vector<std::string> *pathsMatching, bool usepcre, std::string pathPrefix = "");
90 
91     void printTreeSuffix(int depth, std::vector<bool> &lastleaf);
92     void dumpNode(mega::MegaNode* n, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, int extended_info, bool showversions = false, int depth = 0, const char* title = NULL);
93     void dumptree(mega::MegaNode* n, bool treelike, std::vector<bool> &lastleaf, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, int recurse, int extended_info, bool showversions = false, int depth = 0, std::string pathRelativeTo = "NULL");
94     void dumpNodeSummaryHeader(const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions);
95     void dumpNodeSummary(mega::MegaNode* n, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, bool humanreadable = false, const char* title = NULL);
96     void dumpTreeSummary(mega::MegaNode* n, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, int recurse, bool show_versions, int depth = 0, bool humanreadable = false, std::string pathRelativeTo = "NULL");
97     mega::MegaContactRequest * getPcrByContact(std::string contactEmail);
98     bool TestCanWriteOnContainingFolder(std::string *path);
99     std::string getDisplayPath(std::string givenPath, mega::MegaNode* n);
100     int dumpListOfExported(mega::MegaNode* n, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, std::string givenPath);
101     void listnodeshares(mega::MegaNode* n, std::string name);
102     void dumpListOfShared(mega::MegaNode* n, std::string givenPath);
103     void dumpListOfAllShared(mega::MegaNode* n, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, std::string givenPath);
104     void dumpListOfPendingShares(mega::MegaNode* n, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, std::string givenPath);
105     std::string getCurrentPath();
106     long long getVersionsSize(mega::MegaNode* n);
107     void getInfoFromFolder(mega::MegaNode *, mega::MegaApi *, long long *nfiles, long long *nfolders, long long *nversions = NULL);
108 
109 
110     //acting
111     void loginWithPassword(char *password);
112     void changePassword(const char *newpassword, std::string pin2fa = "");
113     void actUponGetExtendedAccountDetails(mega::SynchronousRequestListener  *srl, int timeout = -1);
114     bool actUponFetchNodes(mega::MegaApi * api, mega::SynchronousRequestListener  *srl, int timeout = -1);
115     int actUponLogin(mega::SynchronousRequestListener  *srl, int timeout = -1);
116     void actUponLogout(mega::SynchronousRequestListener  *srl, bool deletedSession, int timeout = 0);
117     int actUponCreateFolder(mega::SynchronousRequestListener  *srl, int timeout = 0);
118     int deleteNode(mega::MegaNode *nodeToDelete, mega::MegaApi* api, int recursive, int force = 0);
119     int deleteNodeVersions(mega::MegaNode *nodeToDelete, mega::MegaApi* api, int force = 0);
120     void downloadNode(std::string localPath, mega::MegaApi* api, mega::MegaNode *node, bool background, bool ignorequotawar, int clientID, MegaCmdMultiTransferListener *listener = NULL);
121     void uploadNode(std::string localPath, mega::MegaApi* api, mega::MegaNode *node, std::string newname, bool background, bool ignorequotawarn, int clientID, MegaCmdMultiTransferListener *multiTransferListener = NULL);
122     void exportNode(mega::MegaNode *n, int64_t expireTime, std::string password = std::string(), bool force = false);
123     void disableExport(mega::MegaNode *n);
124     void shareNode(mega::MegaNode *n, std::string with, int level = mega::MegaShare::ACCESS_READ);
125     void disableShare(mega::MegaNode *n, std::string with);
126     void createOrModifyBackup(std::string local, std::string remote, std::string speriod, int numBackups);
127     std::vector<std::string> listpaths(bool usepcre, std::string askedPath = "", bool discardFiles = false);
128     std::vector<std::string> listlocalpathsstartingby(std::string askedPath = "", bool discardFiles = false);
129     std::vector<std::string> getlistusers();
130     std::vector<std::string> getNodeAttrs(std::string nodePath);
131     std::vector<std::string> getUserAttrs();
132     std::vector<std::string> getsessions();
133     std::vector<std::string> getlistfilesfolders(std::string location);
134 
135     void restartsyncs();
136 
137     void executecommand(std::vector<std::string> words, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions);
138 
139     //doomedtodie
140     void syncstat(mega::Sync* sync);
141 //    const char* treestatename(treestate_t ts);
142     bool is_syncable(const char* name);
143     int loadfile(std::string* name, std::string* data);
144     void signup(std::string name, std::string passwd, std::string email);
145     void signupWithPassword(std::string passwd);
146     void confirm(std::string passwd, std::string email, std::string link);
147     void confirmWithPassword(std::string passwd);
148 
149     int makedir(std::string remotepath, bool recursive, mega::MegaNode *parentnode = NULL);
150     bool IsFolder(std::string path);
151     void doDeleteNode(mega::MegaNode *nodeToDelete, mega::MegaApi* api);
152 
153     void confirmDelete();
154     void discardDelete();
155     void confirmDeleteAll();
156     void discardDeleteAll();
157 
158     void printTransfersHeader(const unsigned int PATHSIZE, bool printstate=true);
159     void printTransfer(mega::MegaTransfer *transfer, const unsigned int PATHSIZE, bool printstate=true);
160     void printTransferColumnDisplayer(ColumnDisplayer *cd, mega::MegaTransfer *transfer, bool printstate=true);
161 
162 #ifdef ENABLE_BACKUPS
163 
164     void printBackupHeader(const unsigned int PATHSIZE);
165     void printBackupSummary(int tag, const char *localfolder, const char *remoteparentfolder, std::string status, const unsigned int PATHSIZE);
166     void printBackupHistory(mega::MegaBackup *backup, const char *timeFormat, mega::MegaNode *parentnode, const unsigned int PATHSIZE);
167     void printBackupDetails(mega::MegaBackup *backup, const char *timeFormat);
168     void printBackup(int tag, mega::MegaBackup *backup, const char *timeFormat, const unsigned int PATHSIZE, bool extendedinfo = false, bool showhistory = false, mega::MegaNode *parentnode = NULL);
169     void printBackup(backup_struct *backupstruct, const char *timeFormat, const unsigned int PATHSIZE, bool extendedinfo = false, bool showhistory = false);
170 #endif
171     void printSyncHeader(const unsigned int PATHSIZE, ColumnDisplayer *cd = nullptr);
172     void printSync(int i, std::string key, const char *nodepath, sync_struct * thesync, mega::MegaNode *n, long long nfiles, long long nfolders, const unsigned int PATHSIZE, ColumnDisplayer *cd = nullptr);
173 
174     void doFind(mega::MegaNode* nodeBase, const char *timeFormat, std::map<std::string, int> *clflags, std::map<std::string, std::string> *cloptions, std::string word, int printfileinfo, std::string pattern, bool usepcre, mega::m_time_t minTime, mega::m_time_t maxTime, int64_t minSize, int64_t maxSize);
175 
176     void move(mega::MegaNode *n, std::string destiny);
177     void copyNode(mega::MegaNode *n, std::string destiny, mega::MegaNode *tn, std::string &targetuser, std::string &newname);
178     std::string getLPWD();
179     bool isValidFolder(std::string destiny);
180     bool establishBackup(std::string local, mega::MegaNode *n, int64_t period, std::string periodstring, int numBackups);
181     mega::MegaNode *getBaseNode(std::string thepath, std::string &rest, bool *isrelative = NULL);
182     void getPathParts(std::string path, std::deque<std::string> *c);
183 
184     bool checkAndInformPSA(CmdPetition *inf, bool enforce = false);
185 
186     bool checkNoErrors(int errorCode, std::string message = "");
187     bool checkNoErrors(mega::MegaError *error, std::string message = "");
188 
189     void confirmCancel(const char* confirmlink, const char* pass);
190     bool amIPro();
191 
192     void processPath(std::string path, bool usepcre, bool &firstone, void (*nodeprocessor)(MegaCmdExecuter *, mega::MegaNode *, bool), MegaCmdExecuter *context = NULL);
193     void catFile(mega::MegaNode *n);
194     void printInfoFile(mega::MegaNode *n, bool &firstone, int PATHSIZE);
195 
196 
197 #ifdef HAVE_LIBUV
198     void removeWebdavLocation(mega::MegaNode *n, bool firstone, std::string name = std::string());
199     void addWebdavLocation(mega::MegaNode *n, bool firstone, std::string name = std::string());
200     void removeFtpLocation(mega::MegaNode *n, bool firstone, std::string name = std::string());
201     void addFtpLocation(mega::MegaNode *n, bool firstone, std::string name = std::string());
202 #endif
203     bool printUserAttribute(int a, std::string user, bool onlylist = false);
204     bool setProxy(const std::string &url, const std::string &username, const std::string &password, int proxyType);
205     void fetchNodes(mega::MegaApi *api = nullptr, int clientID = -27);
206 };
207 
208 }//end namespace
209 #endif // MEGACMDEXECUTER_H
210