Пример #1
0
        static async Task <int> Main(string[] args)
        {
            #if !DEBUG
            AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
            {
                using (var stream = typeof(Program).Assembly.GetManifestResourceStream(
                           new System.Reflection.AssemblyName(e.Name).Name.ToLowerInvariant() + ".dll"))
                {
                    if (stream == null)
                    {
                        return(null);
                    }

                    var bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, (int)stream.Length);
                    return(System.Reflection.Assembly.Load(bytes));
                }
            };

            var iv = (System.Reflection.AssemblyInformationalVersionAttribute)Attribute.GetCustomAttribute(
                System.Reflection.Assembly.GetExecutingAssembly(), typeof(
                    System.Reflection.AssemblyInformationalVersionAttribute));

            if (iv != null)
            {
                Console.Error.WriteLine($"{nameof (SendGmail)} rev. {iv.InformationalVersion}");
            }
            #endif

            try
            {
                if (args.Length == 0)
                {
                    return(await InstallAsync());
                }

                return(await SendGmail.RunAsync(Console.OpenStandardInput()));
            }
            catch (MyException e)
            {
                Console.Error.WriteLine(e.Message);
                return(1);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                return(1);
            }
        }
Пример #2
0
        static async Task <int> InstallAsync()
        {
            if (Console.IsInputRedirected)
            {
                Console.Error.WriteLine($"Run {nameof (SendGmail)} in interactive mode to install.");
                return(1);
            }

            string server;

            try
            {
                if (!GitGetConfig("sendemail.smtpserver", out server) && server != "")
                {
                    Console.Error.WriteLine("git config failed with message " + server);
                    return(1);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("git not found in %PATH%, or other problems running git:");
                Console.Error.WriteLine(e.Message);
                return(1);
            }

            if (!server.Contains(nameof(SendGmail)) || !File.Exists(server.Replace('/', '\\')))
            {
                if (!SendGmail.Confirm($"Install {nameof (SendGmail)} as your git-send-email plug-in?"))
                {
                    return(1);
                }

                var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "atykhyy", nameof(SendGmail));
                Directory.CreateDirectory(folder);
                var target = Path.Combine(folder, nameof(SendGmail) + ".exe");
                File.Copy(System.Reflection.Assembly.GetExecutingAssembly().Location, target);

                // git-send-email wants its slashes straight
                // add outer quotes in case of spaces in path
                GitSetConfig("sendemail.smtpserver", $"\"{target.Replace ('\\', '/')}\"");
            }

            if (!GitGetConfig("user.name", out var username) || !GitGetConfig("user.email", out var useremail))
            {
                Console.Error.WriteLine("user.name and/or user.email are not configured.");
                Console.Error.WriteLine($"Please configure and rerun {nameof (SendGmail)}.");
                return(1);
            }

            if (!SendGmail.Confirm($"Send a test email to {useremail} to set up credentials (recommended)?"))
            {
                return(0);
            }

            if (!GitGetConfig("sendemail.smtpuser", out var smtpuser) && smtpuser == "")
            {
                for (smtpuser = useremail; !SendGmail.Confirm($"Is {smtpuser} the Gmail address you want to use with git-send-email?");)
                {
                    Console.WriteLine("Enter your Gmail address for git-send-email:");
                    smtpuser = Console.ReadLine();
                }

                GitSetConfig("sendemail.smtpuser", smtpuser);
            }

            var now   = DateTimeOffset.Now;
            var email = $"From: {username} <{smtpuser}>\nTo: {useremail}\nSubject: Hello from {nameof (SendGmail)}\n" +
                        $"Date: {now.ToString ("ddd, dd MMM yyyy HH':'mm':'ss K", CultureInfo.InvariantCulture)}\n" +
                        $"Message-Id: <{now.UtcDateTime.ToString ("yyyyMMddHHmmss'.'ffff", CultureInfo.InvariantCulture)}-1-{smtpuser}>\n" +
                        $"X-Mailer: {nameof (SendGmail)} 1.0\n" +
                        "MIME-Version: 1.0\n" +
                        "Content-Transfer-Encoding: 8bit\n\n" +
                        $"Hello from {nameof (SendGmail)}!\n";

            return(await SendGmail.RunAsync(new MemoryStream (Encoding.ASCII.GetBytes(email))));
        }