1#!/usr/local/bin/python3.8
2#
3# Electrum - lightweight Bitcoin client
4#
5# Permission is hereby granted, free of charge, to any person
6# obtaining a copy of this software and associated documentation files
7# (the "Software"), to deal in the Software without restriction,
8# including without limitation the rights to use, copy, modify, merge,
9# publish, distribute, sublicense, and/or sell copies of the Software,
10# and to permit persons to whom the Software is furnished to do so,
11# subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be
14# included in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24import sys
25import html
26from typing import TYPE_CHECKING, Optional, Set
27
28from PyQt5.QtCore import QObject
29import PyQt5.QtCore as QtCore
30from PyQt5.QtWidgets import (QWidget, QLabel, QPushButton, QTextEdit,
31                             QMessageBox, QHBoxLayout, QVBoxLayout)
32
33from electrum.i18n import _
34from electrum.base_crash_reporter import BaseCrashReporter
35from electrum.logging import Logger
36from electrum import constants
37from electrum.network import Network
38
39from .util import MessageBoxMixin, read_QIcon, WaitingDialog
40
41if TYPE_CHECKING:
42    from electrum.simple_config import SimpleConfig
43    from electrum.wallet import Abstract_Wallet
44
45
46class Exception_Window(BaseCrashReporter, QWidget, MessageBoxMixin, Logger):
47    _active_window = None
48
49    def __init__(self, config: 'SimpleConfig', exctype, value, tb):
50        BaseCrashReporter.__init__(self, exctype, value, tb)
51        self.network = Network.get_instance()
52        self.config = config
53
54        QWidget.__init__(self)
55        self.setWindowTitle('Electrum - ' + _('An Error Occurred'))
56        self.setMinimumSize(600, 300)
57
58        Logger.__init__(self)
59
60        main_box = QVBoxLayout()
61
62        heading = QLabel('<h2>' + BaseCrashReporter.CRASH_TITLE + '</h2>')
63        main_box.addWidget(heading)
64        main_box.addWidget(QLabel(BaseCrashReporter.CRASH_MESSAGE))
65
66        main_box.addWidget(QLabel(BaseCrashReporter.REQUEST_HELP_MESSAGE))
67
68        collapse_info = QPushButton(_("Show report contents"))
69        collapse_info.clicked.connect(
70            lambda: self.msg_box(QMessageBox.NoIcon,
71                                 self, _("Report contents"), self.get_report_string(),
72                                 rich_text=True))
73
74        main_box.addWidget(collapse_info)
75
76        main_box.addWidget(QLabel(BaseCrashReporter.DESCRIBE_ERROR_MESSAGE))
77
78        self.description_textfield = QTextEdit()
79        self.description_textfield.setFixedHeight(50)
80        self.description_textfield.setPlaceholderText(self.USER_COMMENT_PLACEHOLDER)
81        main_box.addWidget(self.description_textfield)
82
83        main_box.addWidget(QLabel(BaseCrashReporter.ASK_CONFIRM_SEND))
84
85        buttons = QHBoxLayout()
86
87        report_button = QPushButton(_('Send Bug Report'))
88        report_button.clicked.connect(self.send_report)
89        report_button.setIcon(read_QIcon("tab_send.png"))
90        buttons.addWidget(report_button)
91
92        never_button = QPushButton(_('Never'))
93        never_button.clicked.connect(self.show_never)
94        buttons.addWidget(never_button)
95
96        close_button = QPushButton(_('Not Now'))
97        close_button.clicked.connect(self.close)
98        buttons.addWidget(close_button)
99
100        main_box.addLayout(buttons)
101
102        self.setLayout(main_box)
103        self.show()
104
105    def send_report(self):
106        def on_success(response):
107            # note: 'response' coming from (remote) crash reporter server.
108            # It contains a URL to the GitHub issue, so we allow rich text.
109            self.show_message(parent=self,
110                              title=_("Crash report"),
111                              msg=response,
112                              rich_text=True)
113            self.close()
114        def on_failure(exc_info):
115            e = exc_info[1]
116            self.logger.error('There was a problem with the automatic reporting', exc_info=exc_info)
117            self.show_critical(parent=self,
118                               msg=(_('There was a problem with the automatic reporting:') + '<br/>' +
119                                    repr(e)[:120] + '<br/><br/>' +
120                                    _("Please report this issue manually") +
121                                    f' <a href="{constants.GIT_REPO_ISSUES_URL}">on GitHub</a>.'),
122                               rich_text=True)
123
124        proxy = self.network.proxy
125        task = lambda: BaseCrashReporter.send_report(self, self.network.asyncio_loop, proxy)
126        msg = _('Sending crash report...')
127        WaitingDialog(self, msg, task, on_success, on_failure)
128
129    def on_close(self):
130        Exception_Window._active_window = None
131        self.close()
132
133    def show_never(self):
134        self.config.set_key(BaseCrashReporter.config_key, False)
135        self.close()
136
137    def closeEvent(self, event):
138        self.on_close()
139        event.accept()
140
141    def get_user_description(self):
142        return self.description_textfield.toPlainText()
143
144    def get_wallet_type(self):
145        wallet_types = Exception_Hook._INSTANCE.wallet_types_seen
146        return ",".join(wallet_types)
147
148    def _get_traceback_str_to_display(self) -> str:
149        # The msg_box that shows the report uses rich_text=True, so
150        # if traceback contains special HTML characters, e.g. '<',
151        # they need to be escaped to avoid formatting issues.
152        traceback_str = super()._get_traceback_str_to_display()
153        return html.escape(traceback_str)
154
155
156def _show_window(*args):
157    if not Exception_Window._active_window:
158        Exception_Window._active_window = Exception_Window(*args)
159
160
161class Exception_Hook(QObject, Logger):
162    _report_exception = QtCore.pyqtSignal(object, object, object, object)
163
164    _INSTANCE = None  # type: Optional[Exception_Hook]  # singleton
165
166    def __init__(self, *, config: 'SimpleConfig'):
167        QObject.__init__(self)
168        Logger.__init__(self)
169        assert self._INSTANCE is None, "Exception_Hook is supposed to be a singleton"
170        self.config = config
171        self.wallet_types_seen = set()  # type: Set[str]
172
173        sys.excepthook = self.handler
174        self._report_exception.connect(_show_window)
175
176    @classmethod
177    def maybe_setup(cls, *, config: 'SimpleConfig', wallet: 'Abstract_Wallet' = None) -> None:
178        if not config.get(BaseCrashReporter.config_key, default=True):
179            return
180        if not cls._INSTANCE:
181            cls._INSTANCE = Exception_Hook(config=config)
182        if wallet:
183            cls._INSTANCE.wallet_types_seen.add(wallet.wallet_type)
184
185    def handler(self, *exc_info):
186        self.logger.error('exception caught by crash reporter', exc_info=exc_info)
187        self._report_exception.emit(self.config, *exc_info)
188