-
Notifications
You must be signed in to change notification settings - Fork 4
/
kotnetcli.py
executable file
·304 lines (248 loc) · 11.8 KB
/
kotnetcli.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
## Dependencies: python-mechanize, python-keyring, curses
## Author: Gijs Timmers: https://github.com/GijsTimmers
## Contributors: Gijs Timmers: https://github.com/GijsTimmers
## Jo Van Bulck: https://github.com/jovanbulck
##
## Licence: GPLv3
##
## kotnetcli is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## kotnetcli 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. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with kotnetcli. If not, see <http://www.gnu.org/licenses/>.
import subprocess ## Om systeemcommando's uit te voeren
import argparse ## Parst argumenten
import platform ## Om te kunnen compileren op Windows
import sys ## Basislib
import os ## Basislib
import communicator ## Voor output op maat
from credentials import Credentials ## Opvragen van nummer en wachtwoord
import worker ## Eigenlijke loginmodule
from pinger import ping ## Checken of we op KUL-net zitten
version = "1.3.0-dev"
def main(co, gebruikersnaam, wachtwoord, actie="inloggen"):
if actie == "inloggen":
#ping(co)
kl = worker.Kotnetlogin(co, gebruikersnaam, wachtwoord)
elif actie == "uitloggen":
#ping(co)
kl = worker.Kotnetloguit(co, gebruikersnaam, wachtwoord)
elif actie == "dummyinloggen":
kl = worker.Dummylogin(co, gebruikersnaam, wachtwoord)
elif actie == "dummyuitloggen":
kl = worker.Dummyloguit(co, gebruikersnaam, wachtwoord)
kl.netlogin()
kl.kuleuven()
kl.gegevensinvoeren()
kl.gegevensopsturen()
kl.tegoeden()
def mainLoginprocedure(co, gebruikersnaam, wachtwoord, dummy=False):
kl = worker.Kotnetlogin(co, gebruikersnaam, wachtwoord)
if dummy == True:
kl = worker.Dummylogin(co, gebruikersnaam, wachtwoord)
## kl remains Kotnetlogin if dummy mode is not activated.
kl.netlogin()
kl.kuleuven()
kl.gegevensinvoeren()
kl.gegevensopsturen()
kl.tegoeden()
def mainLoguitprocedure(co, gebruikersnaam, wachtwoord, dummy=False):
kl = worker.Kotnetloguit(co, gebruikersnaam, wachtwoord)
if dummy == True:
kl = worker.Dummyloguit(co, gebruikersnaam, wachtwoord)
kl.netlogin()
kl.kuleuven()
kl.gegevensinvoeren()
kl.gegevensopsturen()
kl.tegoeden()
def mainForceerLoginprocedure(co, gebruikersnaam, wachtwoord, dummy=False):
kl = worker.Kotnetlogin(co, gebruikersnaam, wachtwoord, afsluiten=False)
## IP van uit te loggen apparaat opzoeken
kl.netlogin()
kl.kuleuven()
kl.gegevensinvoeren()
kl.gegevensopsturen()
if kl.tegoeden() == False:
uitteloggenip = kl.uitteloggenipophalen()
print uitteloggenip
## Ander apparaat uitloggen
kl = worker.Kotnetloguit(co, gebruikersnaam, wachtwoord, uitteloggenip=uitteloggenip)
kl.netlogin()
kl.kuleuven()
kl.gegevensinvoeren()
kl.gegevensopsturen()
kl.tegoeden()
## Conventionele login
kl = worker.Kotnetlogin(co, gebruikersnaam, wachtwoord)
kl.netlogin()
kl.kuleuven()
kl.gegevensinvoeren()
kl.gegevensopsturen()
kl.tegoeden()
## An argument parse action that prints license information
## on stdout and exits
class PrintLicenceAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print "kotnetcli: An easy automated way to log in on KotNet\n"
print "This program is free software: you can redistribute it and/or modify"
print "it under the terms of the GNU General Public License as published by"
print "the Free Software Foundation, either version 3 of the License, or"
print "(at your option) any later version.\n"
print "This program is distributed in the hope that it will be useful,"
print "but WITHOUT ANY WARRANTY; without even the implied warranty of"
print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
print "GNU General Public License for more details.\n"
print "You should have received a copy of the GNU General Public License"
print "along with kotnetcli. If not, see <http://www.gnu.org/licenses/>.\n"
print "Visit the github page (https://github.com/GijsTimmers/kotnetcli) to"
print "view the full source code and to collaborate on the project."
exit(0)
def argumentenParser():
parser = argparse.ArgumentParser(description="Script om in- of uit \
te loggen op KotNet")
parser.add_argument("-v", "--version", action="version", version=version)
parser.add_argument("-l", "--license", action=PrintLicenceAction, nargs=0)
## We create three different groups, whose arguments can't be mixed (using
## the add_mutually_exclusive_group() option. If you enter non-combinable
## options, you'll get an error.
workergroep = parser.add_mutually_exclusive_group()
credentialsgroep = parser.add_mutually_exclusive_group()
communicatorgroep = parser.add_mutually_exclusive_group()
## Then, we create three dests: worker, credentials and communicator.
## The value to each of these dests depends on the flags the user applies.
## If he applies none, each dest will use a default value, set with the
## default parameter in add_argument().
## These two things void the need for complex decision trees.
workergroep.add_argument("-i", "--login",\
help="Logs you in on KotNet (default)",\
action="store_const", dest="worker", const="login", default="login")
workergroep.add_argument("-!", "--force-login",\
help="Logs you out on other IP's, and then in on this one",\
action="store_const", dest="worker", const="force_login")
workergroep.add_argument("-o", "--logout",\
help="Logs you out off KotNet",\
action="store_const", dest="worker", const="logout")
workergroep.add_argument("-1", "--dummy-login",\
help="Performs a dry-run logging in",\
action="store_const", dest="worker", const="dummy_login")
workergroep.add_argument("-0", "--dummy-logout",\
help="Performs a dry-run logging out",\
action="store_const", dest="worker", const="dummy_logout")
credentialsgroep.add_argument("-k", "--keyring",\
help="Makes kotnetcli pick up your credentials from the keyring (default)",\
action="store_const", dest="credentials", const="keyring", \
default="keyring")
credentialsgroep.add_argument("-f", "--forget",\
help="Makes kotnetcli forget your credentials",\
action="store_const", dest="credentials", const="forget")
credentialsgroep.add_argument("-g", "--guest-mode",\
help="Logs you in as a different user without forgetting your \
default credentials",\
action="store_const", dest="credentials", const="guest_mode")
communicatorgroep.add_argument("-c", "--color",\
help="Logs you in using colored text output (default)",\
action="store_const", dest="communicator", const="colortext", \
default="colortext")
"""
communicatorgroep.add_argument("-a", "--android",\
help="Logs you in using the Android login system",\
action="store_const", dest="communicator", const="android")
"""
communicatorgroep.add_argument("-u", "--curses",\
help="Logs you in using curses output",\
action="store_const", dest="communicator", const="curses")
communicatorgroep.add_argument("-t", "--plaintext",\
help="Omits the curses interface by using plaintext output",\
action="store_const", dest="communicator", const="plaintext")
communicatorgroep.add_argument("-d", "--dialog",\
help="Omits the curses interface by using dialog based output",\
action="store_const", dest="communicator", const="dialog")
communicatorgroep.add_argument("-b", "--bubble",\
help="Hides all output except for a bubble notification",\
action="store_const", dest="communicator", const="bubble")
communicatorgroep.add_argument("-s", "--summary",\
help="Hides all output except for a short summary",\
action="store_const", dest="communicator", const="summary")
communicatorgroep.add_argument("-q", "--quiet",\
help="Hides all output",\
action="store_const", dest="communicator", const="quiet")
argumenten = parser.parse_args()
return(argumenten)
def aanstuurderObvArgumenten(argumenten):
############## 1. parse credential-related flags ##############
cr = Credentials()
#print argumenten.__dict__
if argumenten.worker == "dummy_login" or argumenten.worker == "dummy_logout":
print "ik wil credentials ophalen voor spek en bonen"
gebruikersnaam, wachtwoord = cr.dummy()
else:
if argumenten.credentials == "keyring":
print "ik haal de credentials uit de keyring"
gebruikersnaam, wachtwoord = cr.getset()
elif argumenten.credentials == "forget":
print "ik wil vergeten"
cr.forget()
exit(0)
elif argumenten.credentials == "guest_mode":
print "ik wil me anders voordoen dan ik ben"
gebruikersnaam, wachtwoord = cr.guest()
############## 2. switch on communicator-related flags ##############
if argumenten.communicator == "curses":
print "ik wil vloeken"
if os.name == "posix":
co = communicator.CursesCommunicator()
else:
co = communicator.ColoramaCommunicator()
elif argumenten.communicator == "android":
print "ik wou dat ik een robot was"
co = communicator.AndroidCommunicator()
elif argumenten.communicator == "colortext":
print "ik wil vrolijke kleuren"
co = communicator.ColoramaCommunicator()
elif argumenten.communicator == "plaintext":
print "ik wil terug naar de basis"
co = communicator.PlaintextCommunicator()
elif argumenten.communicator == "dialog":
print "ik wil fancy dialogs"
if os.name == "posix":
co = communicator.DialogCommunicator()
else:
co = communicator.ColoramaCommunicator()
elif argumenten.communicator == "bubble":
print "ik wil bellen blazen"
if os.name == "posix":
co = communicator.BubbleCommunicator()
else:
co = communicator.ColoramaCommunicator()
elif argumenten.communicator == "summary":
print "ik wil het mooie in de kleine dingen zien"
co = communicator.SummaryCommunicator()
elif argumenten.communicator == "quiet":
print "ik wil zwijgen"
co = communicator.QuietCommunicator()
############## 3. switch on login-type flags ##############
if argumenten.worker == "login":
print "ik wil inloggen"
mainLoginprocedure(co, gebruikersnaam, wachtwoord)
elif argumenten.worker == "force_login":
print "ik moet en zal inloggen"
mainForceerLoginprocedure(co, gebruikersnaam, wachtwoord)
elif argumenten.worker == "logout":
print "ik wil uitloggen"
mainLoguitprocedure(co, gebruikersnaam, wachtwoord)
elif argumenten.worker == "dummy_login":
print "ik wil inloggen voor spek en bonen"
mainLoginprocedure(co, gebruikersnaam, wachtwoord, dummy=True)
elif argumenten.worker == "dummy_logout":
print "ik wil uitloggen voor spek en bonen"
mainLoguitprocedure(co, gebruikersnaam, wachtwoord, dummy=True)
aanstuurderObvArgumenten(argumentenParser())