home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from __future__ import with_statement
- from contextlib import contextmanager
- import oscar.snac as snac
- import oscar
- import common
- import struct
- from oscar.ssi import item, OscarSSIs, SSIException, ssi_err_types
- from util import gen_sequence, lookup_table, callsback
- from oscar.ssi.SSIItem import tuple_key
- from logging import getLogger
- log = getLogger('oscar.ssis')
- property_tlv_types = lookup_table(alias = 305, email = 311, SMSnum = 314, comment = 316)
-
- def _lowerstrip(s):
- return oscar._lowerstrip(s)
-
-
- class SSIManager(object):
-
- def __init__(self, protocol):
- self.o = protocol
- self.ssis = OscarSSIs(self)
- self.ssi_edits_out = 0
- self.generated_ssi_ids = []
-
-
- def add_modify(self, new_ssi):
- yield None
- me = None
- self.ssi_edit().__enter__()
-
- try:
- if new_ssi in self.ssis:
- self._modify_ssi(new_ssi, me())
- else:
- self._add_ssi(new_ssi, me())
- finally:
- pass
-
- yield None
- errors = self.ssi_edit().__exit__
- if not errors[0]:
- self.ssis[new_ssi] = new_ssi
- else:
- raise ','.join([] + []([ ssi_err_types[err] for err in errors ]))
- return self.ssi_edit()
-
- add_modify = gen_sequence(add_modify)
-
- def new_ssi_item_id(self, group):
- newid = 256
- used_ids = set(self.generated_ssi_ids + self.ssis.keys())
- while (group, newid) in used_ids:
- newid += 1
- self.generated_ssi_ids += [
- (group, newid)]
- return newid
-
-
- def new_ssi_group_id(self):
- newid = 256
- used_ids = set(self.generated_ssi_ids + self.ssis.keys())
- while (newid, 0) in used_ids:
- newid += 1
- self.generated_ssi_ids += [
- (newid, 0)]
- return newid
-
-
- def get_ssis_in_group(self, group_id, with_group = False):
- if with_group:
- return _[1]
- return _[2]
-
-
- def get_ssis_by_type(self, type):
- return _[1]
-
-
- def _ssi_action(self, action, new_ssis, parent):
- yield None
- me = None
- self.ssi_edit().__enter__()
-
- try:
- self.o.send_snac(19, action, ''.join((lambda .0: for s in .0:
- s.to_bytes())(new_ssis)), req = True, cb = me.send)
- finally:
- pass
-
- yield None
- self.ssi_edit().__exit__(parent.send(self.o.gen_incoming))
-
- _ssi_action = gen_sequence(_ssi_action)
-
- def _ssi_double_action(self, action1, new_ssis1, action2, new_ssis2, parent):
- yield None
- me = None
- self.ssi_edit().__enter__()
-
- try:
- if isinstance(new_ssis2, item):
- new_ssis2 = [
- new_ssis2]
-
- self.o.send_snac(19, action1, ''.join((lambda .0: for s in .0:
- s.to_bytes())(new_ssis1)), 5, True, me.send, action1)
- self.o.send_snac(19, action2, ''.join((lambda .0: for s in .0:
- s.to_bytes())(new_ssis2)), 5, True, me.send, action2)
- finally:
- pass
-
- yield None
- (sock1, snac1, ret_action1) = self.ssi_edit().__exit__
- yield None
- (sock2, snac2, ret_action2) = self.ssi_edit()
- incoming1 = self.o.gen_incoming((sock1, snac1))
- incoming2 = self.o.gen_incoming((sock2, snac2))
- parent.send((incoming1, ret_action1, incoming2, ret_action2))
-
- _ssi_double_action = gen_sequence(_ssi_double_action)
-
- def _add_ssi(self, *a, **k):
- self._ssi_action(8, *a, **k)
-
-
- def _modify_ssi(self, *a, **k):
- self._ssi_action(9, *a, **k)
-
-
- def _remove_ssi(self, *a, **k):
- self._ssi_action(10, *a, **k)
-
-
- def add_new_ssi(self, name, group_protocol_object = None, position = 0, type_ = None, authorization = False, callback = None):
- yield None
- me = None
- if not isinstance(name, str):
- name = name.encode('utf-8')
-
- if group_protocol_object is not None:
- group_protocol_object = tuple_key(group_protocol_object)
- group_id = group_protocol_object[0]
- item_id = self.new_ssi_item_id(group_protocol_object[0])
- if type_ is None:
- type_ = 0
-
- else:
- group_id = self.new_ssi_group_id()
- item_id = 0
- if type_ is None:
- type_ = 1
-
- new_ssi = item(name, group_id, item_id, type_)
- if group_protocol_object is not None and authorization:
- new_ssi.tlvs[102] = ''
-
- self.ssi_edit().__enter__()
-
- try:
- yield self._add_ssi(new_ssi, me())
- errors = self.ssi_edit().__exit__
- if not errors[0]:
- self.ssis[new_ssi] = new_ssi
- if not group_protocol_object:
- pass
- ids = (0, 0)
- id_to_add = self.ssi_edit() if group_protocol_object else new_ssi.group_id
- self._add_to_group(ids, id_to_add, position)
- else:
- callback.error()
- if errors[0] != 14:
- raise ','.join([] % ([]([ ssi_err_types[err] for err in errors ]), new_ssi))
- errors[0] != 14
- finally:
- pass
-
-
- try:
- log.info(','.join((lambda .0: for g in .0:
- g.name)(self.ssis.root_group)))
- except:
- self.ssi_edit().__exit__
- self.ssi_edit()
- log.error('error repr-ing groups')
-
- self.ssis.root_group.notify()
- callback.success(new_ssi)
-
- add_new_ssi = gen_sequence(callsback(add_new_ssi))
-
- def add_privacy_record(self, buddy, type_, callback = None):
- yield None
- me = None
- name = common.get_bname(buddy)
- self.ssi_edit().__enter__()
-
- try:
- if not (self.find,)((lambda s: _lowerstrip(s.name) == _lowerstrip(name)), type = type_):
- log.critical('adding ' + name + ' to your privacy list')
- buddy_ssi = item(name, 0, self.new_ssi_item_id(0), type_)
- yield self._add_ssi(buddy_ssi, me())
- buddy_errs = self.ssi_edit().__exit__
- log.critical('ACK PRIVACY MOD!' + name)
- if not buddy_errs[0]:
- self.ssis[buddy_ssi] = buddy_ssi
- else:
- callback.error()
- raise ','.join([] + []([ ssi_err_types[err] for err in buddy_errs ]))
- buddy_errs[0]
- callback.error()
- raise SSIException('Buddy already in that privacy list.')
- finally:
- pass
-
- callback.success()
- self.ssis.root_group.notify()
-
- add_privacy_record = gen_sequence(callsback(add_privacy_record))
-
- def block_buddy(self, buddy, callback = None):
- self.add_privacy_record(buddy, oscar.ssi.deny_flag, callback = callback)
-
- block_buddy = callsback(block_buddy)
-
- def allow_buddy(self, buddy):
- self.add_privacy_record(buddy, oscar.ssi.permit_flag)
-
-
- def ignore_buddy(self, buddy, callback = None):
- self.add_privacy_record(buddy, 14, callback = callback)
-
- ignore_buddy = callsback(ignore_buddy)
-
- def unignore_buddy(self, buddy, callback = None):
- self.remove_privacy_record(buddy, type_ = 14, callback = callback)
-
- unignore_buddy = callsback(unignore_buddy)
-
- def remove_privacy_record(self, buddy, type_, callback = None):
- yield None
- me = None
- name = _lowerstrip(common.get_bname(buddy))
- buds_matching = self.find(name = name, type = type_)
- errors = []
- if buds_matching:
- self.ssi_edit().__enter__()
-
- try:
- log.critical('REMOVING PRIVACY RECORD FOR ' + name)
- self._remove_ssi(buds_matching, me())
- finally:
- pass
-
- yield None
- errors = self.ssi_edit().__exit__
-
- for err, ssi in zip(errors, buds_matching):
- if not err:
- del self.ssis[ssi]
- continue
-
- real_errors = filter(None, errors)
- if not buds_matching:
- log.critical("Can't remove privacy record; no ssi in root group for %r (type = %r).", name, type_)
-
- if real_errors:
- callback.error()
- raise ','.join([] + []([ ssi_err_types[err] for err in errors ]))
- real_errors
- callback.success()
- self.o.buddies[name].notify('blocked')
- self.ssis.root_group.notify()
-
- remove_privacy_record = gen_sequence(callsback(remove_privacy_record))
-
- def unblock_buddy(self, buddy, callback = None):
- self.remove_privacy_record(buddy, oscar.ssi.deny_flag, callback = callback)
-
- unblock_buddy = callsback(unblock_buddy)
-
- def unallow_buddy(self, buddy):
- self.remove_privacy_record(buddy, oscar.ssi.permit_flag)
-
-
- def get_privacy_ssi(self):
- PDINFO = 4
- privacy_infos = _[1]
- if len(privacy_infos) == 0:
- pinfo_ssi = item('', 0, self.new_ssi_item_id(0), PDINFO)
- elif len(privacy_infos) == 1:
- pinfo_ssi = privacy_infos[0]
- else:
- log.critical('There was more than one privacy SSI:')
- log.critical(str(privacy_infos))
- raise SSIException('There was more than one privacy SSI:')
- return []
-
-
- def blocklist(self):
- return _[1]
-
-
- def ignorelist(self):
- return _[1]
-
-
- def find(self, f = (lambda x: True), **kwds):
- results = []
- for ssi in self.ssis.values():
- for kwd in kwds:
- if kwd == 'name':
- if _lowerstrip(getattr(ssi, kwd, sentinel)) != _lowerstrip(kwds[kwd]):
- break
-
- _lowerstrip(getattr(ssi, kwd, sentinel)) != _lowerstrip(kwds[kwd])
- if getattr(ssi, kwd, sentinel) != kwds[kwd]:
- break
- continue
- elif f(ssi):
- results.append(ssi)
- continue
-
- return results
-
-
- def _add_to_group(self, group_ids, id_to_add, position):
- yield None
- me = None
-
- try:
- groupclone = self.ssis[group_ids].clone()
- except KeyError:
- raise SSIException('Could not find SSI with group_id == %r', group_ids)
-
- groupclone.add_item_to_group(id_to_add, position)
- yield self._modify_ssi(groupclone, me())
- errors = None
- if errors[0] == 0:
- self.ssis[group_ids].add_item_to_group(id_to_add, position)
- else:
- raise ', '.join([] + []([ ssi_err_types[err] for err in errors ]))
- (errors[0] == 0).ssis.root_group.notify()
-
- _add_to_group = gen_sequence(_add_to_group)
-
- def _remove_from_group(self, key):
- yield None
- me = None
- (group_id, item_id) = tuple_key(key)
- log.info('removing (%d, %d)', group_id, item_id)
- if not item_id:
- group_id = item_id
- item_id = group_id
-
- group_clone = self.ssis[(group_id, 0)].clone()
- group_clone.remove_item_from_group(item_id)
- yield self._modify_ssi(group_clone, me())
- error = None
- if not error[0]:
- self.ssis[(group_id, 0)].remove_item_from_group(item_id)
- else:
- raise ','.join([] + []([ ssi_err_types[err] for err in error ]))
- error[0].ssis.root_group.notify()
-
- _remove_from_group = gen_sequence(_remove_from_group)
-
- def remove_group(self, group_protocol_object):
- yield None
- me = None
- group_protocol_object = getattr(group_protocol_object, 'id', group_protocol_object)
- ssis_to_del = self.get_ssis_in_group(group_protocol_object)
- log.info('Going to remove: %r', ssis_to_del)
- group_to_del = self.ssis[tuple_key(group_protocol_object)]
- groupclone = group_to_del.clone()
- groupclone.tlvs = { }
- ssis_to_del.append(groupclone)
- self.ssi_edit().__enter__()
-
- try:
- self._remove_ssi(ssis_to_del, me())
- yield None
- errors = self.ssi_edit().__exit__
- for ssi, error in zip(ssis_to_del, errors):
- if not error and ssi in self.ssis:
- del self.ssis[ssi]
- continue
- self.ssi_edit()
-
- if group_protocol_object not in self.ssis:
- self._remove_from_group(group_protocol_object)
- finally:
- pass
-
- real_errors = filter(None, errors)
- if real_errors:
- raise SSIException('Error removing group from list: ' + ','.join((lambda .0: for err in .0:
- ssi_err_types[err])(real_errors)))
- real_errors
- self.ssis.root_group.notify()
-
- remove_group = gen_sequence(remove_group)
-
- def remove_buddy_ssi(self, ids, callback = None):
- yield None
- me = None
- self.ssi_edit().__enter__()
-
- try:
- buddy_clone = self.ssis[ids].clone()
- yield self._remove_ssi(buddy_clone, me())
- error = self.ssi_edit().__exit__
- if not error[0]:
- self._remove_from_group(ids)
- del self.ssis[ids]
- callback.success()
- else:
- callback.error()
- raise ','.join([] + []([ ssi_err_types[err] for err in error ]))
- self.ssis.root_group.notify()
- return None
-
-
- remove_buddy_ssi = gen_sequence(callsback(remove_buddy_ssi))
-
- def rename_ssi(self, protocol_object, name):
- yield None
- me = None
- new_ssi = self.ssis[protocol_object].clone()
- new_ssi.name = name.encode('utf-8')
- yield self._modify_ssi(new_ssi, me())
- errors = None
- if errors[0]:
- raise ','.join([] + []([ ssi_err_types[err] for err in errors ]))
- errors[0]
- ssiobj = self.ssis[protocol_object]
- ssiobj.name = name.encode('utf-8')
- self.ssis.get_group(protocol_object).set_ssi(ssiobj)
- self.ssis.root_group.notify()
-
- rename_ssi = gen_sequence(rename_ssi)
-
- def alias_ssi(self, contact, name):
- yield None
- me = None
- buddy = contact.buddy
- id = contact.id
- new_ssi = self.ssis[id].clone()
- name = None if name else None
- new_ssi.set_alias(name)
- yield self._modify_ssi(new_ssi, me())
- errors = None
- if errors[0]:
- raise SSIException('Error setting alias: ' + ','.join((lambda .0: for err in .0:
- ssi_err_types[err])(errors)))
- errors[0]
- self.ssis[id].set_alias(name)
- self.ssis.root_group.notify()
-
- alias_ssi = gen_sequence(alias_ssi)
-
- def move_ssi_to_position(self, item_ids, position, group_to_ids = None, callback = None):
- yield None
- me = None
- item_ids = tuple_key(item_ids)
- if group_to_ids:
- group_to_ids = tuple_key(group_to_ids)
- if group_to_ids[1]:
- raise SSIException("Can't move items into something which is not a group.")
- group_to_ids[1]
-
- if not item_ids[0]:
- raise AssertionError('Atttempted to move something in the ' + 'SSI root group (this is impossible, ' + "since they don't have position).")
- item_ids[0]
- if not item_ids[1]:
- group_from_ids = (0, 0)
- if group_to_ids and group_to_ids != (0, 0):
- raise SSIException("Can't move group into a group which is not the root group.")
- group_to_ids != (0, 0)
- id_to_move = item_ids[0]
- else:
- group_from_ids = (item_ids[0], 0)
- id_to_move = item_ids[1]
- if not group_to_ids or group_from_ids == group_to_ids:
- groupclone = self.ssis[group_from_ids].clone()
- groupclone.move_item_to_position(id_to_move, position)
- yield self._modify_ssi(groupclone, me())
- errors = None
- if not errors[0]:
- self.ssis[group_from_ids].move_item_to_position(id_to_move, position)
- else:
- raise ','.join([] + []([ ssi_err_types[err] for err in errors ]))
- errors[0]
- del id_to_move
- if not group_to_ids[0]:
- raise AssertionError('atttempted to move something to the ' + 'SSI root group (this is impossible, ' + "since they don't have position)")
- group_to_ids[0]
- old_ssi = self.ssis[item_ids]
- new_ssi = old_ssi.clone()
- new_ssi.group_id = group_to_ids[0]
- new_ssi.item_id = self.new_ssi_item_id(group_to_ids[0])
- self.ssi_edit().__enter__()
-
- try:
- yield self._ssi_double_action(10, old_ssi, 8, new_ssi, me())
- (del_errors, action1, add_errors, action2) = self.ssi_edit().__exit__
- if action1 != 10:
- del_errors = add_errors
- add_errors = del_errors
-
- (del_group_clone, add_group_clone) = (None, None)
- if not del_errors[0]:
- del self.ssis[old_ssi]
- del_group_clone = self.ssis[(old_ssi.group_id, 0)].clone()
- del_group_clone.remove_item_from_group(old_ssi.item_id)
-
- if not add_errors[0]:
- self.ssis[new_ssi] = new_ssi
- add_group_clone = self.ssis[(new_ssi.group_id, 0)].clone()
- add_group_clone.add_item_to_group(new_ssi.item_id, position)
-
- mod_ssis = filter(None, [
- del_group_clone,
- add_group_clone])
- self._modify_ssi(mod_ssis, me())
- finally:
- pass
-
- yield None
- mod_errors = self.ssi_edit().__exit__
- del_mod_error = None
- add_mod_error = None
- if not del_errors[0]:
- del_mod_error = mod_errors[0]
- mod_errors = mod_errors[1:]
- if not del_mod_error:
- self.ssis[(old_ssi.group_id, 0)].remove_item_from_group(old_ssi.item_id)
-
-
- if not add_errors[0]:
- add_mod_error = mod_errors[0]
- if not add_mod_error:
- self.ssis[(new_ssi.group_id, 0)].add_item_to_group(new_ssi.item_id, position)
-
-
- errors = filter(None, (add_errors[0], del_errors[0], del_mod_error, add_mod_error))
- if errors:
- err_string = ''
- if del_errors[0]:
- if err_string:
- err_string += ', '
-
- err_string += 'deleting ' + old_ssi.name + ' in group ' + self.ssis[(old_ssi.group_id, 0)].name
-
- if del_mod_error:
- if err_string:
- err_string += ', '
-
- err_string += 'removing ' + old_ssi.name + ' from group ' + self.ssis[(old_ssi.group_id, 0)].name + ' list'
-
- if add_errors[0]:
- if err_string:
- err_string += ', '
-
- err_string += 'adding ' + old_ssi.name + ' in group ' + self.ssis[(new_ssi.group_id, 0)].name
-
- if add_mod_error:
- if err_string:
- err_string += ', '
-
- err_string += 'adding ' + old_ssi.name + ' to group ' + self.ssis[(new_ssi.group_id, 0)].name + ' list'
-
- callback.error()
- raise err_string(','.join % ([], []([ ssi_err_types[err] for err in errors ])))
- errors
- callback.success((new_ssi.group_id, new_ssi.item_id))
- self.ssis.root_group.notify()
-
- move_ssi_to_position = gen_sequence(callsback(move_ssi_to_position))
-
- def _edit_server_list_start(self, import_transaction = False):
- if not self.ssi_edits_out:
- self.o.send_snac(*snac.send_x13_x11(import_transaction))
-
- self.ssi_edits_out += 1
-
-
- def _edit_server_list_end(self):
- self.ssi_edits_out -= 1
- if not self.ssi_edits_out:
- self.o.send_snac(*snac.send_x13_x12())
-
-
-
- def ssi_edit(self, import_transaction = False):
- self._edit_server_list_start(import_transaction)
-
- try:
- yield self
- finally:
- self._edit_server_list_end()
-
-
- ssi_edit = contextmanager(ssi_edit)
-
-