Module: IRC::Server::Modules::Base::Utils

Defined in:
lib/failirc/server/modules/Base.rb

Defined Under Namespace

Modules: Channel, Client, User

Class Method Summary collapse

Class Method Details

.checkFlag(thing, type) ⇒ Object



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/failirc/server/modules/Base.rb', line 628

def self.checkFlag (thing, type)
    # servers can do everything
    if thing.is_a?(IRC::Server)
        return true
    end

    result = thing.modes[type]

    if !result && thing.is_a?(Server::User)
        result = thing.client.modes[type]
    end

    if result.nil?
        result = false
    end

    return result
end

.dispatchMessage(kind, from, to, message, level = nil) ⇒ Object



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/failirc/server/modules/Base.rb', line 647

def self.dispatchMessage (kind, from, to, message, level=nil)
    if from.is_a?(Server::User)
        from = from.client
    end

    if match = message.match(/^\x01([^ ]*)( (.*?))?\x01$/)
        from.server.dispatcher.execute :ctcp, :input, kind, ref{:from}, ref{:to}, match[1], match[3], level
    else
        if kind == :notice
            from.server.dispatcher.execute :notice, :input, ref{:from}, ref{:to}, message, level
        elsif kind == :message
             from.server.dispatcher.execute :message, :input, ref{:from}, ref{:to}, message
        end
    end
end

.escapeMessage(string) ⇒ Object



663
664
665
# File 'lib/failirc/server/modules/Base.rb', line 663

def self.escapeMessage (string)
    string.inspect.gsub(/\\#/, '#').gsub(/\\'/, "'")
end

.motd(user) ⇒ Object

This method sends the MOTD 80 chars per line.



567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/failirc/server/modules/Base.rb', line 567

def self.motd (user)
    user.send :numeric, RPL_MOTDSTART
    
    offset = 0
    motd   = user.server.config.elements['config/server/motd'].text.strip
    
    while line = motd[offset, 80]
        user.send :numeric, RPL_MOTD, line
        offset += 80
    end
    
    user.send :numeric, RPL_ENDOFMOTD
end

.registration(thing) ⇒ Object

This method does some checks trying to register the connection, various checks for nick collisions and such.



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/failirc/server/modules/Base.rb', line 506

def self.registration (thing)
    if !thing.modes[:registered]
        # additional check for nick collisions
        if thing.nick
            if (thing.server.data[:nicks][thing.nick] && thing.server.data[:nicks][thing.nick] != thing) || thing.server.clients[thing.nick]
                if thing.modes[:__warned] != thing.nick
                    thing.send :numeric, ERR_NICKNAMEINUSE, thing.nick
                    thing.modes[:__warned] = thing.nick
                end

                return
            end

            thing.server.data[:nicks][thing.nick] = thing
        end

        # if the client isn't registered but has all the needed attributes, register it
        if thing.user && thing.nick
            if thing.listen.attributes['password'] && thing.listen.attributes['password'] != thing.password
                return false
            end

            thing.modes[:registered] = true
    
            # clean the temporary hash value and use the nick as key
            thing.server.clients.delete(thing.socket)
            thing.server.clients[thing.nick] = thing

            thing.server.data[:nicks].delete(thing.nick)
            thing.modes.delete(:__warned)
    
            thing.server.dispatcher.execute(:registration, thing)
    
            thing.send :numeric, RPL_WELCOME, thing
            thing.send :numeric, RPL_HOSTEDBY, thing
            thing.send :numeric, RPL_SERVCREATEDON
            thing.send :numeric, RPL_SERVINFO, {
                :user    => Base.supportedModes[:client].join(''),
                :channel => Base.supportedModes[:channel].join(''),
            }

            supported = String.new

            Base.support.each {|key, value|
                if value != true
                    supported << " #{key}=#{value}"
                else
                    supported << " #{key}"
                end
            }

            supported = supported[1, supported.length]

            thing.send :numeric, RPL_ISUPPORT, supported
    
            motd(thing)
        end
    end
end

.setFlags(thing, type, value, inherited = false, forceFalse = false) ⇒ Object

This method assigns flags recursively using groups of flags



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/failirc/server/modules/Base.rb', line 582

def self.setFlags (thing, type, value, inherited=false, forceFalse=false)
    if Base.modes[:groups][type]
        main = Base.modes[:groups]
    else
        if thing.is_a?(Server::Channel)
            main = Base.modes[:channel]
        elsif thing.is_a?(Server::User)
            main = Base.modes[:user]
        elsif thing.is_a?(Server::Client)
            main = Base.modes[:client]
        else
            raise 'What sould I do?'
        end
    end

    if !inherited
        if value == false
            thing.modes.delete(type)
        else
            thing.modes[type] = value
        end
    end

    if !(modes = main[type])
        return
    end

    if !modes.is_a?(Array)
        modes = [modes]
    end

    modes.each {|mode|
        if (main[mode] || Base.modes[:groups][mode]) && !thing.modes.has_key?(mode)
            self.setFlags(thing, mode, value, !forceFalse)
        else
            if value == false
                if !main.has_key?(mode)
                    thing.modes.delete(mode)
                end
            else
                thing.modes[mode] = value
            end
        end
    }
end