FIXED: errors when imitating someone whose status is not Online
[emesene_imitate.git] / Imitate.py
blob0579886788c269c92e6aa77f9c7d34a1ed1bb878
1 # -*- coding: utf-8 -*-
2 '''A plug-in that "imitates" someone. It will copy nick, message, picture
3 from that person and auto-update it. Not so useful, but very funny! ;-)
4 '''
6 VERSION = '0.2'
7 import os
8 #import sys
9 import time
10 #import socket
12 #import emesenelib.common
14 import Plugin
15 import status
17 class MainClass( Plugin.Plugin ):
18 '''Main plugin class'''
19 def __init__(self, controller, msn):
20 '''constructor'''
21 Plugin.Plugin.__init__( self, controller, msn )
22 self.description = _('Copy nick, PM, image and status from someone and autoupdates')
23 self.authors = { 'BoySka' : 'boyska gmail com' }
24 self.website = 'http://emesene.org'
25 self.displayName = _('Imitate')
26 self.name = 'Imitate'
29 self.controller = controller
30 self.msn = msn
32 self.user = None #user we are imitating
33 self.saved_status = {}
35 self.cb_ids = {} #callback ids
37 #Plugin methods
39 def start( self ):
40 '''start the plugin'''
41 #self._set_nick("prova")
42 self.controller.Slash.register('imitate', self.slash_imitate,
43 _('Imitates someone'))
45 self.cb_ids['nick-changed'] = self.connect(
46 'nick-changed', self.on_nick_changed)
47 self.cb_ids['message-changed'] = self.connect(
48 'personal-message-changed', self.on_message_changed)
49 self.cb_ids['picture-changed'] = self.connect(
50 'display-picture-changed', self.on_picture_changed)
52 self.enabled = True
54 def stop( self ):
55 '''stop the plugin'''
56 self.controller.Slash.unregister('imitate')
57 self.disconnect(self.cb_ids['nick-changed'])
58 self.disconnect(self.cb_ids['message-changed'])
59 self.disconnect(self.cb_ids['picture-changed'])
60 self.enabled = False
62 def check( self ):
63 '''check if the plugin can be enabled'''
64 return ( True, 'Ok' )
66 #Callbacks
67 def slash_imitate(self, slash_action):
68 '''callback used when /imitate is used'''
69 if not self.user:
70 self._save_status()
73 if slash_action.params == 'stop':
74 self.user = None
75 self._revert_status()
76 return
78 if not slash_action.params:
79 user = slash_action.conversation.getMembers()[0]
80 else:
81 user = slash_action.params
83 self.user = user
85 self._imitate_nick(user)
86 self._imitate_message(user)
87 self._imitate_status(user)
88 self._imitate_picture(user)
90 def on_nick_changed(self, msn, user, nick):
91 '''callback used when someone changes his nick'''
92 if user and user == self.user:
93 self._set_nick(nick)
95 def on_message_changed(self, msn, user, message):
96 '''callback used when someone changes his message'''
97 if user and user == self.user:
98 time.sleep(1)
99 self._set_message(message)
101 def on_picture_changed(self, msn, user, creator, email):
102 '''callback used when someone changes his picture'''
103 if user and user == self.user:
104 time.sleep(1)
105 self._imitate_picture(email)
108 #Internals
109 def _save_status(self):
110 '''saves user nick, message and picture in self.saved_status'''
111 saved = self.saved_status
112 saved['nick'] = self.controller.contacts.get_nick()
113 saved['message'] = self.controller.contacts.get_message()
114 saved['picture'] = self.controller.avatar.getImagePath()
115 #saved['status'] = status.STATUS_TO_MSN[self.controller.contacts.get_status()]
116 saved['status'] = self.controller.contacts.get_status() or 'NLN'
118 def _revert_status(self):
119 '''set nick, message and picture to the ones
120 saved in self.saved_status'''
121 saved = self.saved_status
122 self._set_nick(saved['nick'])
123 self._set_message(saved['message'])
124 #self._set_picture(os.path.join(
125 # self.controller.config.getAvatarsCachePath(),
126 # saved['picture']))
127 self._set_picture(saved['picture'])
128 self._set_status(saved['status'])
131 def _imitate_nick(self, user):
132 '''imitates the nick of user'''
133 nick = self.controller.contacts.get_nick(user)
134 self._set_nick(nick)
136 def _imitate_message(self, user):
137 '''imitates the message of user'''
138 contact = self.msn.contactManager.getContact(user)
139 message = contact.personalMessage
141 self._set_message(message)
143 def _imitate_status(self, user):
144 '''imitates the status of user'''
145 contact = self.msn.contactManager.getContact(user)
146 stat = contact.status
148 self._set_status(stat)
150 def _imitate_picture(self, user):
151 '''imitates the message of user'''
152 contact = self.msn.contactManager.getContact(user)
153 picture = contact.displayPicturePath
154 if picture:
155 picture_path = os.path.join(
156 self.controller.config.getCachePath(), picture)
157 self._set_picture(picture_path)
159 def _set_message(self, message):
160 '''set our message to message'''
161 self.controller.contacts.set_message(message)
163 def _set_status(self, status):
164 '''set our status to status'''
165 self.controller.contacts.set_status(status)
167 def _set_picture(self, picture_path):
168 '''set our picture to picture_path'''
169 self.controller.changeAvatar(picture_path)
171 def _set_nick(self, nick):
172 '''set our nick to nick'''
173 self.controller.contacts.set_nick(nick)