<s> import os <EOL> import re <EOL> import logging <EOL> import sqlite3 <EOL> NOTE_REGEX = re . compile ( r'<STR_LIT>' ) <EOL> class NotesPlugin ( object ) : <EOL> logger = None <EOL> def __init__ ( self , cardinal , config ) : <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> if config is not None : <EOL> if config [ '<STR_LIT>' ] : <EOL> self . callback_id = cardinal . event_manager . register_callback ( <EOL> '<STR_LIT>' , self . join_callback <EOL> ) <EOL> self . _connect_or_create_db ( cardinal ) <EOL> def join_callback ( self , cardinal , user , channel ) : <EOL> content = self . _get_note_from_db ( user . group ( <NUM_LIT:1> ) ) <EOL> if content : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" % ( user . group ( <NUM_LIT:1> ) , content ) ) <EOL> def _connect_or_create_db ( self , cardinal ) : <EOL> try : <EOL> self . conn = sqlite3 . connect ( os . path . join ( <EOL> cardinal . storage_path , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % cardinal . network <EOL> ) ) <EOL> except Exception : <EOL> self . conn = None <EOL> self . logger . exception ( "<STR_LIT>" ) <EOL> return <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . conn . commit ( ) <EOL> def add_note ( self , cardinal , user , channel , msg ) : <EOL> if not self . conn : <EOL> cardinal . sendMsg ( "<STR_LIT>" ) <EOL> return <EOL> message = msg . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> title_message = message [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if len ( message ) < <NUM_LIT:2> or len ( title_message ) < <NUM_LIT:2> : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" ) <EOL> return <EOL> title = title_message [ <NUM_LIT:1> ] . strip ( ) <EOL> content = message [ <NUM_LIT:1> ] . strip ( ) <EOL> if len ( title ) == <NUM_LIT:0> or len ( content ) == <NUM_LIT:0> : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" ) <EOL> return <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( "<STR_LIT>" , <EOL> ( title , content ) ) <EOL> self . conn . commit ( ) <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" % title ) <EOL> add_note . commands = [ '<STR_LIT>' ] <EOL> add_note . help = [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> def delete_note ( self , cardinal , user , channel , msg ) : <EOL> if not self . conn : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" ) <EOL> return <EOL> msg = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if len ( msg ) < <NUM_LIT:2> or len ( msg [ <NUM_LIT:1> ] ) == <NUM_LIT:0> : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" ) <EOL> return <EOL> title = msg [ <NUM_LIT:1> ] <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( "<STR_LIT>" , ( title , ) ) <EOL> result = c . fetchone ( ) <EOL> if not result [ <NUM_LIT:0> ] : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" % title ) <EOL> return <EOL> c . execute ( "<STR_LIT>" , ( title , ) ) <EOL> self . conn . commit ( ) <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" % title ) <EOL> delete_note . commands = [ '<STR_LIT>' ] <EOL> delete_note . help = [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> def get_note ( self , cardinal , user , channel , msg ) : <EOL> if not self . conn : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" ) <EOL> return <EOL> message = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if message [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:!>' : <EOL> title = '<STR_LIT:U+0020>' . join ( message ) [ <NUM_LIT:1> : ] <EOL> else : <EOL> if len ( message ) != <NUM_LIT:2> : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" ) <EOL> return <EOL> title = message [ <NUM_LIT:1> ] <EOL> content = self . _get_note_from_db ( title ) <EOL> if not content : <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" % title ) <EOL> return <EOL> cardinal . sendMsg ( channel , "<STR_LIT>" % ( title , content ) ) <EOL> get_note . commands = [ "<STR_LIT>" ] <EOL> get_note . regex = NOTE_REGEX <EOL> get_note . help = [ "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> def _get_note_from_db ( self , title ) : <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( "<STR_LIT>" , ( title , ) ) <EOL> result = c . fetchone ( ) <EOL> if not result : <EOL> return False <EOL> else : <EOL> return bytes ( result [ <NUM_LIT:0> ] ) <EOL> def close ( self , cardinal ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> cardinal . event_manager . remove_callback ( '<STR_LIT>' , <EOL> self . callback_id ) <EOL> def setup ( cardinal , config ) : <EOL> return NotesPlugin ( cardinal , config ) </s>
