xmpp/filter.go
author Chris Jones <chris@cjones.org>
Wed, 16 Oct 2013 20:05:02 -0600
changeset 167 7ce61108ed86
parent 136 f35f853a52b6
permissions -rw-r--r--
Expose the Status.Fatal function.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     1
package xmpp
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     2
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     3
// Manages the stack of filters that can read and modify stanzas on
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     4
// their way from the remote to the application.
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     5
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     6
// Receive new filters on filterAdd; those new filters get added to
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     7
// the top of the stack. Receive stanzas at the bottom of the stack on
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     8
// input. Send stanzas out the top of the stack on output.
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
     9
func filterMgr(filterAdd <-chan Filter, input <-chan Stanza, output chan<- Stanza) {
136
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents: 133
diff changeset
    10
	defer close(output)
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    11
	for {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    12
		select {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    13
		case stan, ok := <-input:
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    14
			if !ok {
136
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents: 133
diff changeset
    15
				return
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    16
			}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    17
			output <- stan
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    18
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    19
		case filt := <-filterAdd:
136
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents: 133
diff changeset
    20
			ch := make(chan Stanza)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents: 133
diff changeset
    21
			go filt(input, ch)
f35f853a52b6 Simplified and debugged the filter logic.
Chris Jones <christian.jones@sri.com>
parents: 133
diff changeset
    22
			input = ch
121
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    23
		}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    24
	}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    25
}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    26
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    27
// AddRecvFilter adds a new filter to the top of the stack through which
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    28
// incoming stanzas travel on their way up to the client.
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    29
func (cl *Client) AddRecvFilter(filt Filter) {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    30
	if filt == nil {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    31
		return
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    32
	}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    33
	cl.recvFilterAdd <- filt
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    34
}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    35
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    36
// AddSendFilter adds a new filter to the top of the stack through
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    37
// which outgoing stanzas travel on their way down from the client to
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    38
// the network.
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    39
func (cl *Client) AddSendFilter(filt Filter) {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    40
	if filt == nil {
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    41
		return
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    42
	}
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    43
	cl.sendFilterAdd <- filt
ebb86cbdd218 Changed the way filters work. They're now symmetrical, consisting of a paired send filter and receive filter.
Chris Jones <christian.jones@sri.com>
parents:
diff changeset
    44
}