Talking To Myself


Rui Carmo says: (23:12:10 PM)
   info

Bot says: (23:12:16 PM)
   1:12am  up 71 days, 23:10,  1 user,  load average: 0.01, 0.11, 0.17
   75 processes: 74 sleeping, 1 running, 0 zombie, 0 stopped
   CPU states:  0.0% user,  0.3% system,  0.0% nice,  0.0% idle
   Mem:   513596K av,  472976K used,   40620K free,       0K shrd,       0K buff
   Swap:  524536K av,   25624K used,  498912K free                  310848K cached

Bot says: (23:12:16 PM)
     PID USER     PRI  NI  SIZE  RSS SHARE STAT  LIB %CPU %MEM   TIME COMMAND
   15366 newspipe  15   0  1236 1232  1004 R       0  2.8  0.2   0:00 top
       1 root       8   0   520  472   460 S       0  0.0  0.0   0:23 init
       2 root       9   0     0    0     0 SW      0  0.0  0.0   0:00 keventd
       3 root      19  19     0    0     0 SWN     0  0.0  0.0   0:01 ksoftirqd_CPU0
       4 root       9   0     0    0     0 SW      0  0.0  0.0   3:10 kswapd

Took me all of half an hour to do that with only a few lines of Python:

import msnp
import time
import os

class MsnChatListener(msnp.ChatCallbacks):
    def message_received(self, passport_id, display_name, text, charset):
        if( text == "info" ):
          lines = os.popen( "top -b -n 1" ).read().strip().splitlines()
          #break output into decent-sized chunks
          head = "".join(["%s\r" % (a) for a in lines[0:5]])
          self.chat.send_message( head, charset )
          head = "".join(["%s\r" % (a) for a in lines[6:12]])
          self.chat.send_message( head, charset )

class MsnListener(msnp.SessionCallbacks):
    def chat_started(self, chat):
        callbacks = MsnChatListener()
        chat.callbacks = callbacks
        callbacks.chat = chat

msn = msnp.Session(MsnListener())
msn.login([email protected]', 'password')
msn.sync_friend_list()

# wait 10 seconds
for i in range(1, 10):
  msn.process(chats = True)
  time.sleep(1)

msn.change_display_name( 'Bot' )
msn.change_privacy_mode(msnp.PrivacyModes.ALLOW)
msn.change_state(msnp.States.ONLINE)

while True:
    msn.process(chats = True)
    time.sleep(1)

Perl? Ha!


See Also: