Browse Source

Add Sv_IRC client

master
Marrub 2 years ago
parent
commit
5d929e258d
2 changed files with 119 additions and 0 deletions
  1. +3
    -0
      README
  2. +116
    -0
      source/servers/sv_irc.rb

+ 3
- 0
README View File

@@ -4,3 +4,6 @@ Mod_Audio requires:

Sv_Discord requires:
- discordrb

Sv_IRC requires:
- cinch

+ 116
- 0
source/servers/sv_irc.rb View File

@@ -0,0 +1,116 @@
require 'cinch'

# A server implementation for IRC using cinch.
class Sv_IRC < Vrobot4::Server::Server
# The server type name.
def self.type
"IRC"
end

Vrobot4::Server.add_server_type self

attr_reader :bot # The Cinch::Bot instance.

# (see Vrobot4::Server::Server#initialize)
def initialize info
super

this = self
@bot = Cinch::Bot.new do
configure do |cfg|
cfg.server = info["server"]
cfg.nick = (info["nick"] or "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"
cfg.realname = "vrobot4"
cfg.user = "vrobot4"
cfg.message_split_start = "… "
cfg.message_split_end = " …"
end

on :message do |evt|
m = Vrobot4::Server::Message.new \
msg: evt.message,
user: User.new(evt.user, evt.channel),
chan: Channel.new(evt.channel),
serv: this,
reply: -> (text) {evt.reply text},
reply_b: -> (text) {evt.reply text}

this.handle_text_cmd m
end
end

@bot.loggers.level = :warn
end

# (see Vrobot4::Server::Server#connect)
def connect
@bot.start
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: {}}
pinf.each do |pr|
mod = Vrobot4::Module.get_module_type(pr["module"])[:type]
if pr.key? "channel"
@mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod
@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"]
end
end if pinf
end

class ChannelPerms
def initialize() @cprm = {} end

# Returns true if the channel is enabled, false otherwise.
def [](chan) @cprm[chan.name] end

# Sets a channel's permission on/off.
def []=(name, set) @cprm[name] = set end
end
private_constant :ChannelPerms

# 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
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
end
end
end

## EOF

Loading…
Cancel
Save