<commit_before>"""
Atheloph: an IRC bot that writes a log.

TODOs:
    1) implement proactive ping so bot knows if it's disconnected
    2) implement auto-reconnect
    3) HTML logs with one anchor per line (or write a separate script to convert text to html)

    0) regularly tidy up code!
"""

import socket
import sys
import datetime
import time
import select

# Commands for controlling the bot inside a channel
BOT_QUIT = "hau*ab"

# Constants
SERVER = 'chat.freenode.net'
PORT = 6667
REALNAME = NICK = "ateloph_posi"
IDENT = "posiputt"
CHAN = "#5"
# ENTRY_MSG = 'Beep boop, wir testen den logbot. Wer ihn loswerden will, schreibe "' + BOT_QUIT + '".' 
# INFO = "Das Log ist derzeit sowieso nicht oeffentlich, sondern auf posiputts Rechner. Wer neugierig auf die sources ist oder mitmachen will, siehe hier: https://github.com/posiputt/ateloph"
ENTRY_MSG = 'entry.'
INFO = 'info.'
FLUSH_INTERVAL = 3 # num of lines to wait between log buffer flushes
PING_TIMEOUT = 260.
PT_PAUSE = 10 # sleep time before reconnecting after ping timeout


log_enabled = False

'''
Secure shutdown: 
The method closes the socket and flushes the buffer to the log.
Input: Socket socket, String msg, List buf
Outcome: Quits program.
'''
def shutdown(socket, msg, buf):
    socket.close()
    buf = flush_log(buf)
    print msg
    sys.exit("Exiting. Log has been written.")

'''
Flush log:
save current buffer to file, return empty buffer to avoid redundancy
Input: String buf
Return: empty String buf
'''
def flush_log(buf):
    print 'flushing log buffer to file'
    now = datetime.datetime.today()
    with open(str(now.date()) +'.log', 'a') as out:
        print "in with-statement"
        out.write(buf)
        print "written to file"
    out.close()
    print "file closed"
    buf = ""
    print "buf reset"
    return buf    

# connect to server
def conbot():
    s = socket.socket()
    s.connect((SERVER, PORT))
    s.send('NICK ' + NICK + '\n')
    s.send('USER ' + IDENT + ' ' + SERVER +' bla: ' + REALNAME + '\n')
    return s
    
# Parser to get rid of irrelvant information
def parse(line):
    '''
    log_privmsg
    format IRC PRIVMSG for log
    input: string timestamp, string nickname, list words
    return: string logline
    '''
    def log_privmsg(timestamp, nickname, words):
        #print "in log_privmsg"
        words[3] = words[3][1:]         # remove the leading colon
        message = ' '.join(words[3:])
        logline = ' '.join([timestamp, nickname+':' , message])
        #print "log_privmsg ended"
        return logline

    '''
    log_join
    format IRC JOIN for log
    input: string timestamp, string nickname, list words
    return: string logline
    '''
    def log_join(timestamp, nickname, words):
        #print "in log_join"
        channel = words[2]
        logline = ' '.join([timestamp, nickname, 'joined', channel])
        #print "log_join ended"
        return logline

    '''
    log_part
    format IRC PART for log
    input: string timestamp, string nickname, list words
    return: string logline
    '''
    def log_part(timestamp, nickname, words):
        #print "in log_part"
        channel = words[2]
        logline = ' '.join([timestamp, nickname, 'left', channel])
        #print "log_part ended"
        return logline


    functions = {
            'PRIVMSG':  log_privmsg,
            'JOIN':     log_join,
            'PART':     log_part
    }

    out = ''
    #print line
    words = line.split()
    #print words
    if not words[0] == 'PING':
        timestamp = datetime.datetime.today().strftime("%H:%M:%S")
        nickname = words[0].split('!')[0][1:]
        print nickname
        indicator = words[1]
        
        try:
            l = functions[indicator](timestamp, nickname, words)
            print l
            return l + '\n'
        except Exception as e:
            print 'Exception in parse - failed to pass to any appropriate function: ' + str(e)
    return out
    
    
    
if __name__ == '__main__':
    s = conbot()
    s.setblocking(0)
    
    # Generate a String "buffer" buf.
    ## Might be unecessary
    #buf = parse(s.recv(2048))

    try:<commit_msg>bot should now log QUITs<commit_after>"""
Atheloph: an IRC bot that writes a log.

TODOs:
    1) implement proactive ping so bot knows if it's disconnected
    2) implement auto-reconnect
    3) HTML logs with one anchor per line (or write a separate script to convert text to html)

    0) regularly tidy up code!
"""

import socket
import sys
import datetime
import time
import select

# Commands for controlling the bot inside a channel
BOT_QUIT = "hau*ab"

# Constants
SERVER = 'chat.freenode.net'
PORT = 6667
REALNAME = NICK = "ateloph_posi"
IDENT = "posiputt"
CHAN = "#5"
# ENTRY_MSG = 'Beep boop, wir testen den logbot. Wer ihn loswerden will, schreibe "' + BOT_QUIT + '".' 
# INFO = "Das Log ist derzeit sowieso nicht oeffentlich, sondern auf posiputts Rechner. Wer neugierig auf die sources ist oder mitmachen will, siehe hier: https://github.com/posiputt/ateloph"
ENTRY_MSG = 'entry.'
INFO = 'info.'
FLUSH_INTERVAL = 3 # num of lines to wait between log buffer flushes
PING_TIMEOUT = 260.
PT_PAUSE = 10 # sleep time before reconnecting after ping timeout


log_enabled = False

'''
Secure shutdown: 
The method closes the socket and flushes the buffer to the log.
Input: Socket socket, String msg, List buf
Outcome: Quits program.
'''
def shutdown(socket, msg, buf):
    socket.close()
    buf = flush_log(buf)
    print msg
    sys.exit("Exiting. Log has been written.")

'''
Flush log:
save current buffer to file, return empty buffer to avoid redundancy
Input: String buf
Return: empty String buf
'''
def flush_log(buf):
    print 'flushing log buffer to file'
    now = datetime.datetime.today()
    with open(str(now.date()) +'.log', 'a') as out:
        print "in with-statement"
        out.write(buf)
        print "written to file"
    out.close()
    print "file closed"
    buf = ""
    print "buf reset"
    return buf    

# connect to server
def conbot():
    s = socket.socket()
    s.connect((SERVER, PORT))
    s.send('NICK ' + NICK + '\n')
    s.send('USER ' + IDENT + ' ' + SERVER +' bla: ' + REALNAME + '\n')
    return s
    
# Parser to get rid of irrelvant information
def parse(line):
    '''
    log_privmsg
    format IRC PRIVMSG for log
    input: string timestamp, string nickname, list words
    return: string logline
    '''
    def log_privmsg(timestamp, nickname, words):
        #print "in log_privmsg"
        words[3] = words[3][1:]         # remove the leading colon
        message = ' '.join(words[3:])
        logline = ' '.join([timestamp, nickname+':' , message])
        #print "log_privmsg ended"
        return logline

    '''
    log_join
    format IRC JOIN for log
    input: string timestamp, string nickname, list words
    return: string logline
    '''
    def log_join(timestamp, nickname, words):
        #print "in log_join"
        channel = words[2]
        logline = ' '.join([timestamp, nickname, 'joined', channel])
        #print "log_join ended"
        return logline

    '''
    log_quit
    format IRC QUIT for log
    input: string timestamp, string nickname, list words
    return: string logline
    '''
    def log_quit(timestamp, nickname, words):
        #print "in log_quit"
        channel = words[2]
        logline = ' '.join([timestamp, nickname, 'left', channel])
        #print "log_part ended"
        return logline


    functions = {
            'PRIVMSG':  log_privmsg,
            'JOIN':     log_join,
            'QUIT':     log_quit
    }

    out = ''
    #print line
    words = line.split()
    #print words
    if not words[0] == 'PING':
        timestamp = datetime.datetime.today().strftime("%H:%M:%S")
        nickname = words[0].split('!')[0][1:]
        print nickname
        indicator = words[1]
        
        try:
            l = functions[indicator](timestamp, nickname, words)
            print l
            return l + '\n'
        except Exception as e:
            print 'Exception in parse - failed to pass to any appropriate function: ' + str(e)
    return out
    
    
    
if __name__ == '__main__':
    s = conbot()
    s.setblocking(0)
    
    # Generate a String "buffer" buf.
    ## Might be unecessary
    #buf = parse(s.recv(2048))

    try:
{'fraction_non_alphanumeric': 0.10848433937357495, 'fraction_numerical': 0.007440297611904476, 'mean_word_length': 2.578359811077716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3156591', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2552, 'n_words': 893}