me.emily.irc.protocol.parser.ChannelModeParser.java Source code

Java tutorial

Introduction

Here is the source code for me.emily.irc.protocol.parser.ChannelModeParser.java

Source

/*
 * Copyright (C) 2012 Emily Soldal
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.emily.irc.protocol.parser;

import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import me.emily.irc.event.events.ChannelModeEvent;
import me.emily.irc.io.Channel;
import me.emily.irc.io.ServerConnection;
import me.emily.irc.io.User;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

/**
 * @author Emily Soldal
 * @created 13 Feb 2012
 */
public class ChannelModeParser implements Parser {
    private static final Logger log = LoggerFactory.getLogger(ChannelModeParser.class);

    private static final Splitter splitter = Splitter.on(" ").omitEmptyStrings();
    private static final Pattern pattern = Pattern.compile("^:(\\S+) MODE (#\\S+) ([-+]\\S+)(\\s\\S+)*$");

    // :ChanServ!ChanServ@services. MODE #google+platform +v SpecialEmily
    @Override
    public boolean recieve(String message, ServerConnection connection) {

        Matcher matcher = pattern.matcher(message);

        if (matcher.matches()) {

            Channel channel = connection.getChannel(matcher.group(2));
            User user = User.parse(matcher.group(1));

            ModeState state = null;

            String params = matcher.group(4);

            Iterator<String> iterator;
            if (params == null || "".equals(params.trim())) {
                iterator = Lists.<String>newArrayList().iterator();
            }
            iterator = splitter.split(params).iterator();

            for (String c : Splitter.fixedLength(1).split(matcher.group(3))) {
                if (c.equals("+")) {
                    state = ModeState.added;
                    continue;
                }
                if (c.equals("-")) {
                    state = ModeState.removed;
                    continue;
                }

                if (iterator.hasNext()) {

                    String next = iterator.next();
                    log.info(state + " " + c + " " + next);
                    connection.post(new ChannelModeEvent(user, channel, connection, state, c, next));
                } else {
                    log.info(state + " " + c);
                    connection.post(new ChannelModeEvent(user, channel, connection, state, c, ""));
                }
            }

            return true;
        }

        return false;
    }
}