-
Notifications
You must be signed in to change notification settings - Fork 0
/
WikiBot.rb
117 lines (91 loc) · 2.36 KB
/
WikiBot.rb
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
require "MediaFetch"
require "set"
require "socket"
class WikiBot
attr_reader :logger
def initialize(server, port, nick, password = nil)
@socket = TCPSocket.open(server, port || 6667)
@socket.puts "PASSWORD #{password}" if password
nick(nick)
@socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
@channels = Set.new
@hosts_idents = Hash.new
@logger = Logger.new
@commands = Array.new
sleep 1
self
end
def in_channel?(channel)
@channels.include? channel.downcase
end
def quit(reason = nil)
@socket.puts "QUIT :#{reason or ''}"
@socket.gets until @socket.eof?
end
def join(channel, password = nil)
return if in_channel? channel
@channel_password = password || ""
@socket.puts "JOIN #{channel} #{@channel_password}"
@channels.add channel.downcase
end
def nick(newNick = "dariobot")
@socket.puts "NICK #{newNick}"
end
def ping(message)
server = (message.split " ")[1]
@socket.puts "PONG #{server}"
end
def say(message, channel = nil)
if channel.nil?
raise ArgumentError unless @channels.length == 1
channel = "#{@channels.first}"
end
join channel unless @channels.member? channel
@socket.puts "PRIVMSG #{channel} :#{message}"
end
def get_messages()
process_message @socket.gets until @socket.eof?
end
def process_message(message)
puts message
messageParts = message.split " "
return ping message if messageParts[0] == "PING"
senderData = message.match /^:(.+?)!(.+?)@(.+?) (.+?) (.+?) :(.*)/
return if senderData.nil?
nick = senderData[1]
ident = senderData[2]
host = senderData[3]
type = senderData[4]
channel = senderData[5]
message = senderData[6].chomp
return if type != "PRIVMSG"
command = IrcCommand.new nick, ident, host, channel, message, self
log = true
@commands.each { |x|
if x.match command
x.enact command
log = false
end
}
@logger.log channel, nick, message if log
#when ".quote"
# return
#when /https?:\/\/wiki.netsoc.(?:tcd.)?ie/
# puts "wiki"
end
def admin_authenticated(ident, host)
idents = @hosts_idents[host.downcase]
!idents.nil? && idents.member?(ident.downcase)
end
def add_admin(ident, host)
idents = @hosts_idents[host.downcase]
if idents.nil?
idents = Set.new
@hosts_idents[host.downcase] = idents
end
idents.add ident.downcase
end
def add_command(command)
@commands.push command unless command.nil?
end
end