public NntpService() { this.AutoLog = true; this.CanHandlePowerEvent = false; this.CanHandleSessionChangeEvent = false; this.CanPauseAndContinue = false; this.CanShutdown = false; this.CanStop = true; this.ServiceName = "McNNTP"; // Setup LOG4NET XmlConfigurator.Configure(); // Load configuration var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var mcnntpConfigurationSection = (McNNTPConfigurationSection)config.GetSection("mcnntp"); Logger.InfoFormat("Loaded configuration from {0}", config.FilePath); server = new NntpServer { AllowPosting = true, NntpClearPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ClearText").Select(p => p.Port).ToArray(), NntpExplicitTLSPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ExplicitTLS").Select(p => p.Port).ToArray(), NntpImplicitTLSPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ImplicitTLS").Select(p => p.Port).ToArray(), LdapDirectoryConfiguration = mcnntpConfigurationSection.Authentication.UserDirectories.OfType <LdapDirectoryConfigurationElement>().OrderBy(l => l.Priority).FirstOrDefault(), PathHost = mcnntpConfigurationSection.PathHost, SslGenerateSelfSignedServerCertificate = mcnntpConfigurationSection.Ssl == null || mcnntpConfigurationSection.Ssl.GenerateSelfSignedServerCertificate, SslServerCertificateThumbprint = mcnntpConfigurationSection.Ssl == null ? null : mcnntpConfigurationSection.Ssl.ServerCertificateThumbprint }; }
/// <summary> /// Save Server. /// </summary> /// <param name="repository"> /// The repository. /// </param> /// <param name="nntpServerId"> /// The nntp server id. /// </param> /// <param name="boardId"> /// The board id. /// </param> /// <param name="name"> /// The name. /// </param> /// <param name="address"> /// The address. /// </param> /// <param name="port"> /// The port. /// </param> /// <param name="userName"> /// The user name. /// </param> /// <param name="userPass"> /// The user pass. /// </param> public static void Save( this IRepository <NntpServer> repository, [NotNull] int?nntpServerId, [NotNull] int boardId, [NotNull] string name, [NotNull] string address, [NotNull] int?port, [NotNull] string userName, [NotNull] string userPass) { CodeContracts.VerifyNotNull(repository); if (nntpServerId.HasValue) { repository.UpdateOnly( () => new NntpServer { Name = name, Address = address, Port = port, UserName = userName, UserPass = userPass }, n => n.ID == nntpServerId); } else { var entity = new NntpServer { Name = name, BoardID = boardId, Address = address, Port = port, UserName = userName, UserPass = userPass }; repository.Insert(entity); repository.FireNew(entity); } }
/// <summary> /// The main program message loop /// </summary> /// <returns>An error code, indicating an error condition when the value returned is non-zero</returns> /// <exception cref="FormatException">Thrown when an attempt to construct a format string fails to properly format a finalized message</exception> /// <exception cref="IOException">Thrown when the process is unable to write status to the console window</exception> /// <exception cref="ArgumentNullException">Thrown when a 'null' value is attempted to be written to the console window</exception> /// <exception cref="ConfigurationErrorsException">Thrown when the configuration file for the process cannot be parsed</exception> /// <exception cref="SecurityException">Thrown when the X.509 certificate store cannot be opened or enumerated when constructing SSL ports</exception> public static int Main() { var version = Assembly.GetEntryAssembly().GetName().Version; Console.WriteLine("McNNTP Server Console Harness v{0}", version); try { // Setup LOG4NET XmlConfigurator.Configure(); var logger = LogManager.GetLogger(typeof(Program)); // Load configuration var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var mcnntpConfigurationSection = (McNNTPConfigurationSection)config.GetSection("mcnntp"); logger.InfoFormat("Loaded configuration from {0}", config.FilePath); // Verify Database if (DatabaseUtility.VerifyDatabase()) { DatabaseUtility.UpdateSchema(); } else if (DatabaseUtility.UpdateSchema() && !DatabaseUtility.VerifyDatabase(true)) { Console.WriteLine( "Unable to verify a database. Would you like to create and initialize a database?"); var resp = Console.ReadLine(); if (resp != null && new[] { "y", "yes" }.Contains(resp.ToLowerInvariant().Trim())) { DatabaseUtility.RebuildSchema(); } } _ircServer = new IrcServer { SslGenerateSelfSignedServerCertificate = mcnntpConfigurationSection.Ssl == null || mcnntpConfigurationSection.Ssl.GenerateSelfSignedServerCertificate, SslServerCertificateThumbprint = mcnntpConfigurationSection.Ssl == null ? null : mcnntpConfigurationSection.Ssl.ServerCertificateThumbprint }; _ircServer.Start(); _imapServer = new ImapServer { AllowPosting = true, ImapClearPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ClearText" && p.Protocol == "imap") .Select(p => p.Port) .ToArray(), ImapExplicitTLSPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ExplicitTLS" && p.Protocol == "imap") .Select(p => p.Port) .ToArray(), ImapImplicitTLSPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ImplicitTLS" && p.Protocol == "imap") .Select(p => p.Port) .ToArray(), PathHost = mcnntpConfigurationSection.PathHost, SslGenerateSelfSignedServerCertificate = mcnntpConfigurationSection.Ssl == null || mcnntpConfigurationSection.Ssl.GenerateSelfSignedServerCertificate, SslServerCertificateThumbprint = mcnntpConfigurationSection.Ssl == null ? null : mcnntpConfigurationSection.Ssl.ServerCertificateThumbprint }; _imapServer.Start(); _nntpServer = new NntpServer { AllowPosting = true, IrcClearPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ClearText" && p.Protocol == "irc") .Select(p => p.Port) .ToArray(), IrcImplicitTLSPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ImplicitTLS" && p.Protocol == "irc") .Select(p => p.Port) .ToArray(), NntpClearPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ClearText" && p.Protocol == "nntp") .Select(p => p.Port) .ToArray(), NntpExplicitTLSPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ExplicitTLS" && p.Protocol == "nntp") .Select(p => p.Port) .ToArray(), NntpImplicitTLSPorts = mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ImplicitTLS" && p.Protocol == "nntp") .Select(p => p.Port) .ToArray(), LdapDirectoryConfiguration = mcnntpConfigurationSection.Authentication.UserDirectories .OfType <LdapDirectoryConfigurationElement>() .OrderBy(l => l.Priority) .FirstOrDefault(), PathHost = mcnntpConfigurationSection.PathHost, SslGenerateSelfSignedServerCertificate = mcnntpConfigurationSection.Ssl == null || mcnntpConfigurationSection.Ssl.GenerateSelfSignedServerCertificate, SslServerCertificateThumbprint = mcnntpConfigurationSection.Ssl == null ? null : mcnntpConfigurationSection.Ssl.ServerCertificateThumbprint }; _nntpServer.Start(); Console.WriteLine("Type QUIT and press Enter to end the server."); while (true) { Console.Write("\r\n> "); var input = Console.ReadLine(); if (input == null || !_CommandDirectory.ContainsKey(input.Split(' ')[0].ToUpperInvariant())) { Console.WriteLine("Unrecongized command. Type HELP for a list of available commands."); continue; } if (!_CommandDirectory[input.Split(' ')[0].ToUpperInvariant()].Invoke(input)) { continue; } _imapServer.Stop(); _nntpServer.Stop(); return(0); } } catch (AggregateException ae) { foreach (var ex in ae.InnerExceptions) { Console.WriteLine(ex.ToString()); } Console.ReadLine(); return(-2); } catch (SecurityException sex) { Console.WriteLine(sex.ToString()); Console.ReadLine(); return(-3); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); return(-1); } }