Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b72885a038
Fetching contributors…

Cannot retrieve contributors at this time

file 122 lines (98 sloc) 2.904 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
require 'isaac'
require 'yaml'
require 'open-uri'
require 'net/http'

module Gist
  extend self
 
  def write(content, private_gist = false)
    url = URI.parse('http://gist.github.com/gists')
    req = Net::HTTP.post_form(url, data(content, private_gist))
    loc = req['Location']
    puts "Gist created at: #{loc}"
    loc
  end
 
  private
 
  def data(content, private_gist)
    return {
      'file_name[gistfile1]' => nil,
      'file_ext[gistfile1]' => nil,
      'file_contents[gistfile1]' => content
    }.merge(private_gist ? { 'private' => 'on' } : {})
  end
end

class MessageLog
  @@logs = {}
  
  def self.find_or_create_for_channel(channel)
    find(channel) || new(channel)
  end
  
  def self.find(channel)
    @@logs[channel]
  end
  
  def initialize(channel)
    @started_at = Time.now.gmtime
    @channel = channel
    @messages = []
    @@logs[channel] = self
  end
  
  def add(nick, msg)
    @messages << '[' + Time.now.gmtime.strftime('%I:%M%p') + "] <#{nick}> #{msg}"
  end
  
  def read(lines)
    lines = @messages.size if lines > @messages.size
    output = "Channel #{@channel} at #{@started_at.to_s}\n\n"
    output << @messages[-lines, lines].join("\n")
    output << "\n\nLogging finished at: #{Time.now.gmtime.to_s}\n\n"
    output << "Translate time: #{date_link}"
  end
  
  def date_link
    "http://timeanddate.com/worldclock/fixedtime.html?" +
    "day=#{@started_at.day}" +
    "&month=#{@started_at.month}" +
    "&year=#{@started_at.year}" +
    "&hour=#{@started_at.hour}"+
    "&min=#{@started_at.min}" +
    "&sec=#{@started_at.hour}" +
    "&p1=136" # UTC
  end
end


configuration = YAML.load_file(File.dirname(__FILE__) + '/config.yml')

configure do |c|
  configuration.each {|k,v| c.send("#{k}=", v) }
end

helpers do
  def log(channel, nick, msg)
    log = MessageLog.find_or_create_for_channel(channel)
    log.add(nick, msg)
  end
  
  def log_lines(channel, num)
    log = MessageLog.find(channel)
    log.read(num.to_i)
  end
end


on :connect do
  puts "#{configuration['nick']} connected to #{configuration['server']} (port #{configuration['port']}).\n\n"
  puts "To invite #{configuration['nick']} to your channel, use:"
  puts "/msg #{configuration['nick']} !invite #channel-name\n\n"
end

on :channel, /^\!gist log (\d+)/ do
  msg channel, "Chat log gist created! " + Gist.write(log_lines(channel, match[1]))
end

on :channel, /^\!gist (.*)/ do
  msg channel, "Gist created! " + Gist.write(match[1])
end

on :channel, /.*/ do
  log channel, nick, message
end

on :private, /^\!invite (.*)/ do |channel_name|
  msg nick, "Aye-aye! Deploying to #{channel_name}"
  puts "Joining #{channel_name}"
  join channel_name
end

on :private, /^\!leave (.*)/ do |channel_name|
  msg nick, "Roger that. Leaving #{channel_name}"
  puts "Leaving #{channel_name}"
  part channel_name
end

on :private, /.*/ do
  msg nick, Gist.write(message, :private)
end
Something went wrong with that request. Please try again.