Example #1
0
        // LINKS
        public void HandleLink(Link _link)
        {
            switch (_link.Type)
            {
            case LinkType.Url:
            {
                var link = _link.Value as string;
                try
                {
                    if (link.StartsWith("http://") || link.StartsWith("https://") ||
                        MessageBox.Show($"The link \"{link}\" will be opened in an external application.", "open link", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        Process.Start(link);
                    }
                }
                catch { }
            }
            break;

            case LinkType.CloseCurrentSplit:
                App.MainForm.RemoveSelectedSplit();
                break;

            case LinkType.InsertText:
                (App.MainForm.Selected as ChatControl)?.Input.Logic.InsertText(_link.Value as string);
                break;

            case LinkType.UserInfo:
            {
                var data = (UserInfoData)_link.Value;

                var popup = new UserInfoPopup(data)
                {
                    StartPosition = FormStartPosition.Manual,
                    Location      = Cursor.Position
                };

                popup.Show();

                var screen = Screen.FromPoint(Cursor.Position);

                int x = popup.Location.X, y = popup.Location.Y;

                if (popup.Location.X < screen.WorkingArea.X)
                {
                    x = screen.WorkingArea.X;
                }
                else if (popup.Location.X + popup.Width > screen.WorkingArea.Right)
                {
                    x = screen.WorkingArea.Right - popup.Width;
                }

                if (popup.Location.Y < screen.WorkingArea.Y)
                {
                    y = screen.WorkingArea.Y;
                }
                else if (popup.Location.Y + popup.Height > screen.WorkingArea.Bottom)
                {
                    y = screen.WorkingArea.Bottom - popup.Height;
                }

                popup.Location = new Point(x, y);
            }
            break;

            case LinkType.ShowChannel:
            {
                var channelName = (string)_link.Value;

                var widget = App.MainForm.TabControl.TabPages
                             .Where(x => x is ColumnTabPage)
                             .SelectMany(x => ((ColumnTabPage)x).Columns.SelectMany(y => y.Widgets))
                             .FirstOrDefault(
                    c => c is ChatControl && string.Equals(((ChatControl)c).ChannelName, channelName));

                if (widget != null)
                {
                    App.MainForm.TabControl.Select(widget.Parent as Controls.TabPage);
                    widget.Select();
                }
            }
            break;

            case LinkType.TimeoutUser:
            {
                var tuple = _link.Value as Tuple <string, string, int>;

                var channel = TwitchChannel.GetChannel(tuple.Item2);

                if (channel != null)
                {
                    channel.SendMessage($"/timeout {tuple.Item1} {tuple.Item3}");
                }
            }
            break;

            case LinkType.BanUser:
            {
                var tuple = _link.Value as Tuple <string, string>;

                var channel = TwitchChannel.GetChannel(tuple.Item2);

                if (channel != null)
                {
                    channel.SendMessage($"/ban {tuple.Item1}");
                }
            }
            break;
            }
        }
Example #2
0
        public static void addChatCommands()
        {
            Commands.ChatCommands.TryAdd("user", (s, channel, execute) =>
            {
                if (execute)
                {
                    var S = s.SplitWords();
                    if (S.Length > 0 && S[0].Length > 0)
                    {
                        Common.UserInfoData data = new Common.UserInfoData();
                        data.UserName            = S[0];
                        data.Channel             = channel;
                        if ((data.UserId = IrcManager.LoadUserIDFromTwitch(data.UserName)) != null)
                        {
                            var popup = new UserInfoPopup(data)
                            {
                                StartPosition = FormStartPosition.Manual,
                                Location      = Cursor.Position
                            };

                            popup.Show();

                            var screen = Screen.FromPoint(Cursor.Position);

                            int x = popup.Location.X, y = popup.Location.Y;

                            if (popup.Location.X < screen.WorkingArea.X)
                            {
                                x = screen.WorkingArea.X;
                            }
                            else if (popup.Location.X + popup.Width > screen.WorkingArea.Right)
                            {
                                x = screen.WorkingArea.Right - popup.Width;
                            }

                            if (popup.Location.Y < screen.WorkingArea.Y)
                            {
                                y = screen.WorkingArea.Y;
                            }
                            else if (popup.Location.Y + popup.Height > screen.WorkingArea.Bottom)
                            {
                                y = screen.WorkingArea.Bottom - popup.Height;
                            }

                            popup.Location = new Point(x, y);
                        }
                        else
                        {
                            channel.AddMessage(new Chatterino.Common.Message($"This user could not be found (/user {data.UserName})"));
                        }
                    }
                }
                return(null);
            });

            // Chat Commands
            Commands.ChatCommands.TryAdd("w", (s, channel, execute) =>
            {
                if (execute)
                {
                    var S = s.SplitWords();

                    if (S.Length > 1)
                    {
                        var name = S[0];

                        IrcMessage message;
                        IrcMessage.TryParse($":{name}!{name}@{name}.tmi.twitch.tv PRIVMSG #whispers :" + s.SubstringFromWordIndex(1), out message);

                        TwitchChannel.WhisperChannel.AddMessage(new Chatterino.Common.Message(message, TwitchChannel.WhisperChannel, isSentWhisper: true));

                        if (AppSettings.ChatEnableInlineWhispers)
                        {
                            var inlineMessage = new Chatterino.Common.Message(message, TwitchChannel.WhisperChannel, true, false, isSentWhisper: true)
                            {
                                HighlightTab = false
                            };

                            inlineMessage.HighlightType = HighlightType.Whisper;

                            foreach (var c in TwitchChannel.Channels)
                            {
                                c.AddMessage(inlineMessage);
                            }
                        }
                    }
                }

                return("/w " + s);
            });

            Commands.ChatCommands.TryAdd("ignore", (s, channel, execute) =>
            {
                if (execute)
                {
                    var S = s.SplitWords();
                    if (S.Length > 0)
                    {
                        IrcManager.AddIgnoredUser(S[0], null);
                    }
                }
                return(null);
            });
            Commands.ChatCommands.TryAdd("rejoin", (s, channel, execute) =>
            {
                if (execute)
                {
                    Task.Run(() =>
                    {
                        channel.Rejoin();
                    });
                }
                return(null);
            });
            Commands.ChatCommands.TryAdd("unignore", (s, channel, execute) =>
            {
                if (execute)
                {
                    var S = s.SplitWords();
                    if (S.Length > 0)
                    {
                        IrcManager.RemoveIgnoredUser(S[0], null);
                    }
                }
                return(null);
            });

            Commands.ChatCommands.TryAdd("uptime", (s, channel, execute) =>
            {
                if (execute && channel != null)
                {
                    try
                    {
                        var request =
                            WebRequest.Create(
                                $"https://api.twitch.tv/helix/streams?user_login={channel.Name}");
                        if (AppSettings.IgnoreSystemProxy)
                        {
                            request.Proxy = null;
                        }
                        request.Headers["Authorization"] = $"Bearer {IrcManager.Account.OauthToken}";
                        request.Headers["Client-ID"]     = $"{IrcManager.DefaultClientID}";
                        using (var resp = request.GetResponse())
                            using (var stream = resp.GetResponseStream())
                            {
                                var parser = new JsonParser();

                                dynamic json = parser.Parse(stream);
                                dynamic data = json["data"];
                                if (data != null && data.Count > 0 && data[0]["type"] != "")
                                {
                                    dynamic root = data[0];

                                    string createdAt = root["started_at"];

                                    var streamStart = DateTime.Parse(createdAt);

                                    var uptime = DateTime.Now - streamStart;

                                    var text = "Stream uptime: ";

                                    if (uptime.TotalDays > 1)
                                    {
                                        text += (int)uptime.TotalDays + " days, " + uptime.ToString("hh\\h\\ mm\\m\\ ss\\s");
                                    }
                                    else
                                    {
                                        text += uptime.ToString("hh\\h\\ mm\\m\\ ss\\s");
                                    }

                                    channel.AddMessage(new Chatterino.Common.Message(text));
                                }
                            }
                    }
                    catch { }
                }

                return(null);
            });
        }