Browse Source

Add Sv_IRC client

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:
4 4
 
5 5
 Sv_Discord requires:
6 6
 - discordrb
7
+
8
+Sv_IRC requires:
9
+- cinch

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

@@ -0,0 +1,116 @@
1
+require 'cinch'
2
+
3
+# A server implementation for IRC using cinch.
4
+class Sv_IRC < Vrobot4::Server::Server
5
+   # The server type name.
6
+   def self.type
7
+      "IRC"
8
+   end
9
+
10
+   Vrobot4::Server.add_server_type self
11
+
12
+   attr_reader :bot # The Cinch::Bot instance.
13
+
14
+   # (see Vrobot4::Server::Server#initialize)
15
+   def initialize info
16
+      super
17
+
18
+      this = self
19
+      @bot = Cinch::Bot.new do
20
+         configure do |cfg|
21
+            cfg.server   =  info["server"]
22
+            cfg.nick     = (info["nick"]     or "vrobot4")
23
+            cfg.port     =  info["port"]     if info.key? "port"
24
+            cfg.password =  info["pass"]     if info.key? "pass"
25
+            cfg.modes    =  info["modes"]    if info.key? "modes"
26
+            cfg.channels =  info["channels"] if info.key? "channels"
27
+            cfg.realname = "vrobot4"
28
+            cfg.user     = "vrobot4"
29
+            cfg.message_split_start = "… "
30
+            cfg.message_split_end   = " …"
31
+         end
32
+
33
+         on :message do |evt|
34
+            m = Vrobot4::Server::Message.new \
35
+               msg:  evt.message,
36
+               user: User.new(evt.user, evt.channel),
37
+               chan: Channel.new(evt.channel),
38
+               serv: this,
39
+               reply:   -> (text) {evt.reply text},
40
+               reply_b: -> (text) {evt.reply text}
41
+
42
+            this.handle_text_cmd m
43
+         end
44
+      end
45
+
46
+      @bot.loggers.level = :warn
47
+   end
48
+
49
+   # (see Vrobot4::Server::Server#connect)
50
+   def connect
51
+      @bot.start
52
+   end
53
+
54
+   # (see Vrobot4::Server::Server#flags)
55
+   def flags
56
+      "L"
57
+   end
58
+
59
+   protected
60
+   # (see Vrobot4::Server::Server#load_permissions)
61
+   def load_permissions pinf
62
+      @mprm = {chan: {}, role: {}, glob: {}}
63
+      pinf.each do |pr|
64
+         mod = Vrobot4::Module.get_module_type(pr["module"])[:type]
65
+         if pr.key? "channel"
66
+            @mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod
67
+            @mprm[:chan][mod][pr["channel"].downcase] = true
68
+         elsif pr.key? "roles"
69
+            @mprm[:role][mod] = pr["roles"]
70
+         elsif pr.key? "enable"
71
+            @mprm[:glob][mod] = pr["enable"]
72
+         end
73
+      end if pinf
74
+   end
75
+
76
+   class ChannelPerms
77
+      def initialize() @cprm = {} end
78
+
79
+      # Returns true if the channel is enabled, false otherwise.
80
+      def [](chan) @cprm[chan.name] end
81
+
82
+      # Sets a channel's permission on/off.
83
+      def []=(name, set) @cprm[name] = set end
84
+   end
85
+   private_constant :ChannelPerms
86
+
87
+   # An IRC user.
88
+   class User < Vrobot4::Server::User
89
+      attr_reader :real # The Cinch::User instance.
90
+
91
+      # @param user [Cinch::User] the irc user
92
+      # @param chan [Cinch::Channel] the irc channel this object was made in
93
+      def initialize user, chan
94
+         @real = user
95
+         @name = user.nick
96
+            if user.oper?            then @roles = "Oohv"
97
+         elsif chan.opped?      user then @roles = "ohv"
98
+         elsif chan.half_opped? user then @roles = "hv"
99
+         elsif chan.voiced?     user then @roles = "v"
100
+         else                             @roles = "" end
101
+      end
102
+   end
103
+
104
+   # An IRC channel.
105
+   class Channel < Vrobot4::Server::Channel
106
+      attr_reader :real # The Cinch::Channel instance.
107
+
108
+      # @param chan [Cinch::Channel] the irc channel
109
+      def initialize chan
110
+         @real = chan
111
+         @name = chan.name.downcase
112
+      end
113
+   end
114
+end
115
+
116
+## EOF

Loading…
Cancel
Save