Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'feature/support-common-msg-format' into develop

  • Loading branch information...
commit b574180aff3e67f82fd6ecfe9ac3db2c8b050c1a 2 parents 270a291 + 4fb70af
@smn authored
View
54 archive.py
@@ -13,18 +13,18 @@
DATE_FORMAT = '%Y%m%d%H%M%S'
class BaseHandler(webapp.RequestHandler):
-
+
def render_to_string(self, template_file, context):
path = os.path.join(os.path.dirname(__file__), template_file)
return template.render(path, context)
-
+
def render_to_response(self, template_file, context):
return self.response.out.write(self.render_to_string(template_file, context))
-
+
def redirect_to(self, location):
self.response.set_status(301)
self.response.headers['Location'] = location
-
+
def challenge(self, realm):
self.response.set_status(401, message='Authorization Required')
self.response.headers['WWW-Authenticate'] = 'Basic realm="%s"' % realm
@@ -46,18 +46,18 @@ def authenticate(self, realm, callback):
class ArchiveHandler(BaseHandler):
def get(self):
- channels = [channel for channel in Channel.all().order('channel')
+ channels = [channel for channel in Channel.all().order('channel')
if channel.channel.startswith('#')]
self.render_to_response('templates/index.html', locals())
-
+
def post(self):
- msg = Message.log(self.request.body)
+ msg = Message.log_common_message_format(self.request.body)
self.response.set_status(201)
self.response.out.write(msg.key())
class LogSweepHandler(BaseHandler):
def get(self):
- messages = Message.all().filter('timestamp < ', datetime.utcnow() - timedelta(weeks=4))
+ messages = Message.all().filter('timestamp < ', datetime.utcnow() - timedelta(weeks=52))
entries = messages.fetch(1000)
db.delete(entries)
for channel in Channel.all():
@@ -78,8 +78,18 @@ def get(self):
for message in messages:
message.user_is_human = False
db.put(messages)
-
-
+
+ humans = User.all().filter('is_human = ', True)
+ logging.info(humans)
+ for human in humans:
+ messages = Message.all().filter('user = ', human)\
+ .filter('user_is_human = ', False).fetch(1000)
+ logging.info(messages)
+ for message in messages:
+ message.user_is_human = True
+ db.put(messages)
+
+
class ClearHandler(BaseHandler):
def get(self):
# for i in range(10):
@@ -99,31 +109,31 @@ def post(self):
if user:
user.is_human = bool(is_human)
user.save()
-
+
class ChannelHandler(BaseHandler):
-
+
def get(self, server, channel):
channel = Channel.find(server, channel)
-
+
if channel.is_private():
- if not self.authenticate('Private IRC channel %s' % channel.channel,
+ if not self.authenticate('Private IRC channel %s' % channel.channel,
channel.authenticate):
return
-
+
hide_bots = self.request.GET.get('hide_bots', '1') == '1'
query = Message.all().filter('channel =', channel)
-
+
if hide_bots:
query = query.filter('user_is_human = ', True)
-
+
quer = query.order('-timestamp')
-
+
cursor = self.request.GET.get('c')
if cursor:
query.with_cursor(cursor)
previous = memcache.get(cursor) or ''
-
+
today = datetime.utcnow().date()
messages = query.fetch(PAGE_SIZE)
next = query.cursor()
@@ -135,15 +145,15 @@ def get(self, server, channel):
self.render_to_response('templates/channel.html', locals())
class EditChannelHandler(BaseHandler):
-
+
def get(self, server, channel):
channel = Channel.find(server, channel)
if channel.is_private():
- if not self.authenticate('Private IRC chanenl %s' % channel.channel,
+ if not self.authenticate('Private IRC chanenl %s' % channel.channel,
channel.authenticate):
return
self.render_to_response('templates/edit_channel.html', locals())
-
+
def post(self, server, channel):
channel = Channel.find(server, channel)
channel.username = self.request.POST.get('username')
View
67 models.py
@@ -8,43 +8,43 @@
import color
class User(db.Model):
-
+
server = db.StringProperty(required=True)
nickname = db.StringProperty(required=True)
created_at = db.DateTimeProperty(required=True, auto_now_add=True)
last_seen_at = db.DateTimeProperty(required=False)
color = db.ListProperty(int, required=True, default=[])
is_human = db.BooleanProperty(required=True, default=True)
-
+
@staticmethod
def find(server, nickname):
key = db.Key.from_path(User.kind(), '%s/%s' % (nickname, server))
return User.get(key)
-
+
def get_color(self):
if not self.color:
self.color = color.random()
self.put()
return self.color
-
+
class Channel(db.Model):
channel = db.StringProperty(required=True)
server = db.StringProperty(required=True)
username = db.StringProperty(required=False, default='')
password = db.StringProperty(required=False, default='')
topic = db.TextProperty()
-
+
def is_private(self):
return self.username and self.password
-
+
def authenticate(self, username, password):
return username == self.username and password == self.password
-
+
@staticmethod
def find(server, channel):
key = db.Key.from_path(Channel.kind(), '%s/%s' % (unquote(channel), server))
return Channel.get(key)
-
+
class Message(db.Model):
user = db.ReferenceProperty(User)
@@ -54,31 +54,66 @@ class Message(db.Model):
message_type = db.StringProperty(required=True, choices=[
"system", "action", "message"])
message_content = db.TextProperty(required=True)
-
+
json = db.TextProperty(required=True)
-
+
@staticmethod
def log(json_data):
logging.info('Received JSON: %s' % json_data)
-
+
payload = json.loads(json_data)
-
+
nickname = payload.get('nickname')
server = payload.get('server', 'unknown')
channel = payload.get('channel')
message_type = payload.get('message_type')
message_content = payload.get('message_content')
timestamp = parse_timestamp(payload.get('timestamp'))
-
+
user = get_or_create(User, server=server, nickname=nickname)
user.last_seen_at = datetime.utcnow()
user.put()
-
+
channel = get_or_create(Channel, server=server, channel=channel)
# store the message
msg = Message(user=user, channel=channel, message_type=message_type,
- message_content=message_content, json=json_data,
+ message_content=message_content, json=json_data,
timestamp=timestamp, user_is_human = user.is_human)
msg.put()
return msg
-
+
+ @staticmethod
+ def log_common_message_format(json_data):
+ logging.info("Received JSON: %s" % json_data)
+ msg = json.loads(json_data)
+ nickname = msg['from_addr']
+ transport_metadata = msg['transport_metadata']
+
+ server_and_port = transport_metadata.get('irc_server')
+ server = server_and_port.partition(':')[0]
+ channel = transport_metadata.get('irc_channel')
+
+ command_map = {
+ 'PRIVMSG': 'message',
+ 'ACTION': 'action'
+ }
+
+ irc_command = transport_metadata.get('irc_command', 'PRIVMSG')
+ message_content = msg['content']
+ message_type = command_map.get(irc_command)
+
+ if message_type:
+ timestamp = datetime.utcnow()
+
+ user = get_or_create(User, server=server, nickname=nickname)
+ user.last_seen_at = datetime.utcnow()
+ user.put()
+
+ channel = get_or_create(Channel, server=server, channel=channel)
+ msg = Message(user=user, channel=channel, message_type=message_type,
+ message_content=message_content, json=json_data,
+ timestamp=timestamp, user_is_human=user.is_human)
+ msg.put()
+ return msg
+ return ''
+
View
22 sample/create-sample-content.py
@@ -3,13 +3,20 @@
DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'
+
json = """{
- "nickname": "%(nickname)s",
- "server": "irc.freenode.net",
- "channel": "#vumi",
- "message_type": "message",
- "message_content": "testing app engine at %(date)s",
- "timestamp": "%(date)s"
+ "to_addr": "%(nickname1)s!irc.freenode.net",
+ "from_addr": "%(nickname2)s!irc.freenode.net",
+ "content": "testing app engine at %(date)s",
+ "transport_name": "irc_transport",
+ "transport_type": "irc",
+ "transport_metadata": {
+ "transport_nickname": "bot",
+ "irc_server": "irc.freenode.net:6667",
+ "irc_channel": "#vumi",
+ "irc_command": "PRIVMSG",
+ "irc_addressed_to_transport": false
+ }
}""".replace('\n', '')
if len(sys.argv) == 1:
@@ -23,6 +30,7 @@
timestamp = datetime.utcnow() - timedelta(**{interval: i})
payload = json % {
'date': timestamp.strftime(DATE_FORMAT),
- 'nickname': random.sample(['foo', 'bar', 'boo', 'far', 'baz'], 1).pop()
+ 'nickname1': random.sample(['foo', 'bar', 'boo', 'far', 'baz'], 1).pop(),
+ 'nickname2': random.sample(['foo', 'bar', 'boo', 'far', 'baz'], 1).pop(),
}
print "curl -X POST http://localhost:8080/ -d '%s'" % payload
View
18 sample/sample-message.json
@@ -1,8 +1,14 @@
{
- "nickname": "bot",
- "server": "irc.freenode.net",
- "channel": "#vumi",
- "message_type": "message",
- "message_content": "testing app engine",
- "timestamp": "2012-05-16T08:17:53.749184"
+ "to_addr": "bot!irc.freenode.net",
+ "from_addr": "otherbot!irc.freenode.net",
+ "content": "testing app engine",
+ "transport_name": "irc_transport",
+ "transport_type": "irc",
+ "transport_metadata": {
+ "transport_nickname": "bot",
+ "irc_server": "irc.freenode.net:6667",
+ "irc_channel": "#vumi",
+ "irc_command": "PRIVMSG",
+ "irc_addressed_to_transport": false
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.