/
dbsetup.py
269 lines (257 loc) · 11.1 KB
/
dbsetup.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
from model import ModelManager
manager = ModelManager()
#TODO Add URLs
#TODO Add Edit
def usage(args):
print("")
print("Commands:")
print("help: Prints this message")
print("list: Prints what's inside the database")
print("add: Adds a Bot with a Network")
print("del: Deletes a Bot, Network, Channel, Server")
print("edit: Edits a Bot")
print("quit: quits the program")
print("")
def list_database(args):
if args == '?':
print "Lists aspects of the database."
print "list bot [name]: Lists the name of the bot or all the bots"
print "list networks: lists all the networks."
print "list servers: lists all the servers."
print "list channels: lists all the channels."
print "list users: lists all the users."
print "list msg: lists all the messages."
print "list: lists the whole database"
return
#find the occurence of the first space
if args is None or len(args.strip()) == 0:
manager.printAll()
else:
args = args.strip().split(' ')
if len(args) > 0:
if args[0] == 'bot' or args[0] == 'bots':
if len(args) == 2:
manager.printBot(args[1])
else:
manager.printBot()
elif args[0] == 'networks':
manager.printNetworks()
elif args[0] == 'servers':
manager.printServers()
elif args[0] == 'channels':
manager.printChannels()
elif args[0] == 'users':
manager.printUsers()
elif args[0] == 'msg':
manager.printMessages()
else:
print "Unknown list command: %s" % args[0]
list_database('?')
else:
print args
# TODO Check Del bot
# TODO Add Del server
# TODO Add Del chantobot
def delete(args):
if args == '?':
print "Delete a bot, network, server or channel from the database."
print "del bot nick network: Deletes a bot."
print "del network name: Deletes a network."
print "del server network address[:port[=SSL]]: Deletes a server."
print "del channel name: Deletes a channel with \"name\" from the channel database."
print "del chanbot channel bot network: Deletes a channel from a bot."
print "del user name: Deletes a user from the database."
return
else:
if args is None or len(args.strip()) == 0:
print "No command given to del, please specify a valid command."
delete('?')
return
args = args.strip().split(' ')
if len(args) > 0:
if args[0] == 'bot':
if len(args) != 3:
print "Cannot delete bot without the correct number of arguments."
print "del bot nick network: Deletes a bot."
print "del %s" % args
return
manager.delBot(args[1], args[2])
elif args[0] == 'network':
if len(args) != 2:
print "Cannot delete network without the correct number of arguments."
print "del network name: Deletes a network."
print "del %s" % args
return
manager.delNetwork(args[1])
elif args[0] == 'channel':
if len(args) != 2:
print "Cannot delete channel without the correct number of arguments."
print "del channel name: Deletes a channel with \"name\" from the channel database."
print "del %s" % args
return
manager.delChannel(args[1])
elif args[0] == 'chanbot':
if len(args) != 4:
print "Cannot delete a channel from a bot without the correct number of arguments."
print "del chanbot channel bot network: Deletes a channel from a bot."
print "del %s" % args
return
manager.delChannelBot(args[1], args[2], args[3])
elif args[0] == 'user':
if len(args) != 2:
print "Cannot delete a user without the correct number of arguments."
print "del user name: Adds a user with \"name\" from the user database."
print "del %s" % args
return
manager.delUser(args[1])
else:
print "Invalid command, please specify a valid command."
delete('?')
return
def add(args):
if args == '?':
print "Add a bot, network or channel to the database."
print "add bot nick network: Adds a bot to watch a given network."
print "add network name: Adds a network with the given name."
print "add server network address[:port[=SSL]]: Adds a server."
print "add channel name: Adds a channel with \"name\" to the channel database."
print "add chanbot channel bot network: Adds a channel to a bot."
print "add user nick: Adds a user to the database."
print "add msg nick network message: Adds a message to the database for a user."
print "add url message_id url: Adds a URL to the database."
return
else:
if args is None or len(args.strip()) == 0:
print "No command given to add, please specify a valid command."
add('?')
return
args = args.strip().split(' ')
if len(args) > 0:
if args[0] == 'bot':
if len(args) != 3:
print "Cannot add bot without the correct number of arguments."
print "add bot nick network: Adds a bot to watch a given network."
print "add %s" % args
return
bot = manager.addBot(args[1], args[2])
if bot is not None:
print "Bot %s added" % bot
return
print "Bot %s not added with network %s" % (args[1], args[2])
elif args[0] == 'network':
if len(args) != 2:
print "Cannot add network without the correct number of arguments."
print "add network name: Adds a network with the given name."
print "add %s" % args
return
network = manager.addNetwork(args[1])
if network is not None:
print "Network %s added" % network
return
print "Network %s not added" % args[1]
elif args[0] == 'channel':
if len(args) != 2:
print "Cannot add channel without the correct number of arguments."
print "add channel name: Adds a channel with \"name\" to the channel database."
print "add %s" % args
return
channel = manager.addChannel(args[1])
if channel is not None:
print "Channel %s added" % channel
return
print "Channel %s not added" % args[1]
elif args[0] == 'server':
if len(args) != 3:
print "Cannot add server without the correct number of arguments."
print "add server network address[:port[=SSL]]: Adds a server."
print "add %s" % args
return
#Split out the second argument to address, port, SSL
#User regexp to do this eventually
network_name = args[1]
args = args[2].strip().split(':')
if len(args) > 1:
address = args[0]
args = args[1].strip().split('=')
if len(args) == 2:
port = args[0]
SSL = True
else:
port = args[0]
SSL = False
else:
address = args[0]
port = None
SSL = False
#Get rid of the above ugly code and replace with Regexp
server = manager.addServer(network_name, address, port, SSL)
if server is not None:
print "Server %s added" % server
return
print "Server %s not added" % args[1]
elif args[0] == 'chanbot':
if len(args) != 4:
print "Cannot add channel to bot without the correct number of arguments."
print "add chanbot channel bot network: Adds a channel to a bot."
print "add %s" % args
return
manager.addChannelBot(args[1], args[2], args[3])
elif args[0] == 'user':
if len(args) != 2:
print "Cannot add user without the correct number of arguments."
print "add user nick: Adds a user to the database."
print "add %s" % args
return
user = manager.addUser(args[1])
if user is not None:
print "User %s added" % user
return
print "User %s not added" % args[1]
elif args[0] == 'msg':
if len(args) < 4:
print "Cannot add message without the correct number of arguments."
print "add msg nick network message: Adds a message to the database for a user."
print "add %s" % args
return
msg = manager.addMessage(args[1], args[2], args[3], " ".join(args[4:]))
if msg is not None:
print "Message %s added" % msg
return
print "Message %s not added" % args[1]
elif args[0] == 'url':
if len(args) < 3:
print "Cannot add url without the correct number of arguments."
print "add url message_id url: Adds a URL to the database."
print "add %s" % args
return
url = manager.addURL(args[1], args[2])
if url is not None:
print "URL %s added" % url
return
print "URL %s not added" % args[1]
else:
print "Unknown add command: %s" % args[0]
add('?')
else:
print args
#List of all the functions that can be used
function_map = {'help': usage,
'list': list_database,
'add': add,
'del': delete,
'quit': quit}
while True:
manager.init_db()
func_name = raw_input("Command:")
args = None
first_space = func_name.find(' ')
if first_space > 0:
args = func_name[first_space + 1:]
func_name = func_name[0:first_space]
function = function_map.get(func_name, None)
if function is None:
print "Unknown command %s" % func_name
usage('')
else:
function(args)
quit()