Changeset 56

Show
Ignore:
Timestamp:
03/01/08 14:30:57 (10 months ago)
Author:
scott
Message:

Awkpaste initial revision

  • Configurable values in awkpaste
  • Scruffy version of cgi-lib with URL decoding
  • Added paste operations to awkbot_db
  • Added stream updates to awkpaste (should probably be a separate function)
  • Added config changes to awkbot.conf
  • Added paste.cgi bootstrap for awkpaste code…
Location:
trunk/awkbot
Files:
4 added
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/awkbot/bin/awkbot

    r50 r56  
    1616# Give our continuous input through a pipe to tail.  tail -f never exits. 
    1717# Note, that this means awkbot never exits unless killed from the outside. 
     18chmod 666 $livedata 
    1819tail -f $livedata | awk -f $awkbot 
    1920 
  • trunk/awkbot/etc/awkbot.conf

    r47 r56  
    22# IRC Stuff 
    33<irc> 
    4     nickname awktest 
    5     altnick awk_test 
     4    nickname awkbot 
     5    altnick awkbot- 
    66    username awkbot 
    77    realname AWK IRC bot 
     
    1111#    channel awk perl pike 
    1212#    channel c++ 
    13     channel awk 
     13    channel awk #leapfrog 
    1414#    channel blacksun 
    1515    debug 1 
    16 #    startup PRIVMSG NickServ :identify darwin 
     16    startup PRIVMSG NickServ :identify darwin 
    1717</irc> 
    1818 
     
    2222 
    2323<mysql> 
    24     username scott 
    25     password scott 
     24    username scottmc 
     25    password scottmc 
    2626    database awkbot 
    2727</mysql> 
     28 
     29<paste> 
     30    channel #awk 
     31    cgi http://awkpaste.blisted.org/cgi/paste.cgi 
     32    form http://awkpaste.blisted.org/ 
     33</paste> 
  • trunk/awkbot/src/awkbot.awk

    r48 r56  
    7171$1 == "say" {  
    7272    _msg = $3 
    73     for (i = 4; i < NF; i++) { 
     73    for (i = 4; i <= NF; i++) { 
    7474        _msg = _msg " " $i 
    7575    } 
  • trunk/awkbot/src/awkbot_db_mysql.awk

    r47 r56  
    119119    return result 
    120120} 
     121 
     122function awkbot_db_paste_add (nick, description, content) { 
     123    mysql_finish(mysql_query("INSERT INTO paste (nick, subject, content) " \ 
     124            " VALUES (" mysql_quote(nick) "," \ 
     125                        mysql_quote(description) "," \ 
     126                        mysql_quote(content) ")")) 
     127} 
     128 
     129function awkbot_db_paste_get (id,row    ,rv) { 
     130    rv = mysql_query("SELECT nick, subject, content FROM paste " \ 
     131            "WHERE paste_id = " mysql_quote(id)) 
     132 
     133    mysql_fetch_assoc(rv, row) 
     134    mysql_finish(rv) 
     135} 
     136 
     137function awkbot_db_paste_last (     result,row,rv) { 
     138    rv = mysql_query("SELECT max(paste_id) as paste_id FROM paste"); 
     139 
     140    if (mysql_fetch_assoc(rv, row)) { 
     141        result = row["paste_id"] 
     142    } 
     143 
     144    mysql_finish(rv) 
     145 
     146    return result 
     147} 
  • trunk/awkbot/src/awkpaste.awk

    r47 r56  
    22 
    33#import <cgi-lib.awk> 
     4#import <config.awk> 
     5#import <awkbot_db_mysql.awk> 
    46 
    57BEGIN { 
     
    79    cgi_headers("text/plain") 
    810 
    9     for (key in query) { 
    10         print key, ":", query[key] 
     11    config_load("etc/awkbot.conf") 
     12    awkbot_db_init() 
     13 
     14    if (query["id"]) { 
     15        awkbot_db_paste_get(query["id"], paste) 
     16 
     17        id      = query["id"] 
     18        nick    = paste["nick"] 
     19        subject = paste["subject"] 
     20        content = paste["content"] 
     21 
     22        gsub(/\\n/, "\n", content) 
    1123    } 
     24    else { 
     25        stream  = awkbot_db_status_livefeed() 
     26     
     27        nick    = query["name"] 
     28        subject = query["description"] 
     29        content = query["content"] 
     30     
     31        awkbot_db_paste_add(nick, subject, content) 
     32        # This has synchronization issues...but what the hell, this is awk 
     33        id      = awkbot_db_paste_last() 
     34     
     35        if (id) { 
     36            printf("say %s %s pasted %s at %s?id=%s\n",    \ 
     37                    config("paste.channel"), nick, subject,\ 
     38                    config("paste.cgi"), id) >> stream 
     39     
     40            close(stream) 
     41        } 
     42    } 
     43 
     44 
     45    print "Id:", id 
     46    print "Nick:", nick 
     47    print "Subject:", subject 
     48    print content 
    1249} 
  • trunk/awkbot/src/cgi-lib.awk

    r47 r56  
     1 
     2#import <chr.awk> 
     3 
    14BEGIN { 
    25    if (ENVIRON["REQUEST_METHOD"] == "POST")  
     
    710# Set this globally so we don't have to ensure it happens anywhere else... 
    811    ORS = "\r\n" 
     12} 
    913 
    10     # Handle uploads... 
     14function uri_decode (string     ,i,len,result) { 
     15    # for portability, we have to continuously work in the argument provided... 
     16    # standard awk has no Nth match 
     17    len = length(string) 
    1118 
    12     # gawk only 
    13     match(HEADER["Content-Disposition"], / filename="([^;]*)"/, r) 
    14     filename = r[1] ? r[1] : "multipart/mixed" 
    15  
    16     if (filename && multipart) { 
    17         tempfilename = tempfile("cgi-awk") 
     19    while ((i = index(string, "%")) && i + 2 < len) { 
     20        result = result substr(string, 1, i - 1) 
     21        result = result chr(dec( substr(string, i + 1, 2) )) 
     22        string = substr(string, i + 3) 
    1823    } 
    1924 
    20     # When it's multipart, save headers and body together 
    21     if (multipart) { 
    22         for (key in HEADER) print key ":", HEADER[key] >> tempfilename 
    23         print "\r\n" >> tempfilename 
    24     } 
     25    result = result string 
    2526 
    26     while (buffersize < length(input)) { 
    27         getline i 
    28         print i >> tempfile 
    29     } 
    30  
    31     print cgi_read_buffer() >> tempfilename 
     27    gsub("+", " ", result) 
     28    return result 
    3229} 
    3330 
     
    3532    split(_cgilib_in, pairs, /\&/) 
    3633    i = 0     
     34 
    3735    while (pairs[++i]) { 
    3836        split(pairs[i], each, /=/) 
    39         query[each[1]] = each[2] 
     37 
     38        # This assumes no encoding will exist in keys...typically a safe 
     39        # assumption, but not necessarily always true 
     40        query[each[1]] = uri_decode(each[2]) 
    4041    } 
    4142}