Skip to content

Instantly share code, notes, and snippets.

@asterite
Created June 3, 2016 18:33
Show Gist options
  • Save asterite/237b4722ac9e414f9f4062c00df2ebf6 to your computer and use it in GitHub Desktop.
Save asterite/237b4722ac9e414f9f4062c00df2ebf6 to your computer and use it in GitHub Desktop.
diff --git a/framework/src/framework/bot.cr b/framework/src/framework/bot.cr
index 70dec75..aa574cb 100644
--- a/framework/src/framework/bot.cr
+++ b/framework/src/framework/bot.cr
@@ -14,11 +14,11 @@ require "./plugin"
module Framework
class Bot
- getter config
- getter! connection
+ getter config
+ getter! connection
property! user
- delegate channels, config
- delegate logger, config
+ delegate channels, config
+ delegate logger, config
def self.create
new.tap do |bot|
@@ -27,7 +27,7 @@ module Framework
end
private def initialize
- @config = Configuration.new
+ @config = Configuration.new
@filters = [] of Filter::Item
@started = false
@@ -35,7 +35,7 @@ module Framework
end
macro add_plugin(klass)
- config.add_plugin Framework::PluginContainer({{klass}}).new
+ config.add_plugin Framework::PluginContainer({{klass}}, {{klass}}::Config).new
end
def add_filter(filter : Filter::Item)
@@ -54,7 +54,7 @@ module Framework
channel.on_message do |message|
message = Message.new self, message
- event = Event.new(self, :message, message)
+ event = Event.new(self, :message, message)
config.plugins.each_value &.handle(event)
end
diff --git a/framework/src/framework/configuration.cr b/framework/src/framework/configuration.cr
index 31f3a74..fb7c0d7 100644
--- a/framework/src/framework/configuration.cr
+++ b/framework/src/framework/configuration.cr
@@ -35,7 +35,7 @@ module Framework
new false
end
- def initialize(channels : Array(String)|Bool)
+ def initialize(channels : Array(String) | Bool)
case channels
when Bool
@wants_channel_messsages = channels
@@ -95,14 +95,14 @@ module Framework
def to_json(io)
value = if @wants_channel_messsages
- if @channels.empty?
- nil
- else
- @channels
- end
- else
- false
- end
+ if @channels.empty?
+ nil
+ else
+ @channels
+ end
+ else
+ false
+ end
value.to_json io
end
@@ -120,7 +120,7 @@ module Framework
include Plugin
JSON.mapping({
- channels: {type: Framework::Configuration::Plugin::ChannelList, nilable: true, emit_null: true}
+ channels: {type: Framework::Configuration::Plugin::ChannelList, nilable: true, emit_null: true},
})
def initialize_empty
@@ -128,9 +128,9 @@ module Framework
end
end
- property! name
+ property! name : String
delegate listens_to?, channels!
- delegate wants?, channels!
+ delegate wants?, channels!
def channels!
@channels ||= ChannelList.default
@@ -144,18 +144,18 @@ module Framework
class Store
JSON.mapping({
server: {type: String},
- port: {type: Int32, nilable: true},
+ port: {type: Int32, nilable: true},
channels: {type: Array(String)},
nick: {type: String},
- user: {type: String, nilable: true},
- password: {type: String, nilable: true, emit_null: true},
- nickserv_regain: {type: Bool, nilable: true},
- realname: {type: String, nilable: true},
- ssl: {type: Bool, nilable: true},
- try_sasl: {type: Bool, nilable: true},
- log_level: {type: String, nilable: true},
- ignores: {type: Array(String), nilable: true},
- plugins: {type: Hash(String, JSON::Any), nilable: true}
+ user: {type: String, nilable: true},
+ password: {type: String, nilable: true, emit_null: true},
+ nickserv_regain: {type: Bool, nilable: true},
+ realname: {type: String, nilable: true},
+ ssl: {type: Bool, nilable: true},
+ try_sasl: {type: Bool, nilable: true},
+ log_level: {type: String, nilable: true},
+ ignores: {type: Array(String), nilable: true},
+ plugins: {type: Hash(String, JSON::Any), nilable: true},
}, true)
def self.load_plugins(config, json)
@@ -183,33 +183,33 @@ module Framework
end
def to_json(config : Configuration)
- self.port = config.port
- self.channels = config.channels
- self.user = config.user
- self.password = config.password
+ self.port = config.port
+ self.channels = config.channels
+ self.user = config.user
+ self.password = config.password
self.nickserv_regain = config.nickserv_regain?
- self.realname = config.realname
- self.ssl = config.ssl?
- self.try_sasl = config.try_sasl?
- self.log_level = config.log_level
- self.ignores = config.ignores
+ self.realname = config.realname
+ self.ssl = config.ssl?
+ self.try_sasl = config.try_sasl?
+ self.log_level = config.log_level
+ self.ignores = config.ignores
to_pretty_json
end
def restore(config)
- config.server = server
- config.port = port unless port.nil?
- config.channels = channels
- config.nick = nick
- config.user = user unless user.nil?
- config.password = password unless password.nil?
+ config.server = server
+ config.port = port unless port.nil?
+ config.channels = channels
+ config.nick = nick
+ config.user = user unless user.nil?
+ config.password = password unless password.nil?
config.nickserv_regain = nickserv_regain unless nickserv_regain.nil?
- config.realname = realname unless realname.nil?
- config.ssl = ssl unless ssl.nil?
- config.try_sasl = try_sasl unless try_sasl.nil?
- config.log_level = log_level unless log_level.nil?
- config.ignores = ignores unless ignores.nil?
+ config.realname = realname unless realname.nil?
+ config.ssl = ssl unless ssl.nil?
+ config.try_sasl = try_sasl unless try_sasl.nil?
+ config.log_level = log_level unless log_level.nil?
+ config.ignores = ignores unless ignores.nil?
end
end
@@ -218,38 +218,41 @@ module Framework
"info" => Logger::Severity::INFO,
"warn" => Logger::Severity::WARN,
"error" => Logger::Severity::ERROR,
- "fatal" => Logger::Severity::FATAL
+ "fatal" => Logger::Severity::FATAL,
}
- property! server
- property port
- property channels
+ property! server : String?
+ property port : Int32?
+ property channels
property! nick
- property! user
- property password
- property? nickserv_regain
- property! realname
- property? ssl
- property? try_sasl
- property log_level
- property! ignores
- getter logger
- getter plugins
+ property! user : String?
+ property password : String?
+ property? nickserv_regain : Bool?
+ property! realname : String?
+ property? ssl : Bool?
+ property? try_sasl : Bool?
+ property log_level : String?
+ property! ignores : Array(String)?
+ getter logger
+ getter plugins
+
+ @config_file : String?
+ @store : Store?
def initialize
- @plugins = Hash(String, PluginContainer::Workaround).new
+ @plugins = Hash(String, PluginContainer::Workaround).new
@channels = [] of String
- @logger = Logger.new(STDOUT)
+ @logger = Logger.new(STDOUT)
- @nick = "CeBot"
- @user = "cebot"
- @password = nil
+ @nick = "CeBot"
+ @user = "cebot"
+ @password = nil
@nickserv_regain = false
- @realname = "CeBot"
- @ssl = false
- @try_sasl = false
- @log_level = "info"
- @ignores = [] of String
+ @realname = "CeBot"
+ @ssl = false
+ @try_sasl = false
+ @log_level = "info"
+ @ignores = [] of String
set_log_level
end
@@ -301,15 +304,15 @@ module Framework
load if @config_file
IRC::Connection.build do |config|
- config.server = server
- config.port = port
- config.nick = nick
- config.user = user
+ config.server = server
+ config.port = port
+ config.nick = nick
+ config.user = user
config.password = password
config.realname = realname
- config.ssl = ssl?
+ config.ssl = ssl?
config.try_sasl = try_sasl?
- config.logger = logger
+ config.logger = logger
end
end
diff --git a/framework/src/framework/event.cr b/framework/src/framework/event.cr
index 8090b21..dd88e96 100644
--- a/framework/src/framework/event.cr
+++ b/framework/src/framework/event.cr
@@ -7,9 +7,9 @@ module Framework
class Event
getter context
getter type
- getter! sender
- getter! channel
- getter! message
+ getter! sender : User
+ getter! channel : Channel
+ getter! message : Message
def initialize(@context : Bot, @type : Symbol, @message : Message)
@sender = message.sender
diff --git a/framework/src/framework/filter.cr b/framework/src/framework/filter.cr
index 90cd69f..1eaf4ea 100644
--- a/framework/src/framework/filter.cr
+++ b/framework/src/framework/filter.cr
@@ -9,7 +9,7 @@ module Framework
class NickFilter
include Filter
- def initialize(@config)
+ def initialize(@config : Configuration)
end
def call(event)
diff --git a/framework/src/framework/json_store.cr b/framework/src/framework/json_store.cr
index d23a129..b6522ce 100644
--- a/framework/src/framework/json_store.cr
+++ b/framework/src/framework/json_store.cr
@@ -4,7 +4,7 @@ require "thread/read_write_lock"
module Framework
class JsonStore(K, V)
- def initialize(@path)
+ def initialize(@path : String)
@lock = ReadWriteLock.new
@data = Hash(K, V).new
load
diff --git a/framework/src/framework/plugin.cr b/framework/src/framework/plugin.cr
index 0bd0d20..9c6f871 100644
--- a/framework/src/framework/plugin.cr
+++ b/framework/src/framework/plugin.cr
@@ -9,12 +9,10 @@ module Framework
module Plugin
macro config(properties)
{% for key, value in properties %}
- {% properties[key] = {type: value} unless value.is_a?(HashLiteral) %}
+ {% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
{% end %}
class Config
- include Framework::Configuration::Plugin
-
JSON.mapping({
:channels => {type: Framework::Configuration::Plugin::ChannelList, nilable: true, emit_null: true},
{% for key, value in properties %}
@@ -29,15 +27,19 @@ module Framework
{% end %}
end
end
-
- def self.config_class
- Config
- end
end
macro included
+ class Config
+ include Framework::Configuration::Plugin
+
+ JSON.mapping({
+ channels: {type: Framework::Configuration::Plugin::ChannelList, nilable: true, emit_null: true},
+ })
+ end
+
def self.config_class
- Framework::Configuration::Plugin::Default
+ Config
end
@@matchers = [] of Regex
@@ -88,7 +90,7 @@ module Framework
Timer.new seconds, 1, &block
end
- def every(seconds, limit=nil, &block)
+ def every(seconds, limit = nil, &block)
Timer.new seconds, limit, &block
end
diff --git a/framework/src/framework/plugin_container.cr b/framework/src/framework/plugin_container.cr
index c366cb3..c569a7c 100644
--- a/framework/src/framework/plugin_container.cr
+++ b/framework/src/framework/plugin_container.cr
@@ -3,11 +3,11 @@ require "json"
require "./plugin"
module Framework
- class PluginContainer(T)
+ class PluginContainer(T, C)
module Workaround; end
include Workaround
- getter config
+ getter config : C?
delegate channels, config
delegate wants?, config
diff --git a/irc/src/irc/connection.cr b/irc/src/irc/connection.cr
index abec039..e0769d9 100644
--- a/irc/src/irc/connection.cr
+++ b/irc/src/irc/connection.cr
@@ -17,25 +17,25 @@ require "./workers"
module IRC
class Connection
class Config
- property! server
- property port
- property nick
- property user
- property! password
- property realname
- property! ssl
- property! try_sasl
- setter logger
+ property! server : String?
+ property port
+ property nick
+ property user
+ property! password : String?
+ property realname
+ property! ssl : Bool?
+ property! try_sasl : Bool?
+ setter logger
private def initialize
- @port = 6667
- @nick = "Crystal"
- @user = "crystal"
- @password = nil
- @realname = "Crystal IRC"
- @ssl = false
- @try_sasl = false
- @logger = nil
+ @port = 6667
+ @nick = "Crystal"
+ @user = "crystal"
+ @password = nil
+ @realname = "Crystal IRC"
+ @ssl = false
+ @try_sasl = false
+ @logger = nil
end
def self.new(server : String)
@@ -79,12 +79,12 @@ module IRC
end
def initialize(@config : Config)
- @send_queue = ::Channel(String).new(64)
- @users = UserManager.new
- @channels = Repository(String, Channel).new
- @processor = Processor.new(logger)
- @network = Network.new
- @connected = false
+ @send_queue = ::Channel(String).new(64)
+ @users = UserManager.new
+ @channels = Repository(String, Channel).new
+ @processor = Processor.new(logger)
+ @network = Network.new
+ @connected = false
@exit_channel = ::Channel(Int32).new
@users.track Mask.parse(@config.nick) # Track self with pseudo mask
@@ -179,9 +179,9 @@ module IRC
logger.info "Connecting to #{config.server}:#{config.port}#{" (SSL enabled)" if config.ssl?}"
socket = TCPSocket.new config.server, config.port
- socket.read_timeout = 300
+ socket.read_timeout = 300
socket.write_timeout = 5
- socket.keepalive = true
+ socket.keepalive = true
socket = OpenSSL::SSL::Socket.new socket if config.ssl?
@@ -203,7 +203,7 @@ module IRC
end
when "ACK"
network.account_notify = true if cap.parameters.last == "account-notify"
- network.extended_join = true if cap.parameters.last == "extended-join"
+ network.extended_join = true if cap.parameters.last == "extended-join"
if cap.parameters.last == "sasl"
send Message::AUTHENTICATE, "PLAIN"
@@ -218,10 +218,9 @@ module IRC
send Message::AUTHENTICATE, Base64.strict_encode("#{config.nick}\0#{config.nick}\0#{config.password}")
end
- on(Message::RPL_LOGGEDIN, Message::RPL_LOGGEDOUT, Message::ERR_NICKLOCKED,
- Message::RPL_SASLSUCCESS, Message::ERR_SASLFAIL, Message::ERR_SASLTOOLONG,
- Message::RPL_SASL_ABORTED, Message::ERR_SASLALREADY) do |message|
-
+ on(Message::RPL_LOGGEDIN, Message::RPL_LOGGEDOUT, Message::ERR_NICKLOCKED,
+ Message::RPL_SASLSUCCESS, Message::ERR_SASLFAIL, Message::ERR_SASLTOOLONG,
+ Message::RPL_SASL_ABORTED, Message::ERR_SASLALREADY) do |message|
if {Message::RPL_LOGGEDIN, Message::RPL_SASLSUCCESS, Message::ERR_SASLALREADY}.includes? message.type
logger.info "SASL authentication succeeded"
else
@@ -289,14 +288,14 @@ module IRC
logger.info "Connected"
end
- def quit(message="Crystal IRC")
+ def quit(message = "Crystal IRC")
send Message::QUIT, message
@processor.handle_others
stop_workers
exit
end
- def exit(code=0)
+ def exit(code = 0)
@exit_channel.send code
@exit_channel.close
@processor.handle_others
diff --git a/irc/src/irc/modes.cr b/irc/src/irc/modes.cr
index 6f78d72..1bda871 100644
--- a/irc/src/irc/modes.cr
+++ b/irc/src/irc/modes.cr
@@ -3,9 +3,9 @@ module IRC
module Parser
def self.parse(modes)
parameters = modes.split(" ")
- modes = seperate_flags parameters.shift? || ""
+ modes = seperate_flags parameters.shift? || ""
- (modes.size-parameters.size).times do
+ (modes.size - parameters.size).times do
mode = modes.shift
modifier, flag = mode
@@ -36,12 +36,12 @@ module IRC
end
end
- record Flag, flag, parameter
+ record Flag, flag : Char, parameter : String?
include Enumerable(Flag)
- def initialize(modes="")
- @plain_flags = Set(Flag).new
+ def initialize(modes = "")
+ @plain_flags = Set(Flag).new
@parameterized_flags = Set(Flag).new
parse modes
end
@@ -57,10 +57,10 @@ module IRC
end
def get(flag : Char)
- @parameterized_flags.find {|item| item.flag == flag }.try(&.parameter)
+ @parameterized_flags.find { |item| item.flag == flag }.try(&.parameter)
end
- def set(flag : Char, parameter=nil)
+ def set(flag : Char, parameter = nil)
mode = Flag.new(flag, parameter)
if parameter
@@ -70,7 +70,7 @@ module IRC
end
end
- def unset(flag : Char, parameter=nil)
+ def unset(flag : Char, parameter = nil)
mode = Flag.new(flag, parameter)
if parameter
@@ -82,13 +82,13 @@ module IRC
# pass false as parameter to look only at the flag but only in the
# parameterized flags
- def set?(flag : Char, parameter=nil)
+ def set?(flag : Char, parameter = nil)
mode = Flag.new(flag, parameter)
if parameter
@parameterized_flags.includes? mode
elsif parameter == false
- @parameterized_flags.any? &.flag==(flag)
+ @parameterized_flags.any? &.flag == (flag)
else
@plain_flags.includes? mode
end
diff --git a/irc/src/irc/user.cr b/irc/src/irc/user.cr
index a6a2829..544e3be 100644
--- a/irc/src/irc/user.cr
+++ b/irc/src/irc/user.cr
@@ -36,8 +36,8 @@ module IRC
# +x - Gives the user Hidden Hostname (security)
# +Z, +z - Is connected via SSL (cannot be set or unset).
class User
- property authname
- property realname
+ property authname : String?
+ property realname : String?
getter channels
getter mask
getter modes
@@ -45,11 +45,11 @@ module IRC
delegate user, mask
delegate host, mask
- def initialize(@mask)
+ def initialize(@mask : Mask)
@authname = nil
@realname = nil
@channels = Repository(String, Membership).new
- @modes = Modes.new
+ @modes = Modes.new
end
def name
@@ -65,12 +65,12 @@ module IRC
end
{% for item in [{['A'], "server_admin"}, {['a'], "service_admin"}, {['B'], "bot"},
- {['C'], "co_admin"}, {['D', 'd'], "deaf"}, {['h'], "available_for_help"},
- {['i'], "invisible"}, {['N'], "network_admin"}, {['o'], "operator"},
- {['O'], "local_operator"}, {['r'], "registered"}, {['S'], "service"},
- {['w'], "receive_wallops"}, {['z', 'Z'], "secure"},] %}
+ {['C'], "co_admin"}, {['D', 'd'], "deaf"}, {['h'], "available_for_help"},
+ {['i'], "invisible"}, {['N'], "network_admin"}, {['o'], "operator"},
+ {['O'], "local_operator"}, {['r'], "registered"}, {['S'], "service"},
+ {['w'], "receive_wallops"}, {['z', 'Z'], "secure"}] %}
{% flags = item[0] %}
- {% name = item[1] %}
+ {% name = item[1] %}
def {{name.id}}?
{% for flag in flags %}
@modes.set?({{flag}}) ||
diff --git a/irc/src/irc/workers.cr b/irc/src/irc/workers.cr
index 8e77c1e..6952cd3 100644
--- a/irc/src/irc/workers.cr
+++ b/irc/src/irc/workers.cr
@@ -98,7 +98,7 @@ module IRC
class Processor
getter channel
getter handlers
- private getter logger
+ private getter logger : Logger
def initialize(@logger)
@channel = ::Channel(Message).new(64)
diff --git a/thread/src/thread/lib_pthread.cr b/thread/src/thread/lib_pthread.cr
index 1a1ba40..451a940 100644
--- a/thread/src/thread/lib_pthread.cr
+++ b/thread/src/thread/lib_pthread.cr
@@ -1,19 +1,17 @@
-lib LibPThread
- EDEADLK = 35
+lib LibC
+ # MUTEX_RECURSIVE = 1
+ # MUTEX_ERRORCHECK = 2
- MUTEX_RECURSIVE = 1
- MUTEX_ERRORCHECK = 2
+ fun pthread_mutexattr_init(mutex_attr : PthreadMutexattrT*) : Int32
+ fun pthread_mutexattr_settype(mutex_attr : PthreadMutexattrT*, type : Int32) : Int32
- fun mutexattr_init = pthread_mutexattr_init(mutex_attr : MutexAttr*) : Int32
- fun mutexattr_settype = pthread_mutexattr_settype(mutex_attr : MutexAttr*, type : Int32) : Int32
-
- fun mutex_init_fixed = pthread_mutex_init(mutex : Mutex*, mutex_attr : MutexAttr*) : Int32
+ fun pthread_mutex_init(mutex : PthreadMutexT*, mutex_attr : PthreadMutexattrT*) : Int32
type Rwlock = Int64[8]
type RwlockAttr = Void*
- fun rwlock_init = pthread_rwlock_init(lock : Rwlock*, lock_attr : RwlockAttr*) : Int32
- fun rwlock_rdlock = pthread_rwlock_rdlock(lock : Rwlock*) : Int32
- fun rwlock_wrlock = pthread_rwlock_wrlock(lock : Rwlock*) : Int32
- fun rwlock_unlock = pthread_rwlock_unlock(lock : Rwlock*) : Int32
+ fun pthread_rwlock_init(lock : Rwlock*, lock_attr : RwlockAttr*) : Int32
+ fun pthread_rwlock_rdlock(lock : Rwlock*) : Int32
+ fun pthread_rwlock_wrlock(lock : Rwlock*) : Int32
+ fun pthread_rwlock_unlock(lock : Rwlock*) : Int32
end
diff --git a/thread/src/thread/read_write_lock.cr b/thread/src/thread/read_write_lock.cr
index 63e7c02..f1ca706 100644
--- a/thread/src/thread/read_write_lock.cr
+++ b/thread/src/thread/read_write_lock.cr
@@ -2,25 +2,25 @@ require "./lib_pthread"
class ReadWriteLock
def initialize
- LibPThread.rwlock_init(out @lock, nil)
+ LibC.pthread_rwlock_init(out @lock, nil)
end
def read_lock
- LibPThread.rwlock_rdlock(self)
+ LibC.pthread_rwlock_rdlock(self)
yield
ensure
unlock
end
def write_lock
- LibPThread.rwlock_wrlock(self)
+ LibC.pthread_rwlock_wrlock(self)
yield
ensure
unlock
end
def unlock
- LibPThread.rwlock_unlock(self)
+ LibC.pthread_rwlock_unlock(self)
end
def to_unsafe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment