-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
188 lines (146 loc) · 7.45 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# COPYRIGHT (C) 2020-2024 Nicotine+ Contributors
# COPYRIGHT (C) 2011 quinox <quinox@users.sf.net>
#
# GNU GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pynicotine.pluginsystem import BasePlugin
class Plugin(BasePlugin):
PLACEHOLDERS = {
"%files%": "num_files",
"%folders%": "num_folders"
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.settings = {
"message": "Please consider sharing more files if you would like to download from me again. Thanks :)",
"open_private_chat": True,
"num_files": 1,
"num_folders": 1,
"detected_leechers": []
}
self.metasettings = {
"message": {
"description": ("Private chat message to send to leechers. Each line is sent as a separate message, "
"too many message lines may get you temporarily banned for spam!"),
"type": "textview"
},
"open_private_chat": {
"description": "Open chat tabs when sending private messages to leechers",
"type": "bool"
},
"num_files": {
"description": "Require users to have a minimum number of shared files:",
"type": "int", "minimum": 0
},
"num_folders": {
"description": "Require users to have a minimum number of shared folders:",
"type": "int", "minimum": 1
},
"detected_leechers": {
"description": "Detected leechers",
"type": "list string"
}
}
self.probed_users = {}
def loaded_notification(self):
min_num_files = self.metasettings["num_files"]["minimum"]
min_num_folders = self.metasettings["num_folders"]["minimum"]
if self.settings["num_files"] < min_num_files:
self.settings["num_files"] = min_num_files
if self.settings["num_folders"] < min_num_folders:
self.settings["num_folders"] = min_num_folders
self.log(
"Require users have a minimum of %d files in %d shared public folders.",
(self.settings["num_files"], self.settings["num_folders"])
)
def check_user(self, user, num_files, num_folders, source="server"):
if user not in self.probed_users:
# We are not watching this user
return
if self.probed_users[user] == "okay":
# User was already accepted previously, nothing to do
return
if self.probed_users[user] == "requesting_shares" and source != "peer":
# Waiting for stats from peer, but received stats from server. Ignore.
return
is_user_accepted = (num_files >= self.settings["num_files"] and num_folders >= self.settings["num_folders"])
if is_user_accepted or user in self.core.buddies.users:
if user in self.settings["detected_leechers"]:
self.settings["detected_leechers"].remove(user)
self.probed_users[user] = "okay"
if is_user_accepted:
self.log("User '%s' is okay, sharing %s files in %s folders.", (user, num_files, num_folders))
else:
self.log("Buddy '%s' is sharing %s files in %s folders. Not complaining.",
(user, num_files, num_folders))
if self.core.network_filter.is_user_banned(user):
self.core.network_filter.unban_user(user)
self.log("User '%s' was banned but now is okay, removed ban.", user)
self.send_private(user, "You have been unbanned", show_ui=self.settings["open_private_chat"], switch_page=False)
return
if not self.probed_users[user].startswith("requesting"):
# We already dealt with the user this session
return
if user in self.settings["detected_leechers"]:
# We already messaged the user in a previous session
self.probed_users[user] = "processed_leecher"
return
if (num_files <= 0 or num_folders <= 0) and self.probed_users[user] != "requesting_shares":
# SoulseekQt only sends the number of shared files/folders to the server once on startup.
# Verify user's actual number of files/folders.
self.log("User '%s' has no shared files according to the server, requesting shares to verify…", user)
self.probed_users[user] = "requesting_shares"
self.core.userbrowse.request_user_shares(user)
return
if self.settings["message"]:
log_message = ("Leecher detected, '%s' is only sharing %s files in %s folders. Will try to ban now "
"or after transfer…")
else:
log_message = ("Leecher detected, '%s' is only sharing %s files in %s folders. Will try to ban now "
"or after transfer…")
self.probed_users[user] = "pending_leecher"
self.log(log_message, (user, num_files, num_folders))
self.core.network_filter.ban_user(user)
def upload_queued_notification(self, user, virtual_path, real_path):
if user in self.probed_users:
return
self.probed_users[user] = "requesting_stats"
stats = self.core.users.watched.get(user)
if stats is None:
# Transfer manager will request the stats from the server shortly
return
if stats.files is not None and stats.folders is not None:
self.check_user(user, num_files=stats.files, num_folders=stats.folders)
def user_stats_notification(self, user, stats):
self.check_user(user, num_files=stats["files"], num_folders=stats["dirs"], source=stats["source"])
def upload_finished_notification(self, user, *_):
if user not in self.probed_users:
return
if self.probed_users[user] != "pending_leecher":
return
self.probed_users[user] = "processed_leecher"
if not self.settings["message"]:
self.log("Leecher %s doesn't share enough files. No message is specified in plugin settings.", user)
return
for line in self.settings["message"].splitlines():
for placeholder, option_key in self.PLACEHOLDERS.items():
# Replace message placeholders with actual values specified in the plugin settings
line = line.replace(placeholder, str(self.settings[option_key]))
self.send_private(user, line, show_ui=self.settings["open_private_chat"], switch_page=False)
self.core.network_filter.ban_user(user)
if user not in self.settings["detected_leechers"]:
self.settings["detected_leechers"].append(user)
self.log("Leecher '%s' doesn't share enough files. Message sent.", user)