beerbot 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 878a3f275715d57e32aeeb9778313111ec256f08
4
- data.tar.gz: 9c173bbd514a2395150c80ddcdf65ebfdcd0a03b
3
+ metadata.gz: 36f864e883bbc7374b90bc57465a35e14ebc9f7d
4
+ data.tar.gz: df19b2f182e991e2db92400d0dd29764a751f237
5
5
  SHA512:
6
- metadata.gz: 4dca80dd8d697d5cb1edd5e7fc4c0a56814126e9796ebff2df2a8de92572c2e77682ee37702e497804b554f8980d9b883957bb22fa2d193f8a5d2651ba7cdf83
7
- data.tar.gz: 505d47e74bcf67876065b7e9575402f011d2fa662a967999b7adc1ea4856008f37589710f367623a72c6130538d2a13e2c561efb770456080fce998f2a4fc944
6
+ metadata.gz: fa4f36bd206392242e866a96756e57a38a8e9178bb1928b4eaf6f45ec608b6a368cea96c19aab781f7e2b7c71c12b0a5f52502741dca3f6755df2f2941a54547
7
+ data.tar.gz: 4201d0fc06a58b7cc6cdd6dbb91ef7d680600eefb52b6f74963cd3753dbbe41b3945099d45ddd77292b1b3817da894cbc3efa04bf6c669228aef4398c816f7e6
@@ -6,20 +6,34 @@
6
6
  # see <http://www.gnu.org/licenses/>.
7
7
 
8
8
  require 'json'
9
+
9
10
  module BeerBot
11
+
10
12
  module Utils
13
+
11
14
  # A class that loads data from a file and allows you to access it
12
15
  # using the #data method.
13
16
  #
14
17
  # If the file is updated (after >=1 sec), #data will reload.
18
+
15
19
  class DataFile
20
+
16
21
  attr_reader :reloaded # true if last call to #data reloaded file
22
+
23
+ def self.create! filepath
24
+ File.open(filepath,'w'){}
25
+ self.new(filepath)
26
+ end
27
+
17
28
  def initialize filepath
18
29
  @filepath = filepath
19
30
  @data = File.read(filepath)
20
31
  @mtime = File.stat(filepath).mtime
21
32
  @reloaded = false
22
33
  end
34
+
35
+ # Load data from file.
36
+
23
37
  def data
24
38
  @reloaded = false
25
39
  return @data unless File.exists?(@filepath)
@@ -31,14 +45,33 @@ module BeerBot
31
45
  @reloaded = true
32
46
  @data
33
47
  end
48
+
49
+ def save thing
50
+ File.open(@filepath,'w') {|f|
51
+ f.write(thing)
52
+ }
53
+ end
54
+
34
55
  end
56
+
35
57
  # Specialised DataFile that parses json.
58
+
36
59
  class JsonDataFile < DataFile
60
+
37
61
  attr_reader :json
62
+
63
+ def self.create! filepath,data={}
64
+ File.open(filepath,'w') {|f| f.puts(data.to_json)}
65
+ self.new(filepath)
66
+ end
67
+
38
68
  def initialize filepath
39
69
  super
40
70
  @json = JSON.parse(@data)
41
71
  end
72
+
73
+ # Load data from file and parse as json data.
74
+
42
75
  def data
43
76
  super
44
77
  begin
@@ -51,6 +84,20 @@ module BeerBot
51
84
  end
52
85
  @json
53
86
  end
87
+
88
+ # Save thing back to file.
89
+ #
90
+ # Thing is assumed to be a hash or array that we call to_json on.
91
+
92
+ def save thing=nil
93
+ if thing.nil? then
94
+ thing = @json # the thing self.data returns
95
+ end
96
+ super(thing.to_json)
97
+ end
98
+
54
99
  end
100
+
55
101
  end
102
+
56
103
  end
@@ -21,11 +21,6 @@ module BeerBot
21
21
  nick.sub(/^@/,'')
22
22
  end
23
23
 
24
- def user nick
25
- nick = self.remove_op(nick)
26
- super
27
- end
28
-
29
24
  def nick oldnick,nick
30
25
  oldnick = self.remove_op(oldnick)
31
26
  nick = self.remove_op(nick)
@@ -41,6 +36,11 @@ module BeerBot
41
36
  nick = self.remove_op(nick)
42
37
  super
43
38
  end
39
+
40
+ def quit nick
41
+ nick = self.remove_op(nick)
42
+ super
43
+ end
44
44
  end
45
45
 
46
46
  end
@@ -25,26 +25,11 @@ module BeerBot
25
25
 
26
26
  end
27
27
 
28
- # Fetch a user.
29
- def user user
30
- if self[:users].has_key?(user) then
31
- self[:users][user]
32
- else
33
- end
34
- end
35
-
36
- # Fetch a channel.
37
- def channel channel
38
- if self[:channels].has_key?(channel) then
39
- self[:channels][channel]
40
- else
41
- end
42
- end
43
-
44
28
  # Someone joins channel.
45
29
  def join nick,channel
46
30
  self[:channels][channel][:users].add(nick)
47
31
  self[:users][nick][:channels].add(channel)
32
+ self[:users][nick][:quit] = false
48
33
  self
49
34
  end
50
35
 
@@ -58,9 +43,9 @@ module BeerBot
58
43
  # Someone changes nick.
59
44
  def nick oldnick,nick
60
45
  self[:channels].each_pair{|name,chan|
61
- if chan.member?(oldnick) then
62
- chan.delete(oldnick)
63
- chan.add(nick)
46
+ if chan[:users].member?(oldnick) then
47
+ chan[:users].delete(oldnick)
48
+ chan[:users].add(nick)
64
49
  end
65
50
  }
66
51
  self[:users][nick] = self[:users][oldnick]
@@ -72,8 +57,13 @@ module BeerBot
72
57
  self
73
58
  end
74
59
 
75
- # TODO: User quits altogether.
76
- def quit user
60
+ def quit nick
61
+ self[:channels].each_pair{|name,chan|
62
+ chan[:users].delete(nick)
63
+ }
64
+ self[:users][nick][:channels] = Set.new
65
+ self[:users][nick][:quit] = true
66
+ self
77
67
  end
78
68
 
79
69
  end
@@ -7,7 +7,6 @@
7
7
 
8
8
  require 'socket'
9
9
  require 'set'
10
- require_relative 'Connection'
11
10
 
12
11
  module BeerBot
13
12
 
@@ -17,7 +16,7 @@ module BeerBot
17
16
  # We also do some low-level bookkeeping, like returning pong and
18
17
  # looking for welcome message.
19
18
 
20
- class IRCConnection < Connection
19
+ class IRCConnection
21
20
 
22
21
  # Queue containing received messages from the server.
23
22
  attr_accessor :queue,:writeq,:readyq
@@ -9,8 +9,121 @@ module BeerBot
9
9
 
10
10
  module Protocol
11
11
 
12
+ # IRC Protcol module.
13
+ #
14
+ # Main method is parse, whose job is to receive incoming irc
15
+ # strings and convert to a generalised format that higher layers
16
+ # like the dispatcher can use.
17
+
12
18
  module IRC
13
19
 
20
+ # Parse raw irc string and then yield or return a generic
21
+ # representation of the event.
22
+ #
23
+ # Returns [event,*args]
24
+ #
25
+ # Parse's job is to take the constituents parts of an irc
26
+ # message, identify the type of event and return a generic
27
+ # representation of it.
28
+ #
29
+ # So for instance, an irc privmsg (message) is represented as
30
+ # [:msg,from,to,msg]
31
+ #
32
+ # Note that connection readiness and PONG protocol are handled by
33
+ # the irc connection, not here.
34
+
35
+ def self.parse str
36
+
37
+ m = IRCMessage.new(str)
38
+ result = []
39
+
40
+ case m[:command]
41
+
42
+ when 'NICK' # change of nick
43
+ case s=m.check(:prefix,:nick,:trailing)
44
+ when Symbol
45
+ puts "* NICK expected #{s}"
46
+ return nil
47
+ end
48
+ old = m[:prefix][:nick]
49
+ nick = m[:trailing]
50
+ result = [:nick,old,nick]
51
+
52
+
53
+ when 'QUIT' # someone leaves irc
54
+ case s=m.check(:prefix,:nick,:trailing)
55
+ when Symbol
56
+ puts "* QUIT expected #{s}"
57
+ return nil
58
+ end
59
+ nick = m[:prefix][:nick]
60
+ msg = m[:trailing]
61
+ result = [:quit,nick,msg]
62
+
63
+ when 'PART' # someone leaves channel
64
+ case s=m.check(:prefix,:nick,:params)
65
+ when Symbol
66
+ puts "* PART expected #{s}"
67
+ return nil
68
+ end
69
+ if channel=m[:params][0] then
70
+ elsif channel=m[:trailing].strip.split(/\s+/).first then
71
+ else
72
+ channel=nil
73
+ puts "* PART can't determine what is being parted from: '#{str}'"
74
+ return nil
75
+ end
76
+ nick = m[:prefix][:nick]
77
+ result = [:part,nick,channel]
78
+
79
+ when 'INVITE' # someone invites us, oo
80
+ ournick = m[:params][0]
81
+ chan = m[:trailing].strip.split(/\s+/).first
82
+ result = [:invite,chan]
83
+
84
+ when 'JOIN' # someone joins channel
85
+ case s=m.check(:prefix,:nick,:trailing)
86
+ when Symbol
87
+ puts "* JOIN expected #{s}"
88
+ return nil
89
+ end
90
+ channel = m[:trailing]
91
+ nick = m[:prefix][:nick]
92
+ result = [:join,nick,channel]
93
+
94
+ when '353' # channel user list when we join the channel
95
+ case s=m.check(:params,:trailing)
96
+ when Symbol
97
+ puts "* 353 expected #{s}"
98
+ return nil
99
+ end
100
+ channel = m[:params][2]
101
+ users = m[:trailing].split(/\s+/)
102
+ result = [:chanlist,channel,users]
103
+ #puts "[parse/353] #{result}"
104
+
105
+ when '366' # end of 353
106
+ result = [:chanlistend]
107
+
108
+ when 'PRIVMSG'
109
+ case s=m.check(:prefix,:nick,:params,:trailing)
110
+ when Symbol
111
+ #puts "* PRIVMSG expected #{s}"
112
+ return nil
113
+ end
114
+
115
+ msg = m[:trailing].strip
116
+ from = m[:prefix][:nick].strip
117
+ to = m[:params][0].strip unless m[:params].empty?
118
+ result = [:msg,from,to,msg]
119
+
120
+ else # command we don't handle
121
+ result = [:default,m]
122
+ end
123
+
124
+ result
125
+ end
126
+
14
127
  # This class represents an irc message broken down into its
15
128
  # major constituent parts.
16
129
  #
@@ -113,90 +226,6 @@ module BeerBot
113
226
 
114
227
  end
115
228
 
116
- # Parse raw irc string and then yield or return a generic
117
- # representation of the event.
118
- #
119
- # Returns [event,*args]
120
- #
121
- # Parse's job is to take the constituents parts of an irc
122
- # message, identify the type of event and return a generic
123
- # representation of it.
124
- #
125
- # So for instance, an irc privmsg (message) is represented as
126
- # [:msg,from,to,msg]
127
- #
128
- # Note that connection readiness and PONG protocol are handled by
129
- # the irc connection, not here.
130
-
131
- def self.parse str
132
-
133
- m = IRCMessage.new(str)
134
- result = []
135
-
136
- case m[:command]
137
-
138
- when 'NICK' # change of nick
139
- case s=m.check(:prefix,:nick,:trailing)
140
- when Symbol
141
- puts "* NICK expected #{s}"
142
- return nil
143
- end
144
- old = m[:prefix][:nick]
145
- nick = m[:trailing]
146
- result = [:nick,old,nick]
147
-
148
- when 'PART' # someone leaves channel
149
- case s=m.check(:prefix,:nick,:params)
150
- when Symbol
151
- puts "* PART expected #{s}"
152
- return nil
153
- end
154
- channel = m[:params][0]
155
- nick = m[:prefix][:nick]
156
- result = [:part,nick,channel]
157
-
158
- when 'JOIN' # someone joins channel
159
- case s=m.check(:prefix,:nick,:trailing)
160
- when Symbol
161
- puts "* JOIN expected #{s}"
162
- return nil
163
- end
164
- channel = m[:trailing]
165
- nick = m[:prefix][:nick]
166
- result = [:join,nick,channel]
167
-
168
- when '353' # channel user list when we join the channel
169
- case s=m.check(:params,:trailing)
170
- when Symbol
171
- puts "* 353 expected #{s}"
172
- return nil
173
- end
174
- channel = m[:params][2]
175
- users = m[:trailing].split(/\s+/)
176
- result = [:chanlist,channel,users]
177
- #puts "[parse/353] #{result}"
178
-
179
- when '366' # end of 353
180
- result = [:chanlistend]
181
-
182
- when 'PRIVMSG'
183
- case s=m.check(:prefix,:nick,:params,:trailing)
184
- when Symbol
185
- #puts "* PRIVMSG expected #{s}"
186
- return nil
187
- end
188
-
189
- msg = m[:trailing].strip
190
- from = m[:prefix][:nick].strip
191
- to = m[:params][0].strip unless m[:params].empty?
192
- result = [:msg,from,to,msg]
193
-
194
- else # command we don't handle
195
- result = [:default,m]
196
- end
197
-
198
- result
199
- end
200
229
 
201
230
  # Return irc-conformat string from a botmsg hash.
202
231
  #
@@ -249,6 +278,12 @@ module BeerBot
249
278
  "JOIN #{chan}"
250
279
  end
251
280
 
281
+ # Leave channel.
282
+
283
+ def self.leave chan
284
+ "PART #{chan}"
285
+ end
286
+
252
287
  end
253
288
  end
254
289
 
@@ -81,10 +81,15 @@ module BeerBot
81
81
  replies = @bot.event(event,args:args)
82
82
  when :default
83
83
  replies = @bot.event(event,args:args)
84
+
84
85
  when :nick
85
86
  old,nick = args
86
87
  @world.nick(old,nick) if @world
87
88
  replies = @bot.event(event,old:old,nick:nick)
89
+
90
+ when :quit
91
+ nick,msg = args
92
+ @world.quit(nick) if @world
88
93
  when :part
89
94
  nick,channel = args
90
95
  @world.part(nick,channel) if @world
@@ -102,6 +107,8 @@ module BeerBot
102
107
  }
103
108
  end
104
109
  replies = @bot.event(event,channel:channel,users:users)
110
+ when :chanlistend
111
+ # ignore
105
112
 
106
113
  when :msg
107
114
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beerbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bush
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-21 00:00:00.000000000 Z
11
+ date: 2014-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -96,7 +96,6 @@ files:
96
96
  - lib/BeerBot/00.utils/More.rb
97
97
  - lib/BeerBot/00.utils/world/World.rb
98
98
  - lib/BeerBot/00.utils/world/IRCWorld.rb
99
- - lib/BeerBot/01.connect/Connection.rb
100
99
  - lib/BeerBot/01.connect/IRCConnection.rb
101
100
  - lib/BeerBot/01.bot/botmsg.rb
102
101
  - lib/BeerBot/01.bot/BotModule.rb
@@ -1,23 +0,0 @@
1
- # The files in this directory are part of BeerBot, a a ruby irc bot library.
2
- # Copyright (C) 2013,2014 Daniel Bush
3
- # This program is distributed under the terms of the GNU
4
- # General Public License. A copy of the license should be
5
- # enclosed with this project in the file LICENSE. If not
6
- # see <http://www.gnu.org/licenses/>.
7
-
8
- module BeerBot
9
- class Connection
10
- attr_accessor :queue
11
- def initialize
12
- @queue = Queue.new
13
- end
14
- def open
15
- end
16
- def close
17
- end
18
- def write str
19
- end
20
- def ready? &block
21
- end
22
- end
23
- end