summaryrefslogtreecommitdiffstats
path: root/bots/mandoos.py
blob: 772a5ff9a0ad357c652bc6f182529eab59ab5518 (plain)
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
# -*- coding: utf-8 -*-
import socket
import sys
import datetime
import time

#get stuff from command line
network = sys.argv[1]
port = 6667
nick = sys.argv[2]
channel = sys.argv[3]

#setup socket and connect to network
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((network,port))

#send nick, user info to network
sock.send('NICK ' + nick + '\r\n')
sock.send('USER ' + nick + ' ' + nick + ' '+ nick + ' :' + 'localhost\r\n')

#join channel
sock.send('JOIN ' + channel + '\r\n')

currentdate = str(datetime.date.today())
filename = currentdate + '-' + channel + '.log'
#Loop to keep listening
while True:
    data = sock.recv(5120)
    #check current date and if changed open new file
    if currentdate != str(datetime.date.today()):
	#close existing log
	logfile.close() 
  
	#update filename 
	currentdate = str(datetime.date.today());
	filename = currentdate + '-' + channel + '.log'
	#open new log
	logfile = open(filename ,'a')
	
    #check for PING from server and reply with PONG if found
    if data.find('PING')!=-1 :
	sock.send('PONG ' + data.split()[1] + '\r\n')
    if data.find('PRIVMSG ' + channel) != -1:
	logfile = open(filename,'a')
	speaker = data.split('!')[0].replace(':','')
	message = ''.join(data.split(':')[2:])
	logfile.write('<' + str(time.strftime("%H:%M:%S", time.localtime())) + '> ' + speaker + ' : ' + message)
	logfile.close()