usr bin env python coding utf-8 SleekXMPP The Sleek XMPP Library Copyr

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
SleekXMPP: The Sleek XMPP Library
Copyright (C) 2010 Nathanael C. Fritz
This file is part of SleekXMPP.
See the file LICENSE for copying permission.
"""
NICKNAME = 'caufman'
JID = 'caufman@jabber.ru'
PASSWORD = '*****' # здесь надо вписать свой пасворд
import sys
import logging
import getpass
from optparse import OptionParser
import re
import sleekxmpp
# Python versions before 3.0 do not use UTF-8 encoding
# by default. To ensure that Unicode is handled properly
# throughout SleekXMPP, we will set the default encoding
# ourselves to UTF-8.
if sys.version_info < (3, 0):
from sleekxmpp.util.misc_ops import setdefaultencoding
setdefaultencoding('utf8')
else:
raw_input = input
del_finder = re.compile(r'Message #[0-9]+ deleted.')
def no_tag(xml):
tag_finder = re.compile(r'<[^<>]+>')
tags = tag_finder.findall(xml)
#~ print (tags, '\n'+'-'*79)
text = xml
for i in tags:
text = text.replace(i, '')
while text[0] == ' ':
text = text[1:]
print (text, '\n'+'-'*79)
return text
def get_nums(text):
num_finder = re.compile(r'#[0-9]+')
nums = num_finder.findall(text)
return nums
def get_usr(text):
usr_finder = re.compile(r'@.+')
usrs = usr_finder.findall(text)
print(usrs)
return usrs
def get_tags(text):
tag_finder = re.compile(r'\*.+')
tags = tag_finder.findall(text)
print(tags)
return tags
class SendMsgBot(sleekxmpp.ClientXMPP):
"""
A basic SleekXMPP bot that will log in, send a message,
and then log out.
"""
def __init__(self, jid, password, recipient):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
# The message we wish to send, and the JID that
# will receive it.
self.recipient = recipient
#~ self.msg = message
# The session_start event will be triggered when
# the bot establishes its connection with the server
# and the XML streams are ready for use. We want to
# listen for this event so that we we can initialize
# our roster.
self.add_event_handler("session_start", self.start, threaded=True)
self.add_event_handler("message", self.in_mess)
def start(self, event):
"""
Process the session_start event.
Typical actions for the session_start event are
requesting the roster and broadcasting an initial
presence stanza.
Arguments:
event -- An empty dictionary. The session_start
event does not provide any additional
data.
"""
print('Connect to', self.jid, '...')
self.send_presence()
self.get_roster()
print('... done')
def send_m(self, message):
self.msg = message
self.send_message(mto=self.recipient,
mbody=self.msg,
mtype='chat')
def in_mess(self, msg):
if msg['type'] in ('chat', 'normal'):
text = no_tag(('%s' % msg))
if 'Blog:' in text.splitlines()[0]:
for i in get_nums(text):
self.send_m('! '+i)
self.send_m('D '+i)
self.send_m('@'+NICKNAME)
#~ if del_finder(text):
#~ print('Сообщение '+get_nums(text)[0]+' удалено')
#~ elif 'You are subscribed' in text:
#~ for i in get_usr(text):
#~ self.send_m('U '+i)
#~ for j in get_tags(text):
#~ self.send_m('U '+j)
#~ self.send_m('S')
def disconnect(self):
# Using wait=True ensures that the send queue will be
# emptied before ending the session.
self.disconnect(wait=True)
if __name__ == '__main__':
# Setup logging.
#~ logging.basicConfig(level='DEBUG', format='%(levelname)-8s %(message)s')
xmpp = SendMsgBot(JID, PASSWORD, 'juick@juick.com')
for i in range(1):
xmpp.send_m('@'+NICKNAME)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
if xmpp.connect():
xmpp.process(block=True)
xmpp.disconnect()
print("Done")
else:
print("Unable to connect.")