-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
184 lines (157 loc) · 3.94 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* This file is part of GorgZorg, a simple multiplatform CLI network file transfer tool.
* Copyright (C) 2021 Alexandre Albuquerque Arnt
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Source code hosted on: https://github.com/aarnt/gorgzorg
*/
#include <QCoreApplication>
#include <QFileInfo>
#include <iostream>
#include "gorgzorg.h"
#include "argumentlist.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ArgumentList *argList = new ArgumentList(argc, argv);
GorgZorg gz;
QString target;
QString pathToGorg;
QString aux;
if (argList->count() == 1)
{
gz.showHelp();
exit(1);
}
if (argList->getSwitch("-h"))
{
gz.showHelp();
exit(0);
}
else if (argList->getSwitch("--version"))
{
gz.showVersion();
exit(0);
}
aux = argList->getSwitchArg(QLatin1String("-bs"));
if (!aux.isEmpty())
{
bool ok;
int block = aux.toInt(&ok);
if (block > 0)
{
gz.setBlockSize(block);
}
else
{
gz.setBlockSize(4);
}
}
aux = argList->getSwitchArg(QLatin1String("-p"));
if (!aux.isEmpty())
{
bool ok;
int port = aux.toInt(&ok);
if (port <= 0 || port > 65535)
{
std::cout << "ERROR: Valid port numbers are between 1 and 65535!" << std::endl;
exit(1);
}
else
gz.setPort(port);
}
if (argList->getSwitch("-y")) gz.setAlwaysAccept();
if (argList->getSwitch("-q")) gz.setQuitServer();
//Has the user set a directory to copy received files?
if (argList->contains("-d"))
{
aux = argList->getSwitchArg(QLatin1String("-d"));
QFileInfo d(aux);
if (!d.isDir() || !d.exists())
{
std::cout << "ERROR: " << aux.toLatin1().data() << " is not a valid directory!" << std::endl;
exit(1);
}
gz.setZorgPath(aux);
}
if (argList->getSwitch("-v")) gz.setVerbose();
if (argList->contains(QLatin1String("-z")))
{
aux = argList->getSwitchArg(QLatin1String("-z"));
if (!aux.isEmpty())
{
if (!GorgZorg::isValidIP(aux))
{
std::cout << "ERROR: Your are trying to listen on an invalid IPv4 IP!" << std::endl;
exit(1);
}
if (!GorgZorg::isLocalIP(aux))
{
std::cout << "ERROR: GorgZorg can only run on a local network!" << std::endl;
exit(1);
}
}
gz.startServer(aux);
}
else if (argList->contains(QLatin1String("-c")))
{
aux = argList->getSwitchArg("-c");
if (!aux.isEmpty())
{
target=aux;
if (!GorgZorg::isValidIP(aux))
{
std::cout << "ERROR: Your are trying to connect to an invalid IPv4 IP!" << std::endl;
exit(1);
}
if (!GorgZorg::isLocalIP(target))
{
std::cout << "ERROR: GorgZorg can only run on a local network!" << std::endl;
exit(1);
}
}
else
{
std::cout << "ERROR: You should specify an IP to connect to!" << std::endl;
exit(1);
}
//Checks if user wants path to be "tared"
if (argList->getSwitch(QLatin1String("-tar")))
{
gz.setTarContents();
}
//Checks if user wants path to be "ziped"
if (argList->getSwitch(QLatin1String("-zip")))
{
gz.setZipContents();
}
aux = argList->getSwitchArg(QLatin1String("-g"));
if (!aux.isEmpty())
{
if (aux == "." || aux == ".." || aux == "./" || aux == "../")
{
std::cout << "ERROR: This path is not compatible!" << std::endl;
exit(1);
}
pathToGorg=aux;
}
else
{
std::cout << "ERROR: You should specify a filename or path to gorg (send)!" << std::endl;
exit(1);
}
if (!target.isEmpty() && !pathToGorg.isEmpty())
{
gz.connectAndSend(target, pathToGorg);
}
}
else //If user did not set neither '-z' or '-c' params, let's print app help
{
gz.showHelp();
exit(0);
}
return a.exec();
}