This repository has been archived by the owner on Oct 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbchatlib.py
179 lines (154 loc) · 6.08 KB
/
fbchatlib.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
import sys, os, threading, re, gobject
from datetime import datetime
def get_facebook_client():
import facebook
# Replace these with your app's credentials
api_key = '147201912033054'
secret_key = '61ef5734be20d29715ef1133e9456834'
client = facebook.Facebook(api_key, secret_key)
try:
# Try to read cached credentials from the session-key file.
# If authorization fails, you should delete this file and start of.
handle = open('session-key', 'r')
client.uid, client.session_key, client.secret = [ line.strip() for line in handle ]
handle.close()
except IOError:
print client.auth.createToken()
client.login()
print 'Log in to the app in your browser, then press enter.'
while(1):
try:
client.auth.getSession()
break
except:
pass
handle = open('session-key', 'w')
print >> handle, client.uid
print >> handle, client.session_key
print >> handle, client.secret
handle.close()
return client
from pyxmpp.sasl.core import ClientAuthenticator
from pyxmpp.sasl.core import Response, Failure, Success
class XFacebookPlatformClientAuthenticator(ClientAuthenticator):
def __init__(self, password_manager, fb_client=None):
ClientAuthenticator.__init__(self, password_manager)
if fb_client is None:
global global_fb_client
fb_client = global_fb_client
self._fb_client = fb_client
def start(self, ignored_username, ignored_authzid):
return Response()
def challenge(self, challenge):
in_params = dict([part.split('=') for part in challenge.split('&')])
out_params = {'nonce': in_params['nonce']}
out_params = self._fb_client._add_session_args(out_params)
out_params = self._fb_client._build_post_args(in_params['method'], out_params)
import urllib
return Response(urllib.urlencode(out_params))
def finish(self,data):
return Success(None)
from pyxmpp.all import JID, Presence, Message, Iq
from pyxmpp.client import Client
class FacebookChatClient(Client):
def __init__(self, chatbuff=None, **kwargs):
Client.__init__(self, **kwargs)
if chatbuff != None:
self.buffr = chatbuff
def session_started(self):
self.get_stream().set_message_handler('chat', self.got_message)
self.request_roster()
p = Presence()
self.get_stream().send(p)
def roster_handler(self):
roster = str(self.roster)
querymatch = re.compile(".query(.+)roster..<item jid=\"-")
roster = querymatch.sub("", roster)
roster = roster.replace("</item></query>","")
roster = roster.replace("</query>", "")
roster = roster.replace("<item jid=\"-",",")
roster = roster.replace("</item>", "")
roster = roster.replace("@chat.facebook.com\" name=\"",":")
subscriptmatch = re.compile("\" subscription\=\"both\"/?>")
roster = subscriptmatch.sub("", roster)
roster = roster.replace("</group>", "")
roster = roster.replace("<group>", ":")
tmp_roster_array = roster.split(",")
roster_array = []
for i in tmp_roster_array:
i = i.split(":")
roster_array.append(i)
return roster_array
def idle(self):
Client.idle(self)
#send signal to list view to update roster
#HANDLER FOR A RECEIVED MESSAGE
def got_message(self, stanza):
if self.buffr != None:
buffr = self.buffr.get_text(self.buffr.get_start_iter(),self.buffr.get_end_iter())
else:
buffr = None
stanza_body = stanza.get_body()
stanza_node = str(stanza.get_from().node).replace("-","")
if(stanza_body == None):
#gui to show this as tooltip? show/hide dynamic element below buffer?
if buffr == None:
print stanza_node + " is typing...\n"
else:
msgtxt = "[" + datetime.now().strftime("%H:%M:%S") + "] <" + stanza_node + "> " + stanza_body + "\n"
if buffr != None:
addtext = buffr + msgtxt
gobject.idle_add(self.buffr.set_text, addtext)
else:
print msgtxt
logtext = "[" + datetime.now().strftime("%b %d %Y %H:%M:%S") + "] <" + stanza_node + "> " + stanza_body +"\n"
self.write_log(stanza_node, logtext)
#stanza.get_from().node is their UID
def send_message(self,uid,msg):
target = JID('-' + uid, self.jid.domain)
self.get_stream().send(Message(to_jid=target, body=unicode(msg)))
def write_log(self,uid,logtext):
#
# **********************
# THIS NEEDS TO BE FIXED
# **********************
#
# if(not str(os.curdir).endswith("logs")):
# if(os.path.exists("logs")):
# os.chdir("logs")
# else:
# os.mkdir("logs")
# os.chdir("logs")
_file = open(uid+"_log.txt","a+")
_file.write(logtext)
_file.close()
def connect_and_loop(self):
print 'Connecting...'
self.connect()
print 'Processing...'
try:
self.loop(1)
finally:
self.disconnect()
def setup_chat(fb_client, buffr=None, uidarg=None, messarg=None):
global global_fb_client
global_fb_client = fb_client
import pyxmpp.sasl
pyxmpp.sasl.all_mechanisms_dict['X-FACEBOOK-PLATFORM'] = (XFacebookPlatformClientAuthenticator, None)
my_uid = str(global_fb_client.uid)
my_jid = '-' + my_uid + '@chat.facebook.com/TestClient'
print 'Creating stream...'
xmpp_client = FacebookChatClient(
chatbuff = buffr,
jid = JID(my_jid),
password = u'ignored',
auth_methods = ['sasl:X-FACEBOOK-PLATFORM'],
#server = 'localhost'
)
return xmpp_client
if __name__ == "__main__":
print 'Preparing Facebook client...'
global_fb_client = get_facebook_client()
asd = setup_chat(global_fb_client, None)
asd.connect_and_loop()