Permalink
Browse files

Don't split groups by phone

The functionality to attach different groups to different receiving
phones is not used and could be solved by running different smskrupp
instances.

By removing it we will be able to receive messages on different
sim-cards while still sending to groups using one output phone.
  • Loading branch information...
1 parent 5e54a05 commit 8b93e1d2fdc086612c5c62a0e359d1f120d998ea oau committed Dec 15, 2012
Showing with 64 additions and 89 deletions.
  1. +2 −0 config.py.default
  2. +34 −54 core.py
  3. +25 −29 core_test.py
  4. +1 −3 smskrupp
  5. +2 −3 sql/smskrupp.sql
View
2 config.py.default
@@ -1,5 +1,7 @@
class Config:
db = 'smskrupp.db'
+
+ # smsdrc used for sending sms
smsdrc = '/etc/gammu-smsdrc'
test_db = 'smskrupp-test.db'
test_smsdrc = 'test-gammu-smsdrc'
View
88 core.py
@@ -76,13 +76,13 @@ def remove_number(self, member_id=None, number=None, group_id=None):
(member_id,))
self.conn.commit()
- def add_group(self, name, keyword, phone):
+ def add_group(self, name, keyword):
''' create a group and return id of the created group
'''
c = self.cursor
- c.execute("insert into qq_groups (name,keyword,phone) " +
- "values (?,?,?)",
- (name, keyword, phone))
+ c.execute("insert into qq_groups (name,keyword) " +
+ "values (?,?)",
+ (name, keyword))
self.conn.commit()
c.execute('select id from qq_groups where name=?', (name,))
group_id = None
@@ -124,58 +124,38 @@ def get_group_members(self, group_id):
return [{'id': row[0], 'number': row[1], 'alias': row[2], 'sender': (row[3] ==1 ),
'admin':(row[4]==1)} for row in c]
- def get_groups(self, phone=None, number=None):
- ''' returns array of dicts describing groups (id, name keyword, phone) containing number
+ def get_groups(self, number=None):
+ ''' returns array of dicts describing groups (id, name keyword) containing number
'''
c = self.cursor
- if phone and number:
- c.execute('select g.id, g.name, g.keyword, g.phone from qq_groupMembers m '
- +'join qq_groups g on g.id = m.groupId '
- +'where m.number=? and g.phone=? order by g.name asc',
- (number, phone))
- elif number:
- c.execute('select g.id, g.name, g.keyword, g.phone from qq_groupMembers m '
+ if number:
+ c.execute('select g.id, g.name, g.keyword from qq_groupMembers m '
+ 'join qq_groups g on g.id = m.groupId '
+ 'where m.number=? order by g.name asc',
(number,))
- elif phone:
- c.execute('select id,name, keyword, phone from qq_groups where phone=? order by name asc',
- (phone,))
else:
- c.execute('select id,name,keyword,phone from qq_groups order by name asc')
- return [{'id':row[0], 'name':row[1], 'keyword':row[2], 'phone':row[3]} for row in c]
+ c.execute('select id,name,keyword from qq_groups order by name asc')
+ return [{'id':row[0], 'name':row[1], 'keyword':row[2]} for row in c]
- def get_send_groups(self, sender, phone=None):
- ''' returns array of dicts describing groups (id, name keyword, phone) where the sender can send
+ def get_send_groups(self, sender):
+ ''' returns array of dicts describing groups (id, name keyword) where the sender can send
'''
c = self.cursor
- if phone:
- c.execute('select g.id, g.name, g.keyword, g.phone from qq_groupMembers m '
- + 'join qq_groups g on g.id = m.groupId '
- + 'where m.number=? and g.phone=? and m.sender=1 order by g.name asc',
- (sender, phone))
- else:
- c.execute('select g.id, g.name, g.keyword, g.phone from qq_groupMembers m '
- + 'join qq_groups g on g.id = m.groupId '
- + 'where m.number=? and m.sender=1 order by g.name asc',
- (sender,))
- return [{'id':row[0], 'name':row[1], 'keyword':row[2], 'phone':row[3]} for row in c]
-
- def get_admin_groups(self, sender, phone=None):
- ''' returns array of dicts describing groups (id, name keyword, phone) where the sender can admin
+ c.execute('select g.id, g.name, g.keyword from qq_groupMembers m '
+ + 'join qq_groups g on g.id = m.groupId '
+ + 'where m.number=? and m.sender=1 order by g.name asc',
+ (sender,))
+ return [{'id':row[0], 'name':row[1], 'keyword':row[2]} for row in c]
+
+ def get_admin_groups(self, sender):
+ ''' returns array of dicts describing groups (id, name keyword) where the sender can admin
'''
c = self.cursor
- if phone:
- c.execute('select g.id, g.name, g.keyword, g.phone from qq_groupMembers m '
- + 'join qq_groups g on g.id = m.groupId '
- + 'where m.number=? and g.phone=? and m.admin=1 order by g.name asc',
- (sender, phone))
- else:
- c.execute('select g.id, g.name, g.keyword, g.phone from qq_groupMembers m '
- + 'join qq_groups g on g.id = m.groupId '
- + 'where m.number=? and m.admin=1 order by g.name asc',
- (sender,))
- return [{'id':row[0], 'name':row[1], 'keyword':row[2], 'phone':row[3]} for row in c]
+ c.execute('select g.id, g.name, g.keyword from qq_groupMembers m '
+ + 'join qq_groups g on g.id = m.groupId '
+ + 'where m.number=? and m.admin=1 order by g.name asc',
+ (sender,))
+ return [{'id':row[0], 'name':row[1], 'keyword':row[2]} for row in c]
def get_group_id(self, name):
c = self.cursor
@@ -390,9 +370,9 @@ def _log(self, text):
t = strftime("%Y-%m-%d %H:%M:%S", localtime())
log.write("[%s] [doer] %s\n" % (t, text.encode('utf-8')))
- def _parse_action(self, src, phone, orig_msg):
- groups = self.data.get_groups(phone=phone, number=src)
- send_groups = self.data.get_send_groups(src, phone=phone)
+ def _parse_action(self, src, orig_msg):
+ groups = self.data.get_groups(number=src)
+ send_groups = self.data.get_send_groups(src)
lmsg = orig_msg.lower().strip()
if lmsg == 'stop' or lmsg == 'stopp':
return {'action': 'stop', 'groups': groups}
@@ -458,7 +438,7 @@ def _parse_action(self, src, phone, orig_msg):
def _handle_message(self, ids, src, phone, orig_msg):
self._log("got message '%s' from %s to %s" % (orig_msg, src, phone))
- action = self._parse_action(src, phone, orig_msg)
+ action = self._parse_action(src, orig_msg)
status = 'invalid'
if action['action'] == 'stop':
@@ -496,16 +476,16 @@ def _handle_message(self, ids, src, phone, orig_msg):
self.data.set_member_info(mid, sender=True, admin=True)
is_sender, is_admin = True, True
- user_groups = self.data.get_groups(number=action['number'], phone=phone)
- user_send_groups = self.data.get_send_groups(action['number'], phone=phone)
+ user_groups = self.data.get_groups(number=action['number'])
+ user_send_groups = self.data.get_send_groups(action['number'])
welcomes = Helper().get_welcomes(group['name'], group['keyword'], is_sender, is_admin, user_groups, user_send_groups)
for msg in welcomes:
self.sender.send(action['number'], msg)
elif action['action'] == 'invalid':
# send help message
- user_groups = self.data.get_groups(number=src, phone=phone)
- user_send_groups = self.data.get_send_groups(src, phone=phone)
- user_admin_groups = self.data.get_admin_groups(src, phone=phone)
+ user_groups = self.data.get_groups(number=src)
+ user_send_groups = self.data.get_send_groups(src)
+ user_admin_groups = self.data.get_admin_groups(src)
helps = Helper().get_help(user_groups, user_send_groups, user_admin_groups)
for msg in helps:
self.sender.send(src, msg)
View
54 core_test.py
@@ -11,27 +11,25 @@ def setUp(self):
self.data.purge_all_data()
def test_add_group(self):
- gid = self.data.add_group("group1", "keyword", "phone1")
+ gid = self.data.add_group("group1", "keyword")
assert isinstance(gid, int)
- gs = self.data.get_groups(phone="phone1")
+ gs = self.data.get_groups()
assert 1 == len(gs)
assert 'group1' == gs[0]['name']
assert 'keyword' == gs[0]['keyword']
- assert 'phone1' == gs[0]['phone']
assert gid == gs[0]['id']
def test_get_groups(self):
number1 = "345"
number2 = "346"
- phone = "phone1"
- gid1 = self.data.add_group("group1", "keyword1", phone)
- gid2 = self.data.add_group("group2", "keyword2", phone)
+ gid1 = self.data.add_group("group1", "keyword1")
+ gid2 = self.data.add_group("group2", "keyword2")
self.data.add_number(number1, "alias", gid1)
self.data.add_number(number2, "alias", gid2)
- gs = self.data.get_groups(phone=phone)
+ gs = self.data.get_groups()
assert 2 == len(gs)
- gs = self.data.get_groups(phone=phone, number=number1)
+ gs = self.data.get_groups(number=number1)
assert 1 == len(gs)
assert 'group1' == gs[0]['name']
@@ -41,7 +39,7 @@ def test_get_groups(self):
def test_add_number(self):
number = "123"
- gid = self.data.add_group("group1", "keyword", "123")
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
members = self.data.get_group_members(gid)
assert mid
@@ -53,7 +51,7 @@ def test_add_number(self):
def test_add_number_no_alias(self):
number1 = "123"
number2 = "124"
- gid = self.data.add_group("group1", "keyword", "123")
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number1, None, gid)
mid2 = self.data.add_number(number2, None, gid)
members = self.data.get_group_members(gid)
@@ -70,7 +68,7 @@ def test_add_number_no_alias(self):
def test_remove_number(self):
number1 = "123"
number2 = "1235"
- gid = self.data.add_group("group1", "keyword", "123")
+ gid = self.data.add_group("group1", "keyword")
self.data.add_number(number1, "alias", gid)
self.data.remove_number(number=number1, group_id=gid)
numbers = self.data.get_group_members(gid)
@@ -83,8 +81,7 @@ def test_remove_number(self):
def test_add_sender(self):
number = "1234"
- phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True, admin=True)
senders = self.data.get_group_senders(gid)
@@ -95,8 +92,7 @@ def test_add_sender(self):
def test_add_admin(self):
number = "1234"
- phone = "phone1337"
- gid = self.data.add_group("group1", "", phone)
+ gid = self.data.add_group("group1", "")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(member_id=mid, admin=True)
admins = self.data.get_group_admins(gid)
@@ -128,7 +124,7 @@ def setUp(self):
def test_run(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", "123")
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True)
self.data.fake_incoming(number, phone, "hello")
@@ -137,7 +133,7 @@ def test_run(self):
def test_run_stop_command(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
self.data.add_number(number, "alias", gid)
self.data.fake_incoming(number, phone, "stop")
self.doer.run()
@@ -146,7 +142,7 @@ def test_run_stop_command(self):
def test_run_stop_command_prefix(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
self.data.add_number(number, "alias", gid)
self.data.fake_incoming(number, phone, "/keyword stop")
self.doer.run()
@@ -155,7 +151,7 @@ def test_run_stop_command_prefix(self):
def test_run_admin_command_add_unauthorized(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
self.data.add_number(number, "alias", gid)
self.data.fake_incoming(number, phone, "/add 073123")
self.doer.run()
@@ -168,7 +164,7 @@ def test_run_admin_command_add_unauthorized(self):
def test_run_admin_command_add(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True, admin=True)
self.data.fake_incoming(number, phone, "/add 073123")
@@ -182,7 +178,7 @@ def test_run_admin_command_add(self):
def test_run_admin_command_add_sender(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True, admin=True)
self.data.fake_incoming(number, phone, "/Add Sender 073123")
@@ -196,7 +192,7 @@ def test_run_admin_command_add_sender(self):
def test_run_admin_command_add_admin(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True, admin=True)
self.data.fake_incoming(number, phone, "/add admin 073123")
@@ -210,7 +206,7 @@ def test_run_admin_command_add_admin(self):
def test_run_admin_command_add_keyword(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True, admin=True)
self.data.fake_incoming(number, phone, "/keyword add 073123")
@@ -224,7 +220,7 @@ def test_run_admin_command_add_keyword(self):
def test_run_admin_command_add_sender_keyword(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True, admin=True)
self.data.fake_incoming(number, phone, "/KEYWORD Add Sender 073123")
@@ -238,7 +234,7 @@ def test_run_admin_command_add_sender_keyword(self):
def test_run_admin_command_add_admin_keyword(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True, admin=True)
self.data.fake_incoming(number, phone, "/KEyWord add admin 073123")
@@ -252,7 +248,7 @@ def test_run_admin_command_add_admin_keyword(self):
def test_run_sendout_unauthorized(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
self.data.add_number(number, "alias", gid)
s = FakeSender()
@@ -274,7 +270,7 @@ def test_run_sendout_unknown_number(self):
def test_run_sendout_keyword_unauthorized(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
self.data.add_number(number, "alias", gid)
s = FakeSender()
@@ -286,7 +282,7 @@ def test_run_sendout_keyword_unauthorized(self):
def test_run_sendout(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True)
@@ -300,7 +296,7 @@ def test_run_sendout(self):
def test_run_sendout_prefix(self):
number = "+46736000001"
phone = "phone1"
- gid = self.data.add_group("group1", "keyword", phone)
+ gid = self.data.add_group("group1", "keyword")
mid = self.data.add_number(number, "alias", gid)
self.data.set_member_info(mid, sender=True)
View
4 smskrupp
@@ -2,8 +2,6 @@
import sys
import core
-from config import config
-
def usage():
print("%s add-group <name> <keyword>" % sys.argv[0])
@@ -25,7 +23,7 @@ data = core.Data()
if sys.argv[1] == 'add-group':
if len(sys.argv) != 4:
usage()
- data.add_group(sys.argv[2], sys.argv[3], config.default_phone)
+ data.add_group(sys.argv[2], sys.argv[3])
elif sys.argv[1] == 'list-groups':
for g in data.get_groups():
print g['name']
View
5 sql/smskrupp.sql
@@ -14,9 +14,8 @@ CREATE TABLE qq_groups (
id integer primary key autoincrement,
name varchar(50) not null,
keyword varchar(10) not null,
-phone varchar(22) not null,
-unique (name, phone),
-unique (keyword, phone));
+unique (name),
+unique (keyword));
DROP TABLE IF EXISTS qq_webUsers;
CREATE TABLE qq_webUsers (

0 comments on commit 8b93e1d

Please sign in to comment.