/[james]/archive/newsstats/!NewsStats/py/Newsbase.py
ViewVC logotype

Contents of /archive/newsstats/!NewsStats/py/Newsbase.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download) (as text)
Tue Feb 11 09:59:30 2003 UTC (21 years, 8 months ago) by james
File MIME type: text/x-python
File size: 6198 byte(s)
Initial import.

1 """Interface with Newsbase or compatible news database (eg. MsgServe).
2
3 Ensure that message 0x83581 (NewsBase_Reply) was passed to wimp.task().
4
5 Functions:
6
7 Startup() -- look for a Newsbase compatible server
8 ShutDown() -- Inform Newsbase that we've finished
9 ListGroups() -- Get a list of active groups matching the pattern
10 ListArts() -- Get a list of the articles available for a group
11 GetArticle() -- Return the filename of an article
12
13 Variables:
14
15 groups -- List of active groups, available after ListGroups()
16
17 """
18
19
20 import os
21 import string
22 import wimp
23
24
25 def init():
26 """Look for a Newsbase compatible server.
27
28 Set Newsbase.active to indicate if Newsbase is running.
29 Report an error if Newsbase returns one.
30
31 """
32 global active, handle
33 active = 0
34 # NewsBase_Command NewsBase_Startup
35 wimp.send_message(18, 0x83580, 0, (0, 60, 0))
36 event, details = wimp.poll(0x3973)
37 if event == 18 and details == 0x83580:
38 # got our own broadcast
39 event, details = wimp.poll(0x3973)
40 if event in (17, 18) and details == 0x83581:
41 if wimp.b[5] == 0:
42 handle = wimp.b[1]
43 active = 1
44 elif wimp.b[5] % 10 != 0:
45 wimp.warning(wimp.b.nullstring(32, wimp.b[0]))
46
47
48 def quit():
49 """Inform Newsbase that we've finished."""
50 global active
51 if active:
52 wimp.send_message(17, 0x83580, handle, (10,))
53 active = 0
54
55
56 def Reply():
57 global active
58 if wimp.b[5] == 0:
59 init()
60 elif wimp.b[5] == 10:
61 active = 0
62
63
64 def SetUser(username, password):
65 """Set the Newsbase user.
66
67 Return None if OK, a string giving error otherwise.
68
69 """
70 wimp.send_message(17, 0x83580, handle, (20, 0, 0, username + '\0' + password))
71 event, details = wimp.poll(0x3973)
72 while not (event == 17 and details == 0x83581):
73 event, details = wimp.poll(0x3973)
74 if wimp.b[5] == 20:
75 return None
76 elif wimp.b[5] % 10 != 0:
77 return wimp.b.nullstring(32, wimp.b[0])
78 return 'Unexpected response from Newsbase'
79
80
81 def ListGroups(username):
82 """Get a list of active groups for the given user.
83
84 Returns a list of strings giving the group names,
85 or a string if Newsbase returned it as an error.
86
87 """
88 global groups, group_ranges
89 groups = []
90 # NewsBase_Command NewsBase_GetUserInfo (home directory)
91 wimp.send_message(17, 0x83580, handle, (440, 5, 0, username))
92 event, details = wimp.poll(0x3973)
93 while not (event in (17, 18) and details == 0x83581):
94 event, details = wimp.poll(0x3973)
95 if wimp.b[5] == 440:
96 home = wimp.b.nullstring(32 + len(username) + 1, wimp.b[0])
97 newsrc = None
98 if os.path.isfile(os.path.join(home, 'NewsRC')):
99 newsrc = os.path.join(home, 'NewsRC')
100 else:
101 for dir in os.listdir(home):
102 if os.path.isdir(os.path.join(home, dir)):
103 if os.path.isfile(os.path.join(home, dir, 'NewsRC')):
104 newsrc = os.path.join(home, dir, 'NewsRC')
105 break
106 if newsrc is None:
107 return 'Unable to locate NewsRC'
108 file = open(newsrc, 'r')
109 groups = file.readlines()
110 file.close()
111 groups = map(lambda z: z[:-1].split(':')[0], groups)
112 if groups[-1] == 'end':
113 groups = groups[:-1]
114 groups.sort()
115 return groups
116 elif wimp.b[5] % 10 != 0:
117 return wimp.b.nullstring(32, wimp.b[0])
118 return 'Unexpected response from Newsbase'
119
120
121 def ListArts(group):
122 """Get a list of the articles available for a group.
123
124 Returns a list of tuples of the form
125 (article number, size, From:, Subject:, Message-ID:, Date:),
126 or a string if Newsbase returned it as an error.
127
128 Calls wimp.poll and may call wimp.handle.
129
130 """
131 articles = []
132
133 wimp.send_message(17, 0x83580, handle, (100, 0, 0, group))
134 event, details = wimp.poll(0x3973)
135 while not (event == 17 and details == 0x83581):
136 event, details = wimp.poll(0x3973)
137 if wimp.b[5] == 100:
138 pass
139 elif wimp.b[5] % 10 != 0:
140 return wimp.b.nullstring(32, wimp.b[0])
141 else:
142 return 'Unexpected response from Newsbase'
143
144 event, details = wimp.poll(0x3973)
145 while not (event == 17 and details == 0x83581):
146 event, details = wimp.poll(0x3973)
147 if wimp.b[5] == 100:
148 pass
149 elif wimp.b[5] % 10 != 0:
150 return wimp.b.nullstring(32, wimp.b[0])
151 else:
152 return 'Unexpected response from Newsbase'
153
154 wimp.send_message(17, 0x83580, handle, (200, 0, 0, group))
155 while 1:
156 event, details = wimp.poll(1)
157 while not (event == 17 and details == 0x83581):
158 wimp.handle((event, details))
159 event, details = wimp.poll(1)
160 if wimp.b[5] == 200:
161 if wimp.b[6] == 0:
162 return articles
163 else:
164 article_no = wimp.b[6]
165 article_size = wimp.b[7]
166 data = wimp.b.tostring(32, wimp.b[0])
167 article_from, article_subject, article_id, article_date = string.split(data, chr(0))[:4]
168 articles.append((article_no, article_size, article_from, article_subject,
169 article_id, article_date))
170 # if (len(article_numbers) & 7) == 0:
171 # window_status.icon_text(2, str(len(article_numbers)))
172 elif wimp.b[5] % 10 != 0:
173 return wimp.b.nullstring(32, wimp.b[0])
174 else:
175 return 'Unexpected response from Newsbase'
176
177
178 def GetArticle(group, article):
179 """Return the filename of an article.
180
181 If successful, returns (1, filename). Otherwise returns
182 (0, error message).
183
184 Calls wimp.poll and may call wimp.handle.
185
186 """
187 wimp.send_message(17, 0x83580, handle, (220, article, 0, group))
188 event, details = wimp.poll(0x141)
189 while not (event in (17, 18) and details == 0x83581):
190 wimp.handle((event, details))
191 event, details = wimp.poll(0x141)
192 if wimp.b[5] == 220:
193 return 1, wimp.b.nullstring(32, wimp.b[0])
194 else:
195 return 0, wimp.b.nullstring(32, wimp.b[0])

  ViewVC Help
Powered by ViewVC 1.1.26