Migrate xmpp-caps.xml to XDG cache dir
[pidgin-git.git] / libpurple / purple-notifications-example
blobdd744a6933813fea32b6feaf9704ed484d43320a
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 from __future__ import absolute_import, division, print_function
13 import os
15 import dbus
16 import dbus.glib
17 import dbus.decorators
18 import gobject
21 def ensureimconversation(conversation, account, name):
22 if conversation != 0:
23 return conversation
24 else:
25 # 1 = PURPLE_CONV_IM
26 return purple.PurpleConversationNew(1, account, name)
29 def receivedimmsg(account, name, message, conversation, flags):
30 buddy = purple.PurpleFindBuddy(account, name)
31 if buddy != 0:
32 alias = purple.PurpleBuddyGetAlias(buddy)
33 else:
34 alias = name
36 text = "%s says %s" % (alias, message)
37 code = os.spawnlp(os.P_WAIT, "xmessage", "xmessage", "-buttons",
38 "'So what?','Show me',Close,Abuse", text)
40 if code == 101: # so what?
41 pass
42 else:
43 conversation = ensureimconversation(conversation, account, name)
45 if code == 102: # show me
46 window = purple.PurpleConversationGetWindow(conversation)
47 purple.PurpleConvWindowRaise(window)
49 if code == 103: # close
50 purple.PurpleConversationDestroy(conversation)
52 if code == 104: # abuse
53 im = purple.PurpleConversationGetImData(conversation)
54 purple.PurpleConvImSend(im, "Go away you f...")
57 def buddysignedon(buddyid):
58 alias = purple.PurpleBuddyGetAlias(buddyid)
59 text = "%s is online" % alias
61 code = os.spawnlp(os.P_WAIT, "xmessage", "xmessage", "-buttons",
62 "'So what?','Let's talk'", text)
64 if code == 101: # so what?
65 pass
67 if code == 102: # talk
68 name = purple.PurpleBuddyGetName(buddyid)
69 account = purple.PurpleBuddyGetAccount(buddyid)
70 purple.PurpleConversationNew(1, account, name)
73 bus = dbus.SessionBus()
74 obj = bus.get_object("im.pidgin.purple.PurpleService",
75 "/im/pidgin/purple/PurpleObject")
76 purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
78 bus.add_signal_receiver(receivedimmsg,
79 dbus_interface="im.pidgin.purple.PurpleInterface",
80 signal_name="ReceivedImMsg")
82 bus.add_signal_receiver(buddysignedon,
83 dbus_interface="im.pidgin.purple.PurpleInterface",
84 signal_name="BuddySignedOn")
86 print("This is a simple purple notification server.")
87 print("It shows notifications when your buddy signs on or you get an "
88 "IM message.")
90 loop = gobject.MainLoop()
91 loop.run()