2019-02-23 22:43:50 -08:00
|
|
|
# IRC backend.
|
|
|
|
module Backend_IRC
|
|
|
|
require 'cinch'
|
|
|
|
|
|
|
|
# Helper for channel permissions.
|
|
|
|
class ChannelPerms
|
|
|
|
def initialize() @cprm = {} end
|
|
|
|
|
|
|
|
# @param chan [Backend_IRC::Channel] the channel for this permission
|
|
|
|
# @return [Boolean] true if the channel is enabled, false otherwise.
|
|
|
|
def [](chan) @cprm[chan.name] end
|
|
|
|
|
|
|
|
# Sets a channel's permission on/off.
|
|
|
|
# @param name [String] the channel for this permission
|
|
|
|
# @param set [Boolean] if this permission should be set
|
|
|
|
# @return [void]
|
|
|
|
def []=(name, set) @cprm[name] = set end
|
|
|
|
end
|
|
|
|
|
|
|
|
# An IRC user.
|
|
|
|
class User < Vrobot4::Server::User
|
|
|
|
attr_reader :real # The Cinch::User instance.
|
|
|
|
|
|
|
|
# @param user [Cinch::User] the irc user
|
|
|
|
# @param chan [Cinch::Channel] the irc channel this object was made in
|
|
|
|
def initialize user, chan
|
|
|
|
@real = user
|
|
|
|
@name = user.nick
|
|
|
|
@id = Vrobot4.hash_str user.nick.downcase
|
|
|
|
if user.oper? then @roles = "Oohv"
|
|
|
|
elsif chan.opped? user then @roles = "ohv"
|
|
|
|
elsif chan.half_opped? user then @roles = "hv"
|
|
|
|
elsif chan.voiced? user then @roles = "v"
|
|
|
|
else @roles = "" end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# An IRC channel.
|
|
|
|
class Channel < Vrobot4::Server::Channel
|
|
|
|
attr_reader :real # The Cinch::Channel instance.
|
|
|
|
|
|
|
|
# @param chan [Cinch::Channel] the irc channel
|
|
|
|
def initialize chan
|
|
|
|
@real = chan
|
|
|
|
@name = chan.name.downcase
|
|
|
|
@id = Vrobot4.hash_str chan.name.downcase
|
|
|
|
end
|
2019-03-04 23:23:15 -08:00
|
|
|
|
|
|
|
def send_msg text
|
|
|
|
@real.send text
|
|
|
|
end
|
2019-02-23 22:43:50 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
# An IRC server.
|
|
|
|
class Server < Vrobot4::Server::Server
|
|
|
|
# The server type name.
|
|
|
|
def self.type() "IRC" end
|
|
|
|
|
|
|
|
# (see Vrobot4::Server::Server#initialize)
|
|
|
|
def initialize info, bot
|
|
|
|
super
|
2019-02-24 03:00:41 -08:00
|
|
|
@id = Vrobot4.hash_str info[:server].downcase
|
2019-02-23 22:43:50 -08:00
|
|
|
|
2019-02-24 03:00:41 -08:00
|
|
|
for mod in info[:modules] do
|
2019-02-23 22:43:50 -08:00
|
|
|
load_mod mod
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# (see Vrobot4::Server::Server#flags)
|
|
|
|
def flags
|
|
|
|
?L
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# (see Vrobot4::Server::Server#load_permissions)
|
|
|
|
def load_permissions pinf
|
|
|
|
@mprm = {chan: {}, role: {}, glob: {}}
|
|
|
|
return unless pinf
|
|
|
|
pinf.each do |pr|
|
2019-02-24 03:00:41 -08:00
|
|
|
mod = Vrobot4::Module.get_module_type(pr[:module])[:type]
|
|
|
|
if pr.key? :channel
|
2019-02-23 22:43:50 -08:00
|
|
|
@mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan][mod]
|
2019-02-24 03:00:41 -08:00
|
|
|
@mprm[:chan][mod][pr[:channel].downcase] = true
|
|
|
|
elsif pr.key? :roles
|
|
|
|
@mprm[:role][mod] = pr[:roles]
|
|
|
|
elsif pr.key? :enable
|
|
|
|
@mprm[:glob][mod] = pr[:enable]
|
2019-02-23 22:43:50 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# A bot implementation for IRC using cinch.
|
|
|
|
class Bot < Vrobot4::Robots::Bot
|
|
|
|
# The bot type name.
|
|
|
|
def self.type() "IRC" end
|
|
|
|
Vrobot4::Robots.add_bot_type self
|
|
|
|
|
|
|
|
attr_accessor :serv # @return [Vrobot4::Server::Server] the server
|
2019-03-04 23:23:15 -08:00
|
|
|
attr_reader :real # @return [Cinch::Bot] the bot instance
|
2019-02-23 22:43:50 -08:00
|
|
|
|
|
|
|
# (see Vrobot4::Robots::Bot#initialize)
|
|
|
|
def initialize info
|
|
|
|
super
|
|
|
|
this = self
|
|
|
|
|
2019-03-04 23:23:15 -08:00
|
|
|
@real = Cinch::Bot.new do
|
2019-02-23 22:43:50 -08:00
|
|
|
configure do |cfg|
|
2019-07-17 16:00:54 -07:00
|
|
|
cfg.server = info[:server]
|
|
|
|
cfg.nick = info[:nick] || "vrobot4"
|
|
|
|
cfg.port = info[:port] if info.key? :port
|
|
|
|
cfg.password = info[:pass] if info.key? :pass
|
|
|
|
cfg.modes = info[:modes] if info.key? :modes
|
|
|
|
cfg.channels = info[:channels] if info.key? :channels
|
2019-07-17 16:04:18 -07:00
|
|
|
cfg.ssl.use = true
|
2019-07-17 16:00:54 -07:00
|
|
|
cfg.ssl.verify = true
|
|
|
|
cfg.realname = "vrobot4"
|
|
|
|
cfg.user = "vrobot4"
|
2019-02-23 22:43:50 -08:00
|
|
|
cfg.message_split_start = "… "
|
|
|
|
cfg.message_split_end = " …"
|
|
|
|
end
|
|
|
|
|
|
|
|
on :message do |evt|
|
|
|
|
return unless evt.channel
|
|
|
|
|
|
|
|
m = Vrobot4::Server::Message.new \
|
|
|
|
msg: evt.message,
|
|
|
|
user: User.new(evt.user, evt.channel),
|
|
|
|
chan: Channel.new(evt.channel),
|
|
|
|
serv: this.serv,
|
2019-02-24 03:00:41 -08:00
|
|
|
bot: this,
|
2019-02-23 22:43:50 -08:00
|
|
|
reply: -> (text) {evt.reply text},
|
|
|
|
reply_b: -> (text) {evt.reply text}
|
|
|
|
|
|
|
|
this.serv.handle_text_cmd m
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-04 23:23:15 -08:00
|
|
|
@real.loggers.level = :warn
|
2019-02-23 22:43:50 -08:00
|
|
|
@serv = Server.new info, self
|
|
|
|
end
|
|
|
|
|
|
|
|
# (see Vrobot4::Robots::Bot#connect)
|
|
|
|
def connect
|
2019-03-04 23:23:15 -08:00
|
|
|
@real.start
|
2019-02-23 22:43:50 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
## EOF
|