Skip to content

Commit

Permalink
Moving logging lists, fixing comments, moving code to it's rightful p…
Browse files Browse the repository at this point in the history
…lace
  • Loading branch information
oskopek committed Feb 3, 2014
1 parent 2019075 commit c30fae2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
11 changes: 2 additions & 9 deletions cryptoim/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ def __init__(self, config_file):
CryptoShell init
"""

# super().__init__() # Python 3 only
cmd.Cmd.__init__(self)
self.config = configparser.ConfigParser()
self.config.read(config_file)

import os
if not os.path.exists(config_file):
from os import path
if not path.exists(config_file):
self.config.add_section('friends')
with open(config_file, 'w') as c_file:
self.config.write(c_file)
Expand All @@ -57,12 +56,6 @@ def __init__(self, config_file):
self.xmpp_client = None
self.current_chat = None

# Logging
self.received_msg_list = []
self.received_jid_list = []
self.sent_msg_list = []
self.sent_jid_list = []

# -- basic commands --
def do_exit(self, arg):
"""
Expand Down
57 changes: 33 additions & 24 deletions cryptoim/xmpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@
See the file NOTICE.rst file for copying permission.
"""

import logging
import sleekxmpp
import logging, sleekxmpp

import cryptoim.encryptor_core as encryptor
import cryptoim.decryptor_core as decryptor
import cryptoim.key_exchange as keyex

class CryptoXMPP(sleekxmpp.ClientXMPP):

"""
A simple SleekXMPP client.
A simple SleekXMPP client.
"""

def __init__(self, jid, password, parent):
Expand Down Expand Up @@ -73,6 +71,20 @@ def __init__(self, jid, password, parent):
# Store full JIDs here
self.key_queue = dict()

# Logging
self.received_msg_list = []
self.received_jid_list = []
self.sent_msg_list = []
self.sent_jid_list = []

# Setup the ClientXMPP and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
self.register_plugin('xep_0030') # Service Discovery
self.register_plugin('xep_0004') # Data Forms
self.register_plugin('xep_0060') # PubSub
self.register_plugin('xep_0199') # XMPP Ping

def connected(self, event):
"""
Process the connected event.
Expand Down Expand Up @@ -161,8 +173,8 @@ def message(self, msg):
del q_entry # TODO check if it actually gets removed

# Log:
self.parent.sent_jid_list.append(sender.full)
self.parent.sent_msg_list.append(msg_text)
self.sent_jid_list.append(sender.full)
self.sent_msg_list.append(msg_text)

else:
ciphertext = text
Expand All @@ -173,44 +185,37 @@ def message(self, msg):
del key # TODO check if it actually gets removed

# Log:
self.parent.received_jid_list.append(sender.full)
self.parent.received_msg_list.append(decrypted_message)
self.received_jid_list.append(sender.full)
self.received_msg_list.append(decrypted_message)


class XMPPClient(object):
"""
The XMPP client object, used as a wrapper for the SleekXMPP client.
Used as a simple wrapper for CryptoXMPP
"""
LOG_LEVEL = logging.CRITICAL
xmpp = None

def __init__(self, jid, password, parent, loglevel=LOG_LEVEL):
"""
Initializes the ClientXMPP, logging, etc
Initializes the CryptoXMPP and logging
"""

# Setup logging.
logging.basicConfig(level=loglevel,
format='%(levelname)-8s %(message)s')

# Setup the ClientXMPP and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
# Setup the actual client
self.xmpp = CryptoXMPP(jid, password, parent)
self.xmpp.register_plugin('xep_0030') # Service Discovery
self.xmpp.register_plugin('xep_0004') # Data Forms
self.xmpp.register_plugin('xep_0060') # PubSub
self.xmpp.register_plugin('xep_0199') # XMPP Ping


def connect_server(self, should_block=False, should_reattempt=True):
"""
Connects the ClientXMPP to the server, specify thread blocking.
Connects the CryptoXMPP to the server,
specifying thread blocking and reattempting on failed connection.
"""

# Connect to the XMPP server and start processing XMPP stanzas.
if self.xmpp.connect(reattempt=should_reattempt):
# If you do not have the dnspython library installed, you will need
# TODO If you do not have the dnspython library installed, you will need
# to manually specify the name of the server if it does not match
# the one in the JID. For example, to use Google Talk you would
# need to use:
Expand All @@ -221,21 +226,21 @@ def connect_server(self, should_block=False, should_reattempt=True):

def disconnect_server(self):
"""
Disconnects the ClientXMPP from the server.
Disconnects the CryptoXMPP from the server.
"""

self.xmpp.disconnect(wait=True)

def is_connected(self):
"""
Checks if the ClientXMPP is currently connected to the server.
Checks if the CryptoXMPP is currently connected to the server.
"""

return self.xmpp.is_connected

def is_in_session(self):
"""
Checks if the ClientXMPP is currently in a session
Checks if the CryptoXMPP is currently in a session
"""

return self.xmpp.in_session
Expand All @@ -244,6 +249,7 @@ def send_message(self, recipient, msg):
"""
Sends a chat message to the designated recipient.
"""

prime = keyex.prime_pick()
base = keyex.base_pick()
a = keyex.generate_random(2, 100)
Expand All @@ -252,10 +258,13 @@ def send_message(self, recipient, msg):
self.xmpp.send_message(mto = recipient, mbody = keyex.encode_syn(prime, base, A), mtype = 'chat')
self.xmpp.msg_queue[strip_resource(recipient)] = (msg, prime, a) # Do not store resource in msg_queue

# Tool functions:

def strip_resource(jid):
"""
Strips all the characters after a forward-slash '/', inclusive
"""

if '/' in jid:
jid = jid[:jid.index('/')]
return jid
28 changes: 13 additions & 15 deletions tests/integration/TestXMPP.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def test_send_message():
Test for xmpp.XMPPClient.send_message
"""
xmpp_client, xmpp_client2 = init_xmpp_clients()
crypto_shell = xmpp_client.xmpp.parent
waitForConnection(xmpp_client, True)
waitForConnection(xmpp_client2, True)
waitForSession(xmpp_client, True)
Expand All @@ -67,18 +66,18 @@ def test_send_message():
recipient = xmpp_client2.xmpp.boundjid.full
xmpp_client.send_message(recipient, msg)

waitForNonEmptyList(crypto_shell.sent_msg_list)
waitForNonEmptyList(xmpp_client.xmpp.sent_msg_list)

xmpp_client.disconnect_server()
xmpp_client2.disconnect_server()
waitForConnection(xmpp_client, False)
waitForConnection(xmpp_client2, False)

# Assert that xmpp_client sent the message (it is bound to be sent after disconnect if it waits)
ok_(1 == len(crypto_shell.sent_msg_list))
eq_(len(crypto_shell.sent_jid_list), len(crypto_shell.sent_msg_list))
eq_(msg, crypto_shell.sent_msg_list[-1])
eq_(recipient, crypto_shell.sent_jid_list[-1])
# Assert that xmpp_client sent the message (it is bound to be sent after disconnect if it waits)
ok_(1 == len(xmpp_client.xmpp.sent_msg_list))
eq_(len(xmpp_client.xmpp.sent_jid_list), len(xmpp_client.xmpp.sent_msg_list))
eq_(msg, xmpp_client.xmpp.sent_msg_list[-1])
eq_(recipient, xmpp_client.xmpp.sent_jid_list[-1])

def test_not_connect():
"""
Expand Down Expand Up @@ -116,7 +115,6 @@ def test_receive_message():
Test for CryptoXMPP.message (receive message)
"""
xmpp_client, xmpp_client2 = init_xmpp_clients()
crypto_shell2 = xmpp_client2.xmpp.parent

# Assert connected
waitForConnection(xmpp_client, True)
Expand All @@ -129,7 +127,7 @@ def test_receive_message():
xmpp_client.xmpp.send_message(mto = xmpp_client2.xmpp.boundjid.full, mbody = 'test', mtype = 'error') # Test for dropping non-chat messages
ciphertext = xmpp_client.send_message(xmpp_client2.xmpp.boundjid.full, plaintext)

waitForNonEmptyList(crypto_shell2.received_msg_list)
waitForNonEmptyList(xmpp_client2.xmpp.received_msg_list)

# Disconnect
xmpp_client.disconnect_server()
Expand All @@ -138,13 +136,13 @@ def test_receive_message():
waitForConnection(xmpp_client2, False)

# Assert that xmpp_client2 got it (it is bound to be received after disconnect if it waits)
print(('Received msg list: ', crypto_shell2.received_msg_list))
print(('Received jid list: ', crypto_shell2.received_jid_list))
print(('Received msg list: ', xmpp_client2.xmpp.received_msg_list))
print(('Received jid list: ', xmpp_client2.xmpp.received_jid_list))

ok_(1 == len(crypto_shell2.received_msg_list))
eq_(len(crypto_shell2.received_jid_list), len(crypto_shell2.received_msg_list))
eq_(plaintext, crypto_shell2.received_msg_list[-1])
eq_(xmpp_client.xmpp.boundjid.full, crypto_shell2.received_jid_list[-1])
ok_(1 == len(xmpp_client2.xmpp.received_msg_list))
eq_(len(xmpp_client2.xmpp.received_jid_list), len(xmpp_client2.xmpp.received_msg_list))
eq_(plaintext, xmpp_client2.xmpp.received_msg_list[-1])
eq_(xmpp_client.xmpp.boundjid.full, xmpp_client2.xmpp.received_jid_list[-1])

# Test tools

Expand Down

0 comments on commit c30fae2

Please sign in to comment.