Esempio n. 1
0
        /// <summary>
        /// Creates a new IRC client.
        /// </summary>
        /// <param name="host">Hostname.</param>
        /// <param name="port">Port.</param>
        /// <param name="user">User to connect with.</param>
        public Client(string host, int port, User user)
        {
            Channels = new List<Channel>();
            Users = new List<User> {user};
            Host = host;
            Port = port;
            User = user;

            tcpClient = new TcpClient();
            networkThread = new Thread(ProcessMessages);

            // Register neccessary internal events
            Connect += () =>
            {
                // Send IRCv3 capabilities.
                Send(new CapabilityListSupportedMessage());
                Send(new CapabilityRequestMessage(IRC.Capability.MultiPrefix, IRC.Capability.AwayNotify));
                Send(new CapabilityEndMessage());
            };
            Ping += message => Send(new PongMessage(message.Extra));
            // Request channel modes on channel join.
            ChannelJoin += message => Send(new Messages.Send.ChannelModeMessage(message.Channel));
        }
Esempio n. 2
0
 internal void OnUserRemove(User user) => UserRemove?.Invoke(user);
Esempio n. 3
0
 internal void OnUserAdd(User user) => UserAdd?.Invoke(user);
Esempio n. 4
0
 public void RemoveUser(User user)
 {
     user.Channels.Remove(this);
     Client.Users.Remove(user);
     OnUserRemove(user);
 }
Esempio n. 5
0
 /// <summary>
 /// Adds a user to the channel and fires the UserAdd event.
 /// </summary>
 public void AddUser(User user)
 {
     if (!user.Channels.Contains(this))
     {
         user.Channels.Add(this);
         if (!Client.Users.Contains(user))
             Client.Users.Add(user);
         OnUserAdd(user);
     }
 }
Esempio n. 6
0
 public UiUser(User user, Color color)
 {
     User = user;
     Color = color;
 }
Esempio n. 7
0
 public UiUser(User user)
 {
     User = user;
     Color = Colors.Black;
 }
Esempio n. 8
0
 internal void OnRankChange(User user, string channel, UserRank rank) => RankChange?.Invoke(user, channel, rank);
Esempio n. 9
0
 internal void OnAwayChange(User user, bool away) => AwayChange?.Invoke(user, away);
Esempio n. 10
0
        /// <summary>
        /// Returns the user that best matches the mask provided.
        /// If any fields are blank in the user, they will be filled in with data from the mask.
        /// If the user is not found, they will be created.
        /// </summary>
        public User UserFromMask(string mask)
        {
            var match = User.MaskRegex.Match(mask);
            if (!match.Success) return null;

            var user = UserFromNick(match.Groups[1].Value);
            if (user == null)
            {
                user = new User(mask);
                Users.Add(user);
            }
            user.Nick = match.Groups[1].Value;
            user.Ident = match.Groups[2].Value;
            user.Host = match.Groups[3].Value;
            return user;
        }
Esempio n. 11
0
        public Network(string host, int port, User userirc, string[] initialChannels)
        {
            Name = host; // By default, name is host.
            Users = new ObservableCollection<UiUser>();
            DisplayUsers = new ObservableCollection<UiUser>();
            Channels = new ObservableCollection<UiChannel>();

            Me = new UiUser(userirc, ViewModelLocator.Main.GenColor());
            Users.Add(Me);

            // Create IRC client and handle connection.
            Client = new Client(host, port, userirc);
            Client.IRCMessage += message => Console.WriteLine(message.Text);
            Client.Connect += () =>
            {
                foreach (var chan in initialChannels)
                    Client.Send(new JoinMessage(chan));
            };
            Client.ReplyISupport += message =>
            {
                // Use the NETWORK= parameter in 005 to set the server name.
                string networkName;
                if (message.TryGetParameter("Network", out networkName))
                    Name = networkName;
            };

            // Update user list on certain events.
            Client.Nick += message => { SortUsers(); };
            Client.RankChange += (user, channel, rank) => { SortUsers(); };
            Client.AwayChange += (user, away) => { SortUsers(); };

            // Subscribe to events when a channel is joined.
            Client.ChannelJoin += message =>
            {
                var channelJoined = new UiChannel(message.Channel, this);
                if (ViewModelLocator.Main.Channel == null)
                    ViewModelLocator.Main.Channel = channelJoined;
                DispatcherHelper.CheckBeginInvokeOnUI(() => Channels.Add(channelJoined));
                message.Channel.Message += privateMessage => //Subscribe to the joined channel's messages
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(
                        () =>
                        {
                            var chan = Channels.FirstOrDefault(c => c.Channel == message.Channel);
                            chan?.AddLine(privateMessage);
                        });
                };
                // Show system messages (will be moved to cause/effect system eventually)
                message.Channel.UserJoin +=
                    joinMessage =>
                    {
                        AddSystemLine(
                            $"{joinMessage.User.Nick} ({joinMessage.User.Ident}@{joinMessage.User.Host}) has joined.", channelJoined.Channel);
                    };
                message.Channel.UserPart +=
                    partMessage =>
                    {
                        var reason = string.IsNullOrEmpty(partMessage.Reason) ? string.Empty : $"({partMessage.Reason})";
                        AddSystemLine($"{partMessage.User.Nick} has left. {reason}", channelJoined.Channel);
                    };

                message.Channel.UserAdd += user =>
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        //Don't add duplicate users to the master list.
                        if (Users.All(u => u.User != user))
                        {
                            Users.Add(new UiUser(user, ViewModelLocator.Main.GenColor()));
                            SortUsers();
                        }
                    });
                };

                message.Channel.UserRemove += user =>
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        Users.Remove(Users.FirstOrDefault(x => x.User == user));
                        SortUsers();
                    });
                };
            };
            // Show system messages (will be moved to cause/effect system eventually)
            Client.Quit +=
                message =>
                {
                    var reason = string.IsNullOrEmpty(message.Reason) ? string.Empty : $"({message.Reason})";
                    AddSystemLine($"{message.User.Nick} has quit. {reason}", message.User.Channels.ToArray());
                };
            Client.Nick += message =>
            {
                AddSystemLine($"{message.OldNick} is now known as {message.Nick}.", message.User.Channels.ToArray());
            };
            //irc.ReplyMOTDEnd += delegate(MOTDEndMessage message) { Text += message.MOTD + Environment.NewLine; };
            Client.Start();
        }
Esempio n. 12
0
 public UiUser GetUser(User user)
 {
     return Users.FirstOrDefault(x => x.User == user);
 }