summaryrefslogtreecommitdiff
path: root/main.lisp
blob: a16e1ed9810333393a4db0f9a36bcb4fdd9d5318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;;;; -*- coding: utf-8; mode: lisp -*-
;;;; Copyright 2007, Matthias Andreas Benkard.

;;;------------------------------------------------------------------------
;;; This file is part of The Mulkblog Project.
;;;
;;; The Mulkblog Project is free software.  You can redistribute it and/or
;;; modify it under the terms of the Affero General Public License as
;;; published by Affero, Inc.; either version 1 of the License, or
;;; (at your option) any later version.
;;;
;;; The Mulkblog Project is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty
;;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the Affero General Public
;;; License in the COPYING file that comes with The Mulkblog Project; if
;;; not, write to Affero, Inc., 510 Third Street, Suite 225, San
;;; Francisco, CA 94107 USA.
;;;------------------------------------------------------------------------

(in-package #:mulk.journal)


(defun call-with-initialised-journal (func)
  (let* ((*site*            (if (file-exists-p #p"/home/mulk") :mst-plus :nfs.net))
         (*debugging-p*     (eq *site* :mst-plus))
         (*query*           #+clisp (mapcan #'(lambda (param)
                                                (list (keywordify param)
                                                      (http-query-parameter param)))
                                            (http-query-parameter-list))
                            #-clisp '())
         (*http-env*        (http-get-env-vars))
         (*subpath-query*   (subseq (gethash "REQUEST_URI" *http-env*)
                                    (length (ecase *site*
                                              (:mst-plus (gethash "SCRIPT_NAME" *http-env*))
                                              (:nfs.net "/journal")))))
         (*subpath-string*  (subseq *subpath-query*
                                    0
                                    (or (position #\? *subpath-query*)
                                        (length *subpath-query*))))
         (*subpath*         (split-sequence #\/ *subpath-string*
                                            :remove-empty-subseqs t))
         (*post-number*     (parse-integer (or (first *subpath*)
                                               (getf *query* :id ""))
                                           :junk-allowed t  #|| :radix 12 ||#))
         (*action*          (or (keywordify (getf *query* :action))
                                (cond (*post-number*                      :view)
                                      ((string= "feed" (first *subpath*)) :view-atom-feed)
                                      ((string= "debug" (first *subpath*)) :view-debugging-page)
                                      (t                                  nil))))
         (*method*          (keywordify (gethash "REQUEST_METHOD" *http-env*)))
         (*script-filename* (pathname-as-file
                             (or (gethash "SCRIPT_FILENAME" *http-env*)
                                 "/home/mulk/Dokumente/Projekte/Mulkblog/journal.cgi")))
         (*script-dir*      (make-pathname
                             :directory (pathname-directory *script-filename*)))
         (*data-dir*        (ecase *site*
                              (:mst-plus *script-dir*)
                              (:nfs.net #p"/home/protected/journal/")))
         (*cache-dir*       (merge-pathnames #p"cache/" *data-dir*))
         (database-file     (merge-pathnames #p"journal.sqlite3" *data-dir*))
         (sqlite-library    (merge-pathnames #p"libsqlite3.so"
                                             (ecase *site*
                                               (:mst-plus #p"/usr/lib/")
                                               (:nfs.net #p"/usr/local/lib/")))))
    (clsql:push-library-path *script-dir*)
    (clsql:push-library-path #p"/usr/local/lib/")
    (push *script-dir* clsql-sys:*foreign-library-search-paths*)
    (clsql-uffi::load-uffi-foreign-library)
    (uffi:load-foreign-library (merge-pathnames "clsql_uffi.so"
                                                *script-dir*))
    (uffi:load-foreign-library sqlite-library)
    (clsql:with-database (db (list (namestring database-file))
                             :database-type :sqlite3
                             :make-default t)
      (assert db)
      (funcall func))))


#+clisp
(defun journal-main ()
  (ext:letf ((custom:*terminal-encoding* (ext:make-encoding
                                          :charset charset:utf-8)))
    (with-initialised-journal
      (let ((*random-state* (make-random-state t)))
        (case *action*
          (:post-comment   (with-transaction ()
                             (let* ((entry (find-entry *post-number*))
                                    (comment
                                     (make-instance 'journal-comment
                                        :id       (make-journal-comment-id)
                                        :uuid     (make-uuid)
                                        :entry-id (id-of entry)
                                        :date     (get-universal-time)
                                        :author   (getf *query* :author)
                                        :email    (getf *query* :email)
                                        :website  (getf *query* :website)
                                        :body     (getf *query* :comment-body))))
                               (push comment (comments-about entry))
                               (update-records-from-instance comment)
                               (update-records-from-instance entry)))
                           (show-web-journal))
          (:view-atom-feed (show-atom-feed))
          (:view-debugging-page (show-debugging-page))
          (otherwise       (show-web-journal)))))))


#+clisp
(defun cl-user::script-main ()
  (dolist (env-var '("HTTP_CACHE_CONTROL" "HTTP_IF_MODIFIED_SINCE"))
    (pushnew env-var http::*http-env-vars*))
  (http:http-init)
  (handler-bind
      ((error #'
        (lambda (e)
          (declare (ignorable e))
          (<:html
           (<:head
            (<:title "Kompottkins Weisheiten: Fehler"))
           (<:body
            (<:h1 "Kompottkins Weisheiten: Fehlerbericht")
            (<:p "Leider ist waehrend der Bearbeitung Ihrer Anfrage ein
                Fehler aufgetreten.  Wir bitten dies zu entschuldigen.
                Ein detaillierter Fehlerbericht folgt.")
            (<:pre (<:as-html (with-output-to-string (out)
                                #+clisp (system::pretty-print-condition e out)
                                #+clisp (system::print-backtrace :out out)))))))))
    (journal-main)))