4 #!/usr/bin/env python
# This is a simple purple notification server.
# It shows notifications when your buddy signs on or you get an IM message.
#
# This script requires Python 2.4 and PyGTK bindings
#
# Note that all function names are resolved dynamically, no
# purple-specific library is needed.
import dbus
import dbus.glib
import dbus.decorators
import gobject
import os
def myaccount():
for a in purple.PurpleAccountsGetAllActive():
protocolName = purple.PurpleAccountGetProtocolName(a)
#cmd="echo '{}' ".format(protocolName)
#os.system(cmd)
if purple.PurpleAccountGetProtocolName(a) == 'Discord':
#cmd="echo '{}' ".format(a)
#os.system(cmd)
break
return a
def mybuddy(account):
for online in purple.PurpleFindBuddies( account, '' ):
#cmd="echo '{}' ".format(online)
#os.system(cmd)
name_buddy=purple.PurpleBuddyGetAlias( online ).encode('utf-8')
if name_buddy=='prapop':
#cmd="echo '{}' ".format(name_buddy)
#os.system(cmd)
break
buddy=purple.PurpleBuddyGetName( online )
return buddy
def sendmyim(buddy,text):
if buddy!=0:
buddycheck=purple.PurpleBuddyGetName( buddy )
else:
buddycheck="12345674890"
f = open('block.txt', 'r+')
block_list = [line for line in f.readlines()]
f.close()
res = any(buddycheck.encode('utf-8') in ele for ele in block_list)
if not res:
buddyid = purple.PurpleBuddyGetName(buddy)
name_buddy =purple.PurpleBuddyGetAlias(buddy).encode('utf-8')
f=open('lastnotice.txt','w')
f.write('{} {}' .format(name_buddy,buddyid))
f.close
account=myaccount()
buddy=mybuddy(account)
conv = purple.PurpleConversationNew(1, account,buddy.encode('utf-8'))
im = purple.PurpleConvIm(conv)
purple.PurpleConvImSend(im, text.encode('utf-8'))
def ensureimconversation(conversation, account, name):
if conversation != 0:
return conversation
else:
# 1 = PURPLE_CONV_IM
return purple.PurpleConversationNew(1, account, name)
def receivedimmsg(account, name, message, conversation, flags):
buddy = purple.PurpleFindBuddy(account, name)
if buddy != 0:
alias = purple.PurpleBuddyGetAlias(buddy)
else:
alias = name
if "block it" in message:
f=open('lastnotice.txt','r')
text=f.read()
f.close
w=open('block.txt','a')
w.write("%s\n" % (text) )
w.close
buddy = 0
text = "block %s" % (text)
else:
text = "%s says %s" % (alias, message)
conversation = ensureimconversation(conversation, account, name)
purple.PurpleConversationDestroy(conversation)
#cmd="echo '{}' ".format(text.encode('utf-8'))
#os.system(cmd)
sendmyim(buddy,text)
def buddysignedon(buddyid):
alias = purple.PurpleBuddyGetAlias(buddyid)
text = "%s is online" % alias
#cmd="echo '{}' ".format(text.encode('utf-8'))
#os.system(cmd)
sendmyim(buddyid,text)
def buddysignedoff(buddyid):
alias = purple.PurpleBuddyGetAlias(buddyid)
text = "%s is offline" % alias
sendmyim(buddyid,text)
def buddychange(buddyid,newstatus,oldstatus):
alias = purple.PurpleBuddyGetAlias(buddyid)
new=purple.PurpleStatusGetName(newstatus)
old=purple.PurpleStatusGetName(oldstatus)
text = "%s change %s to %s" % (alias,new,old)
if new==old:
text="%s return from idle" % (alias)
sendmyim(buddyid,text)
def buddyidle(buddyid,newstatus,oldstatus):
alias = purple.PurpleBuddyGetAlias(buddyid)
text = "%s is idle" % alias
sendmyim(buddyid,text)
bus = dbus.SessionBus()
obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
bus.add_signal_receiver(receivedimmsg,
dbus_interface = "im.pidgin.purple.PurpleInterface",
signal_name = "ReceivedImMsg")
bus.add_signal_receiver(buddysignedon,
dbus_interface = "im.pidgin.purple.PurpleInterface",
signal_name = "BuddySignedOn")
bus.add_signal_receiver(buddysignedoff,
dbus_interface = "im.pidgin.purple.PurpleInterface",
signal_name = "BuddySignedOff")
bus.add_signal_receiver(buddychange,
dbus_interface = "im.pidgin.purple.PurpleInterface",
signal_name = "BuddyStatusChanged")
bus.add_signal_receiver(buddyidle,
dbus_interface = "im.pidgin.purple.PurpleInterface",
signal_name = "BuddyIdleChanged")
print "This is a simple purple notification server."
print "It shows notifications when your buddy signs on or you get an IM message."
loop = gobject.MainLoop()
loop.run()