Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Introduce change-number command AND the quiet_hours config. #14

Merged
merged 4 commits into from

2 participants

@olleolleolle

Change number

  • has a nose test
  • tries to follow convention (but fails to accepts all-lowercase SQL, since colorful syntax highlighting is so sweet)

quiet_hours config

Does this do the trick? Simplest possible thing. Needs a way to test this, though. Update: Ah, http://stevepulec.com/freezegun/

@oau
Owner

The change_number() function won't work unless the group_id parameter is supplied. It needs to be either a required parameter or it's absence should change the number in all groups.

@olleolleolle

Good! I'll get on that later. This PR will be updated.

@oau oau merged commit ddc7ffb into oau:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2013
  1. @olleolleolle

    Whitespace

    olleolleolle authored
  2. @olleolleolle

    Add change-number command

    olleolleolle authored
    Fixes #12
  3. @olleolleolle

    Whitespace

    olleolleolle authored
  4. @olleolleolle

    Introduce quiet_hours config

    olleolleolle authored
    Fixes #7
This page is out of date. Refresh to see the latest.
Showing with 71 additions and 20 deletions.
  1. +1 −0  config.py.default
  2. +33 −16 core.py
  3. +14 −0 core_test.py
  4. +23 −4 smskrupp
View
1  config.py.default
@@ -11,5 +11,6 @@ class Config:
default_phone = 'phone1'
debug = False
flask_key = ''
+ quiet_hours = [21, 22, 23, 0, 1, 2, 3, 4, 5, 6]
config = Config()
View
49 core.py
@@ -5,6 +5,7 @@
from config import config
import sqlite3
from time import strftime, localtime
+import datetime
def normalize_number(number):
@@ -56,6 +57,16 @@ def add_number(self, number, alias, group_id):
self.conn.commit()
return self.get_member_id(number, group_id)
+ def change_number(self, number, new_number, group_id=None):
+ ''' Updates number if number already exists in group
+ does nothing if number does not exist.
+ '''
+ c = self.cursor
+ c.execute("UPDATE `qq_groupMembers` SET `number`=? WHERE `number`=? AND `groupId`=?",
+ (new_number, number, group_id))
+ self.conn.commit()
+ return self.get_member_id(new_number, group_id)
+
def set_member_info(self, member_id, **kwargs):
c = self.cursor
if 'sender' in kwargs:
@@ -120,11 +131,11 @@ def get_group_members(self, group_id):
''' return array of dicts describing members (id, number, alias, sender, admin)
'''
c = self.cursor
- c.execute('select id,number,alias,sender,admin from qq_groupMembers '+
+ c.execute('select id,number,alias,sender,admin from qq_groupMembers ' +
'where groupId=?',
(group_id, ))
- return [{'id': row[0], 'number': row[1], 'alias': row[2], 'sender': (row[3] ==1 ),
- 'admin':(row[4]==1)} for row in c]
+ 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, number=None):
''' returns array of dicts describing groups (id, name keyword) containing number
@@ -176,7 +187,7 @@ def get_group_info(self, group_id=None, name=None):
x = c.fetchone()
if not x:
return None
- return {'id':x[0], 'name':x[1], 'keyword':x[2]}
+ return {'id': x[0], 'name': x[1], 'keyword': x[2]}
def get_member_id(self, number, group_id):
c = self.cursor
@@ -356,20 +367,20 @@ def check_webuser_login(self, username, password):
return row[3], row[2]
return 0, 0
-
def increment_sent_stats(self, group_id):
day = strftime("%Y-%m-%d %H:%M:%S", localtime())
c = self.cursor
- c.execute("insert or ignore into qq_groupStatistics "+
- "(day, groupId, cnt) "+
+ c.execute("insert or ignore into qq_groupStatistics " +
+ "(day, groupId, cnt) " +
"values (?,?,0)",
(day, group_id))
- c.execute("update qq_groupStatistics "+
+ c.execute("update qq_groupStatistics " +
"set cnt=cnt+1 "
"where day=? and groupId=?",
(day, group_id))
self.conn.commit()
+
class Doer:
def __init__(self, sender):
self.data = Data()
@@ -467,15 +478,18 @@ def _handle_message(self, ids, src, phone, orig_msg):
self.data.remove_number(number=src, group_id=g['id'])
status = 'stop'
elif action['action'] == 'sendout':
- group = action['group']
- msg = action['msg']
- if not src in [m['number'] for m in self.data.get_group_senders(group['id'])]:
- self._log("Warning: Unauthorized sendout command '%s' from %s to %s" %
- (orig_msg, src, phone))
- status = 'unauthorized'
+ if self.is_quiet_period():
+ ids = []
else:
- self.sendout(group['id'], msg)
- status = 'send'
+ group = action['group']
+ msg = action['msg']
+ if not src in [m['number'] for m in self.data.get_group_senders(group['id'])]:
+ self._log("Warning: Unauthorized sendout command '%s' from %s to %s" %
+ (orig_msg, src, phone))
+ status = 'unauthorized'
+ else:
+ self.sendout(group['id'], msg)
+ status = 'send'
elif action['action'] in ['add', 'add_sender', 'add_admin']:
group = action['group']
if not src in [m['number'] for m in self.data.get_group_admins(group['id'])]:
@@ -517,6 +531,9 @@ def run(self):
self._handle_message(m['ids'], m['src'], m['phone'], m['text'])
self.cleanup()
+ def is_quiet_period(self):
+ return datetime.datetime.now().time().hour in config.quiet_hours
+
class Sender:
def __init__(self):
View
14 core_test.py
@@ -6,6 +6,7 @@ class TestData:
def setUp(self):
config.db = config.test_db
config.smsdrc = config.test_smsdrc
+ config.quiet_hours = [] # Time-travel library needed to test the quiet hours functionality
self.data = core.Data()
self.data.setup_db()
self.data.purge_all_data()
@@ -79,6 +80,19 @@ def test_remove_number(self):
numbers = self.data.get_group_members(gid)
assert len(numbers) == 0
+ def test_change_number(self):
+ number = "+46736000001"
+ group_name = "group1"
+ gid = self.data.add_group(group_name, "keyword")
+ mid = self.data.add_number(number, "alias", gid)
+ self.data.set_member_info(mid, sender=True)
+ new_number = "+46736000002"
+ self.data.change_number(number, new_number, group_id=gid)
+ numbers = self.data.get_group_members(gid)
+ assert len(numbers) == 1
+ member = numbers[0]
+ assert member['number'] == new_number
+
def test_add_sender(self):
number = "1234"
gid = self.data.add_group("group1", "keyword")
View
27 smskrupp
@@ -5,11 +5,13 @@ import sys
import core
+
def usage():
print("%s add-group <name> <keyword>" % sys.argv[0])
print("%s list-groups" % sys.argv[0])
print("%s list-members <group>" % sys.argv[0])
print("%s add-member <number> <alias> <group>" % sys.argv[0])
+ print("%s change-number <number> <new_number> <group>" % sys.argv[0])
print("%s rm-member <number> <group|ALL>" % sys.argv[0])
print("%s set-sender <number> <group>" % sys.argv[0])
print("%s set-admin <number> <group>" % sys.argv[0])
@@ -68,6 +70,23 @@ elif argv[1] == u'add-member':
mid = data.add_number(number, alias, group_id)
if mid == None:
print(u"error: alias already exists")
+elif argv[1] == u'change-number':
+ if len(argv) != 5:
+ usage()
+ number, new_number, group = argv[2:]
+ number = core.normalize_number(number)
+ new_number = core.normalize_number(new_number)
+
+ if not number or not new_number:
+ print(u"number error!")
+ sys.exit(1)
+ group_id = data.get_group_id(group)
+ if not group_id:
+ print(u"group error!")
+ sys.exit(1)
+ mid = data.change_number(number, new_number, group_id=group_id)
+ if mid == None:
+ print(u"error: unable to change number")
elif argv[1] == u'rm-member':
if len(argv) != 4:
usage()
@@ -135,12 +154,12 @@ elif argv[1] == u'dump-groups':
print(len(argv))
usage()
for g in data.get_groups():
- print(u'smskrupp add-group %s %s'%(g['name'], g['keyword']))
+ print(u'smskrupp add-group %s %s' % (g['name'], g['keyword']))
for member in data.get_group_members(g['id']):
- print(u'smskrupp add-member %s %s %s'%(member['number'], member['alias'], g['name']))
+ print(u'smskrupp add-member %s %s %s' % (member['number'], member['alias'], g['name']))
if member['sender']:
- print(u'smskrupp set-sender %s %s'%(member['number'], g['name']))
+ print(u'smskrupp set-sender %s %s' % (member['number'], g['name']))
if member['admin']:
- print(u'smskrupp set-admin %s %s'%(member['number'], g['name']))
+ print(u'smskrupp set-admin %s %s' % (member['number'], g['name']))
else:
usage()
Something went wrong with that request. Please try again.