@ -40,45 +40,87 @@ def request_handle(used_handles_path):
return handle
def write_to_log ( self , entry ) :
def rec ( self , entry ) :
output = self . output
# print(f'Output: { output }')
log = ' index.html '
css = ' stylesheet.css '
used_handles = ' .handles.txt '
log_path = os . path . join ( output , log )
css_path = os . path . join ( output , css )
used_handles_path = os . path . join ( output , used_handles )
# check if file exists, if not: write it!
if not os . path . isfile ( log_path ) :
html_template = open ( ' templates/index.html ' , ' r ' ) . read ( )
css_template = open ( ' templates/stylesheet.css ' , ' r ' ) . read ( )
with open ( log_path , ' w ' ) as l :
l . write ( html_template )
l . write ( f ' <h1> { self . groupchat } </h1> ' )
with open ( css_path , ' w ' ) as c :
c . write ( css_template )
with open ( used_handles_path , ' w ' ) as h :
h . write ( ' ----- ' )
# add entry to log
# save entry
handle = request_handle ( used_handles_path )
print ( f ' Picked a handle: { handle } ' )
now = datetime . now ( ) . strftime ( ' % A %d % B ( % Y) ' )
print ( f ' Now is: { now } ' )
post = f ''' <div id= " { handle } " class= " post " >
< small class = " postid " > { handle } < / small >
{ entry }
< small class = " date " > Added on { now } < / small >
< / div > '''
print ( f ' Post: { post } ' )
with open ( log_path , ' a+ ' ) as l :
l . write ( post )
print ( ' added to the log! ' )
newfile_path = output + ' /entries/ ' + handle + ' .txt '
with open ( newfile_path , ' w ' ) as f :
f . write ( entry )
with open ( used_handles_path , ' a+ ' ) as h :
h . write ( handle )
print ( ' added to the .handles file! ' )
print ( ' Saved! ' )
def delete ( self , handle ) :
used_handles = ' .handles.txt '
used_handles_path = os . path . join ( self . output , used_handles )
possible_file_paths = [
os . path . join ( self . output , ' entries ' , handle + ' .txt ' ) ,
os . path . join ( self . output , ' entries ' , handle + ' .png ' ) ,
os . path . join ( self . output , ' entries ' , handle + ' .jpg ' )
]
for path in possible_file_paths :
if os . path . isfile ( path ) :
cmd = f ' rm { path } '
print ( f ' > { cmd } ' )
os . system ( cmd )
print ( f ' Removed { path } . ' )
pass
# delete handle from .handles.txt file
# read
with open ( used_handles_path , ' r ' ) as h :
txt = h . read ( )
txt = txt . replace ( handle + ' \n ' , " " )
# write
with open ( used_handles_path , ' w ' ) as h :
h . write ( txt )
# def write_to_log(self, entry):
# output = self.output
# # print(f'Output: { output }')
# log = 'index.html'
# css = 'stylesheet.css'
# used_handles = '.handles.txt'
# log_path = os.path.join(output, log)
# css_path = os.path.join(output, css)
# used_handles_path = os.path.join(output, used_handles)
# # check if file exists, if not: write it!
# if not os.path.isfile(log_path):
# html_template = open('templates/index.html', 'r').read()
# css_template = open('templates/stylesheet.css', 'r').read()
# with open(log_path, 'w') as l:
# l.write(html_template)
# l.write(f'<h1>{ self.groupchat }</h1>')
# with open(css_path, 'w') as c:
# c.write(css_template)
# with open(used_handles_path, 'w') as h:
# h.write('-----')
# # add entry to log
# handle = request_handle(used_handles_path)
# print(f'Picked a handle: { handle }')
# now = datetime.now().strftime('%A %d %B (%Y)')
# print(f'Now is: { now }')
# post = f'''<div id="{ handle }" class="post">
# <small class="postid">{ handle }</small>
# { entry }
# <small class="date">Added on { now }</small>
# </div>'''
# print(f'Post: { post }')
# with open(log_path, 'a+') as l:
# l.write(post)
# print('added to the log!')
# with open(used_handles_path, 'a+') as h:
# h.write(handle)
# print('added to the .handles file!')
# *spark
# add annotations
@ -119,12 +161,13 @@ class MUCBot(slixmpp.ClientXMPP):
and messages that are marked with @bot to a folder .
"""
def __init__ ( self , use , password , groupchat , nickname , output ) :
def __init__ ( self , use , password , groupchat , nickname , output , mode ) :
slixmpp . ClientXMPP . __init__ ( self , use , password )
self . groupchat = groupchat
self . nick = nickname
self . output = output
self . mode = mode
# The session_start event will be triggered when
# the bot establishes its connection with the server
@ -179,6 +222,9 @@ PS. you can access these logs at https://vvvvvvaria.org/logs/.''',
# Check if output folder exists
if not os . path . exists ( self . output ) :
os . mkdir ( self . output )
os . mkdir ( self . output + ' /entries/ ' )
with open ( os . path . join ( self . output , ' .handles.txt ' ) , ' w ' ) as f :
pass
# Check if an OOB URL is included in the stanza (which is how an image is sent)
# (OOB object - https://xmpp.org/extensions/xep-0066.html#x-oob)
@ -210,23 +256,34 @@ PS. you can access these logs at https://vvvvvvaria.org/logs/.''',
mbody = f ' Noted! And added to the log. Thanks { msg [ " mucnick " ] } ! ' ,
mtype = ' groupchat ' )
# Add the message to the log!
message = msg [ ' body ' ] . replace ( ' __ADD__ ' , ' ' )
message = f ' <div class= " entry text " > { message } </div> '
write_to_log ( self , message )
# Record the entry
rec ( self , msg [ ' body ' ] )
# Include a new post in the log (only when '__ADD__' is used in the message)
if ' __ANNOTA TE__ ' in msg [ ' body ' ] :
# Delete a post from the log
if ' __DELE TE__ ' in msg [ ' body ' ] :
handle = msg [ ' body ' ] . split ( ) [ 1 ]
annotation = msg [ ' body ' ] . replace ( ' __ANNOTATE__ ' , ' ' ) . replace ( handle , ' ' )
post = find_in_soup ( self , handle , annotation )
handle = re . findall ( " [aioeu][aioeu][aioeu][aioeu][aioeu] " , msg [ ' body ' ] ) [ 0 ]
# reply from the bot
self . send_message ( mto = self . groupchat ,
mbody = " Thanks! " ,
mbody = f ' Noted! The following post is deleted from the log: { handle } ' ,
mtype = ' groupchat ' )
# Delete the entry
delete ( self , handle )
# Include a new post in the log (only when '__ADD__' is used in the message)
# if '__ANNOTATE__' in msg['body']:
# handle = msg['body'].split()[1]
# annotation = msg['body'].replace('__ANNOTATE__', '').replace(handle, '')
# post = find_in_soup(self, handle, annotation)
# # reply from the bot
# self.send_message(mto=self.groupchat,
# mbody="Thanks!",
# mtype='groupchat')
# Check if this is a book ...
if ' __BOOK__ ' in msg [ ' body ' ] :
@ -270,6 +327,16 @@ PS. you can access these logs at https://vvvvvvaria.org/logs/.''',
self . send_message ( mto = self . groupchat , mbody = ' Sorry, no book found! ' , mtype = ' groupchat ' )
# Generate HTML logfiles
# By default: log
if self . mode :
mode = str ( self . mode . lower ( ) . strip ( ) )
if mode == " log " :
print ( ' > log.py ' )
elif mode == " stream " :
print ( ' > stream.py ' )
elif mode == " distribusi " :
print ( ' > distribusi.py ' )
if __name__ == ' __main__ ' :
# Setup the command line arguments.
@ -295,6 +362,9 @@ if __name__ == '__main__':
parser . add_argument ( " -o " , " --output " , dest = " output " ,
help = " output folder, this is where the files are stored " ,
type = str )
parser . add_argument ( " -m " , " --mode " , dest = " mode " ,
help = " logmode, options include: log, stream, distribusi " ,
type = str , default = ' log ' )
args = parser . parse_args ( )
@ -316,7 +386,7 @@ if __name__ == '__main__':
# Setup the MUCBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
xmpp = MUCBot ( args . use , args . password , args . groupchat , args . nickname , args . output )
xmpp = MUCBot ( args . use , args . password , args . groupchat , args . nickname , args . output , args . mode )
xmpp . register_plugin ( ' xep_0030 ' ) # Service Discovery
xmpp . register_plugin ( ' xep_0045 ' ) # Multi-User Chat
xmpp . register_plugin ( ' xep_0199 ' ) # XMPP Ping