Skip to content
Snippets Groups Projects
Commit 21b0898e authored by zimio's avatar zimio
Browse files

Fix on_offer. Start fixing handler

parent b0f9e19b
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ except ImportError:
# Namespace for file sharing
NS_FILE_SHARING = 'urn:xmpp:fis'
class Protocol():
'''
Creates and extracts information from stanzas
......@@ -29,25 +30,19 @@ class Protocol():
query.setAttr('node', path)
return iq
def buildReply(self, typ, stanza):
iq = nbxmpp.Iq(typ, to=stanza.getFrom(), frm=stanza.getTo(),
attrs={'id': stanza.getID()})
iq.addChild(name='match', namespace=NS_FILE_SHARING)
return iq
def buildFileNode(self, file_info):
node = nbxmpp.Node(tag='file')
node.setNamespace(nbxmpp.NS_JINGLE_FILE_TRANSFER)
if not file_info['name']:
if not 'name' in file_info:
raise Exception("Child name is required.")
node.addChild(name='name').setData(file_info['name'])
if file_info['date']:
if 'date' in file_info:
node.addChild(name='date').setData(file_info['date'])
if file_info['desc']:
if 'desc' in file_info:
node.addChild(name='desc').setData(file_info['desc'])
if file_info['size']:
if 'size' in file_info:
node.addChild(name='size').setData(file_info['size'])
if file_info['hash']:
if 'hash' in file_info:
h = Hashes()
h.addHash(file_info['hash'], 'sha-1')
node.addChild(node=h)
......@@ -90,29 +85,28 @@ class ProtocolDispatcher():
self.fsw = fsw
def set_window(self, window):
self.fsw = window
def handler(self, stanza):
def handler(self, stanza, fjid):
# handles incoming match stanza
if stanza.getTag('match').getTag('offer'):
self.on_offer(stanza)
elif stanza.getTag('match').getTag('request'):
self.on_request(stanza)
# TODO: Stanza checking
if stanza.getType() == 'get':
self.on_request(stanza, fjid)
elif stanza.getType() == 'result':
return self.on_offer(stanza, fjid)
else:
# TODO: reply with malformed stanza error
pass
def on_request(self, stanza):
def on_request(self, stanza, fjid):
'''
try:
fjid = helpers.get_full_jid_from_iq(stanza)
except helpers.InvalidFormat:
# A message from a non-valid JID arrived, it has been ignored.
return
return -1
'''
if stanza.getTag('error'):
# TODO: better handle this
return
return -1
jid = gajim.get_jid_without_resource(fjid)
req = stanza.getTag('match').getTag('request')
if req.getTag('directory') and not \
......@@ -126,44 +120,24 @@ class ProtocolDispatcher():
files = self.plugin.database.get_files_from_dir(self.account, jid, dir_)
response = self.offer(stanza.getID(), fjid, files)
self.conn.connection.send(response)
def on_offer(self, stanza):
# We just got a stanza offering files
fjid = helpers.get_full_jid_from_iq(stanza)
info = get_files_info(stanza)
if fjid not in self.fsw.browse_jid or not info:
# We weren't expecting anything from this contact, do nothing
# Or we didn't receive any offering files
return
flist = []
for f in info[0]:
flist.append(f['name'])
flist.extend(info[1])
self.fsw.browse_fref = self.fsw.add_file_list(flist, self.fsw.ts_search,
self.fsw.browse_fref,
self.fsw.browse_jid[fjid]
)
for f in info[0]:
iter_ = self.fsw.browse_fref[f['name']]
path = self.fsw.ts_search.get_path(iter_)
self.fsw.brw_file_info[path] = (f['name'], f['date'], f['size'],
f['hash'], f['desc'])
# TODO: add tooltip
'''
for f in info[0]:
r = self.fsw.browse_fref[f['name']]
path = self.fsw.ts_search.get_path(r)
# AM HERE WORKING ON THE TOOLTIP
tooltip.set_text('noooo')
self.fsw.tv_search.set_tooltip_row(tooltip, path)
'''
for dir_ in info[1]:
if dir_ not in self.fsw.empty_row_child:
parent = self.fsw.browse_fref[dir_]
row = self.fsw.ts_search.append(parent, ('',))
self.fsw.empty_row_child[dir_] = row
return 0
def on_offer(self, stanza, fjid):
offered = []
query = stanza.getQuery()
for child in query.getChildren():
if child.getName() == 'directory':
offered.append({'name' : child.getAttr('name'),
'type' : 'directory'})
elif child.getName() == 'file':
attrs = {'type' : 'file'}
grandchildren = child.getChildren()
for grandchild in grandchildren:
attrs[grandchild.getName()] = grandchild.getData()
offered.append(attrs)
else:
print 'File sharing. Cant handle unknown type: ' + str(child)
return offered
def get_files_info(stanza):
......
......@@ -2,7 +2,7 @@
import unittest
from mock import Mock
import sys, os
sys.path.append(os.path.abspath(sys.path[0]) + '/../')
......@@ -24,7 +24,6 @@ class TestProtocol(unittest.TestCase):
def test_buildFileNode(self):
file_info = {'name' : 'test2.text',
'date' : '00000',
'desc' : 'test',
'hash' : '00000',
'size' : '00000',
......@@ -33,11 +32,10 @@ class TestProtocol(unittest.TestCase):
node = self.protocol.buildFileNode(file_info)
self.assertEqual(node.getName(), 'file')
self.assertEqual(node.getNamespace(), nbxmpp.NS_JINGLE_FILE_TRANSFER)
self.assertEqual(len(node.getChildren()), 5)
self.assertEqual(len(node.getChildren()), 4)
def test_offer(self):
items = [ {'name' : 'test2.text',
'date' : '00000',
'desc' : 'test',
'hash' : '00000',
'size' : '00000',
......@@ -59,10 +57,52 @@ class TestProtocol(unittest.TestCase):
self.assertEqual(len(node.getChildren()), 2)
# Mock modules
fshare_protocol.gajim = Mock()
fshare_protocol.helpers = Mock()
def test_reply(self):
pass
class TestProtocolDispatcher(unittest.TestCase):
def setUp(self):
account = 'test@gajim.org/test'
testc = {account : Mock()}
fshare_protocol.gajim.connections = testc
self.dispatcher = fshare_protocol.ProtocolDispatcher(
account, Mock())
def test_handler(self):
protocol = fshare_protocol.Protocol('test@gajim.org/test')
iq = protocol.request('peer@gajim.org/test', '1234',
'documents/test2.txt')
#offer = self.dispatcher.on_offer
request = self.dispatcher.on_request
#self.dispatcher.on_offer = Mock()
self.dispatcher.on_request = Mock()
self.dispatcher.handler(iq, 'peer@gajim.org/test')
assert(self.dispatcher.on_request.called)
self.dispatcher.on_request = request
def test_offer(self):
from fshare_protocol import OfferHandled
items = [ {'name' : 'test2.text',
'type' : 'file'
},
{
'name' : 'secret docs',
'type' : 'directory'
}
]
protocol = fshare_protocol.Protocol('test@gajim.org/test')
iq = protocol.offer('1234', 'peer@gajim.org/test',
'documents', items)
offered_files = self.dispatcher.on_offer(iq, 'peer@gajim.org/test')
self.assertEqual(len(offered_files), 2)
def test_request(self):
pass
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment