Java tutorial
/* * 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.regex.Matcher; import java.util.regex.Pattern; import me.emily.irc.io.Channel; import me.emily.irc.io.ServerConnection; import me.emily.irc.io.User; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; /** * @author Emily Soldal * @created 6 Feb 2012 */ public class UserModeParser implements Parser { private static final Splitter splitter = Splitter.on(" ").omitEmptyStrings(); private static final Pattern pattern = Pattern.compile("^:(\\S+) MODE ([^#]\\S*) :([-+]\\S+)(\\s\\S+)?$"); // :LezBot!~bot@li265-227.members.linode.com MODE LezBot :+i @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), connection); String params = matcher.group(4); params = params == null ? "" : params; Iterable<String> parameters = splitter.split(params); int p = 0; boolean add; for (char c : matcher.group(3).toCharArray()) { switch (c) { case '+': add = true; break; case '-': add = false; break; default: if (takesParams(c, connection)) { String param = Iterables.get(parameters, p); p++; } else { } } } return true; } return false; } private boolean takesParams(char c, ServerConnection connection) { return false; } }