-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanageRedirisSpam.py
executable file
·327 lines (273 loc) · 10.4 KB
/
manageRedirisSpam.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
import configparser
import os
import sys
import re
import logging
import keyring
import getpass
import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
optTxt = {
'' : 'No messages',
'n': 'Nothing to do',
'a': 'Deleting all'
}
def makeConnection(SERVER, USER, PASSWORD):
url = 'https://'+SERVER+'/'
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = '/usr/bin/chromium-browser'
driver = webdriver.Chrome(executable_path= os.path.expanduser('~/usr/local/bin/chromedriver') , chrome_options=chrome_options)
driver.get(url)
time.sleep(1)
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), '/tmp', 'kk1.png'))
elemU = driver.find_element_by_name("username")
#while elemU:
print("Identifying...")
elemP = driver.find_element_by_name("password")
elemU.clear()
elemU.send_keys(USER)
elemP.clear()
elemP.send_keys(PASSWORD)
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), '/tmp', 'kk2.png'))
elemP.send_keys(Keys.RETURN)
time.sleep(1)
# try:
# elemU = driver.find_element_by_name("username").clear()
# except:
# elemU = None
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), '/tmp', 'kk3.png'))
return driver
def getPassword(server, user):
# Deleting keyring.delete_password(server, user)
password = keyring.get_password(server, user)
if not password:
logging.info("[%s,%s] New account. Setting password" % (server, user))
password = getpass.getpass()
keyring.set_password(server, user, password)
return password
def selectCategory(logging):
categories = ['showSpam', 'showValidMail', 'showPendingValidationMail', 'showMailingList', 'showVirusWarnings', 'showNotifications', 'showTrash']
i = 0
for cat in categories:
print("%d) %s"% (i, cat))
i = i + 1
sel = input("Category? ")
if int(sel) < len(categories):
return categories[int(sel)]
else:
return ""
def selectCategoryLink(logging, catName, links):
i = 0
j = -1
for link in links:
if link['href'].find('action=show')>0:
if link['href'].find(catName)>0:
j = i
cat = catName
i = i + 1
return links[j]
def getMessage(logging, browser, link, number, sel):
browser.follow_link(link)
forms = browser.get_forms()
listMsg = []
selTen = sel % 10
if len(forms) >= 4:
form = forms[3]
options = list(form['mails[]'].options)
options.reverse()
logging.debug("Message ids %s" % options)
trList = browser.find_all("tr")
subjects = {}
i = 0
for row in trList:
cellsS = row.find_all("td", { "class" : "subject clickable"})
cellsA = row.find_all("td", { "class" : "sender clickable"})
if cellsS:
if (i == selTen):
break
else:
i = i + 1
options.pop()
return options.pop(), cellsA[0]['title'], cellsS[0]['title']
def listMessages(logging, driver):
tr = []
i = 0
while(not tr):
if True:
time.sleep(30)
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), '/tmp', 'kk3'+str(i)+'.png'))
title = driver.title
posIni = title.find('(')+1
posFin = title.find(')')
with open("/tmp/%s-%s.html" %
(datetime.date.today().isoformat(), time.time()), "w") as f:
f.write(driver.page_source)
if posIni > 0:
numMsgs = int(title[posIni:posFin])
else:
numMsgs = 0
print("There are ... %d spam messages" % numMsgs)
elements = driver.find_elements_by_class_name("ng-binding")
numMax = 0
for element in elements:
if hasattr(element, 'text'):
if element.text.find(' of ')>0:
#print(element.text)
posIni = element.text.find('-')
posFin = element.text.find(' ')
#print(posIni, posFin)
numMax = int(element.text[posIni+1:posFin])
break
#print("seguimos numMax %d" % numMax)
tr = driver.find_elements_by_tag_name('td')
listMsg = []
#print(len(tr))
for i in range(min(numMax,numMsgs)):
num = 5*i
listMsg.append((tr[num], tr[num+1].text,tr[num+2].text, tr[num+3].text, tr[num+4].text))
else:
print("Wait...")
tr = []
time.sleep(3)
i = i + 1
return listMsg
def showMessages(logging, listMsg):
i = 0
numMsg = len(listMsg)
#if numMsg > 10:
# numMsg = 10
print("")
for row in listMsg[0:numMsg]:
print("%2d) %-10s %-40s" % (i, listMsg[i][1][:10].ljust(10), listMsg[i][2][:40].ljust(40)))
i = i + 1
#if i % 10 == 0:
# print("---------------------------")
def selectMessageText(logging, browser, text, link):
links = link
(listMsg, form, linkMsg) = listMessages(logging, browser, links)
for line in listMsg:
if (line[2].find(text)>=0):
return (line, linkMsg)
return ("","")
def selectMessages(logging, browser, link):
links = link
sel = '10'
validSel = False
while not validSel:
(listMsg, form, linkMsg) = listMessages(logging, browser, links)
logging.debug("---> %s" % listMsg)
if listMsg:
showMessages(logging, listMsg)
sel = input("Message? (number for message to be moved to valid/spam mail, 'a' for deleting all messages shown) ")
if (sel in 'an') or (sel.isdigit() and int(sel) + 1 <= len(listMsg)):
validSel = True
else:
return("", [], listMsg, linkMsg)
return (sel, form, listMsg, linkMsg)
def getCommands(logging, driver):
actionLinks = driver.find_elements_by_class_name('fActionLink')
# Identificar instrucciones relevantes
commands = {}
for tri in actionLinks:
operation = tri.get_attribute('data-ng-click')
#print(operation)
if tri.is_displayed():
if operation == 'deleteMessage()':
commands['delete'] = tri
elif operation == 'toggleSelectAll()':
commands['select'] = tri
elif operation == 'refreshMailbox()':
commands['refresh'] = tri
elif operation == 'releaseMessages()':
commands['release'] = tri
elif operation == 'loadNextPage()':
commands['next'] = tri
elif operation == 'loadPreviousPage()':
commands['prev'] = tri
return(commands)
def main():
config = configparser.ConfigParser()
config.read([os.path.expanduser('~/.mySocial/config/Web.cfg')])
rows, columns = os.popen('stty size', 'r').read().split()
i = 1
print("Configured accounts:")
for section in config.sections():
print('%s) %s' % (str(i), section))
i = i + 1
selection = input('Select one: ')
logging.basicConfig(#filename='example.log',
level=logging.INFO,format='%(asctime)s %(message)s')
SERVER = config.get(config.sections()[int(selection) - 1], 'server')
USER = config.get(config.sections()[int(selection) - 1], 'user')
PASSWORD = getPassword(SERVER, USER)
driver = makeConnection(SERVER, USER, PASSWORD)
PASSWORD = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
listMsg = listMessages(logging, driver)
#print(driver.page_source)
while listMsg:
showMessages(logging, listMsg)
commands = getCommands(logging, driver)
rep = input("Borrar todos? (s/n) ")
if (rep == 's'):
print("Borraré")
commands['select'].click()
time.sleep(2)
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), '/tmp', 'kkSelAll.png'))
commands = getCommands(logging, driver)
commands['delete'].click()
elif rep.isdigit():
print("Salvar %s" % rep)
print(listMsg[int(rep)])
listMsg[int(rep)][0].click()
time.sleep(2)
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), '/tmp', 'kkSelect.png'))
commands = getCommands(logging, driver)
commands['release'].click()
time.sleep(1)
elif rep == 'n':
sys.exit()
commands['refresh'].click()
time.sleep(1)
listMsg = listMessages(logging, driver)
if __name__ == '__main__':
main()
# compactFolder()
# markAllAsRead()
# showEmptyConfirmationDialog()
# toggleselectall()
# refreshMailbox()
# releaseMessages()
# None
# markAsRead(true)
# markAsRead(false)
# addToWhiteBlackList(true)
# addToWhiteBlackList(false)
# saveAsMessage()
# None
# loadFirstPage()
# loadLastPage()
# loadPreviousPage()
# loadNextPage()
# None
# selectQuickFilter(filter)
# selectQuickFilter(filter)
# selectQuickFilter(filter)
# deleteMessage()
#<ul data-ng-show="isMailMenuVisible()" class="nav mails-top-links navbar-right" style="margin-right: 12px">
# <li data-ng-style="{'visibility': loading?'hidden':'visible'}" style="visibility: visible;">
# <span class="fActionLink" data-ng-click="deleteMessage()" data-ng-show="!deleteInProgress" title="">
# <i class="fa fa-trash"></i>
# <!-- ngIf: !uiMainState.isSmallDesktop --><span data-ng-if="!uiMainState.isSmallDesktop" class="ng-binding ng-scope"> Borrar</span><!-- end ngIf: !uiMainState.isSmallDesktop -->
# </span>
# <span class="disabled ng-hide" data-ng-show="deleteInProgress">
# <i class="fa fa-trash"></i>
# <!-- ngIf: !uiMainState.isSmallDesktop --><span data-ng-if="!uiMainState.isSmallDesktop" class="ng-binding ng-scope"> Borrar</span><!-- end ngIf: !uiMainState.isSmallDesktop -->
# </span>
# </li>
# </ul>
#css=.nav:nth-child(6) .fActionLink > .ng-binding