-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwp-gopher.py
executable file
·186 lines (143 loc) · 4.83 KB
/
wp-gopher.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# Wordpress Gopher interface 0.3.0. Be afraid.
# The MIT License
#
# Copyright (c) 2007-10 Adam Harvey
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import ConfigParser
import MySQLdb
import sys
config = ConfigParser.ConfigParser()
config.read(("/etc/wp-gopher.ini", "wp-gopher.ini"))
dbh = MySQLdb.connect(host = config.get("database", "host"),
user = config.get("database", "user"),
passwd = config.get("database", "password"),
db = config.get("database", "database")
)
def printblankline():
printitem("i", "")
def printcopyright():
"""Prints a copyright message."""
global config
printitem("i", config.get("blog", "copyright"))
def printitem(type, description, selector = "", domain = None, port = None):
"""Prints a directory item in the proper Gopher format."""
global config
if not domain:
domain = config.get("blog", "domain")
if not port:
port = config.getint("blog", "port")
print "%s%s\t/%s\t%s\t%d\r\n" % (type, description, selector, domain, port),
def printtitle(pagetitle = None):
"""Prints a title as an information message."""
global config
title = config.get("blog", "title")
if pagetitle:
title += " :: %s" % pagetitle
printitem("i", title)
def index(limit = None):
"""Prints a suitable index of blog posts, using limit if necessary."""
global dbh
c = dbh.cursor()
c.execute("SELECT post_name, post_date, post_title FROM posts WHERE post_type = %s AND post_status = %s ORDER BY post_date DESC", ("post", "publish"))
if limit:
rows = c.fetchmany(limit)
else:
rows = c.fetchall()
printtitle()
printblankline()
for row in rows:
printitem("h", "%s (%s)" % (row[2], row[1]), row[0])
printblankline()
if limit:
printitem("i", "Only the last %d entries are listed here." % limit)
printitem("1", "View all entries", "all")
else:
printitem("i", "All blog entries are shown.")
printblankline()
printitem("7", "Search this blog")
printblankline()
printcopyright()
def search(term):
"""Searches for a given term."""
global dbh
# Surround the term in the appropriate MySQL regex word boundary
# markers.
searchterm = "[[:<:]]" + term + r"[[:>:]]"
c = dbh.cursor()
c.execute("SELECT post_name, post_date, post_title FROM posts WHERE post_type = %s AND post_status = %s AND (post_title RLIKE %s OR post_content RLIKE %s) ORDER BY post_date DESC", ("post", "publish", searchterm, searchterm))
rows = c.fetchall()
printtitle("Search Results :: %s" % term)
printblankline()
if len(rows) > 0:
for row in rows:
printitem("h", "%s (%s)" % (row[2], row[1]), row[0])
else:
printitem("i", "No entries were found.")
printblankline()
printitem("1", "View new entries", "")
printblankline()
printcopyright()
def post(name):
"""Prints a post."""
global config
global dbh
if name == "all":
return index()
elif name == "":
return index(config.getint("blog", "default"))
elif "\t" in name:
(selector, term) = name.split("\t", 1)
return search(term)
c = dbh.cursor()
c.execute("SELECT post_title, post_content FROM posts WHERE post_type = %s AND post_status = %s AND post_name = %s", ("post", "publish", name))
row = c.fetchone()
charset = config.get("blog", "charset")
title = config.get("blog", "title")
if row:
pagetitle, body = row
else:
pagetitle = "Error"
body = "Post not found"
print """
<!DOCTYPE HTML>
<html>
<head>
<title>%s :: %s</title>
<meta http-equiv="Content-Type" content="text/html; charset=%s" />
</head>
<body>
<h1>%s :: %s</h1>
%s
<p>
<a href="gopher://%s">Back to index</a>
</p>
<p style="font-style: italic; font-size: 80%%">
%s
</p>
</body>
</html>
""" % (title, pagetitle, charset, title, pagetitle, body, config.get("blog", "domain"), config.get("blog", "copyright"))
try:
post(sys.stdin.readline().strip("\r\n/"))
except Exception, e:
printtitle("Error")
printitem("3", str(e))
# vim:set nocin ai ts=8 sw=8 noet: