diff --git a/protocol.py b/protocol.py index c64ad63a..0f7f8ce4 100644 --- a/protocol.py +++ b/protocol.py @@ -51,7 +51,7 @@ if sys.platform == 'darwin' and getattr(sys, 'frozen', False): class ProtocolHandler(GenericProtocolHandler): - POLL = 100 # ms + POLL = 100 # ms def start(self, master): GenericProtocolHandler.start(self, master) @@ -68,84 +68,92 @@ if sys.platform == 'darwin' and getattr(sys, 'frozen', False): def init(self): self = objc.super(EventHandler, self).init() - NSAppleEventManager.sharedAppleEventManager().setEventHandler_andSelector_forEventClass_andEventID_(self, 'handleEvent:withReplyEvent:', kInternetEventClass, kAEGetURL) + NSAppleEventManager.sharedAppleEventManager().setEventHandler_andSelector_forEventClass_andEventID_( + self, + 'handleEvent:withReplyEvent:', + kInternetEventClass, + kAEGetURL + ) return self def handleEvent_withReplyEvent_(self, event, replyEvent): - protocolhandler.lasturl = urllib.parse.unquote(event.paramDescriptorForKeyword_(keyDirectObject).stringValue()).strip() + protocolhandler.lasturl = urllib.parse.unquote( + event.paramDescriptorForKeyword_(keyDirectObject).stringValue()).strip() protocolhandler.master.after(ProtocolHandler.POLL, protocolhandler.poll) elif sys.platform == 'win32' and getattr(sys, 'frozen', False) and not is_wine and not config.auth_force_localserver: class WNDCLASS(Structure): - _fields_ = [('style', UINT), - ('lpfnWndProc', WINFUNCTYPE(c_long, HWND, UINT, WPARAM, LPARAM)), - ('cbClsExtra', INT), - ('cbWndExtra', INT), - ('hInstance', HINSTANCE), - ('hIcon', HICON), - ('hCursor', c_void_p), - ('hbrBackground', HBRUSH), - ('lpszMenuName', LPCWSTR), - ('lpszClassName', LPCWSTR) + _fields_ = [ + ('style', UINT), + ('lpfnWndProc', WINFUNCTYPE(c_long, HWND, UINT, WPARAM, LPARAM)), + ('cbClsExtra', INT), + ('cbWndExtra', INT), + ('hInstance', HINSTANCE), + ('hIcon', HICON), + ('hCursor', c_void_p), + ('hbrBackground', HBRUSH), + ('lpszMenuName', LPCWSTR), + ('lpszClassName', LPCWSTR) ] - CW_USEDEFAULT = 0x80000000 + CW_USEDEFAULT = 0x80000000 - CreateWindowEx = windll.user32.CreateWindowExW + CreateWindowEx = windll.user32.CreateWindowExW CreateWindowEx.argtypes = [DWORD, LPCWSTR, LPCWSTR, DWORD, INT, INT, INT, INT, HWND, HMENU, HINSTANCE, LPVOID] - CreateWindowEx.restype = HWND - RegisterClass = windll.user32.RegisterClassW - RegisterClass.argtypes = [POINTER(WNDCLASS)] - DefWindowProc = windll.user32.DefWindowProcW - GetParent = windll.user32.GetParent + CreateWindowEx.restype = HWND + RegisterClass = windll.user32.RegisterClassW + RegisterClass.argtypes = [POINTER(WNDCLASS)] + DefWindowProc = windll.user32.DefWindowProcW + GetParent = windll.user32.GetParent SetForegroundWindow = windll.user32.SetForegroundWindow - GetMessage = windll.user32.GetMessageW - TranslateMessage = windll.user32.TranslateMessage - DispatchMessage = windll.user32.DispatchMessageW + GetMessage = windll.user32.GetMessageW + TranslateMessage = windll.user32.TranslateMessage + DispatchMessage = windll.user32.DispatchMessageW PostThreadMessage = windll.user32.PostThreadMessageW - SendMessage = windll.user32.SendMessageW + SendMessage = windll.user32.SendMessageW SendMessage.argtypes = [HWND, UINT, WPARAM, LPARAM] - PostMessage = windll.user32.PostMessageW + PostMessage = windll.user32.PostMessageW PostMessage.argtypes = [HWND, UINT, WPARAM, LPARAM] - WM_QUIT = 0x0012 - WM_DDE_INITIATE = 0x03E0 - WM_DDE_TERMINATE = 0x03E1 - WM_DDE_ACK = 0x03E4 - WM_DDE_EXECUTE = 0x03E8 + WM_QUIT = 0x0012 + WM_DDE_INITIATE = 0x03E0 + WM_DDE_TERMINATE = 0x03E1 + WM_DDE_ACK = 0x03E4 + WM_DDE_EXECUTE = 0x03E8 - PackDDElParam = windll.user32.PackDDElParam + PackDDElParam = windll.user32.PackDDElParam PackDDElParam.argtypes = [UINT, LPARAM, LPARAM] - GlobalAddAtom = windll.kernel32.GlobalAddAtomW + GlobalAddAtom = windll.kernel32.GlobalAddAtomW GlobalAddAtom.argtypes = [LPWSTR] GlobalAddAtom.restype = ATOM GlobalGetAtomName = windll.kernel32.GlobalGetAtomNameW GlobalGetAtomName.argtypes = [ATOM, LPWSTR, INT] GlobalGetAtomName.restype = UINT - GlobalLock = windll.kernel32.GlobalLock + GlobalLock = windll.kernel32.GlobalLock GlobalLock.argtypes = [HGLOBAL] GlobalLock.restype = LPVOID - GlobalUnlock = windll.kernel32.GlobalUnlock + GlobalUnlock = windll.kernel32.GlobalUnlock GlobalUnlock.argtypes = [HGLOBAL] GlobalUnlock.restype = BOOL - @WINFUNCTYPE(c_long, HWND, UINT, WPARAM, LPARAM) def WndProc(hwnd, message, wParam, lParam): service = create_unicode_buffer(256) topic = create_unicode_buffer(256) if message == WM_DDE_INITIATE: - if ((lParam & 0xffff == 0 or (GlobalGetAtomName(lParam & 0xffff, service, 256) and service.value == appname)) and - (lParam >> 16 == 0 or (GlobalGetAtomName(lParam >> 16, topic, 256) and topic.value.lower() == 'system'))): - SendMessage(wParam, WM_DDE_ACK, hwnd, PackDDElParam(WM_DDE_ACK, GlobalAddAtom(appname), GlobalAddAtom('System'))) + if ( + (lParam & 0xffff == 0 or (GlobalGetAtomName(lParam & 0xffff, service, 256) and service.value == appname)) and + (lParam >> 16 == 0 or (GlobalGetAtomName(lParam >> 16, topic, 256) and topic.value.lower() == 'system')) + ): + SendMessage(wParam, WM_DDE_ACK, hwnd, PackDDElParam( + WM_DDE_ACK, GlobalAddAtom(appname), GlobalAddAtom('System'))) return 0 return DefWindowProc(hwnd, message, wParam, lParam) - class ProtocolHandler(GenericProtocolHandler): def __init__(self): @@ -163,31 +171,33 @@ elif sys.platform == 'win32' and getattr(sys, 'frozen', False) and not is_wine a if thread: self.thread = None PostThreadMessage(thread.ident, WM_QUIT, 0, 0) - thread.join() # Wait for it to quit + thread.join() # Wait for it to quit def worker(self): wndclass = WNDCLASS() - wndclass.style = 0 - wndclass.lpfnWndProc = WndProc - wndclass.cbClsExtra = 0 - wndclass.cbWndExtra = 0 - wndclass.hInstance = windll.kernel32.GetModuleHandleW(0) - wndclass.hIcon = None - wndclass.hCursor = None - wndclass.hbrBackground = None - wndclass.lpszMenuName = None - wndclass.lpszClassName = 'DDEServer' + wndclass.style = 0 + wndclass.lpfnWndProc = WndProc + wndclass.cbClsExtra = 0 + wndclass.cbWndExtra = 0 + wndclass.hInstance = windll.kernel32.GetModuleHandleW(0) + wndclass.hIcon = None + wndclass.hCursor = None + wndclass.hbrBackground = None + wndclass.lpszMenuName = None + wndclass.lpszClassName = 'DDEServer' if RegisterClass(byref(wndclass)): - hwnd = CreateWindowEx(0, - wndclass.lpszClassName, - "DDE Server", - 0, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - self.master.winfo_id(), # Don't use HWND_MESSAGE since the window won't get DDE broadcasts - None, - wndclass.hInstance, - None) + hwnd = CreateWindowEx( + 0, + wndclass.lpszClassName, + "DDE Server", + 0, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + self.master.winfo_id(), # Don't use HWND_MESSAGE since the window won't get DDE broadcasts + None, + wndclass.hInstance, + None + ) msg = MSG() while GetMessage(byref(msg), None, 0, 0) != 0: logger.trace(f'DDE message of type: {msg.message}') @@ -201,7 +211,7 @@ elif sys.platform == 'win32' and getattr(sys, 'frozen', False) and not is_wine a if url.startswith(self.redirect): logger.debug(f'Message starts with {self.redirect}') self.event(url) - SetForegroundWindow(GetParent(self.master.winfo_id())) # raise app window + SetForegroundWindow(GetParent(self.master.winfo_id())) # raise app window PostMessage(msg.wParam, WM_DDE_ACK, hwnd, PackDDElParam(WM_DDE_ACK, 0x80, msg.lParam)) else: PostMessage(msg.wParam, WM_DDE_ACK, hwnd, PackDDElParam(WM_DDE_ACK, 0, msg.lParam)) @@ -213,7 +223,7 @@ elif sys.platform == 'win32' and getattr(sys, 'frozen', False) and not is_wine a else: print('Failed to register DDE for cAPI') -else: # Linux / Run from source +else: # Linux / Run from source from http.server import HTTPServer, BaseHTTPRequestHandler @@ -264,7 +274,7 @@ else: # Linux / Run from source self.send_response(200) return True else: - self.send_response(404) # Not found + self.send_response(404) # Not found return False def do_HEAD(self): @@ -275,7 +285,8 @@ else: # Linux / Run from source if self.parse(): self.send_header('Content-Type', 'text/html') self.end_headers() - self.wfile.write('{}'.format('Authentication successful').encode('utf-8')) + self.wfile.write( + '{}'.format('Authentication successful').encode('utf-8')) self.wfile.write('

{}

'.format('Authentication successful').encode('utf-8')) else: self.end_headers()