annotate src/gaim-notifications-example.py @ 12037:d799f242be3f

[gaim-migrate @ 14330] Documentation tyop fix from Evan Schoenberg. committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Thu, 10 Nov 2005 16:51:37 +0000
parents 64fadbf3810f
children ac5bc9a7b603
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11331
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
1 #!/usr/bin/env python
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
2
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
3 # This is a simple gaim notification server.
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
4 # It shows notifications when your buddy signs on or you get an IM message.
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
5 #
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
6 # This script requires Python 2.4 and PyGTK bindings
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
7 #
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
8 # Note that all function names are resolved dynamically, no
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
9 # gaim-specific library is needed.
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
10
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
11 import dbus
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
12 import dbus.glib
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
13 import dbus.decorators
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
14 import gobject
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
15 import os
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
16
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
17 def ensureimconversation(conversation, account, name):
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
18 if conversation != 0:
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
19 return conversation
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
20 else:
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
21 # 1 = GAIM_CONV_IM
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
22 return gaim.GaimConversationNew(1, account, name)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
23
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
24 def receivedimmsg(account, name, message, conversation, flags):
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
25 buddy = gaim.GaimFindBuddy(account, name)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
26 if buddy != 0:
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
27 alias = gaim.GaimBuddyGetAlias(buddy)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
28 else:
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
29 alias = name
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
30
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
31 text = "%s says %s" % (alias, message)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
32 code = os.spawnlp(os.P_WAIT, "xmessage", "xmessage", "-buttons",
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
33 "'So what?','Show me',Close,Abuse", text)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
34
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
35 if code == 101: # so what?
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
36 pass
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
37 else:
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
38 conversation = ensureimconversation(conversation, account, name)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
39
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
40 if code == 102: # show me
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
41 window = gaim.GaimConversationGetWindow(conversation)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
42 gaim.GaimConvWindowRaise(window)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
43
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
44 if code == 103: # close
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
45 gaim.GaimConversationDestroy(conversation)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
46
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
47 if code == 104: # abuse
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
48 im = gaim.GaimConversationGetImData(conversation)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
49 gaim.GaimConvImSend(im, "Go away you f...")
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
50
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
51
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
52 def buddysignedon(buddyid):
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
53 alias = gaim.GaimBuddyGetAlias(buddyid)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
54 text = "%s is online" % alias
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
55
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
56 code = os.spawnlp(os.P_WAIT, "xmessage", "xmessage", "-buttons",
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
57 "'So what?','Let's talk'", text)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
58
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
59 if code == 101: # so what?
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
60 pass
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
61
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
62 if code == 102: # let's talk
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
63 name = gaim.GaimBuddyGetName(buddyid)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
64 account = gaim.GaimBuddyGetAccount(buddyid)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
65 gaim.GaimConversationNew(1, account, name)
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
66
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
67
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
68 bus = dbus.SessionBus()
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
69 obj = bus.get_object("org.gaim.GaimService", "/org/gaim/GaimObject")
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
70 gaim = dbus.Interface(obj, "org.gaim.GaimInterface")
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
71
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
72 bus.add_signal_receiver(receivedimmsg,
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
73 dbus_interface = "org.gaim.GaimInterface",
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
74 signal_name = "ReceivedImMsg")
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
75
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
76 bus.add_signal_receiver(buddysignedon,
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
77 dbus_interface = "org.gaim.GaimInterface",
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
78 signal_name = "BuddySignedOn")
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
79
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
80 print """This is a simple gaim notification server.
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
81 It shows notifications when your buddy signs on or you get an IM message."""
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
82
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
83 loop = gobject.MainLoop()
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
84 loop.run()
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
85
64fadbf3810f [gaim-migrate @ 13544]
Piotr Zielinski <zielaj>
parents:
diff changeset
86