forked from Hack42/hackbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kassa.py
executable file
·219 lines (199 loc) · 7.25 KB
/
kassa.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import glob
import importlib
import time
import json
import pickle
import copy
import sys
sessions={}
class log:
pass
class Session:
SID=""
plugins={}
counter=0
nextcall={}
prompt=""
help={}
cache={}
iets=0
def import_from(self, module, name):
module = __import__(module, fromlist=[name])
return getattr(module, name)
def __init__(self,SID,client):
self.SID=SID
self.client=client
self.plugins={}
def startup(self):
print "Startup",self.SID
for fname in glob.glob("plugins/*.py"):
plugname=fname.split("/")[1].rstrip(".py")
if plugname!='__init__' and not plugname in self.plugins:
if plugname in sys.modules:
del sys.modules[plugname]
print self.plugins
for fname in glob.glob("plugins/*.py"):
plugname=fname.split("/")[1].rstrip(".py")
if plugname!='__init__' and not plugname in self.plugins:
if plugname in sys.modules:
del sys.modules[plugname]
self.plugins[plugname]= self.import_from('plugins.'+plugname,plugname)(self.SID,self)
print plugname,self.import_from('plugins.'+plugname,plugname)
self.send_message(True,'message','loaded plugin '+plugname);
try:
self.help.update(self.plugins[plugname].help())
except:
pass
self.receipt=self.plugins['receipt']
self.accounts=self.plugins['accounts']
self.products=self.plugins['products']
self.stock=self.plugins['stock']
self.log=self.plugins['log']
self.POS=self.plugins['POS']
self.counter+=1
self.send_message(False,'message','Please wait while loading...');
for plug in self.plugins:
self.plugins[plug].startup()
self.send_message(True,'commands',json.dumps(self.help))
self.send_message(True,'message','Enter product, command or username');
print self.plugins
def realcallhook(self,hook,arg):
for plug in self.plugins:
try:
getattr(self.plugins[plug], 'hook_'+hook)
try:
getattr(self.plugins[plug], 'hook_'+hook)(arg)
except:
import traceback
print traceback.format_exc()
except AttributeError:
pass
def callhook(self,hook,arg):
self.realcallhook('pre_'+hook,arg);
self.realcallhook(hook,arg);
self.realcallhook('post_'+hook,arg);
return True
def donext(self,plug,function):
self.nextcall={'plug': plug, 'function': function}
def input(self,text):
if text=="":
self.send_message(True,'message',"Enter product, command or username")
self.send_message(True,'buttons',json.dumps({}))
return True
if self.iets==0 and text!="abort":
self.iets==1
self.buttons={}
done=0
for plug in self.plugins:
try:
self.plugins[plug].pre_input(text)
except AttributeError:
pass
except:
import traceback
print traceback.format_exc()
self.prompt=""
if self.nextcall!={}:
try:
print(text)
print(self.nextcall)
plug=self.nextcall['plug']
func=self.nextcall['function']
self.nextcall={}
print(getattr(plug,func))
if getattr(plug,func)(text):
done=1
except:
import traceback
print(traceback.format_exc())
if done==1:
print("Call done with self.nextcall")
elif done==0:
for part in text.split():
if part!='':
done=0
print("Running part",part,self.nextcall)
self.prompt=""
if self.nextcall!={}:
try:
plug=self.nextcall['plug']
func=self.nextcall['function']
self.nextcall={}
if getattr(plug,func)(part):
done=1
except:
import traceback
print(traceback.format_exc())
if done==0:
for plug in self.plugins:
try:
if self.plugins[plug].input(part):
done=1
break;
except AttributeError:
import traceback
print(traceback.format_exc())
pass
except:
import traceback
print(traceback.format_exc())
if done==0:
if self.plugins['withdraw'].withdraw(part):
done=1
if self.plugins['accounts'].newuser(part):
done=1
if done==1 and self.prompt=="":
self.send_message(True,'message',"Enter product, command or username")
elif self.prompt=="":
self.send_message(True,'message',"Unknown product, command or username")
self.callhook('wrong',())
if self.nextcall=={} and self.buttons=={}:
self.send_message(True,'buttons',json.dumps({}))
def send_message(self,retain,topic,message) :
if not 'hack42bar/output/session/'+self.SID+'/'+topic in self.cache or self.cache['hack42bar/output/session/'+self.SID+'/'+topic]!=message or len(topic)<8:
print(retain,'hack42bar/output/session/'+self.SID+'/'+topic,":",message)
self.cache['hack42bar/output/session/'+self.SID+'/'+topic]=message
if topic=="message":
self.prompt=message
if topic=="buttons":
self.buttons=message
self.client.publish('hack42bar/output/session/'+self.SID+'/'+topic,message,1,retain)
def get_session(SID,client):
global sessions
if not SID in sessions:
print "Starting new session",SID
sessions[SID]=Session(SID,client)
sessions[SID].startup()
client.publish('hack42bar/output/sessions',json.dumps(sessions.keys()))
return sessions[SID]
def run_session(client,SID,action,data):
if action=='input':
session=get_session(SID,client)
session.input(data)
else:
print("unhandled",action)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("hack42bar/input/#")
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload))
elms=msg.topic.split("/")
msg=msg.payload
if len(elms)<3: return;
#try:
run_session(client,elms[3],elms[4],msg)
while 1:
try:
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_start()
while True:
time.sleep(1)
#client.loop_forever()
except:
time.sleep(5)