Beispiel #1
0
        internal static Message PrefixDispatch(Match m, string prefix, string channel, string text)
        {
            var message = new Message();

            var regexMessagePattern = "^:(.[^\\s]+)!(.[^\\s]+)@(.[^\\s]+)$";
            var regexMessage = new Regex(regexMessagePattern);
            Match match = regexMessage.Match(prefix);
            message = new Message();
            if(match.Success) {
                message.Nick = match.Groups[1].Value;
                message.Username = match.Groups[2].Value;
                message.Host = match.Groups[3].Value;
                message.Channel = channel;
                message.Date = DateTime.Now;
                message.MessageText = text;
            }
            return message;
        }
Beispiel #2
0
        /// <summary>
        /// Reading from stream
        /// </summary>
        private void ReadContext()
        {
            StreamReader sr = new StreamReader(stream);
            string line;
            try {
                Regex regex = null;
                while((line = sr.ReadLine()) != null) {
                    //TODO: Handle sutuation when nick is taken
                    if(line.StartsWith("PING")) {
                        string[] ping = line.Split(new char[] { ':' });
                        string pong = String.Format("PONG :{0}", ping[1]);
                        SendLine(pong);
                    } else {
                        regex = new Regex("((?=^:)(.[^\\s]+) (.[a-zA-Z]+|[0-9]{3,3}) (.[a-z,\\s]+)|(.[a-zA-Z]+|[0-9]{3,3}) (.+ )*):{0,1}(.*)");

                        Match m = regex.Match(line);
            #if DEBUG
                        Debug.WriteLine(line);
            #endif
                        if(m.Success) {
                            //Groups 3 and 5 are responsible for command
                            string command = !string.IsNullOrEmpty(m.Groups[3].Value) ? m.Groups[3].Value : (!string.IsNullOrEmpty(m.Groups[5].Value)) ? m.Groups[5].Value : null;
                            switch(command) {
                                case "PRIVMSG": // group 3 and 4 or 5 and 6
                                    {
                                        var prefix = m.Groups[2].Value;
                                        var channel = m.Groups[4].Value;
                                        var text = m.Groups[7].Value;
                                        var message = Helper.PrefixDispatch(m, prefix, channel, text);
                                        if(MessageArrived != null) {
                                            MessageArrived(new MessageEventArgs() {
                                                Msg = message
                                            });
                                        }
                                    }
                                    break;
                                case "JOIN"://channel name in group 7
                                    if(ChannelJoined != null) {
                                        ChannelJoined(new MessageEventArgs() {
                                            Channel = new Channel(m.Groups[7].Value) {
                                                //channel info
                                            },
                                            Msg = new Message() {
                                                //message info
                                            }
                                        });
                                    }
                                    break;
                                case "PART": {
                                        var prefix = m.Groups[2].Value;
                                        var channel = m.Groups[4].Value;
                                        var text = m.Groups[7].Value;
                                        var message = Helper.PrefixDispatch(m, prefix, channel, text);
                                        if(MessageArrived != null) {
                                            MessageArrived(new MessageEventArgs() {
                                                Msg = message
                                            });
                                        }
                                    }
                                    break;
                                case "MODE":
                                    break;
                                case "353": // nicks in channel
                                    break;
                                case "366":
                                case "NOTICE":
                                default:
                                    var msg = new Message();

                                    msg.MessageText = m.Groups[7].Value;
                                    if(MessageArrived != null) {
                                        MessageArrived(new MessageEventArgs() {
                                            Msg = msg
                                        });
                                    }

                                    break;
                            }
                        } else {

                        }
                    }
                }
            } catch(Exception e) {
                throw;
            } finally {
                //sr.Close();
            }
        }