Merge lp:~stefanor/ibid/sqlalchemy-370772 into lp:~ibid-core/ibid/old-trunk-pack-0.92

Proposed by Stefano Rivera
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
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.

To post a comment you must log in.
Revision history for this message
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal

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.

Revision history for this message
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...

Revision history for this message
Stefano Rivera (stefanor) wrote : Posted in a previous version of this proposal

No reviews yet, so re-submitted to get new diff

Revision history for this message
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.

Revision history for this message
Michael Gorven (mgorven) wrote : Posted in a previous version of this proposal

+ self.log.exception(u"Exception occured in %s processor
of %s plugin", processor.__class__.__name__, processor.name)

Can you just change the log message so something like "Exception occured while
committing session after %s processor of %s plugin".

+ self[name] = scoped_session(sessionmaker(bind=engine))

Do all versions of SQLAlchemy default to using transactions and autoflush?

- session.flush()
         log.info(u"%s %s permission for account %s (%s) by account %s",
actions[action.lower()], name, account.id, account.username, event.account)

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

review: Needs Fixing
Revision history for this message
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)

Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve
lp:~stefanor/ibid/sqlalchemy-370772 updated
633. By Stefano Rivera

Merge from trunk

Revision history for this message
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.

lp:~stefanor/ibid/sqlalchemy-370772 updated
634. By Stefano Rivera

Commit in plugins, to ensure that Ibid only confirms successful actions (rollback in dispatcher)

Revision history for this message
Stefano Rivera (stefanor) wrote :

r634

Revision history for this message
Michael Gorven (mgorven) wrote :

Why is the session deleted if an exception occurs? Is rolling back not
sufficient?

Revision history for this message
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.

Revision history for this message
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

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ibid/auth.py'
--- ibid/auth.py 2009-03-16 21:00:23 +0000
+++ ibid/auth.py 2009-05-02 22:05:13 +0000
@@ -23,11 +23,11 @@
23 salt = ''.join([choice(chars) for i in xrange(8)])23 salt = ''.join([choice(chars) for i in xrange(8)])
24 return unicode(salt + sha1(salt + password).hexdigest())24 return unicode(salt + sha1(salt + password).hexdigest())
2525
26def permission(name, account, source):26def permission(name, account, source, session):
27 if account:27 if account:
28 session = ibid.databases.ibid()28 permission = session.query(Permission) \
29 permission = session.query(Permission).filter_by(account_id=account).filter_by(name=name).first()29 .filter_by(account_id=account) \
30 session.close()30 .filter_by(name=name).first()
3131
32 if permission:32 if permission:
33 return permission.value33 return permission.value
@@ -95,7 +95,7 @@
95 return False95 return False
9696
97 def authorise(self, event, name):97 def authorise(self, event, name):
98 value = permission(name, event.account, event.source)98 value = permission(name, event.account, event.source, event.session)
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)
100100
101 if value == 'yes':101 if value == 'yes':
@@ -105,15 +105,17 @@
105105
106 return False106 return False
107107
108 def implicit(self, event, credential = None):108 def implicit(self, event, credential=None):
109 return True109 return True
110110
111 def password(self, event, password):111 def password(self, event, password):
112 if password is None:112 if password is None:
113 return False113 return False
114114
115 session = ibid.databases.ibid()115 for credential in event.session.query(Credential) \
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') \
117 .filter_by(account_id=event.account) \
118 .filter(or_(Credential.source == event.source, Credential.source == None)).all():
117 if hash(password, credential.credential) == credential.credential:119 if hash(password, credential.credential) == credential.credential:
118 return True120 return True
119121
120122
=== modified file 'ibid/core.py'
--- ibid/core.py 2009-05-02 15:14:56 +0000
+++ ibid/core.py 2009-05-03 16:37:03 +0000
@@ -7,6 +7,7 @@
7from twisted.python.modules import getModule7from twisted.python.modules import getModule
8from sqlalchemy import create_engine8from sqlalchemy import create_engine
9from sqlalchemy.orm import sessionmaker, scoped_session9from sqlalchemy.orm import sessionmaker, scoped_session
10from sqlalchemy.exceptions import IntegrityError
1011
11import ibid12import ibid
12import auth13import auth
@@ -21,9 +22,25 @@
21 try:22 try:
22 processor.process(event)23 processor.process(event)
23 except Exception:24 except Exception:
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",
26 processor.__class__.__name__, processor.name)
25 event.complain = u'exception'27 event.complain = u'exception'
2628
29 if 'session' in event and (event.session.dirty or event.session.deleted):
30 try:
31 event.session.commit()
32 except IntegrityError:
33 self.log.exception(u"Exception occured committing session from the %s processor of %s plugin",
34 processor.__class__.__name__, processor.name)
35 event.complain = u'exception'
36 event.session.rollback()
37 event.session.close()
38 del event['session']
39
40 if 'session' in event:
41 event.session.close()
42 del event['session']
43
27 log_level = logging.DEBUG44 log_level = logging.DEBUG
28 if event.type == u'clock' and not event.processed:45 if event.type == u'clock' and not event.processed:
29 log_level -= 546 log_level -= 5
@@ -263,7 +280,7 @@
263280
264 engine.dialect.use_ansiquotes = True281 engine.dialect.use_ansiquotes = True
265282
266 self[name] = scoped_session(sessionmaker(bind=engine, transactional=False, autoflush=True))283 self[name] = scoped_session(sessionmaker(bind=engine))
267284
268 self.log.info(u"Loaded %s database", name)285 self.log.info(u"Loaded %s database", name)
269286
270287
=== modified file 'ibid/event.py'
--- ibid/event.py 2009-05-01 11:14:06 +0000
+++ ibid/event.py 2009-05-02 19:29:44 +0000
@@ -1,3 +1,4 @@
1import ibid
12
2class Event(dict):3class Event(dict):
34
@@ -9,6 +10,8 @@
9 self.processed = False10 self.processed = False
1011
11 def __getattr__(self, name):12 def __getattr__(self, name):
13 if name == 'session' and 'session' not in self:
14 self['session'] = ibid.databases.ibid()
12 return self[name]15 return self[name]
1316
14 def __setattr__(self, name, value):17 def __setattr__(self, name, value):
1518
=== modified file 'ibid/models.py'
--- ibid/models.py 2009-04-21 20:09:06 +0000
+++ ibid/models.py 2009-05-02 19:29:44 +0000
@@ -61,7 +61,6 @@
61 metadata.tables[dependancy].versioned_schema.upgrade_schema(sessionmaker)61 metadata.tables[dependancy].versioned_schema.upgrade_schema(sessionmaker)
6262
63 self.upgrade_session = session = sessionmaker()63 self.upgrade_session = session = sessionmaker()
64 trans = session.begin()
6564
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()
6766
@@ -79,8 +78,7 @@
79 for version in range(schema.version + 1, self.version + 1):78 for version in range(schema.version + 1, self.version + 1):
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)
8180
82 trans.commit()81 session.commit()
83 trans = session.begin()
8482
85 getattr(self, 'upgrade_%i_to_%i' % (version - 1, version))()83 getattr(self, 'upgrade_%i_to_%i' % (version - 1, version))()
8684
@@ -88,10 +86,10 @@
88 session.save_or_update(schema)86 session.save_or_update(schema)
89 del self.upgrade_reflected_model87 del self.upgrade_reflected_model
9088
91 trans.commit()89 session.commit()
9290
93 except:91 except:
94 trans.rollback()92 session.rollback()
95 raise93 raise
9694
97 session.close()95 session.close()
@@ -236,7 +234,7 @@
236 schema = Schema(unicode(self.table.name), self.version)234 schema = Schema(unicode(self.table.name), self.version)
237 session.save_or_update(schema)235 session.save_or_update(schema)
238236
239 session.flush()237 session.commit()
240 session.close()238 session.close()
241239
242 __table__.versioned_schema = SchemaSchema(__table__, 1)240 __table__.versioned_schema = SchemaSchema(__table__, 1)
243241
=== modified file 'ibid/plugins/auth.py'
--- ibid/plugins/auth.py 2009-05-01 12:22:37 +0000
+++ ibid/plugins/auth.py 2009-05-03 16:25:57 +0000
@@ -5,7 +5,7 @@
5import ibid5import ibid
6from ibid.plugins import Processor, match, auth_responses, authorise6from ibid.plugins import Processor, match, auth_responses, authorise
7from ibid.models import Credential, Permission, Account7from ibid.models import Credential, Permission, Account
8from ibid.auth import hash, permission8from ibid.auth import hash
99
10help = {}10help = {}
1111
@@ -21,7 +21,6 @@
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+(.+)$')
22 def handler(self, event, user, source, method, credential):22 def handler(self, event, user, source, method, credential):
2323
24 session = ibid.databases.ibid()
25 if user.lower() == 'me':24 if user.lower() == 'me':
26 if not event.account:25 if not event.account:
27 event.addresponse(u"I don't know who you are")26 event.addresponse(u"I don't know who you are")
@@ -29,15 +28,14 @@
29 if not ibid.auth.authenticate(event):28 if not ibid.auth.authenticate(event):
30 event.complain = 'notauthed'29 event.complain = 'notauthed'
31 return30 return
32 account = session.query(Account).filter_by(id=event.account).first()31 account = event.session.query(Account).filter_by(id=event.account).first()
3332
34 else:33 else:
35 if not auth_responses(event, 'admin'):34 if not auth_responses(event, 'admin'):
36 return35 return
37 account = session.query(Account).filter_by(username=user).first()36 account = event.session.query(Account).filter_by(username=user).first()
38 if not account:37 if not account:
39 event.addresponse(u"I don't know who %s is", user)38 event.addresponse(u"I don't know who %s is", user)
40 session.close()
41 return39 return
4240
43 if source:41 if source:
@@ -49,14 +47,15 @@
49 if method.lower() == 'password':47 if method.lower() == 'password':
50 password = hash(credential)48 password = hash(credential)
51 event.message['clean'] = event.message['clean'][:-len(credential)] + password49 event.message['clean'] = event.message['clean'][:-len(credential)] + password
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)] \
51 + password + event.message['raw'][event.message['raw'].rfind(credential)+len(credential):]
53 credential = password52 credential = password
5453
55 credential = Credential(method, credential, source, account.id)54 credential = Credential(method, credential, source, account.id)
56 session.save_or_update(credential)55 event.session.save_or_update(credential)
57 session.flush()56 event.session.flush()
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",
59 session.close()58 method, credential.credential, account.id, account.username, source, event.account)
6059
61 event.addresponse(True)60 event.addresponse(True)
6261
@@ -73,17 +72,17 @@
73 @authorise72 @authorise
74 def grant(self, event, action, name, username, auth):73 def grant(self, event, action, name, username, auth):
7574
76 session = ibid.databases.ibid()75 account = event.session.query(Account).filter_by(username=username).first()
77 account = session.query(Account).filter_by(username=username).first()
78 if not account:76 if not account:
79 event.addresponse(u"I don't know who %s is", username)77 event.addresponse(u"I don't know who %s is", username)
80 session.close()
81 return78 return
8279
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) \
81 .filter_by(account_id=account.id) \
82 .filter(func.lower(Permission.name) == name.lower()).first()
84 if action.lower() == 'remove':83 if action.lower() == 'remove':
85 if permission:84 if permission:
86 session.delete(permission)85 event.session.delete(permission)
87 else:86 else:
88 event.addresponse(u"%s doesn't have that permission anyway", username)87 event.addresponse(u"%s doesn't have that permission anyway", username)
89 return88 return
@@ -108,26 +107,25 @@
108 return107 return
109108
110 permission.value = value109 permission.value = value
111 session.save_or_update(permission)110 event.session.save_or_update(permission)
112111
113 session.flush()112 event.session.flush()
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",
115 session.close()114 actions[action.lower()], name, account.id, account.username, event.account)
116115
117 event.addresponse(True)116 event.addresponse(True)
118117
119 @match(r'^permissions(?:\s+for\s+(\S+))?$')118 @match(r'^permissions(?:\s+for\s+(\S+))?$')
120 def list(self, event, username):119 def list(self, event, username):
121 session = ibid.databases.ibid()
122 if not username:120 if not username:
123 if not event.account:121 if not event.account:
124 event.addresponse(u"I don't know who you are")122 event.addresponse(u"I don't know who you are")
125 return123 return
126 account = session.query(Account).filter_by(id=event.account).first()124 account = event.session.query(Account).filter_by(id=event.account).first()
127 else:125 else:
128 if not auth_responses(event, u'accounts'):126 if not auth_responses(event, u'accounts'):
129 return127 return
130 account = session.query(Account).filter_by(username=username).first()128 account = event.session.query(Account).filter_by(username=username).first()
131 if not account:129 if not account:
132 event.addresponse(u"I don't know who %s is", username)130 event.addresponse(u"I don't know who %s is", username)
133 return131 return
134132
=== modified file 'ibid/plugins/factoid.py'
--- ibid/plugins/factoid.py 2009-05-01 12:17:57 +0000
+++ ibid/plugins/factoid.py 2009-05-03 16:33:10 +0000
@@ -6,7 +6,6 @@
6from sqlalchemy.orm import relation, eagerload6from sqlalchemy.orm import relation, eagerload
7from sqlalchemy.sql import func7from sqlalchemy.sql import func
88
9import ibid
10from ibid.plugins import Processor, match, handler, authorise, auth_responses, RPC9from ibid.plugins import Processor, match, handler, authorise, auth_responses, RPC
11from ibid.config import Option, IntOption10from ibid.config import Option, IntOption
12from ibid.plugins.identity import get_identities11from ibid.plugins.identity import get_identities
@@ -160,9 +159,7 @@
160159
161 @match(r'^literal\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$')160 @match(r'^literal\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$')
162 def literal(self, event, name, number, pattern, is_regex):161 def literal(self, event, name, number, pattern, is_regex):
163 session = ibid.databases.ibid()162 factoids = get_factoid(event.session, name, number, pattern, is_regex, literal=True)
164 factoids = get_factoid(session, name, number, pattern, is_regex, literal=True)
165 session.close()
166 number = number and int(number) or 0163 number = number and int(number) or 0
167 if factoids:164 if factoids:
168 event.addresponse(u', '.join(u'%i: %s'165 event.addresponse(u', '.join(u'%i: %s'
@@ -179,12 +176,11 @@
179 @match(r'^forget\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$')176 @match(r'^forget\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$')
180 @authorise177 @authorise
181 def forget(self, event, name, number, pattern, is_regex):178 def forget(self, event, name, number, pattern, is_regex):
182 session = ibid.databases.ibid()179 factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True)
183 factoids = get_factoid(session, name, number, pattern, is_regex, all=True)
184 if factoids:180 if factoids:
185 factoidadmin = auth_responses(event, u'factoidadmin')181 factoidadmin = auth_responses(event, u'factoidadmin')
186 identities = get_identities(event, session)182 identities = get_identities(event)
187 factoid = session.query(Factoid).get(factoids[0][0].id)183 factoid = event.session.query(Factoid).get(factoids[0][0].id)
188184
189 if (number or pattern):185 if (number or pattern):
190 if len(factoids) > 1:186 if len(factoids) > 1:
@@ -194,30 +190,28 @@
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:
195 return191 return
196192
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:
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:
199 return195 return
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'])
201 session.delete(factoid)197 event.session.delete(factoid)
202 else:198 else:
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'])
204 session.delete(factoids[0][2])200 event.session.delete(factoids[0][2])
205201
206 else:202 else:
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:
208 return204 return
209205
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:
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:
212 return208 return
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'])
214 session.delete(factoid)210 event.session.delete(factoid)
215 else:211 else:
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'])
217 session.delete(factoids[0][1])213 event.session.delete(factoids[0][1])
218214
219 session.flush()
220 session.close()
221 event.addresponse(True)215 event.addresponse(True)
222 else:216 else:
223 event.addresponse(u"I didn't know about %s anyway", name)217 event.addresponse(u"I didn't know about %s anyway", name)
@@ -230,15 +224,12 @@
230 event.addresponse(u"That makes no sense, they *are* the same")224 event.addresponse(u"That makes no sense, they *are* the same")
231 return225 return
232226
233 session = ibid.databases.ibid()227 factoid = event.session.query(Factoid).join(Factoid.names)\
234 factoid = session.query(Factoid).join(Factoid.names)\
235 .filter(func.lower(FactoidName.name)==escape_name(source).lower()).first()228 .filter(func.lower(FactoidName.name)==escape_name(source).lower()).first()
236 if factoid:229 if factoid:
237 name = FactoidName(escape_name(unicode(target)), event.identity)230 name = FactoidName(escape_name(unicode(target)), event.identity)
238 factoid.names.append(name)231 factoid.names.append(name)
239 session.save_or_update(factoid)232 event.session.save_or_update(factoid)
240 session.flush()
241 session.close()
242 event.addresponse(True)233 event.addresponse(True)
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'])
244 else:235 else:
@@ -266,8 +257,7 @@
266 pattern = m.group(1)257 pattern = m.group(1)
267 is_regex = bool(m.group(2))258 is_regex = bool(m.group(2))
268259
269 session = ibid.databases.ibid()260 query = event.session.query(Factoid)\
270 query = session.query(Factoid)\
271 .join(Factoid.names).add_entity(FactoidName)\261 .join(Factoid.names).add_entity(FactoidName)\
272 .join(Factoid.values)262 .join(Factoid.values)
273263
@@ -320,9 +310,7 @@
320 event.addresponse(response)310 event.addresponse(response)
321311
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={}):
323 session = ibid.databases.ibid()313 factoid = get_factoid(event.session, name, number, pattern, is_regex)
324 factoid = get_factoid(session, name, number, pattern, is_regex)
325 session.close()
326314
327 if factoid:315 if factoid:
328 (factoid, fname, fvalue) = factoid316 (factoid, fname, fvalue) = factoid
@@ -377,17 +365,15 @@
377 def set_factoid(self, event, correction, name, verb1, verb2, addition, value):365 def set_factoid(self, event, correction, name, verb1, verb2, addition, value):
378 verb = verb1 and verb1 or verb2366 verb = verb1 and verb1 or verb2
379367
380 session = ibid.databases.ibid()368 factoid = event.session.query(Factoid).join(Factoid.names)\
381 factoid = session.query(Factoid).join(Factoid.names)\
382 .filter(func.lower(FactoidName.name)==escape_name(name).lower()).first()369 .filter(func.lower(FactoidName.name)==escape_name(name).lower()).first()
383 if factoid:370 if factoid:
384 if correction:371 if correction:
385 identities = get_identities(event, session)372 identities = get_identities(event)
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:
387 return374 return
388 for fvalue in factoid.values:375 for fvalue in factoid.values:
389 session.delete(fvalue)376 event.session.delete(fvalue)
390 session.flush()
391 elif not addition:377 elif not addition:
392 event.addresponse(u'I already know stuff about %s', name)378 event.addresponse(u'I already know stuff about %s', name)
393 return379 return
@@ -395,6 +381,7 @@
395 factoid = Factoid()381 factoid = Factoid()
396 fname = FactoidName(escape_name(unicode(name)), event.identity)382 fname = FactoidName(escape_name(unicode(name)), event.identity)
397 factoid.names.append(fname)383 factoid.names.append(fname)
384 event.session.flush()
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)
399386
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):
@@ -402,9 +389,7 @@
402 fvalue = FactoidValue(unicode(value), event.identity)389 fvalue = FactoidValue(unicode(value), event.identity)
403 factoid.values.append(fvalue)390 factoid.values.append(fvalue)
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'])
405 session.save_or_update(factoid)392 event.session.save_or_update(factoid)
406 session.flush()
407 session.close()
408 event.addresponse(True)393 event.addresponse(True)
409394
410class Modify(Processor):395class Modify(Processor):
@@ -419,8 +404,7 @@
419 @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*\+=\s?(.+)$')404 @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*\+=\s?(.+)$')
420 @authorise405 @authorise
421 def append(self, event, name, number, pattern, is_regex, suffix):406 def append(self, event, name, number, pattern, is_regex, suffix):
422 session = ibid.databases.ibid()407 factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True)
423 factoids = get_factoid(session, name, number, pattern, is_regex, all=True)
424 if len(factoids) == 0:408 if len(factoids) == 0:
425 if pattern:409 if pattern:
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", {
@@ -433,7 +417,7 @@
433 event.addresponse(u"Pattern matches multiple factoids, please be more specific")417 event.addresponse(u"Pattern matches multiple factoids, please be more specific")
434 else:418 else:
435 factoidadmin = auth_responses(event, u'factoidadmin')419 factoidadmin = auth_responses(event, u'factoidadmin')
436 identities = get_identities(event, session)420 identities = get_identities(event)
437 factoid = factoids[0]421 factoid = factoids[0]
438422
439 if factoid[2].identity_id not in identities and not factoidadmin:423 if factoid[2].identity_id not in identities and not factoidadmin:
@@ -443,16 +427,13 @@
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'])
444 factoid[2].value += suffix428 factoid[2].value += suffix
445429
446 session.save_or_update(factoid[2])430 event.session.save_or_update(factoid[2])
447 session.flush()
448 session.close()
449 event.addresponse(True)431 event.addresponse(True)
450432
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]*)$')
452 @authorise434 @authorise
453 def modify(self, event, name, number, pattern, is_regex, operation, separator):435 def modify(self, event, name, number, pattern, is_regex, operation, separator):
454 session = ibid.databases.ibid()436 factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True)
455 factoids = get_factoid(session, name, number, pattern, is_regex, all=True)
456 if len(factoids) == 0:437 if len(factoids) == 0:
457 if pattern:438 if pattern:
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", {
@@ -465,7 +446,7 @@
465 event.addresponse(u"Pattern matches multiple factoids, please be more specific")446 event.addresponse(u"Pattern matches multiple factoids, please be more specific")
466 else:447 else:
467 factoidadmin = auth_responses(event, u'factoidadmin')448 factoidadmin = auth_responses(event, u'factoidadmin')
468 identities = get_identities(event, session)449 identities = get_identities(event)
469 factoid = factoids[0]450 factoid = factoids[0]
470451
471 if factoid[2].identity_id not in identities and not factoidadmin:452 if factoid[2].identity_id not in identities and not factoidadmin:
@@ -537,9 +518,7 @@
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)",
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'])
539520
540 session.save_or_update(factoid[2])521 event.session.save_or_update(factoid[2])
541 session.flush()
542 session.close()
543 event.addresponse(True)522 event.addresponse(True)
544523
545# vi: set et sta sw=4 ts=4:524# vi: set et sta sw=4 ts=4:
546525
=== modified file 'ibid/plugins/feeds.py'
--- ibid/plugins/feeds.py 2009-05-01 12:17:57 +0000
+++ ibid/plugins/feeds.py 2009-05-03 16:33:10 +0000
@@ -10,7 +10,6 @@
10import feedparser10import feedparser
11from html2text import html2text_file11from html2text import html2text_file
1212
13import ibid
14from ibid.plugins import Processor, match, authorise13from ibid.plugins import Processor, match, authorise
15from ibid.models import Base, VersionedSchema14from ibid.models import Base, VersionedSchema
16from ibid.utils import cacheable_download, get_html_parse_tree15from ibid.utils import cacheable_download, get_html_parse_tree
@@ -56,8 +55,8 @@
56 @match(r'^add\s+feed\s+(.+?)\s+as\s+(.+?)$')55 @match(r'^add\s+feed\s+(.+?)\s+as\s+(.+?)$')
57 @authorise56 @authorise
58 def add(self, event, url, name):57 def add(self, event, url, name):
59 session = ibid.databases.ibid()58 feed = event.session.query(Feed) \
60 feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first()59 .filter(func.lower(Feed.name) == name.lower()).first()
6160
62 if feed:61 if feed:
63 event.addresponse(u"I already have the %s feed", name)62 event.addresponse(u"I already have the %s feed", name)
@@ -85,15 +84,13 @@
85 return84 return
8685
87 feed = Feed(unicode(name), unicode(url), event.identity)86 feed = Feed(unicode(name), unicode(url), event.identity)
88 session.save(feed)87 event.session.save(feed)
89 session.flush()
90 event.addresponse(True)88 event.addresponse(True)
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))
9290
93 @match(r'^(?:list\s+)?feeds$')91 @match(r'^(?:list\s+)?feeds$')
94 def list(self, event):92 def list(self, event):
95 session = ibid.databases.ibid()93 feeds = event.session.query(Feed).all()
96 feeds = session.query(Feed).all()
97 if feeds:94 if feeds:
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])))
99 else:96 else:
@@ -102,19 +99,16 @@
102 @match(r'^remove\s+(.+?)\s+feed$')99 @match(r'^remove\s+(.+?)\s+feed$')
103 @authorise100 @authorise
104 def remove(self, event, name):101 def remove(self, event, name):
105 session = ibid.databases.ibid()102 feed = event.session.query(Feed) \
106 feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first()103 .filter(func.lower(Feed.name) == name.lower()).first()
107104
108 if not feed:105 if not feed:
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)
110 else:107 else:
111 session.delete(feed)108 event.session.delete(feed)
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)
113 session.flush()
114 event.addresponse(True)110 event.addresponse(True)
115111
116 session.close()
117
118class Retrieve(Processor):112class Retrieve(Processor):
119 u"""latest [ <count> ] articles from <name> [ starting at <number> ]113 u"""latest [ <count> ] articles from <name> [ starting at <number> ]
120 article ( <number> | /<pattern>/ ) from <name>"""114 article ( <number> | /<pattern>/ ) from <name>"""
@@ -125,9 +119,8 @@
125 number = number and int(number) or 10119 number = number and int(number) or 10
126 start = start and int(start) or 0120 start = start and int(start) or 0
127121
128 session = ibid.databases.ibid()122 feed = event.session.query(Feed) \
129 feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first()123 .filter(func.lower(Feed.name) == name.lower()).first()
130 session.close()
131124
132 if not feed:125 if not feed:
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)
@@ -144,9 +137,8 @@
144137
145 @match(r'^article\s+(?:(\d+)|/(.+?)/)\s+from\s+(.+?)$')138 @match(r'^article\s+(?:(\d+)|/(.+?)/)\s+from\s+(.+?)$')
146 def article(self, event, number, pattern, name):139 def article(self, event, number, pattern, name):
147 session = ibid.databases.ibid()140 feed = event.session.query(Feed) \
148 feed = session.query(Feed).filter(func.lower(Feed.name)==name.lower()).first()141 .filter(func.lower(Feed.name) == name.lower()).first()
149 session.close()
150142
151 if not feed:143 if not feed:
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)
153145
=== modified file 'ibid/plugins/identity.py'
--- ibid/plugins/identity.py 2009-04-27 00:50:19 +0000
+++ ibid/plugins/identity.py 2009-05-03 16:37:03 +0000
@@ -15,43 +15,45 @@
1515
16log = logging.getLogger('plugins.identity')16log = logging.getLogger('plugins.identity')
1717
18help['accounts'] = u'An account represents a person. An account has one or more identities, which is a user on a specific source.'18help['accounts'] = u'An account represents a person. ' \
19 'An account has one or more identities, which is a user on a specific source.'
19class Accounts(Processor):20class Accounts(Processor):
20 u"""create account <name>"""21 u"""create account <name>"""
21 feature = 'accounts'22 feature = 'accounts'
2223
23 @match(r'^create\s+account\s+(.+)$')24 @match(r'^create\s+account\s+(.+)$')
24 def account(self, event, username):25 def account(self, event, username):
25 session = ibid.databases.ibid()
26 admin = False26 admin = False
2727
28 if event.account:28 if event.account:
29 if ibid.auth.authenticate(event) and ibid.auth.authorise(event, 'accounts'):29 if ibid.auth.authenticate(event) and ibid.auth.authorise(event, 'accounts'):
30 admin = True30 admin = True
31 else:31 else:
32 account = session.query(Account).filter_by(id=event.account).first()32 account = event.session.query(Account).filter_by(id=event.account).first()
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)
34 return34 return
3535
36 account = session.query(Account).filter_by(username=username).first()36 account = event.session.query(Account).filter_by(username=username).first()
37 if account:37 if account:
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". ' \
39 'Please choose a different name', account.username)
39 return40 return
4041
41 account = Account(username)42 account = Account(username)
42 session.save_or_update(account)43 event.session.save_or_update(account)
43 session.flush()44 event.session.flush()
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)",
46 account.id, account.username, event.account, event.identity, event.sender['connection'])
4547
46 if not admin:48 if not admin:
47 identity = session.query(Identity).filter_by(id=event.identity).first()49 identity = event.session.query(Identity).filter_by(id=event.identity).first()
48 identity.account_id = account.id50 identity.account_id = account.id
49 session.save_or_update(identity)51 session.save_or_update(identity)
50 session.flush()52 event.session.flush()
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)",
54 identity.id, identity.identity, identity.source, account.id, account.username)
5255
53 identify_cache.clear()56 identify_cache.clear()
54 session.close()
55 event.addresponse(True)57 event.addresponse(True)
5658
57chars = string.letters + string.digits59chars = string.letters + string.digits
@@ -68,44 +70,54 @@
6870
69 @match(r'^(I|.+?)\s+(?:is|am)\s+(.+)\s+on\s+(.+)$')71 @match(r'^(I|.+?)\s+(?:is|am)\s+(.+)\s+on\s+(.+)$')
70 def identity(self, event, username, identity, source):72 def identity(self, event, username, identity, source):
71 session = ibid.databases.ibid()
72 admin = False73 admin = False
73 identity = identity.replace(' ', '')74 identity = identity.replace(' ', '')
7475
75 if username.upper() == 'I':76 if username.upper() == 'I':
76 if event.account:77 if event.account:
77 account = session.query(Account).filter_by(id=event.account).first()78 account = event.session.query(Account).filter_by(id=event.account).first()
78 else:79 else:
79 username = event.sender['id']80 username = event.sender['id']
80 account = session.query(Account).filter_by(username=username).first()81
82 account = event.session.query(Account).filter_by(username=username).first()
83
81 if account:84 if account:
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. '
83 u"Please use 'create account <name>'", username)86 u"Please use 'create account <name>'", username)
84 return87 return
88
85 account = Account(username)89 account = Account(username)
86 session.save_or_update(account)90 event.session.save_or_update(account)
87 session.flush()91
88 currentidentity = session.query(Identity).filter_by(id=event.identity).first()92 currentidentity = event.session.query(Identity).filter_by(id=event.identity).first()
89 currentidentity.account_id = account.id93 currentidentity.account_id = account.id
90 session.save_or_update(currentidentity)94 event.session.save_or_update(currentidentity)
91 session.flush()95
92 identify_cache.clear()96 identify_cache.clear()
97
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)
94 log.info(u"Created account %s (%s) by %s/%s (%s)", account.id, account.username, event.account, event.identity, event.sender['connection'])99
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()
101 log.info(u"Created account %s (%s) by %s/%s (%s)",
102 account.id, account.username, event.account, event.identity, event.sender['connection'])
103 log.info(u"Attached identity %s (%s on %s) to account %s (%s)",
104 currentidentity.id, currentidentity.identity, currentidentity.source, account.id, account.username)
96105
97 else:106 else:
98 if not auth_responses(event, 'accounts'):107 if not auth_responses(event, 'accounts'):
99 return108 return
100 admin = True109 admin = True
101 account = session.query(Account).filter_by(username=username).first()110 account = event.session.query(Account).filter_by(username=username).first()
102 if not account:111 if not account:
103 event.addresponse(u"I don't know who %s is", username)112 event.addresponse(u"I don't know who %s is", username)
104 return113 return
105114
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) \
116 .filter(func.lower(Identity.identity) == identity.lower()) \
117 .filter(func.lower(Identity.source) == source.lower()).first()
107 if ident and ident.account:118 if ident and ident.account:
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',
120 ident.account.username)
109 return121 return
110122
111 if source not in ibid.sources:123 if source not in ibid.sources:
@@ -121,22 +133,26 @@
121 if event.public:133 if event.public:
122 response['target'] = event.sender['id']134 response['target'] = event.sender['id']
123 event.addresponse(response)135 event.addresponse(response)
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)",
137 token, event.account, event.identity, event.sender['connection'])
125138
126 else:139 else:
127 if not ident:140 if not ident:
128 ident = Identity(source, identity)141 ident = Identity(source, identity)
129 ident.account_id = account.id142 ident.account_id = account.id
130 session.save_or_update(ident)143 event.session.save_or_update(ident)
131 session.flush()144
132 identify_cache.clear()145 identify_cache.clear()
133 event.addresponse(True)146 event.addresponse(True)
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
148 event.session.flush()
149 log.info(u"Attached identity %s (%s on %s) to account %s (%s) by %s/%s (%s)",
150 ident.id, ident.identity, ident.source, account.id, account.username,
151 event.account, event.identity, event.sender['connection'])
135152
136 @match(r'^(\S{16})$')153 @match(r'^(\S{16})$')
137 def token(self, event, token):154 def token(self, event, token):
138 if token in self.tokens:155 if token in self.tokens:
139 session = ibid.databases.ibid()
140 (account_id, user, source) = self.tokens[token]156 (account_id, user, source) = self.tokens[token]
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():
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', {
@@ -145,45 +161,49 @@
145 })161 })
146 return162 return
147163
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) \
165 .filter(func.lower(Identity.identity) == user.lower()) \
166 .filter(func.lower(Identity.source) == source.lower()).first()
149 if not identity:167 if not identity:
150 identity = Identity(source, user)168 identity = Identity(source, user)
151 identity.account_id = account_id169 identity.account_id = account_id
152 session.save_or_update(identity)170 event.session.save_or_update(identity)
153 session.flush()
154 session.close()
155 identify_cache.clear()171 identify_cache.clear()
156172
157 del self.tokens[token]173 del self.tokens[token]
158 event.addresponse(u'Identity added')174 event.addresponse(u'Identity added')
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
176 event.session.flush()
177 log.info(u"Attached identity %s (%s on %s) to account %s by %s/%s (%s) with token %s",
178 identity.id, identity.identity, identity.source, account_id, event.account,
179 event.identity, event.sender['connection'], token)
160180
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+))?$')
162 def remove(self, event, user, source, username):182 def remove(self, event, user, source, username):
163 session = ibid.databases.ibid()
164
165 if not username:183 if not username:
166 account = session.query(Account).get(event.account)184 account = event.session.query(Account).get(event.account)
167 else:185 else:
168 if not auth_responses(event, 'accounts'):186 if not auth_responses(event, 'accounts'):
169 return187 return
170 account = session.query(Account).filter_by(username=username).first()188 account = event.session.query(Account).filter_by(username=username).first()
171 if not account:189 if not account:
172 event.addresponse(u"I don't know who %s is", username)190 event.addresponse(u"I don't know who %s is", username)
173 return191 return
174192
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) \
194 .filter_by(account_id=account.id) \
195 .filter(func.lower(Identity.identity) == user.lower()) \
196 .filter(func.lower(Identity.source) == source.lower()).first()
176 if not identity:197 if not identity:
177 event.addresponse(u"I don't know about that identity")198 event.addresponse(u"I don't know about that identity")
178 else:199 else:
179 identity.account_id = None200 identity.account_id = None
180 session.save_or_update(identity)201 event.session.save_or_update(identity)
181 session.flush()
182 identify_cache.clear()202 identify_cache.clear()
183 event.addresponse(True)203 event.addresponse(True)
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)",
185205 identity.id, identity.identity, identity.source, account.id,
186 session.close()206 account.username, event.account, event.identity, event.sender['connection'])
187207
188class Attributes(Processor):208class Attributes(Processor):
189 u"""set (my|<account>) <name> to <value>"""209 u"""set (my|<account>) <name> to <value>"""
@@ -191,13 +211,12 @@
191211
192 @match(r"^set\s+(my|.+?)(?:\'s)?\s+(.+)\s+to\s+(.+)$")212 @match(r"^set\s+(my|.+?)(?:\'s)?\s+(.+)\s+to\s+(.+)$")
193 def attribute(self, event, username, name, value):213 def attribute(self, event, username, name, value):
194 session = ibid.databases.ibid()
195214
196 if username.lower() == 'my':215 if username.lower() == 'my':
197 if not event.account:216 if not event.account:
198 event.addresponse(u"I don't know who you are")217 event.addresponse(u"I don't know who you are")
199 return218 return
200 account = session.query(Account).filter_by(id=event.account).first()219 account = event.session.query(Account).filter_by(id=event.account).first()
201 if not account:220 if not account:
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)
203 return222 return
@@ -205,17 +224,17 @@
205 else:224 else:
206 if not auth_responses(event, 'accounts'):225 if not auth_responses(event, 'accounts'):
207 return226 return
208 account = session.query(Account).filter_by(username=username).first()227 account = event.session.query(Account).filter_by(username=username).first()
209 if not account:228 if not account:
210 event.addresponse(u"I don't know who %s is", username)229 event.addresponse(u"I don't know who %s is", username)
211 return230 return
212231
213 account.attributes.append(Attribute(name, value))232 account.attributes.append(Attribute(name, value))
214 session.save_or_update(account)233 event.session.save_or_update(account)
215 session.flush()
216 session.close()
217 event.addresponse(True)234 event.addresponse(True)
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)",
236 name, value, account.id, account.username, event.account,
237 event.identity, event.sender['connection'])
219238
220class Describe(Processor):239class Describe(Processor):
221 u"""who (am I|is <username>)"""240 u"""who (am I|is <username>)"""
@@ -223,19 +242,18 @@
223242
224 @match(r'^who\s+(?:is|am)\s+(I|.+?)$')243 @match(r'^who\s+(?:is|am)\s+(I|.+?)$')
225 def describe(self, event, username):244 def describe(self, event, username):
226 session = ibid.databases.ibid()
227 if username.upper() == 'I':245 if username.upper() == 'I':
228 if not event.account:246 if not event.account:
229 identity = session.query(Identity).get(event.identity)247 identity = event.session.query(Identity).get(event.identity)
230 event.addresponse(u"%(name)s on %(source)s", {248 event.addresponse(u"%(name)s on %(source)s", {
231 'name': identity.identity,249 'name': identity.identity,
232 'source': identity.source,250 'source': identity.source,
233 })251 })
234 return252 return
235 account = session.query(Account).get(event.account)253 account = event.session.query(Account).get(event.account)
236254
237 else:255 else:
238 account = session.query(Account).filter_by(username=username).first()256 account = event.session.query(Account).filter_by(username=username).first()
239 if not account:257 if not account:
240 event.addresponse(u"I don't know who %s is", username)258 event.addresponse(u"I don't know who %s is", username)
241 return259 return
@@ -244,7 +262,6 @@
244 'accountname': account.username,262 'accountname': account.username,
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),
246 })264 })
247 session.close()
248265
249class Identify(Processor):266class Identify(Processor):
250267
@@ -256,25 +273,26 @@
256 (event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])]273 (event.identity, event.account) = identify_cache[(event.source, event.sender['connection'])]
257 return274 return
258275
259 session = ibid.databases.ibid()276 identity = event.session.query(Identity) \
260 identity = session.query(Identity)\277 .options(eagerload('account')) \
261 .options(eagerload('account'))\278 .filter(func.lower(Identity.source) == event.source.lower()) \
262 .filter(func.lower(Identity.source) == event.source.lower())\279 .filter(func.lower(Identity.identity) == event.sender['id'].lower()) \
263 .filter(func.lower(Identity.identity) == event.sender['id'].lower())\
264 .first()280 .first()
265 if not identity:281 if not identity:
266 identity = Identity(event.source, event.sender['id'])282 identity = Identity(event.source, event.sender['id'])
267 session.save_or_update(identity)283 event.session.save_or_update(identity)
268 try:284 try:
269 session.flush()285 event.session.commit()
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)
271 except IntegrityError:287 except IntegrityError:
272 session.expunge(identity)288 event.session.rollback()
289 event.session.close()
290 del event['session']
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)
274 identity = session.query(Identity)\292 identity = event.session.query(Identity) \
275 .options(eagerload('account'))\293 .options(eagerload('account')) \
276 .filter(func.lower(Identity.source) == event.source.lower())\294 .filter(func.lower(Identity.source) == event.source.lower()) \
277 .filter(func.lower(Identity.identity) == event.sender['id'].lower())\295 .filter(func.lower(Identity.identity) == event.sender['id'].lower()) \
278 .one()296 .one()
279297
280 event.identity = identity.id298 event.identity = identity.id
@@ -284,29 +302,9 @@
284 event.account = None302 event.account = None
285 identify_cache[(event.source, event.sender['connection'])] = (event.identity, event.account)303 identify_cache[(event.source, event.sender['connection'])] = (event.identity, event.account)
286304
287 session.close()305def get_identities(event):
288
289def identify(source, user):
290
291 session = ibid.databases.ibid()
292
293 identity = session.query(Identity).filter(func.lower(Identity.source)==source.lower()).filter(func.lower(Identity.identity)==user.lower()).first()
294 account = session.query(Account).filter_by(username=user).first()
295
296 if not account and not identity:
297 return None
298 if not account:
299 return identity
300 if not identity or identity in account.identities:
301 return account
302 return (account, identity)
303
304def get_identities(event, session=None):
305 if not session:
306 session = ibid.databases.ibid()
307
308 if event.account:306 if event.account:
309 account = session.query(Account).get(event.account)307 account = event.session.query(Account).get(event.account)
310 return [identity.id for identity in account.identities]308 return [identity.id for identity in account.identities]
311 else:309 else:
312 return (event.identity,)310 return (event.identity,)
313311
=== modified file 'ibid/plugins/karma.py'
--- ibid/plugins/karma.py 2009-05-01 12:17:57 +0000
+++ ibid/plugins/karma.py 2009-05-03 16:33:10 +0000
@@ -4,7 +4,6 @@
4from sqlalchemy import Column, Integer, Unicode, DateTime, Table4from sqlalchemy import Column, Integer, Unicode, DateTime, Table
5from sqlalchemy.sql import func5from sqlalchemy.sql import func
66
7import ibid
8from ibid.plugins import Processor, match, handler, authorise7from ibid.plugins import Processor, match, handler, authorise
9from ibid.config import Option, BoolOption, IntOption8from ibid.config import Option, BoolOption, IntOption
10from ibid.models import Base, VersionedSchema9from ibid.models import Base, VersionedSchema
@@ -59,8 +58,8 @@
59 if subject.lower() in self.ignore:58 if subject.lower() in self.ignore:
60 return59 return
6160
62 session = ibid.databases.ibid()61 karma = event.session.query(Karma) \
63 karma = session.query(Karma).filter(func.lower(Karma.subject)==subject.lower()).first()62 .filter(func.lower(Karma.subject) == subject.lower()).first()
64 if not karma:63 if not karma:
65 karma = Karma(subject)64 karma = Karma(subject)
6665
@@ -82,14 +81,12 @@
82 if karma.value == 0 and karma.changes <= self.importance:81 if karma.value == 0 and karma.changes <= self.importance:
83 change = u'Forgotten (unimportant)'82 change = u'Forgotten (unimportant)'
8483
85 session.delete(karma)84 event.session.delete(karma)
86 else:85 else:
87 session.save_or_update(karma)86 event.session.save_or_update(karma)
8887
89 session.flush()88 log.info(u"%s karma for '%s' by %s/%s (%s) because: %s",
90 session.close()89 change, subject, event.account, event.identity, event.sender['connection'], reason)
91
92 log.info(u"%s karma for '%s' by %s/%s (%s) because: %s", change, subject, event.account, event.identity, event.sender['connection'], reason)
9390
94 if self.reply:91 if self.reply:
95 event.addresponse(True)92 event.addresponse(True)
@@ -103,8 +100,8 @@
103100
104 @match(r'^karma\s+(?:for\s+)?(.+)$')101 @match(r'^karma\s+(?:for\s+)?(.+)$')
105 def handle_karma(self, event, subject):102 def handle_karma(self, event, subject):
106 session = ibid.databases.ibid()103 karma = event.session.query(Karma) \
107 karma = session.query(Karma).filter(func.lower(Karma.subject)==subject.lower()).first()104 .filter(func.lower(Karma.subject) == subject.lower()).first()
108 if not karma:105 if not karma:
109 event.addresponse(u'nobody cares, dude')106 event.addresponse(u'nobody cares, dude')
110 elif karma.value == 0:107 elif karma.value == 0:
@@ -114,17 +111,16 @@
114 'subject': subject,111 'subject': subject,
115 'value': karma.value,112 'value': karma.value,
116 })113 })
117 session.close()
118114
119 @match(r'^(reverse\s+)?karmaladder$')115 @match(r'^(reverse\s+)?karmaladder$')
120 def ladder(self, event, reverse):116 def ladder(self, event, reverse):
121 session = ibid.databases.ibid()117 karmas = event.session.query(Karma) \
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()) \
119 .limit(30).all()
123 if karmas:120 if karmas:
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]))
125 else:122 else:
126 event.addresponse(u"I don't really care about anything")123 event.addresponse(u"I don't really care about anything")
127 session.close()
128124
129class Forget(Processor):125class Forget(Processor):
130 u"""forget karma for <subject> [[reason]]"""126 u"""forget karma for <subject> [[reason]]"""
@@ -138,17 +134,16 @@
138 @match(r'^forget\s+karma\s+for\s+(.+?)(?:\s*[[{(]+\s*(.+?)\s*[\]})]+)?$')134 @match(r'^forget\s+karma\s+for\s+(.+?)(?:\s*[[{(]+\s*(.+?)\s*[\]})]+)?$')
139 @authorise135 @authorise
140 def forget(self, event, subject, reason):136 def forget(self, event, subject, reason):
141 session = ibid.databases.ibid()137 karma = event.session.query(Karma) \
142 karma = session.query(Karma).filter(func.lower(Karma.subject)==subject.lower()).first()138 .filter(func.lower(Karma.subject) == subject.lower()).first()
143 if not karma:139 if not karma:
144 karma = Karma(subject)140 karma = Karma(subject)
145 event.addresponse(u"I was pretty ambivalent about %s, anyway", subject)141 event.addresponse(u"I was pretty ambivalent about %s, anyway", subject)
146142
147 session.delete(karma)143 event.session.delete(karma)
148 session.flush()
149 session.close()
150144
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",
146 subject, event.account, event.identity, event.sender['connection'], reason)
152 event.addresponse(True)147 event.addresponse(True)
153148
154# vi: set et sta sw=4 ts=4:149# vi: set et sta sw=4 ts=4:
155150
=== modified file 'ibid/plugins/memo.py'
--- ibid/plugins/memo.py 2009-05-01 12:27:24 +0000
+++ ibid/plugins/memo.py 2009-05-03 16:33:10 +0000
@@ -5,10 +5,9 @@
5from sqlalchemy.orm import relation5from sqlalchemy.orm import relation
6from sqlalchemy.sql import func6from sqlalchemy.sql import func
77
8import ibid
9from ibid.plugins import Processor, handler, match, authorise8from ibid.plugins import Processor, handler, match, authorise
10from ibid.config import Option9from ibid.config import Option
11from ibid.plugins.auth import permission10from ibid.auth import permission
12from ibid.plugins.identity import get_identities11from ibid.plugins.identity import get_identities
13from ibid.models import Base, VersionedSchema, Identity, Account12from ibid.models import Base, VersionedSchema, Identity, Account
14from ibid.utils import ago13from ibid.utils import ago
@@ -51,10 +50,12 @@
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+)?(.+)$')
52 @authorise51 @authorise
53 def tell(self, event, how, who, memo):52 def tell(self, event, how, who, memo):
54 session = ibid.databases.ibid()53 to = event.session.query(Identity) \
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()) \
55 .filter_by(source=event.source).first()
56 if not to:56 if not to:
57 account = session.query(Account).filter(func.lower(Account.username)==who.lower()).first()57 account = event.session.query(Account) \
58 .filter(func.lower(Account.username) == who.lower()).first()
58 if account:59 if account:
59 for identity in account.identities:60 for identity in account.identities:
60 if identity.source == event.source:61 if identity.source == event.source:
@@ -63,27 +64,32 @@
63 identity = account.identities[0]64 identity = account.identities[0]
64 if not to:65 if not to:
65 to = Identity(event.source, who)66 to = Identity(event.source, who)
66 session.save(to)67 event.session.save(to)
67 session.flush()68
69 event.session.flush()
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)
6971
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':
71 event.addresponse(u'Just tell %s yourself', who)73 event.addresponse(u'Just tell %s yourself', who)
72 return74 return
7375
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'))
75 session.save_or_update(memo)77 event.session.save_or_update(memo)
76 session.flush()78
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()
80 log.info(u"Stored memo %s for %s (%s) from %s (%s): %s",
81 memo.id, to.id, who, event.identity, event.sender['connection'], memo.memo)
78 event.memo = memo.id82 event.memo = memo.id
79 session.close()
80 memo_cache.clear()83 memo_cache.clear()
8184
82 event.addresponse(True)85 event.addresponse(True)
8386
84def get_memos(session, event, delivered=False):87def get_memos(event, delivered=False):
85 identities = get_identities(event, session)88 identities = get_identities(event)
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) \
90 .filter_by(delivered=delivered) \
91 .filter(Memo.to_id.in_(identities)) \
92 .order_by(Memo.time.asc()).all()
8793
88class Deliver(Processor):94class Deliver(Processor):
89 feature = 'memo'95 feature = 'memo'
@@ -96,8 +102,7 @@
96 if event.identity in memo_cache:102 if event.identity in memo_cache:
97 return103 return
98104
99 session = ibid.databases.ibid()105 memos = get_memos(event)
100 memos = get_memos(session, event)
101106
102 for memo in memos:107 for memo in memos:
103 # Don't deliver if the user just sent a memo to themself108 # Don't deliver if the user just sent a memo to themself
@@ -122,11 +127,9 @@
122 })127 })
123128
124 memo.delivered = True129 memo.delivered = True
125 session.save_or_update(memo)130 event.session.save_or_update(memo)
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)",
127132 memo.id, event.identity, event.sender['connection'])
128 session.flush()
129 session.close()
130133
131 if 'memo' not in event:134 if 'memo' not in event:
132 memo_cache[event.identity] = None135 memo_cache[event.identity] = None
@@ -146,16 +149,13 @@
146 if event.identity in memo_cache:149 if event.identity in memo_cache:
147 return150 return
148151
149 session = ibid.databases.ibid()152 memos = get_memos(event)
150 memos = get_memos(session, event)
151153
152 if len(memos) > 0:154 if len(memos) > 0:
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']})
154 else:156 else:
155 memo_cache[event.identity] = None157 memo_cache[event.identity] = None
156158
157 session.close()
158
159class Messages(Processor):159class Messages(Processor):
160 u"""my messages160 u"""my messages
161 message <number>"""161 message <number>"""
@@ -165,18 +165,15 @@
165165
166 @match(r'^my\s+messages$')166 @match(r'^my\s+messages$')
167 def messages(self, event):167 def messages(self, event):
168 session = ibid.databases.ibid()168 memos = get_memos(event, True)
169 memos = get_memos(session, event, True)
170 if memos:169 if memos:
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]))
172 else:171 else:
173 event.addresponse(u"Sorry, nobody loves you")172 event.addresponse(u"Sorry, nobody loves you")
174 session.close()
175173
176 @match(r'^message\s+(\d+)$')174 @match(r'^message\s+(\d+)$')
177 def message(self, event, number):175 def message(self, event, number):
178 session = ibid.databases.ibid()176 memos = get_memos(event, True)
179 memos = get_memos(session, event, True)
180 memo = memos[int(number)]177 memo = memos[int(number)]
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", {
182 'sender': memo.sender.identity,179 'sender': memo.sender.identity,
@@ -184,7 +181,5 @@
184 'time': memo.time.strftime(self.datetime_format),181 'time': memo.time.strftime(self.datetime_format),
185 'message': memo.memo,182 'message': memo.memo,
186 })183 })
187 session.close()
188
189184
190# vi: set et sta sw=4 ts=4:185# vi: set et sta sw=4 ts=4:
191186
=== modified file 'ibid/plugins/seen.py'
--- ibid/plugins/seen.py 2009-05-01 12:22:37 +0000
+++ ibid/plugins/seen.py 2009-05-03 16:37:03 +0000
@@ -6,7 +6,6 @@
6from sqlalchemy.sql import func6from sqlalchemy.sql import func
7from sqlalchemy.exceptions import IntegrityError7from sqlalchemy.exceptions import IntegrityError
88
9import ibid
10from ibid.plugins import Processor, match9from ibid.plugins import Processor, match
11from ibid.config import Option10from ibid.config import Option
12from ibid.models import Base, VersionedSchema, Identity, Account11from ibid.models import Base, VersionedSchema, Identity, Account
@@ -51,8 +50,9 @@
51 if event.type != 'message' and event.type != 'state':50 if event.type != 'message' and event.type != 'state':
52 return51 return
5352
54 session = ibid.databases.ibid()53 sighting = event.session.query(Sighting) \
55 sighting = session.query(Sighting).filter_by(identity_id=event.identity).filter_by(type=event.type).first()54 .filter_by(identity_id=event.identity) \
55 .filter_by(type=event.type).first()
56 if not sighting:56 if not sighting:
57 sighting = Sighting(event.identity, event.type)57 sighting = Sighting(event.identity, event.type)
5858
@@ -65,12 +65,15 @@
65 sighting.time = datetime.now()65 sighting.time = datetime.now()
66 sighting.count = sighting.count + 166 sighting.count = sighting.count + 1
6767
68 event.session.save_or_update(sighting)
68 try:69 try:
69 session.save_or_update(sighting)70 event.session.commit()
70 session.flush()
71 except IntegrityError:71 except IntegrityError:
72 log.debug('Race on seen update for identity %s', event.identity)72 event.session.rollback()
73 session.close()73 event.session.close()
74 del event['session']
75 log.debug(u'Race encountered updating seen for %s on %s',
76 event.sender['id'], event.source)
7477
75class Seen(Processor):78class Seen(Processor):
76 u"""seen <who>"""79 u"""seen <who>"""
@@ -81,14 +84,15 @@
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+))?$')
82 def handler(self, event, who, source):85 def handler(self, event, who, source):
8386
84 session = ibid.databases.ibid()
85 account = None87 account = None
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) \
89 .filter(func.lower(Identity.source) == (source and source or event.source).lower()) \
90 .filter(func.lower(Identity.identity) == who.lower()).first()
87 if identity and identity.account and not source:91 if identity and identity.account and not source:
88 account = identity.account92 account = identity.account
8993
90 if not identity and not source:94 if not identity and not source:
91 account = session.query(Account).filter_by(username=who).first()95 account = event.session.query(Account).filter_by(username=who).first()
9296
93 if not identity and not account:97 if not identity and not account:
94 event.addresponse(u"I don't know who %s is", who)98 event.addresponse(u"I don't know who %s is", who)
@@ -98,13 +102,15 @@
98 states = []102 states = []
99 if account:103 if account:
100 for identity in account.identities:104 for identity in account.identities:
101 for sighting in session.query(Sighting).filter_by(identity_id=identity.id).all():105 for sighting in event.session.query(Sighting) \
106 .filter_by(identity_id=identity.id).all():
102 if sighting.type == 'message':107 if sighting.type == 'message':
103 messages.append(sighting)108 messages.append(sighting)
104 else:109 else:
105 states.append(sighting)110 states.append(sighting)
106 else:111 else:
107 for sighting in session.query(Sighting).filter_by(identity_id=identity.id).all():112 for sighting in event.session.query(Sighting) \
113 .filter_by(identity_id=identity.id).all():
108 if sighting.type == 'message':114 if sighting.type == 'message':
109 messages.append(sighting)115 messages.append(sighting)
110 else:116 else:
@@ -133,6 +139,5 @@
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))
134140
135 event.addresponse(reply)141 event.addresponse(reply)
136 session.close()
137142
138# vi: set et sta sw=4 ts=4:143# vi: set et sta sw=4 ts=4:
139144
=== modified file 'ibid/plugins/url.py'
--- ibid/plugins/url.py 2009-05-01 12:22:37 +0000
+++ ibid/plugins/url.py 2009-05-02 19:29:44 +0000
@@ -114,11 +114,8 @@
114 else:114 else:
115 url = 'http://%s' % url115 url = 'http://%s' % url
116116
117 session = ibid.databases.ibid()
118 u = URL(url, event.channel, event.identity)117 u = URL(url, event.channel, event.identity)
119 session.save_or_update(u)118 event.session.save_or_update(u)
120 session.flush()
121 session.close()
122119
123 if self.username != None:120 if self.username != None:
124 self.delicious.add_post(self.username, self.password, event, url)121 self.delicious.add_post(self.username, self.password, event, url)
125122
=== modified file 'ibid/source/irc.py'
--- ibid/source/irc.py 2009-05-01 12:59:07 +0000
+++ ibid/source/irc.py 2009-05-02 19:29:44 +0000
@@ -237,8 +237,9 @@
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)
238238
239 def auth_hostmask(self, event, credential = None):239 def auth_hostmask(self, event, credential = None):
240 session = ibid.databases.ibid()240 for credential in event.session.query(Credential) \
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) \
242 .filter(or_(Credential.source == event.source, Credential.source == None)).all():
242 if fnmatch(event.sender['connection'], credential.credential):243 if fnmatch(event.sender['connection'], credential.credential):
243 return True244 return True
244245
245246
=== modified file 'scripts/ibid-factpack'
--- scripts/ibid-factpack 2009-04-26 11:04:32 +0000
+++ scripts/ibid-factpack 2009-05-02 19:29:44 +0000
@@ -28,7 +28,6 @@
28ibid.reload_reloader()28ibid.reload_reloader()
29ibid.reloader.reload_databases()29ibid.reloader.reload_databases()
30session = ibid.databases.ibid()30session = ibid.databases.ibid()
31session.begin()
3231
33if options.remove:32if options.remove:
34 factpack = session.query(Factpack).filter_by(name=filename).first()33 factpack = session.query(Factpack).filter_by(name=filename).first()
@@ -76,7 +75,6 @@
7675
77factpack = Factpack(name)76factpack = Factpack(name)
78session.save(factpack)77session.save(factpack)
79session.flush()
8078
81existing = []79existing = []
82for names, values in facts:80for names, values in facts:
8381
=== modified file 'scripts/ibid-plugin'
--- scripts/ibid-plugin 2009-05-02 16:20:45 +0000
+++ scripts/ibid-plugin 2009-05-03 16:37:03 +0000
@@ -14,6 +14,7 @@
14 pass14 pass
1515
16from twisted.python.modules import getModule16from twisted.python.modules import getModule
17from sqlalchemy.exceptions import IntegrityError
1718
18import ibid19import ibid
19import ibid.plugins20import ibid.plugins
@@ -80,15 +81,19 @@
80username = unicode(getenv('USER'))81username = unicode(getenv('USER'))
81if not username:82if not username:
82 username = u'tester'83 username = u'tester'
84
83session = ibid.databases.ibid()85session = ibid.databases.ibid()
86
84identity = session.query(Identity).filter_by(identity=username).first()87identity = session.query(Identity).filter_by(identity=username).first()
85if not identity:88if not identity:
86 identity = Identity(u'test_source',username)89 identity = Identity(u'test_source',username)
87 session.save(identity)90 session.save(identity)
88 session.flush()91 session.commit()
89 identity = session.query(Identity).filter_by(identity=username).first()92 identity = session.query(Identity).filter_by(identity=username).first()
90identity_id = identity.id93identity_id = identity.id
9194
95session.close()
96
92try:97try:
93 encoding = getenv("LANG").split(".")[1]98 encoding = getenv("LANG").split(".")[1]
94except:99except:
@@ -115,14 +120,28 @@
115 try:120 try:
116 processor.process(event)121 processor.process(event)
117 except Exception:122 except Exception:
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",
124 processor.__class__.__name__, processor.name)
119 event.complain = 'exception'125 event.complain = 'exception'
120126
127 if 'session' in event:
128 try:
129 event.session.commit()
130 except IntegrityError:
131 self.log.exception(u"Exception occured committing session from the %s processor of %s plugin",
132 processor.__class__.__name__, processor.name)
133 event.complain = u'exception'
134 event.session.rollback()
135 event.session.close()
136 del event['session']
137
121 for response in event.responses:138 for response in event.responses:
122 if isinstance(response, dict):139 if isinstance(response, dict):
123 response = response['reply']140 response = response['reply']
124 print 'Response: %s' % response141 print 'Response: %s' % response
125142
143 event.session.close()
144
126if readline:145if readline:
127 readline.write_history_file('.history')146 readline.write_history_file('.history')
128print "\nExiting"147print "\nExiting"
129148
=== modified file 'scripts/ibid-setup'
--- scripts/ibid-setup 2009-05-01 10:39:58 +0000
+++ scripts/ibid-setup 2009-05-02 19:29:44 +0000
@@ -83,7 +83,6 @@
83 exit(1)83 exit(1)
8484
85session = Session()85session = Session()
86session.begin()
87account = Account(identity)86account = Account(identity)
88identity = Identity(source, identity)87identity = Identity(source, identity)
89account.identities.append(identity)88account.identities.append(identity)
9089
=== modified file 'scripts/ibid_import'
--- scripts/ibid_import 2009-02-22 07:43:34 +0000
+++ scripts/ibid_import 2009-05-02 19:29:44 +0000
@@ -63,7 +63,9 @@
63 return None63 return None
64 user = decode(user)64 user = decode(user)
65 65
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) \
67 .filter(func.lower(Identity.identity) == user.lower()) \
68 .filter(func.lower(Identity.source) == source.lower()).first()
67 if not identity:69 if not identity:
68 identity = Identity(source, user)70 identity = Identity(source, user)
69 identity.created = created71 identity.created = created
@@ -231,5 +233,6 @@
231 print 'Done'233 print 'Done'
232234
233 knab.close()235 knab.close()
236 ibid.close()
234237
235# vi: set et sta sw=4 ts=4:238# vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches