Permalink
Browse files

initial spike, anita records transcripts!

  • Loading branch information...
1 parent ebe3a62 commit 0ffd43858d606a230eb728cefeb02e9dd20a2b6a @semmons99 semmons99 committed May 10, 2012
Showing with 127 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +4 −0 Gemfile
  3. +54 −0 Rakefile
  4. +6 −0 bin/anita
  5. +9 −0 config/environment.rb.example
  6. +9 −0 lib/anita.rb
  7. +14 −0 lib/anita/bot.rb
  8. +12 −0 lib/anita/db.rb
  9. +14 −0 lib/anita/messages.rb
View
@@ -11,8 +11,13 @@ spec/reports
test/tmp
test/version_tmp
tmp
+Gemfile.lock
# YARD artifacts
.yardoc
_yardoc
doc/
+
+# Project specific
+config/environment.rb
+data
View
@@ -0,0 +1,4 @@
+source :rubygems
+
+gem "cinch", "~> 2.0.2"
+gem "sqlite3", "~> 1.3.6"
View
@@ -0,0 +1,54 @@
+require "fileutils"
+require "sqlite3"
+
+desc "Start bot (runs setup if needed)."
+task "start" => ["setup"] do
+ FileUtils.cd(File.dirname(__FILE__)) do
+ system "bundle exec ruby bin/anita"
+ end
+end
+
+desc "Run all setup tasks."
+task "setup" => ["setup:configuration", "setup:database"]
+
+desc "Install dependencies."
+task "setup:dependencies" do
+ FileUtils.cd(File.dirname(__FILE__)) do
+ unless File.exists?("Gemfile.lock")
+ system("gem install bundler")
+ system("bundle install")
+ end
+ end
+end
+
+desc "Create default configuration."
+task "setup:configuration" do
+ dir = File.expand_path(File.dirname(__FILE__) + "/config")
+
+ unless File.exists?(dir + "/environment.rb")
+ FileUtils.cp(
+ dir + "/environment.rb.example",
+ dir + "/environment.rb"
+ )
+
+ system("$EDITOR #{dir}/environment.rb")
+ end
+end
+
+desc "Initialize database."
+task "setup:database" => ["setup:configuration", "setup:dependencies"] do
+ require_relative "config/environment"
+
+ dir = File.dirname(Anita::Config::DB)
+ FileUtils.mkdir(dir) unless Dir.exists?(dir)
+
+ db = SQLite3::Database.new(Anita::Config::DB)
+ db.execute("
+ CREATE TABLE IF NOT EXISTS transcripts (
+ timestamp TEXT NOT NULL,
+ channel TEXT NOT NULL,
+ nick TEXT NOT NULL,
+ text TEXT NOT NULL
+ )"
+ )
+end
View
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require_relative "../config/environment.rb"
+require_relative "../lib/anita"
+
+Anita::Bot.start
@@ -0,0 +1,9 @@
+module Anita
+ module Config
+ NICK = "an_awesome_bot"
+ PASSWORD = "a_sekret_password"
+ SERVER = "irc.freenode.net"
+ CHANNELS = ["#cinch-bots"]
+ DB = File.dirname(__FILE__) + "/../data/anita.db"
+ end
+end
View
@@ -0,0 +1,9 @@
+require "sqlite3"
+require "cinch"
+require "date"
+
+require_relative "../config/environment"
+
+require_relative "anita/db"
+require_relative "anita/messages"
+require_relative "anita/bot"
View
@@ -0,0 +1,14 @@
+module Anita
+ Bot = Cinch::Bot.new do
+ configure do |c|
+ c.nick = Anita::Config::NICK
+ c.password = Anita::Config::PASSWORD
+ c.server = Anita::Config::SERVER
+ c.channels = Anita::Config::CHANNELS
+ end
+
+ on :channel do |message|
+ Messages.save(message)
+ end
+ end
+end
View
@@ -0,0 +1,12 @@
+module Anita
+ module Storage
+ DB = SQLite3::Database.new(Anita::Config::DB)
+
+ module Statements
+ Save = DB.prepare("
+ INSERT INTO transcripts (timestamp, channel, nick, text)
+ VALUES (:timestamp, :channel, :nick, :text)"
+ )
+ end
+ end
+end
View
@@ -0,0 +1,14 @@
+module Anita
+ module Messages
+ def self.save(m)
+ Storage::Statements::Save.execute(
+ "timestamp" => DateTime.now.to_s,
+ "channel" => m.channel.to_s,
+ "nick" => m.user.to_s,
+ "text" => m.message.encode(
+ "UTF-8", invalid: :replace, undef: :replace
+ )
+ )
+ end
+ end
+end

0 comments on commit 0ffd438

Please sign in to comment.