Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes
jabberbot.xmppbot.Contact Class Reference

List of all members.

Public Member Functions

def __init__
def is_valid
def add_resource
def set_supports
def supports
def max_prio_resource
def remove_resource
def is_dnd
def set_show
def uses_resource
def __str__

Public Attributes

 jid
 resources
 language
 last_online
 messages

Static Public Attributes

int default_ttl = 3600

Detailed Description

Abstraction of a roster item / contact

This class handles some logic related to keeping track of
contact availability, status, etc.

Definition at line 30 of file xmppbot.py.


Constructor & Destructor Documentation

def jabberbot.xmppbot.Contact.__init__ (   self,
  jid,
  resource,
  priority,
  show,
  language = None 
)

Definition at line 40 of file xmppbot.py.

00040 
00041     def __init__(self, jid, resource, priority, show, language=None):
00042         self.jid = jid
00043         self.resources = {resource: {'show': show, 'priority': priority, 'supports': []}}
00044         self.language = language
00045 
00046         # The last time when this contact was seen online.
00047         # This value has meaning for offline contacts only.
00048         self.last_online = None
00049 
00050         # Queued messages, waiting for contact to change its "show"
00051         # status to something different than "dnd". The messages should
00052         # also be sent when contact becomes "unavailable" directly from
00053         # "dnd", as we can't guarantee, that the bot will be up and running
00054         # the next time she becomes "available".
00055         self.messages = []


Member Function Documentation

Definition at line 160 of file xmppbot.py.

00160 
00161     def __str__(self):
00162         retval = "%s (%s) has %d queued messages"
00163         res = ", ".join([name + " is " + res['show'] for name, res in self.resources.items()])
00164         return retval % (self.jid.as_unicode(), res, len(self.messages))
00165 

def jabberbot.xmppbot.Contact.add_resource (   self,
  resource,
  show,
  priority 
)
Adds information about a connected resource

@param resource: resource name
@param show: a show presence property, as defined in XMPP
@param priority: priority of the given resource

Definition at line 65 of file xmppbot.py.

00065 
00066     def add_resource(self, resource, show, priority):
00067         """Adds information about a connected resource
00068 
00069         @param resource: resource name
00070         @param show: a show presence property, as defined in XMPP
00071         @param priority: priority of the given resource
00072 
00073         """
00074         self.resources[resource] = {'show': show, 'priority': priority, 'supports': []}
00075         self.last_online = None

Checks if contact is DoNotDisturb

The contact is DND if its resource with the highest priority is DND

Definition at line 129 of file xmppbot.py.

00129 
00130     def is_dnd(self):
00131         """Checks if contact is DoNotDisturb
00132 
00133         The contact is DND if its resource with the highest priority is DND
00134 
00135         """
00136         max_prio_res = self.max_prio_resource()
00137 
00138         # If there are no resources the contact is offline, not dnd
00139         if max_prio_res:
00140             return max_prio_res['show'] == u"dnd"
00141         else:
00142             return False

Here is the call graph for this function:

def jabberbot.xmppbot.Contact.is_valid (   self,
  current_time 
)
Check if this contact entry is still valid and should be kept

@param time: current time in seconds

Definition at line 56 of file xmppbot.py.

00056 
00057     def is_valid(self, current_time):
00058         """Check if this contact entry is still valid and should be kept
00059 
00060         @param time: current time in seconds
00061 
00062         """
00063         # No resources == offline
00064         return self.resources or current_time < self.last_online + self.default_ttl

Returns the resource (dict) with the highest priority

@return: highest priority resource or None if contacts is offline
@rtype: dict or None

Definition at line 93 of file xmppbot.py.

00093 
00094     def max_prio_resource(self):
00095         """Returns the resource (dict) with the highest priority
00096 
00097         @return: highest priority resource or None if contacts is offline
00098         @rtype: dict or None
00099 
00100         """
00101         if not self.resources:
00102             return None
00103 
00104         # Priority can't be lower than -128
00105         max_prio = -129
00106         selected = None
00107 
00108         for resource in self.resources.itervalues():
00109             # TODO: check RFC for behaviour of 2 resources with the same priority
00110             if resource['priority'] > max_prio:
00111                 max_prio = resource['priority']
00112                 selected = resource
00113 
00114         return selected

Here is the caller graph for this function:

def jabberbot.xmppbot.Contact.remove_resource (   self,
  resource 
)
Removes information about a connected resource

@param resource: resource name

Definition at line 115 of file xmppbot.py.

00115 
00116     def remove_resource(self, resource):
00117         """Removes information about a connected resource
00118 
00119         @param resource: resource name
00120 
00121         """
00122         if self.resources.has_key(resource):
00123             del self.resources[resource]
00124         else:
00125             raise ValueError("No such resource!")
00126 
00127         if not self.resources:
00128             self.last_online = time.time()

def jabberbot.xmppbot.Contact.set_show (   self,
  resource,
  show 
)
Sets show property for a given resource

@param resource: resource to alter
@param show: new value of the show property
@raise ValueError: no resource with given name has been found

Definition at line 143 of file xmppbot.py.

00143 
00144     def set_show(self, resource, show):
00145         """Sets show property for a given resource
00146 
00147         @param resource: resource to alter
00148         @param show: new value of the show property
00149         @raise ValueError: no resource with given name has been found
00150 
00151         """
00152         if self.resources.has_key(resource):
00153             self.resources[resource]['show'] = show
00154         else:
00155             raise ValueError("There's no such resource")

def jabberbot.xmppbot.Contact.set_supports (   self,
  resource,
  extension 
)
Flag a given resource as supporting a particular extension

Definition at line 76 of file xmppbot.py.

00076 
00077     def set_supports(self, resource, extension):
00078         """Flag a given resource as supporting a particular extension"""
00079         self.resources[resource]['supports'].append(extension)

def jabberbot.xmppbot.Contact.supports (   self,
  resource,
  extension 
)
Check if a given resource supports a particular extension

If no resource is specified, check the resource with the highest
priority among currently connected.

Definition at line 80 of file xmppbot.py.

00080 
00081     def supports(self, resource, extension):
00082         """Check if a given resource supports a particular extension
00083 
00084         If no resource is specified, check the resource with the highest
00085         priority among currently connected.
00086 
00087         """
00088         if resource and resource in self.resources:
00089             return extension in self.resources[resource]['supports']
00090         else:
00091             resource = self.max_prio_resource()
00092             return resource and extension in resource['supports']

Here is the call graph for this function:

def jabberbot.xmppbot.Contact.uses_resource (   self,
  resource 
)
Checks if contact uses a given resource

Definition at line 156 of file xmppbot.py.

00156 
00157     def uses_resource(self, resource):
00158         """Checks if contact uses a given resource"""
00159         return self.resources.has_key(resource)


Member Data Documentation

Definition at line 38 of file xmppbot.py.

Definition at line 41 of file xmppbot.py.

Definition at line 43 of file xmppbot.py.

Definition at line 47 of file xmppbot.py.

Definition at line 54 of file xmppbot.py.

Definition at line 42 of file xmppbot.py.


The documentation for this class was generated from the following file: