2009-09-12 15:51:21 +02:00
|
|
|
# Copyright (C) 2009 red-agent <hell.director@gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Provides an actual implementation of the standard commands.
|
|
|
|
"""
|
|
|
|
|
2009-09-13 16:43:44 +02:00
|
|
|
import dialogs
|
2009-09-12 15:51:21 +02:00
|
|
|
from common import gajim
|
2009-09-13 16:43:44 +02:00
|
|
|
from common import helpers
|
|
|
|
from common.exceptions import GajimGeneralException
|
2009-09-12 15:51:21 +02:00
|
|
|
|
|
|
|
from framework import command, CommandError
|
|
|
|
from middleware import ChatMiddleware
|
|
|
|
|
|
|
|
class CommonCommands(ChatMiddleware):
|
|
|
|
"""
|
|
|
|
Here defined commands will be common to all, chat, private chat and group
|
|
|
|
chat. Keep in mind that self is set to an instance of either ChatControl,
|
|
|
|
PrivateChatControl or GroupchatControl when command is being called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@command
|
|
|
|
def clear(self):
|
|
|
|
"""
|
|
|
|
Clear the text window
|
|
|
|
"""
|
|
|
|
self.conv_textview.clear()
|
|
|
|
|
|
|
|
@command
|
|
|
|
def compact(self):
|
|
|
|
"""
|
|
|
|
Hide the chat buttons
|
|
|
|
"""
|
|
|
|
self.chat_buttons_set_visible(not self.hide_chat_buttons)
|
|
|
|
|
2009-09-17 03:38:39 +02:00
|
|
|
@command(overlap=True)
|
2009-09-12 15:51:21 +02:00
|
|
|
def help(self, command=None, all=False):
|
|
|
|
"""
|
|
|
|
Show help on a given command or a list of available commands if -(-a)ll is
|
|
|
|
given
|
|
|
|
"""
|
|
|
|
if command:
|
|
|
|
command = self.retrieve_command(command)
|
|
|
|
|
|
|
|
doc = _(command.extract_doc())
|
|
|
|
usage = command.extract_arg_usage()
|
|
|
|
|
|
|
|
if doc:
|
|
|
|
return (doc + '\n\n' + usage) if command.usage else doc
|
|
|
|
else:
|
|
|
|
return usage
|
|
|
|
elif all:
|
|
|
|
for command in self.list_commands():
|
|
|
|
names = ', '.join(command.names)
|
|
|
|
description = command.extract_description()
|
|
|
|
|
|
|
|
self.echo("%s - %s" % (names, description))
|
|
|
|
else:
|
|
|
|
self.echo(self._help_(self, 'help'))
|
|
|
|
|
|
|
|
@command(raw=True)
|
|
|
|
def say(self, message):
|
|
|
|
"""
|
|
|
|
Send a message to the contact
|
|
|
|
"""
|
|
|
|
self.send(message)
|
|
|
|
|
|
|
|
@command(raw=True)
|
|
|
|
def me(self, action):
|
|
|
|
"""
|
|
|
|
Send action (in the third person) to the current chat
|
|
|
|
"""
|
|
|
|
self.send("/me %s" % action)
|
|
|
|
|
|
|
|
class ChatCommands(CommonCommands):
|
|
|
|
"""
|
|
|
|
Here defined commands will be unique to a chat. Use it as a hoster to provide
|
|
|
|
commands which should be unique to a chat. Keep in mind that self is set to
|
|
|
|
an instance of ChatControl when command is being called.
|
|
|
|
"""
|
|
|
|
|
2009-09-14 21:04:49 +02:00
|
|
|
DISPATCH = True
|
|
|
|
INHERIT = True
|
2009-09-12 15:51:21 +02:00
|
|
|
|
|
|
|
@command
|
|
|
|
def ping(self):
|
|
|
|
"""
|
|
|
|
Send a ping to the contact
|
|
|
|
"""
|
|
|
|
if self.account == gajim.ZEROCONF_ACC_NAME:
|
2009-09-12 22:22:50 +02:00
|
|
|
raise CommandError(_('Command is not supported for zeroconf accounts'))
|
2009-09-12 15:51:21 +02:00
|
|
|
gajim.connections[self.account].sendPing(self.contact)
|
|
|
|
|
|
|
|
class PrivateChatCommands(CommonCommands):
|
|
|
|
"""
|
|
|
|
Here defined commands will be unique to a private chat. Use it as a hoster to
|
|
|
|
provide commands which should be unique to a private chat. Keep in mind that
|
|
|
|
self is set to an instance of PrivateChatControl when command is being called.
|
|
|
|
"""
|
|
|
|
|
2009-09-14 21:04:49 +02:00
|
|
|
DISPATCH = True
|
|
|
|
INHERIT = True
|
2009-09-12 15:51:21 +02:00
|
|
|
|
|
|
|
class GroupChatCommands(CommonCommands):
|
|
|
|
"""
|
|
|
|
Here defined commands will be unique to a group chat. Use it as a hoster to
|
|
|
|
provide commands which should be unique to a group chat. Keep in mind that
|
|
|
|
self is set to an instance of GroupchatControl when command is being called.
|
|
|
|
"""
|
|
|
|
|
2009-09-14 21:04:49 +02:00
|
|
|
DISPATCH = True
|
|
|
|
INHERIT = True
|
2009-09-13 16:43:44 +02:00
|
|
|
|
|
|
|
@command(raw=True)
|
|
|
|
def nick(self, new_nick):
|
|
|
|
"""
|
|
|
|
Change your nickname in a group chat
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
new_nick = helpers.parse_resource(new_nick)
|
|
|
|
except Exception:
|
|
|
|
raise CommandError(_("Invalid nickname"))
|
|
|
|
self.connection.join_gc(new_nick, self.room_jid, None, change_nick=True)
|
2009-09-15 21:59:48 +02:00
|
|
|
self.new_nick = new_nick
|
2009-09-13 16:43:44 +02:00
|
|
|
|
|
|
|
@command('query', raw=True)
|
|
|
|
def chat(self, nick):
|
|
|
|
"""
|
|
|
|
Open a private chat window with a specified occupant
|
|
|
|
"""
|
|
|
|
nicks = gajim.contacts.get_nick_list(self.account, self.room_jid)
|
|
|
|
if nick in nicks:
|
|
|
|
self.on_send_pm(nick=nick)
|
|
|
|
else:
|
|
|
|
raise CommandError(_("Nickname not found"))
|
|
|
|
|
2009-09-17 03:38:39 +02:00
|
|
|
@command('msg', raw=True)
|
|
|
|
def message(self, nick, a_message):
|
2009-09-13 16:43:44 +02:00
|
|
|
"""
|
|
|
|
Open a private chat window with a specified occupant and send him a
|
|
|
|
message
|
|
|
|
"""
|
|
|
|
nicks = gajim.contacts.get_nick_list(self.account, self.room_jid)
|
|
|
|
if nick in nicks:
|
|
|
|
self.on_send_pm(nick=nick, msg=a_message)
|
|
|
|
else:
|
|
|
|
raise CommandError(_("Nickname not found"))
|
|
|
|
|
|
|
|
@command(raw=True, empty=True)
|
|
|
|
def topic(self, new_topic):
|
|
|
|
"""
|
|
|
|
Display or change a group chat topic
|
|
|
|
"""
|
|
|
|
if new_topic:
|
|
|
|
self.connection.send_gc_subject(self.room_jid, new_topic)
|
|
|
|
else:
|
|
|
|
return self.subject
|
|
|
|
|
2009-09-17 03:38:39 +02:00
|
|
|
@command(raw=True, empty=True)
|
|
|
|
def invite(self, jid, reason):
|
2009-09-13 16:43:44 +02:00
|
|
|
"""
|
|
|
|
Invite a user to a room for a reason
|
|
|
|
"""
|
|
|
|
self.connection.send_invite(self.room_jid, jid, reason)
|
|
|
|
return _("Invited %s to %s") % (jid, self.room_jid)
|
|
|
|
|
2009-09-17 03:38:39 +02:00
|
|
|
@command(raw=True, empty=True)
|
|
|
|
def join(self, jid, nick):
|
2009-09-13 16:43:44 +02:00
|
|
|
"""
|
|
|
|
Join a group chat given by a jid, optionally using given nickname
|
|
|
|
"""
|
2009-09-17 03:38:39 +02:00
|
|
|
if not nick:
|
|
|
|
nick = self.nick
|
2009-09-13 16:43:44 +02:00
|
|
|
|
|
|
|
if '@' not in jid:
|
|
|
|
jid = jid + '@' + gajim.get_server_from_jid(self.room_jid)
|
|
|
|
|
|
|
|
try:
|
|
|
|
gajim.interface.instances[self.account]['join_gc'].window.present()
|
|
|
|
except KeyError:
|
|
|
|
try:
|
2009-09-24 00:28:35 +02:00
|
|
|
dialogs.JoinGroupchatWindow(account=self.account, room_jid=jid, nick=nick)
|
2009-09-13 16:43:44 +02:00
|
|
|
except GajimGeneralException:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@command('part', 'close', raw=True, empty=True)
|
|
|
|
def leave(self, reason):
|
|
|
|
"""
|
|
|
|
Leave the groupchat, optionally giving a reason, and close tab or window
|
|
|
|
"""
|
|
|
|
self.parent_win.remove_tab(self, self.parent_win.CLOSE_COMMAND, reason)
|
|
|
|
|
2009-09-17 03:38:39 +02:00
|
|
|
@command(raw=True, empty=True)
|
|
|
|
def ban(self, who, reason):
|
2009-09-13 16:43:44 +02:00
|
|
|
"""
|
|
|
|
Ban user by a nick or a jid from a groupchat
|
|
|
|
|
|
|
|
If given nickname is not found it will be treated as a jid.
|
|
|
|
"""
|
|
|
|
if who in gajim.contacts.get_nick_list(self.account, self.room_jid):
|
|
|
|
contact = gajim.contacts.get_gc_contact(self.account, self.room_jid, who)
|
|
|
|
who = contact.jid
|
2009-09-17 03:38:39 +02:00
|
|
|
self.connection.gc_set_affiliation(self.room_jid, who, 'outcast', reason or str())
|
2009-09-13 16:43:44 +02:00
|
|
|
|
2009-09-17 03:38:39 +02:00
|
|
|
@command(raw=True, empty=True)
|
|
|
|
def kick(self, who, reason):
|
2009-09-13 16:43:44 +02:00
|
|
|
"""
|
|
|
|
Kick user by a nick from a groupchat
|
|
|
|
"""
|
|
|
|
if not who in gajim.contacts.get_nick_list(self.account, self.room_jid):
|
|
|
|
raise CommandError(_("Nickname not found"))
|
2009-09-17 03:38:39 +02:00
|
|
|
self.connection.gc_set_role(self.room_jid, who, 'none', reason or str())
|
2009-09-13 16:43:44 +02:00
|
|
|
|
|
|
|
@command
|
|
|
|
def names(self, verbose=False):
|
|
|
|
"""
|
|
|
|
Display names of all group chat occupants
|
|
|
|
"""
|
|
|
|
get_contact = lambda nick: gajim.contacts.get_gc_contact(self.account, self.room_jid, nick)
|
|
|
|
nicks = gajim.contacts.get_nick_list(self.account, self.room_jid)
|
|
|
|
|
|
|
|
# First we do alpha-numeric sort and then role-based one.
|
|
|
|
nicks.sort()
|
|
|
|
nicks.sort(key=lambda nick: get_contact(nick).role)
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
for nick in nicks:
|
|
|
|
contact = get_contact(nick)
|
|
|
|
|
|
|
|
role = helpers.get_uf_role(contact.role)
|
|
|
|
affiliation = helpers.get_uf_affiliation(contact.affiliation)
|
|
|
|
|
|
|
|
self.echo("%s - %s - %s" % (nick, role, affiliation))
|
|
|
|
else:
|
|
|
|
return ', '.join(nicks)
|
|
|
|
|
|
|
|
@command(raw=True)
|
|
|
|
def block(self, who):
|
|
|
|
"""
|
|
|
|
Forbid an occupant to send you public or private messages
|
|
|
|
"""
|
|
|
|
self.on_block(None, who)
|
|
|
|
|
|
|
|
@command(raw=True)
|
|
|
|
def unblock(self, who):
|
|
|
|
"""
|
|
|
|
Allow an occupant to send you public or privates messages
|
|
|
|
"""
|
|
|
|
self.on_unblock(None, who)
|