public
Description: irc bot for collaborative tweeting, with a #sinatra twist.
Homepage:
Clone URL: git://github.com/ichverstehe/nancie.git
nancie / bot.rb
100644 98 lines (79 sloc) 2.331 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
# Meh, I went overboard. Usage:
#
# To tweet, write in channel:
# nancie, tweet this: some nice thing about sinatra and stuff
# To give permissions, private message:
# /msg nancie allow awesome_user
#
# Additional extensions are welcome.
 
require 'rubygems'
require 'isaac' # requires 0.2 / shaft
require 'rest_client'
require 'yaml'
require 'json'
 
module Nancie
  extend self
 
  attr_reader :config
 
  def load_config
    @config = YAML.load_file('config.yml')
  end
 
  def write_config
    File.open('config.yml', 'w') { |f| YAML.dump(@config, f) }
  end
 
  def allowed?(nick)
    config['allowed'].include?(nick)
  end
 
  def allow!(nick)
    @config['allowed'] << nick
    write_config
  end
 
  def twitter_credentials
    "#{config['twitter']['login']}:#{config['twitter']['password']}"
  end
end
 
Nancie.load_config
 
configure do |c|
  c.nick = Nancie.config['irc']['nick']
  c.server = Nancie.config['irc']['server']
  c.port = Nancie.config['irc']['port'] || 6667
end
 
helpers do
  def twitter(url, params={})
    JSON.parse(RestClient.post "http://#{Nancie.twitter_credentials}@twitter.com/" + url + ".json", params)
  end
 
  def ensure_permissions
    halt unless Nancie.allowed?(nick)
  end
end
 
on :connect do
  join "##{Nancie.config['irc']['channel']}"
  msg 'nickserv', "identify #{Nancie.config['irc']['nickserv']}"
end
 
on :channel, /^#{Nancie.config['irc']['nick']}.*tweet this.? (.*)/ do
  ensure_permissions
  begin
    reply = twitter "statuses/update", :status => match[0]
    msg channel, "#{nick}, http://twitter.com/#{Nancie.config['twitter']['login']}/status/#{reply['id']}"
  rescue
    msg channel, "#{nick}, something went wrong as I tried to tweet."
  end
end
 
on :channel, /^#{Nancie.config['irc']['nick']}.* follow (\S+)/ do
  ensure_permissions
  begin
    follow = match[0]
    reply = twitter "friendships/create/#{follow}"
    msg channel, "#{nick}, we're now following #{reply['screen_name']}."
  rescue
    msg channel, "#{nick}, something went wrong as I tried to follow #{follow}."
  end
end
 
on :private, /^allow (\S+)/ do
  ensure_permissions
 
  allow = match[0]
  Nancie.allow!(allow)
  msg nick, "#{allow} has throwing stars!"
end
 
on :channel, /^#{Nancie.config['irc']['nick']}.* what is the answer to life, the universe, and everything/ do
  msg channel, "#{nick}, 42"
end