Skip to content

Commit

Permalink
Merge pull request #238 from SecSchool/fix-some-issues
Browse files Browse the repository at this point in the history
Fix issue #216 and test_ipc_path function for linux
  • Loading branch information
NikOverflow authored Mar 30, 2024
2 parents 9562a8c + 90cc54f commit 3af12f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 0 additions & 2 deletions pypresence/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def connect(self):
def close(self):
self.send_data(2, {'v': 1, 'client_id': self.client_id})
self.loop.close()
self.sock_writer.close()
if sys.platform == 'win32' or sys.platform == 'win64':
self.sock_writer._call_connection_lost(None)

Expand Down Expand Up @@ -85,6 +84,5 @@ async def connect(self):
def close(self):
self.send_data(2, {'v': 1, 'client_id': self.client_id})
self.loop.close()
self.sock_writer.close()
if sys.platform == 'win32' or sys.platform == 'win64':
self.sock_writer._call_connection_lost(None)
14 changes: 10 additions & 4 deletions pypresence/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import tempfile
import time
import socket

from .exceptions import PyPresenceException

Expand All @@ -23,8 +24,13 @@ def remove_none(d: dict):

def test_ipc_path(path):
'''Tests an IPC pipe to ensure that it actually works'''
with open(path):
return True
if sys.platform == 'win32' or sys.platform == 'win64':
with open(path):
return True
else:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
client.connect(path)
return True


# Returns on first IPC pipe matching Discord's
Expand All @@ -34,7 +40,7 @@ def get_ipc_path(pipe=None):
ipc = f"{ipc}{pipe}"

if sys.platform in ('linux', 'darwin'):
tempdir = (os.environ.get('XDG_RUNTIME_DIR') or tempfile.gettempdir())
tempdir = os.environ.get('XDG_RUNTIME_DIR') or (f"/run/user/{os.getuid()}" if os.path.exists(f"/run/user/{os.getuid()}") else tempfile.gettempdir())
paths = ['.', 'snap.discord', 'app/com.discordapp.Discord', 'app/com.discordapp.DiscordCanary']
elif sys.platform == 'win32':
tempdir = r'\\?\pipe'
Expand All @@ -46,7 +52,7 @@ def get_ipc_path(pipe=None):
full_path = os.path.abspath(os.path.join(tempdir, path))
if sys.platform == 'win32' or os.path.isdir(full_path):
for entry in os.scandir(full_path):
if entry.name.startswith(ipc) and os.path.exists(entry) and test_ipc_path(entry):
if entry.name.startswith(ipc) and os.path.exists(entry) and test_ipc_path(entry.path):
return entry.path


Expand Down

0 comments on commit 3af12f1

Please sign in to comment.