Package command_system :: Package implementation :: Module standard
[hide private]
[frames] | no frames]

Source Code for Module command_system.implementation.standard

  1  # Copyright (C) 2009-2010  Alexander Cherniuk <ts33kr@gmail.com> 
  2  # 
  3  # This program is free software: you can redistribute it and/or modify 
  4  # it under the terms of the GNU General Public License as published by 
  5  # the Free Software Foundation, either version 3 of the License, or 
  6  # (at your option) any later version. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, 
  9  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 11  # GNU General Public License for more details. 
 12  # 
 13  # You should have received a copy of the GNU General Public License 
 14  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 15   
 16  """ 
 17  Provides an actual implementation for the standard commands. 
 18  """ 
 19   
 20  from time import localtime, strftime 
 21  from datetime import date 
 22   
 23  import dialogs 
 24  from common import gajim 
 25  from common import helpers 
 26  from common.exceptions import GajimGeneralException 
 27  from common.logger import Constants 
 28   
 29  from ..errors import CommandError 
 30  from ..framework import CommandContainer, command, doc 
 31  from ..mapping import generate_usage 
 32   
 33  from hosts import * 
 34  import execute 
35 36 # This holds constants fron the logger, which we'll be using in some of our 37 # commands. 38 lc = Constants() 39 40 -class StandardCommonCommands(CommandContainer):
41 """ 42 This command container contains standard commands which are common 43 to all - chat, private chat, group chat. 44 """ 45 46 AUTOMATIC = True 47 HOSTS = ChatCommands, PrivateChatCommands, GroupChatCommands 48 49 @command 50 @doc(_("Clear the text window"))
51 - def clear(self):
52 self.conv_textview.clear()
53 54 @command 55 @doc(_("Hide the chat buttons"))
56 - def compact(self):
57 new_status = not self.hide_chat_buttons 58 self.chat_buttons_set_visible(new_status)
59 60 @command(overlap=True) 61 @doc(_("Show help on a given command or a list of available commands if -a is given"))
62 - def help(self, command=None, all=False):
63 if command: 64 command = self.get_command(command) 65 66 documentation = _(command.extract_documentation()) 67 usage = generate_usage(command) 68 69 text = [] 70 71 if documentation: 72 text.append(documentation) 73 if command.usage: 74 text.append(usage) 75 76 return '\n\n'.join(text) 77 elif all: 78 for command in self.list_commands(): 79 names = ', '.join(command.names) 80 description = command.extract_description() 81 82 self.echo("%s - %s" % (names, description)) 83 else: 84 help = self.get_command('help') 85 self.echo(help(self, 'help'))
86 87 @command(raw=True) 88 @doc(_("Send a message to the contact"))
89 - def say(self, message):
90 self.send(message)
91 92 @command(raw=True) 93 @doc(_("Send action (in the third person) to the current chat"))
94 - def me(self, action):
95 self.send("/me %s" % action)
96 97 @command('lastlog', overlap=True) 98 @doc(_("Show logged messages which mention given text"))
99 - def grep(self, text, limit=None):
100 results = gajim.logger.get_search_results_for_query(self.contact.jid, 101 text, self.account) 102 103 if not results: 104 raise CommandError(_("%s: Nothing found") % text) 105 106 if limit: 107 try: 108 results = results[len(results) - int(limit):] 109 except ValueError: 110 raise CommandError(_("Limit must be an integer")) 111 112 for row in results: 113 contact, time, kind, show, message, subject = row 114 115 if not contact: 116 if kind == lc.KIND_CHAT_MSG_SENT: 117 contact = gajim.nicks[self.account] 118 else: 119 contact = self.contact.name 120 121 time_obj = localtime(time) 122 date_obj = date.fromtimestamp(time) 123 date_ = strftime('%Y-%m-%d', time_obj) 124 time_ = strftime('%H:%M:%S', time_obj) 125 126 if date_obj == date.today(): 127 formatted = "[%s] %s: %s" % (time_, contact, message) 128 else: 129 formatted = "[%s, %s] %s: %s" % (date_, time_, contact, message) 130 131 self.echo(formatted)
132 133 @command(raw=True, empty=True) 134 @doc(_(""" 135 Set current the status 136 137 Status can be given as one of the following values: online, away, 138 chat, xa, dnd. 139 """))
140 - def status(self, status, message):
141 if status not in ('online', 'away', 'chat', 'xa', 'dnd'): 142 raise CommandError("Invalid status given") 143 for connection in gajim.connections.itervalues(): 144 connection.change_status(status, message)
145 146 @command(raw=True, empty=True) 147 @doc(_("Set the current status to away"))
148 - def away(self, message):
149 if not message: 150 message = _("Away") 151 for connection in gajim.connections.itervalues(): 152 connection.change_status('away', message)
153 154 @command('back', raw=True, empty=True) 155 @doc(_("Set the current status to online"))
156 - def online(self, message):
157 if not message: 158 message = _("Available") 159 for connection in gajim.connections.itervalues(): 160 connection.change_status('online', message)
161
162 -class StandardCommonChatCommands(CommandContainer):
163 """ 164 This command container contans standard commands, which are common 165 to a chat and a private chat only. 166 """ 167 168 AUTOMATIC = True 169 HOSTS = ChatCommands, PrivateChatCommands 170 171 @command 172 @doc(_("Toggle the GPG encryption"))
173 - def gpg(self):
174 self._toggle_gpg()
175 176 @command 177 @doc(_("Send a ping to the contact"))
178 - def ping(self):
179 if self.account == gajim.ZEROCONF_ACC_NAME: 180 raise CommandError(_('Command is not supported for zeroconf accounts')) 181 gajim.connections[self.account].sendPing(self.contact)
182 183 @command 184 @doc(_("Send DTMF sequence through an open audio session"))
185 - def dtmf(self, sequence):
186 if not self.audio_sid: 187 raise CommandError(_("No open audio sessions with the contact")) 188 for tone in sequence: 189 if not (tone in ("*", "#") or tone.isdigit()): 190 raise CommandError(_("%s is not a valid tone") % tone) 191 gjs = self.connection.get_jingle_session 192 session = gjs(self.full_jid, self.audio_sid) 193 content = session.get_content("audio") 194 content.batch_dtmf(sequence)
195 196 @command 197 @doc(_("Toggle audio session"))
198 - def audio(self):
199 if not self.audio_available: 200 raise CommandError(_("Audio sessions are not available")) 201 # An audio session is toggled by inverting the state of the 202 # appropriate button. 203 state = self._audio_button.get_active() 204 self._audio_button.set_active(not state)
205 206 @command 207 @doc(_("Toggle video session"))
208 - def video(self):
209 if not self.video_available: 210 raise CommandError(_("Video sessions are not available")) 211 # A video session is toggled by inverting the state of the 212 # appropriate button. 213 state = self._video_button.get_active() 214 self._video_button.set_active(not state)
215
216 -class StandardChatCommands(CommandContainer):
217 """ 218 This command container contains standard commands which are unique 219 to a chat. 220 """ 221 222 AUTOMATIC = True 223 HOSTS = ChatCommands,
224
225 -class StandardPrivateChatCommands(CommandContainer):
226 """ 227 This command container contains standard commands which are unique 228 to a private chat. 229 """ 230 231 AUTOMATIC = True 232 HOSTS = PrivateChatCommands,
233
234 -class StandardGroupChatCommands(CommandContainer):
235 """ 236 This command container contains standard commands which are unique 237 to a group chat. 238 """ 239 240 AUTOMATIC = True 241 HOSTS = GroupChatCommands, 242 243 @command(raw=True) 244 @doc(_("Change your nickname in a group chat"))
245 - def nick(self, new_nick):
246 try: 247 new_nick = helpers.parse_resource(new_nick) 248 except Exception: 249 raise CommandError(_("Invalid nickname")) 250 self.connection.join_gc(new_nick, self.room_jid, None, change_nick=True) 251 self.new_nick = new_nick
252 253 @command('query', raw=True) 254 @doc(_("Open a private chat window with a specified occupant"))
255 - def chat(self, nick):
256 nicks = gajim.contacts.get_nick_list(self.account, self.room_jid) 257 if nick in nicks: 258 self.on_send_pm(nick=nick) 259 else: 260 raise CommandError(_("Nickname not found"))
261 262 @command('msg', raw=True) 263 @doc(_("Open a private chat window with a specified occupant and send him a message"))
264 - def message(self, nick, a_message):
265 nicks = gajim.contacts.get_nick_list(self.account, self.room_jid) 266 if nick in nicks: 267 self.on_send_pm(nick=nick, msg=a_message) 268 else: 269 raise CommandError(_("Nickname not found"))
270 271 @command(raw=True, empty=True) 272 @doc(_("Display or change a group chat topic"))
273 - def topic(self, new_topic):
274 if new_topic: 275 self.connection.send_gc_subject(self.room_jid, new_topic) 276 else: 277 return self.subject
278 279 @command(raw=True, empty=True) 280 @doc(_("Invite a user to a room for a reason"))
281 - def invite(self, jid, reason):
282 self.connection.send_invite(self.room_jid, jid, reason) 283 return _("Invited %s to %s") % (jid, self.room_jid)
284 285 @command(raw=True, empty=True) 286 @doc(_("Join a group chat given by a jid, optionally using given nickname"))
287 - def join(self, jid, nick):
288 if not nick: 289 nick = self.nick 290 291 if '@' not in jid: 292 jid = jid + '@' + gajim.get_server_from_jid(self.room_jid) 293 294 try: 295 gajim.interface.instances[self.account]['join_gc'].window.present() 296 except KeyError: 297 try: 298 dialogs.JoinGroupchatWindow(account=self.account, room_jid=jid, nick=nick) 299 except GajimGeneralException: 300 pass
301 302 @command('part', 'close', raw=True, empty=True) 303 @doc(_("Leave the groupchat, optionally giving a reason, and close tab or window"))
304 - def leave(self, reason):
305 self.parent_win.remove_tab(self, self.parent_win.CLOSE_COMMAND, reason)
306 307 @command(raw=True, empty=True) 308 @doc(_(""" 309 Ban user by a nick or a jid from a groupchat 310 311 If given nickname is not found it will be treated as a jid. 312 """))
313 - def ban(self, who, reason):
314 if who in gajim.contacts.get_nick_list(self.account, self.room_jid): 315 contact = gajim.contacts.get_gc_contact(self.account, self.room_jid, who) 316 who = contact.jid 317 self.connection.gc_set_affiliation(self.room_jid, who, 'outcast', reason or str())
318 319 @command(raw=True, empty=True) 320 @doc(_("Kick user by a nick from a groupchat"))
321 - def kick(self, who, reason):
322 if not who in gajim.contacts.get_nick_list(self.account, self.room_jid): 323 raise CommandError(_("Nickname not found")) 324 self.connection.gc_set_role(self.room_jid, who, 'none', reason or str())
325 326 @command 327 @doc(_("Display names of all group chat occupants"))
328 - def names(self, verbose=False):
329 ggc = gajim.contacts.get_gc_contact 330 gnl = gajim.contacts.get_nick_list 331 332 get_contact = lambda nick: ggc(self.account, self.room_jid, nick) 333 get_role = lambda nick: get_contact(nick).role 334 nicks = gnl(self.account, self.room_jid) 335 336 nicks = sorted(nicks) 337 nicks = sorted(nicks, key=get_role) 338 339 if not verbose: 340 return ", ".join(nicks) 341 342 for nick in nicks: 343 contact = get_contact(nick) 344 role = helpers.get_uf_role(contact.role) 345 affiliation = helpers.get_uf_affiliation(contact.affiliation) 346 self.echo("%s - %s - %s" % (nick, role, affiliation))
347 348 @command('ignore', raw=True) 349 @doc(_("Forbid an occupant to send you public or private messages"))
350 - def block(self, who):
351 self.on_block(None, who)
352 353 @command('unignore', raw=True) 354 @doc(_("Allow an occupant to send you public or private messages"))
355 - def unblock(self, who):
356 self.on_unblock(None, who)
357