Protocols won't be kept in lib. Not sure how to handle them - for now, a new root...
[rat.git] / lib / rat / protocol.rb
blob306689bc1daaa8dfe856ab02ce25f38e2b614968
1 module Rat
2   module Protocol
3     Protocols = []
4     def self.initialize
5       Protocols.each {|protocol| protocol::initialize }
6     end
7     
8     Initialized = []
9     
10     # To be inherited from for any +Protocol+. A +Rat+ +Protocol+ must expose at
11     # least four things - an +::initialize+ method, a +::new+ (+#initialize+, in
12     # other words) method, a +::terminate+ method, and a +#<<+ method.
13     class Base
14       def self.inherited klass
15         Protocols << klass
16       end
17       
18       # One of the required methods in a +Protocol+ plugin, +::initialize+ is
19       # run when +Rat+ starts (if there is a default connection set up involving
20       # your protocol), when the first window is created for a protocol, or when the
21       # protocol is otherwise initialized by +Rat+. It's perfect to
22       # make any connections, declare presence, and so on. It will only be run
23       # once, unless +::terminate+ is run and then the protocol is re-connected.
24       # 
25       # This should take a login argument, and optionally a password argument
26       # (if relevant), and should eventually call `super`.
27       def self.initialize # login, password = nil
28         Protocol::Initialized << self
29       end
30       
31       # Provided by +Base+, and probably doesn't need to be modified - this
32       # predicate will determine if your protocol has been globally initialized
33       # by +Rat+ yet.
34       def self.initialized?
35         Protocol.initialized.include? self
36       end
37       
38       # Required for every protocol, this method is run when +Rat+ closes down
39       # itself, or, for some reason, closes the protocol.
40       # 
41       # This should eventually call `super`.
42       def self.terminate
43         Protocol.initialized.delete self
44       end
45       
46       # The second required method, +::new+ (+#initialize+) is run when a window is
47       # dedicated to a protocol. Should accept a window object (where incoming
48       # messages or status information can be printed) and a target (that the user
49       # specified, where messages should be sent or whatever is appropriate).
50       # 
51       # This should take window and target arguments, and eventually call
52       # +super+ with those arguments.
53       def initialize window, target
54         @window = window
55         @target = target
56       end
57       
58       # Finally, a +#<<+ method that deals with outgoing messages, sending
59       # them on their merry way or processing them in some way.
60       #
61       # Should recieve a message argument, and can optionally call +super+ to
62       # simply print the target and message to the window.
63       def << message
64         @window << ['Rattagan', message].join(" > ")
65       end
66     end
67     
68     # This class applies to any window with no protocol.
69     class None < Rat::Protocol::Base
70       def self.initialize
71         super
72       end
73       
74       def self.terminate
75         super
76       end
77       
78       
79       def initialize window, target
80         super window, target
81       end
82       
83       def << message
84         super message
85       end
86     end
87   end
88 end