1 # -*- coding: utf-8 -*-
2 import xmpp
, inspect
, re
6 def DEBUG(self
, text
=None):
7 '''Режим отладки и тестирования'''
9 self
.config_file
= 'nia_test.cfg'
13 admin_comm_pref
= 'admin_'
17 self
.config_file
= 'nia.cfg'
18 self
.resource
= 'Nia Teppelin .NET'
19 self
.version
= '0.666'
20 self
.os
= 'Windows Vista'
24 user
, confs
, ignore
, alias
= self
.config(False)
25 self
.JID
= user
['jid']
26 self
.PASSWD
= user
['passwd']
27 self
.NICK
= unicode(user
['nick'],'utf-8')
28 self
.admin
= xmpp
.protocol
.JID(user
['admin'])
34 self
.admin_commands
= {}
35 self
.help = {'com':[],'admin':[]}
36 for (name
, value
) in inspect
.getmembers(self
):
37 if inspect
.ismethod(value
) and name
.startswith(self
.comm_pref
):
38 self
.commands
[name
[len(self
.comm_pref
):]] = value
39 self
.help['com'].append(name
[len(self
.comm_pref
):])
40 if inspect
.ismethod(value
) and name
.startswith(self
.admin_comm_pref
):
41 self
.admin_commands
[name
[len(self
.admin_comm_pref
):]] = value
42 self
.help['admin'].append(name
[len(self
.admin_comm_pref
):])
43 self
.help = {'com':', '.join(self
.help['com']),'admin':', '.join(self
.help['admin'])}
47 def config(self
,flag
,confs
=None,ignore
=None):
48 config
= ConfigParser
.ConfigParser()
50 config
.add_section('alias')
51 for key
in self
.alias
:
52 config
.set('alias', key
, self
.alias
[key
])
53 config
.add_section('general')
54 config
.set('general', 'jid', self
.JID
)
55 config
.set('general', 'passwd', self
.PASSWD
)
56 config
.set('general', 'nick', self
.NICK
)
57 config
.set('general', 'admin', self
.admin
)
58 config
.set('general', 'ignore', ','.join(ignore
))
59 config
.set('general', 'confs', ','.join(confs
) )
60 config
.write(open(self
.config_file
,'w'))
63 config
.read(self
.config_file
)
64 user
= {'jid':config
.get('general','jid'),
65 'passwd':config
.get('general','passwd'),
66 'nick':config
.get('general','nick'),
67 'admin':config
.get('general','admin')}
68 confs
= config
.get('general','confs').decode('utf-8').split(',')
69 ignore
= config
.get('general','ignore').decode('utf-8').split(',')
70 for key
in config
.options('alias'):
71 alias
[key
] = config
.get('alias',key
)
72 return user
, confs
, ignore
, alias
80 '''Подключение к серверу'''
81 self
.jid
= xmpp
.protocol
.JID(self
.JID
)
82 self
.conn
=xmpp
.Client(self
.jid
.getDomain(),debug
=[])
84 self
.conn
.auth(self
.jid
.getNode(),self
.PASSWD
,'nyaa~')
85 self
.conn
.sendInitPresence()
86 self
.conn
.RegisterDisconnectHandler(self
.conn
.reconnectAndReauth
)
87 self
.conn
.RegisterHandler('message',self
.get_mes
)
88 self
.conn
.RegisterHandler('iq', self
.iq_version
, typ
='get', ns
=xmpp
.NS_VERSION
)
89 self
.conn
.RegisterHandler('iq', self
.get_iq
, typ
='result', ns
=xmpp
.NS_VERSION
)
91 def iq_version(self
, conn
, iq
):
92 """Returns reply to iq:version"""
93 iq
=iq
.buildReply('result')
95 qp
.setTagData('name', self
.resource
)
96 qp
.setTagData('version', self
.version
)
97 qp
.setTagData('os', self
.os
)
99 raise xmpp
.NodeProcessed
101 def join_room(self
, confs
):
103 self
.p
=xmpp
.Presence(to
='%s/%s'%(conf
,self
.NICK
))
104 self
.p
.setTag('Nia',namespace
=xmpp
.NS_MUC
).setTagData('password','')
105 self
.p
.getTag('Nia').addChild('history',{'maxchars':'0','maxstanzas':'0'})
106 self
.conn
.send(self
.p
)
107 def leave_room(self
, confs
):
109 to
= '%s/%s'%(conf
,self
.NICK
)
110 self
.send_system(to
,'offline','unavailable')
113 self
.join_room(self
.CONFS
)
116 self
.join_room(self
.CONFS
)
120 except xmpp
.protocol
.XMLNotWellFormed
:
124 def send(self
, text
, extra
=None, flag
=True):
130 self
.conn
.send(xmpp
.protocol
.Message(self
.to
,text
,self
.type))
132 '''Отправка сообщения в форме xhtml'''
135 <html xmlns='http://jabber.org/protocol/xhtml-im'>
136 <body xml:lang='en-US' xmlns='http://www.w3.org/1999/xhtml'>
141 self
.conn
.send("<message to='%s' type='%s'><body>%s</body>%s</message>"%(self
.to
,self
.type,text
,xhtml
))
143 def send_system(self
,to
,msg
,type):
144 '''Отправка системного сообщения. Статусы'''
146 self
.conn
.send(xmpp
.protocol
.Presence(to
=to
,status
=msg
,typ
=type))
148 def XMLescape(self
, text
):
149 return xmpp
.simplexml
.XMLescape(text
)
151 def get_mes(self
, conn
, mess
):
154 text
= re
.findall('^%s[\W]{0,2}[\s]{1,3}(.*?)$'%self
.NICK
,self
.text
)
156 text
= re
.findall('^(.*?)$',self
.text
)
159 tmp
= text
[0].split(' ',1)
160 if len(tmp
) >= 2: cmd
, args
= tmp
[0], tmp
[1]
161 elif len(tmp
) == 1: cmd
, args
= tmp
[0], ''
164 else: return False, False
165 def alias(cmd
, args
):
166 text
= ' '.join( (self
.alias
[cmd
], args
))
167 tmp
= text
.split(' ',1)
168 if len(tmp
) >= 2: cmd
, args
= tmp
[0], tmp
[1]
169 elif len(tmp
) == 1: cmd
, args
= tmp
[0], ''
172 self
.type=mess
.getType()
173 self
.nick
=mess
.getFrom()
174 self
.text
=mess
.getBody()
176 if self
.type == 'groupchat':
177 self
.to
= self
.nick
.getStripped()
178 nick
= self
.nick
.getResource()
180 elif self
.type == 'chat' and self
.nick
.getDomain().startswith('conference.'):
182 nick
= self
.nick
.getResource()
184 elif self
.type == 'chat':
185 self
.to
= self
.nick
.getStripped()
186 nick
= self
.nick
.getNode()
189 ''' if (user in CONFERENCES) or (user in [i+u'/'+jid.getResource() for i in CONFERENCES]) or (user in IGNORE) or (user.getStripped() in IGNORE) or (type(text)!=type(u'')):
191 self
.DEBUG([self
.nick
,self
.text
,self
.type])
193 if self
.ignore
.count(self
.nick
) or re
.match('%s/%s'%(self
.to
,self
.NICK
),'%s/%s'%(self
.to
,nick
) ):
195 elif self
.text
.startswith(self
.NICK
) or not self
.type_f
:
197 if self
.alias
.has_key(cmd
):
198 cmd
,args
= alias(cmd
, args
)
200 if self
.commands
.has_key(cmd
):
201 self
.commands
[cmd
](args
)
202 elif self
.admin_commands
.has_key(cmd
):
203 if nick
== self
.admin
.getNode() or self
.to
== str(self
.admin
).lower() :
204 self
.admin_commands
[cmd
](self
.nick
,args
)
205 else: self
.send('%s~ nyaaa? Access denied...'%nick
)
206 else: self
.send('%s~ nyaaa? Type "help"...'%nick
)
207 else: self
.send('%s~ nyaaa? Type "help"...'%nick
)
210 def send_iq(self
,_type
, to
):
211 self
.conn
.send(xmpp
.protocol
.Iq(to
=to
,typ
=_type
,queryNS
=xmpp
.NS_VERSION
))
212 def get_iq(self
,conn
,mess
):
213 query
= mess
.getTag('query')
214 client
= '%s %s'%(query
.getTagData('name'),query
.getTagData('version') )
215 os
= query
.getTagData('os')
216 target
= mess
.getFrom().getResource()
217 toversion
= '%s has client %s at %s'%(target
, client
, os
)
226 http://code.google.com/p/robocat/source/browse/trunk/start.py
227 http://www.linux.org.ru/view-message.jsp?msgid=2591531#2591657