Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sender and admin refactoring

  • Loading branch information...
commit 34ef2ada53c4569c756bcdad428d80d436424192 1 parent d7f3fa4
oau authored
Showing with 239 additions and 40 deletions.
  1. +32 −6 cli.py
  2. +2 −1  config.py → config.py.default
  3. +137 −22 core.py
  4. +68 −11 core_test.py
View
38 cli.py
@@ -7,7 +7,8 @@ def usage():
print("%s add-group <name>"%sys.argv[0])
print("%s list-groups"%sys.argv[0])
print("%s add-member <number> <alias> <group>"%sys.argv[0])
- print("%s add-sender <number> <group> <phone> [keyword]"%sys.argv[0])
+ print("%s set-sender <number> <group> <phone> [keyword]"%sys.argv[0])
+ print("%s set-admin <number> <group> <phone> [keyword]"%sys.argv[0])
print("%s list-members <group>"%sys.argv[0])
print("%s fake-incoming <src> <phone> <msg>"%sys.argv[0])
sys.exit(1)
@@ -32,9 +33,11 @@ def usage():
print "no such group!"
sys.exit(1)
for member in data.get_group_members(gid):
- s = '%s [%s]'%(member['alias'],member['number'])
- if member['number'] in data.get_group_senders(gid):
- s = '* '+s
+ if member['sender']: s = 's'
+ else: s = ' '
+ if member['admin']: s = 'a'+s
+ else: s = ' '+s
+ s += ' %s [%s]'%(member['alias'],member['number'])
print(s)
elif sys.argv[1] == 'add-member':
if len(sys.argv) != 5:
@@ -49,7 +52,7 @@ def usage():
print "group error!"
sys.exit(1)
data.add_number(number, alias, group_id)
-elif sys.argv[1] == 'add-sender':
+elif sys.argv[1] == 'set-sender':
if len(sys.argv) != 5 and len(sys.argv) != 6:
usage()
@@ -57,7 +60,30 @@ def usage():
number = core.normalize_number(number)
keyword = sys.argv[5] if len(sys.argv) == 6 else ""
group_id = data.get_group_id(group)
- data.add_sender(number, phone, group_id, keyword)
+ if not group_id:
+ print "group error!"
+ sys.exit(1)
+ mid = data.get_member_id(number, group_id);
+ if not mid:
+ print "no such number!"
+ sys.exit(1)
+ data.set_sender(phone, member_id=mid, keyword=keyword)
+elif sys.argv[1] == 'set-admin':
+ if len(sys.argv) != 5 and len(sys.argv) != 6:
+ usage()
+
+ number,group,phone = sys.argv[2:5]
+ number = core.normalize_number(number)
+ keyword = sys.argv[5] if len(sys.argv) == 6 else ""
+ group_id = data.get_group_id(group)
+ if not group_id:
+ print "group error!"
+ sys.exit(1)
+ mid = data.get_member_id(number, group_id);
+ if not mid:
+ print "no such number!"
+ sys.exit(1)
+ data.set_admin(phone, member_id=mid, keyword=keyword)
elif sys.argv[1] == 'fake-incoming':
if len(sys.argv) != 5:
usage()
View
3  config.py → config.py.default
@@ -1,6 +1,7 @@
class Config:
smsdrc = '/etc/gammu-smsdrc'
log = '/tmp/smsd.log'
- db = '/tmp/smsd/smsd.db'
+ db = '/tmp/smsd.db'
+ test_db = 'tmp.db'
config = Config()
View
159 core.py
@@ -26,32 +26,73 @@ def setup_db(self):
"groupId integer not null, alias varchar(50))")
c.execute("CREATE TABLE IF NOT EXISTS qq_sendmap "+
"(id integer primary key autoincrement, "+
- "src varchar(22) not null, dest varchar(22) not null, "+
- "keyword varchar(32) not null, groupId int not null)")
+ "memberId not null, dest varchar(22) not null, "+
+ "keyword varchar(32) not null)")
+ c.execute("CREATE TABLE IF NOT EXISTS qq_adminmap "+
+ "(id integer primary key autoincrement, "+
+ "memberId integer not null, dest varchar(22) not null, "+
+ "keyword varchar(32) not null)")
self.conn.commit()
def add_number(self, number, alias, group_id):
+ ''' updates alias if number already exists in group
+ '''
c = self.cursor
- c.execute("insert into qq_groupMembers "+
+ c.execute("insert or ignore into qq_groupMembers "+
"(number, groupId, alias) "+
"values (?,?,?)",
(number, group_id, alias))
+ c.execute("update qq_groupMembers set alias=? "+
+ "where number=? and groupId=?",
+ (alias, number, group_id))
self.conn.commit()
+ return self.get_member_id(number,group_id)
- def add_sender(self, number, dest, group_id, keyword=""):
- c = self.cursor
- c.execute("insert into qq_sendmap (src,dest,keyword,groupId) "+
- "values (?,?,?,?)",
- (number, dest, keyword, group_id))
- self.conn.commit()
+ #def add_sender(self, number, dest, keyword=""):
+ def set_sender(self, dest, keyword="", member_id=None, number=None, group_id=None):
+ ''' requires either member_id or number+group_id
+ '''
+ if not member_id:
+ if number and group_id:
+ member_id = self.get_member_id(number, group_id)
+ else:
+ raise TypeError("Didn't get the required kw params")
- def remove_number(self, number, group_id):
- c = self.cursor
- c.execute("delete from qq_groupMembers where number=? and groupId=?",
- (number, group_id))
- c.execute("delete from qq_sendmap where src=? and groupId=?",
- (number, group_id))
- self.conn.commit()
+ if member_id:
+ c = self.cursor
+ c.execute("insert into qq_sendmap (memberId,dest,keyword) "+
+ "values (?,?,?)",
+ (member_id, dest, keyword))
+ self.conn.commit()
+
+ def set_admin(self, dest, keyword="", member_id=None, number=None, group_id=None):
+ ''' requires either member_id or number+group_id
+ '''
+ if not member_id:
+ if number and group_id:
+ member_id = self.get_member_id(number, group_id)
+ else:
+ raise TypeError("Didn't get the required kw params")
+
+ if member_id:
+ c = self.cursor
+ c.execute("insert into qq_adminmap (memberId,dest,keyword) "+
+ "values (?,?,?)",
+ (member_id, dest, keyword))
+ self.conn.commit()
+
+ def remove_number(self, member_id=None, number=None, group_id=None):
+ if not member_id:
+ member_id = self.get_member_id(number, group_id)
+ if member_id:
+ c = self.cursor
+ c.execute("delete from qq_sendmap where memberId=?",
+ (member_id,))
+ c.execute("delete from qq_adminmap where memberId=?",
+ (member_id,))
+ c.execute("delete from qq_groupMembers where id=?",
+ (member_id,))
+ self.conn.commit()
def add_group(self, name):
c = self.cursor
@@ -67,19 +108,48 @@ def add_group(self, name):
def get_group_senders(self, group_id):
c = self.cursor
- c.execute('select src from qq_sendmap '+
- 'where groupId=?', (group_id,))
+ c.execute('select m.number from qq_groupMembers m '+
+ 'join qq_sendmap s on m.id=s.memberId '+
+ 'where m.groupId=?', (group_id,))
senders = []
for row in c:
senders.append(row[0])
return senders
+ def get_group_admins(self, group_id):
+ c = self.cursor
+ c.execute('select m.number from qq_groupMembers m '+
+ 'join qq_adminmap a on m.id=a.memberId '+
+ 'where m.groupId=?', (group_id,))
+ admins = []
+ for row in c:
+ admins.append(row[0])
+ return admins
+
+ def get_admin(self, src, dest, msg):
+ ''' return pair (cmd, groupId)
+ '''
+ c = self.cursor
+ c.execute('select a.keyword,m.groupId from qq_groupMembers m '+
+ 'join qq_adminmap a on m.id = a.memberId '+
+ 'where m.number=? and a.dest=? order by length(keyword) desc',
+ (src,dest))
+ for row in c:
+ keyword = row[0]
+ if len(keyword) > 0: keyword += ' '
+ if msg.lower().startswith(keyword.lower()):
+ msg_out = msg[len(keyword):]
+ return keyword,msg_out,row[1]
+ return None
+
def get_sendout(self, src, dest, msg):
''' return pair (msg, groupId)
'''
c = self.cursor
- c.execute('select keyword,groupId from qq_sendmap '+
- 'where src=? and dest=? order by length(keyword) desc', (src,dest))
+ c.execute('select a.keyword,m.groupId from qq_groupMembers m '+
+ 'join qq_sendmap a on m.id = a.memberId '+
+ 'where m.number=? and a.dest=? order by length(keyword) desc',
+ (src,dest))
for row in c:
keyword = row[0]
if len(keyword) > 0: keyword += ' '
@@ -90,10 +160,16 @@ def get_sendout(self, src, dest, msg):
def get_group_members(self, group_id):
c = self.cursor
- c.execute('select number,alias from qq_groupMembers where groupId=?', (group_id,))
+ c.execute('select number,alias,s.id,a.id from qq_groupMembers m '+
+ 'left join qq_sendmap s on m.id=s.memberId '+
+ 'left join qq_adminmap a on m.id=a.memberId '+
+ 'where m.groupId=?',
+ (group_id,))
members = []
for row in c:
- members.append({'number':row[0],'alias':row[1]})
+ sender = row[2] != None
+ admin = row[3] != None
+ members.append({'number':row[0],'alias':row[1],'sender':sender,'admin':admin})
return members
def get_groups(self):
@@ -112,6 +188,15 @@ def get_group_id(self,name):
return None
return x[0]
+ def get_member_id(self, number, group_id):
+ c = self.cursor
+ c.execute('select id from qq_groupMembers where groupId=? and number=?',
+ (group_id, number))
+ x = c.fetchone()
+ if not x:
+ return None
+ return x[0]
+
def get_unprocessed(self):
c = self.cursor
c.execute("select ID,SenderNumber,RecipientID,TextDecoded "+
@@ -137,6 +222,7 @@ def purge_all_data(self):
c.execute('delete from qq_groupMembers')
c.execute('delete from qq_groups')
c.execute('delete from qq_sendmap')
+ c.execute('delete from qq_adminmap')
self.conn.commit()
def cleanup(self):
@@ -169,6 +255,35 @@ def run(self):
messages = self.data.get_unprocessed()
for m in messages:
i,src,dest,msg = m
+ self._log("found message: [%d] %s->%s '%s'"%(i,src,dest,msg))
+ a = self.data.get_admin(src,dest,msg)
+ if a:
+ keyword,cmd,group = a
+ self._log("doing command '%s' to group %d"%(cmd,group))
+ if cmd.startswith('add sender '):
+ number = normalize_number(cmd[len('add sender '):])
+ if number:
+ mid = self.data.add_number(number, 'noname', group)
+ self.data.set_sender(dest, member_id=mid, keyword=keyword)
+ else:
+ self._log("error: couldn't find number in add command")
+ elif cmd.startswith('add admin '):
+ number = normalize_number(cmd[len('add admin '):])
+ if number:
+ mid = self.data.add_number(number, 'noname', group)
+ self.data.set_sender(dest, member_id=mid, keyword=keyword)
+ self.data.set_admin(dest, member_id=mid, keyword=keyword)
+ else:
+ self._log("error: couldn't find number in add command")
+ elif cmd.startswith('add '):
+ number = normalize_number(cmd[len('add '):])
+ if number:
+ self.data.add_number(number, 'noname', group)
+ else:
+ self._log("error: couldn't find number in add command")
+ else:
+ self._log("error: unknown admin command!")
+
s = self.data.get_sendout(src,dest,msg)
if s:
msg,group = s
View
79 core_test.py
@@ -8,7 +8,7 @@ def nomalize_number(number):
class TestData:
def setUp(self):
- config.db = "tmp.db"
+ config.db = config.test_db
self.data = core.Data()
self.data.setup_db()
self.data.purge_all_data()
@@ -20,30 +20,49 @@ def test_add_group(self):
def test_add_number(self):
number = "123"
gid = self.data.add_group("group1")
- self.data.add_number(number, "alias", gid)
+ mid = self.data.add_number(number, "alias", gid)
members = self.data.get_group_members(gid)
+ assert mid
assert len(members) == 1
assert members[0]['number'] == number
assert members[0]['alias'] == 'alias'
def test_remove_number(self):
- number = "123"
+ number1 = "123"
+ number2 = "1235"
gid = self.data.add_group("group1")
- self.data.add_number(number, "alias", gid)
- self.data.remove_number(number, gid)
+ self.data.add_number(number1, "alias", gid)
+ self.data.remove_number(number=number1, group_id=gid)
+ numbers = self.data.get_group_members(gid)
+ assert len(numbers) == 0
+
+ mid = self.data.add_number(number2, "alias", gid)
+ self.data.remove_number(member_id=mid)
numbers = self.data.get_group_members(gid)
assert len(numbers) == 0
def test_add_sender(self):
number1 = "1234"
- number2 = "7777"
+ dest = "7777"
gid = self.data.add_group("group1")
- self.data.add_number(number1, "alias", gid)
- self.data.add_sender(number1, number2, gid)
- msg,group = self.data.get_sendout(number1, number2, "hello")
+ mid = self.data.add_number(number1, "alias", gid)
+ self.data.set_sender(dest, member_id=mid)
+ self.data.set_admin(dest, member_id=mid)
+ msg,group = self.data.get_sendout(number1, dest, "hello")
assert msg == "hello"
assert group == gid
+ def test_add_admin(self):
+ number1 = "1234"
+ dest = "7777"
+ gid = self.data.add_group("group1")
+ mid = self.data.add_number(number1, "alias", gid)
+ self.data.set_admin(dest, member_id=mid)
+ kw,cmd,group = self.data.get_admin(number1, dest, "hello")
+ assert kw == ""
+ assert cmd == "hello"
+ assert group == gid
+
def test_get_unprocessed(self):
number1 = "+46730000009"
self.data.fake_incoming(number1, "phone1", "hello")
@@ -65,7 +84,45 @@ def test_run(self):
number = "+46736000001"
phone = "phone1"
gid = self.data.add_group("group1")
- self.data.add_number(number, "alias", gid)
- self.data.add_sender(number, phone, gid)
+ mid = self.data.add_number(number, "alias", gid)
+ self.data.set_sender(phone, member_id = mid)
self.data.fake_incoming(number, phone, "hello")
self.worker.run()
+
+ def test_run_admin_command_add(self):
+ number = "+46736000001"
+ phone = "phone1"
+ gid = self.data.add_group("group1")
+ mid = self.data.add_number(number, "alias", gid)
+ self.data.set_admin(phone, member_id = mid)
+ self.data.fake_incoming(number, phone, "add 073123")
+ self.worker.run()
+ assert "+4673123" in map(lambda x: x['number'], self.data.get_group_members(gid))
+ assert not "+4673123" in self.data.get_group_senders(gid)
+ assert not "+4673123" in self.data.get_group_admins(gid)
+
+ def test_run_admin_command_add_sender(self):
+ number = "+46736000001"
+ phone = "phone1"
+ gid = self.data.add_group("group1")
+ mid = self.data.add_number(number, "alias", gid)
+ self.data.set_admin(phone, member_id = mid)
+ self.data.fake_incoming(number, phone, "add sender 073123")
+ self.worker.run()
+ assert "+4673123" in map(lambda x: x['number'],
+ self.data.get_group_members(gid))
+ assert "+4673123" in self.data.get_group_senders(gid)
+ assert not "+4673123" in self.data.get_group_admins(gid)
+
+ def test_run_admin_command_add_admin(self):
+ number = "+46736000001"
+ phone = "phone1"
+ gid = self.data.add_group("group1")
+ mid = self.data.add_number(number, "alias", gid)
+ self.data.set_admin(phone, member_id = mid)
+ self.data.fake_incoming(number, phone, "add admin 073123")
+ self.worker.run()
+ assert "+4673123" in map(lambda x: x['number'],
+ self.data.get_group_members(gid))
+ assert "+4673123" in self.data.get_group_senders(gid)
+ assert "+4673123" in self.data.get_group_admins(gid)
Please sign in to comment.
Something went wrong with that request. Please try again.