-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySTModuleCore.py
executable file
·304 lines (252 loc) · 9.06 KB
/
MySTModuleCore.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
This file is part of MyServerTalks.
MyServerTalks 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 2 of the License, or
(at your option) any later version.
MyServerTalks 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 MyServerTalks; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
'''
import os, sys, platform
from MySTBase import *
from MySTModule import *
from MySTReply import *
''' Extends MySTModule '''
class MySTModuleCore(MySTModule):
def __init__(self, config, user_handler):
self.config = config
self.user_handler = user_handler
# Returns core functions
def getCoreFunctions(self):
return {'env':['user', 'super'], 'escope':['user', 'super'], 'contacts':['super']}
# Execute a internal function
def execute(self, instruct):
result_code = 0
result_msg = 'OK'
# Get the called command
command = instruct.getCommand()
# Get the parameters from current called command
parameters = instruct.getParameters()
# --------------------------------------------------------------------------------------------
# Help list all commands for escope
if (command == 'help'):
# User escope
escope = self.user_handler.getUserEscope(instruct.getContact()).lower()
# Module list
modules = self.config.get('modules')
# commands for this user
commands = []
for module in modules:
values = self.config.get('modules', module)
# status of module
status = values['status'].replace(' ', '')
if (status == 'disabled'): continue
# escopes of this module
escopes = values['escope'].replace(' ', '')
escopes = escopes.split(',')
if escope in escopes:
commands.append(module)
if (len(commands) > 0):
result_code = MySTBase.getSucessCode()
result_msg = 'Commands for you: ' + ', '.join(commands)
else:
result_code = MySTBase.getUnavaibleCode()
result_msg = 'No commands for you. Sorry.'
# --------------------------------------------------------------------------------------------
# Information about MyST
elif (command == 'about'):
info = "MyServerTalks! v%s\n" % MySTBase.getVersion()
info += "Url: www.myservertalks.com\n"
info += "PID: %s\n" % os.getpid()
info += "Operational System: %s\n" % ' '.join(platform.uname())
info += "Local Date/Time: %s\n" % MySTBase.getDateTime()
result_code = MySTBase.getSucessCode()
result_msg = info
# --------------------------------------------------------------------------------------------
# User escope
elif (command == 'escope'):
escope = ''
key = ''
try:
escope = parameters[0]
key = parameters[1].strip()
except Exception:
pass
escope = escope.strip()
escope = escope.lower()
if (escope == ''):
result_code = MySTBase.getSucessCode()
result_msg = 'Your session escope is ' + self.user_handler.getUserEscope(instruct.getContact()).upper()
else:
if (escope == 'super'):
if (key == ''):
result_code = MySTBase.getErrorCode()
result_msg = 'You must to specify you super key'
else:
# Validate the key (secret)
if not self.user_handler.escopeAuth(instruct.getContact(), key):
result_code = MySTBase.getErrorCode()
result_msg = 'Incorret password for SUPER escope'
if escope in ('user', 'super'):
if (result_code == MySTBase.getSucessCode()):
self.user_handler.setUserEscope(instruct.getContact(), escope)
result_msg = 'Escope changed to ' + escope.upper()
else:
result_msg = 'Error while trying to change your user escope.'
else:
result_code = MySTBase.getErrorCode()
result_msg = 'Error. You are trying to scale to a invalid escope.'
# --------------------------------------------------------------------------------------------
# User environment functions
elif (command == 'env'):
cmd = ''
var = ''
val = ''
try:
cmd = parameters[0]
var = parameters[1]
val = parameters[2]
except Exception:
pass
if (cmd == 'set'):
if (var and val):
self.user_handler.setUserEnvironment(instruct.getContact(), var, val)
else:
result_code = MySTBase.getErrorCode()
result_msg = 'Env set must to receive both variable and value'
elif (cmd == 'get'):
if var:
val = self.user_handler.getUserEnvironment(instruct.getContact(), var)
result_code = MySTBase.getSucessCode()
result_msg = val
else:
result_code = MySTBase.getErrorCode()
result_msg = 'Env get must to specify varname'
elif (cmd == 'show'):
env = self.user_handler.getUserEnvironment(instruct.getContact())
result = ''
for v in env:
info = v + ' = "' + env[v] + '"'
result = result + info + "\n"
if (result == ''):
result_code = MySTBase.getUnavaibleCode()
result_msg = 'Environment empty'
else:
result_code = MySTBase.getSucessCode()
result_msg = result
# --------------------------------------------------------------------------------------------
# Contact management functions
# TODO:
# - more details on contact show
# - contacts view user (profile view + session information)
# - contact send [user|all]
elif (command == 'contacts'):
cmd = ''
contact = ''
try:
cmd = parameters[0]
contact = parameters[1]
except Exception:
pass
msg_must_especify = 'You must specify the contact to '
if (cmd == 'show'):
contacts = self.user_handler.getContactList()
if (contacts.count == 0):
result_msg = 'There are no contacts in this server roster'
result_code = MySTBase.getErrorCode()
else:
result_code = MySTBase.getSucessCode()
result_msg = "\n".join(contacts)
elif (cmd == 'add'):
if not contact:
result_code = MySTBase.getErrorCode()
result_msg = msg_must_especify + cmd
else:
self.user_handler.addContact(contact)
result_code = MySTBase.getSucessCode()
result_msg = 'User added with sucess'
elif (cmd == 'remove'):
if not contact:
result_msg = msg_must_especify + cmd
result_code = MySTBase.getErrorCode()
else:
self.user_handler.removeContact(contact)
result_code = MySTBase.getSucessCode()
result_msg = 'User removed with sucess'
else:
result_code = MySTBase.getErrorCode()
result_msg = 'Unknown contacts command'
# --------------------------------------------------------------------------------------------
elif (command == 'pwd'):
result_code = MySTBase.getSucessCode()
current_path = self.user_handler.getUserEnvironment(instruct.getContact(), 'PWD')
if (current_path != ''):
result_msg = current_path
else:
result_msg = sys.path[0]
# --------------------------------------------------------------------------------------------
elif (command == 'chdir'):
dir = ''
try:
dir = parameters[0]
except Exception:
pass
# Return current directory
if (dir == ''):
result_code = MySTBase.getSucessCode()
result_msg = self.user_handler.getUserEnvironment(instruct.getContact(), 'PWD')
else:
# Merge directory with older directory
# dir[0] is the first char of 'dir'
if (dir[0] != '/'):
dir = self.user_handler.getUserEnvironment(instruct.getContact(), 'PWD') + '/' + dir
dir = os.path.normpath(dir).replace('\\\\', '\\')
if os.path.isdir(dir):
self.user_handler.setUserEnvironment(instruct.getContact(), 'PWD', os.path.normpath(dir))
result_code = MySTBase.getSucessCode()
result_msg = os.path.normpath(dir)
else:
result_code = MySTBase.getErrorCode()
result_msg = 'No such directory'
# --------------------------------------------------------------------------------------------
# List current directory files
elif (command == 'list'):
target = ''
try:
target = parameters[0]
except Exception:
pass
if (target == ''):
if (self.user_handler.getUserEnvironment(instruct.getContact(), 'PWD') == ''):
target = '/'
else:
target = self.user_handler.getUserEnvironment(instruct.getContact(), 'PWD')
if not os.path.exists(target):
result_msg = 'No such file or directory'
result_code = MySTBase.getErrorCode()
else:
result_code = MySTBase.getSucessCode()
if os.path.isdir(target):
# list of dirs
dirs = os.listdir(target)
result = []
for dir in dirs:
d = dir
# full path to dir
fullpath = os.path.normpath(target + '/' + dir).replace('\\\\', '\\')
# Change directory apresentation
if os.path.isdir(fullpath):
d = "[" + dir + "]"
result.append(d)
result.sort()
result_msg = "\n".join(result)
else:
result_msg = target
return MySTReply(result_code, result_msg)