Friday, March 5, 2021

Example(2) to send status to discord

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()


Example(1) to send status to discord

prepare python-dbus, python-gobject 


 #!/usr/bin/env python


import dbus

import os

from dbus.mainloop.glib import DBusGMainLoop


class CheckedObject:

    def __init__(self, obj):

        self.obj = obj


    def __getattr__(self, attr):

        return CheckedAttribute(self, attr)


class CheckedAttribute:

    def __init__(self, cobj, attr):

        self.cobj = cobj

        self.attr = attr


    def __call__(self, *args):

        result = self.cobj.obj.__getattr__(self.attr)(*args)

        if result == 0:

            raise Exception("Error: %s %s returned %s" %

                            (self.attr, args, result))

        return result


dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

bus = dbus.SessionBus()

obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")

purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")


for a in purple.PurpleAccountsGetAllActive():

    protocolName = purple.PurpleAccountGetProtocolName(a)

    cmd="echo '{}' ".format(protocolName)

    os.system(cmd)

    if purple.PurpleAccountGetProtocolName(a) == 'Discord':

        account = a

        cmd="echo '{}' ".format(a)

        os.system(cmd)


# account=396


name = purple.PurpleAccountGetUsername(account)

cmd="echo '{}' ".format(name)

os.system(cmd)


# for online in purple.PurpleFindBuddies( (purple.PurpleAccountsGetAllActive())[0], '' ):


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 )

cmd="echo '{}' ".format(buddy)

os.system(cmd)

conv = purple.PurpleConversationNew(1, account,buddy)

im = purple.PurpleConvIm(conv)

msg='test'

# purple.PurpleConvImSend(im, msg)


cpurple=CheckedObject(purple)

xx=cpurple.PurpleAccountGetConnection(account)

# msginfo=purple.ServGetInfo(33043,buddy)

cmd="echo '{}' ".format(xx)

os.system(cmd)