def send_message(self, to, message, chat_type="chatbox"): """ Send a chat message """ pm = getToolByName(self.context, "portal_membership") if pm.isAnonymousUser(): return json.dumps({"status": config.AUTH_FAIL}) server = utils.get_connection(self.context) if server is None: return json.dumps({"status": config.SERVER_ERROR}) member = pm.getAuthenticatedMember() if not hasattr(member, "chatpass"): return json.dumps({"status": config.AUTH_FAIL}) message = cgi.escape(message) message = utils.urlize(message, blank=True, auto_escape=False) password = getattr(member, "chatpass") username = member.getId() fullname = member.getProperty("fullname") or username log.debug(u"Chat message from %s sent to %s" % (username, to)) server = utils.get_connection(self.context) if server is None: return json.dumps({"status": config.SERVER_ERROR}) if chat_type == "chatroom": func = server.sendChatRoomMessage else: func = server.sendMessage try: resp = func(username, password, fullname, to, message) except xmlrpclib.Fault, e: log.warn("Error from chat.service: sendMessage: %s" % e) return json.dumps({"status": config.SERVER_ERROR})
def get_uncleared_messages(self, audience="*", chatrooms="*", until=None, mark_cleared=False): """ Retrieve the uncleared messages from the chat server. If audience == '*', messages from all conversation partners are returned. """ pm = getToolByName(self.context, "portal_membership") if pm.isAnonymousUser(): return json.dumps({"status": config.AUTH_FAIL}) member = pm.getAuthenticatedMember() username = member.getId() if hasattr(member, "chatpass"): password = getattr(member, "chatpass") else: log.warn( "The member %s is registered in the Chat Service, but " "doesn't have his password anymore. This could be because an " "existing Chat Service is being used with a new Plone instance. " "Deleting the user's entry in the Chat Service's acl_users and " "folder in 'users' should fix this problem" % username ) # This will raise an attribute error password = getattr(member, "chatpass") server = utils.get_connection(self.context) if server is None: return json.dumps({"status": config.SERVER_ERROR}) try: # self, username, password, partner, chatrooms, clear resp = server.getUnclearedMessages(username, password, audience, chatrooms, until, mark_cleared) except xmlrpclib.Fault, e: err_msg = e.faultString.strip("\n").split("\n")[-1] log.warn("Error from chat.service: getUnclearedMessages: %s" % err_msg) return json.dumps({"status": config.SERVER_ERROR})
def initialize(self, username=None): """ Check if the user is registered, and register if not... """ pm = getToolByName(self.context, "portal_membership") if pm.isAnonymousUser(): return json.dumps(config.AUTH_FAIL_RESPONSE) if username is None: member = pm.getAuthenticatedMember() username = member.getId() else: member = pm.getMemberById(username) log.debug("initialize called, username: %s" % username) server = utils.get_connection(self.context) if server is None: return json.dumps({"status": config.SERVER_ERROR}) try: resp = json.loads(server.isRegistered(username)) except socket.error, e: # Catch socket errors: timeouts connection refusede etc. # so that we can notify the caller log.warn("Socket error for %s %s" % (username, e)) # We return the same output as the poll would have # returned... return json.dumps({"status": config.TIMEOUT})
def poll(self, username, since): """ Poll the chat server to retrieve new online users and chat messages """ if not isinstance(username, str) and not isinstance(username, unicode): return json.dumps({"status": config.AUTH_FAIL}) context = self.context password = None cache = getattr(self, "_v_user_password_dict", {}) password = getattr(cache, username.encode("utf-8"), None) if password is None: pm = getToolByName(context, "portal_membership") member = pm.getMemberById(username) if not member or not hasattr(member, "chatpass"): return json.dumps({"status": config.AUTH_FAIL}) password = getattr(member, "chatpass") cache[username.encode("utf-8")] = password server = utils.get_connection(context) if server is None: return json.dumps({"status": config.SERVER_ERROR}) try: server.confirmAsOnline(username) return server.getNewMessages(username, password, since) except socket.timeout: # Catch timeouts so that we can notify the caller log.warn("poll: timeout error for %s" % username) return json.dumps({"status": config.TIMEOUT_RESPONSE}) except xmlrpclib.Fault, e: err_msg = e.faultString log.warn("Error from chat.service: getNewMessages: %s" % err_msg) return json.dumps({"status": config.SERVER_ERROR})
def handleChatRoomRemoved(chatroom, event): """ Inform the messaging service of chatroom deletion. """ if not chatroom.REQUEST.get('controller_state') or \ chatroom.REQUEST.controller_state.id == 'delete_confirmation': # The object is not yet removed, the user have been presented a # confirmation prompt. return getMultiAdapter((chatroom, chatroom.REQUEST), name='babblechat').initialize() pm = getToolByName(chatroom, 'portal_membership') member = pm.getAuthenticatedMember() password = _getChatPassword(member) if password is None: return s = get_connection(chatroom) try: result = json.loads(s.removeChatRoom( member.getId(), password, '/'.join(chatroom.getPhysicalPath()), )) except xmlrpclib.Fault, e: log.error('XMLRPC Error from babble.server: removeChatRoom: %s' % e) return
def handleChatRoomAdded(chatroom, event): """ Register the chatroom with the messaging service. """ getMultiAdapter((chatroom, chatroom.REQUEST), name='babblechat').initialize() pm = getToolByName(chatroom, 'portal_membership') member = pm.getAuthenticatedMember() password = _getChatPassword(member) if password is None: return s = get_connection(chatroom) try: result = json.loads(s.createChatRoom( member.getId(), password, '/'.join(chatroom.getPhysicalPath()), get_participants(chatroom), )) except xmlrpclib.Fault, e: log.error('XMLRPC Error from babble.server: createChatRoom: %s' % e) return
def conversation(self): site = getSite() pm = getToolByName(site, 'portal_membership') if pm.isAnonymousUser(): # XXX: What do we do when anonymous has sharing rights? return _("Please log in to view this conversation") # self doesn't have a full acquisition chain, so we use the brain from the # catalog catalog = getToolByName(site, 'portal_catalog') ps = catalog( portal_type=self.portal_type, id=self.id, creation_date=self.creation_date, modification_date=self.modification_date) if len(ps) != 1: return _("CatalogError: Could not retrieve the conversation. Please " "contact your site administrator.") brain = ps[0] member = pm.getAuthenticatedMember() username = member.getId() password = _getChatPassword(member) if password is None: return _("Error fetching the conversation. You do not have chat " "password. Please contact your site administrator.") s = get_connection(site) try: response = json.loads(s.getMessages( username, password, None, [brain.getPath()], None, None )) except xmlrpclib.Fault, e: log.error('Error from babble.server: getMessages: %s' % e) return self.cached_conversation
def _editChatRoom(chatroom): pm = getToolByName(chatroom, 'portal_membership') member = pm.getAuthenticatedMember() password = _getChatPassword(member) if password is None: return chatroom_path = '/'.join(chatroom.getPhysicalPath()) s = get_connection(chatroom) participants = get_participants(chatroom) try: resp = json.loads( s.editChatRoom( member.getId(), password, chatroom_path, participants, )) except xmlrpclib.Fault, e: err_msg = e.faultString.strip('\n').split('\n')[-1] log.error('Error from babble.server: editChatRoom: %s' % err_msg) return