merge of 'd03c32ee4bd5625c28af8c8b33920944d499eef6'
[pidgin-git.git] / libpurple / purple-notifications-example
blob2ecfeefad795fbb01c31e75f247972183f9194dc
1 #!/usr/bin/env python
3 # This is a simple purple notification server.
4 # It shows notifications when your buddy signs on or you get an IM message.
6 # This script requires Python 2.4 and PyGTK bindings
8 # Note that all function names are resolved dynamically, no
9 # purple-specific library is needed.
11 import dbus
12 import dbus.glib
13 import dbus.decorators
14 import gobject
15 import os
17 def ensureimconversation(conversation, account, name):
18 if conversation != 0:
19 return conversation
20 else:
21 # 1 = PURPLE_CONV_IM
22 return purple.PurpleConversationNew(1, account, name)
24 def receivedimmsg(account, name, message, conversation, flags):
25 buddy = purple.PurpleFindBuddy(account, name)
26 if buddy != 0:
27 alias = purple.PurpleBuddyGetAlias(buddy)
28 else:
29 alias = name
31 text = "%s says %s" % (alias, message)
32 code = os.spawnlp(os.P_WAIT, "xmessage", "xmessage", "-buttons",
33 "'So what?','Show me',Close,Abuse", text)
35 if code == 101: # so what?
36 pass
37 else:
38 conversation = ensureimconversation(conversation, account, name)
40 if code == 102: # show me
41 window = purple.PurpleConversationGetWindow(conversation)
42 purple.PurpleConvWindowRaise(window)
44 if code == 103: # close
45 purple.PurpleConversationDestroy(conversation)
47 if code == 104: # abuse
48 im = purple.PurpleConversationGetImData(conversation)
49 purple.PurpleConvImSend(im, "Go away you f...")
52 def buddysignedon(buddyid):
53 alias = purple.PurpleBuddyGetAlias(buddyid)
54 text = "%s is online" % alias
56 code = os.spawnlp(os.P_WAIT, "xmessage", "xmessage", "-buttons",
57 "'So what?','Let's talk'", text)
59 if code == 101: # so what?
60 pass
62 if code == 102: # talk
63 name = purple.PurpleBuddyGetName(buddyid)
64 account = purple.PurpleBuddyGetAccount(buddyid)
65 purple.PurpleConversationNew(1, account, name)
68 bus = dbus.SessionBus()
69 obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
70 purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
72 bus.add_signal_receiver(receivedimmsg,
73 dbus_interface = "im.pidgin.purple.PurpleInterface",
74 signal_name = "ReceivedImMsg")
76 bus.add_signal_receiver(buddysignedon,
77 dbus_interface = "im.pidgin.purple.PurpleInterface",
78 signal_name = "BuddySignedOn")
80 print "This is a simple purple notification server."
81 print "It shows notifications when your buddy signs on or you get an IM message."
83 loop = gobject.MainLoop()
84 loop.run()