summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-07-21 11:05:47 +0200
committerSanto Cariotti <sancn@live.com>2017-07-21 11:05:47 +0200
commit1451011e86dcbcaba2e43013ee86042ad27dc6b5 (patch)
treeb31dd291bf6c5e41a237ad853e66ba2363a6035a /lib
parent99d5a8a7c7f86725d772f19e007c24e9f1397740 (diff)
it accepts only 1 user
Diffstat (limited to 'lib')
-rw-r--r--lib/app.py12
-rw-r--r--lib/classes.py23
-rw-r--r--lib/commands.py17
-rw-r--r--lib/listcommands.py2
4 files changed, 19 insertions, 35 deletions
diff --git a/lib/app.py b/lib/app.py
index f994863..31fd270 100644
--- a/lib/app.py
+++ b/lib/app.py
@@ -1,4 +1,4 @@
-from classes import PersonalError, Colors, r, s, host, clear
+from classes import PersonalError, Colors, r, clear
from commands import Commands
from listcommands import ListCommands
@@ -13,16 +13,15 @@ class Stout(Commands):
if self.user == '':
return word
else:
- return word + Colors.grey + '(' + self.user + ':' + host + ') '
+ return word + Colors.grey + '(' + self.user + ') '
@staticmethod
def username():
"""
- if users exists, return username:0.0.0.0, else return an empty string.
- there is a set into user:HOST with a variable called name
+ if users exists, return username, else return an empty string
"""
- if r.hget('user:'+host, 'name') is not None:
- return r.hget('user:'+host, 'name').decode('utf-8') #default is a byte
+ if r.get('user') is not None:
+ return r.get('user').decode('utf-8') #default is a byte
else:
return ''
@@ -69,5 +68,4 @@ if __name__ == '__main__':
app.action(cmd)
r.save()
- s.close()
clear()
diff --git a/lib/classes.py b/lib/classes.py
index fc04e2d..1a1c876 100644
--- a/lib/classes.py
+++ b/lib/classes.py
@@ -2,9 +2,6 @@ import redis, socket, os
from config import config as co
r = redis.Redis(host=co['host'], port=co['port'], unix_socket_path=co['unix_socket_path'], db=co['db'])
-s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-s.connect(('8.8.8.8', 80))
-host = s.getsockname()[0]
def clear():
"""
@@ -15,16 +12,16 @@ def clear():
except:
os.system('cls')
-def userexist(name):
- """
- lista is a list for the set called USERSNAME. check if name is in lista
- """
- lista = r.zrange('usersname', 0, -1)
- for i in lista:
- if name == i.decode('utf-8'):
- return True
- else:
- return False
+# def userexist(name):
+# """
+# lista is a list for the set called USERSNAME. check if name is in lista
+# """
+# lista = r.zrange('usersname', 0, -1)
+# for i in lista:
+# if name == i.decode('utf-8'):
+# return True
+# else:
+# return False
class PersonalError(Exception):
diff --git a/lib/commands.py b/lib/commands.py
index 1b5a5a8..61c3eaf 100644
--- a/lib/commands.py
+++ b/lib/commands.py
@@ -1,4 +1,4 @@
-from classes import userexist, PersonalError, r, host, clear, Colors
+from classes import PersonalError, r, clear, Colors
from listcommands import ListCommands
from config import config as co
@@ -16,17 +16,12 @@ class Commands(object):
if len(cmd[2]) > 10:
raise PersonalError('lunghezza maggiore del consetito. Max 10')
- if userexist(cmd[2]) == True: #check if the user exists
- raise PersonalError('questo nome utente esiste giĆ ')
-
if len(cmd) > ListCommands.info['set'][1]:
raise PersonalError(Colors.grey + 'set user' + Colors.red + ' accetta 1 parametro')
- if self.user != '': r.zrem('usersname', self.user) #if user already exists, del the name from set
self.user = cmd[2]
- r.hset('user:'+host, 'name', self.user) #change name of user:0.0.0.0
- r.zadd('usersname', self.user, 0) #insert new name into the set
+ r.set('user', self.user) #change name of user
print('Ok')
elif cmd[1] is not ListCommands.commands['set']:
@@ -46,19 +41,13 @@ class Commands(object):
if cmd[2] == 'user':
#if self.user is empty, print 'nil'
if self.user != '':
- print(r.hget('user:'+host, 'name').decode('utf-8'))
+ print(r.get('user').decode('utf-8'))
else:
print('nil')
elif cmd[2] not in ListCommands.commands['get'][1]: #check if the word after 'i' exists
ListCommands.err('keyword')
else:
print(co[cmd[2]])
- elif cmd[1] == 'user?':
- #if there are more than 2 word after 'get', the func doens't work
- if len(cmd) > 3:
- raise PersonalError(Colors.grey + 'get user?' + Colors.red + ' accetta 1 parametro')
- #return 1 if the user exists
- print((lambda x: '0' if x == False else '1')(userexist(cmd[2])))
else:
raise KeyError
except IndexError:
diff --git a/lib/listcommands.py b/lib/listcommands.py
index 6c7ea65..3f4f3ad 100644
--- a/lib/listcommands.py
+++ b/lib/listcommands.py
@@ -16,7 +16,7 @@ class ListCommands(object):
'quit' : None,
'clear' : None,
'set' : ['user'],
- 'get' : ['i', ['user', 'host', 'port', 'unix_socket_path', 'db'], 'user?']
+ 'get' : ['i', ['user', 'host', 'port', 'unix_socket_path', 'db']]
}
@staticmethod