sendmsg (Irc::Bot)

    # File lib/rbot/ircbot.rb, line 929
      def sendmsg(type, where, original_message, options={})
        opts = @default_send_options.merge(options)

        # For starters, set up appropriate queue channels and rings
        mchan = opts[:queue_channel]
        mring = opts[:queue_ring]
        if mchan
          chan = mchan
        else
          chan = where
        end
        if mring
          ring = mring
        else
          case where
          when User
            ring = 1
          else
            ring = 2
          end
        end

        multi_line = original_message.to_s.gsub(/[\r\n]+/, "\n")

        # if target is a channel with nocolor modes, strip colours
        if where.kind_of?(Channel) and where.mode.any?(*config['server.nocolor_modes'])
          multi_line.replace BasicUserMessage.strip_formatting(multi_line)
        end

        messages = Array.new
        case opts[:newlines]
        when :join
          messages << [multi_line.gsub("\n", opts[:join_with])]
        when :split
          multi_line.each_line { |line|
            line.chomp!
            next unless(line.size > 0)
            messages << line
          }
        else
          raise "Unknown :newlines option #{opts[:newlines]} while sending #{original_message.inspect}"
        end

        # The IRC protocol requires that each raw message must be not longer
        # than 512 characters. From this length with have to subtract the EOL
        # terminators (CR+LF) and the length of ":botnick!botuser@bothost "
        # that will be prepended by the server to all of our messages.

        # The maximum raw message length we can send is therefore 512 - 2 - 2
        # minus the length of our hostmask.

        max_len = 508 - myself.fullform.size

        # On servers that support IDENTIFY-MSG, we have to subtract 1, because messages
        # will have a + or - prepended
        if server.capabilities["identify-msg""identify-msg"]
          max_len -= 1
        end

        # When splitting the message, we'll be prefixing the following string:
        # (e.g. "PRIVMSG #rbot :")
        fixed = "#{type} #{where} :"

        # And this is what's left
        left = max_len - fixed.size

        truncate = opts[:truncate_text]
        truncate = @default_send_options[:truncate_text] if truncate.size > left
        truncate = "" if truncate.size > left

        all_lines = messages.map { |line|
          if line.size < left
            line
          else
            case opts[:overlong]
            when :split
              msg = line.dup
              sub_lines = Array.new
              begin
                sub_lines << msg.slice!(0, left)
                break if msg.empty?
                lastspace = sub_lines.last.rindex(opts[:split_at])
                if lastspace
                  msg.replace sub_lines.last.slice!(lastspace, sub_lines.last.size) + msg
                  msg.gsub!(/^#{opts[:split_at]}/, "") if opts[:purge_split]
                end
              end until msg.empty?
              sub_lines
            when :truncate
              line.slice(0, left - truncate.size) << truncate
            else
              raise "Unknown :overlong option #{opts[:overlong]} while sending #{original_message.inspect}"
            end
          end
        }.flatten

        if opts[:max_lines] > 0 and all_lines.length > opts[:max_lines]
          lines = all_lines[0...opts[:max_lines]]
          new_last = lines.last.slice(0, left - truncate.size) << truncate
          lines.last.replace(new_last)
        else
          lines = all_lines
        end

        lines.each { |line|
          sendq "#{fixed}#{line}", chan, ring
          delegate_sent(type, where, line)
        }
      end