yaib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yaib (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ mocha (0.9.12)
11
+ rspec (2.5.0)
12
+ rspec-core (~> 2.5.0)
13
+ rspec-expectations (~> 2.5.0)
14
+ rspec-mocks (~> 2.5.0)
15
+ rspec-core (2.5.1)
16
+ rspec-expectations (2.5.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.5.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ mocha
25
+ rspec
26
+ yaib!
@@ -0,0 +1,5 @@
1
+ h1. YAIB
2
+
3
+ As the name suggests YAIB is another IRC bot framework.
4
+
5
+ Early BETA is a good description for its current state so a lot is subject to change so the readme will be lacking for now
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,20 @@
1
+ require 'yaib/version'
2
+ require 'yaib/irc'
3
+ require 'yaib/config'
4
+ require 'yaib/bot'
5
+ require 'yaib/respond'
6
+ require 'yaib/channel'
7
+ require 'yaib/message'
8
+ require 'yaib/string'
9
+ require 'yaib/responder'
10
+ require 'yaib/user'
11
+ require 'logger'
12
+
13
+ module YAIB
14
+ def self.construct
15
+ @config = Config.new
16
+ yield @config
17
+ @config.log.info("Creating Bot")
18
+ @bot = Bot.new(@config)
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ module YAIB
2
+ class Bot
3
+ attr_accessor :channels
4
+ attr_reader :channels, :irc, :config
5
+
6
+ def initialize(conf)
7
+ @config = conf
8
+ @config.log.info("Connecting to IRC Server")
9
+ @irc = IRC.new(config.server, config.port, config.nick, config.name, config.host, config.pass, config.log)
10
+ @channels = {}
11
+ @users = {}
12
+ @listeners = []
13
+ @commands = []
14
+ @config.listeners.each do |responder|
15
+ @listeners.push responder.new(config)
16
+ end
17
+ @config.commands.each do |responder|
18
+ @commands.push responder.new(config)
19
+ end
20
+ @config.log.info("Joining Channels")
21
+ config.channels.each do |channel|
22
+ @channels[channel] = Channel.new(channel, self)
23
+ end
24
+ loop do
25
+ recv
26
+ end
27
+ end
28
+
29
+ def recv
30
+ msg = @irc.recv
31
+ @config.log.info("Recieved: #{msg}")
32
+ if msg =~ /PING/
33
+ @irc.send("PONG #{@config.host}")
34
+ else
35
+ if msg.privmsg?
36
+ msg = YAIB::Message.new(msg, self)
37
+ @listeners.each do |listen|
38
+ listen.listen(msg)
39
+ end
40
+ @commands.each do |cmd|
41
+ if msg.message =~ /^#{config.prefix}#{cmd.matcher}/
42
+ cmd.execute(msg)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ def return_user(inick, chan)
50
+ user = @users[inick]
51
+ unless user
52
+ user = User.new(inick, self)
53
+ @users[inick] = user
54
+ end
55
+ user.channel = chan
56
+ user
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,12 @@
1
+ module YAIB
2
+ class Channel < YAIB::Respond
3
+ attr_accessor :name
4
+
5
+ def initialize(channel, bot)
6
+ @bot = bot
7
+ @name = channel
8
+ @channel = @name
9
+ @bot.irc.join channel
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module YAIB
2
+ class Config
3
+ attr_accessor :nick, :name, :channels, :server, :port, :log, :host, :pass, :listeners, :prefix, :commands
4
+
5
+ def initialize
6
+ @nick = "yaib-bot"
7
+ @name = "yaib-bot"
8
+ @channels = []
9
+ @port = 6667
10
+ @host = "arcath.net"
11
+ @log = Logger.new("log/yaib.log", "weekly")
12
+ @listeners = []
13
+ @prefix = "!"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,51 @@
1
+ module YAIB
2
+ class IRC
3
+ require 'socket'
4
+
5
+ def initialize(server, port, nick, name, host, pass, log)
6
+ @log = log
7
+ @log.info("Creating Connection")
8
+ @con = TCPSocket.new(server, port)
9
+ @log.info("Sending Identification")
10
+ send("USER #{nick} #{host} bla :#{name}")
11
+ send("NICK #{nick}")
12
+ @log.info("Getting Response")
13
+ msg = recv
14
+ while msg !~ /^:.* 001.*/
15
+ @log.info("Received: #{msg}")
16
+ if msg =~ /433/
17
+ @log.info("Nickname was taken adding a _")
18
+ nick += "_"
19
+ send("NICK #{nick}")
20
+ end
21
+ msg = @con.recv(512)
22
+ end
23
+ @log.info("Setting nickname variable")
24
+ @nick = nick
25
+ end
26
+
27
+ def recv
28
+ @con.recv(512)
29
+ end
30
+
31
+ def send(s)
32
+ s = s.gsub(/\n/,'').gsub(/\r/,'')
33
+ @log.info("Sending: #{s}")
34
+ @con.send(s + "\n", 0)
35
+ end
36
+
37
+ def join(channel)
38
+ @log.info("Joining #{channel}")
39
+ send("JOIN #{channel}")
40
+ msg = recv
41
+ while msg =~ /25[0-5]/
42
+ msg = nil
43
+ msg = recv unless msg =~ /255/
44
+ end
45
+ end
46
+
47
+ def part(channel)
48
+ send("PART #{channel}")
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,24 @@
1
+ module YAIB
2
+ class Message
3
+ attr_reader :message, :user, :channel
4
+
5
+ def initialize(rawmsg, bot)
6
+ @message, rawchan, rawuser = parse(rawmsg)
7
+ if rawchan == bot.config.nick then
8
+ @channel = bot.return_user rawuser, rawuser
9
+ @user = @channel
10
+ else
11
+ @channel = bot.channels[rawchan]
12
+ @user = bot.return_user rawuser, rawchan
13
+ end
14
+ end
15
+
16
+ def parse(rawmsg)
17
+ [rawmsg.scan(/.* PRIVMSG .*? \:(.*)/).join, rawmsg.scan(/.* PRIVMSG (.*?) \:.*/).join, rawmsg.split(/\!/)[0].sub(/^\:/,'')]
18
+ end
19
+
20
+ def privmsg(s)
21
+ @channel.privmsg(s)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module YAIB
2
+ class Respond
3
+ def privmsg(s)
4
+ @bot.irc.send("PRIVMSG #{@channel} :#{s}")
5
+ end
6
+
7
+ def action(s)
8
+ @bot.irc.send("ACTION #{@channel} :#{s}")
9
+ end
10
+
11
+ def notice(s)
12
+ @bot.irc.send("NOTICE #{@channel} :#{s}")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ module YAIB
2
+ class Responder
3
+ def initialize(config)
4
+ @config = config
5
+ config.log.info("Created new responder")
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ class String
2
+ def privmsg?
3
+ self=~/^\:.*!.*\@.*PRIVMSG\ .* \:.*/
4
+ end
5
+ def part?
6
+ self=~/^\:.*!.*\@.*PART\ \#.*/
7
+ end
8
+ def quit?
9
+ self=~/^\:.*!.*\@.*QUIT\ :.*/
10
+ end
11
+ def join?
12
+ self=~/^\:.*!.*\@.*JOIN\ :\#.*/
13
+ end
14
+ def op?
15
+ self=~/^\:.*!.* MODE \#.* \+o .*/
16
+ end
17
+ def deop?
18
+ self=~/^\:.*!.* MODE \#.* \-o .*/
19
+ end
20
+ def invite?
21
+ self=~/^\:.*!.* INVITE .* \:.*/
22
+ end
23
+ def ping?
24
+ self=~/^PING :.*/
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ module YAIB
2
+ class User < YAIB::Respond
3
+ attr_accessor :nick, :channel
4
+
5
+ def initialize(nick, bot)
6
+ @nick = nick
7
+ @bot = bot
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module YAIB
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ # Logfile created on Sun Apr 24 11:21:38 +0100 2011 by logger.rb/22285
@@ -0,0 +1,11 @@
1
+ require 'lib/yaib.rb'
2
+
3
+ describe YAIB, "#construct" do
4
+ it "Should take a block" do
5
+ #YAIB::Bot.stubs!(:new).and_return(true)
6
+ YAIB.construct do |c|
7
+ c.nick = "Test"
8
+ c.server = "irc.freenode.net"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "yaib/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "yaib"
7
+ s.version = YAIB::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Adam \"Arcath\" Laycock"]
10
+ s.email = ["gems@arcath.net"]
11
+ s.homepage = "http://www.arcath.net"
12
+ s.summary = "Yet Another IRC Bot Framework"
13
+
14
+ s.add_development_dependency "rspec"
15
+ s.add_development_dependency "mocha"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaib
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Adam "Arcath" Laycock
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-28 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description:
50
+ email:
51
+ - gems@arcath.net
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - README.textile
62
+ - Rakefile
63
+ - lib/yaib.rb
64
+ - lib/yaib/bot.rb
65
+ - lib/yaib/channel.rb
66
+ - lib/yaib/config.rb
67
+ - lib/yaib/irc.rb
68
+ - lib/yaib/message.rb
69
+ - lib/yaib/respond.rb
70
+ - lib/yaib/responder.rb
71
+ - lib/yaib/string.rb
72
+ - lib/yaib/user.rb
73
+ - lib/yaib/version.rb
74
+ - log/yaib.log
75
+ - spec/yaib_spec.rb
76
+ - yaib.gemspec
77
+ has_rdoc: true
78
+ homepage: http://www.arcath.net
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.6.2
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Yet Another IRC Bot Framework
111
+ test_files:
112
+ - spec/yaib_spec.rb