Author: VenkatramanSubramanian Date: 2009-01-16 13:47:50 -0500 (Fri, 16 Jan 2009) New Revision: 1484
Added: trunk/concordance/examples/imiq.py Log: Added imiq(for xep-0148) Added: trunk/concordance/examples/imiq.py =================================================================== --- trunk/concordance/examples/imiq.py (rev 0) +++ trunk/concordance/examples/imiq.py 2009-01-16 18:47:50 UTC (rev 1484) @@ -0,0 +1,129 @@ +#!/usr/bin/env python3.0 + +''' Simple Concordance server that finds out your IQ(XEP-0148)''' + +__credits__ = '''Copyright (C) 2009 Copyleft Games Group + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, see http://www.gnu.org/licenses +''' +__author__ = 'Copyleft Games Group' +__date__ = 'Last change on '+ \ + '$Date: 2009-01-15 01:24:08 +0530 (Thu, 15 Jan 2009) $'[7:-20]+ \ + 'by '+'$Author: VenkatramanSubramanian $'[9:-2] +__version__ = 'Trunk (r'+'$Rev: 1483 $'[6:-2]+')' + + +import concordance +import xml.etree.cElementTree as ElementTree + +#stores Userid:ImIQScore +UserDB={} + +#Stored UserID:LastSuggestedIQMessage +#this message will be compared with the message sent by the user and +#if it matches, then the score of the user will be lowered +UserLastIQMsg={} + +class ImIQ(concordance.Core) : + def createNode(self, parent_node,tag_name, tag_val): + ''' + Method that creates a node given its name and value; + It is attached to its parent + TO-DO : Make this as th standard way of creating a 'simple'node; + can this be moved to a better OO paradigm? + ''' + temp_node = ElementTree.SubElement(parent_node, tag_name) + temp_node.text = tag_val + + def failedError(self, err_code, err_type, err_desc, input): + ''' + Error Information to be added when something fails or errors out + ''' + out_error = ElementTree.SubElement(input, 'error') + out_error.attrib['code'] = err_code + out_error.attrib['type'] = err_type + ElementTree.SubElement(out_error, err_desc) + out_error[0].attrib['xmlns'] ='urn:ietf:params:xml:ns:xmpp-stanzas' + + def getIntelligentResponse(self, input, out_root): + ''' + Server Hints at a More Intelligent Message + Example: + User may ask : d00d, u r dum -- RTFM, OK? + Suggested : Ive heard that theres this thing called the Internet, + which contains incredible amounts of helpful information. + Have you considered using it? + ''' + out_query = ElementTree.SubElement(out_root, 'query') + out_query.attrib['xmlns'] = 'jabber:iq:iq' + ElementTree.SubElement(out_query, 'hint') + out_query[0].text = 'Have you ever considered the thing called Internet?' #TODO: obviously! + + def getIQUser(self, input, out_root): + ''' + Send the Users IQ + ''' + + def setIQUser(self, input, out_root): + ''' + Set the Users IQ + Reduces the Users IQ, if he/she wants to set his score + TODO - needs to be checked for a few things + ''' + #construct the 'query' node and its children + out_query = ElementTree.SubElement(out_root, 'query') + out_query.attrib['xmlns'] = 'jabber:iq:iq' + self.createNode(out_query,'num',100) #TODO Hardcoded Value ??? + self.createNode(out_query,'desc',genius) #TODO Hardcoded Value ??? + + #if the user whose score is to be set is the same as the user + # then throw an error and reduce his score + # self.failedError('405', 'cancel', 'not-allowed', out_root) #??? Uncomment this + #Reduce the score + #UserDB[get-the-email] = UserDB[get-the-email] - 10 + + def clientHandle(self, session, message) : + ''' + this method keeps waiting for a message and + dispatches it to the suitabe method after filtering + ''' + # get xml root of input + print("C: %s" % message) + in_root = ElementTree.fromstring(message) + + # this example only replies to <iq/> requests + if in_root.tag != '{jabber:client}iq' : + return "" + + out_root = ElementTree.Element('iq') + out_root.attrib['id'] = in_root.attrib['id'] + + if in_root.attrib['type'] == 'get' : + out_root.attrib['type'] = 'result' + for query in in_root : + #this should be made to accept only one user + self.getIQUser(query, out_root) + elif in_root.attrib['type'] == 'set': + self.setIQUser(query, out_root) + print("Dumb User - wants to set his own IQ") + + else : + return "" + + print("S: %s" % ElementTree.tostring(out_root)) + return ElementTree.tostring(out_root) + +#clientHandle just waits for any events from the client/entity +imiqcore=ImIQ() +imiqcore() Property changes on: trunk/concordance/examples/imiq.py ___________________________________________________________________ Added: svn:executable + * _______________________________________________ PySoy-SVN mailing list PySoy-SVN@pysoy.org http://www.pysoy.org/mailman/listinfo/pysoy-svn