@ -40,45 +40,87 @@ def request_handle(used_handles_path):
return handle
return handle
def write_to_log ( self , entry ) :
def rec ( self , entry ) :
output = self . output
output = self . output
# print(f'Output: { output }')
log = ' index.html '
css = ' stylesheet.css '
used_handles = ' .handles.txt '
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 )
used_handles_path = os . path . join ( output , used_handles )
# check if file exists, if not: write it!
# save entry
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 )
handle = request_handle ( used_handles_path )
print ( f ' Picked a handle: { handle } ' )
newfile_path = output + ' /entries/ ' + handle + ' .txt '
now = datetime . now ( ) . strftime ( ' % A %d % B ( % Y) ' )
with open ( newfile_path , ' w ' ) as f :
print ( f ' Now is: { now } ' )
f . write ( entry )
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 :
with open ( used_handles_path , ' a+ ' ) as h :
h . write ( handle )
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
# *spark
# add annotations
# add annotations
@ -119,12 +161,13 @@ class MUCBot(slixmpp.ClientXMPP):
and messages that are marked with @bot to a folder .
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 )
slixmpp . ClientXMPP . __init__ ( self , use , password )
self . groupchat = groupchat
self . groupchat = groupchat
self . nick = nickname
self . nick = nickname
self . output = output
self . output = output
self . mode = mode
# The session_start event will be triggered when
# The session_start event will be triggered when
# the bot establishes its connection with the server
# 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
# Check if output folder exists
if not os . path . exists ( self . output ) :
if not os . path . exists ( self . output ) :
os . mkdir ( 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)
# 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)
# (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 " ] } ! ' ,
mbody = f ' Noted! And added to the log. Thanks { msg [ " mucnick " ] } ! ' ,
mtype = ' groupchat ' )
mtype = ' groupchat ' )
# Add the message to the log!
# Record the entry
message = msg [ ' body ' ] . replace ( ' __ADD__ ' , ' ' )
rec ( self , msg [ ' body ' ] )
message = f ' <div class= " entry text " > { message } </div> '
write_to_log ( self , message )
# Include a new post in the log (only when '__ADD__' is used in the message)
# Delete a post from the log
if ' __ANNOTA TE__ ' in msg [ ' body ' ] :
if ' __DELE TE__ ' in msg [ ' body ' ] :
handle = msg [ ' body ' ] . split ( ) [ 1 ]
handle = re . findall ( " [aioeu][aioeu][aioeu][aioeu][aioeu] " , msg [ ' body ' ] ) [ 0 ]
annotation = msg [ ' body ' ] . replace ( ' __ANNOTATE__ ' , ' ' ) . replace ( handle , ' ' )
post = find_in_soup ( self , handle , annotation )
# reply from the bot
# reply from the bot
self . send_message ( mto = self . groupchat ,
self . send_message ( mto = self . groupchat ,
mbody = " Thanks! " ,
mbody = f ' Noted! The following post is deleted from the log: { handle } ' ,
mtype = ' groupchat ' )
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 ...
# Check if this is a book ...
if ' __BOOK__ ' in msg [ ' body ' ] :
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 ' )
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__ ' :
if __name__ == ' __main__ ' :
# Setup the command line arguments.
# Setup the command line arguments.
@ -295,6 +362,9 @@ if __name__ == '__main__':
parser . add_argument ( " -o " , " --output " , dest = " output " ,
parser . add_argument ( " -o " , " --output " , dest = " output " ,
help = " output folder, this is where the files are stored " ,
help = " output folder, this is where the files are stored " ,
type = str )
type = str )
parser . add_argument ( " -m " , " --mode " , dest = " mode " ,
help = " logmode, options include: log, stream, distribusi " ,
type = str , default = ' log ' )
args = parser . parse_args ( )
args = parser . parse_args ( )
@ -316,7 +386,7 @@ if __name__ == '__main__':
# Setup the MUCBot and register plugins. Note that while plugins may
# Setup the MUCBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# have interdependencies, the order in which you register them does
# not matter.
# 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_0030 ' ) # Service Discovery
xmpp . register_plugin ( ' xep_0045 ' ) # Multi-User Chat
xmpp . register_plugin ( ' xep_0045 ' ) # Multi-User Chat
xmpp . register_plugin ( ' xep_0199 ' ) # XMPP Ping
xmpp . register_plugin ( ' xep_0199 ' ) # XMPP Ping