diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 04:23:20 -0700 | 
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-20 04:23:20 -0700 | 
| commit | 7f134adccb20abe773234acecf526c9951b418e8 (patch) | |
| tree | eeb4b4f96a963591f244c8f661b84dd194cf1484 /core | |
| parent | 7e7aa3278a9e478b02eb152575fb00ee0802ac81 (diff) | |
| download | dinobot-7f134adccb20abe773234acecf526c9951b418e8.tar.gz dinobot-7f134adccb20abe773234acecf526c9951b418e8.tar.xz | |
Improved namespace and directory structure.
Diffstat (limited to 'core')
| -rw-r--r-- | core/irc.rb | 67 | ||||
| -rw-r--r-- | core/logger.rb | 50 | 
2 files changed, 117 insertions, 0 deletions
| diff --git a/core/irc.rb b/core/irc.rb new file mode 100644 index 0000000..779b37b --- /dev/null +++ b/core/irc.rb @@ -0,0 +1,67 @@ +require 'socket' + +require_relative 'logger' + +module Dinobot +  module Core +    class IRC +      def initialize(server, port, nick, pass=nil) +        @server = server +        @port = port +        @nick = nick +        @pass = pass + +        @socket = nil +        @logger = Dinobot::Core::Logger.instance +      end + +      def connect +        @logger.info "Connecting to #{@server}:#{@port}." + +        @socket = TCPSocket.new(@server, @port) + +        puts "PASS #{@pass}" if @pass +        puts "NICK #{@nick}" +        puts "USER #{@nick} 0 * :#{@nick}" +      end + +      def disconnect +        @socket.close +      end + +      def connected? +        !(@socket.nil? || @socket.closed?) +      end + +      def gets +        str = @socket.gets + +        @logger.in str.inspect + +        str +      end + +      def puts(str) +        @logger.out str.inspect + +        @socket.puts str +      end + +      def pong(message) +        puts "PONG #{message}" +      end + +      def join(channel) +        puts "JOIN #{channel}" +      end + +      def part(channel) +        puts "PART #{channel}" +      end + +      def privmsg(channel, message) +        puts "PRIVMSG #{channel} :#{message}" +      end +    end +  end +end diff --git a/core/logger.rb b/core/logger.rb new file mode 100644 index 0000000..d73cd15 --- /dev/null +++ b/core/logger.rb @@ -0,0 +1,50 @@ +module Dinobot +  module Core +    class Logger +      @@instance = nil +      @@mutex = Mutex.new + +      def in(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[32m<<\e[0m ") +      end + +      def out(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[36m>>\e[0m ") +      end + +      def error(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[31m!!\e[0m ") +      end + +      def info(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, "\e[33m==\e[0m ") +      end + +      def indent(*lines) +        str = lines.join("\n") + +        puts str.gsub(/^/, '   ') +      end + +      class << self +        def instance +          return @@instance if @@instance + +          @@mutex.synchronize do +            @@instance ||= new +          end +        end +      end + +      private_class_method :allocate, :new +    end +  end +end | 
