newton 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/newton/bot.rb +58 -8
- data/lib/newton/callback.rb +3 -1
- data/lib/newton/channel.rb +11 -0
- data/lib/newton/irc.rb +4 -0
- data/lib/newton/user.rb +5 -0
- metadata +3 -3
data/README.md
CHANGED
@@ -91,7 +91,7 @@ least in certain implementations of Ruby.
|
|
91
91
|
|
92
92
|
Nobody is able to remember all IRC numeric replies (e.g. 401 for "no
|
93
93
|
such nick"). Unfortunately, some frameworks, like for example Isaac,
|
94
|
-
require you to
|
94
|
+
require you to use those codes when hooking on errors (`on :error,
|
95
95
|
401 do`). Those codes, however, also all have a name, e.g.
|
96
96
|
ERR_NOSUCHNICK instead of 401 – and Newton provides all of those names
|
97
97
|
as constants.
|
data/lib/newton/bot.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require 'socket'
|
3
3
|
require "thread"
|
4
|
+
require "ostruct"
|
4
5
|
require "newton/rubyext/module"
|
5
6
|
require "newton/rubyext/queue"
|
6
7
|
require "newton/rubyext/string"
|
@@ -23,7 +24,7 @@ require "newton/isupport"
|
|
23
24
|
|
24
25
|
Thread.abort_on_exception = true
|
25
26
|
module Newton
|
26
|
-
VERSION = '0.0
|
27
|
+
VERSION = '0.1.0'
|
27
28
|
|
28
29
|
# @member [String] server The address of the server to connnect to
|
29
30
|
# @member [Number] port The port of the server to connect to
|
@@ -34,8 +35,6 @@ module Newton
|
|
34
35
|
# @member [Float] messages_per_second How many messages the server processes per second
|
35
36
|
# @member [Number] server_queue_size The size of the message queue of the server
|
36
37
|
# @member [Symbol] strictness The strictness of Newton. Allowed values: :strict and :forgiving
|
37
|
-
class Config < Struct.new(:server, :port, :ssl, :password, :nick, :realname, :verbose, :messages_per_second, :server_queue_size, :strictness)
|
38
|
-
end
|
39
38
|
|
40
39
|
class Bot
|
41
40
|
# @return [Config]
|
@@ -102,7 +101,20 @@ module Newton
|
|
102
101
|
# @yield
|
103
102
|
def initialize(&b)
|
104
103
|
@events = {}
|
105
|
-
@config =
|
104
|
+
@config = OpenStruct.new({
|
105
|
+
:server => "localhost",
|
106
|
+
:port => 6667,
|
107
|
+
:ssl => false,
|
108
|
+
:password => nil,
|
109
|
+
:nick => "newton",
|
110
|
+
:realname => "Newton",
|
111
|
+
:verbose => false,
|
112
|
+
:messages_per_second => 0.5,
|
113
|
+
:server_queue_size => 10,
|
114
|
+
:strictness => :forgiving,
|
115
|
+
:message_split_start => '... ',
|
116
|
+
:message_split_end => ' ...',
|
117
|
+
})
|
106
118
|
|
107
119
|
@store = {}
|
108
120
|
@semaphores = {}
|
@@ -224,11 +236,30 @@ module Newton
|
|
224
236
|
# @see User#send
|
225
237
|
def msg(recipient, text)
|
226
238
|
text = text.to_s
|
239
|
+
split_start = @config.message_split_start || ""
|
240
|
+
split_end = @config.message_split_end || ""
|
241
|
+
|
227
242
|
text.split("\n").each do |line|
|
228
|
-
#
|
229
|
-
|
230
|
-
|
231
|
-
|
243
|
+
# 498 = 510 - length(":" . " PRIVMSG " . " :");
|
244
|
+
maxlength = 498 - self.mask.to_s.length - recipient.to_s.length
|
245
|
+
maxlength_without_end = maxlength - split_end.length
|
246
|
+
|
247
|
+
if text.length > maxlength
|
248
|
+
splitted = []
|
249
|
+
|
250
|
+
while text.length > maxlength_without_end
|
251
|
+
pos = text.rindex(/\s/, maxlength)
|
252
|
+
r = pos || maxlength_without_end
|
253
|
+
splitted << text.slice!(0, r) + split_end
|
254
|
+
text = split_start + text
|
255
|
+
end
|
256
|
+
|
257
|
+
splitted << text
|
258
|
+
splitted.each do |string|
|
259
|
+
raw("PRIVMSG #{recipient} :#{string}")
|
260
|
+
end
|
261
|
+
else
|
262
|
+
raw("PRIVMSG #{recipient} :#{text}")
|
232
263
|
end
|
233
264
|
end
|
234
265
|
end
|
@@ -271,6 +302,25 @@ module Newton
|
|
271
302
|
@config.nick
|
272
303
|
end
|
273
304
|
|
305
|
+
# @return [String]
|
306
|
+
attr_reader :host
|
307
|
+
attr_reader :mask
|
308
|
+
attr_reader :user
|
309
|
+
attr_reader :realname
|
310
|
+
attr_reader :signed_on_at
|
311
|
+
def secure?
|
312
|
+
end
|
313
|
+
|
314
|
+
def unknown?
|
315
|
+
false
|
316
|
+
end
|
317
|
+
|
318
|
+
[:host, :mask, :user, :realname, :signed_on_at, :secure?].each do |attr|
|
319
|
+
define_method(attr) do
|
320
|
+
User(nick).__send__(attr)
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
274
324
|
# Sets the bot's nick.
|
275
325
|
#
|
276
326
|
# @param [String] new_nick
|
data/lib/newton/callback.rb
CHANGED
data/lib/newton/channel.rb
CHANGED
@@ -348,6 +348,17 @@ module Newton
|
|
348
348
|
@bot.raw "JOIN #{[@name, key].compact.join(" ")}"
|
349
349
|
end
|
350
350
|
|
351
|
+
# @return [Boolean]
|
352
|
+
def ==(other)
|
353
|
+
@name == other.to_s
|
354
|
+
end
|
355
|
+
alias_method :eql?, "=="
|
356
|
+
|
357
|
+
# @return [Fixnum]
|
358
|
+
def hash
|
359
|
+
@name.hash
|
360
|
+
end
|
361
|
+
|
351
362
|
# @return [String]
|
352
363
|
def to_s
|
353
364
|
@name
|
data/lib/newton/irc.rb
CHANGED
@@ -195,6 +195,7 @@ module Newton
|
|
195
195
|
user.sync(:unknown?, true, true)
|
196
196
|
elsif msg.command == RPL_ENDOFWHOIS.to_s
|
197
197
|
user = User.find_ensured(msg.params[1], @bot)
|
198
|
+
user.in_whois = false
|
198
199
|
if @whois_updates[user].empty? && !user.attr(:unknown?, true, true)
|
199
200
|
# for some reason, we did not receive user information. one
|
200
201
|
# reason is freenode throttling WHOIS
|
@@ -239,6 +240,9 @@ module Newton
|
|
239
240
|
end
|
240
241
|
|
241
242
|
msg.channel.mark_as_synced(:bans)
|
243
|
+
elsif msg.command == "396"
|
244
|
+
# note: designed for freenode
|
245
|
+
User(msg.params[0]).sync(:host, params[1], true)
|
242
246
|
end
|
243
247
|
|
244
248
|
# TODO work with strings/constants, too
|
data/lib/newton/user.rb
CHANGED
@@ -61,6 +61,8 @@ module Newton
|
|
61
61
|
attr_accessor :bot
|
62
62
|
# @return [Boolean]
|
63
63
|
attr_accessor :synced
|
64
|
+
# @return [Boolean]
|
65
|
+
attr_accessor :in_whois
|
64
66
|
|
65
67
|
# By default, you can use methods like User#user, User#host and
|
66
68
|
# alike – If you however fear that another thread might change
|
@@ -126,10 +128,13 @@ module Newton
|
|
126
128
|
#
|
127
129
|
# @return [void]
|
128
130
|
def whois
|
131
|
+
return if @in_whois
|
129
132
|
@synced = false
|
130
133
|
@data.keys.each do |attr|
|
131
134
|
unsync attr
|
132
135
|
end
|
136
|
+
|
137
|
+
@in_whois = true
|
133
138
|
@bot.raw "WHOIS #@nick #@nick"
|
134
139
|
end
|
135
140
|
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.2
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Dominik Honnef
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-07-19 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|