ponder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +7 -0
- data/README.md +338 -0
- data/examples/echo.rb +25 -0
- data/examples/github_blog.rb +31 -0
- data/lib/ponder.rb +22 -0
- data/lib/ponder/async_irc.rb +117 -0
- data/lib/ponder/callback.rb +42 -0
- data/lib/ponder/connection.rb +37 -0
- data/lib/ponder/delegate.rb +11 -0
- data/lib/ponder/filter.rb +7 -0
- data/lib/ponder/formatting.rb +30 -0
- data/lib/ponder/irc.rb +110 -0
- data/lib/ponder/logger/blind_io.rb +11 -0
- data/lib/ponder/logger/twoflogger.rb +93 -0
- data/lib/ponder/logger/twoflogger18.rb +93 -0
- data/lib/ponder/thaum.rb +242 -0
- data/lib/ponder/version.rb +3 -0
- data/lib/ruby/1.8/string.rb +5 -0
- data/ponder.gemspec +39 -0
- data/test/all_tests.rb +7 -0
- data/test/test_async_irc.rb +108 -0
- data/test/test_callback.rb +55 -0
- data/test/test_helper.rb +3 -0
- data/test/test_irc.rb +130 -0
- metadata +107 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2010 Tobias Bühlmann
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,338 @@
|
|
1
|
+
# Ponder
|
2
|
+
|
3
|
+
## Description
|
4
|
+
Ponder (Stibbons) is a Domain Specific Language for writing IRC Bots using the [EventMachine](http://github.com/eventmachine/eventmachine "EventMachine") library.
|
5
|
+
|
6
|
+
## Getting started
|
7
|
+
### Installation
|
8
|
+
$ sudo gem install ponder
|
9
|
+
|
10
|
+
### Configuring the Bot (Thaum!)
|
11
|
+
require 'rubygems'
|
12
|
+
require 'ponder'
|
13
|
+
|
14
|
+
@ponder = Ponder::Thaum.new
|
15
|
+
|
16
|
+
@ponder.configure do |c|
|
17
|
+
c.nick = 'Ponder'
|
18
|
+
c.server = 'irc.freenode.net'
|
19
|
+
c.port = 6667
|
20
|
+
end
|
21
|
+
|
22
|
+
### Starting the Thaum
|
23
|
+
@ponder.connect
|
24
|
+
|
25
|
+
### Event Handling
|
26
|
+
This naked Thaum will connect to the server and answer PING requests (and VERSION and TIME). If you want the Thaum to join a channel when it's connected, use the following:
|
27
|
+
|
28
|
+
@ponder.on :connect do
|
29
|
+
@ponder.join '#mended_drum'
|
30
|
+
end
|
31
|
+
|
32
|
+
If you want the Thaum to answer on specific channel messages, register this Event Handler:
|
33
|
+
|
34
|
+
@ponder.on :channel, /ponder/ do |event_data|
|
35
|
+
@ponder.message event_data[:channel], 'Heard my name!'
|
36
|
+
end
|
37
|
+
|
38
|
+
Now, if an incoming channel message contains the word "ponder", the Thaum will send the message "Heard my name!" to that specific channel. See the **Advanced Event Handling** chapter for more details on how to register Event Handlers and the `event_data` hash. For more examples, have a look at the examples directory.
|
39
|
+
|
40
|
+
## Advanced Configuration
|
41
|
+
Besides the configuration for nick, server and port as shown in the **Getting Started** chapter, there are some more preferences Ponder accepts. All of them:
|
42
|
+
|
43
|
+
* `server`
|
44
|
+
|
45
|
+
The `server` variable describes the server the Thaum shall connect to. It defaults to `'localhost'`.
|
46
|
+
|
47
|
+
* `port`
|
48
|
+
|
49
|
+
`port` describes the port that is used for the connection. It defaults to `6667`.
|
50
|
+
|
51
|
+
* `nick`
|
52
|
+
|
53
|
+
`nick` describes the nick the Thaum will try to register when connecting to the server. It will not be updated if the Thaum changes its nick. It defaults to `'Ponder'`.
|
54
|
+
|
55
|
+
* `username`
|
56
|
+
|
57
|
+
`username` is used for describing the username. It defaults to `'Ponder'`.
|
58
|
+
|
59
|
+
* `real_name`
|
60
|
+
|
61
|
+
`real_name` is used for describing the real name. It defaults to `'Ponder'`.
|
62
|
+
|
63
|
+
* `verbose`
|
64
|
+
|
65
|
+
If `verbose` is set to `true`, all incoming and outgoing traffic will be put to the console. Plus exceptions raised in Callbacks (errors). It defaults to `true`.
|
66
|
+
|
67
|
+
* `logging`
|
68
|
+
|
69
|
+
If `logging` is set to `true`, all incoming and outgoing traffic will be logged to logs/traffic.log and errors will be logged to logs/error.log. If you just want to log errors, you can manipulate the traffic logger in the configure block with `c.traffic_logger = c.empty_logger`. A logger set to `empty_logger` will not log anything.
|
70
|
+
|
71
|
+
You can also define other loggers for traffic\_logger and error\_logger with `c.traffic_logger = @my_cool_logger` or `c.traffic_logger = Logger.new(...)`. Per default, there are just #info and #error called on the logger.
|
72
|
+
|
73
|
+
You can access the logger instances via `@ponder.traffic_logger` or `@ponder.error_logger`, so you could do: `@ponder.traffic_logger.info('I did this and that right now')`.
|
74
|
+
|
75
|
+
It defaults to `false`. (xxx other methods)
|
76
|
+
|
77
|
+
* `reconnect`
|
78
|
+
|
79
|
+
If `reconnect` is set to `true`, the Thaum will try to reconnect after being disconnected from the server (netsplit, ...). It will not try to reconnect if you call `quit` on the Thaum. It defaults to `true`.
|
80
|
+
|
81
|
+
* `reconnect_interval`
|
82
|
+
|
83
|
+
If `reconnect` is set to `true`, `reconnect_interval` describes the time in seconds, which the Thaum will wait before trying to reconnect. It defaults to `30`.
|
84
|
+
|
85
|
+
* `auto_rename`
|
86
|
+
|
87
|
+
If `auto_rename` is set to `true`, the Thaum will try to rename if the chosen `nick` is already in use. It will try to take a random nick from "nick000" to "nick999".
|
88
|
+
|
89
|
+
For further information, have a look at the examples.
|
90
|
+
|
91
|
+
## Advanced Event Handling
|
92
|
+
A Thaum can react on several events, so here is a list of handlers that can be used as argument in the `on` method:
|
93
|
+
|
94
|
+
* `join`
|
95
|
+
|
96
|
+
The `join` handler reacts, if an user joins a channel in which the Thaum is in. Example:
|
97
|
+
|
98
|
+
@ponder.on :join do
|
99
|
+
# ...
|
100
|
+
end
|
101
|
+
|
102
|
+
If using a block variable, you have access to a hash with detailed information about the event. Example:
|
103
|
+
|
104
|
+
@ponder.on :join do |event_data|
|
105
|
+
@ponder.message event_data[:channel], "Hello #{event_data[:nick]}! Welcome to #{event_data[:channel]}."
|
106
|
+
end
|
107
|
+
|
108
|
+
Which will greet a joined user with a channel message.
|
109
|
+
|
110
|
+
The hash contains data for the keys `:nick`, `:user`, `:host` and `:channel`.
|
111
|
+
|
112
|
+
* `part`
|
113
|
+
|
114
|
+
Similar to the `join` handler but reacts on parting users. The block variable hash contains data for the keys `:nick`, `:user`, `:host`, `:channel` and `:message`. The value for `:message` is the message the parting user leaves.
|
115
|
+
|
116
|
+
* `quit`
|
117
|
+
|
118
|
+
The `quit` handler reacts, if an user quits from the server (and the Thaum can see it in a channel). The block variable hash contains data for the keys `:nick`, `:user`, `:host` and `:message`.
|
119
|
+
|
120
|
+
* `channel`
|
121
|
+
|
122
|
+
If an user sends a message to a channel, you can react with the `channel` handler. Example (from above):
|
123
|
+
|
124
|
+
@ponder.on :channel, /ponder/ do |event_data|
|
125
|
+
@ponder.message event_data[:channel], 'Heard my name!'
|
126
|
+
end
|
127
|
+
|
128
|
+
The block variable hash contains data for the keys `:nick`, `:user`, `:host`, `:channel` and `:message`.
|
129
|
+
|
130
|
+
* `query`
|
131
|
+
|
132
|
+
The `query` handler is like the `channel` handler, but for queries. Same keys in the data hash but no `:channel`.
|
133
|
+
|
134
|
+
* `nickchange`
|
135
|
+
|
136
|
+
`nickchange` reacts on nickchanges. Data hash keys are `:nick`, `:user`, `:host` and `:new_nick`, where `nick` is the nick before renaming and `new_nick` the nick after renaming.
|
137
|
+
|
138
|
+
* `kick`
|
139
|
+
|
140
|
+
If an user is being kicked, the `kick` handler can handle that event. Data hash keys are: `:nick`, `:user`, `:host`, `:channel`, `:victim` and `:reason`.
|
141
|
+
|
142
|
+
* `topic`
|
143
|
+
|
144
|
+
`topic` is for reacting on topic changes. Data hash keys are: `:nick`, `:user`, `:host`, `:channel` and `:topic`, where `:topic` is the new topic. You can provide a Regexp to just react on specific patterns:
|
145
|
+
|
146
|
+
@ponder.on :topic, /foo/ do |event_data|
|
147
|
+
# ...
|
148
|
+
end
|
149
|
+
|
150
|
+
This will just work for topics that include the word "foo".
|
151
|
+
|
152
|
+
* `disconnect`
|
153
|
+
|
154
|
+
`disconnect` reacts on being disconnected from the server (netsplit, quit, ...). It does not react if you exit the program with ^C.
|
155
|
+
|
156
|
+
* Raw numerics
|
157
|
+
|
158
|
+
A Thaum can seperately react on events with raw numerics, too. So you could do:
|
159
|
+
|
160
|
+
@ponder.on 301 do |event_data|
|
161
|
+
# ...
|
162
|
+
end
|
163
|
+
|
164
|
+
The data hash will contain the `:params` key. The corresponding value is the complete traffic line that came in.
|
165
|
+
|
166
|
+
For all Event Handlers there is a `:type` key in the data hash (if the variable is specified). Its value gives the type of event, like `:channel`, `:join` or `301`.
|
167
|
+
|
168
|
+
You can even share handler bodies between different events. So you are able to do something like this:
|
169
|
+
|
170
|
+
@ponder.on [:join, :part, :quit] do |event_data|
|
171
|
+
# ...
|
172
|
+
end
|
173
|
+
|
174
|
+
or
|
175
|
+
|
176
|
+
@ponder.on [:channel, :query], /ponder/ do |event_data|
|
177
|
+
@ponder.message((event_data[:channel] || event_data[:nick]), 'Yes?')
|
178
|
+
end
|
179
|
+
|
180
|
+
or
|
181
|
+
|
182
|
+
@ponder.on [:channel, :nickchange], /foo/ do |event_data|
|
183
|
+
# ...
|
184
|
+
end
|
185
|
+
|
186
|
+
They are not really shared at all, Ponder will just copy the Callback, but it's comfortable.
|
187
|
+
|
188
|
+
## Commanding the Thaum
|
189
|
+
Command the Thaum, very simple. Just call a method listed below on the Ponder object. I will keep this short, since I assume you're at least little experienced with IRC.
|
190
|
+
|
191
|
+
* `message(recipient, message)`
|
192
|
+
* `notice(recipient, message)`
|
193
|
+
* `mode(recipient, option)`
|
194
|
+
* `kick(channel, user, reason = nil)`
|
195
|
+
* `action(recipient, message)`
|
196
|
+
* `topic(channel, topic)`
|
197
|
+
* `join(channel, password = nil)`
|
198
|
+
* `part(channel, message = nil)`
|
199
|
+
* `quit(message = nil)`
|
200
|
+
* `rename(nick)`
|
201
|
+
* `away(message = nil)`
|
202
|
+
* `back`
|
203
|
+
* `invite(nick, channel)`
|
204
|
+
* `ban(channel, address)`
|
205
|
+
|
206
|
+
Last but not least some cool "give me something back" methods:
|
207
|
+
|
208
|
+
* `get_topic(channel)`
|
209
|
+
|
210
|
+
* Possible return values for `get_topic` are:
|
211
|
+
|
212
|
+
* `{:raw_numeric => 331, :message => 'No topic is set'}` if no topic is set
|
213
|
+
* `{:raw_numeric => 332, :message => message}` with `message` the topic message
|
214
|
+
* `{:raw_numeric => 403, :message => 'No such channel'}` if there is no such channel
|
215
|
+
* `{:raw_numeric => 442, :message => "You're not on that channel"}` if you cannot actually see the topic
|
216
|
+
* `false` if the request times out (30 seconds)
|
217
|
+
|
218
|
+
* `channel_info(channel)`
|
219
|
+
|
220
|
+
* Possible return values:
|
221
|
+
|
222
|
+
* If successful, a hash with keys:
|
223
|
+
|
224
|
+
* `:modes` (letters)
|
225
|
+
* `:channel_limit` (if channel limit is set)
|
226
|
+
* `:created_at` (Time object of the time the channel was created)
|
227
|
+
|
228
|
+
* `false`, if the request is not successful or times out (30 seconds)
|
229
|
+
|
230
|
+
* `whois(nick)`
|
231
|
+
|
232
|
+
* Possible return values:
|
233
|
+
|
234
|
+
* If successful, a hash with keys:
|
235
|
+
|
236
|
+
* `:nick`
|
237
|
+
* `:username`
|
238
|
+
* `:host`
|
239
|
+
* `:real_name`
|
240
|
+
* `:server` (a hash with the keys `:address` and `:name`)
|
241
|
+
* `:channels` (a hash like `{'#foo' => '@', '#bar' => nil}` where the values are user privileges)
|
242
|
+
* `:registered` (`true`, if registered, else `nil`)
|
243
|
+
|
244
|
+
* If not successful
|
245
|
+
|
246
|
+
* `false`
|
247
|
+
|
248
|
+
* If times out (30 seconds)
|
249
|
+
|
250
|
+
* `nil`
|
251
|
+
|
252
|
+
Example:
|
253
|
+
|
254
|
+
# Ponder, kick an user (and check if I'm allowed to command you)!
|
255
|
+
@ponder.on :channel, /^!kick \S+$/ do |event_data|
|
256
|
+
user_data = @ponder.whois(event_data[:nick])
|
257
|
+
if user_data[:registered] && (user_data[:channels][event_data[:channel]] == '@')
|
258
|
+
user_to_kick = event_data[:message].split(' ')[1]
|
259
|
+
@ponder.kick event_data[:channel], user_to_kick, 'GO!'
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
## Filters
|
264
|
+
### Before Filters
|
265
|
+
You can have Before Filters! They are called before each event handling process and can - among other things - manipulate the `event_data` hash. If a Before Filter returns `false`, no further filters (no After Filters either) are called and the event handling process won't fire up. Example:
|
266
|
+
|
267
|
+
@ponder.before_filter(:channel, /foo/) do
|
268
|
+
# ...
|
269
|
+
end
|
270
|
+
|
271
|
+
This Before Filter will be called, if a channel message with the word "foo" gets in. You can use all other event types (like :query, :kick, ...) as well. Also possible is an array notation like `before_filter([:query, :channel], /foo/) ...`. If you want the filter to work an all event types, you can simply use `:all`. Filters will be called in defining order; first defined, first called. Event specific filters are called before `:all` filters.
|
272
|
+
|
273
|
+
### After Filters
|
274
|
+
After Filters work the same way as Before Filters do, just after the actual event handling process. An After Filter does not hinder later After Filters to fire up if it returns `false`. Example:
|
275
|
+
|
276
|
+
@ponder.after_filter(:all, //) do
|
277
|
+
# ...
|
278
|
+
end
|
279
|
+
|
280
|
+
## Timers
|
281
|
+
If you need something in an event handling process to be time-displaced, you should not use `sleep`. I recommend using the comfortable timer methods EventMachine provides. A one shot timer looks like this:
|
282
|
+
|
283
|
+
EventMachine::Timer.new(10) do
|
284
|
+
# code to be run after 10 seconds
|
285
|
+
end
|
286
|
+
|
287
|
+
If you want the timer to be canceled before starting, you can do it like this:
|
288
|
+
|
289
|
+
timer = EventMachine::Timer.new(10) do
|
290
|
+
# code to be run after 10 seconds
|
291
|
+
end
|
292
|
+
|
293
|
+
# ...
|
294
|
+
|
295
|
+
timer.cancel
|
296
|
+
|
297
|
+
You can even have periodic timers which will fire up every n seconds:
|
298
|
+
|
299
|
+
EventMachine::PeriodicTimer.new(10) do
|
300
|
+
# code to be run every 10 seconds
|
301
|
+
end
|
302
|
+
|
303
|
+
A periodic timer can be canceled just like the other one.
|
304
|
+
|
305
|
+
## Formatting
|
306
|
+
You can format your messages with colors, make it bold, italic or underlined. All of those formatting constants are availabe through `Ponder::Formatting`.
|
307
|
+
|
308
|
+
### Colors
|
309
|
+
For coloring text you first set the color code with `Ponder::Formatting::COLOR_CODE` followed by a color followed by the text. For ending the colored text, set the uncolor code with `Ponder::Formatting::UNCOLOR`.
|
310
|
+
|
311
|
+
Availabe colors are white, black, blue, green, red, brown, purple, orange, yellow, lime, teal, cyan, royal, pink, gray and silver. You can set one with the `Ponder::Formatting::COLORS` hash. Example:
|
312
|
+
|
313
|
+
"This will be #{Ponder::Formatting::COLOR_CODE}#{Ponder::Formatting::COLORS[:red]}red#{Ponder::Formatting::UNCOLOR_CODE}. This not."
|
314
|
+
|
315
|
+
### Font Styles
|
316
|
+
If you want to make a text bold, italic or underlined, use `Ponder::Formatting::BOLD`, `Ponder::Formatting::ITALIC` or `Ponder::Formatting::UNDERLINE`. After the text, close it with the same constant. Example:
|
317
|
+
|
318
|
+
"This will be #{Ponder::Formatting::UNDERLINE}underlined#{Ponder::Formatting::UNDERLINE}. This not."
|
319
|
+
|
320
|
+
### Shortened Formatting
|
321
|
+
If you don't always want to use `Ponder::Formatting`, use `include Ponder::Formatting`. All constants will then be availabe without `Ponder::Formatting` in front.
|
322
|
+
|
323
|
+
## Source
|
324
|
+
The source can be found at GitHub: [tbuehlmann/ponder](http://github.com/tbuehlmann/ponder "Ponder").
|
325
|
+
|
326
|
+
You can contact me through GitHub and IRC (#ponder in the Freenode network).
|
327
|
+
|
328
|
+
## Discworld Context
|
329
|
+
So, why all that silly names? Ponder Stibbons? Thaum? Twoflogger (referring to Twoflower), BlindIO? What's the Mended Drum? Who's the Librarian? Simply put, I freaking enshrine Terry Pratchett's Discworld Novels and there were no better name for this project than Ponder. Ponder Stibbons is the Head of Inadvisably Applied Magic at the Unseen University of Ankh Morpork. He researched the Thaum, like the atom, just for magic. And I just love that character, so there we are. If you're a fan too or want to talk about the Discworld, the framework, whatever, don't hesitate to contact me.
|
330
|
+
|
331
|
+
## License
|
332
|
+
Copyright (c) 2010 Tobias Bühlmann
|
333
|
+
|
334
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
335
|
+
|
336
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
337
|
+
|
338
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/examples/echo.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
$LOAD_PATH.unshift Pathname.new(__FILE__).dirname.expand_path.join('..', 'lib')
|
3
|
+
|
4
|
+
require 'ponder'
|
5
|
+
|
6
|
+
# This Thaum will parrot all channel messages.
|
7
|
+
@ponder = Ponder::Thaum.new
|
8
|
+
|
9
|
+
@ponder.configure do |c|
|
10
|
+
c.server = 'chat.freenode.org'
|
11
|
+
c.port = 6667
|
12
|
+
c.nick = 'Ponder'
|
13
|
+
c.verbose = true
|
14
|
+
c.logging = false
|
15
|
+
end
|
16
|
+
|
17
|
+
@ponder.on :connect do
|
18
|
+
@ponder.join '#ponder'
|
19
|
+
end
|
20
|
+
|
21
|
+
@ponder.on :channel, // do |event_data|
|
22
|
+
@ponder.message event_data[:channel], event_data[:message]
|
23
|
+
end
|
24
|
+
|
25
|
+
@ponder.connect
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
$LOAD_PATH.unshift Pathname.new(__FILE__).dirname.expand_path.join('..', 'lib')
|
3
|
+
|
4
|
+
require 'ponder'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'nokogiri'
|
7
|
+
require 'open-uri'
|
8
|
+
|
9
|
+
# This Thaum answers the channel message "blog?" with the title of the newest github blog entry.
|
10
|
+
@ponder = Ponder::Thaum.new
|
11
|
+
|
12
|
+
@ponder.configure do |c|
|
13
|
+
c.server = 'chat.freenode.org'
|
14
|
+
c.port = 6667
|
15
|
+
c.nick = 'Ponder'
|
16
|
+
c.verbose = true
|
17
|
+
c.logging = false
|
18
|
+
end
|
19
|
+
|
20
|
+
@ponder.on :connect do
|
21
|
+
@ponder.join '#ponder'
|
22
|
+
end
|
23
|
+
|
24
|
+
@ponder.on :channel, /^blog\?$/ do |event_data|
|
25
|
+
doc = Nokogiri::HTML(open('http://github.com/blog'))
|
26
|
+
title = doc.xpath('//html/body/div/div[2]/div/div/ul/li/h2/a')[0].text
|
27
|
+
|
28
|
+
@ponder.message event_data[:channel], "Newest Github Blog Post: #{title}"
|
29
|
+
end
|
30
|
+
|
31
|
+
@ponder.connect
|
data/lib/ponder.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift Pathname.new(__FILE__).dirname.expand_path
|
5
|
+
|
6
|
+
module Ponder
|
7
|
+
def self.root
|
8
|
+
Pathname.new($0).dirname.expand_path
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'ponder/version'
|
12
|
+
require 'ponder/thaum'
|
13
|
+
require 'ponder/formatting'
|
14
|
+
require 'ponder/logger/blind_io'
|
15
|
+
|
16
|
+
if RUBY_VERSION < '1.9'
|
17
|
+
require 'ruby/1.8/string'
|
18
|
+
require 'ponder/logger/twoflogger18'
|
19
|
+
else
|
20
|
+
require 'ponder/logger/twoflogger'
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
module Ponder
|
5
|
+
module AsyncIRC
|
6
|
+
def get_topic(channel)
|
7
|
+
queue = Queue.new
|
8
|
+
@observer_queues[queue] = [/:\S+ (331|332|403|442) \S+ #{Regexp.escape(channel)} :/i]
|
9
|
+
raw "TOPIC #{channel}"
|
10
|
+
|
11
|
+
topic = begin
|
12
|
+
Timeout::timeout(30) do
|
13
|
+
response = queue.pop
|
14
|
+
raw_numeric = response.scan(/^:\S+ (\d{3})/)[0][0]
|
15
|
+
|
16
|
+
case raw_numeric
|
17
|
+
when '331'
|
18
|
+
{:raw_numeric => 331, :message => 'No topic is set'}
|
19
|
+
when '332'
|
20
|
+
{:raw_numeric => 332, :message => response.scan(/ :(.*)/)[0][0]}
|
21
|
+
when '403'
|
22
|
+
{:raw_numeric => 403, :message => 'No such channel'}
|
23
|
+
when '442'
|
24
|
+
{:raw_numeric => 442, :message => "You're not on that channel"}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
rescue Timeout::Error
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
@observer_queues.delete queue
|
32
|
+
return topic
|
33
|
+
end
|
34
|
+
|
35
|
+
def channel_info(channel)
|
36
|
+
queue = Queue.new
|
37
|
+
@observer_queues[queue] = [/:\S+ (324|329|403) \S+ #{Regexp.escape(channel)}/i]
|
38
|
+
raw "MODE #{channel}"
|
39
|
+
information = {}
|
40
|
+
running = true
|
41
|
+
|
42
|
+
begin
|
43
|
+
Timeout::timeout(30) do
|
44
|
+
while running
|
45
|
+
response = queue.pop
|
46
|
+
raw_numeric = response.scan(/^:\S+ (\d{3})/)[0][0]
|
47
|
+
|
48
|
+
case raw_numeric
|
49
|
+
when '324'
|
50
|
+
information[:modes] = response.scan(/^:\S+ 324 \S+ \S+ \+(\w*)/)[0][0].split('')
|
51
|
+
limit = response.scan(/^:\S+ 324 \S+ \S+ \+\w* (\w*)/)[0]
|
52
|
+
information[:channel_limit] = limit[0].to_i if limit
|
53
|
+
when '329'
|
54
|
+
information[:created_at] = Time.at(response.scan(/^:\S+ 329 \S+ \S+ (\d+)/)[0][0].to_i)
|
55
|
+
running = false
|
56
|
+
when '403'
|
57
|
+
information = false
|
58
|
+
running = false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
rescue Timeout::Error
|
63
|
+
information = false
|
64
|
+
end
|
65
|
+
|
66
|
+
@observer_queues.delete queue
|
67
|
+
return information
|
68
|
+
end
|
69
|
+
|
70
|
+
def whois(nick)
|
71
|
+
queue = Queue.new
|
72
|
+
@observer_queues[queue] = [/^:\S+ (307|311|312|318|319|401) \S+ #{Regexp.escape(nick)}/i]
|
73
|
+
raw "WHOIS #{nick}"
|
74
|
+
whois = {}
|
75
|
+
running = true
|
76
|
+
|
77
|
+
while running
|
78
|
+
begin
|
79
|
+
Timeout::timeout(30) do
|
80
|
+
response = queue.pop
|
81
|
+
raw_numeric = response.scan(/^:\S+ (\d{3})/)[0][0]
|
82
|
+
|
83
|
+
case raw_numeric
|
84
|
+
when '307'
|
85
|
+
whois[:registered] = true
|
86
|
+
when '311'
|
87
|
+
response = response.scan(/^:\S+ 311 \S+ (\S+) (\S+) (\S+) \* :(.*)$/)[0]
|
88
|
+
whois[:nick] = response[0]
|
89
|
+
whois[:username] = response[1]
|
90
|
+
whois[:host] = response[2]
|
91
|
+
whois[:real_name] = response[3]
|
92
|
+
when '312'
|
93
|
+
response = response.scan(/^:\S+ 312 \S+ \S+ (\S+) :(.*)/)[0]
|
94
|
+
whois[:server] = {:address => response[0], :name => response[1]}
|
95
|
+
when '318'
|
96
|
+
running = false
|
97
|
+
when '319'
|
98
|
+
channels_with_mode = response.scan(/^:\S+ 319 \S+ \S+ :(.*)/)[0][0].split(' ')
|
99
|
+
whois[:channels] = {}
|
100
|
+
channels_with_mode.each do |c|
|
101
|
+
whois[:channels][c.scan(/(.)?(#\S+)/)[0][1]] = c.scan(/(.)?(#\S+)/)[0][0]
|
102
|
+
end
|
103
|
+
when '401'
|
104
|
+
whois = false
|
105
|
+
running = false
|
106
|
+
end
|
107
|
+
end
|
108
|
+
rescue Timeout::Error
|
109
|
+
nil
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
@observer_queues.delete queue
|
114
|
+
return whois
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|