Module: ModSpox::Helpers

Defined in:
lib/mod_spox/Helpers.rb

Class Method Summary collapse

Class Method Details

.find_model(string, create = true) ⇒ Object

string

name of target

Locates target model and returns it. String can be a nick or channel name. If the string given does not match the required pattern for a channel or nick, the string is returned.



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
# File 'lib/mod_spox/Helpers.rb', line 62

def Helpers.find_model(string, create=true)
    @@channel_cache = {} unless Helpers.class_variable_defined?(:@@channel_cache)
    @@nick_cache = {} unless Helpers.class_variable_defined?(:@@nick_cache)
    if(string =~ /^[A-Za-z\|\\\{\}\[\]\^\`~\_\-]+[A-Za-z0-9\|\\\{\}\[\]\^\`~\_\-]*$/)
        Logger.log("Model: #{string} -> Nick")
        nick = nil
        if(@@nick_cache.has_key?(string.to_sym))
            begin
                nick = Models::Nick[@@nick_cache[string.to_sym]]
                Logger.log("Handler cache hit for nick: #{string}", 30)
            rescue Object => boom
                Logger.log("Failed to grab cached nick: #{boom}")
            end
        end
        unless(nick)
            nick = Models::Nick.locate(string, create)
            @@nick_cache[string.to_sym] = nick.pk if nick.is_a?(Models::Nick)
            Logger.log("Nick was retrieved from database")
        end
        return nick
    elsif(string =~ /^[&#+!]/)
        Logger.log("Model: #{string} -> Channel")
        if(@@channel_cache.has_key?(string.to_sym))
            begin
                channel = Models::Channel[@@channel_cache[string.to_sym]]
                Logger.log("Handler cache hit for channel: #{string}", 30)
            rescue Object => boom
                Logger.log("Failed to grab cached channel: #{boom}")
            end
        end
        unless(channel)
            channel = Models::Channel.locate(string, create)
            @@channel_cache[string.to_sym] = channel.pk if channel.is_a?(Models::Channel)
            Logger.log("Channel was retrieved from database")
        end
        return channel
    elsif(model = Models::Server.filter(:host => string, :connected => true).first)
        Logger.log("Model: #{string} -> Server")
        return model
    else
        Logger.log("FAIL Model: #{string} -> No match")
        return string
    end
end

.format_seconds(secs) ⇒ Object

secs

number of seconds

Converts seconds into a human readable string



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mod_spox/Helpers.rb', line 7

def Helpers.format_seconds(secs)
    str = []
    d = (secs / 86400).to_i
    secs = secs % 86400
    h = (secs / 3600).to_i
    secs = secs % 3600
    m = (secs / 60).to_i
    secs = secs % 60
    {:day => d, :hour => h, :minute => m, :second => secs}.each_pair do |type, value|
        if(value > 0)
            str << "#{value} #{type}#{value == 1 ? '':'s'}"
        end
    end
    return str.join(' ')
end

.safe_exec(command, timeout = 10) ⇒ Object

command

command to execute

timeout

maximum number of seconds to run

Execute a system command (use with care)



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mod_spox/Helpers.rb', line 26

def Helpers.safe_exec(command, timeout=10)
    begin
        Timeout::timeout(timeout) do
            result = `#{command}`
        end
    rescue Timeout::Error => boom
        Logger.log("Command execution exceeded allowed time (command: #{command} | timeout: #{timeout})")
    rescue Object => boom
        Logger.log("Command generated an exception (command: #{command} | error: #{boom})")
    end
end

.tinyurl(url) ⇒ Object

url

URL to shorten

Gets a tinyurl for given URL



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mod_spox/Helpers.rb', line 40

def Helpers.tinyurl(url)
    begin
        connection = Net::HTTP.new('tinyurl.com', 80)
        resp, data = connection.get("/create.php?url=#{url}", nil)
        if(resp.code !~ /^200$/)
            raise "Failed to make the URL small."
        end
        data.gsub!(/[\n\r]/, '')
        if(data =~ /<input type=hidden name=tinyurl value="(.+?)">/)
            return $1
        else
            raise "Failed to locate the small URL."
        end
    rescue Object => boom
        raise "Failed to process URL. #{boom}"
    end
end