string GetString(RegistryKey key, string name)
 {
     try
     {
         var o = key.GetValue(name);
         if (o is string)
         {
             return((string)o);
         }
         var ic = o as IConvertible;
         if (ic != null)
         {
             try
             {
                 return(ic.ToString(System.Globalization.CultureInfo.InvariantCulture));
             }
             catch (Exception exp)
             {
                 Traces.Main_TraceEvent(TraceEventType.Critical, 1, "GetString.ToString: Error: {0}", NNTPServer.Traces.ExceptionToString(exp));
             }
         }
         if (o != null)
         {
             return(o.ToString());
         }
     }
     catch (Exception exp)
     {
         Traces.Main_TraceEvent(TraceEventType.Critical, 1, "GetString: Error: {0}", NNTPServer.Traces.ExceptionToString(exp));
     }
     return(null);
 }
Example #2
0
        private static void StartBridgeInternal(MainWindow t, int port)
        {
            // Create our DataSource for the forums
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Creating datasource for NNTP server");
            t._forumsDataSource = new DataSourceStackApps(t._accessToken);
            t._forumsDataSource.UsePlainTextConverter      = UserSettings.Default.UsePlainTextConverter;
            t._forumsDataSource.AutoLineWrap               = UserSettings.Default.AutoLineWrap;
            t._forumsDataSource.HeaderEncoding             = UserSettings.Default.EncodingForClientEncoding;
            t._forumsDataSource.InMimeUseHtml              = (UserSettings.Default.InMimeUse == UserSettings.MimeContentType.TextHtml);
            t._forumsDataSource.PostsAreAlwaysFormatFlowed = UserSettings.Default.PostsAreAlwaysFormatFlowed;
            t._forumsDataSource.TabAsSpace       = UserSettings.Default.TabAsSpace;
            t._forumsDataSource.UseCodeColorizer = UserSettings.Default.UseCodeColorizer;

            t._forumsDataSource.ProgressData += t._forumsDataSource_ProgressData;


            // Now start the NNTP-Server
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Starting NNTP server");
            t._nntpServer = new NNTPServer.NntpServer(t._forumsDataSource, true);
            t._nntpServer.EncodingSend      = UserSettings.Default.EncodingForClientEncoding;
            t._nntpServer.ListGroupDisabled = UserSettings.Default.DisableLISTGROUP;
            string errorMessage;

            t._nntpServer.Start(port, 64, UserSettings.Default.BindToWorld, out errorMessage);
            if (errorMessage != null)
            {
                throw new ApplicationException(errorMessage);
            }
        }
 void SetInt32(RegistryKey key, string name, int value)
 {
     try
     {
         key.SetValue(name, value, RegistryValueKind.DWord);
     }
     catch (Exception exp)
     {
         Traces.Main_TraceEvent(TraceEventType.Critical, 1, "SetInt32: Error: {0}", NNTPServer.Traces.ExceptionToString(exp));
     }
 }
        public void Save()
        {
            lock (this)
            {
                try
                {
                    using (var r = UserAppDataRegistryForWriting)
                    {
                        SetBoolean(r, "AutoStart", AutoStart);

                        SetBoolean(r, "UseAuthentication", UseAuthentication);

                        SetBoolean(r, "ShowInboxAndNotifications", ShowInboxAndNotifications);

                        SetBoolean(r, "AutoMinimize", AutoMinimize);

                        SetBoolean(r, "BindToWorld", BindToWorld);

                        SetEnum(r, "UsePlainTextConverterEnum", UsePlainTextConverter);

                        SetBoolean(r, "PostsAreAlwaysFormatFlowed", PostsAreAlwaysFormatFlowed);

                        SetInt32(r, "Port", Port);

                        SetInt32(r, "AutoLineWrap", AutoLineWrap);

                        SetString(r, "EncodingForClient", EncodingForClient);

                        SetEnum(r, "InMimeUse", InMimeUse);
                        SetBoolean(r, "DisableLISTGROUP", DisableLISTGROUP);

                        SetInt32(r, "TabAsSpace", TabAsSpace);

                        SetBoolean(r, "UseCodeColorizer", UseCodeColorizer);

                        SetBoolean(r, "DisableArticleCache", DisableArticleCache);

                        SetEnum(r, "MessageInfos", MessageInfos);
                    }
                }
                catch (Exception exp)
                {
                    Traces.Main_TraceEvent(TraceEventType.Critical, 1, "Error saving settings to the registry: {0}",
                                           NNTPServer.Traces.ExceptionToString(exp));
                }
            }
        }
 bool?GetBoolean(RegistryKey key, string name)
 {
     try
     {
         var iv = GetInt32(key, name);
         if (iv.HasValue)
         {
             if (iv.Value != 0)
             {
                 return(true);
             }
             return(false);
         }
     }
     catch (Exception exp)
     {
         Traces.Main_TraceEvent(TraceEventType.Critical, 1, "GetBoolean: Error: {0}", NNTPServer.Traces.ExceptionToString(exp));
     }
     return(null);
 }
        static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string expMsg = NNTPServer.Traces.ExceptionToString(e.ExceptionObject as Exception);

            if (string.IsNullOrEmpty(expMsg))
            {
                expMsg = "<Unknown Exception>";
            }
            Traces.Main_TraceEvent(System.Diagnostics.TraceEventType.Critical, 1, "UnhandledException: {0}",
                                   expMsg);

            var    exp = e.ExceptionObject as Exception;
            string msg = "<Unknown>";

            if (exp != null)
            {
                msg = exp.Message;
            }

            var dlg = new SendDebugDataWindow();

            dlg.UnhandledExceptionMessage = msg;
            if (dlg.ShowDialog() == true)
            //MessageBoxResult res = MessageBox.Show(
            //  string.Format("UnhandledException occured: {0}\r\n\r\nDo you want to send debug data to the developer?", msg),
            //  "Error", MessageBoxButton.YesNo, MessageBoxImage.Stop);
            //if (res == MessageBoxResult.Yes)
            {
                try
                {
                    var app = (App)Current;
                    app.SendLogs(expMsg, dlg.UsersEMail, dlg.UsersDescription, dlg.UserSendEmail);
                }
                catch { }
                // Terminate...
                //Current.Shutdown(-1);
                Environment.Exit(-1);
            }
        }
Example #7
0
 private void mnuDebugWindow_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (mnuDebugWindow.IsChecked)
         {
             _debugWindow       = new DebugWindow();
             _debugWindow.Owner = this;
             _debugWindow.Show();
             _debugWindow.Closed += new EventHandler(_debugWindow_Closed);
         }
         else
         {
             _debugWindow.Close();
             _debugWindow = null;
         }
     }
     catch (Exception exp)
     {
         Traces.Main_TraceEvent(TraceEventType.Error, 1, "Error in mnuDebugWindow_click: {0}", NNTPServer.Traces.ExceptionToString(exp));
     }
 }
        void SetEnum(RegistryKey key, string name, Enum value)
        {
            int iVal = 0;

            try
            {
                var ic = value as IConvertible;
                if (ic != null)
                {
                    iVal = ic.ToInt32(System.Globalization.CultureInfo.InvariantCulture);
                }
                else
                {
                    throw new NotSupportedException("Could not convert enum to int");
                }
            }
            catch (Exception exp)
            {
                Traces.Main_TraceEvent(TraceEventType.Critical, 1, "SetEnum {0}: Error: {1}", value, NNTPServer.Traces.ExceptionToString(exp));
                return;
            }
            SetInt32(key, name, iVal);
        }
        void Load()
        {
            try
            {
                using (var r = UserAppDataRegistryForReading)
                {
                    if (r == null)
                    {
                        return;
                    }

                    int?   i;
                    bool?  b;
                    string s;

                    b = GetBoolean(r, "AutoStart");
                    if (b.HasValue)
                    {
                        AutoStart = b.Value;
                    }

                    b = GetBoolean(r, "UseAuthentication");
                    if (b.HasValue)
                    {
                        UseAuthentication = b.Value;
                    }

                    b = GetBoolean(r, "ShowInboxAndNotifications");
                    if (b.HasValue)
                    {
                        ShowInboxAndNotifications = b.Value;
                    }

                    b = GetBoolean(r, "AutoMinimize");
                    if (b.HasValue)
                    {
                        AutoMinimize = b.Value;
                    }

                    b = GetBoolean(r, "BindToWorld");
                    if (b.HasValue)
                    {
                        BindToWorld = b.Value;
                    }

                    UsePlainTextConverters?ptc = GetEnum <UsePlainTextConverters>(r, "UsePlainTextConverterEnum");
                    if (ptc.HasValue)
                    {
                        UsePlainTextConverter = ptc.Value;
                    }

                    b = GetBoolean(r, "PostsAreAlwaysFormatFlowed");
                    if (b.HasValue)
                    {
                        PostsAreAlwaysFormatFlowed = b.Value;
                    }

                    i = GetInt32(r, "Port");
                    if (i.HasValue)
                    {
                        Port = i.Value;
                    }

                    i = GetInt32(r, "AutoLineWrap");
                    if (i.HasValue)
                    {
                        AutoLineWrap = i.Value;
                    }

                    s = GetString(r, "EncodingForClient");
                    EncodingForClient = s;

                    MimeContentType?mt = GetEnum <MimeContentType>(r, "InMimeUse");
                    if (mt.HasValue)
                    {
                        InMimeUse = mt.Value;
                    }

                    b = GetBoolean(r, "DisableLISTGROUP");
                    if (b.HasValue)
                    {
                        DisableLISTGROUP = b.Value;
                    }

                    i = GetInt32(r, "TabAsSpace");
                    if (i.HasValue)
                    {
                        TabAsSpace = i.Value;
                    }

                    b = GetBoolean(r, "UseCodeColorizer");
                    if (b.HasValue)
                    {
                        UseCodeColorizer = b.Value;
                    }

                    b = GetBoolean(r, "DisableArticleCache");
                    if (b.HasValue)
                    {
                        DisableArticleCache = b.Value;
                    }

                    MessageInfoEnum?mie = GetEnum <MessageInfoEnum>(r, "MessageInfos");
                    if (mie.HasValue)
                    {
                        MessageInfos = mie.Value;
                    }
                }
            }
            catch (Exception exp)
            {
                Traces.Main_TraceEvent(TraceEventType.Critical, 1, "Error loading settings from the registry: {0}", NNTPServer.Traces.ExceptionToString(exp));
            }
        }
        internal void SendLogs(string unhandledException, string userEmail, string userComment, bool userSendEmail)
        {
            string zipFile = null;

            try
            {
                zipFile = _logFile.CreateZipFile();
            }
            catch (Exception exp)
            {
                Traces.Main_TraceEvent(TraceEventType.Error, 1, NNTPServer.Traces.ExceptionToString(exp));
            }

            if (zipFile == null)
            {
                if (string.IsNullOrEmpty(unhandledException) == false)
                {
                    MessageBox.Show("Failed to ZIP the debug data! Please restart the bridge!", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else
                {
                    MessageBox.Show("Failed to ZIP the debug data!", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                return;
            }
            try
            {
                string err = UploadDiagnoseData.DoIt(zipFile, unhandledException, userEmail, userComment, userSendEmail);
                if (err == null)
                {
                    if (string.IsNullOrEmpty(unhandledException) == false)
                    {
                        MessageBox.Show("Debug data send... Thank your for helping to improve the bridge or find problems!\r\n\r\nPlease restart the bridge!");
                    }
                    else
                    {
                        MessageBox.Show("Debug data send... Thank your for helping to improve the bridge or find problems!");
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(unhandledException) == false)
                    {
                        MessageBox.Show("Failed to send debug data:\r\n" + err + "\r\n\r\nPlease restart the bridge!", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else
                    {
                        MessageBox.Show("Failed to send debug data:\r\n" + err, "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }
            }
            catch (Exception exp)
            {
                Traces.Main_TraceEvent(TraceEventType.Error, 1, NNTPServer.Traces.ExceptionToString(exp));
            }
            try
            {
                File.Delete(zipFile);
            }
            catch (Exception exp)
            {
                Traces.Main_TraceEvent(TraceEventType.Error, 1, NNTPServer.Traces.ExceptionToString(exp));
            }
        }
Example #11
0
        private void StartBridgeAsync(Action <bool, string> onFinishedCallback)
        {
            int port = 119;
            int parsedPort;

            if (int.TryParse(txtPort.Text, out parsedPort))
            {
                port = parsedPort;
            }

            lblInfo.Text       = "Starting server... please wait...";
            cmdStart.IsEnabled = false;

            // First do authetntication in the "main" UI-thread!
            if (UserSettings.Default.UseAuthentication)
            {
                // Try to login....
                Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "OAuth 2.0: Try authentication...");

                var scopes = new List <string>();
                scopes.Add("no_expiry");
                if (UserSettings.Default.ShowInboxAndNotifications)
                {
                    scopes.Add("read_inbox");
                }

                var dlg = new AuthWindow(scopes);
                dlg.Owner = this;
                if (dlg.ShowDialog() == true)
                {
                    this._accessToken = dlg.AccessToken;
                }
                else
                {
                    Traces.Main_TraceEvent(TraceEventType.Error, 1, string.Format(string.Format("Authentication failed: {0}", dlg.ErrorDescription)));
                    if (onFinishedCallback != null)
                    {
                        onFinishedCallback(false,
                                           "Could not authenticate with StackExchange! If you want to work without authentication, please disable autehntication in the User Settings (General|UserAuthentication)");
                    }
                    return;
                }
            }

            var thread = new System.Threading.Thread(
                delegate(object o)
            {
                var t     = o as MainWindow;
                var bRes  = false;
                var error = string.Empty;
                try
                {
                    StartBridgeInternal(t, port);
                    bRes = true;
                }
                catch (Exception exp)
                {
                    Traces.Main_TraceEvent(TraceEventType.Error, 1, "StartBridgeInternal: {0}", NNTPServer.Traces.ExceptionToString(exp));
                    error = exp.Message;
                }
                t.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(
                        delegate
                {
                    if (bRes)
                    {
                        t.Started = true;
                    }
                    else
                    {
                        t.lblInfo.Text = error;
                        t.ApplySettings();  // for correcting the "LiveId auologin" menu entry
                    }
                    t.cmdStart.IsEnabled = true;
                    if (onFinishedCallback != null)
                    {
                        onFinishedCallback(bRes, error);
                    }
                }));
            });

            thread.IsBackground = true;
            thread.SetApartmentState(System.Threading.ApartmentState.STA);
            thread.Start(this);
        }