<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, June 02, 2011, 11:17 PM -->
<!-- MuClient version 4.72 -->

<!-- Plugin "avalon_bb_archive" generated by Plugin Wizard -->

<muclient>
<plugin
   name="avalon_bb_archive"
   author="Matt Adcock"
   id="9c3c61d57547bb41f4b98b38"
   language="Lua"
   purpose="Archives bulletin board posts in a database "
   save_state="y"
   date_written="2011-06-02 23:13:53"
   requires="4.72"
   version="1.0"
   >

</plugin>


<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Triggers  -->

<triggers>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^BB\&gt; $"
   regexp="y"
   script="bbEnd"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^Date\: (.*?)\.?$"
   regexp="y"
   script="bbDate"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^From\: (.*?)\.?$"
   regexp="y"
   script="bbFrom"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^Message \#(\d+)\.?$"
   regexp="y"
   script="bbNumber"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^Subj\: (.*?)\.?$"
   regexp="y"
   script="bbSubject"
   sequence="100"
  >
  </trigger>
  <trigger
   group="bb_archive"
   match="^(.*?)$"
   name="bb_message_capture"
   regexp="y"
   script="bbMessage"
   sequence="101"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^To  \: (.*?)\.?$"
   regexp="y"
   script="bbTo"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^(.*?)\/(.*?)\:(.*?) FORUM$"
   regexp="y"
   script="bbSection"
   sequence="98"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^(.*?)\/(.*?)\:(.*?) SECTION$"
   regexp="y"
   script="bbSection"
   sequence="99"
  >
  </trigger>
  <trigger
   enabled="y"
   group="bb_archive"
   match="^(.*?)\/(.*?)\:(.*?)$"
   regexp="y"
   script="bbSection"
   sequence="100"
  >
  </trigger>
</triggers>

<!--  Aliases  -->

<aliases>
  <alias
   script="bbResult"
   match="^bbresult (.*?)$"
   enabled="y"
   group="bb_archive"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   script="bbSearch"
   match="^bbsearch (.*?) (.*?)$"
   enabled="y"
   group="bb_archive"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   script="bbSections"
   match="^bbsections$"
   enabled="y"
   group="bb_archive"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   script="bbSetSection"
   match="^bbsetsection (.*?)$"
   enabled="y"
   group="bb_archive"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>

<!--  Variables  -->

<variables>
  <variable name="bbsection">ALL</variable>
</variables>

<!--  Script  -->


<script>
<![CDATA[
require "var"
require "sqlite3"

function systemNote (msgtype, msg, colour1, colour2)
    ColourTell ("silver", "", "[")
    ColourTell (colour1 or "orangered", "", msgtype)
    ColourTell ("silver", "", "] ")
    ColourTell (colour2 or "white", "", msg)
    Tell ("\n")
end

-- bb archive
bb = { ["number"] = 0, ["date"] = "", ["from"] = "", ["to"] = "", ["subject"] = "", ["message"] = "", ["section"] = "" ,}

function bbStart (name, line, wildcards)
   	bbReset ()
    EnableTriggerGroup ("bb_archive", true)
end

function bbNumber (name, line, wildcards)
    bb.number = tonumber(wildcards[1])
end

function bbDate (name, line, wildcards)
    bb.date = wildcards[1]
end

function bbFrom (name, line, wildcards)
     bb.from = wildcards[1]
end

function bbTo (name, line, wildcards)
    bb.to = wildcards[1]
end

function bbSubject (name, line, wildcards)
    bb.subject = wildcards[1]
    EnableTrigger ("bb_message_capture", true)
end

function bbMessage (name, line, wildcards)
    bb.message = bb.message .. wildcards[1] .. "\n"
end

function bbSection (name, line, wildcards)
    bb.section = wildcards[3]
end

function bbReset ()
	bb = { ["number"] = 0, ["date"] = "", ["from"] = "", ["to"] = "", ["subject"] = "", ["message"] = "", ["section"] = "" ,}
end

databasename = GetInfo (66) .. "avalon.sqlite"

function bbEnd (name, line, wildcards)
    -- end capture of message and insert into db
    EnableTrigger ("bb_message_capture", false)
    
    db = sqlite3.open(databasename)  
    
    -- check table exists
    db:exec ([[CREATE TABLE IF NOT EXISTS posts (
        post_id    TEXT UNIQUE,
        bb_section    TEXT NOT NULL,
        bb_number     INTEGER,
        bb_date       TEXT NOT NULL,
        bb_from       TEXT NOT NULL,
        bb_to         TEXT NOT NULL,
        bb_subject    TEXT NOT NULL,
        bb_message    TEXT NOT NULL)
        ]])


    if bb.number == 0 then
        systemNote ("DB", "No post data found.")
        -- clear the data
        bbReset ()
        return
    end
        
    bb.post_id = bb.section .. bb.number
    -- work on database here
    stmt = db:prepare[[ INSERT OR IGNORE INTO posts VALUES (:post_id, :section, :number, :date, :from, :to, :subject, :message) ]]
    stmt:bind_names(bb)
    stmt:step()
    stmt:finalize()
    systemNote("DB", "Post added to database.")
    db:close()
    -- reset bb data
    bbReset ()
end

function bbSearch (name, line, wildcards)
    field = "bb_" .. wildcards[1]
    value = "%" .. wildcards[2] .. "%"
    results = {}
    db = sqlite3.open(databasename)
    -- are we searching a specific section?
    section = var.bbsection or "ALL"
    if section == "ALL" then
        query = "SELECT * FROM posts WHERE " .. field .. " LIKE '" .. value .. "'"
    else
        query = "SELECT * FROM posts WHERE " .. field .. " LIKE '" .. value .. "' AND bb_section = '" .. var.bbsection .. "'"
    end    
    for r in db:nrows (query) do
        table.insert(results, r)
    end
    db:close()
    -- display results
    ColourNote ("white", "", "Results from the " .. var.bbsection .. " section:")
    for _,r in ipairs(results) do
        Tell ("  ")
        Hyperlink ("bbresult " .. r['post_id'], r['bb_number'], "", "white", "", false)
        Tell ("\t" .. r['bb_subject'])
        Note ("")
    end
    Note ("")
    ColourNote ("white", "", #results .. " posts found with '" .. wildcards[1] .. "' containing '" .. wildcards[2] .. "' in section " .. var.bbsection .. ".")
end

function bbResult (name, line, wildcards)
    -- iterate results for matching post_id
    for _,r in ipairs(results) do
        if r['post_id'] == wildcards[1] then
            ColourNote ("white", "", "Post #" .. r['bb_number'] .. " in the " .. r['bb_section'] .. " section.")
            Note ("Date: " .. r['bb_date'])
            Note ("From: " .. r['bb_from'])
            Note ("To: " .. r['bb_to'])
            Note ("Subject: " .. r['bb_subject'])
            Note (r['bb_message'])
        end
    end
end

function bbSetSection (name, line, wildcards)
    -- set a section as default
    var.bbsection = wildcards[1]
    ColourNote ("white", "", "Current section for searching is now: " .. var.bbsection)
    Note ("")
end

function bbSections (name, line, wildcards)
    -- list all available sections
    results = {}
    db = sqlite3.open(databasename)
    query = "SELECT DISTINCT bb_section FROM posts"
    for r in db:nrows (query) do
        table.insert(results, r)
    end
    db:close()
    -- display results
    ColourNote ("white", "", "You can search the following bulletin board sections: ")
    Tell ("  ")
    Hyperlink ("bbsetsection ALL", "ALL", "", "white", "", false)
    Note ("")
    for _,r in ipairs(results) do
        Tell ("  ")
        Hyperlink ("bbsetsection " .. r['bb_section'], r['bb_section'], "", "white", "", false)
        Note ("")
    end
    Note ("")
end
]]>
</script>


</muclient>

