session.rb

来自「用ruby on rails写的一个博客程序,还不错..ruby on rail」· RB 代码 · 共 616 行 · 第 1/2 页

RB
616
字号
      @connection.send(Jabber::Protocol::Presence.gen_xa(id, status))    end        ##    # Sends a free for chat presence message    #    # status:: [String] The status message    #    def announce_free_for_chat(status=nil)      @connection.send(Jabber::Protocol::Presence.gen_chat(id, status))    end        ##    # Sends a 'normal' presence message    #    # status:: [String] The status message    #    def announce_normal(status=nil)      @connection.send(Jabber::Protocol::Presence.gen_normal(id, status))    end        ##    # Sends an away from computer presence message    #    # status:: [String] The status message    #    def announce_away_from_computer(status=nil)      @connection.send(Jabber::Protocol::Presence.gen_away(id, status))    end        ##    # Sends a do not disturb presence message    #    # status:: [String] The status message    #    def announce_do_not_disturb(status=nil)      @connection.send(Jabber::Protocol::Presence.gen_dnd(id, status))    end        ##    # Sets the handler for subscription requests, notifications, etc.    #    def set_subscription_handler(handler=nil, &block)      @subscriptionHandler = handler.new(self) if handler      @subscriptionHandler = block if block_given? and !handler    end        def enable_autosubscription      set_subscription_handler AutoSubscriptionHandler    end        def subscribe(to, name="")       to = JID.to_jid(to)      roster_item = @roster[to]            if roster_item #if you already have a roster item just send the subscribe request        if roster_item.subscription=="to" or roster_item.subscription=="both"          return        end        @connection.send(Jabber::Protocol::Presence.gen_new_subscription(to))        return      end      myid = self.id      @connection.send(Jabber::Protocol::Iq.gen_add_rosteritem(self, myid, to, name)) do |element|        if element.attr_id==myid          element.consume_element          if element.attr_type=="result"            @connection.send(Jabber::Protocol::Presence.gen_new_subscription(to))          end        end      end    end        ##    # Adds a filter to the Connection to manage tracking resources that come online/offline    #    def register_presence_filter      @connection.add_filter("presenceAvailableFilter") do |element|        if element.element_tag=="presence"          type = element.attr_type          type = nil if type.nil?          case type          when nil, "available"            element.consume_element            from = JID.new(element.attr_from)            rItem = @roster[from]            show = element.show.element_data            show = "chat" unless show            status = element.status.element_data            status = "" unless status            if rItem              resource = rItem[from.resource]              if resource                resource.update(show, status)              else                rItem.add(from.resource, show, status)              end            end          when "unavailable"            element.consume_element            from = JID.new(element.attr_from)            rItem = @roster[from]            resource = rItem.delete(from.resource) if rItem          when "subscribe", "unsubscribe", "subscribed", "unsubscribed"            element.consume_element            from = JID.new(element.attr_from)            break unless @subscriptionHandler            if @subscriptionHandler.kind_of? Proc              @subscriptionHandler.call(Subscription.new(self, type.intern, from, id))            else              @subscriptionHandler.send(Subscription.new(self, type.intern, from, id))            end          end        end #if presence       end #do    end        ##    # Creates a new message to the supplied JID of type NORMAL    #     # to:: [Jabber::JID] Who to send the message to    # type:: [String = Jabber::Protocol::Message::NORMAL] The type of message to send (see Jabber::Protocol::Message)    # return:: [Jabber::Protocol::Message] The new message    #    def new_message(to, type=Jabber::Protocol::Message::NORMAL)      msg = Jabber::Protocol::Message.new(to, type)      msg.session=self      return msg    end        ##    # Creates a new message addressed to the supplied JID of type CHAT    #    # to:: [JID] Who to send the message to    # return:: [Jabber::Protocol::Message] The new (chat) message    #    def new_chat_message(to)      self.new_message(to, Jabber::Protocol::Message::CHAT)    end        ##    # Creates a new message addressed to the supplied JID of type GROUPCHAT    #    # to:: [JID] Who to send the message to    # return:: [Jabber::Protocol::Message] The new (group chat) message    #    def new_group_chat_message(to)      self.new_message(to, Jabber::Protocol::Message::GROUPCHAT)    end        ##    # Adds a filter to the Connection to manage tracking messages to forward    # to registered message listeners.    #    def register_message_filter      @connection.add_filter("messageFilter") do |element|        if element.element_tag=="message" and @messageListeners.size > 0          element.consume_element          message = Jabber::Protocol::Message.from_element(self, element)          notify_message_listeners(message)        end #if message       end #do    end        ##    # Add a listener for new messages    #    # Usage::    #    id = session.add_message_listener do |message|    #      puts message    #    end    #     # &block [Block] The block to process a message    # return:: [String] The listener ID...used to remove the listener    #    def add_message_listener(&block)      id = Jabber.gen_random_id("", 10)      @messageListeners[id]=block if block      return id    end        ##    # Deletes a message listener    #    # id:: [String] A messanger ID returned from add_message_listener    #    def delete_message_listener(lid)      @messageListeners.delete(lid)    end        #Cleanup methods        ##    # Releases the connection and resets the session.  The Session instance    # is no longer usable after this method is called    #    def release      begin        @connection.on_connection_exception do          #Do nothing...we are shutting down        end        @connection.send(Jabber::Protocol::Presence.gen_unavailable(id))        @connection.send(Jabber::Protocol.gen_close_stream)      rescue        #ignore error      end      begin        @connection.close      rescue        #ignore error      end    end        ##    # Same as _release    #    def close      release    end        ##    # Requests the Roster for the (authenticated) account.  This method blocks    # until a reply to the roster request is received.    #    def request_roster      if @authenticated        msg_id = id        @connection.send(Jabber::Protocol::Iq.gen_roster(self, msg_id)) do |element|          if element.attr_id == msg_id            element.consume_element            element.query.item.count.times do |i|              item = element.query.item[i]              @roster.add(item.attr_jid, item.attr_subscription, item.attr_name, item.group.element_data)            end          end        end        Thread.stop        register_roster_filter      end    end        ##    # Registers the roster filter with the Connection to forward IQ requests    # to the IQ listeners(they register by namespace)    #    def register_iq_filter()       @connection.add_filter("iqFilter") do |element|        if element.element_tag=="iq" then          element.consume_element          query=element.query          h=@iqHandlers[query.attr_xmlns]          h.call(Jabber::Protocol::Iq.from_element(self,element)) if h        end      end    end            ##    # Registers the roster filter with the Connection to forward roster changes to    # the roster listeners.    #    def register_roster_filter      @connection.add_filter("rosterFilter") do |element|        if element.element_tag=="iq" and element.query.attr_xmlns=="jabber:iq:roster" and element.attr_type=="set"          element.consume_element          item = element.query.item          if item.attr_subscription=="remove" then            @roster.remove(item.attr_jid)          else            @roster.add(item.attr_jid, item.attr_subscription, item.attr_name, item.group.element_data)          end        end      end    end        ##    # Registers a listener for roster events    #    # &block:: [Block] The listener block to process roster changes    # return:: [String] A roster ID to use when removing this listener    #    def add_roster_listener(&block)      roster.add_listener(&block)    end        ##    # Deletes the roster listener    #    # id:: [String] A roster ID received from the add_roster_listener method    #    def delete_roster_listener(id)      roster.delete_listener(id)    end        ##    # Notifies message listeners of the received message    #    # message:: [Jabber::Protocol::Message] The received message    #    def notify_message_listeners(message)      @messageListeners.each_value {|listener| listener.call(message)}    end  end    end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?