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.

452 lines
11 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. from mumble.forms import *
  38. locale.setlocale(locale.LC_ALL, '')
  39. def getNum( prompt, **kwargs ):
  40. id = None;
  41. while type(id) != int:
  42. print
  43. try:
  44. id = raw_input( "%s >>> " % prompt ).strip();
  45. if id == 'q':
  46. return None;
  47. elif id in kwargs:
  48. return kwargs[id];
  49. id = int( id );
  50. except Exception, instance:
  51. print "Error reading input. Did you type a number?";
  52. print instance;
  53. return id;
  54. def util_editModel( model, blacklist = None ):
  55. while True:
  56. print "Current settings"
  57. print "================"
  58. for field in model._meta.fields:
  59. if blacklist and field.name in blacklist:
  60. continue;
  61. print "#%-5d %-30s %s" % ( model._meta.fields.index( field ), field.verbose_name, getattr( model, field.name ) );
  62. print "================"
  63. print "Enter the index of the parameter you would like to change,"
  64. print "or q to return."
  65. idx = getNum( "Index" );
  66. if idx is None:
  67. save = raw_input( "save? Y/n >>> " );
  68. if not save or save.lower() == 'y':
  69. print "saving changes.";
  70. model.save();
  71. else:
  72. print "NOT saving changes."
  73. return;
  74. field = model._meta.fields[idx];
  75. if blacklist and field.name in blacklist:
  76. print "This field can not be changed.";
  77. elif isinstance( field, ForeignKey ):
  78. print "This is a ForeignKey.";
  79. print field.rel.to.objects.all();
  80. else:
  81. value = None;
  82. while value is None:
  83. print
  84. try:
  85. value = field.to_python( raw_input( "%s >>> " % field.name ).strip() );
  86. except Exception, instance:
  87. print instance;
  88. setattr( model, field.name, value );
  89. def act_serverDetails( server ):
  90. "View or edit server settings."
  91. util_editModel( server, ( "id", "sslcrt", "sslkey" ) );
  92. def act_registeredUsers( server ):
  93. "View or edit user registrations."
  94. mumbleusers_list = server.mumbleuser_set.all();
  95. print "Currently registered accounts";
  96. print "=============================";
  97. for mu in mumbleusers_list:
  98. if mu.owner is not None:
  99. print "#%-5d %-20s Owner: %-20s Admin: %s" % ( mu.id, mu.name, mu.owner.username, mu.getAdmin() );
  100. else:
  101. print "#%-5d %-20s" % ( mu.id, mu.name );
  102. print "=============================";
  103. print "Enter the ID of the account you would like to change, n to create a new one, or q to return."
  104. while True:
  105. idx = getNum( "ID", n=-1 );
  106. if idx is None:
  107. return;
  108. if idx == -1:
  109. mu = MumbleUser();
  110. mu.server = server;
  111. else:
  112. mu = mumbleusers_list.get( id=idx );
  113. util_editModel( mu, ( "id", "mumbleid", "server" ) );
  114. def act_listChannels( server ):
  115. "Display a channel tree."
  116. def printItem( item, level ):
  117. print "%s%s" % ( " "*level, item );
  118. server.rootchan.visit( printItem );
  119. def act_chanDetails( server ):
  120. "Display detailed information about one specific channel."
  121. print "Please choose the channel by entering the according ID (the number in parentheses)."
  122. act_listChannels( server );
  123. id = getNum( "ID" );
  124. if id is None: return;
  125. print "Channel name: %s" % server.channels[id].name
  126. print "Channel ID: %d" % server.channels[id].chanid
  127. print "Users online: %d" % len( server.channels[id].players )
  128. print "Linked chans: %d" % len( server.channels[id].linked )
  129. def cli_chooseServer():
  130. mumble_all = Mumble.objects.all().order_by( 'name', 'id' );
  131. print "Please choose a Server instance by typing the corresponding ID.\n";
  132. for mm in mumble_all:
  133. print "#%d\t%s" % ( mm.id, mm.name );
  134. print "n: Create new instance";
  135. print "q: Exit";
  136. id = getNum( "ID", n = -1 );
  137. if id is None:
  138. return;
  139. elif id == -1:
  140. return Mumble();
  141. return Mumble.objects.get( id=id );
  142. def cli_chooseAction( server ):
  143. actions = {
  144. "LISTCHAN": act_listChannels,
  145. "CHANINFO": act_chanDetails,
  146. "EDITSERVER": act_serverDetails,
  147. "EDITUSERS": act_registeredUsers,
  148. };
  149. while True:
  150. print "What do you want to do?"
  151. keys = actions.keys();
  152. for act in keys:
  153. print "#%-5d %-20s %s" % ( keys.index(act), act, actions[act].__doc__ );
  154. print "q: Return to server selection";
  155. idx = getNum( "Index" );
  156. if idx is None:
  157. return;
  158. # call action function
  159. func = actions[ keys[idx] ]
  160. func( server );
  161. print
  162. def oldmain():
  163. print
  164. while True:
  165. mumble = cli_chooseServer();
  166. if mumble is None:
  167. print "Bye.";
  168. return;
  169. print "Selected %s." % mumble;
  170. print
  171. cli_chooseAction( mumble );
  172. print
  173. class BaseWindow( object ):
  174. tabName = "tehBasez";
  175. def __init__( self, parentwin, mumble, pos_x, pos_y ):
  176. self.pos = ( pos_x, pos_y );
  177. self.win = parentwin.subwin( pos_y, pos_x );
  178. self.mm = mumble;
  179. self.win.keypad(1);
  180. def draw( self ):
  181. self.win.addstr( 1, 1, self.tabName );
  182. def border( self ):
  183. self.win.border();
  184. def enter( self ):
  185. while( True ):
  186. key = self.win.getch();
  187. if key == curses.KEY_UP:
  188. return;
  189. class FormEditor( object ):
  190. def __init__( self, win, form ):
  191. self.win = win;
  192. self.form = form;
  193. def draw( self ):
  194. curr_y = 1;
  195. for fname in self.form.fields:
  196. field = self.form.fields[fname];
  197. value = unicode( getattr( self.form.data, fname ) );
  198. self.win.addstr( curr_y, 1, field.label.encode(locale.getpreferredencoding()) );
  199. self.win.addstr( curr_y, 30, value.encode(locale.getpreferredencoding()) );
  200. curr_y += 1;
  201. class WndChannels( BaseWindow ):
  202. tabName = 'Channels';
  203. def printItem( self, item, level ):
  204. str = "";
  205. if item.is_server or item.is_channel:
  206. str = "%s (Channel)" % item.name;
  207. else:
  208. str = "%s (Player)" % item.name
  209. self.win.addstr( self.curr_y, 4*level+1, str.encode(locale.getpreferredencoding()) )
  210. self.curr_y += 1;
  211. def draw( self ):
  212. self.curr_y = 1;
  213. self.mm.rootchan.visit( self.printItem );
  214. class WndSettings( BaseWindow, FormEditor ):
  215. tabName = 'Server settings';
  216. def __init__( self, parentwin, mumble, pos_x, pos_y ):
  217. BaseWindow.__init__( self, parentwin, mumble, pos_x, pos_y );
  218. FormEditor.__init__( self, self.win, MumbleAdminForm( mumble ) );
  219. def draw( self ):
  220. FormEditor.draw( self );
  221. class WndUsers( BaseWindow ):
  222. tabName = 'Registered users';
  223. class MumbleForm( object ):
  224. def __init__( self, parentwin, mumble, pos_x, pos_y ):
  225. self.pos = ( pos_x, pos_y );
  226. self.win = parentwin.subwin( pos_y, pos_x );
  227. self.mm = mumble;
  228. self.win.keypad(1);
  229. self.windows = (
  230. WndChannels( self.win, mumble, pos_x + 2, pos_y + 2 ),
  231. WndSettings( self.win, mumble, pos_x + 2, pos_y + 2 ),
  232. WndUsers( self.win, mumble, pos_x + 2, pos_y + 2 ),
  233. );
  234. self.curridx = 0;
  235. self.currmax = len( self.windows ) - 1;
  236. currwin = property( lambda self: self.windows[self.curridx], None );
  237. def mvwin( self, pos_x, pos_y ):
  238. self.win.mvwin( pos_y, pos_x );
  239. def mvdefault( self ):
  240. self.win.mvwin( self.pos[1], self.pos[0] );
  241. def draw( self ):
  242. self.win.addstr( 0, 0, self.mm.name.encode(locale.getpreferredencoding()) );
  243. def drawTabs( self ):
  244. first = True;
  245. for subwin in self.windows:
  246. flags = 0;
  247. if subwin is self.currwin: flags |= curses.A_STANDOUT;
  248. if first:
  249. self.win.addstr( 1, 2, "%-20s" % subwin.tabName, flags );
  250. first = False;
  251. else:
  252. self.win.addstr( "%-20s" % subwin.tabName, flags );
  253. def enter( self ):
  254. self.drawTabs();
  255. self.currwin.draw();
  256. self.currwin.border();
  257. while( True ):
  258. key = self.win.getch();
  259. if key == curses.KEY_LEFT and self.curridx > 0:
  260. self.curridx -= 1;
  261. elif key == curses.KEY_RIGHT and self.curridx < self.currmax:
  262. self.curridx += 1;
  263. elif key in ( ord('q'), ord('Q'), curses.KEY_UP ):
  264. return;
  265. elif key in ( curses.KEY_ENTER, curses.KEY_DOWN, ord('\n') ):
  266. self.currwin.enter();
  267. self.win.clear();
  268. self.draw();
  269. self.drawTabs();
  270. self.currwin.draw();
  271. self.currwin.border();
  272. self.win.refresh();
  273. def main( stdscr ):
  274. first_y = 3;
  275. curr_y = first_y;
  276. mumbles = list();
  277. for mm in Mumble.objects.all().order_by( "name", "id" ):
  278. mwin = MumbleForm( stdscr, mm, pos_x=5, pos_y=curr_y );
  279. mumbles.append( mwin );
  280. mwin.draw();
  281. curr_y += 1;
  282. selectedIdx = 0;
  283. selectedMax = len(mumbles) - 1;
  284. while( True ):
  285. selectedObj = mumbles[selectedIdx];
  286. # Draw selection marker
  287. stdscr.addstr( first_y + selectedIdx, 3, '*' );
  288. stdscr.refresh();
  289. key = stdscr.getch();
  290. if key == curses.KEY_UP:
  291. stdscr.addstr( first_y + selectedIdx, 3, ' ' );
  292. selectedIdx -= 1;
  293. elif key == curses.KEY_DOWN:
  294. stdscr.addstr( first_y + selectedIdx, 3, ' ' );
  295. selectedIdx += 1;
  296. elif key in ( curses.KEY_RIGHT, curses.KEY_ENTER, ord('\n') ):
  297. stdscr.clear();
  298. selectedObj.mvwin( 5, first_y );
  299. selectedObj.draw();
  300. stdscr.refresh();
  301. selectedObj.enter();
  302. stdscr.clear();
  303. selectedObj.mvdefault();
  304. for mwin in mumbles:
  305. mwin.draw();
  306. elif key in ( ord('q'), ord('Q') ):
  307. return;
  308. if selectedIdx < 0: selectedIdx = 0;
  309. elif selectedIdx > selectedMax: selectedIdx = selectedMax;
  310. if __name__ == '__main__':
  311. #parser = OptionParser();
  312. #parser.add_option( "-v", "--verbose", help="verbose output messages", default=False, action="store_true" );
  313. #parser.add_option( "-n", "--num", help="size of the Matrix", default=4, type = 'int' );
  314. #parser.add_option( "-s", "--sure", help="don't prompt if num >= 10", default=False, action="store_true" );
  315. #options, args = parser.parse_args();
  316. curses.wrapper( main );