Skip to content

Instantly share code, notes, and snippets.

@AlexanderPavlenko
Created February 5, 2012 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlexanderPavlenko/1745061 to your computer and use it in GitHub Desktop.
Save AlexanderPavlenko/1745061 to your computer and use it in GitHub Desktop.
anti-spam for Skype
import Skype4Py
from time import sleep
skype = Skype4Py.Skype()
TRYING = 1
TEXT = {
'QUESTION':"Hello. It's antispam bot. Answer, 2+2=? One figure, please. You have %i trying." % (TRYING),
'ANSWER':"4",
'WIN':"Right. I'll write you soon.",
'AGAIN':"Try one more time.",
'FAIL':"Autoignoring.",
'INSTANT_BAN':"Autoignoring due to banned word in the authorization request or in the name."
}
BANNED = {
'WORDS':("http", "www"),
'NAMES':("Katherine Knox", "Shalonda POWELL", "Krysta MYERS", "Vicky GILLIAM", "Vonni Binford", "Cloris Mayr")
}
HIDE_MESSAGES_AFTER_WRONG_ANSWER = True # True may cause loss of the new answers
REFRESH_DELAY = 30 # in seconds
GET_NAME_DELAY = 1.5
queue = {}
def answer_received(msg, status):
username = msg._GetSender()._GetHandle()
if username in queue.keys():
if status==Skype4Py.cmsReceived:
if msg._GetBody()!=TEXT['ANSWER']:
if HIDE_MESSAGES_AFTER_WRONG_ANSWER:
skype.UnregisterEventHandler('MessageStatus', answer_received)
for eachMsg in msg._GetChat()._GetRecentMessages():
try:
eachMsg.MarkAsSeen()
except:
continue
skype.RegisterEventHandler('MessageStatus', answer_received)
queue[username] = queue[username]-1
if queue[username]:
skype.SendMessage(username, TEXT['AGAIN'])
return
else:
del queue[username]
skype.SendMessage(username, TEXT['FAIL'])
msg._GetSender()._SetIsBlocked(True)
else:
del queue[username]
skype.SendMessage(username, TEXT['WIN'])
if len(queue)==0:
skype.UnregisterEventHandler('MessageStatus', answer_received)
def hasBanned(txtstr, target):
for word in BANNED[target]:
if word in txtstr:
return True
return False
def auth_received(from_user):
newUsers = skype._GetUsersWaitingAuthorization()
if not newUsers:
for key in queue.keys():
del queue[key]
return
for user in newUsers:
username = user._GetHandle()
if not (username in queue.keys()):
if TRYING < 1:
skype.SendMessage(username, TEXT['FAIL'])
user._SetIsBlocked(True)
continue
if hasBanned(user._GetReceivedAuthRequest(), 'WORDS'):
skype.SendMessage(username, TEXT['INSTANT_BAN'])
user._SetIsBlocked(True)
continue
skype.RegisterEventHandler('MessageStatus', answer_received)
skype.SendMessage(username, TEXT['QUESTION'])
queue[username] = TRYING
sleep(GET_NAME_DELAY)
if hasBanned(user._GetFullName(), 'NAMES'): # Skype's bug: full name is available only after message sending
del queue[username]
skype.SendMessage(username, TEXT['INSTANT_BAN'])
user._SetIsBlocked(True)
if len(queue)==0:
skype.UnregisterEventHandler('MessageStatus', answer_received)
continue
skype.RegisterEventHandler('UserAuthorizationRequestReceived', auth_received)
while True:
skype.Attach()
auth_received(None)
sleep(REFRESH_DELAY)
@nisargshh
Copy link

nisargshh commented Jul 23, 2016

does this work for groups?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment