Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

414 lines
9.7 KiB

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. * Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  5. *
  6. * Mumble-Django is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This package is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. """
  16. # Set this to the same path you used in settings.py, or None for auto-detection.
  17. MUMBLE_DJANGO_ROOT = None;
  18. ### DO NOT CHANGE ANYTHING BELOW THIS LINE ###
  19. import os, sys
  20. from os.path import join, dirname, abspath, exists
  21. # Path auto-detection
  22. if not MUMBLE_DJANGO_ROOT or not exists( MUMBLE_DJANGO_ROOT ):
  23. MUMBLE_DJANGO_ROOT = dirname(abspath(__file__));
  24. # environment variables
  25. sys.path.append( MUMBLE_DJANGO_ROOT )
  26. sys.path.append( join( MUMBLE_DJANGO_ROOT, 'pyweb' ) )
  27. os.environ['DJANGO_SETTINGS_MODULE'] = 'pyweb.settings'
  28. # If you get an error about Python not being able to write to the Python
  29. # egg cache, the egg cache path might be set awkwardly. This should not
  30. # happen under normal circumstances, but every now and then, it does.
  31. # Uncomment this line to point the egg cache to /tmp.
  32. #os.environ['PYTHON_EGG_CACHE'] = '/tmp/pyeggs'
  33. import locale
  34. import curses
  35. from django.db.models.fields.related import ForeignKey
  36. from mumble.models import *
  37. locale.setlocale(locale.LC_ALL, '')
  38. def getNum( prompt, **kwargs ):
  39. id = None;
  40. while type(id) != int:
  41. print
  42. try:
  43. id = raw_input( "%s >>> " % prompt ).strip();
  44. if id == 'q':
  45. return None;
  46. elif id in kwargs:
  47. return kwargs[id];
  48. id = int( id );
  49. except Exception, instance:
  50. print "Error reading input. Did you type a number?";
  51. print instance;
  52. return id;
  53. def util_editModel( model, blacklist = None ):
  54. while True:
  55. print "Current settings"
  56. print "================"
  57. for field in model._meta.fields:
  58. if blacklist and field.name in blacklist:
  59. continue;
  60. print "#%-5d %-30s %s" % ( model._meta.fields.index( field ), field.verbose_name, getattr( model, field.name ) );
  61. print "================"
  62. print "Enter the index of the parameter you would like to change,"
  63. print "or q to return."
  64. idx = getNum( "Index" );
  65. if idx is None:
  66. save = raw_input( "save? Y/n >>> " );
  67. if not save or save.lower() == 'y':
  68. print "saving changes.";
  69. model.save();
  70. else:
  71. print "NOT saving changes."
  72. return;
  73. field = model._meta.fields[idx];
  74. if blacklist and field.name in blacklist:
  75. print "This field can not be changed.";
  76. elif isinstance( field, ForeignKey ):
  77. print "This is a ForeignKey.";
  78. print field.rel.to.objects.all();
  79. else:
  80. value = None;
  81. while value is None:
  82. print
  83. try:
  84. value = field.to_python( raw_input( "%s >>> " % field.name ).strip() );
  85. except Exception, instance:
  86. print instance;
  87. setattr( model, field.name, value );
  88. def act_serverDetails( server ):
  89. "View or edit server settings."
  90. util_editModel( server, ( "id", "sslcrt", "sslkey" ) );
  91. def act_registeredUsers( server ):
  92. "View or edit user registrations."
  93. mumbleusers_list = server.mumbleuser_set.all();
  94. print "Currently registered accounts";
  95. print "=============================";
  96. for mu in mumbleusers_list:
  97. if mu.owner is not None:
  98. print "#%-5d %-20s Owner: %-20s Admin: %s" % ( mu.id, mu.name, mu.owner.username, mu.getAdmin() );
  99. else:
  100. print "#%-5d %-20s" % ( mu.id, mu.name );
  101. print "=============================";
  102. print "Enter the ID of the account you would like to change, n to create a new one, or q to return."
  103. while True:
  104. idx = getNum( "ID", n=-1 );
  105. if idx is None:
  106. return;
  107. if idx == -1:
  108. mu = MumbleUser();
  109. mu.server = server;
  110. else:
  111. mu = mumbleusers_list.get( id=idx );
  112. util_editModel( mu, ( "id", "mumbleid", "server" ) );
  113. def act_listChannels( server ):
  114. "Display a channel tree."
  115. def printItem( item, level ):
  116. print "%s%s" % ( " "*level, item );
  117. server.rootchan.visit( printItem );
  118. def act_chanDetails( server ):
  119. "Display detailed information about one specific channel."
  120. print "Please choose the channel by entering the according ID (the number in parentheses)."
  121. act_listChannels( server );
  122. id = getNum( "ID" );
  123. if id is None: return;
  124. print "Channel name: %s" % server.channels[id].name
  125. print "Channel ID: %d" % server.channels[id].chanid
  126. print "Users online: %d" % len( server.channels[id].players )
  127. print "Linked chans: %d" % len( server.channels[id].linked )
  128. def cli_chooseServer():
  129. mumble_all = Mumble.objects.all().order_by( 'name', 'id' );
  130. print "Please choose a Server instance by typing the corresponding ID.\n";
  131. for mm in mumble_all:
  132. print "#%d\t%s" % ( mm.id, mm.name );
  133. print "n: Create new instance";
  134. print "q: Exit";
  135. id = getNum( "ID", n = -1 );
  136. if id is None:
  137. return;
  138. elif id == -1:
  139. return Mumble();
  140. return Mumble.objects.get( id=id );
  141. def cli_chooseAction( server ):
  142. actions = {
  143. "LISTCHAN": act_listChannels,
  144. "CHANINFO": act_chanDetails,
  145. "EDITSERVER": act_serverDetails,
  146. "EDITUSERS": act_registeredUsers,
  147. };
  148. while True:
  149. print "What do you want to do?"
  150. keys = actions.keys();
  151. for act in keys:
  152. print "#%-5d %-20s %s" % ( keys.index(act), act, actions[act].__doc__ );
  153. print "q: Return to server selection";
  154. idx = getNum( "Index" );
  155. if idx is None:
  156. return;
  157. # call action function
  158. func = actions[ keys[idx] ]
  159. func( server );
  160. print
  161. def oldmain():
  162. print
  163. while True:
  164. mumble = cli_chooseServer();
  165. if mumble is None:
  166. print "Bye.";
  167. return;
  168. print "Selected %s." % mumble;
  169. print
  170. cli_chooseAction( mumble );
  171. print
  172. class BaseWindow( object ):
  173. tabName = "tehBasez";
  174. def __init__( self, parentwin, mumble, pos_x, pos_y ):
  175. self.pos = ( pos_x, pos_y );
  176. self.win = parentwin.subwin( pos_y, pos_x );
  177. self.mm = mumble;
  178. self.win.keypad(1);
  179. def draw( self ):
  180. self.win.addstr( 1, 1, self.tabName );
  181. def border( self ):
  182. self.win.border();
  183. class WndChannels( BaseWindow ):
  184. tabName = 'Channels';
  185. def printItem( self, item, level ):
  186. str = "";
  187. if item.is_server or item.is_channel:
  188. str = "%s (Channel)" % item.name;
  189. else:
  190. str = "%s (Player)" % item.name
  191. self.win.addstr( self.curr_y, 4*level+1, str.encode(locale.getpreferredencoding()) )
  192. self.curr_y += 1;
  193. def draw( self ):
  194. self.curr_y = 1;
  195. self.mm.rootchan.visit( self.printItem );
  196. class WndSettings( BaseWindow ):
  197. tabName = 'Server settings';
  198. class WndUsers( BaseWindow ):
  199. tabName = 'Registered users';
  200. class MumbleForm( object ):
  201. def __init__( self, parentwin, mumble, pos_x, pos_y ):
  202. self.pos = ( pos_x, pos_y );
  203. self.win = parentwin.subwin( pos_y, pos_x );
  204. self.mm = mumble;
  205. self.win.keypad(1);
  206. self.windows = (
  207. WndChannels( self.win, mumble, pos_x + 2, pos_y + 2 ),
  208. WndSettings( self.win, mumble, pos_x + 2, pos_y + 2 ),
  209. WndUsers( self.win, mumble, pos_x + 2, pos_y + 2 ),
  210. );
  211. self.curridx = 0;
  212. self.currmax = len( self.windows ) - 1;
  213. currwin = property( lambda self: self.windows[self.curridx], None );
  214. def mvwin( self, pos_x, pos_y ):
  215. self.win.mvwin( pos_y, pos_x );
  216. def mvdefault( self ):
  217. self.win.mvwin( self.pos[1], self.pos[0] );
  218. def draw( self ):
  219. self.win.addstr( 0, 0, self.mm.name );
  220. def drawTabs( self ):
  221. first = True;
  222. for subwin in self.windows:
  223. flags = 0;
  224. if subwin is self.currwin: flags |= curses.A_STANDOUT;
  225. if first:
  226. self.win.addstr( 1, 2, "%-20s" % subwin.tabName, flags );
  227. first = False;
  228. else:
  229. self.win.addstr( "%-20s" % subwin.tabName, flags );
  230. def enter( self ):
  231. self.drawTabs();
  232. self.currwin.draw();
  233. self.currwin.border();
  234. while( True ):
  235. key = self.win.getch();
  236. if key == curses.KEY_LEFT and self.curridx > 0:
  237. self.curridx -= 1;
  238. elif key == curses.KEY_RIGHT and self.curridx < self.currmax:
  239. self.curridx += 1;
  240. elif key in ( ord('q'), ord('Q') ):
  241. return;
  242. self.win.clear();
  243. self.draw();
  244. self.drawTabs();
  245. self.currwin.draw();
  246. self.currwin.border();
  247. self.win.refresh();
  248. def main( stdscr ):
  249. first_y = 3;
  250. curr_y = first_y;
  251. mumbles = list();
  252. for mm in Mumble.objects.all().order_by( "name", "id" ):
  253. mwin = MumbleForm( stdscr, mm, pos_x=5, pos_y=curr_y );
  254. mumbles.append( mwin );
  255. mwin.draw();
  256. curr_y += 1;
  257. selectedIdx = 0;
  258. selectedMax = len(mumbles) - 1;
  259. while( True ):
  260. selectedObj = mumbles[selectedIdx];
  261. # Draw selection marker
  262. stdscr.addstr( first_y + selectedIdx, 3, '*' );
  263. stdscr.refresh();
  264. key = stdscr.getch();
  265. if key == curses.KEY_UP:
  266. stdscr.addstr( first_y + selectedIdx, 3, ' ' );
  267. selectedIdx -= 1;
  268. elif key == curses.KEY_DOWN:
  269. stdscr.addstr( first_y + selectedIdx, 3, ' ' );
  270. selectedIdx += 1;
  271. elif key in ( curses.KEY_ENTER, ord('\n') ):
  272. stdscr.clear();
  273. selectedObj.mvwin( 5, first_y );
  274. selectedObj.draw();
  275. stdscr.refresh();
  276. selectedObj.enter();
  277. stdscr.clear();
  278. selectedObj.mvdefault();
  279. for mwin in mumbles:
  280. mwin.draw();
  281. elif key in ( ord('q'), ord('Q') ):
  282. return;
  283. if selectedIdx < 0: selectedIdx = 0;
  284. elif selectedIdx > selectedMax: selectedIdx = selectedMax;
  285. if __name__ == '__main__':
  286. #parser = OptionParser();
  287. #parser.add_option( "-v", "--verbose", help="verbose output messages", default=False, action="store_true" );
  288. #parser.add_option( "-n", "--num", help="size of the Matrix", default=4, type = 'int' );
  289. #parser.add_option( "-s", "--sure", help="don't prompt if num >= 10", default=False, action="store_true" );
  290. #options, args = parser.parse_args();
  291. curses.wrapper( main );