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.

334 lines
10 KiB

16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. *
  5. * Mumble-Django is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This package is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. """
  15. import simplejson
  16. from StringIO import StringIO
  17. from os.path import join
  18. from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
  19. from django.template import RequestContext
  20. from django.http import Http404, HttpResponse, HttpResponseRedirect
  21. from django.contrib.auth.decorators import login_required
  22. from django.contrib.auth.models import User
  23. from django.conf import settings
  24. from django.core.urlresolvers import reverse
  25. from django.contrib.auth import views as auth_views
  26. from models import Mumble, MumbleUser
  27. from forms import *
  28. from mmobjects import *
  29. def redir( request ):
  30. """ Redirect to the servers list. """
  31. if request.META['HTTP_USER_AGENT'].startswith( 'BlackBerry' ) or \
  32. "Opera Mobi" in request.META['HTTP_USER_AGENT'] or \
  33. "Opera Mini" in request.META['HTTP_USER_AGENT'] or \
  34. "Windows CE" in request.META['HTTP_USER_AGENT'] or \
  35. "MIDP" in request.META['HTTP_USER_AGENT'] or \
  36. "Palm" in request.META['HTTP_USER_AGENT'] or \
  37. "NetFront" in request.META['HTTP_USER_AGENT'] or \
  38. "Nokia" in request.META['HTTP_USER_AGENT'] or \
  39. "Symbian" in request.META['HTTP_USER_AGENT'] or \
  40. "UP.Browser" in request.META['HTTP_USER_AGENT'] or \
  41. "UP.Link" in request.META['HTTP_USER_AGENT'] or \
  42. "WinWAP" in request.META['HTTP_USER_AGENT'] or \
  43. "Android" in request.META['HTTP_USER_AGENT'] or \
  44. "DoCoMo" in request.META['HTTP_USER_AGENT'] or \
  45. "KDDI-" in request.META['HTTP_USER_AGENT'] or \
  46. "Softbank" in request.META['HTTP_USER_AGENT'] or \
  47. "J-Phone" in request.META['HTTP_USER_AGENT'] or \
  48. "IEMobile" in request.META['HTTP_USER_AGENT'] or \
  49. "iPod" in request.META['HTTP_USER_AGENT'] or \
  50. "iPhone" in request.META['HTTP_USER_AGENT']:
  51. return HttpResponseRedirect( reverse( mobile_mumbles ) );
  52. else:
  53. return HttpResponseRedirect( reverse( mumbles ) );
  54. def mumbles( request ):
  55. """ Display a list of all configured Mumble servers, or redirect if only one configured. """
  56. mumbles = get_list_or_404( Mumble );
  57. if len(mumbles) == 1:
  58. return HttpResponseRedirect( reverse( show, kwargs={ 'server': mumbles[0].id, } ) );
  59. return render_to_response(
  60. 'mumble/list.html',
  61. { 'MumbleObjects': mumbles,
  62. 'MumbleActive': True,
  63. },
  64. context_instance = RequestContext(request)
  65. );
  66. def mobile_mumbles( request ):
  67. """ Display a list of all configured Mumble servers, or redirect if only one configured. """
  68. mumbles = get_list_or_404( Mumble );
  69. if len(mumbles) == 1:
  70. return HttpResponseRedirect( reverse( mobile_show, kwargs={ 'server': mumbles[0].id, } ) );
  71. return render_to_response(
  72. 'mumble/mobile_list.html',
  73. { 'MumbleObjects': mumbles,
  74. 'MumbleActive': True,
  75. },
  76. context_instance = RequestContext(request)
  77. );
  78. def show( request, server ):
  79. """Display the channel list for the given Server ID.
  80. This includes not only the channel list itself, but indeed the user registration,
  81. server admin and user texture form as well. The template then uses JavaScript
  82. to display these forms integrated into the Channel viewer.
  83. """
  84. srv = get_object_or_404( Mumble, id=server );
  85. isAdmin = srv.isUserAdmin( request.user );
  86. # The tab to start on.
  87. displayTab = 0;
  88. if isAdmin:
  89. if request.method == 'POST' and "mode" in request.POST and request.POST['mode'] == 'admin':
  90. adminform = MumbleForm( request.POST, instance=srv );
  91. if adminform.is_valid():
  92. adminform.save();
  93. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  94. else:
  95. displayTab = 2;
  96. else:
  97. adminform = MumbleForm( instance=srv );
  98. else:
  99. adminform = None;
  100. registered = False;
  101. user = None;
  102. if request.user.is_authenticated():
  103. # Unregistered users may or may not need a password to register.
  104. if settings.PROTECTED_MODE and srv.passwd:
  105. unregged_user_form = MumbleUserPasswordForm;
  106. # Unregistered users may or may not want to link an existing account
  107. elif settings.ALLOW_ACCOUNT_LINKING:
  108. unregged_user_form = MumbleUserLinkForm;
  109. else:
  110. unregged_user_form = MumbleUserForm;
  111. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'reg':
  112. try:
  113. user = MumbleUser.objects.get( server=srv, owner=request.user );
  114. except MumbleUser.DoesNotExist:
  115. regform = unregged_user_form( request.POST );
  116. regform.server = srv;
  117. if regform.is_valid():
  118. model = regform.save( commit=False );
  119. model.owner = request.user;
  120. model.server = srv;
  121. # If we're linking accounts, the change is local only.
  122. model.save( dontConfigureMurmur=( "linkacc" in regform.data ) );
  123. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  124. else:
  125. displayTab = 1;
  126. else:
  127. regform = MumbleUserForm( request.POST, instance=user );
  128. regform.server = srv;
  129. if regform.is_valid():
  130. regform.save();
  131. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  132. else:
  133. displayTab = 1;
  134. else:
  135. try:
  136. user = MumbleUser.objects.get( server=srv, owner=request.user );
  137. except MumbleUser.DoesNotExist:
  138. regform = unregged_user_form();
  139. else:
  140. regform = MumbleUserForm( instance=user );
  141. registered = True;
  142. if request.method == 'POST' and 'mode' in request.POST and request.POST['mode'] == 'texture' and registered:
  143. textureform = MumbleTextureForm( request.POST, request.FILES );
  144. if textureform.is_valid():
  145. user.setTexture( request.FILES['texturefile'] );
  146. return HttpResponseRedirect( reverse( show, kwargs={ 'server': int(server), } ) );
  147. else:
  148. textureform = MumbleTextureForm();
  149. else:
  150. regform = None;
  151. textureform = None;
  152. # ChannelTable is a somewhat misleading name, as it actually contains channels and players.
  153. channelTable = [];
  154. for id in srv.channels:
  155. if id != 0 and srv.channels[id].show:
  156. channelTable.append( srv.channels[id] );
  157. for id in srv.players:
  158. channelTable.append( srv.players[id] );
  159. show_url = reverse( show, kwargs={ 'server': srv.id } );
  160. login_url = reverse( auth_views.login );
  161. return render_to_response(
  162. 'mumble/mumble.html',
  163. {
  164. 'login_url': "%s?next=%s" % ( login_url, show_url ),
  165. 'DBaseObject': srv,
  166. 'ChannelTable': channelTable,
  167. 'CurrentUserIsAdmin': isAdmin,
  168. 'AdminForm': adminform,
  169. 'RegForm': regform,
  170. 'TextureForm': textureform,
  171. 'Registered': registered,
  172. 'DisplayTab': displayTab,
  173. 'MumbleActive': True,
  174. 'MumbleAccount':user,
  175. },
  176. context_instance = RequestContext(request)
  177. );
  178. def mobile_show( request, server ):
  179. """ Display the channel list for the given Server ID. """
  180. srv = get_object_or_404( Mumble, id=server );
  181. user = None;
  182. if request.user.is_authenticated():
  183. try:
  184. user = MumbleUser.objects.get( server=srv, owner=request.user );
  185. except MumbleUser.DoesNotExist:
  186. pass;
  187. return render_to_response(
  188. 'mumble/mobile_mumble.html',
  189. {
  190. 'DBaseObject': srv,
  191. 'MumbleActive': True,
  192. 'MumbleAccount':user,
  193. },
  194. context_instance = RequestContext(request)
  195. );
  196. def showTexture( request, server, userid = None ):
  197. """ Pack the given user's texture into an HttpResponse.
  198. If userid is none, use the currently logged in User.
  199. """
  200. srv = get_object_or_404( Mumble, id=int(server) );
  201. if userid is None:
  202. if request.user.is_authenticated():
  203. user = get_object_or_404( MumbleUser, server=srv, owner=request.user );
  204. else:
  205. raise Http404();
  206. else:
  207. user = get_object_or_404( MumbleUser, server=srv, id=int(userid) );
  208. try:
  209. img = user.getTexture();
  210. except ValueError:
  211. raise Http404();
  212. else:
  213. buffer = StringIO();
  214. img.save( buffer, "PNG" );
  215. return HttpResponse( buffer.getvalue(), "image/png" );
  216. @login_required
  217. def users( request, server ):
  218. """ Create a list of MumbleUsers for a given server serialized as a JSON object.
  219. If the request has a "data" field, evaluate that and update the user records.
  220. """
  221. srv = get_object_or_404( Mumble, id=int(server) );
  222. if "resync" in request.POST and request.POST['resync'] == "true":
  223. srv.readUsersFromMurmur();
  224. if not srv.isUserAdmin( request.user ):
  225. return HttpResponse(
  226. simplejson.dumps({ 'success': False, 'objects': [], 'errormsg': 'Access denied' }),
  227. mimetype='text/javascript'
  228. );
  229. if request.method == 'POST':
  230. data = simplejson.loads( request.POST['data'] );
  231. for record in data:
  232. if record['id'] == -1:
  233. if record['delete']:
  234. continue;
  235. mu = MumbleUser( server=srv );
  236. else:
  237. mu = MumbleUser.objects.get( id=record['id'] );
  238. if record['delete']:
  239. mu.delete();
  240. continue;
  241. mu.name = record['name'];
  242. mu.password = record['password'];
  243. mu.isAdmin = record['admin'];
  244. if record['owner']:
  245. mu.owner = User.objects.get( id=int(record['owner']) );
  246. mu.save();
  247. users = [];
  248. for mu in srv.mumbleuser_set.all():
  249. owner = None;
  250. if mu.owner is not None:
  251. owner = mu.owner.id
  252. users.append( {
  253. 'id': mu.id,
  254. 'name': mu.name,
  255. 'password': None,
  256. 'owner': owner,
  257. 'admin': mu.getAdmin(),
  258. } );
  259. return HttpResponse(
  260. simplejson.dumps( { 'success': True, 'objects': users } ),
  261. mimetype='text/javascript'
  262. );
  263. @login_required
  264. def djangousers( request ):
  265. """ Return a list of all Django users' names and IDs. """
  266. users = [ { 'uid': '', 'uname': '------' } ];
  267. for du in User.objects.all().order_by( 'username' ):
  268. users.append( {
  269. 'uid': du.id,
  270. 'uname': unicode( du ),
  271. } );
  272. return HttpResponse(
  273. simplejson.dumps( { 'success': True, 'objects': users } ),
  274. mimetype='text/javascript'
  275. );