Changeset 2912 for pjproject/trunk
- Timestamp:
- Aug 24, 2009 11:56:13 AM (15 years ago)
- Location:
- pjproject/trunk/pjsip-apps/src/confbot
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/confbot/confbot.py
r2911 r2912 82 82 self.member.on_typing(is_typing, buddy=self.buddy) 83 83 84 85 86 87 ############################################################################## 88 # 89 # 84 90 # This class represents individual room member (either/both chat and voice conf) 91 # 92 # 85 93 class Member: 86 94 def __init__(self, bot, uri): … … 93 101 self.in_voice = False 94 102 self.im_error = False 103 self.html = False 95 104 96 105 def __str__(self): … … 103 112 str = str + " [" 104 113 if (self.in_voice): 105 str = str + " incall"114 str = str + " voice" 106 115 if (self.in_chat): 107 str = str + " inchat " 116 str = str + " chat" 117 if (self.html): 118 str = str + " html" 119 else: 120 str = str + " plain" 121 108 122 if (self.im_error): 109 str = str + " im error"123 str = str + " im_error" 110 124 str = str + "]" 111 125 return str … … 134 148 self.bot.DEBUG("send_pager() to " + self.uri) 135 149 if self.in_chat and not self.im_error and self.buddy: 150 if self.html: 151 #This will make us receive html! 152 #mime = "text/html" 153 body = body.replace("<", "<") 154 body = body.replace(">", ">") 155 body = body.replace('"', """) 156 body = body.replace("\n", "<BR>\n") 136 157 self.buddy.send_pager(body, content_type=mime) 137 158 self.bot.DEBUG("..sent\n") … … 241 262 self.im_error = (code/100 != 2) 242 263 264 265 266 ############################################################################## 267 # 268 # 243 269 # The Bot instance (singleton) 270 # 271 # 244 272 class Bot(pj.AccountCallback): 245 273 def __init__(self): … … 249 277 self.calls = [] 250 278 self.members = {} 279 self.cfg = None 251 280 252 281 def DEBUG(self, msg, level=TRACE): … … 257 286 --h[elp] Display this help screen 258 287 --j[oin] Join the chat room 288 --html on|off Set to receive HTML or plain text 259 289 260 290 Participant commands: … … 279 309 msg = "" 280 310 for uri, m in self.members.iteritems(): 281 msg = msg + str(m) 311 msg = msg + str(m) + "\n" 282 312 return msg 283 313 … … 296 326 def main(self, cfg_file): 297 327 try: 298 cfg = __import__(cfg_file)328 cfg = self.cfg = __import__(cfg_file) 299 329 300 330 self.lib.init(ua_cfg=cfg.ua_cfg, log_cfg=cfg.log_cfg, media_cfg=cfg.media_cfg) … … 364 394 del member 365 395 396 def handle_admin_cmd(self, member, body): 397 if member and self.cfg.admins and not member.uri in self.cfg.admins: 398 member.send_pager("You are not admin") 399 return 400 args = body.split() 401 msg = "" 402 403 if len(args)==1: 404 args.append(" ") 405 406 if args[1]=="list": 407 if not self.cfg.admins: 408 msg = "Everyone is admin!" 409 else: 410 msg = str(self.cfg.admins) 411 elif args[1]=="add": 412 if len(args)!=3: 413 msg = "Usage: add <URI>" 414 else: 415 self.cfg.admins.append(args[2]) 416 msg = args[2] + " added as admin" 417 elif args[1]=="del": 418 if len(args)!=3: 419 msg = "Usage: del <URI>" 420 elif args[2] not in self.cfg.admins: 421 msg = args[2] + " is not admin" 422 else: 423 self.cfg.admins.remove(args[2]) 424 msg = args[2] + " has been removed from admins" 425 elif args[1]=="rr": 426 msg = "Reregistering.." 427 self.acc.set_registration(True) 428 elif args[1]=="call": 429 if len(args)!=3: 430 msg = "Usage: call <URI>" 431 else: 432 uri = args[2] 433 try: 434 call = self.acc.make_call(uri) 435 except pj.Error, e: 436 msg = "Error: " + str(e) 437 call = None 438 439 if call: 440 if not uri in self.members: 441 m = Member(self, uri) 442 self.members[m.uri] = m 443 else: 444 m = self.members[uri] 445 msg = "Adding " + m.uri + " to voice conference.." 446 m.join_call(call) 447 elif args[1]=="dc" or args[1]=="hold" or args[1]=="update" or args[1]=="reinvite": 448 if len(args)!=3: 449 msg = "Usage: " + args[1] + " <URI>" 450 else: 451 uri = args[2] 452 if not uri in self.members: 453 msg = "Member not found/URI doesn't match (note: case matters!)" 454 else: 455 m = self.members[uri] 456 if m.call: 457 if args[1]=="dc": 458 msg = "Disconnecting.." 459 m.call.hangup(603, "You're disconnected by admin") 460 elif args[1]=="hold": 461 msg = "Holding the call" 462 m.call.hold() 463 elif args[1]=="update": 464 msg = "Sending UPDATE" 465 m.call.update() 466 elif args[1]=="reinvite": 467 msg = "Sending re-INVITE" 468 m.call.reinvite() 469 else: 470 msg = "He is not in call" 471 else: 472 msg = "Unknown admin command " + body 473 474 #print "msg is '%(msg)s'" % {'msg': msg} 475 476 if True: 477 if member: 478 member.send_pager(msg) 479 else: 480 print msg 481 366 482 def handle_cmd(self, member, from_uri, body): 367 483 body = body.strip(" \t\r\n") 368 484 msg = "" 369 485 handled = True 370 if body=="--l ist" or body=="--l":486 if body=="--l" or body=="--list": 371 487 msg = self.listmembers() 372 488 if msg=="": 373 489 msg = "Nobody is here" 374 elif body =="--show" or body=="--s":490 elif body[0:3]=="--s": 375 491 msg = self.showsettings() 376 elif body=="--help" or body=="--h": 492 elif body[0:6]=="--html" and member: 493 if body[8:11]=="off": 494 member.html = False 495 else: 496 member.html = True 497 elif body=="--h" or body=="--help": 377 498 msg = self.helpstring() 378 499 elif body=="--leave": … … 381 502 else: 382 503 member.buddy.unsubscribe() 383 elif body =="--join" or body=="--j":504 elif body[0:3]=="--j": 384 505 if not from_uri in self.members: 385 506 m = Member(self, from_uri) … … 391 512 self.DEBUG("Adding " + m.uri + " to chatroom\n") 392 513 m.join_chat() 514 elif body[0:3]=="--a": 515 self.handle_admin_cmd(member, body) 516 handled = True 393 517 else: 394 518 handled = False … … 423 547 self.DEBUG("Registration state: %(code)d/%(reason)s\n" % \ 424 548 {'code': ai.reg_status, 'reason': ai.reg_reason}, INFO) 425 pass 549 if ai.reg_status/100==2 and ai.reg_expires > 0: 550 self.acc.set_basic_status(True) 426 551 427 552 def on_pager(self, from_uri, contact, mime_type, body): … … 437 562 438 563 564 565 566 ############################################################################## 567 # 439 568 # 440 569 # main() 570 # 441 571 # 442 572 if __name__ == "__main__": -
pjproject/trunk/pjsip-apps/src/confbot/config.py
r2911 r2912 5 5 import pjsua as pj 6 6 7 # Set of admins. If empty then everyone is admin! 8 admins = set([]) 9 7 10 # acc_cfg holds the account config (set it to None to disable account) 11 acc_cfg = None 8 12 acc_cfg = pj.AccountConfig() 9 acc_cfg.id = "sip:102@pjsip.org" 10 acc_cfg.reg_uri = "sip:pjsip.org" 11 acc_cfg.proxy = [ "sip:pjsip.org;lr;transport=tcp" ] 12 acc_cfg.auth_cred = [ pj.AuthCred("*", "102", "pw102") ] 13 acc_cfg.publish_enabled = True 14 acc_cfg.require_timer = True 13 if acc_cfg: 14 acc_cfg.id = "sip:bot@pjsip.org" 15 acc_cfg.reg_uri = "sip:pjsip.org" 16 acc_cfg.proxy = [ "sip:pjsip.org;lr;transport=tcp" ] 17 acc_cfg.auth_cred = [ pj.AuthCred("*", "bot", "secretpass") ] 18 acc_cfg.publish_enabled = True 19 #acc_cfg.require_timer = True 15 20 16 21 # Transport configs (set them to None to disable the transport)
Note: See TracChangeset
for help on using the changeset viewer.