Package spade :: Module Organization_new
[hide private]
[frames] | no frames]

Source Code for Module spade.Organization_new

  1  # -*- coding: UTF8 -*- 
  2  import random 
  3  import string 
  4   
  5  from xmpp import * 
  6  from Queue import * 
  7  import Unit_new 
  8  import DF 
  9  import Behaviour 
 10   
 11   
12 -class CreationError(Exception):
13 - def __init__(self):
14 Exception.__init__(self)
15 -class NotValidName(CreationError):
16 - def __init__(self):
17 Exception.__init__(self)
18 -class NotValidType(CreationError):
19 - def __init__(self):
20 Exception.__init__(self)
21 -class NotValidGoal(CreationError):
22 - def __init__(self):
23 Exception.__init__(self)
24 -class NotCreatePermision(CreationError):
25 - def __init__(self):
26 Exception.__init__(self)
27 -class NotSupervisor(CreationError):
28 - def __init__(self):
29 Exception.__init__(self)
30 -class JoinError(Exception):
31 - def __init__(self):
32 Exception.__init__(self)
33 -class PaswordNeeded(JoinError):
34 - def __init__(self):
35 pass
36 -class MembersOnly(JoinError):
37 - def __init__(self):
38 Exception.__init__(self)
39 -class BanedUser(JoinError):
40 - def __init__(self):
41 Exception.__init__(self)
42 -class NickNameConflict(JoinError):
43 - def __init__(self):
44 Exception.__init__(self)
45 -class MaximumUsers(JoinError):
46 - def __init__(self):
47 pass
48 -class LockedOrganization(JoinError):
49 - def __init__(self):
50 Exception.__init__(self)
51 -class MemberOfFederation(JoinError):
52 - def __init__(self):
53 Exception.__init__(self)
54 -class Unavailable(Exception):
55 - def __init__(self):
56 Exception.__init__(self)
57 -class UnavailableFunction(Exception):
58 - def __init__(self):
59 Exception.__init__(self)
60 -class DestroyError(Exception):
61 - def __init__(self):
62 Exception.__init__(self)
63 -class NotValidUnit(Exception):
64 - def __init__(self):
65 Exception.__init__(self)
66 -class LastOwner(Exception):
67 - def __init__(self):
68 Exception.__init__(self)
69
70 -class Organization_new(Unit_new.Unit_new):
71
72 - def __init__(self,agent, nick, name, type=None, goalList=None, agentList=[], contentLanguage="sl",create=True):
73 Behaviour.OneShotBehaviour.__init__(self) 74 self.myAgent=agent 75 self.name = name 76 self.type = type 77 self.goalList = goalList 78 self.agentList = agentList 79 self.contentLanguage = contentLanguage 80 self.platform = self.myAgent.getSpadePlatformJID() 81 self.muc_name = self.myAgent.getMUC() 82 self._roster = {} 83 self.nick=nick 84 self.create=create 85 id_base = "".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) #@UnusedVariable 86 self.ID = str(name) + str(self.myAgent.getAID().getName()) + id_base 87 self.state="unavailable" 88 self.UnavailableMsg="Organization" 89 self.members=[] 90 self.owner=False 91 self.orgOwner=None
92 93
94 - def setup(self):
95 pass
96
97 - def myCreate(self):
98 if not self.checkGoal(self.goalList): 99 raise NotValidGoal 100 elif not self.checkType(): 101 raise NotVvalidType 102 elif not self.testRoomName(): 103 raise NotValidName 104 elif not self.createRoom(): 105 raise CreationError 106 else: 107 self.state="available" 108 if self.agentList!=[]: 109 #enviando invitaciones 110 self.invite(self.agentList) 111 #registrando en el DF 112 # dad = DF.DfAgentDescription() 113 # ds = DF.ServiceDescription() 114 # ds.setType("ORGANIZATION") 115 # ds.setName(self.name) 116 # dad.setAID(self.myAgent.getAID()) 117 # dad.addService(ds) 118 # res = self.myAgent.registerService(dad) 119 120 self.owner=True 121 self.orgOwner=self.myAgent.JID 122 p = Presence() 123 t = Behaviour.MessageTemplate(p) 124 self.presenceBehaviour=self.PresenceBehaviour(self.muc_name,self.name,self.nick,self) 125 self.myAgent.addBehaviour(self.presenceBehaviour, t) 126 if self.type=="Matrix" or self.type=="Federation": 127 self.createTeam()
128 129
130 - def myJoin(self):
131 #The Organization exists 132 if not self.testOrganizationName(): 133 #The room no existe 134 raise NotValidName 135 elif not self.myJoinRoom(): 136 #No es una organizacion 137 #raise JoinError 138 pass 139 else: 140 info=self.getInfo() 141 if info: 142 self.type = info["type"] 143 self.contentLanguage = info["contentLanguage"] 144 self.parent=info["parent"] 145 self.goal=info["goal"] 146 self.state="available" 147 p = Presence() 148 t = Behaviour.MessageTemplate(p) 149 self.presenceBehaviour=self.PresenceBehaviour(self.muc_name,self.name,self.nick,self) 150 self.myAgent.addBehaviour(self.presenceBehaviour, t)
151 152
153 - def testRoomName(self):
154 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 155 iq = Iq(frm=self.muc_name, attrs={"id":ID}) 156 t = Behaviour.MessageTemplate(iq) 157 b=self.TestRoomNameBehaviour(ID,self.muc_name,self.name) 158 self.myAgent.addBehaviour(b, t) 159 b.join() 160 return b.result
161 162 163 164 165 166
167 - class TestRoomNameBehaviour(Behaviour.OneShotBehaviour):
168 - def __init__(self,ID,muc_name,roomname):
169 Behaviour.OneShotBehaviour.__init__(self) 170 self.ID=ID 171 self.result = False 172 self.muc_name=muc_name 173 self.roomname=roomname
174
175 - def _process(self):
176 iq = Iq(to=self.muc_name,typ='get', attrs={"id":self.ID}) 177 query = Protocol('query',xmlns="http://jabber.org/protocol/disco#items") 178 iq.addChild(node=query) 179 self.myAgent.jabber.send(iq) 180 msg = self._receive(True,10) 181 if msg: 182 if query: 183 self.result = True 184 items = msg.getQueryChildren() 185 for item in items: 186 if item.getAttr("jid") == str(self.roomname+"@"+self.muc_name): 187 self.result = False 188 else: 189 self.result = False
190
191 - def testOrganizationName(self):
192 info=self.getInfo() 193 if info: 194 if info["parent"]=="Organization": 195 return True 196 return False
197 198 199 200 201 202 203
204 - def createRoom(self):
205 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 206 p = Presence(frm=self.name+"@"+self.muc_name+"/"+self.nick) 207 t1 = Behaviour.MessageTemplate(p) 208 b=self.CreateRoomBehaviour(ID,self.muc_name,self.name,self.nick,self.contentLanguage,self.type,self.goalList) 209 self.myAgent.addBehaviour(b, t1) 210 b.join() 211 return b.result
212
213 - class CreateRoomBehaviour(Behaviour.OneShotBehaviour):
214 - def __init__(self,ID,muc_name,roomname,nick,contentLanguage,type,goal):
215 Behaviour.OneShotBehaviour.__init__(self) 216 self.result = False 217 self.ID=ID 218 self.nick=nick 219 self.muc_name=muc_name 220 self.name=roomname 221 self.contentLanguage=contentLanguage 222 self.type=type 223 self.goal=goal
224
225 - def _process(self):
226 p = Presence(to=self.name+"@"+self.muc_name+"/"+self.nick) 227 x = Protocol("x", xmlns="http://jabber.org/protocol/muc") 228 p.addChild(node=x) 229 self.myAgent.jabber.send(p) 230 msg=self._receive(True,10) 231 if msg: 232 if msg.getAttr("type")=="error": 233 print "Room creation is restricted" 234 self.result= False 235 return 236 else: 237 self.result= False 238 return 239 template= Iq(frm=self.name+"@"+self.muc_name, attrs={"id":self.ID}) 240 t = Behaviour.MessageTemplate(template) 241 self.setTemplate(t) 242 iq = Iq(to=self.name+"@"+self.muc_name,typ='get', attrs={"id":self.ID}) 243 query = Protocol('query',xmlns="http://jabber.org/protocol/muc#owner") 244 iq.addChild(node=query) 245 self.myAgent.jabber.send(iq) 246 msg = self._receive(True,10) 247 #para descartar los presence anteriores 248 while msg and msg.getName()!="iq": 249 msg = self._receive(True,10) 250 251 #setting room configuration 252 if not msg or msg.getAttr("type")=="error": 253 print "No configuration is possible: " 254 self.result= False 255 return 256 #falta por revisar!!!! 257 iq = Iq(to=self.name+"@"+self.muc_name,typ='set', attrs={"id":self.ID}) 258 query = Protocol('query',xmlns="http://jabber.org/protocol/muc#owner") 259 x=Node(tag="x", attrs={"xmlns":"jabber:x:data"," type":"submit"}) 260 resquery=msg.getQueryChildren()[0] #nos quedamos con el hijo de query 261 if resquery: items = resquery.getTags("field") 262 if resquery==None: 263 print "No configuration is possible" 264 self.result= False 265 for item in items: 266 value=None 267 if item.getAttr("var") == "muc#roomconfig_lang": 268 value=self.contentLanguage 269 if item.getAttr("var") == "muc#roomconfig_roomdesc": 270 value=self.type 271 if item.getAttr("var") == "muc#roomconfig_roomtype": 272 value="Organization" 273 if item.getAttr("var") == "muc#roomconfig_roomname": 274 value=self.name 275 if item.getAttr("var") == "muc#roomconfig_presencebroadcast": 276 value="moderator" 277 if item.getAttr("var") == "muc#roomconfig_persistentroom": 278 value="1" 279 if item.getAttr("var") == "muc#roomconfig_publicroom": 280 value="1" 281 if item.getAttr("var") == "muc#roomconfig_moderatedroom": 282 value="1" 283 if item.getAttr("var") == "muc#roomconfig_membersonly": 284 value="0" 285 if item.getAttr("var") == "muc#roomconfig_passwordprotectedroom": 286 value="0" 287 if item.getAttr("var") == "muc#roomconfig_whois": 288 value="moderators" #como es esto?? 289 ###CAMBIA############################################3 290 if item.getAttr("var") == "muc#roomconfig_changesubject": 291 value="1" 292 if value: 293 node=Node(tag="field", attrs={"var":item.getAttr("var")}) 294 valnode=Node(tag="value") 295 valnode.addData(value) 296 node.addChild(node=valnode) 297 x.addChild(node=node) 298 query.addChild(node=x) 299 iq.addChild(node=query) 300 self.myAgent.jabber.send(iq) 301 msg = self._receive(True,10) 302 if msg and msg.getAttr("type")=="result": #comprobar mejor el mensaje que se devuelve 303 #modifying the Room Subject 304 m = Message(to=self.name+"@"+self.muc_name, typ="groupchat") 305 sub = Node(tag="subject") 306 sub.addData(str(self.goal)) 307 m.addChild(node=sub) 308 self.myAgent.jabber.send(m) 309 self.result= True 310 else: 311 self.result= False
312
313 - def createTeam(self):
314 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 315 p = Presence(frm="Team:"+self.name+"@"+self.muc_name+"/"+self.nick) 316 t1 = Behaviour.MessageTemplate(p) 317 b=self.CreateTeamBehaviour(ID,self.muc_name,self.name,self.nick,self.contentLanguage,self.goalList,self.agentList) 318 self.myAgent.addBehaviour(b, t1) 319 b.join() 320 return b.result
321
322 - class CreateTeamBehaviour(Behaviour.OneShotBehaviour):
323 - def __init__(self,ID,muc_name,roomname,nick,contentLanguage,goal,agentList):
324 Behaviour.OneShotBehaviour.__init__(self) 325 self.result = False 326 self.ID=ID 327 self.nick=nick 328 self.muc_name=muc_name 329 self.name="Team:"+roomname 330 self.parent=roomname 331 self.contentLanguage=contentLanguage 332 self.type="Team" 333 self.goal=goal 334 self.agentList=agentList
335
336 - def _process(self):
337 p = Presence(to=self.name+"@"+self.muc_name+"/"+self.nick) 338 x = Protocol("x", xmlns="http://jabber.org/protocol/muc") 339 p.addChild(node=x) 340 self.myAgent.jabber.send(p) 341 msg=self._receive(True,10) 342 if msg: 343 if msg.getAttr("type")=="error": 344 print "Room creation is restricted" 345 self.result= False 346 return 347 else: 348 self.result= False 349 return 350 template= Iq(frm=self.name+"@"+self.muc_name, attrs={"id":self.ID}) 351 t = Behaviour.MessageTemplate(template) 352 self.setTemplate(t) 353 iq = Iq(to=self.name+"@"+self.muc_name,typ='get', attrs={"id":self.ID}) 354 query = Protocol('query',xmlns="http://jabber.org/protocol/muc#owner") 355 iq.addChild(node=query) 356 self.myAgent.jabber.send(iq) 357 msg = self._receive(True,10) 358 #para descartar los presence anteriores 359 while msg and msg.getName()!="iq": 360 msg = self._receive(True,10) 361 362 #setting room configuration 363 if not msg or msg.getAttr("type")=="error": 364 print "No configuration is possible: " 365 self.result= False 366 return 367 #falta por revisar!!!! 368 iq = Iq(to=self.name+"@"+self.muc_name,typ='set', attrs={"id":self.ID}) 369 query = Protocol('query',xmlns="http://jabber.org/protocol/muc#owner") 370 x=Node(tag="x", attrs={"xmlns":"jabber:x:data"," type":"submit"}) 371 resquery=msg.getQueryChildren()[0] #nos quedamos con el hijo de query 372 if resquery: items = resquery.getTags("field") 373 if resquery==None: 374 print "No configuration is possible" 375 self.result= False 376 for item in items: 377 value=None 378 if item.getAttr("var"): 379 value=item.getAttr("value") #tomamos el valor 380 if item.getAttr("var") == "muc#roomconfig_lang": 381 value=self.contentLanguage 382 if item.getAttr("var") == "muc#roomconfig_roomdesc": 383 value=self.type 384 if item.getAttr("var") == "muc#roomconfig_roomtype": 385 value="Unit:"+self.parent 386 if item.getAttr("var") == "muc#roomconfig_roomname": 387 value=self.name 388 if item.getAttr("var") == "muc#roomconfig_presencebroadcast": 389 value="moderator" 390 if item.getAttr("var") == "muc#roomconfig_persistentroom": 391 value="1" 392 if item.getAttr("var") == "muc#roomconfig_publicroom": 393 value="1" 394 if item.getAttr("var") == "muc#roomconfig_moderatedroom": 395 value="0" 396 if item.getAttr("var") == "muc#roomconfig_membersonly": 397 value="1" 398 if item.getAttr("var") == "muc#roomconfig_passwordprotectedroom": 399 value="0" 400 if item.getAttr("var") == "muc#roomconfig_whois": 401 value="anyone" #como es esto?? 402 if item.getAttr("var") == "muc#roomconfig_changeSubject": 403 value="0" 404 if value: 405 node=Node(tag="field", attrs={"var":item.getAttr("var")}) 406 valnode=Node(tag="value") 407 valnode.addData(value) 408 node.addChild(node=valnode) 409 x.addChild(node=node) 410 query.addChild(node=x) 411 iq.addChild(node=query) 412 self.myAgent.jabber.send(iq) 413 msg = self._receive(True,10) 414 if msg and msg.getAttr("type")=="result": #comprobar mejor el mensaje que se devuelve 415 #añadiendo los members invitacion 416 for agent in self.agentList: 417 iq = Iq(to=self.name+"@"+self.muc_name,typ='set', attrs={"id":self.ID}) 418 query = Protocol('query',xmlns="http://jabber.org/protocol/muc#admin") 419 item= Node(tag="item", attrs={"affiliation":"owner","jid":agent}) 420 query.addChild(node=item) 421 iq.addChild(node=query) 422 self.myAgent.jabber.send(iq) 423 m = Message(to=self.name+"@"+self.muc_name, typ="groupchat") 424 sub = Node(tag="subject") 425 sub.addData(str(self.goal)) 426 m.addChild(node=sub) 427 self.myAgent.jabber.send(m) 428 self.result= True 429 else: 430 self.result= False
431
432 - def checkGoal(self,goalList):
433 #falta por implementar 434 if goalList!=None: 435 return True 436 else: 437 return False
438 439
440 - def checkType(self):
441 types=("Flat","Team","Hierarchy","Bureaucracy","Matrix","Federation","Coalition","Congregation") 442 if self.type in types: 443 return True 444 return False
445
446 - def invite(self,agentList):
447 if self.state=="unavailable": 448 raise Unavailable 449 return 450 for agent in agentList: 451 message = Node(tag="message", attrs={"to":self.name+"@"+self.muc_name}) 452 x=Node(tag="x",attrs={"xmlns":"http://jabber.org/protocol/muc#user"}) 453 y=Node(tag="invite",attrs={"to":agent}) 454 r=Node(tag="reason") 455 r.addData("Inivitation to the Organization "+self.name) 456 y.addChild(node=r) 457 x.addChild(node=y) 458 message.addChild(node=x) 459 self.myAgent.jabber.send(message)
460
461 - def myJoinRoom(self):
462 p = Presence(frm=self.name+"@"+self.muc_name+"/"+self.nick,attrs={"type":"error"}) 463 t1 = Behaviour.MessageTemplate(p) 464 b=self. MyJoinRoomBehaviour(self.muc_name,self.name,self.nick) 465 self.myAgent.addBehaviour(b, t1) 466 b.join() 467 return b.result
468
469 - class MyJoinRoomBehaviour(Behaviour.OneShotBehaviour):
470 - def __init__(self,muc_name,roomname,nick):
471 Behaviour.OneShotBehaviour.__init__(self) 472 self.result = False 473 self.nick=nick 474 self.muc_name=muc_name 475 self.name=roomname
476 477
478 - def _process(self):
479 p = Presence(to=self.name+"@"+self.muc_name+"/"+self.nick) 480 x = Protocol("x", xmlns="http://jabber.org/protocol/muc") 481 p.addChild(node=x) 482 self.myAgent.jabber.send(p) 483 msg=self._receive(True,10) 484 if msg: 485 error=msg.getTag("error") 486 if error.getAttr("code")=="401": 487 raise PaswordNeeded 488 if error.getAttr("code")=="407": 489 raise MembersOnly 490 if error.getAttr("code")=="403": 491 raise BanedUser 492 if error.getAttr("code")=="409": 493 raise NickNameConflict 494 if error.getAttr("code")=="503": 495 raise MaximumUsers 496 if error.getAttr("code")=="404": 497 raise LockedOrganization 498 self.result = False 499 return 500 self.result = True
501
502 - def getRegistrationForm(self,unitName):
503 """ 504 Returns a dataform with all requested information for joining 505 """ 506 if self.state=="unavailable": 507 raise Unavailable 508 return 509 if unitName not in self.getUnitList(): 510 raise NotValidUnit 511 return 512 513 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 514 p = Iq(frm=unitName+"@"+self.muc_name, attrs={"id":ID}) 515 t1 = Behaviour.MessageTemplate(p) 516 b=self.GetRegistrationFormBehaviour(self.muc_name,ID,unitName) 517 self.myAgent.addBehaviour(b, t1) 518 b.join() 519 return b.result
520
521 - class GetRegistrationFormBehaviour(Behaviour.OneShotBehaviour):
522 - def __init__(self,muc_name,ID,unitName):
523 Behaviour.OneShotBehaviour.__init__(self) 524 self.ID=ID 525 self.muc_name=muc_name 526 self.unitName=unitName 527 self.result=None
528
529 - def _process(self):
530 iq = Iq(to=self.unitName+"@"+self.muc_name,typ='get', attrs={"id":self.ID}) 531 query = Protocol('query',xmlns="jabber:iq:register") 532 iq.addChild(node=query) 533 self.myAgent.jabber.send(iq) 534 msg = self._receive(True,10) 535 if msg: 536 if msg.getAttr("type")!="result": 537 error=msg.getError() 538 print error 539 return 540 else: 541 if msg.getTag("register")!=None: 542 print "The agent has yet registered in the Unit "+self.unitName 543 else: 544 self.result=msg.getChildren()[0]
545 #cambiar
546 - def sendRegistrationForm(self,unitName,dataForm):
547 """ 548 Sends a dataform for a specific unit. If valid, agent is registered and allowed to join 549 """ 550 if self.state=="unavailable": 551 raise Unavailable 552 return 553 #comprobando que es una unidad de la organizacion 554 if unitName not in self.getUnitList(): 555 raise NotValidUnit 556 return 557 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 558 p = Iq(frm=unitName+"@"+self.muc_name, attrs={"id":ID}) 559 t1 = Behaviour.MessageTemplate(p) 560 b=self.GetRegistrationFormBehaviour(self.muc_name,self.name,ID,unitName,dataForm) 561 self.myAgent.addBehaviour(b, t1) 562 b.join() 563 return b.result
564
565 - class SendRegistrationFormBehaviour(Behaviour.OneShotBehaviour):
566 - def __init__(self,muc_name,roomname,ID,unitName,dataForm):
567 Behaviour.OneShotBehaviour.__init__(self) 568 self.ID=ID 569 self.muc_name=muc_name 570 self.name=roomname 571 self.unitName=unitName 572 self.dataForm=dataForm 573 self.result=None
574
575 - def _process(self):
576 iq = Iq(to=self.unitName+"@"+self.muc_name,typ='set', attrs={"id":self.ID}) 577 query = Protocol('query',xmlns="jabber:iq:register") 578 query.addNode(node=self.dataForm) 579 iq.addChild(node=query) 580 self.myAgent.jabber.send(iq) 581 msg = self._receive(True,10) 582 if msg: 583 if msg.getAttr("type")!="result": 584 error=msg.getTag("error") 585 if error.getAttr("code")=="409": 586 print "Error: Conflict, this nickname is already reserved" 587 if error.getAttr("code")=="503": 588 print "Error: Resgistration Not Supported" 589 if error.getAttr("code")=="400": 590 print "Error: Bad Request"
591
592 - def getUnitList(self):
593 """ 594 Returns a dataform with all requested information for joining 595 """ 596 if self.state=="unavailable": 597 raise Unavailable 598 return 599 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 600 iq = Iq(frm=self.muc_name, attrs={"id":ID}) 601 t = Behaviour.MessageTemplate(iq) 602 b=self.GetUnitListBehaviour(ID,self.muc_name,self.name) 603 self.myAgent.addBehaviour(b, t) 604 b.join() 605 return b.result
606 607 608 609 610
611 - class GetUnitListBehaviour(Behaviour.OneShotBehaviour):
612 - def __init__(self,ID,muc_name,roomname):
613 Behaviour.OneShotBehaviour.__init__(self) 614 self.ID=ID 615 self.result = [] 616 self.muc_name=muc_name 617 self.roomname=roomname
618
619 - def _process(self):
620 self.result=[] 621 iq = Iq(to=self.muc_name,typ='get', attrs={"id":self.ID}) 622 query = Protocol('query',xmlns="http://jabber.org/protocol/disco#items") 623 iq.addChild(node=query) 624 self.myAgent.jabber.send(iq) 625 msg = self._receive(True,10) 626 if msg: 627 if query: 628 items = msg.getQueryChildren() 629 for item in items: 630 if item.getAttr("jid"): 631 iq = Iq(to=item.getAttr("jid"),typ='get', attrs={"id":self.ID}) 632 query = Protocol('query',xmlns="http://jabber.org/protocol/disco#info") 633 iq.addChild(node=query) 634 name=str(item.getAttr("name")) 635 self.myAgent.jabber.send(iq) 636 template = Iq(frm=item.getAttr("jid"),typ='result', attrs={"id":self.ID}) 637 t = Behaviour.MessageTemplate(template) 638 self.setTemplate(t) 639 msg = self._receive(True,10) 640 if msg: 641 query = msg.getTag("query") 642 if query: 643 x = query.getTag("x") 644 if x: 645 items =x.getChildren() 646 for item in items: 647 value=None 648 if item.getAttr("var")=="muc#roominfo_type": 649 if item.getTags("value"): 650 value=item.getTags("value")[0].getData() 651 if value=="Unit:"+self.roomname: 652 self.result.append(name)
653
654 - def getUnitInfo(self,unitname):
655 if self.state=="unavailable": 656 raise Unavailable 657 return 658 if unitname not in selg.getUnitList(): 659 raise NotValidUnit 660 return 661 662 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 663 p = Iq(frm=self.name+"@"+self.muc_name,typ='result', attrs={"id":ID}) 664 t1 = Behaviour.MessageTemplate(p) 665 b=self.GetUnitInfoBehaviour(self.muc_name,unitname,ID) 666 self.myAgent.addBehaviour(b, t1) 667 b.join() 668 return b.result
669 670
671 - class GetUnitInfoBehaviour(Behaviour.OneShotBehaviour):
672 - def __init__(self,muc_name,roomname,ID):
673 Behaviour.OneShotBehaviour.__init__(self) 674 self.ID=ID 675 self.muc_name=muc_name 676 self.name=roomname 677 self.result=None
678
679 - def _process(self):
680 info={} 681 iq = Iq(to=self.name+"@"+self.muc_name,typ='get', attrs={"id":self.ID}) 682 query = Protocol('query',xmlns="http://jabber.org/protocol/disco#info") 683 iq.addChild(node=query) 684 self.myAgent.jabber.send(iq) 685 msg=self._receive(True,10) 686 if msg: 687 query = msg.getTag("query") 688 if query: 689 x = query.getTag("x") 690 if x: 691 items =x.getChildren() 692 for item in items: 693 if item.getAttr("var")=="muc#roominfo_description": 694 if item.getTags("value"): 695 info["type"]=item.getTags("value")[0].getData() 696 if item.getAttr("var")=="muc#roominfo_subject": 697 if item.getTags("value"): 698 info["goal"]=item.getTags("value")[0].getData() 699 if item.getAttr("var")=="muc#roominfo_type": 700 if item.getTags("value"): 701 info["contentLanguage"]=item.getTags("value")[0].getData() 702 self.result=info
703
704 - def leave(self):
705 """ 706 Agent leaves and it is removed from the member list 707 """ 708 if self.state=="unavailable": 709 raise Unavailable 710 return 711 owners=self.getOwnerList() 712 if self.owner!=True: 713 units=self.getUnitList() 714 units.append(self.name) 715 for u in units: 716 p = Presence(to=u+"@"+self.muc_name+"/"+self.nick,typ="unavailable") 717 self.myAgent.jabber.send(p) 718 self.state="unavailable" 719 self.myAgent.removeBehaviour(self.presenceBehaviour) 720 self.myAgent.removeBehaviour(self) 721 else: 722 raise LastOwner
723
724 - def destroy(self):
725 #deberia implicar dejar tambien las salas de la organizacion??? 726 """ 727 Organization owner destroys the organization 728 """ 729 if self.state=="unavailable": 730 raise Unavailable 731 return 732 units=self.getUnitLis 733 units.append(self.name) 734 if self.owner==True: 735 for u in units: 736 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 737 p = Iq(frm=self.name+"@"+self.muc_name, attrs={"id":ID}) 738 t1 = Behaviour.MessageTemplate(p) 739 b=self.DestroyBehaviour(self.muc_name,u,ID) 740 self.myAgent.addBehaviour(b, t1) 741 b.join() 742 #destruir los comportamientos 743 self.myAgent.removeBehaviour(self.presenceBehaviour) 744 self.myAgent.removeBehaviour(self) 745 self.state="unavailable" 746 #destruir las unidades 747 else: 748 raise DestroyError
749 750
751 - class DestroyBehaviour(Behaviour.OneShotBehaviour):
752 - def __init__(self,muc_name,roomname,ID):
753 Behaviour.OneShotBehaviour.__init__(self) 754 self.ID=ID 755 self.muc_name=muc_name 756 self.name=roomname 757 self.result=False
758
759 - def _process(self):
760 iq = Iq(to=self.name+"@"+self.muc_name,typ='set', attrs={"id":self.ID}) 761 query = Protocol('query',xmlns="http://jabber.org/protocol/muc#owner") 762 item= Node(tag="destroy") 763 query.addChild(node=item) 764 iq.addChild(node=query) 765 self.myAgent.jabber.send(iq) 766 msg = self._receive(True,10) 767 if msg: 768 if msg.getAttr("type")!="result": 769 #print "Error: This agent is not a owner of the organization" 770 print msg.getError() 771 return 772 else: 773 self.result=True
774 775
776 - def joinUnit(self,unit):
777 if self.state=="unavailable": 778 raise Unavailable 779 return 780 if unit.name not in self.getUnitList(): 781 raise NotValidUnit 782 return 783 if self.type=="Federation": 784 if self.checkIsMember(unit.name): 785 raise MemberOfFederation 786 return 787 unit.create=False 788 self.myAgent.addBehaviour(unit)
789 790
791 - def checkIsMember(self,unit):
792 ismember=False 793 units=self.getUnitList() 794 if unit in units: 795 units.remove(unit) 796 team="Team:"+self.name 797 if team in units: 798 units.remove(team) 799 jid=str(self.myAgent.JID) 800 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 801 p = Iq(frm=jid, attrs={"id":ID}) 802 t1 = Behaviour.MessageTemplate(p) 803 b=self.IsMemberBehaviour(jid,ID) 804 self.myAgent.addBehaviour(b, t1) 805 b.join() 806 b.result 807 for u in units: 808 if u in b.result: 809 ismember=True 810 return ismember
811
812 - class IsMemberBehaviour(Behaviour.OneShotBehaviour):
813 - def __init__(self,jid,ID):
814 Behaviour.OneShotBehaviour.__init__(self) 815 self.ID=ID 816 self.jid=jid 817 self.result=[]
818
819 - def _process(self):
820 iq = Iq(to=self.jid,typ='get', attrs={"id":self.ID}) 821 query = Protocol('query',xmlns="http://jabber.org/protocol/disco#items",attrs={"node":"http://jabber.org/protocol/muc#rooms"}) 822 iq.addChild(node=query) 823 self.myAgent.jabber.send(iq) 824 msg = self._receive(True,10) 825 if msg: 826 items =msg.getChildren() 827 for item in items: 828 if item.getAttr("jid"): 829 sala=str(item.getAttr("jid")) 830 self.result.append(sala.split('@')[0])
831 832 833 834 835
836 - def addUnit(self,unit):
837 """ 838 Creates a new unit inside an organization 839 """ 840 if self.state=="unavailable": 841 raise Unavailable 842 return 843 if self.checkTypes(self.type,unit.type): 844 #un sitwch para aquellas organizaciones donde todos puedan crear unidades 845 if self.type!="Matrix" and self.type!="Federation": 846 if self.checkOwnerAdmin(self.myAgent.JID): 847 unit.create=True 848 unit.parent=self.name 849 unit.parent_type=self.type 850 if self.orgOwner==None: 851 self.orgOwner=self.getOwnerList()[0] 852 unit.orgOwner=self.orgOwner 853 self.myAgent.addBehaviour(unit) 854 else: 855 raise NotCreatePermision 856 elif self.checkSupervisor(self.myAgent.JID): 857 unit.create=True 858 unit.parent=self.name 859 unit.parent_type=self.type 860 if self.orgOwner==None: 861 self.orgOwner=self.getOwnerList()[0] 862 unit.orgOwner=self.orgOwner 863 self.myAgent.addBehaviour(unit) 864 else: 865 raise NotSupervisor 866 else: 867 raise NotValidType
868
869 - def checkTypes(self,orgType,unitType):
870 if orgType=="Flat": 871 return True 872 if orgType=="Team" and unitType=="Team": 873 return True 874 if orgType=="Hierarchy" and unitType=="Hierarchy": 875 return True 876 if orgType=="Bureaucracy" and unitType=="Hierarchy": 877 return True 878 if orgType=="Matrix" and unitType=="Hierarchy": 879 return True 880 if orgType=="Federation" and unitType=="Hierarchy": 881 return True 882 if orgType=="Coalition" and unitType=="Team": 883 return True 884 if orgType=="Congregation" and unitType=="Hierarchy": 885 return True 886 if orgType=="Congregation" and unitType=="Team": 887 return True 888 if orgType=="Congregation" and unitType=="Flat": 889 return True 890 return False
891 892 893 894
895 - def getSupervisorList(self):
896 list=[] 897 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 898 p = Iq(frm="Team:"+self.name+"@"+self.muc_name, attrs={"id":ID}) 899 t1 = Behaviour.MessageTemplate(p) 900 b=self.GetMemberListBehaviour(self.muc_name,"Team:"+self.name,ID) 901 self.myAgent.addBehaviour(b, t1) 902 b.join() 903 member=b.result 904 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 905 p = Iq(frm="Team:"+self.name+"@"+self.muc_name, attrs={"id":ID}) 906 t = Behaviour.MessageTemplate(p) 907 b=self.GetOwnerListBehaviour(self.muc_name,"Team:"+self.name,ID) 908 self.myAgent.addBehaviour(b, t) 909 b.join() 910 owner=b.result 911 ID="".join([string.ascii_letters[int(random.randint(0,len(string.ascii_letters)-1))] for a in range(5)]) 912 p = Iq(frm=self.name+"@"+self.muc_name, attrs={"id":ID}) 913 t = Behaviour.MessageTemplate(p) 914 b=self.GetAdminListBehaviour(self.muc_name,"Team:"+self.name,ID) 915 self.myAgent.addBehaviour(b, t) 916 b.join() 917 admin=b.result 918 for i in owner: 919 list.append(i) 920 for i in member: 921 list.append(i) 922 for i in admin: 923 list.append(i) 924 return list
925
926 - def sendMessage(self,message):
928
929 - def sendPrivateMessage(self,recName,message):
931
932 - def giveVoice(self,nickname):
934
935 - def revokeVoice(self,nickname):
937
938 - def _process(self):
939 pass
940