#
# -*- encoding: utf-8 -*-
#

import re
import config
import surllib
import semail
import mechanize

URL_PREFIX     = '/Infoweb/Fi/Besked/'
URL_BOX_PREFIX = URL_PREFIX + 'Oversigt.asp?Bakke='
TRAYS = ('ind', 'ud')

def diaExamineMessage(url, mid):
    bs = surllib.skoleGetURL(url, True)
    
    # first, find main text
    tr = bs.find('tr', valign='top')
    assert(tr)
    phtml = tr.find('td')
    msg = semail.Message(u'dialogue', phtml)
    msg.setMessageID(mid)
    
    # next, look at the header
    header = bs.find('table', 'linje1')
    assert(header) # there must be a header
    headerLines = header.findAll('tr')
    assert(len(headerLines) >= 3) # there must be something inside the header

    for hl in headerLines:
        txt = hl.text
        if not txt:
            continue # ignore 
        elif txt.startswith(u'Denne besked slettes'):
            pass # ignore
        elif hl.find('h4'):
            # title
            msg.setTitle(txt)
        elif txt.startswith(u'Besked fra') or txt.startswith(u'Oprettet af'):
            # Besked fra Frk Nielsen - modtaget den 26-09-2012 20:29:44	
            msg.updatePersonDate(hl)
        elif txt.startswith(u'Sendt til '):
            # Sendt til ... 
            msg.setRecipient(txt.split(u' ', 2)[-1])
        else:
            config.log(u'Ukendt header i besked #%s: %s' % (mid, txt),-1)
            
    msg.maybeSend()
    
def diaFindMessages(data):
    global URL_PREFIX
    bs = surllib.beautify(data)

    atags = bs.findAll('a')
    for atag in atags:
        href = atag['href']
        
        if not href.startswith('VisBesked'):
            continue # ignore

        title = atag.text.strip()
        if not title:
            continue # ignore (this is the envelope icon)

        lurl = 'https://%s%s%s' % (config.HOSTNAME, URL_PREFIX, href)
        mid = re.search('Id=(\\d+)', href).group(1)

        diaExamineMessage(lurl, mid)

def skoleDialogue():
    global URL_BOX_PREFIX, TRAYS
    surllib.skoleLogin()
    br = surllib.getBrowser()

    for tray in TRAYS:
        config.log(u'Behandler beskeder i bakken %s' % tray)

        url = 'https://%s%s%s' % (config.HOSTNAME, URL_BOX_PREFIX, tray)
        
        # first read the initial page
        resp = br.open(url)
        data = resp.read()
        diaFindMessages(data)
        
        # select specific class
        try:
            br.select_form(name='FrontPage_Form1')
        except mechanize._mechanize.FormNotFoundError, e:
            # ignore
            continue
        br['R1'] = ('klasse',)
        
        resp = br.submit()
        data = resp.read()
        diaFindMessages(data)

if __name__ == '__main__':
    # test
    skoleDialogue()

@@ -242,16 +242,86 @@
 , mid):%0A
+    '''Look at the url and mid. Returns True iff an email was sent'''%0A
     bs =
@@ -1445,24 +1445,31 @@
        %0A    
+return 
 msg.maybeSen
@@ -1504,16 +1504,110 @@
 (data):%0A
+    '''Find all messages in the html data, return True iff at least one%0A    email was sent'''%0A
     glob
@@ -1681,16 +1681,40 @@
 ll('a')%0A
+    newMsgFound = False%0A
     for 
@@ -2087,16 +2087,19 @@
         
+if 
 diaExami
@@ -2118,16 +2118,72 @@
 rl, mid)
+:%0A            newMsgFound = True%0A    return newMsgFound 
 %0A%0Adef sk
@@ -2467,15 +2467,9 @@
   # 
-first r
+R
 ead 
@@ -2488,93 +2488,33 @@
 page
-%0A        resp = br.open(url)%0A        data = resp.read()%0A        diaFindM
+, and search for m
 essages
-(data)
 %0A   
@@ -2522,245 +2522,43 @@
     
-%0A        # select specific class%0A        try:%0A            br.select_form(name='FrontPage_Form1')%0A        except mechanize._mechanize.FormNotFoundError, e:%0A            # ignore%0A            continue%0A        br%5B'R1'%5D = ('klasse',
+config.log(u'Bakke-URL: %25s' %25 url
 )%0A
-        %0A
     
@@ -2575,15 +2575,16 @@
  br.
-submit(
+open(url
 )%0A  
@@ -2637,16 +2637,24 @@
 es(data)
+        
 %0A%0Aif __n

Remove uncessary page fetch hopefully fixing a "not viewing HTML" error