Merge lp:~stefanor/ibid/sqlalchemy-370772 into lp:~ibid-core/ibid/old-trunk-pack-0.92
- sqlalchemy-370772
- Merge into old-trunk-pack-0.92
Status: | Merged | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Approved by: | Stefano Rivera | ||||||||||||
Approved revision: | 634 | ||||||||||||
Merged at revision: | 634 | ||||||||||||
Proposed branch: | lp:~stefanor/ibid/sqlalchemy-370772 | ||||||||||||
Merge into: | lp:~ibid-core/ibid/old-trunk-pack-0.92 | ||||||||||||
Diff against target: | None lines | ||||||||||||
To merge this branch: | bzr merge lp:~stefanor/ibid/sqlalchemy-370772 | ||||||||||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Michael Gorven | Approve | ||
Jonathan Hitchcock | Approve | ||
Review via email: mp+6141@code.launchpad.net |
This proposal supersedes a proposal from 2009-05-02.
Commit message
Description of the change
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal | # |
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal | # |
> We still have the option of attaching identities and accounts to the event.
> However, I'm not doing that now.
Maybe we don't. You can't reuse rolled back sessions...
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal | # |
No reviews yet, so re-submitted to get new diff
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal | # |
Note: This can't be merged until all DB-touching branches are in trunk and this has been updated to fix them too.
Also: There appears to be a bug in granting priviledges, but it'd help to have accounts-330942 merged to fix that.
Michael Gorven (mgorven) wrote : Posted in a previous version of this proposal | # |
+ self.log.
of %s plugin", processor.
Can you just change the log message so something like "Exception occured while
committing session after %s processor of %s plugin".
+ self[name] = scoped_
Do all versions of SQLAlchemy default to using transactions and autoflush?
- session.flush()
actions[
The account ID won't be available if autoflush isn't on.
There's a problem we didn't think of, which is that processors add responses
before knowing that their database changes succeeded. So the bot will still
respond positively if the commit() by the dispatcher failed. Maybe we do need
to commit() in the plugin itself.
I'd prefer the except block in Identify.identify() to only catch
IntegrityErrors.
review needs_fixing
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal | # |
> Can you just change the log message so something like "Exception occured while
> committing session after %s processor of %s plugin".
Agreed. I sort of skipped over that. r628
> Do all versions of SQLAlchemy default to using transactions and autoflush?
All supported versions. And not likely to change.
> The account ID won't be available if autoflush isn't on.
No, autoflush doesn't help, but yes we do need a manual flush. r629
Although logging factoid IDs is of arguable value.
> There's a problem we didn't think of, which is that processors add responses
> before knowing that their database changes succeeded. So the bot will still
> respond positively if the commit() by the dispatcher failed. Maybe we do need
> to commit() in the plugin itself.
Yes, that's certainly an issue. We even have the option of re-running failed
processors now (although we'd need to differentiate which are safe to re-run,
etc.)
> I'd prefer the except block in Identify.identify() to only catch
> IntegrityErrors.
Agreed. r631
The reason for that is SQLite can cause an OperationalError to be thrown if
the database is locked. The correct behaviour in that case is probably to sleep
and then try and commit again. (at least at the pysqlite level, that'd be the
right thing)
Jonathan Hitchcock (vhata) : | # |
- 633. By Stefano Rivera
-
Merge from trunk
Michael Gorven (mgorven) wrote : | # |
This still allows positive acknowledgements to be returned even if the
commit() fails. If we need to flush() in order to get an ID for logging, why
don't we just commit() instead? As long as the dispatcher cleans up the
session when commit() fails, I don't see a problem.
- 634. By Stefano Rivera
-
Commit in plugins, to ensure that Ibid only confirms successful actions (rollback in dispatcher)
Stefano Rivera (stefanor) wrote : | # |
r634
Michael Gorven (mgorven) wrote : | # |
Why is the session deleted if an exception occurs? Is rolling back not
sufficient?
Stefano Rivera (stefanor) wrote : | # |
> Why is the session deleted if an exception occurs? Is rolling back not
> sufficient?
Exactly, you can't operate on a rolled back sqlalchemy session.
Michael Gorven (mgorven) wrote : | # |
> Exactly, you can't operate on a rolled back sqlalchemy session.
Um, I don't understand why that should be the case, but okay.
review approve
Preview Diff
1 | === modified file 'ibid/auth.py' | |||
2 | --- ibid/auth.py 2009-03-16 21:00:23 +0000 | |||
3 | +++ ibid/auth.py 2009-05-02 22:05:13 +0000 | |||
4 | @@ -23,11 +23,11 @@ | |||
5 | 23 | salt = ''.join([choice(chars) for i in xrange(8)]) | 23 | salt = ''.join([choice(chars) for i in xrange(8)]) |
6 | 24 | return unicode(salt + sha1(salt + password).hexdigest()) | 24 | return unicode(salt + sha1(salt + password).hexdigest()) |
7 | 25 | 25 | ||
9 | 26 | def permission(name, account, source): | 26 | def permission(name, account, source, session): |
10 | 27 | if account: | 27 | if account: |
14 | 28 | session = ibid.databases.ibid() | 28 | permission = session.query(Permission) \ |
15 | 29 | permission = session.query(Permission).filter_by(account_id=account).filter_by(name=name).first() | 29 | .filter_by(account_id=account) \ |
16 | 30 | session.close() | 30 | .filter_by(name=name).first() |
17 | 31 | 31 | ||
18 | 32 | if permission: | 32 | if permission: |
19 | 33 | return permission.value | 33 | return permission.value |
20 | @@ -95,7 +95,7 @@ | |||
21 | 95 | return False | 95 | return False |
22 | 96 | 96 | ||
23 | 97 | def authorise(self, event, name): | 97 | def authorise(self, event, name): |
25 | 98 | value = permission(name, event.account, event.source) | 98 | value = permission(name, event.account, event.source, event.session) |
26 | 99 | self.log.info(u"Checking %s permission for %s/%s (%s): %s", name, event.account, event.identity, event.sender['connection'], value) | 99 | self.log.info(u"Checking %s permission for %s/%s (%s): %s", name, event.account, event.identity, event.sender['connection'], value) |
27 | 100 | 100 | ||
28 | 101 | if value == 'yes': | 101 | if value == 'yes': |
29 | @@ -105,15 +105,17 @@ | |||
30 | 105 | 105 | ||
31 | 106 | return False | 106 | return False |
32 | 107 | 107 | ||
34 | 108 | def implicit(self, event, credential = None): | 108 | def implicit(self, event, credential=None): |
35 | 109 | return True | 109 | return True |
36 | 110 | 110 | ||
37 | 111 | def password(self, event, password): | 111 | def password(self, event, password): |
38 | 112 | if password is None: | 112 | if password is None: |
39 | 113 | return False | 113 | return False |
40 | 114 | 114 | ||
43 | 115 | session = ibid.databases.ibid() | 115 | for credential in event.session.query(Credential) \ |
44 | 116 | for credential in session.query(Credential).filter_by(method=u'password').filter_by(account_id=event.account).filter(or_(Credential.source == event.source, Credential.source == None)).all(): | 116 | .filter_by(method=u'password') \ |
45 | 117 | .filter_by(account_id=event.account) \ | ||
46 | 118 | .filter(or_(Credential.source == event.source, Credential.source == None)).all(): | ||
47 | 117 | if hash(password, credential.credential) == credential.credential: | 119 | if hash(password, credential.credential) == credential.credential: |
48 | 118 | return True | 120 | return True |
49 | 119 | 121 | ||
50 | 120 | 122 | ||
51 | === modified file 'ibid/core.py' | |||
52 | --- ibid/core.py 2009-05-02 15:14:56 +0000 | |||
53 | +++ ibid/core.py 2009-05-03 16:37:03 +0000 | |||
54 | @@ -7,6 +7,7 @@ | |||
55 | 7 | from twisted.python.modules import getModule | 7 | from twisted.python.modules import getModule |
56 | 8 | from sqlalchemy import create_engine | 8 | from sqlalchemy import create_engine |
57 | 9 | from sqlalchemy.orm import sessionmaker, scoped_session | 9 | from sqlalchemy.orm import sessionmaker, scoped_session |
58 | 10 | from sqlalchemy.exceptions import IntegrityError | ||
59 | 10 | 11 | ||
60 | 11 | import ibid | 12 | import ibid |
61 | 12 | import auth | 13 | import auth |
62 | @@ -21,9 +22,25 @@ | |||
63 | 21 | try: | 22 | try: |
64 | 22 | processor.process(event) | 23 | processor.process(event) |
65 | 23 | except Exception: | 24 | except Exception: |
67 | 24 | self.log.exception(u"Exception occured in %s processor of %s plugin", processor.__class__.__name__, processor.name) | 25 | self.log.exception(u"Exception occured in %s processor of %s plugin", |
68 | 26 | processor.__class__.__name__, processor.name) | ||
69 | 25 | event.complain = u'exception' | 27 | event.complain = u'exception' |
70 | 26 | 28 | ||
71 | 29 | if 'session' in event and (event.session.dirty or event.session.deleted): | ||
72 | 30 | try: | ||
73 | 31 | event.session.commit() | ||
74 | 32 | except IntegrityError: | ||
75 | 33 | self.log.exception(u"Exception occured committing session from the %s processor of %s plugin", | ||
76 | 34 | processor.__class__.__name__, processor.name) | ||
77 | 35 | event.complain = u'exception' | ||
78 | 36 | event.session.rollback() | ||
79 | 37 | event.session.close() | ||
80 | 38 | del event['session'] | ||
81 | 39 | |||
82 | 40 | if 'session' in event: | ||
83 | 41 | event.session.close() | ||
84 | 42 | del event['session'] | ||
85 | 43 | |||
86 | 27 | log_level = logging.DEBUG | 44 | log_level = logging.DEBUG |
87 | 28 | if event.type == u'clock' and not event.processed: | 45 | if event.type == u'clock' and not event.processed: |
88 | 29 | log_level -= 5 | 46 | log_level -= 5 |
89 | @@ -263,7 +280,7 @@ | |||
90 | 263 | 280 | ||
91 | 264 | engine.dialect.use_ansiquotes = True | 281 | engine.dialect.use_ansiquotes = True |
92 | 265 | 282 | ||
94 | 266 | self[name] = scoped_session(sessionmaker(bind=engine, transactional=False, autoflush=True)) | 283 | self[name] = scoped_session(sessionmaker(bind=engine)) |
95 | 267 | 284 | ||
96 | 268 | self.log.info(u"Loaded %s database", name) | 285 | self.log.info(u"Loaded %s database", name) |
97 | 269 | 286 | ||
98 | 270 | 287 | ||
99 | === modified file 'ibid/event.py' | |||
100 | --- ibid/event.py 2009-05-01 11:14:06 +0000 | |||
101 | +++ ibid/event.py 2009-05-02 19:29:44 +0000 | |||
102 | @@ -1,3 +1,4 @@ | |||
103 | 1 | import ibid | ||
104 | 1 | 2 | ||
105 | 2 | class Event(dict): | 3 | class Event(dict): |
106 | 3 | 4 | ||
107 | @@ -9,6 +10,8 @@ | |||
108 | 9 | self.processed = False | 10 | self.processed = False |
109 | 10 | 11 | ||
110 | 11 | def __getattr__(self, name): | 12 | def __getattr__(self, name): |
111 | 13 | if name == 'session' and 'session' not in self: | ||
112 | 14 | self['session'] = ibid.databases.ibid() | ||
113 | 12 | return self[name] | 15 | return self[name] |
114 | 13 | 16 | ||
115 | 14 | def __setattr__(self, name, value): | 17 | def __setattr__(self, name, value): |
116 | 15 | 18 | ||
117 | === modified file 'ibid/models.py' | |||
118 | --- ibid/models.py 2009-04-21 20:09:06 +0000 | |||
119 | +++ ibid/models.py 2009-05-02 19:29:44 +0000 | |||
120 | @@ -61,7 +61,6 @@ | |||
121 | 61 | metadata.tables[dependancy].versioned_schema.upgrade_schema(sessionmaker) | 61 | metadata.tables[dependancy].versioned_schema.upgrade_schema(sessionmaker) |
122 | 62 | 62 | ||
123 | 63 | self.upgrade_session = session = sessionmaker() | 63 | self.upgrade_session = session = sessionmaker() |
124 | 64 | trans = session.begin() | ||
125 | 65 | 64 | ||
126 | 66 | schema = session.query(Schema).filter(Schema.table==unicode(self.table.name)).first() | 65 | schema = session.query(Schema).filter(Schema.table==unicode(self.table.name)).first() |
127 | 67 | 66 | ||
128 | @@ -79,8 +78,7 @@ | |||
129 | 79 | for version in range(schema.version + 1, self.version + 1): | 78 | for version in range(schema.version + 1, self.version + 1): |
130 | 80 | log.info(u"Upgrading table %s to version %i", self.table.name, version) | 79 | log.info(u"Upgrading table %s to version %i", self.table.name, version) |
131 | 81 | 80 | ||
134 | 82 | trans.commit() | 81 | session.commit() |
133 | 83 | trans = session.begin() | ||
135 | 84 | 82 | ||
136 | 85 | getattr(self, 'upgrade_%i_to_%i' % (version - 1, version))() | 83 | getattr(self, 'upgrade_%i_to_%i' % (version - 1, version))() |
137 | 86 | 84 | ||
138 | @@ -88,10 +86,10 @@ | |||
139 | 88 | session.save_or_update(schema) | 86 | session.save_or_update(schema) |
140 | 89 | del self.upgrade_reflected_model | 87 | del self.upgrade_reflected_model |
141 | 90 | 88 | ||
143 | 91 | trans.commit() | 89 | session.commit() |
144 | 92 | 90 | ||
145 | 93 | except: | 91 | except: |
147 | 94 | trans.rollback() | 92 | session.rollback() |
148 | 95 | raise | 93 | raise |
149 | 96 | 94 | ||
150 | 97 | session.close() | 95 | session.close() |
151 | @@ -236,7 +234,7 @@ | |||
152 | 236 | schema = Schema(unicode(self.table.name), self.version) | 234 | schema = Schema(unicode(self.table.name), self.version) |
153 | 237 | session.save_or_update(schema) | 235 | session.save_or_update(schema) |
154 | 238 | 236 | ||
156 | 239 | session.flush() | 237 | session.commit() |
157 | 240 | session.close() | 238 | session.close() |
158 | 241 | 239 | ||
159 | 242 | __table__.versioned_schema = SchemaSchema(__table__, 1) | 240 | __table__.versioned_schema = SchemaSchema(__table__, 1) |
160 | 243 | 241 | ||
161 | === modified file 'ibid/plugins/auth.py' | |||
162 | --- ibid/plugins/auth.py 2009-05-01 12:22:37 +0000 | |||
163 | +++ ibid/plugins/auth.py 2009-05-03 16:25:57 +0000 | |||
164 | @@ -5,7 +5,7 @@ | |||
165 | 5 | import ibid | 5 | import ibid |
166 | 6 | from ibid.plugins import Processor, match, auth_responses, authorise | 6 | from ibid.plugins import Processor, match, auth_responses, authorise |
167 | 7 | from ibid.models import Credential, Permission, Account | 7 | from ibid.models import Credential, Permission, Account |
169 | 8 | from ibid.auth import hash, permission | 8 | from ibid.auth import hash |
170 | 9 | 9 | ||
171 | 10 | help = {} | 10 | help = {} |
172 | 11 | 11 | ||
173 | @@ -21,7 +21,6 @@ | |||
174 | 21 | @match(r'^authenticate\s+(.+?)(?:\s+on\s+(.+))?\s+using\s+(\S+)\s+(.+)$') | 21 | @match(r'^authenticate\s+(.+?)(?:\s+on\s+(.+))?\s+using\s+(\S+)\s+(.+)$') |
175 | 22 | def handler(self, event, user, source, method, credential): | 22 | def handler(self, event, user, source, method, credential): |
176 | 23 | 23 | ||
177 | 24 | session = ibid.databases.ibid() | ||
178 | 25 | if user.lower() == 'me': | 24 | if user.lower() == 'me': |
179 | 26 | if not event.account: | 25 | if not event.account: |
180 | 27 | event.addresponse(u"I don't know who you are") | 26 | event.addresponse(u"I don't know who you are") |
181 | @@ -29,15 +28,14 @@ | |||
182 | 29 | if not ibid.auth.authenticate(event): | 28 | if not ibid.auth.authenticate(event): |
183 | 30 | event.complain = 'notauthed' | 29 | event.complain = 'notauthed' |
184 | 31 | return | 30 | return |
186 | 32 | account = session.query(Account).filter_by(id=event.account).first() | 31 | account = event.session.query(Account).filter_by(id=event.account).first() |
187 | 33 | 32 | ||
188 | 34 | else: | 33 | else: |
189 | 35 | if not auth_responses(event, 'admin'): | 34 | if not auth_responses(event, 'admin'): |
190 | 36 | return | 35 | return |
192 | 37 | account = session.query(Account).filter_by(username=user).first() | 36 | account = event.session.query(Account).filter_by(username=user).first() |
193 | 38 | if not account: | 37 | if not account: |
194 | 39 | event.addresponse(u"I don't know who %s is", user) | 38 | event.addresponse(u"I don't know who %s is", user) |
195 | 40 | session.close() | ||
196 | 41 | return | 39 | return |
197 | 42 | 40 | ||
198 | 43 | if source: | 41 | if source: |
199 | @@ -49,14 +47,15 @@ | |||
200 | 49 | if method.lower() == 'password': | 47 | if method.lower() == 'password': |
201 | 50 | password = hash(credential) | 48 | password = hash(credential) |
202 | 51 | event.message['clean'] = event.message['clean'][:-len(credential)] + password | 49 | event.message['clean'] = event.message['clean'][:-len(credential)] + password |
204 | 52 | event.message['raw'] = event.message['raw'][:event.message['raw'].rfind(credential)] + password + event.message['raw'][event.message['raw'].rfind(credential)+len(credential):] | 50 | event.message['raw'] = event.message['raw'][:event.message['raw'].rfind(credential)] \ |
205 | 51 | + password + event.message['raw'][event.message['raw'].rfind(credential)+len(credential):] | ||
206 | 53 | credential = password | 52 | credential = password |
207 | 54 | 53 | ||
208 | 55 | credential = Credential(method, credential, source, account.id) | 54 | credential = Credential(method, credential, source, account.id) |
213 | 56 | session.save_or_update(credential) | 55 | event.session.save_or_update(credential) |
214 | 57 | session.flush() | 56 | event.session.flush() |
215 | 58 | log.info(u"Added %s credential %s for account %s (%s) on %s by account %s", method, credential.credential, account.id, account.username, source, event.account) | 57 | log.info(u"Added %s credential %s for account %s (%s) on %s by account %s", |
216 | 59 | session.close() | 58 | method, credential.credential, account.id, account.username, source, event.account) |
217 | 60 | 59 | ||
218 | 61 | event.addresponse(True) | 60 | event.addresponse(True) |
219 | 62 | 61 | ||
220 | @@ -73,17 +72,17 @@ | |||
221 | 73 | @authorise | 72 | @authorise |
222 | 74 | def grant(self, event, action, name, username, auth): | 73 | def grant(self, event, action, name, username, auth): |
223 | 75 | 74 | ||
226 | 76 | session = ibid.databases.ibid() | 75 | account = event.session.query(Account).filter_by(username=username).first() |
225 | 77 | account = session.query(Account).filter_by(username=username).first() | ||
227 | 78 | if not account: | 76 | if not account: |
228 | 79 | event.addresponse(u"I don't know who %s is", username) | 77 | event.addresponse(u"I don't know who %s is", username) |
229 | 80 | session.close() | ||
230 | 81 | return | 78 | return |
231 | 82 | 79 | ||
233 | 83 | permission = session.query(Permission).filter_by(account_id=account.id).filter(func.lower(Permission.name)==name.lower()).first() | 80 | permission = event.session.query(Permission) \ |
234 | 81 | .filter_by(account_id=account.id) \ | ||
235 | 82 | .filter(func.lower(Permission.name) == name.lower()).first() | ||
236 | 84 | if action.lower() == 'remove': | 83 | if action.lower() == 'remove': |
237 | 85 | if permission: | 84 | if permission: |
239 | 86 | session.delete(permission) | 85 | event.session.delete(permission) |
240 | 87 | else: | 86 | else: |
241 | 88 | event.addresponse(u"%s doesn't have that permission anyway", username) | 87 | event.addresponse(u"%s doesn't have that permission anyway", username) |
242 | 89 | return | 88 | return |
243 | @@ -108,26 +107,25 @@ | |||
244 | 108 | return | 107 | return |
245 | 109 | 108 | ||
246 | 110 | permission.value = value | 109 | permission.value = value |
248 | 111 | session.save_or_update(permission) | 110 | event.session.save_or_update(permission) |
249 | 112 | 111 | ||
253 | 113 | session.flush() | 112 | event.session.flush() |
254 | 114 | log.info(u"%s %s permission for account %s (%s) by account %s", actions[action.lower()], name, account.id, account.username, event.account) | 113 | log.info(u"%s %s permission for account %s (%s) by account %s", |
255 | 115 | session.close() | 114 | actions[action.lower()], name, account.id, account.username, event.account) |
256 | 116 | 115 | ||
257 | 117 | event.addresponse(True) | 116 | event.addresponse(True) |
258 | 118 | 117 | ||
259 | 119 | @match(r'^permissions(?:\s+for\s+(\S+))?$') | 118 | @match(r'^permissions(?:\s+for\s+(\S+))?$') |
260 | 120 | def list(self, event, username): | 119 | def list(self, event, username): |
261 | 121 | session = ibid.databases.ibid() | ||
262 | 122 | if not username: | 120 | if not username: |
263 | 123 | if not event.account: | 121 | if not event.account: |
264 | 124 | event.addresponse(u"I don't know who you are") | 122 | event.addresponse(u"I don't know who you are") |
265 | 125 | return | 123 | return |
267 | 126 | account = session.query(Account).filter_by(id=event.account).first() | 124 | account = event.session.query(Account).filter_by(id=event.account).first() |
268 | 127 | else: | 125 | else: |
269 | 128 | if not auth_responses(event, u'accounts'): | 126 | if not auth_responses(event, u'accounts'): |
270 | 129 | return | 127 | return |
272 | 130 | account = session.query(Account).filter_by(username=username).first() | 128 | account = event.session.query(Account).filter_by(username=username).first() |
273 | 131 | if not account: | 129 | if not account: |
274 | 132 | event.addresponse(u"I don't know who %s is", username) | 130 | event.addresponse(u"I don't know who %s is", username) |
275 | 133 | return | 131 | return |
276 | 134 | 132 | ||
277 | === modified file 'ibid/plugins/factoid.py' | |||
278 | --- ibid/plugins/factoid.py 2009-05-01 12:17:57 +0000 | |||
279 | +++ ibid/plugins/factoid.py 2009-05-03 16:33:10 +0000 | |||
280 | @@ -6,7 +6,6 @@ | |||
281 | 6 | from sqlalchemy.orm import relation, eagerload | 6 | from sqlalchemy.orm import relation, eagerload |
282 | 7 | from sqlalchemy.sql import func | 7 | from sqlalchemy.sql import func |
283 | 8 | 8 | ||
284 | 9 | import ibid | ||
285 | 10 | from ibid.plugins import Processor, match, handler, authorise, auth_responses, RPC | 9 | from ibid.plugins import Processor, match, handler, authorise, auth_responses, RPC |
286 | 11 | from ibid.config import Option, IntOption | 10 | from ibid.config import Option, IntOption |
287 | 12 | from ibid.plugins.identity import get_identities | 11 | from ibid.plugins.identity import get_identities |
288 | @@ -160,9 +159,7 @@ | |||
289 | 160 | 159 | ||
290 | 161 | @match(r'^literal\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$') | 160 | @match(r'^literal\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$') |
291 | 162 | def literal(self, event, name, number, pattern, is_regex): | 161 | def literal(self, event, name, number, pattern, is_regex): |
295 | 163 | session = ibid.databases.ibid() | 162 | factoids = get_factoid(event.session, name, number, pattern, is_regex, literal=True) |
293 | 164 | factoids = get_factoid(session, name, number, pattern, is_regex, literal=True) | ||
294 | 165 | session.close() | ||
296 | 166 | number = number and int(number) or 0 | 163 | number = number and int(number) or 0 |
297 | 167 | if factoids: | 164 | if factoids: |
298 | 168 | event.addresponse(u', '.join(u'%i: %s' | 165 | event.addresponse(u', '.join(u'%i: %s' |
299 | @@ -179,12 +176,11 @@ | |||
300 | 179 | @match(r'^forget\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$') | 176 | @match(r'^forget\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$') |
301 | 180 | @authorise | 177 | @authorise |
302 | 181 | def forget(self, event, name, number, pattern, is_regex): | 178 | def forget(self, event, name, number, pattern, is_regex): |
305 | 182 | session = ibid.databases.ibid() | 179 | factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True) |
304 | 183 | factoids = get_factoid(session, name, number, pattern, is_regex, all=True) | ||
306 | 184 | if factoids: | 180 | if factoids: |
307 | 185 | factoidadmin = auth_responses(event, u'factoidadmin') | 181 | factoidadmin = auth_responses(event, u'factoidadmin') |
310 | 186 | identities = get_identities(event, session) | 182 | identities = get_identities(event) |
311 | 187 | factoid = session.query(Factoid).get(factoids[0][0].id) | 183 | factoid = event.session.query(Factoid).get(factoids[0][0].id) |
312 | 188 | 184 | ||
313 | 189 | if (number or pattern): | 185 | if (number or pattern): |
314 | 190 | if len(factoids) > 1: | 186 | if len(factoids) > 1: |
315 | @@ -194,30 +190,28 @@ | |||
316 | 194 | if factoids[0][2].identity_id not in identities and not factoidadmin: | 190 | if factoids[0][2].identity_id not in identities and not factoidadmin: |
317 | 195 | return | 191 | return |
318 | 196 | 192 | ||
320 | 197 | if session.query(FactoidValue).filter_by(factoid_id=factoid.id).count() == 1: | 193 | if event.session.query(FactoidValue).filter_by(factoid_id=factoid.id).count() == 1: |
321 | 198 | if len(filter(lambda x: x.identity_id not in identities, factoid.names)) > 0 and not factoidadmin: | 194 | if len(filter(lambda x: x.identity_id not in identities, factoid.names)) > 0 and not factoidadmin: |
322 | 199 | return | 195 | return |
323 | 200 | log.info(u"Deleting factoid %s (%s) by %s/%s (%s)", factoid.id, name, event.account, event.identity, event.sender['connection']) | 196 | log.info(u"Deleting factoid %s (%s) by %s/%s (%s)", factoid.id, name, event.account, event.identity, event.sender['connection']) |
325 | 201 | session.delete(factoid) | 197 | event.session.delete(factoid) |
326 | 202 | else: | 198 | else: |
327 | 203 | log.info(u"Deleting value %s of factoid %s (%s) by %s/%s (%s)", factoids[0][2].id, factoid.id, factoids[0][2].value, event.account, event.identity, event.sender['connection']) | 199 | log.info(u"Deleting value %s of factoid %s (%s) by %s/%s (%s)", factoids[0][2].id, factoid.id, factoids[0][2].value, event.account, event.identity, event.sender['connection']) |
329 | 204 | session.delete(factoids[0][2]) | 200 | event.session.delete(factoids[0][2]) |
330 | 205 | 201 | ||
331 | 206 | else: | 202 | else: |
332 | 207 | if factoids[0][1].identity_id not in identities and not factoidadmin: | 203 | if factoids[0][1].identity_id not in identities and not factoidadmin: |
333 | 208 | return | 204 | return |
334 | 209 | 205 | ||
336 | 210 | if session.query(FactoidName).filter_by(factoid_id=factoid.id).count() == 1: | 206 | if event.session.query(FactoidName).filter_by(factoid_id=factoid.id).count() == 1: |
337 | 211 | if len(filter(lambda x: x.identity_id not in identities, factoid.values)) > 0 and not factoidadmin: | 207 | if len(filter(lambda x: x.identity_id not in identities, factoid.values)) > 0 and not factoidadmin: |
338 | 212 | return | 208 | return |
339 | 213 | log.info(u"Deleting factoid %s (%s) by %s/%s (%s)", factoid.id, name, event.account, event.identity, event.sender['connection']) | 209 | log.info(u"Deleting factoid %s (%s) by %s/%s (%s)", factoid.id, name, event.account, event.identity, event.sender['connection']) |
341 | 214 | session.delete(factoid) | 210 | event.session.delete(factoid) |
342 | 215 | else: | 211 | else: |
343 | 216 | log.info(u"Deleting name %s of factoid %s (%s) by %s/%s (%s)", factoids[0][1].id, factoid.id, factoids[0][1].name, event.account, event.identity, event.sender['connection']) | 212 | log.info(u"Deleting name %s of factoid %s (%s) by %s/%s (%s)", factoids[0][1].id, factoid.id, factoids[0][1].name, event.account, event.identity, event.sender['connection']) |
345 | 217 | session.delete(factoids[0][1]) | 213 | event.session.delete(factoids[0][1]) |
346 | 218 | 214 | ||
347 | 219 | session.flush() | ||
348 | 220 | session.close() | ||
349 | 221 | event.addresponse(True) | 215 | event.addresponse(True) |
350 | 222 | else: | 216 | else: |
351 | 223 | event.addresponse(u"I didn't know about %s anyway", name) | 217 | event.addresponse(u"I didn't know about %s anyway", name) |
352 | @@ -230,15 +224,12 @@ | |||
353 | 230 | event.addresponse(u"That makes no sense, they *are* the same") | 224 | event.addresponse(u"That makes no sense, they *are* the same") |
354 | 231 | return | 225 | return |
355 | 232 | 226 | ||
358 | 233 | session = ibid.databases.ibid() | 227 | factoid = event.session.query(Factoid).join(Factoid.names)\ |
357 | 234 | factoid = session.query(Factoid).join(Factoid.names)\ | ||
359 | 235 | .filter(func.lower(FactoidName.name)==escape_name(source).lower()).first() | 228 | .filter(func.lower(FactoidName.name)==escape_name(source).lower()).first() |
360 | 236 | if factoid: | 229 | if factoid: |
361 | 237 | name = FactoidName(escape_name(unicode(target)), event.identity) | 230 | name = FactoidName(escape_name(unicode(target)), event.identity) |
362 | 238 | factoid.names.append(name) | 231 | factoid.names.append(name) |
366 | 239 | session.save_or_update(factoid) | 232 | event.session.save_or_update(factoid) |
364 | 240 | session.flush() | ||
365 | 241 | session.close() | ||
367 | 242 | event.addresponse(True) | 233 | event.addresponse(True) |
368 | 243 | log.info(u"Added name '%s' to factoid %s by %s/%s (%s)", name.name, factoid.id, event.account, event.identity, event.sender['connection']) | 234 | log.info(u"Added name '%s' to factoid %s by %s/%s (%s)", name.name, factoid.id, event.account, event.identity, event.sender['connection']) |
369 | 244 | else: | 235 | else: |
370 | @@ -266,8 +257,7 @@ | |||
371 | 266 | pattern = m.group(1) | 257 | pattern = m.group(1) |
372 | 267 | is_regex = bool(m.group(2)) | 258 | is_regex = bool(m.group(2)) |
373 | 268 | 259 | ||
376 | 269 | session = ibid.databases.ibid() | 260 | query = event.session.query(Factoid)\ |
375 | 270 | query = session.query(Factoid)\ | ||
377 | 271 | .join(Factoid.names).add_entity(FactoidName)\ | 261 | .join(Factoid.names).add_entity(FactoidName)\ |
378 | 272 | .join(Factoid.values) | 262 | .join(Factoid.values) |
379 | 273 | 263 | ||
380 | @@ -320,9 +310,7 @@ | |||
381 | 320 | event.addresponse(response) | 310 | event.addresponse(response) |
382 | 321 | 311 | ||
383 | 322 | def remote_get(self, name, number=None, pattern=None, is_regex=None, event={}): | 312 | def remote_get(self, name, number=None, pattern=None, is_regex=None, event={}): |
387 | 323 | session = ibid.databases.ibid() | 313 | factoid = get_factoid(event.session, name, number, pattern, is_regex) |
385 | 324 | factoid = get_factoid(session, name, number, pattern, is_regex) | ||
386 | 325 | session.close() | ||
388 | 326 | 314 | ||
389 | 327 | if factoid: | 315 | if factoid: |
390 | 328 | (factoid, fname, fvalue) = factoid | 316 | (factoid, fname, fvalue) = factoid |
391 | @@ -377,17 +365,15 @@ | |||
392 | 377 | def set_factoid(self, event, correction, name, verb1, verb2, addition, value): | 365 | def set_factoid(self, event, correction, name, verb1, verb2, addition, value): |
393 | 378 | verb = verb1 and verb1 or verb2 | 366 | verb = verb1 and verb1 or verb2 |
394 | 379 | 367 | ||
397 | 380 | session = ibid.databases.ibid() | 368 | factoid = event.session.query(Factoid).join(Factoid.names)\ |
396 | 381 | factoid = session.query(Factoid).join(Factoid.names)\ | ||
398 | 382 | .filter(func.lower(FactoidName.name)==escape_name(name).lower()).first() | 369 | .filter(func.lower(FactoidName.name)==escape_name(name).lower()).first() |
399 | 383 | if factoid: | 370 | if factoid: |
400 | 384 | if correction: | 371 | if correction: |
402 | 385 | identities = get_identities(event, session) | 372 | identities = get_identities(event) |
403 | 386 | if not auth_responses(event, u'factoidadmin') and len(filter(lambda x: x.identity_id not in identities, factoid.values)) > 0: | 373 | if not auth_responses(event, u'factoidadmin') and len(filter(lambda x: x.identity_id not in identities, factoid.values)) > 0: |
404 | 387 | return | 374 | return |
405 | 388 | for fvalue in factoid.values: | 375 | for fvalue in factoid.values: |
408 | 389 | session.delete(fvalue) | 376 | event.session.delete(fvalue) |
407 | 390 | session.flush() | ||
409 | 391 | elif not addition: | 377 | elif not addition: |
410 | 392 | event.addresponse(u'I already know stuff about %s', name) | 378 | event.addresponse(u'I already know stuff about %s', name) |
411 | 393 | return | 379 | return |
412 | @@ -395,6 +381,7 @@ | |||
413 | 395 | factoid = Factoid() | 381 | factoid = Factoid() |
414 | 396 | fname = FactoidName(escape_name(unicode(name)), event.identity) | 382 | fname = FactoidName(escape_name(unicode(name)), event.identity) |
415 | 397 | factoid.names.append(fname) | 383 | factoid.names.append(fname) |
416 | 384 | event.session.flush() | ||
417 | 398 | log.info(u"Created factoid %s with name '%s' by %s", factoid.id, fname.name, event.identity) | 385 | log.info(u"Created factoid %s with name '%s' by %s", factoid.id, fname.name, event.identity) |
418 | 399 | 386 | ||
419 | 400 | if not reply_re.match(value) and not action_re.match(value): | 387 | if not reply_re.match(value) and not action_re.match(value): |
420 | @@ -402,9 +389,7 @@ | |||
421 | 402 | fvalue = FactoidValue(unicode(value), event.identity) | 389 | fvalue = FactoidValue(unicode(value), event.identity) |
422 | 403 | factoid.values.append(fvalue) | 390 | factoid.values.append(fvalue) |
423 | 404 | log.info(u"Added value '%s' to factoid %s by %s/%s (%s)", fvalue.value, factoid.id, event.account, event.identity, event.sender['connection']) | 391 | log.info(u"Added value '%s' to factoid %s by %s/%s (%s)", fvalue.value, factoid.id, event.account, event.identity, event.sender['connection']) |
427 | 405 | session.save_or_update(factoid) | 392 | event.session.save_or_update(factoid) |
425 | 406 | session.flush() | ||
426 | 407 | session.close() | ||
428 | 408 | event.addresponse(True) | 393 | event.addresponse(True) |
429 | 409 | 394 | ||
430 | 410 | class Modify(Processor): | 395 | class Modify(Processor): |
431 | @@ -419,8 +404,7 @@ | |||
432 | 419 | @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*\+=\s?(.+)$') | 404 | @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*\+=\s?(.+)$') |
433 | 420 | @authorise | 405 | @authorise |
434 | 421 | def append(self, event, name, number, pattern, is_regex, suffix): | 406 | def append(self, event, name, number, pattern, is_regex, suffix): |
437 | 422 | session = ibid.databases.ibid() | 407 | factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True) |
436 | 423 | factoids = get_factoid(session, name, number, pattern, is_regex, all=True) | ||
438 | 424 | if len(factoids) == 0: | 408 | if len(factoids) == 0: |
439 | 425 | if pattern: | 409 | if pattern: |
440 | 426 | event.addresponse(u"I don't know about any %(name)s matching %(pattern)s", { | 410 | event.addresponse(u"I don't know about any %(name)s matching %(pattern)s", { |
441 | @@ -433,7 +417,7 @@ | |||
442 | 433 | event.addresponse(u"Pattern matches multiple factoids, please be more specific") | 417 | event.addresponse(u"Pattern matches multiple factoids, please be more specific") |
443 | 434 | else: | 418 | else: |
444 | 435 | factoidadmin = auth_responses(event, u'factoidadmin') | 419 | factoidadmin = auth_responses(event, u'factoidadmin') |
446 | 436 | identities = get_identities(event, session) | 420 | identities = get_identities(event) |
447 | 437 | factoid = factoids[0] | 421 | factoid = factoids[0] |
448 | 438 | 422 | ||
449 | 439 | if factoid[2].identity_id not in identities and not factoidadmin: | 423 | if factoid[2].identity_id not in identities and not factoidadmin: |
450 | @@ -443,16 +427,13 @@ | |||
451 | 443 | suffix, factoid[2].id, factoid[0].id, factoid[2].value, event.account, event.identity, event.sender['connection']) | 427 | suffix, factoid[2].id, factoid[0].id, factoid[2].value, event.account, event.identity, event.sender['connection']) |
452 | 444 | factoid[2].value += suffix | 428 | factoid[2].value += suffix |
453 | 445 | 429 | ||
457 | 446 | session.save_or_update(factoid[2]) | 430 | event.session.save_or_update(factoid[2]) |
455 | 447 | session.flush() | ||
456 | 448 | session.close() | ||
458 | 449 | event.addresponse(True) | 431 | event.addresponse(True) |
459 | 450 | 432 | ||
460 | 451 | @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*(?:~=|=~)\s*([sy](?P<sep>.).+(?P=sep).*(?P=sep)[gir]*)$') | 433 | @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*(?:~=|=~)\s*([sy](?P<sep>.).+(?P=sep).*(?P=sep)[gir]*)$') |
461 | 452 | @authorise | 434 | @authorise |
462 | 453 | def modify(self, event, name, number, pattern, is_regex, operation, separator): | 435 | def modify(self, event, name, number, pattern, is_regex, operation, separator): |
465 | 454 | session = ibid.databases.ibid() | 436 | factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True) |
464 | 455 | factoids = get_factoid(session, name, number, pattern, is_regex, all=True) | ||
466 | 456 | if len(factoids) == 0: | 437 | if len(factoids) == 0: |
467 | 457 | if pattern: | 438 | if pattern: |
468 | 458 | event.addresponse(u"I don't know about any %(name)s matching %(pattern)s", { | 439 | event.addresponse(u"I don't know about any %(name)s matching %(pattern)s", { |
469 | @@ -465,7 +446,7 @@ | |||
470 | 465 | event.addresponse(u"Pattern matches multiple factoids, please be more specific") | 446 | event.addresponse(u"Pattern matches multiple factoids, please be more specific") |
471 | 466 | else: | 447 | else: |
472 | 467 | factoidadmin = auth_responses(event, u'factoidadmin') | 448 | factoidadmin = auth_responses(event, u'factoidadmin') |
474 | 468 | identities = get_identities(event, session) | 449 | identities = get_identities(event) |
475 | 469 | factoid = factoids[0] | 450 | factoid = factoids[0] |
476 | 470 | 451 | ||
477 | 471 | if factoid[2].identity_id not in identities and not factoidadmin: | 452 | if factoid[2].identity_id not in identities and not factoidadmin: |
478 | @@ -537,9 +518,7 @@ | |||
479 | 537 | log.info(u"Applying '%s' to value %s of factoid %s (%s) by %s/%s (%s)", | 518 | log.info(u"Applying '%s' to value %s of factoid %s (%s) by %s/%s (%s)", |
480 | 538 | operation, factoid[2].id, factoid[0].id, oldvalue, event.account, event.identity, event.sender['connection']) | 519 | operation, factoid[2].id, factoid[0].id, oldvalue, event.account, event.identity, event.sender['connection']) |
481 | 539 | 520 | ||
485 | 540 | session.save_or_update(factoid[2]) | 521 | event.session.save_or_update(factoid[2]) |
483 | 541 | session.flush() | ||
484 | 542 | session.close() | ||
486 | 543 | event.addresponse(True) | 522 | event.addresponse(True) |
487 | 544 | 523 | ||
488 | 545 | # vi: set et sta sw=4 ts=4: | 524 | # vi: set et sta sw=4 ts=4: |
489 | 546 | 525 | ||
490 | === modified file 'ibid/plugins/feeds.py' | |||
491 | --- ibid/plugins/feeds.py 2009-05-01 12:17:57 +0000 | |||
492 | +++ ibid/plugins/feeds.py 2009-05-03 16:33:10 +0000 | |||
493 | @@ -10,7 +10,6 @@ | |||
494 | 10 | import feedparser | 10 | import feedparser |
495 | 11 | from html2text import html2text_file | 11 | from html2text import html2text_file |
496 | 12 | 12 | ||
497 | 13 | import ibid | ||
498 | 14 | from ibid.plugins import Processor, match, authorise | 13 | from ibid.plugins import Processor, match, authorise |
499 | 15 | from ibid.models import Base, VersionedSchema | 14 | from ibid.models import Base, VersionedSchema |
500 | 16 | from ibid.utils import cacheable_download, get_html_parse_tree | 15 | from ibid.utils import cacheable_download, get_html_parse_tree |
501 | @@ -56,8 +55,8 @@ | |||
502 | 56 | @match(r'^add\s+feed\s+(.+?)\s+as\s+(.+?)$') | 55 | @match(r'^add\s+feed\s+(.+?)\s+as\s+(.+?)$') |
503 | 57 | @authorise | 56 | @authorise |
504 | 58 | def add(self, event, url, name): | 57 | def add(self, event, url, name): |
507 | 59 | session = ibid.databases.ibid() | 58 | feed = event.session.query(Feed) \ |
508 | 60 | feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first() | 59 | .filter(func.lower(Feed.name) == name.lower()).first() |
509 | 61 | 60 | ||
510 | 62 | if feed: | 61 | if feed: |
511 | 63 | event.addresponse(u"I already have the %s feed", name) | 62 | event.addresponse(u"I already have the %s feed", name) |
512 | @@ -85,15 +84,13 @@ | |||
513 | 85 | return | 84 | return |
514 | 86 | 85 | ||
515 | 87 | feed = Feed(unicode(name), unicode(url), event.identity) | 86 | feed = Feed(unicode(name), unicode(url), event.identity) |
518 | 88 | session.save(feed) | 87 | event.session.save(feed) |
517 | 89 | session.flush() | ||
519 | 90 | event.addresponse(True) | 88 | event.addresponse(True) |
520 | 91 | log.info(u"Added feed '%s' by %s/%s (%s): %s (Found %s entries)", name, event.account, event.identity, event.sender['connection'], url, len(feed.entries)) | 89 | log.info(u"Added feed '%s' by %s/%s (%s): %s (Found %s entries)", name, event.account, event.identity, event.sender['connection'], url, len(feed.entries)) |
521 | 92 | 90 | ||
522 | 93 | @match(r'^(?:list\s+)?feeds$') | 91 | @match(r'^(?:list\s+)?feeds$') |
523 | 94 | def list(self, event): | 92 | def list(self, event): |
526 | 95 | session = ibid.databases.ibid() | 93 | feeds = event.session.query(Feed).all() |
525 | 96 | feeds = session.query(Feed).all() | ||
527 | 97 | if feeds: | 94 | if feeds: |
528 | 98 | event.addresponse(u'I know about: %s', u', '.join(sorted([feed.name for feed in feeds]))) | 95 | event.addresponse(u'I know about: %s', u', '.join(sorted([feed.name for feed in feeds]))) |
529 | 99 | else: | 96 | else: |
530 | @@ -102,19 +99,16 @@ | |||
531 | 102 | @match(r'^remove\s+(.+?)\s+feed$') | 99 | @match(r'^remove\s+(.+?)\s+feed$') |
532 | 103 | @authorise | 100 | @authorise |
533 | 104 | def remove(self, event, name): | 101 | def remove(self, event, name): |
536 | 105 | session = ibid.databases.ibid() | 102 | feed = event.session.query(Feed) \ |
537 | 106 | feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first() | 103 | .filter(func.lower(Feed.name) == name.lower()).first() |
538 | 107 | 104 | ||
539 | 108 | if not feed: | 105 | if not feed: |
540 | 109 | event.addresponse(u"I don't have the %s feed anyway", name) | 106 | event.addresponse(u"I don't have the %s feed anyway", name) |
541 | 110 | else: | 107 | else: |
543 | 111 | session.delete(feed) | 108 | event.session.delete(feed) |
544 | 112 | log.info(u"Deleted feed '%s' by %s/%s (%s): %s", name, event.account, event.identity, event.sender['connection'], feed.url) | 109 | log.info(u"Deleted feed '%s' by %s/%s (%s): %s", name, event.account, event.identity, event.sender['connection'], feed.url) |
545 | 113 | session.flush() | ||
546 | 114 | event.addresponse(True) | 110 | event.addresponse(True) |
547 | 115 | 111 | ||
548 | 116 | session.close() | ||
549 | 117 | |||
550 | 118 | class Retrieve(Processor): | 112 | class Retrieve(Processor): |
551 | 119 | u"""latest [ <count> ] articles from <name> [ starting at <number> ] | 113 | u"""latest [ <count> ] articles from <name> [ starting at <number> ] |
552 | 120 | article ( <number> | /<pattern>/ ) from <name>""" | 114 | article ( <number> | /<pattern>/ ) from <name>""" |
553 | @@ -125,9 +119,8 @@ | |||
554 | 125 | number = number and int(number) or 10 | 119 | number = number and int(number) or 10 |
555 | 126 | start = start and int(start) or 0 | 120 | start = start and int(start) or 0 |
556 | 127 | 121 | ||
560 | 128 | session = ibid.databases.ibid() | 122 | feed = event.session.query(Feed) \ |
561 | 129 | feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first() | 123 | .filter(func.lower(Feed.name) == name.lower()).first() |
559 | 130 | session.close() | ||
562 | 131 | 124 | ||
563 | 132 | if not feed: | 125 | if not feed: |
564 | 133 | event.addresponse(u"I don't know about the %s feed", name) | 126 | event.addresponse(u"I don't know about the %s feed", name) |
565 | @@ -144,9 +137,8 @@ | |||
566 | 144 | 137 | ||
567 | 145 | @match(r'^article\s+(?:(\d+)|/(.+?)/)\s+from\s+(.+?)$') | 138 | @match(r'^article\s+(?:(\d+)|/(.+?)/)\s+from\s+(.+?)$') |
568 | 146 | def article(self, event, number, pattern, name): | 139 | def article(self, event, number, pattern, name): |
572 | 147 | session = ibid.databases.ibid() | 140 | feed = event.session.query(Feed) \ |
573 | 148 | feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first() | 141 | .filter(func.lower(Feed.name) == name.lower()).first() |
571 | 149 | session.close() | ||
574 | 150 | 142 | ||
575 | 151 | if not feed: | 143 | if not feed: |
576 | 152 | event.addresponse(u"I don't know about the %s feed", name) | 144 | event.addresponse(u"I don't know about the %s feed", name) |
577 | 153 | 145 | ||
578 | === modified file 'ibid/plugins/identity.py' | |||
579 | --- ibid/plugins/identity.py 2009-04-27 00:50:19 +0000 | |||
580 | +++ ibid/plugins/identity.py 2009-05-03 16:37:03 +0000 | |||
581 | @@ -15,43 +15,45 @@ | |||
582 | 15 | 15 | ||
583 | 16 | log = logging.getLogger('plugins.identity') | 16 | log = logging.getLogger('plugins.identity') |
584 | 17 | 17 | ||
586 | 18 | help['accounts'] = u'An account represents a person. An account has one or more identities, which is a user on a specific source.' | 18 | help['accounts'] = u'An account represents a person. ' \ |
587 | 19 | 'An account has one or more identities, which is a user on a specific source.' | ||
588 | 19 | class Accounts(Processor): | 20 | class Accounts(Processor): |
589 | 20 | u"""create account <name>""" | 21 | u"""create account <name>""" |
590 | 21 | feature = 'accounts' | 22 | feature = 'accounts' |
591 | 22 | 23 | ||
592 | 23 | @match(r'^create\s+account\s+(.+)$') | 24 | @match(r'^create\s+account\s+(.+)$') |
593 | 24 | def account(self, event, username): | 25 | def account(self, event, username): |
594 | 25 | session = ibid.databases.ibid() | ||
595 | 26 | admin = False | 26 | admin = False |
596 | 27 | 27 | ||
597 | 28 | if event.account: | 28 | if event.account: |
598 | 29 | if ibid.auth.authenticate(event) and ibid.auth.authorise(event, 'accounts'): | 29 | if ibid.auth.authenticate(event) and ibid.auth.authorise(event, 'accounts'): |
599 | 30 | admin = True | 30 | admin = True |
600 | 31 | else: | 31 | else: |
602 | 32 | account = session.query(Account).filter_by(id=event.account).first() | 32 | account = event.session.query(Account).filter_by(id=event.account).first() |
603 | 33 | event.addresponse(u'You already have an account called "%s"', account.username) | 33 | event.addresponse(u'You already have an account called "%s"', account.username) |
604 | 34 | return | 34 | return |
605 | 35 | 35 | ||
607 | 36 | account = session.query(Account).filter_by(username=username).first() | 36 | account = event.session.query(Account).filter_by(username=username).first() |
608 | 37 | if account: | 37 | if account: |
610 | 38 | event.addresponse(u'There is already an account called "%s". Please choose a different name', account.username) | 38 | event.addresponse(u'There is already an account called "%s". ' \ |
611 | 39 | 'Please choose a different name', account.username) | ||
612 | 39 | return | 40 | return |
613 | 40 | 41 | ||
614 | 41 | account = Account(username) | 42 | account = Account(username) |
618 | 42 | session.save_or_update(account) | 43 | event.session.save_or_update(account) |
619 | 43 | session.flush() | 44 | event.session.flush() |
620 | 44 | log.info(u"Created account %s (%s) by %s/%s (%s)", account.id, account.username, event.account, event.identity, event.sender['connection']) | 45 | log.info(u"Created account %s (%s) by %s/%s (%s)", |
621 | 46 | account.id, account.username, event.account, event.identity, event.sender['connection']) | ||
622 | 45 | 47 | ||
623 | 46 | if not admin: | 48 | if not admin: |
625 | 47 | identity = session.query(Identity).filter_by(id=event.identity).first() | 49 | identity = event.session.query(Identity).filter_by(id=event.identity).first() |
626 | 48 | identity.account_id = account.id | 50 | identity.account_id = account.id |
627 | 49 | session.save_or_update(identity) | 51 | session.save_or_update(identity) |
630 | 50 | session.flush() | 52 | event.session.flush() |
631 | 51 | log.info(u"Attached identity %s (%s on %s) to account %s (%s)", identity.id, identity.identity, identity.source, account.id, account.username) | 53 | log.info(u"Attached identity %s (%s on %s) to account %s (%s)", |
632 | 54 | identity.id, identity.identity, identity.source, account.id, account.username) | ||
633 | 52 | 55 | ||
634 | 53 | identify_cache.clear() | 56 | identify_cache.clear() |
635 | 54 | session.close() | ||
636 | 55 | event.addresponse(True) | 57 | event.addresponse(True) |
637 | 56 | 58 | ||
638 | 57 | chars = string.letters + string.digits | 59 | chars = string.letters + string.digits |
639 | @@ -68,44 +70,54 @@ | |||
640 | 68 | 70 | ||
641 | 69 | @match(r'^(I|.+?)\s+(?:is|am)\s+(.+)\s+on\s+(.+)$') | 71 | @match(r'^(I|.+?)\s+(?:is|am)\s+(.+)\s+on\s+(.+)$') |
642 | 70 | def identity(self, event, username, identity, source): | 72 | def identity(self, event, username, identity, source): |
643 | 71 | session = ibid.databases.ibid() | ||
644 | 72 | admin = False | 73 | admin = False |
645 | 73 | identity = identity.replace(' ', '') | 74 | identity = identity.replace(' ', '') |
646 | 74 | 75 | ||
647 | 75 | if username.upper() == 'I': | 76 | if username.upper() == 'I': |
648 | 76 | if event.account: | 77 | if event.account: |
650 | 77 | account = session.query(Account).filter_by(id=event.account).first() | 78 | account = event.session.query(Account).filter_by(id=event.account).first() |
651 | 78 | else: | 79 | else: |
652 | 79 | username = event.sender['id'] | 80 | username = event.sender['id'] |
654 | 80 | account = session.query(Account).filter_by(username=username).first() | 81 | |
655 | 82 | account = event.session.query(Account).filter_by(username=username).first() | ||
656 | 83 | |||
657 | 81 | if account: | 84 | if account: |
658 | 82 | event.addresponse(u'I tried to create the account %s for you, but it already exists. ' | 85 | event.addresponse(u'I tried to create the account %s for you, but it already exists. ' |
659 | 83 | u"Please use 'create account <name>'", username) | 86 | u"Please use 'create account <name>'", username) |
660 | 84 | return | 87 | return |
661 | 88 | |||
662 | 85 | account = Account(username) | 89 | account = Account(username) |
666 | 86 | session.save_or_update(account) | 90 | event.session.save_or_update(account) |
667 | 87 | session.flush() | 91 | |
668 | 88 | currentidentity = session.query(Identity).filter_by(id=event.identity).first() | 92 | currentidentity = event.session.query(Identity).filter_by(id=event.identity).first() |
669 | 89 | currentidentity.account_id = account.id | 93 | currentidentity.account_id = account.id |
672 | 90 | session.save_or_update(currentidentity) | 94 | event.session.save_or_update(currentidentity) |
673 | 91 | session.flush() | 95 | |
674 | 92 | identify_cache.clear() | 96 | identify_cache.clear() |
675 | 97 | |||
676 | 93 | event.addresponse(u"I've created the account %s for you", username) | 98 | event.addresponse(u"I've created the account %s for you", username) |
679 | 94 | log.info(u"Created account %s (%s) by %s/%s (%s)", account.id, account.username, event.account, event.identity, event.sender['connection']) | 99 | |
680 | 95 | log.info(u"Attached identity %s (%s on %s) to account %s (%s)", currentidentity.id, currentidentity.identity, currentidentity.source, account.id, account.username) | 100 | event.session.flush() |
681 | 101 | log.info(u"Created account %s (%s) by %s/%s (%s)", | ||
682 | 102 | account.id, account.username, event.account, event.identity, event.sender['connection']) | ||
683 | 103 | log.info(u"Attached identity %s (%s on %s) to account %s (%s)", | ||
684 | 104 | currentidentity.id, currentidentity.identity, currentidentity.source, account.id, account.username) | ||
685 | 96 | 105 | ||
686 | 97 | else: | 106 | else: |
687 | 98 | if not auth_responses(event, 'accounts'): | 107 | if not auth_responses(event, 'accounts'): |
688 | 99 | return | 108 | return |
689 | 100 | admin = True | 109 | admin = True |
691 | 101 | account = session.query(Account).filter_by(username=username).first() | 110 | account = event.session.query(Account).filter_by(username=username).first() |
692 | 102 | if not account: | 111 | if not account: |
693 | 103 | event.addresponse(u"I don't know who %s is", username) | 112 | event.addresponse(u"I don't know who %s is", username) |
694 | 104 | return | 113 | return |
695 | 105 | 114 | ||
697 | 106 | ident = session.query(Identity).filter(func.lower(Identity.identity)==identity.lower()).filter(func.lower(Identity.source)==source.lower()).first() | 115 | ident = event.session.query(Identity) \ |
698 | 116 | .filter(func.lower(Identity.identity) == identity.lower()) \ | ||
699 | 117 | .filter(func.lower(Identity.source) == source.lower()).first() | ||
700 | 107 | if ident and ident.account: | 118 | if ident and ident.account: |
702 | 108 | event.addresponse(u'This identity is already attached to account %s', ident.account.username) | 119 | event.addresponse(u'This identity is already attached to account %s', |
703 | 120 | ident.account.username) | ||
704 | 109 | return | 121 | return |
705 | 110 | 122 | ||
706 | 111 | if source not in ibid.sources: | 123 | if source not in ibid.sources: |
707 | @@ -121,22 +133,26 @@ | |||
708 | 121 | if event.public: | 133 | if event.public: |
709 | 122 | response['target'] = event.sender['id'] | 134 | response['target'] = event.sender['id'] |
710 | 123 | event.addresponse(response) | 135 | event.addresponse(response) |
712 | 124 | log.info(u"Sent token %s to %s/%s (%s)", token, event.account, event.identity, event.sender['connection']) | 136 | log.info(u"Sent token %s to %s/%s (%s)", |
713 | 137 | token, event.account, event.identity, event.sender['connection']) | ||
714 | 125 | 138 | ||
715 | 126 | else: | 139 | else: |
716 | 127 | if not ident: | 140 | if not ident: |
717 | 128 | ident = Identity(source, identity) | 141 | ident = Identity(source, identity) |
718 | 129 | ident.account_id = account.id | 142 | ident.account_id = account.id |
721 | 130 | session.save_or_update(ident) | 143 | event.session.save_or_update(ident) |
722 | 131 | session.flush() | 144 | |
723 | 132 | identify_cache.clear() | 145 | identify_cache.clear() |
724 | 133 | event.addresponse(True) | 146 | event.addresponse(True) |
726 | 134 | log.info(u"Attached identity %s (%s on %s) to account %s (%s) by %s/%s (%s)", ident.id, ident.identity, ident.source, account.id, account.username, event.account, event.identity, event.sender['connection']) | 147 | |
727 | 148 | event.session.flush() | ||
728 | 149 | log.info(u"Attached identity %s (%s on %s) to account %s (%s) by %s/%s (%s)", | ||
729 | 150 | ident.id, ident.identity, ident.source, account.id, account.username, | ||
730 | 151 | event.account, event.identity, event.sender['connection']) | ||
731 | 135 | 152 | ||
732 | 136 | @match(r'^(\S{16})$') | 153 | @match(r'^(\S{16})$') |
733 | 137 | def token(self, event, token): | 154 | def token(self, event, token): |
734 | 138 | if token in self.tokens: | 155 | if token in self.tokens: |
735 | 139 | session = ibid.databases.ibid() | ||
736 | 140 | (account_id, user, source) = self.tokens[token] | 156 | (account_id, user, source) = self.tokens[token] |
737 | 141 | if event.source.lower() != source.lower() or event.sender['id'].lower() != user.lower(): | 157 | if event.source.lower() != source.lower() or event.sender['id'].lower() != user.lower(): |
738 | 142 | event.addresponse(u'You need to send me this token from %(name)s on %(source)s', { | 158 | event.addresponse(u'You need to send me this token from %(name)s on %(source)s', { |
739 | @@ -145,45 +161,49 @@ | |||
740 | 145 | }) | 161 | }) |
741 | 146 | return | 162 | return |
742 | 147 | 163 | ||
744 | 148 | identity = session.query(Identity).filter(func.lower(Identity.identity)==user.lower()).filter(func.lower(Identity.source)==source.lower()).first() | 164 | identity = event.session.query(Identity) \ |
745 | 165 | .filter(func.lower(Identity.identity) == user.lower()) \ | ||
746 | 166 | .filter(func.lower(Identity.source) == source.lower()).first() | ||
747 | 149 | if not identity: | 167 | if not identity: |
748 | 150 | identity = Identity(source, user) | 168 | identity = Identity(source, user) |
749 | 151 | identity.account_id = account_id | 169 | identity.account_id = account_id |
753 | 152 | session.save_or_update(identity) | 170 | event.session.save_or_update(identity) |
751 | 153 | session.flush() | ||
752 | 154 | session.close() | ||
754 | 155 | identify_cache.clear() | 171 | identify_cache.clear() |
755 | 156 | 172 | ||
756 | 157 | del self.tokens[token] | 173 | del self.tokens[token] |
757 | 158 | event.addresponse(u'Identity added') | 174 | event.addresponse(u'Identity added') |
759 | 159 | log.info(u"Attached identity %s (%s on %s) to account %s by %s/%s (%s) with token %s", identity.id, identity.identity, identity.source, account_id, event.account, event.identity, event.sender['connection'], token) | 175 | |
760 | 176 | event.session.flush() | ||
761 | 177 | log.info(u"Attached identity %s (%s on %s) to account %s by %s/%s (%s) with token %s", | ||
762 | 178 | identity.id, identity.identity, identity.source, account_id, event.account, | ||
763 | 179 | event.identity, event.sender['connection'], token) | ||
764 | 160 | 180 | ||
765 | 161 | @match(r'^remove\s+identity\s+(.+?)\s+on\s+(\S+)(?:\s+from\s+(\S+))?$') | 181 | @match(r'^remove\s+identity\s+(.+?)\s+on\s+(\S+)(?:\s+from\s+(\S+))?$') |
766 | 162 | def remove(self, event, user, source, username): | 182 | def remove(self, event, user, source, username): |
767 | 163 | session = ibid.databases.ibid() | ||
768 | 164 | |||
769 | 165 | if not username: | 183 | if not username: |
771 | 166 | account = session.query(Account).get(event.account) | 184 | account = event.session.query(Account).get(event.account) |
772 | 167 | else: | 185 | else: |
773 | 168 | if not auth_responses(event, 'accounts'): | 186 | if not auth_responses(event, 'accounts'): |
774 | 169 | return | 187 | return |
776 | 170 | account = session.query(Account).filter_by(username=username).first() | 188 | account = event.session.query(Account).filter_by(username=username).first() |
777 | 171 | if not account: | 189 | if not account: |
778 | 172 | event.addresponse(u"I don't know who %s is", username) | 190 | event.addresponse(u"I don't know who %s is", username) |
779 | 173 | return | 191 | return |
780 | 174 | 192 | ||
782 | 175 | identity = session.query(Identity).filter_by(account_id=account.id).filter(func.lower(Identity.identity)==user.lower()).filter(func.lower(Identity.source)==source.lower()).first() | 193 | identity = event.session.query(Identity) \ |
783 | 194 | .filter_by(account_id=account.id) \ | ||
784 | 195 | .filter(func.lower(Identity.identity) == user.lower()) \ | ||
785 | 196 | .filter(func.lower(Identity.source) == source.lower()).first() | ||
786 | 176 | if not identity: | 197 | if not identity: |
787 | 177 | event.addresponse(u"I don't know about that identity") | 198 | event.addresponse(u"I don't know about that identity") |
788 | 178 | else: | 199 | else: |
789 | 179 | identity.account_id = None | 200 | identity.account_id = None |
792 | 180 | session.save_or_update(identity) | 201 | event.session.save_or_update(identity) |
791 | 181 | session.flush() | ||
793 | 182 | identify_cache.clear() | 202 | identify_cache.clear() |
794 | 183 | event.addresponse(True) | 203 | event.addresponse(True) |
798 | 184 | log.info(u"Removed identity %s (%s on %s) from account %s (%s) by %s/%s (%s)", identity.id, identity.identity, identity.source, account.id, account.username, event.account, event.identity, event.sender['connection']) | 204 | log.info(u"Removed identity %s (%s on %s) from account %s (%s) by %s/%s (%s)", |
799 | 185 | 205 | identity.id, identity.identity, identity.source, account.id, | |
800 | 186 | session.close() | 206 | account.username, event.account, event.identity, event.sender['connection']) |
801 | 187 | 207 | ||
802 | 188 | class Attributes(Processor): | 208 | class Attributes(Processor): |
803 | 189 | u"""set (my|<account>) <name> to <value>""" | 209 | u"""set (my|<account>) <name> to <value>""" |
804 | @@ -191,13 +211,12 @@ | |||
805 | 191 | 211 | ||
806 | 192 | @match(r"^set\s+(my|.+?)(?:\'s)?\s+(.+)\s+to\s+(.+)$") | 212 | @match(r"^set\s+(my|.+?)(?:\'s)?\s+(.+)\s+to\s+(.+)$") |
807 | 193 | def attribute(self, event, username, name, value): | 213 | def attribute(self, event, username, name, value): |
808 | 194 | session = ibid.databases.ibid() | ||
809 | 195 | 214 | ||
810 | 196 | if username.lower() == 'my': | 215 | if username.lower() == 'my': |
811 | 197 | if not event.account: | 216 | if not event.account: |
812 | 198 | event.addresponse(u"I don't know who you are") | 217 | event.addresponse(u"I don't know who you are") |
813 | 199 | return | 218 | return |
815 | 200 | account = session.query(Account).filter_by(id=event.account).first() | 219 | account = event.session.query(Account).filter_by(id=event.account).first() |
816 | 201 | if not account: | 220 | if not account: |
817 | 202 | event.addresponse(u"%s doesn't exist. Please use 'add account' first", username) | 221 | event.addresponse(u"%s doesn't exist. Please use 'add account' first", username) |
818 | 203 | return | 222 | return |
819 | @@ -205,17 +224,17 @@ | |||
820 | 205 | else: | 224 | else: |
821 | 206 | if not auth_responses(event, 'accounts'): | 225 | if not auth_responses(event, 'accounts'): |
822 | 207 | return | 226 | return |
824 | 208 | account = session.query(Account).filter_by(username=username).first() | 227 | account = event.session.query(Account).filter_by(username=username).first() |
825 | 209 | if not account: | 228 | if not account: |
826 | 210 | event.addresponse(u"I don't know who %s is", username) | 229 | event.addresponse(u"I don't know who %s is", username) |
827 | 211 | return | 230 | return |
828 | 212 | 231 | ||
829 | 213 | account.attributes.append(Attribute(name, value)) | 232 | account.attributes.append(Attribute(name, value)) |
833 | 214 | session.save_or_update(account) | 233 | event.session.save_or_update(account) |
831 | 215 | session.flush() | ||
832 | 216 | session.close() | ||
834 | 217 | event.addresponse(True) | 234 | event.addresponse(True) |
836 | 218 | log.info(u"Added attribute '%s' = '%s' to account %s (%s) by %s/%s (%s)", name, value, account.id, account.username, event.account, event.identity, event.sender['connection']) | 235 | log.info(u"Added attribute '%s' = '%s' to account %s (%s) by %s/%s (%s)", |
837 | 236 | name, value, account.id, account.username, event.account, | ||
838 | 237 | event.identity, event.sender['connection']) | ||
839 | 219 | 238 | ||
840 | 220 | class Describe(Processor): | 239 | class Describe(Processor): |
841 | 221 | u"""who (am I|is <username>)""" | 240 | u"""who (am I|is <username>)""" |
842 | @@ -223,19 +242,18 @@ | |||
843 | 223 | 242 | ||
844 | 224 | @match(r'^who\s+(?:is|am)\s+(I|.+?)$') | 243 | @match(r'^who\s+(?:is|am)\s+(I|.+?)$') |
845 | 225 | def describe(self, event, username): | 244 | def describe(self, event, username): |
846 | 226 | session = ibid.databases.ibid() | ||
847 | 227 | if username.upper() == 'I': | 245 | if username.upper() == 'I': |
848 | 228 | if not event.account: | 246 | if not event.account: |
850 | 229 | identity = session.query(Identity).get(event.identity) | 247 | identity = event.session.query(Identity).get(event.identity) |
851 | 230 | event.addresponse(u"%(name)s on %(source)s", { | 248 | event.addresponse(u"%(name)s on %(source)s", { |
852 | 231 | 'name': identity.identity, | 249 | 'name': identity.identity, |
853 | 232 | 'source': identity.source, | 250 | 'source': identity.source, |
854 | 233 | }) | 251 | }) |
855 | 234 | return | 252 | return |
857 | 235 | account = session.query(Account).get(event.account) | 253 | account = event.session.query(Account).get(event.account) |
858 | 236 | 254 | ||
859 | 237 | else: | 255 | else: |
861 | 238 | account = session.query(Account).filter_by(username=username).first() | 256 | account = event.session.query(Account).filter_by(username=username).first() |
862 | 239 | if not account: | 257 | if not account: |
863 | 240 | event.addresponse(u"I don't know who %s is", username) | 258 | event.addresponse(u"I don't know who %s is", username) |
864 | 241 | return | 259 | return |
865 | @@ -244,7 +262,6 @@ | |||
866 | 244 | 'accountname': account.username, | 262 | 'accountname': account.username, |
867 | 245 | 'identities': u', '.join(u'%s on %s' % (identity.identity, identity.source) for identity in account.identities), | 263 | 'identities': u', '.join(u'%s on %s' % (identity.identity, identity.source) for identity in account.identities), |
868 | 246 | }) | 264 | }) |
869 | 247 | session.close() | ||
870 | 248 | 265 | ||
871 | 249 | class Identify(Processor): | 266 | class Identify(Processor): |
872 | 250 | 267 | ||
873 | @@ -256,25 +273,26 @@ | |||
874 | 256 | (event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])] | 273 | (event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])] |
875 | 257 | return | 274 | return |
876 | 258 | 275 | ||
882 | 259 | session = ibid.databases.ibid() | 276 | identity = event.session.query(Identity) \ |
883 | 260 | identity = session.query(Identity)\ | 277 | .options(eagerload('account')) \ |
884 | 261 | .options(eagerload('account'))\ | 278 | .filter(func.lower(Identity.source) == event.source.lower()) \ |
885 | 262 | .filter(func.lower(Identity.source) == event.source.lower())\ | 279 | .filter(func.lower(Identity.identity) == event.sender['id'].lower()) \ |
881 | 263 | .filter(func.lower(Identity.identity) == event.sender['id'].lower())\ | ||
886 | 264 | .first() | 280 | .first() |
887 | 265 | if not identity: | 281 | if not identity: |
888 | 266 | identity = Identity(event.source, event.sender['id']) | 282 | identity = Identity(event.source, event.sender['id']) |
890 | 267 | session.save_or_update(identity) | 283 | event.session.save_or_update(identity) |
891 | 268 | try: | 284 | try: |
893 | 269 | session.flush() | 285 | event.session.commit() |
894 | 270 | log.info(u'Created identity %s for %s on %s', identity.id, identity.identity, identity.source) | 286 | log.info(u'Created identity %s for %s on %s', identity.id, identity.identity, identity.source) |
895 | 271 | except IntegrityError: | 287 | except IntegrityError: |
897 | 272 | session.expunge(identity) | 288 | event.session.rollback() |
898 | 289 | event.session.close() | ||
899 | 290 | del event['session'] | ||
900 | 273 | log.debug(u'Race encountered creating identity for %s on %s', event.sender['id'], event.source) | 291 | log.debug(u'Race encountered creating identity for %s on %s', event.sender['id'], event.source) |
905 | 274 | identity = session.query(Identity)\ | 292 | identity = event.session.query(Identity) \ |
906 | 275 | .options(eagerload('account'))\ | 293 | .options(eagerload('account')) \ |
907 | 276 | .filter(func.lower(Identity.source) == event.source.lower())\ | 294 | .filter(func.lower(Identity.source) == event.source.lower()) \ |
908 | 277 | .filter(func.lower(Identity.identity) == event.sender['id'].lower())\ | 295 | .filter(func.lower(Identity.identity) == event.sender['id'].lower()) \ |
909 | 278 | .one() | 296 | .one() |
910 | 279 | 297 | ||
911 | 280 | event.identity = identity.id | 298 | event.identity = identity.id |
912 | @@ -284,29 +302,9 @@ | |||
913 | 284 | event.account = None | 302 | event.account = None |
914 | 285 | identify_cache[(event.source, event.sender['connection'])] = (event.identity, event.account) | 303 | identify_cache[(event.source, event.sender['connection'])] = (event.identity, event.account) |
915 | 286 | 304 | ||
937 | 287 | session.close() | 305 | def get_identities(event): |
917 | 288 | |||
918 | 289 | def identify(source, user): | ||
919 | 290 | |||
920 | 291 | session = ibid.databases.ibid() | ||
921 | 292 | |||
922 | 293 | identity = session.query(Identity).filter(func.lower(Identity.source)==source.lower()).filter(func.lower(Identity.identity)==user.lower()).first() | ||
923 | 294 | account = session.query(Account).filter_by(username=user).first() | ||
924 | 295 | |||
925 | 296 | if not account and not identity: | ||
926 | 297 | return None | ||
927 | 298 | if not account: | ||
928 | 299 | return identity | ||
929 | 300 | if not identity or identity in account.identities: | ||
930 | 301 | return account | ||
931 | 302 | return (account, identity) | ||
932 | 303 | |||
933 | 304 | def get_identities(event, session=None): | ||
934 | 305 | if not session: | ||
935 | 306 | session = ibid.databases.ibid() | ||
936 | 307 | |||
938 | 308 | if event.account: | 306 | if event.account: |
940 | 309 | account = session.query(Account).get(event.account) | 307 | account = event.session.query(Account).get(event.account) |
941 | 310 | return [identity.id for identity in account.identities] | 308 | return [identity.id for identity in account.identities] |
942 | 311 | else: | 309 | else: |
943 | 312 | return (event.identity,) | 310 | return (event.identity,) |
944 | 313 | 311 | ||
945 | === modified file 'ibid/plugins/karma.py' | |||
946 | --- ibid/plugins/karma.py 2009-05-01 12:17:57 +0000 | |||
947 | +++ ibid/plugins/karma.py 2009-05-03 16:33:10 +0000 | |||
948 | @@ -4,7 +4,6 @@ | |||
949 | 4 | from sqlalchemy import Column, Integer, Unicode, DateTime, Table | 4 | from sqlalchemy import Column, Integer, Unicode, DateTime, Table |
950 | 5 | from sqlalchemy.sql import func | 5 | from sqlalchemy.sql import func |
951 | 6 | 6 | ||
952 | 7 | import ibid | ||
953 | 8 | from ibid.plugins import Processor, match, handler, authorise | 7 | from ibid.plugins import Processor, match, handler, authorise |
954 | 9 | from ibid.config import Option, BoolOption, IntOption | 8 | from ibid.config import Option, BoolOption, IntOption |
955 | 10 | from ibid.models import Base, VersionedSchema | 9 | from ibid.models import Base, VersionedSchema |
956 | @@ -59,8 +58,8 @@ | |||
957 | 59 | if subject.lower() in self.ignore: | 58 | if subject.lower() in self.ignore: |
958 | 60 | return | 59 | return |
959 | 61 | 60 | ||
962 | 62 | session = ibid.databases.ibid() | 61 | karma = event.session.query(Karma) \ |
963 | 63 | karma = session.query(Karma).filter(func.lower(Karma.subject)==subject.lower()).first() | 62 | .filter(func.lower(Karma.subject) == subject.lower()).first() |
964 | 64 | if not karma: | 63 | if not karma: |
965 | 65 | karma = Karma(subject) | 64 | karma = Karma(subject) |
966 | 66 | 65 | ||
967 | @@ -82,14 +81,12 @@ | |||
968 | 82 | if karma.value == 0 and karma.changes <= self.importance: | 81 | if karma.value == 0 and karma.changes <= self.importance: |
969 | 83 | change = u'Forgotten (unimportant)' | 82 | change = u'Forgotten (unimportant)' |
970 | 84 | 83 | ||
972 | 85 | session.delete(karma) | 84 | event.session.delete(karma) |
973 | 86 | else: | 85 | else: |
980 | 87 | session.save_or_update(karma) | 86 | event.session.save_or_update(karma) |
981 | 88 | 87 | ||
982 | 89 | session.flush() | 88 | log.info(u"%s karma for '%s' by %s/%s (%s) because: %s", |
983 | 90 | session.close() | 89 | change, subject, event.account, event.identity, event.sender['connection'], reason) |
978 | 91 | |||
979 | 92 | log.info(u"%s karma for '%s' by %s/%s (%s) because: %s", change, subject, event.account, event.identity, event.sender['connection'], reason) | ||
984 | 93 | 90 | ||
985 | 94 | if self.reply: | 91 | if self.reply: |
986 | 95 | event.addresponse(True) | 92 | event.addresponse(True) |
987 | @@ -103,8 +100,8 @@ | |||
988 | 103 | 100 | ||
989 | 104 | @match(r'^karma\s+(?:for\s+)?(.+)$') | 101 | @match(r'^karma\s+(?:for\s+)?(.+)$') |
990 | 105 | def handle_karma(self, event, subject): | 102 | def handle_karma(self, event, subject): |
993 | 106 | session = ibid.databases.ibid() | 103 | karma = event.session.query(Karma) \ |
994 | 107 | karma = session.query(Karma).filter(func.lower(Karma.subject)==subject.lower()).first() | 104 | .filter(func.lower(Karma.subject) == subject.lower()).first() |
995 | 108 | if not karma: | 105 | if not karma: |
996 | 109 | event.addresponse(u'nobody cares, dude') | 106 | event.addresponse(u'nobody cares, dude') |
997 | 110 | elif karma.value == 0: | 107 | elif karma.value == 0: |
998 | @@ -114,17 +111,16 @@ | |||
999 | 114 | 'subject': subject, | 111 | 'subject': subject, |
1000 | 115 | 'value': karma.value, | 112 | 'value': karma.value, |
1001 | 116 | }) | 113 | }) |
1002 | 117 | session.close() | ||
1003 | 118 | 114 | ||
1004 | 119 | @match(r'^(reverse\s+)?karmaladder$') | 115 | @match(r'^(reverse\s+)?karmaladder$') |
1005 | 120 | def ladder(self, event, reverse): | 116 | def ladder(self, event, reverse): |
1008 | 121 | session = ibid.databases.ibid() | 117 | karmas = event.session.query(Karma) \ |
1009 | 122 | karmas = session.query(Karma).order_by(reverse and Karma.value.asc() or Karma.value.desc()).limit(30).all() | 118 | .order_by(reverse and Karma.value.asc() or Karma.value.desc()) \ |
1010 | 119 | .limit(30).all() | ||
1011 | 123 | if karmas: | 120 | if karmas: |
1012 | 124 | event.addresponse(', '.join(['%s: %s (%s)' % (karmas.index(karma), karma.subject, karma.value) for karma in karmas])) | 121 | event.addresponse(', '.join(['%s: %s (%s)' % (karmas.index(karma), karma.subject, karma.value) for karma in karmas])) |
1013 | 125 | else: | 122 | else: |
1014 | 126 | event.addresponse(u"I don't really care about anything") | 123 | event.addresponse(u"I don't really care about anything") |
1015 | 127 | session.close() | ||
1016 | 128 | 124 | ||
1017 | 129 | class Forget(Processor): | 125 | class Forget(Processor): |
1018 | 130 | u"""forget karma for <subject> [[reason]]""" | 126 | u"""forget karma for <subject> [[reason]]""" |
1019 | @@ -138,17 +134,16 @@ | |||
1020 | 138 | @match(r'^forget\s+karma\s+for\s+(.+?)(?:\s*[[{(]+\s*(.+?)\s*[\]})]+)?$') | 134 | @match(r'^forget\s+karma\s+for\s+(.+?)(?:\s*[[{(]+\s*(.+?)\s*[\]})]+)?$') |
1021 | 139 | @authorise | 135 | @authorise |
1022 | 140 | def forget(self, event, subject, reason): | 136 | def forget(self, event, subject, reason): |
1025 | 141 | session = ibid.databases.ibid() | 137 | karma = event.session.query(Karma) \ |
1026 | 142 | karma = session.query(Karma).filter(func.lower(Karma.subject)==subject.lower()).first() | 138 | .filter(func.lower(Karma.subject) == subject.lower()).first() |
1027 | 143 | if not karma: | 139 | if not karma: |
1028 | 144 | karma = Karma(subject) | 140 | karma = Karma(subject) |
1029 | 145 | event.addresponse(u"I was pretty ambivalent about %s, anyway", subject) | 141 | event.addresponse(u"I was pretty ambivalent about %s, anyway", subject) |
1030 | 146 | 142 | ||
1034 | 147 | session.delete(karma) | 143 | event.session.delete(karma) |
1032 | 148 | session.flush() | ||
1033 | 149 | session.close() | ||
1035 | 150 | 144 | ||
1037 | 151 | log.info(u"Forgot karma for '%s' by %s/%s (%s) because: %s", subject, event.account, event.identity, event.sender['connection'], reason) | 145 | log.info(u"Forgot karma for '%s' by %s/%s (%s) because: %s", |
1038 | 146 | subject, event.account, event.identity, event.sender['connection'], reason) | ||
1039 | 152 | event.addresponse(True) | 147 | event.addresponse(True) |
1040 | 153 | 148 | ||
1041 | 154 | # vi: set et sta sw=4 ts=4: | 149 | # vi: set et sta sw=4 ts=4: |
1042 | 155 | 150 | ||
1043 | === modified file 'ibid/plugins/memo.py' | |||
1044 | --- ibid/plugins/memo.py 2009-05-01 12:27:24 +0000 | |||
1045 | +++ ibid/plugins/memo.py 2009-05-03 16:33:10 +0000 | |||
1046 | @@ -5,10 +5,9 @@ | |||
1047 | 5 | from sqlalchemy.orm import relation | 5 | from sqlalchemy.orm import relation |
1048 | 6 | from sqlalchemy.sql import func | 6 | from sqlalchemy.sql import func |
1049 | 7 | 7 | ||
1050 | 8 | import ibid | ||
1051 | 9 | from ibid.plugins import Processor, handler, match, authorise | 8 | from ibid.plugins import Processor, handler, match, authorise |
1052 | 10 | from ibid.config import Option | 9 | from ibid.config import Option |
1054 | 11 | from ibid.plugins.auth import permission | 10 | from ibid.auth import permission |
1055 | 12 | from ibid.plugins.identity import get_identities | 11 | from ibid.plugins.identity import get_identities |
1056 | 13 | from ibid.models import Base, VersionedSchema, Identity, Account | 12 | from ibid.models import Base, VersionedSchema, Identity, Account |
1057 | 14 | from ibid.utils import ago | 13 | from ibid.utils import ago |
1058 | @@ -51,10 +50,12 @@ | |||
1059 | 51 | @match(r'^(?:please\s+)?(tell|pm|privmsg|msg)\s+(\S+)\s+(?:(?:that|to)\s+)?(.+)$') | 50 | @match(r'^(?:please\s+)?(tell|pm|privmsg|msg)\s+(\S+)\s+(?:(?:that|to)\s+)?(.+)$') |
1060 | 52 | @authorise | 51 | @authorise |
1061 | 53 | def tell(self, event, how, who, memo): | 52 | def tell(self, event, how, who, memo): |
1064 | 54 | session = ibid.databases.ibid() | 53 | to = event.session.query(Identity) \ |
1065 | 55 | to = session.query(Identity).filter(func.lower(Identity.identity)==who.lower()).filter_by(source=event.source).first() | 54 | .filter(func.lower(Identity.identity) == who.lower()) \ |
1066 | 55 | .filter_by(source=event.source).first() | ||
1067 | 56 | if not to: | 56 | if not to: |
1069 | 57 | account = session.query(Account).filter(func.lower(Account.username)==who.lower()).first() | 57 | account = event.session.query(Account) \ |
1070 | 58 | .filter(func.lower(Account.username) == who.lower()).first() | ||
1071 | 58 | if account: | 59 | if account: |
1072 | 59 | for identity in account.identities: | 60 | for identity in account.identities: |
1073 | 60 | if identity.source == event.source: | 61 | if identity.source == event.source: |
1074 | @@ -63,27 +64,32 @@ | |||
1075 | 63 | identity = account.identities[0] | 64 | identity = account.identities[0] |
1076 | 64 | if not to: | 65 | if not to: |
1077 | 65 | to = Identity(event.source, who) | 66 | to = Identity(event.source, who) |
1080 | 66 | session.save(to) | 67 | event.session.save(to) |
1081 | 67 | session.flush() | 68 | |
1082 | 69 | event.session.flush() | ||
1083 | 68 | log.info(u"Created identity %s for %s on %s", to.id, to.identity, to.source) | 70 | log.info(u"Created identity %s for %s on %s", to.id, to.identity, to.source) |
1084 | 69 | 71 | ||
1086 | 70 | if permission(u'recvmemo', to.account and to.account.id or None, to.source) != 'yes': | 72 | if permission(u'recvmemo', to.account and to.account.id or None, to.source, event.session) != 'yes': |
1087 | 71 | event.addresponse(u'Just tell %s yourself', who) | 73 | event.addresponse(u'Just tell %s yourself', who) |
1088 | 72 | return | 74 | return |
1089 | 73 | 75 | ||
1090 | 74 | memo = Memo(event.identity, to.id, memo, how.lower() in ('pm', 'privmsg', 'msg')) | 76 | memo = Memo(event.identity, to.id, memo, how.lower() in ('pm', 'privmsg', 'msg')) |
1094 | 75 | session.save_or_update(memo) | 77 | event.session.save_or_update(memo) |
1095 | 76 | session.flush() | 78 | |
1096 | 77 | log.info(u"Stored memo %s for %s (%s) from %s (%s): %s", memo.id, to.id, who, event.identity, event.sender['connection'], memo.memo) | 79 | event.session.flush() |
1097 | 80 | log.info(u"Stored memo %s for %s (%s) from %s (%s): %s", | ||
1098 | 81 | memo.id, to.id, who, event.identity, event.sender['connection'], memo.memo) | ||
1099 | 78 | event.memo = memo.id | 82 | event.memo = memo.id |
1100 | 79 | session.close() | ||
1101 | 80 | memo_cache.clear() | 83 | memo_cache.clear() |
1102 | 81 | 84 | ||
1103 | 82 | event.addresponse(True) | 85 | event.addresponse(True) |
1104 | 83 | 86 | ||
1108 | 84 | def get_memos(session, event, delivered=False): | 87 | def get_memos(event, delivered=False): |
1109 | 85 | identities = get_identities(event, session) | 88 | identities = get_identities(event) |
1110 | 86 | return session.query(Memo).filter_by(delivered=delivered).filter(Memo.to_id.in_(identities)).order_by(Memo.time.asc()).all() | 89 | return event.session.query(Memo) \ |
1111 | 90 | .filter_by(delivered=delivered) \ | ||
1112 | 91 | .filter(Memo.to_id.in_(identities)) \ | ||
1113 | 92 | .order_by(Memo.time.asc()).all() | ||
1114 | 87 | 93 | ||
1115 | 88 | class Deliver(Processor): | 94 | class Deliver(Processor): |
1116 | 89 | feature = 'memo' | 95 | feature = 'memo' |
1117 | @@ -96,8 +102,7 @@ | |||
1118 | 96 | if event.identity in memo_cache: | 102 | if event.identity in memo_cache: |
1119 | 97 | return | 103 | return |
1120 | 98 | 104 | ||
1123 | 99 | session = ibid.databases.ibid() | 105 | memos = get_memos(event) |
1122 | 100 | memos = get_memos(session, event) | ||
1124 | 101 | 106 | ||
1125 | 102 | for memo in memos: | 107 | for memo in memos: |
1126 | 103 | # Don't deliver if the user just sent a memo to themself | 108 | # Don't deliver if the user just sent a memo to themself |
1127 | @@ -122,11 +127,9 @@ | |||
1128 | 122 | }) | 127 | }) |
1129 | 123 | 128 | ||
1130 | 124 | memo.delivered = True | 129 | memo.delivered = True |
1136 | 125 | session.save_or_update(memo) | 130 | event.session.save_or_update(memo) |
1137 | 126 | log.info(u"Delivered memo %s to %s (%s)", memo.id, event.identity, event.sender['connection']) | 131 | log.info(u"Delivered memo %s to %s (%s)", |
1138 | 127 | 132 | memo.id, event.identity, event.sender['connection']) | |
1134 | 128 | session.flush() | ||
1135 | 129 | session.close() | ||
1139 | 130 | 133 | ||
1140 | 131 | if 'memo' not in event: | 134 | if 'memo' not in event: |
1141 | 132 | memo_cache[event.identity] = None | 135 | memo_cache[event.identity] = None |
1142 | @@ -146,16 +149,13 @@ | |||
1143 | 146 | if event.identity in memo_cache: | 149 | if event.identity in memo_cache: |
1144 | 147 | return | 150 | return |
1145 | 148 | 151 | ||
1148 | 149 | session = ibid.databases.ibid() | 152 | memos = get_memos(event) |
1147 | 150 | memos = get_memos(session, event) | ||
1149 | 151 | 153 | ||
1150 | 152 | if len(memos) > 0: | 154 | if len(memos) > 0: |
1151 | 153 | event.addresponse({'reply': u'You have %s messages' % len(memos), 'target': event.sender['id']}) | 155 | event.addresponse({'reply': u'You have %s messages' % len(memos), 'target': event.sender['id']}) |
1152 | 154 | else: | 156 | else: |
1153 | 155 | memo_cache[event.identity] = None | 157 | memo_cache[event.identity] = None |
1154 | 156 | 158 | ||
1155 | 157 | session.close() | ||
1156 | 158 | |||
1157 | 159 | class Messages(Processor): | 159 | class Messages(Processor): |
1158 | 160 | u"""my messages | 160 | u"""my messages |
1159 | 161 | message <number>""" | 161 | message <number>""" |
1160 | @@ -165,18 +165,15 @@ | |||
1161 | 165 | 165 | ||
1162 | 166 | @match(r'^my\s+messages$') | 166 | @match(r'^my\s+messages$') |
1163 | 167 | def messages(self, event): | 167 | def messages(self, event): |
1166 | 168 | session = ibid.databases.ibid() | 168 | memos = get_memos(event, True) |
1165 | 169 | memos = get_memos(session, event, True) | ||
1167 | 170 | if memos: | 169 | if memos: |
1168 | 171 | event.addresponse(u', '.join(['%s: %s (%s)' % (memos.index(memo), memo.sender.identity, memo.time.strftime(self.datetime_format)) for memo in memos])) | 170 | event.addresponse(u', '.join(['%s: %s (%s)' % (memos.index(memo), memo.sender.identity, memo.time.strftime(self.datetime_format)) for memo in memos])) |
1169 | 172 | else: | 171 | else: |
1170 | 173 | event.addresponse(u"Sorry, nobody loves you") | 172 | event.addresponse(u"Sorry, nobody loves you") |
1171 | 174 | session.close() | ||
1172 | 175 | 173 | ||
1173 | 176 | @match(r'^message\s+(\d+)$') | 174 | @match(r'^message\s+(\d+)$') |
1174 | 177 | def message(self, event, number): | 175 | def message(self, event, number): |
1177 | 178 | session = ibid.databases.ibid() | 176 | memos = get_memos(event, True) |
1176 | 179 | memos = get_memos(session, event, True) | ||
1178 | 180 | memo = memos[int(number)] | 177 | memo = memos[int(number)] |
1179 | 181 | event.addresponse(u"From %(sender)s on %(source)s at %(time)s: %(message)s", { | 178 | event.addresponse(u"From %(sender)s on %(source)s at %(time)s: %(message)s", { |
1180 | 182 | 'sender': memo.sender.identity, | 179 | 'sender': memo.sender.identity, |
1181 | @@ -184,7 +181,5 @@ | |||
1182 | 184 | 'time': memo.time.strftime(self.datetime_format), | 181 | 'time': memo.time.strftime(self.datetime_format), |
1183 | 185 | 'message': memo.memo, | 182 | 'message': memo.memo, |
1184 | 186 | }) | 183 | }) |
1185 | 187 | session.close() | ||
1186 | 188 | |||
1187 | 189 | 184 | ||
1188 | 190 | # vi: set et sta sw=4 ts=4: | 185 | # vi: set et sta sw=4 ts=4: |
1189 | 191 | 186 | ||
1190 | === modified file 'ibid/plugins/seen.py' | |||
1191 | --- ibid/plugins/seen.py 2009-05-01 12:22:37 +0000 | |||
1192 | +++ ibid/plugins/seen.py 2009-05-03 16:37:03 +0000 | |||
1193 | @@ -6,7 +6,6 @@ | |||
1194 | 6 | from sqlalchemy.sql import func | 6 | from sqlalchemy.sql import func |
1195 | 7 | from sqlalchemy.exceptions import IntegrityError | 7 | from sqlalchemy.exceptions import IntegrityError |
1196 | 8 | 8 | ||
1197 | 9 | import ibid | ||
1198 | 10 | from ibid.plugins import Processor, match | 9 | from ibid.plugins import Processor, match |
1199 | 11 | from ibid.config import Option | 10 | from ibid.config import Option |
1200 | 12 | from ibid.models import Base, VersionedSchema, Identity, Account | 11 | from ibid.models import Base, VersionedSchema, Identity, Account |
1201 | @@ -51,8 +50,9 @@ | |||
1202 | 51 | if event.type != 'message' and event.type != 'state': | 50 | if event.type != 'message' and event.type != 'state': |
1203 | 52 | return | 51 | return |
1204 | 53 | 52 | ||
1207 | 54 | session = ibid.databases.ibid() | 53 | sighting = event.session.query(Sighting) \ |
1208 | 55 | sighting = session.query(Sighting).filter_by(identity_id=event.identity).filter_by(type=event.type).first() | 54 | .filter_by(identity_id=event.identity) \ |
1209 | 55 | .filter_by(type=event.type).first() | ||
1210 | 56 | if not sighting: | 56 | if not sighting: |
1211 | 57 | sighting = Sighting(event.identity, event.type) | 57 | sighting = Sighting(event.identity, event.type) |
1212 | 58 | 58 | ||
1213 | @@ -65,12 +65,15 @@ | |||
1214 | 65 | sighting.time = datetime.now() | 65 | sighting.time = datetime.now() |
1215 | 66 | sighting.count = sighting.count + 1 | 66 | sighting.count = sighting.count + 1 |
1216 | 67 | 67 | ||
1217 | 68 | event.session.save_or_update(sighting) | ||
1218 | 68 | try: | 69 | try: |
1221 | 69 | session.save_or_update(sighting) | 70 | event.session.commit() |
1220 | 70 | session.flush() | ||
1222 | 71 | except IntegrityError: | 71 | except IntegrityError: |
1225 | 72 | log.debug('Race on seen update for identity %s', event.identity) | 72 | event.session.rollback() |
1226 | 73 | session.close() | 73 | event.session.close() |
1227 | 74 | del event['session'] | ||
1228 | 75 | log.debug(u'Race encountered updating seen for %s on %s', | ||
1229 | 76 | event.sender['id'], event.source) | ||
1230 | 74 | 77 | ||
1231 | 75 | class Seen(Processor): | 78 | class Seen(Processor): |
1232 | 76 | u"""seen <who>""" | 79 | u"""seen <who>""" |
1233 | @@ -81,14 +84,15 @@ | |||
1234 | 81 | @match(r'^(?:have\s+you\s+)?seen\s+(\S+)(?:\s+on\s+(\S+))?$') | 84 | @match(r'^(?:have\s+you\s+)?seen\s+(\S+)(?:\s+on\s+(\S+))?$') |
1235 | 82 | def handler(self, event, who, source): | 85 | def handler(self, event, who, source): |
1236 | 83 | 86 | ||
1237 | 84 | session = ibid.databases.ibid() | ||
1238 | 85 | account = None | 87 | account = None |
1240 | 86 | identity = session.query(Identity).filter(func.lower(Identity.source)==(source and source or event.source).lower()).filter(func.lower(Identity.identity)==who.lower()).first() | 88 | identity = event.session.query(Identity) \ |
1241 | 89 | .filter(func.lower(Identity.source) == (source and source or event.source).lower()) \ | ||
1242 | 90 | .filter(func.lower(Identity.identity) == who.lower()).first() | ||
1243 | 87 | if identity and identity.account and not source: | 91 | if identity and identity.account and not source: |
1244 | 88 | account = identity.account | 92 | account = identity.account |
1245 | 89 | 93 | ||
1246 | 90 | if not identity and not source: | 94 | if not identity and not source: |
1248 | 91 | account = session.query(Account).filter_by(username=who).first() | 95 | account = event.session.query(Account).filter_by(username=who).first() |
1249 | 92 | 96 | ||
1250 | 93 | if not identity and not account: | 97 | if not identity and not account: |
1251 | 94 | event.addresponse(u"I don't know who %s is", who) | 98 | event.addresponse(u"I don't know who %s is", who) |
1252 | @@ -98,13 +102,15 @@ | |||
1253 | 98 | states = [] | 102 | states = [] |
1254 | 99 | if account: | 103 | if account: |
1255 | 100 | for identity in account.identities: | 104 | for identity in account.identities: |
1257 | 101 | for sighting in session.query(Sighting).filter_by(identity_id=identity.id).all(): | 105 | for sighting in event.session.query(Sighting) \ |
1258 | 106 | .filter_by(identity_id=identity.id).all(): | ||
1259 | 102 | if sighting.type == 'message': | 107 | if sighting.type == 'message': |
1260 | 103 | messages.append(sighting) | 108 | messages.append(sighting) |
1261 | 104 | else: | 109 | else: |
1262 | 105 | states.append(sighting) | 110 | states.append(sighting) |
1263 | 106 | else: | 111 | else: |
1265 | 107 | for sighting in session.query(Sighting).filter_by(identity_id=identity.id).all(): | 112 | for sighting in event.session.query(Sighting) \ |
1266 | 113 | .filter_by(identity_id=identity.id).all(): | ||
1267 | 108 | if sighting.type == 'message': | 114 | if sighting.type == 'message': |
1268 | 109 | messages.append(sighting) | 115 | messages.append(sighting) |
1269 | 110 | else: | 116 | else: |
1270 | @@ -133,6 +139,5 @@ | |||
1271 | 133 | reply += u" has been %s on %s since %s" % (sighting.value, sighting.identity.source, sighting.time.strftime(self.datetime_format)) | 139 | reply += u" has been %s on %s since %s" % (sighting.value, sighting.identity.source, sighting.time.strftime(self.datetime_format)) |
1272 | 134 | 140 | ||
1273 | 135 | event.addresponse(reply) | 141 | event.addresponse(reply) |
1274 | 136 | session.close() | ||
1275 | 137 | 142 | ||
1276 | 138 | # vi: set et sta sw=4 ts=4: | 143 | # vi: set et sta sw=4 ts=4: |
1277 | 139 | 144 | ||
1278 | === modified file 'ibid/plugins/url.py' | |||
1279 | --- ibid/plugins/url.py 2009-05-01 12:22:37 +0000 | |||
1280 | +++ ibid/plugins/url.py 2009-05-02 19:29:44 +0000 | |||
1281 | @@ -114,11 +114,8 @@ | |||
1282 | 114 | else: | 114 | else: |
1283 | 115 | url = 'http://%s' % url | 115 | url = 'http://%s' % url |
1284 | 116 | 116 | ||
1285 | 117 | session = ibid.databases.ibid() | ||
1286 | 118 | u = URL(url, event.channel, event.identity) | 117 | u = URL(url, event.channel, event.identity) |
1290 | 119 | session.save_or_update(u) | 118 | event.session.save_or_update(u) |
1288 | 120 | session.flush() | ||
1289 | 121 | session.close() | ||
1291 | 122 | 119 | ||
1292 | 123 | if self.username != None: | 120 | if self.username != None: |
1293 | 124 | self.delicious.add_post(self.username, self.password, event, url) | 121 | self.delicious.add_post(self.username, self.password, event, url) |
1294 | 125 | 122 | ||
1295 | === modified file 'ibid/source/irc.py' | |||
1296 | --- ibid/source/irc.py 2009-05-01 12:59:07 +0000 | |||
1297 | +++ ibid/source/irc.py 2009-05-02 19:29:44 +0000 | |||
1298 | @@ -237,8 +237,9 @@ | |||
1299 | 237 | return u'irc://%s@%s:%s' % (self.nick, self.server, self.port) | 237 | return u'irc://%s@%s:%s' % (self.nick, self.server, self.port) |
1300 | 238 | 238 | ||
1301 | 239 | def auth_hostmask(self, event, credential = None): | 239 | def auth_hostmask(self, event, credential = None): |
1304 | 240 | session = ibid.databases.ibid() | 240 | for credential in event.session.query(Credential) \ |
1305 | 241 | for credential in session.query(Credential).filter_by(method=u'hostmask').filter_by(account_id=event.account).filter(or_(Credential.source == event.source, Credential.source == None)).all(): | 241 | .filter_by(method=u'hostmask').filter_by(account_id=event.account) \ |
1306 | 242 | .filter(or_(Credential.source == event.source, Credential.source == None)).all(): | ||
1307 | 242 | if fnmatch(event.sender['connection'], credential.credential): | 243 | if fnmatch(event.sender['connection'], credential.credential): |
1308 | 243 | return True | 244 | return True |
1309 | 244 | 245 | ||
1310 | 245 | 246 | ||
1311 | === modified file 'scripts/ibid-factpack' | |||
1312 | --- scripts/ibid-factpack 2009-04-26 11:04:32 +0000 | |||
1313 | +++ scripts/ibid-factpack 2009-05-02 19:29:44 +0000 | |||
1314 | @@ -28,7 +28,6 @@ | |||
1315 | 28 | ibid.reload_reloader() | 28 | ibid.reload_reloader() |
1316 | 29 | ibid.reloader.reload_databases() | 29 | ibid.reloader.reload_databases() |
1317 | 30 | session = ibid.databases.ibid() | 30 | session = ibid.databases.ibid() |
1318 | 31 | session.begin() | ||
1319 | 32 | 31 | ||
1320 | 33 | if options.remove: | 32 | if options.remove: |
1321 | 34 | factpack = session.query(Factpack).filter_by(name=filename).first() | 33 | factpack = session.query(Factpack).filter_by(name=filename).first() |
1322 | @@ -76,7 +75,6 @@ | |||
1323 | 76 | 75 | ||
1324 | 77 | factpack = Factpack(name) | 76 | factpack = Factpack(name) |
1325 | 78 | session.save(factpack) | 77 | session.save(factpack) |
1326 | 79 | session.flush() | ||
1327 | 80 | 78 | ||
1328 | 81 | existing = [] | 79 | existing = [] |
1329 | 82 | for names, values in facts: | 80 | for names, values in facts: |
1330 | 83 | 81 | ||
1331 | === modified file 'scripts/ibid-plugin' | |||
1332 | --- scripts/ibid-plugin 2009-05-02 16:20:45 +0000 | |||
1333 | +++ scripts/ibid-plugin 2009-05-03 16:37:03 +0000 | |||
1334 | @@ -14,6 +14,7 @@ | |||
1335 | 14 | pass | 14 | pass |
1336 | 15 | 15 | ||
1337 | 16 | from twisted.python.modules import getModule | 16 | from twisted.python.modules import getModule |
1338 | 17 | from sqlalchemy.exceptions import IntegrityError | ||
1339 | 17 | 18 | ||
1340 | 18 | import ibid | 19 | import ibid |
1341 | 19 | import ibid.plugins | 20 | import ibid.plugins |
1342 | @@ -80,15 +81,19 @@ | |||
1343 | 80 | username = unicode(getenv('USER')) | 81 | username = unicode(getenv('USER')) |
1344 | 81 | if not username: | 82 | if not username: |
1345 | 82 | username = u'tester' | 83 | username = u'tester' |
1346 | 84 | |||
1347 | 83 | session = ibid.databases.ibid() | 85 | session = ibid.databases.ibid() |
1348 | 86 | |||
1349 | 84 | identity = session.query(Identity).filter_by(identity=username).first() | 87 | identity = session.query(Identity).filter_by(identity=username).first() |
1350 | 85 | if not identity: | 88 | if not identity: |
1351 | 86 | identity = Identity(u'test_source',username) | 89 | identity = Identity(u'test_source',username) |
1352 | 87 | session.save(identity) | 90 | session.save(identity) |
1354 | 88 | session.flush() | 91 | session.commit() |
1355 | 89 | identity = session.query(Identity).filter_by(identity=username).first() | 92 | identity = session.query(Identity).filter_by(identity=username).first() |
1356 | 90 | identity_id = identity.id | 93 | identity_id = identity.id |
1357 | 91 | 94 | ||
1358 | 95 | session.close() | ||
1359 | 96 | |||
1360 | 92 | try: | 97 | try: |
1361 | 93 | encoding = getenv("LANG").split(".")[1] | 98 | encoding = getenv("LANG").split(".")[1] |
1362 | 94 | except: | 99 | except: |
1363 | @@ -115,14 +120,28 @@ | |||
1364 | 115 | try: | 120 | try: |
1365 | 116 | processor.process(event) | 121 | processor.process(event) |
1366 | 117 | except Exception: | 122 | except Exception: |
1368 | 118 | log.exception(u"Exception occured in %s processor of %s plugin", processor.__class__.__name__, processor.name) | 123 | log.exception(u"Exception occured in %s processor of %s plugin", |
1369 | 124 | processor.__class__.__name__, processor.name) | ||
1370 | 119 | event.complain = 'exception' | 125 | event.complain = 'exception' |
1371 | 120 | 126 | ||
1372 | 127 | if 'session' in event: | ||
1373 | 128 | try: | ||
1374 | 129 | event.session.commit() | ||
1375 | 130 | except IntegrityError: | ||
1376 | 131 | self.log.exception(u"Exception occured committing session from the %s processor of %s plugin", | ||
1377 | 132 | processor.__class__.__name__, processor.name) | ||
1378 | 133 | event.complain = u'exception' | ||
1379 | 134 | event.session.rollback() | ||
1380 | 135 | event.session.close() | ||
1381 | 136 | del event['session'] | ||
1382 | 137 | |||
1383 | 121 | for response in event.responses: | 138 | for response in event.responses: |
1384 | 122 | if isinstance(response, dict): | 139 | if isinstance(response, dict): |
1385 | 123 | response = response['reply'] | 140 | response = response['reply'] |
1386 | 124 | print 'Response: %s' % response | 141 | print 'Response: %s' % response |
1387 | 125 | 142 | ||
1388 | 143 | event.session.close() | ||
1389 | 144 | |||
1390 | 126 | if readline: | 145 | if readline: |
1391 | 127 | readline.write_history_file('.history') | 146 | readline.write_history_file('.history') |
1392 | 128 | print "\nExiting" | 147 | print "\nExiting" |
1393 | 129 | 148 | ||
1394 | === modified file 'scripts/ibid-setup' | |||
1395 | --- scripts/ibid-setup 2009-05-01 10:39:58 +0000 | |||
1396 | +++ scripts/ibid-setup 2009-05-02 19:29:44 +0000 | |||
1397 | @@ -83,7 +83,6 @@ | |||
1398 | 83 | exit(1) | 83 | exit(1) |
1399 | 84 | 84 | ||
1400 | 85 | session = Session() | 85 | session = Session() |
1401 | 86 | session.begin() | ||
1402 | 87 | account = Account(identity) | 86 | account = Account(identity) |
1403 | 88 | identity = Identity(source, identity) | 87 | identity = Identity(source, identity) |
1404 | 89 | account.identities.append(identity) | 88 | account.identities.append(identity) |
1405 | 90 | 89 | ||
1406 | === modified file 'scripts/ibid_import' | |||
1407 | --- scripts/ibid_import 2009-02-22 07:43:34 +0000 | |||
1408 | +++ scripts/ibid_import 2009-05-02 19:29:44 +0000 | |||
1409 | @@ -63,7 +63,9 @@ | |||
1410 | 63 | return None | 63 | return None |
1411 | 64 | user = decode(user) | 64 | user = decode(user) |
1412 | 65 | 65 | ||
1414 | 66 | identity = session.query(Identity).filter(func.lower(Identity.identity)==user.lower()).filter(func.lower(Identity.source)==source.lower()).first() | 66 | identity = session.query(Identity) \ |
1415 | 67 | .filter(func.lower(Identity.identity) == user.lower()) \ | ||
1416 | 68 | .filter(func.lower(Identity.source) == source.lower()).first() | ||
1417 | 67 | if not identity: | 69 | if not identity: |
1418 | 68 | identity = Identity(source, user) | 70 | identity = Identity(source, user) |
1419 | 69 | identity.created = created | 71 | identity.created = created |
1420 | @@ -231,5 +233,6 @@ | |||
1421 | 231 | print 'Done' | 233 | print 'Done' |
1422 | 232 | 234 | ||
1423 | 233 | knab.close() | 235 | knab.close() |
1424 | 236 | ibid.close() | ||
1425 | 234 | 237 | ||
1426 | 235 | # vi: set et sta sw=4 ts=4: | 238 | # vi: set et sta sw=4 ts=4: |
Happy reviewing, folks :P
This is one mother of a branch.
We still have the option of attaching identities and accounts to the event. However, I'm not doing that now.