Source

sboty / chatbot_msn_setup.py

Full commit
  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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env python26
#-*- coding: utf-8 -*-
"""
CHATBOT MSN Client
"""

import sys
import os
import socket
import select
import urllib
import time
from primary import chatbotini
from msn_lib import msnlib
from msn_lib import msncb

# Basic classes
MSN = msnlib.msnd()
MSN.cb = msncb.cb()


class UsefFunct():
    """
    Useful functions
    """
    def __init__(self):
        """Start"""
        pass

    def do_login(self, email, logtype, msg):
        """
        Performs the chat log.
        """
        if CONFIG["log history"]:
            logfile = CONFIG["history directory"] + "/" + email
            if logtype == "in":
                lines = chatbotini.now() + " <<< " + str(msg) + "\n"
            elif logtype == "out":
                lines = chatbotini.now() + " >>> " + str(msg) + "\n"

            file_dict = open(logfile, "a")
            file_dict.write(lines)
            file_dict.close()

    def null(self, samp):
        """
        Null function, useful to void debug ones.
        """
        pass

    def emailtonick(self, email):
        """
        Returns a nick using the given email, or None if nobody matches.
        """
        if email in MSN.users.keys():
            return MSN.users[email].nick
        else:
            return None

    def getconfigure(self):
        """
        Parses the configuration, returns a var:value dict.
        """
        config = {"email": chatbotini.LOGINEMAILMSN,
                  "password": chatbotini.LOGINPASSWORDMSN}
        return config

    def quit(self, code=0):
        """
        Signs out from MSN.
        """
        print "Closing"
        chatbotini.connection_log("Sesion terminada\n\n\n", "msn")
        try:
            try:
                MSN.disconnect()
            except:
                pass
        except:
            pass
        sys.exit(code)


class ServDiscon():
    """
    Server disconnect
    """
    def __init__(self):
        """Start"""
        pass

    def cb_errors(self, md1, errno, params):
        """
        Handles server errors.
        """
        if not errno in msncb.error_table:
            desc = "Unknown"
        else:
            desc = msncb.error_table[errno]
        desc = "\rServer sent error %d: %s\n" % (errno, desc)
        print desc
        msncb.cb_err(md1, errno, params)

    def cb_adding(self, md1, typedat, tid, params):
        """
        Handler for adding users.
        """
        plist = params.split(" ")
        typedat = plist[0]
        if typedat == "RL" or typedat == "FL":
            email = plist[2]
            nick = urllib.unquote(plist[3])
        if typedat == "RL":
            out = "\r" + ("%s (%s) " % (email, nick)
            ) + "has added you to his contact list"
            print out
            email = params.split(" ")[2]
            MSN.useradd(email)  # Automatically adds the user
            try:
                MSN.userunblock(email.strip())
            except KeyError:
                pass
        elif typedat == "FL":
            out = "\r" + ("%s (%s) " % (email, nick)) + """has
            been added to your contact list"""
            print out
        msncb.cb_add(md1, typedat, tid, params)

    def cb_messages(self, md1, typedat, tid, params, sbd):
        """
        Handles MSN messages.
        """
        global LAST_RECEIVED
        plist = tid.split(" ")
        email = plist[0]

        # Parse
        lines = params.split("\n")
        headers = {}
        eoh = 0
        for line in lines:
            # End of headers
            if line == "\r":
                break
            tvv = line.split(":", 1)
            typedat = tvv[0]
            value = tvv[1].strip()
            headers[typedat] = value
            eoh += 1
        eoh += 1

        # Handle special hotmail messages
        if email == "Hotmail":
            if not "Content-Type" in headers:
                return
            hotmail_info = {}
            # Parse the body
            for line in lines:
                line = line.strip()
                if not line:
                    continue
                tvv = line.split(":", 1)
                typedat = tvv[0]
                value = tvv[1].strip()
                hotmail_info[typedat] = value
            msnlib.debug(params)
            if headers["Content-Type"
            ] == "text/x-msmsgsinitialemailnotification; charset=UTF-8":
                newmsgs = int(hotmail_info["Inbox-Unread"])
                if not newmsgs:
                    return
                print "\rYou have %s unread email(s)" % str(newmsgs) \
                    + " in your Hotmail account"
            elif headers["Content-Type"
            ] == "text/x-msmsgsemailnotification; charset=UTF-8":
                from_name = hotmail_info["From"]
                from_addr = hotmail_info["From-Addr"]
                subject = hotmail_info["Subject"]
                print "\rYou have just received an email in your" + \
                    " Hotmail account:"
                print "\r\tFrom: %s (%s)" % (from_name, from_addr)
                print "\r\tSubject: %s" % subject
            return

        if "Content-Type" in headers and headers[
            "Content-Type"] == "text/x-msmsgscontrol":
            nick = USEF_FUNCT.emailtonick(email)
            if not nick:
                nick = email
            if not "typing" in MSN.users[email].priv:
                MSN.users[email].priv["typing"] = 0
            if not MSN.users[email].priv["typing"] and email not in IGNORED:
                # When typing
                pass
            MSN.users[email].priv["typing"] = time.time()
        elif "Content-Type" in headers and headers[
            "Content-Type"] == "text/x-clientcaps":
            pass
        elif "Content-Type" in headers and headers[
            "Content-Type"] == "text/x-keepalive":
            pass
        else:
            # Messages
            MSN.users[email].priv["typing"] = 0
            disp_message(email, lines, eoh)
            if len(HISTORY_BUFFER) > CONFIG["history size"]:
                del(HISTORY_BUFFER[0])
            HISTORY_BUFFER.append((time.time(), email, lines[eoh:]))
        LAST_RECEIVED = email
        msncb.cb_msg(md1, type, tid, params, sbd)

    def cb_disconnect(self, md1, type, tid, params):
        """
        When the server disconnect us from MSN.
        """
        print "\rServer sent disconnect (probably you logged in somewhere else)"
        USEF_FUNCT.quit()
        msncb.cb_out(md1, type, tid, params)

    def cb_join(self, md1, type, tid, params, sbd):
        """
        Join a conversation and send pending messages.
        """
        email = tid
        nick = USEF_FUNCT.emailtonick(email)
        if not nick:
            nick = email
        if sbd.emails and email != sbd.emails[0]:
            first_nick = USEF_FUNCT.emailtonick(sbd.emails[0])
            if not first_nick:
                first_nick = sbd.emails[0]
            print "\rUser %s has joined the chat with %s" % (nick, first_nick)
        elif len(sbd.msgqueue) > 0:
            print "\rFlushing messages for %s:" % nick
            for msg in sbd.msgqueue:
                print nick, msg
        msncb.cb_joi(md1, type, tid, params, sbd)


def disp_message(email, lines, eoh=0):
    """
    Prints an incoming message and an optional pointer at the
    end of your header.
    """
    message = ""
    for line in lines[-1:]:
        line = line.replace(
                    "\xc3\xb1", "n").replace(
                    "\xc3\xa1", "a").replace(
                    "\xc3\xa9", "e").replace(
                    "\xc3\xad", "i").replace(
                    "\xc3\xb3", "o").replace(
                    "\xc3\xba", "u")
        message += line.strip() + " "
    message = message.strip().decode("utf-8", "ignore")
    # Log query message
    USEF_FUNCT.do_login(email, "in", message.encode("utf-8"))
    message_to_send = chatbotini.action_process(message, email, msn=MSN)
    extramsg = u""
    message_to_send += extramsg.encode("utf-8")
    MSN.sendmsg(email, message_to_send.replace(r"\n", "\n"))
    # Log response message
    USEF_FUNCT.do_login(email, "out", message_to_send)

#classes start
USEF_FUNCT = UsefFunct()
SERV_DISCON = ServDiscon()

MSN.cb.out = SERV_DISCON.cb_disconnect
MSN.cb.msg = SERV_DISCON.cb_messages
MSN.cb.joi = SERV_DISCON.cb_join
MSN.cb.err = SERV_DISCON.cb_errors
MSN.cb.add = SERV_DISCON.cb_adding


print "\n\n* ChatBot MSN Client *\n"
chatbotini.connection_log("Iniciando sesion\n", "msn")

# First, the configuration
print "Loading config... "

CONFIG = USEF_FUNCT.getconfigure()
CONFIG["profile"] = None

# Set the mandatory values
MSN.email = CONFIG["email"]
MSN.pwd = CONFIG["password"]

if not "history size" in CONFIG:
    CONFIG["history size"] = 10
else:
    try:
        CONFIG["history size"] = int(CONFIG["history size"])
    except:
        print "history size must be integer, using default"
        CONFIG["history size"] = 10

# Input history size
if not "input history size" in CONFIG:
    CONFIG["input history size"] = 10
else:
    try:
        CONFIG["history size"] = int(CONFIG["history size"])
    except:
        print "input history size must be integer, using default"
        CONFIG["input history size"] = 10

# Initial status
CONFIG["initial status"] = "online"

# Debug
CONFIG["debug"] = 0

# Log history
CONFIG["log history"] = 1

# History directory
CONFIG["history directory"] = chatbotini.LOGDIR + "/msn"

# Show realnick changes
if not "show realnick changes" in CONFIG:
    CONFIG["show realnick changes"] = 0
elif CONFIG["show realnick changes"] != "yes":
    CONFIG["show realnick changes"] = 0

# Encoding
if not "encoding" in CONFIG:
    if "LC_ALL" in os.environ and os.environ["LC_ALL"]:
        CONFIG["encoding"] = os.environ["LC_ALL"]
    elif "LANG" in os.environ and os.environ["LANG"]:
        CONFIG["encoding"] = os.environ["LANG"]
    else:
        CONFIG["encoding"] = "iso-8859-1"
MSN.encoding = CONFIG["encoding"]

print "Done!"

# Set or void the debug
if not CONFIG["debug"]:
    msnlib.debug = USEF_FUNCT.null
    msncb.debug = USEF_FUNCT.null

# Login to msn
print "Logging in... "
chatbotini.connection_log("Autenticando\n", "msn")
try:
    MSN.login()
    print "Done!"
except KeyboardInterrupt:
    chatbotini.connection_log("Interrupcion de teclado (Ctrl + C)\n", "msn")
    USEF_FUNCT.quit()
except socket.error, info:
    print "Network error: " + str(info)
    print "Closing"
    chatbotini.connection_log("Error de red (%s)\n" % str(info), "msn")
    chatbotini.connection_log("Terminando\n\n\n", "msn")
    sys.exit(1)
except Exception, info:
    print "Exception logging in"
    print "Error: %s" % str(info)
    print "Closing"
    chatbotini.connection_log(str(info) + "\n", "msn")
    chatbotini.connection_log("Terminando\n\n\n", "msn")
    sys.exit(1)


# Call sync to get the lists and refresh
print "Sending user list request... "
if MSN.sync():
    print "Done!"
    LIST_COMPLETE = 0
else:
    print "Error syncing users"


# Global variables
HISTORY_BUFFER = []    # History buffer
LAST_RECEIVED = ""   # Email of the last person we received a message from
IGNORED = []         # People being locally ignored

# Loop
while 1:
    FDS = MSN.pollable()
    INFD = FDS[0]
    OUTFD = FDS[1]
    INFD.append(sys.stdin)

    try:
        FDS = select.select(INFD, OUTFD, [], 5)
    except KeyboardInterrupt:
        chatbotini.connection_log("Interrupcion de teclado (Ctrl + C)\n", "msn")
        USEF_FUNCT.quit()

    for comp in FDS[0] + FDS[1]:        # see msnlib.msnd.pollable.__doc__
        try:
            MSN.read(comp)

            # See if we got all the user list, so we can
            # change our initial status
            if not LIST_COMPLETE and MSN.lst_total == MSN.syn_total:
                LIST_COMPLETE = 1
                if MSN.change_status(CONFIG["initial status"]):
                    print "\rStatus set to %s" % CONFIG["initial status"]
                    chatbotini.connection_log("Sesion iniciada\n", "msn")
                else:
                    print "\rError setting status: unknown status %s" % CONFIG[
                    "initial status"]
                # Re-adding and Unblocking users just in case
                contacts = MSN.users
                for e_mail in contacts:
                    MSN.useradd(e_mail)
                    try:
                        MSN.userunblock(e_mail)
                    except KeyError:
                        pass
            # If not online then change the status
            if MSN.status != "NLN":
                MSN.change_status("online")

        except socket.error, err:
            chatbotini.connection_log("Error en los sockets\n", "msn")
            if comp != MSN:
                if comp.msgqueue:
                    nick = USEF_FUNCT.emailtonick(comp.emails[0])
                    print """\rConnection with %s closed - the following
                    messages couldn't be sent:""" % (nick)
                    for mssage in comp.msgqueue:
                        print "\t>>> " + mssage
                MSN.close(comp)
            else:
                print "\nMain socket closed (%s)" % str(err)
                chatbotini.connection_log(
                "Se cerro el socket principal\n", "msn")
                USEF_FUNCT.quit(1)