Swept everything related to commands

This commit is contained in:
red-agent 2009-09-11 04:54:26 +03:00
parent faf3a1fe6f
commit c38e7050f5
2 changed files with 17 additions and 474 deletions

View File

@ -603,35 +603,14 @@ class ChatControlBase(MessageControl):
self.drag_entered_conv = True
self.conv_textview.tv.set_editable(True)
def _process_command(self, message):
if not message or message[0] != '/':
return False
message = message[1:]
message_array = message.split(' ', 1)
command = message_array.pop(0).lower()
if message_array == ['']:
message_array = []
if command == 'clear' and not len(message_array):
self.conv_textview.clear() # clear conversation
self.clear(self.msg_textview) # clear message textview too
return True
elif message == 'compact' and not len(message_array):
self.chat_buttons_set_visible(not self.hide_chat_buttons)
self.clear(self.msg_textview)
return True
return False
def send_message(self, message, keyID='', type_='chat', chatstate=None,
msg_id=None, composing_xep=None, resource=None, process_command=True,
msg_id=None, composing_xep=None, resource=None,
xhtml=None, callback=None, callback_args=[]):
'''Send the given message to the active tab. Doesn't return None if error
'''
if not message or message == '\n':
return None
if not process_command or not self._process_command(message):
MessageControl.send_message(self, message, keyID, type_=type_,
chatstate=chatstate, msg_id=msg_id, composing_xep=composing_xep,
resource=resource, user_nick=self.user_nick, xhtml=xhtml,
@ -1129,7 +1108,6 @@ class ChatControl(ChatControlBase):
'''A control for standard 1-1 chat'''
TYPE_ID = message_control.TYPE_CHAT
old_msg_kind = None # last kind of the printed message
CHAT_CMDS = ['clear', 'compact', 'help', 'me', 'ping', 'say']
def __init__(self, parent_win, contact, acct, session, resource = None):
ChatControlBase.__init__(self, self.TYPE_ID, parent_win,
@ -1718,83 +1696,11 @@ class ChatControl(ChatControlBase):
elif self.session and self.session.enable_encryption:
dialogs.ESessionInfoWindow(self.session)
def _process_command(self, message):
if message[0] != '/':
return False
# Handle common commands
if ChatControlBase._process_command(self, message):
return True
message = message[1:]
message_array = message.split(' ', 1)
command = message_array.pop(0).lower()
if message_array == ['']:
message_array = []
if command == 'me':
if len(message_array):
return False # /me is not really a command
else:
self.get_command_help(command)
return True # do not send "/me" as message
if command == 'help':
if len(message_array):
subcommand = message_array.pop(0)
self.get_command_help(subcommand)
else:
self.get_command_help(command)
self.clear(self.msg_textview)
return True
elif command == 'ping':
if not len(message_array):
if self.account == gajim.ZEROCONF_ACC_NAME:
self.print_conversation(
_('Command not supported for zeroconf account.'), 'info')
else:
gajim.connections[self.account].sendPing(self.contact)
else:
self.get_command_help(command)
self.clear(self.msg_textview)
return True
return False
def get_command_help(self, command):
if command == 'help':
self.print_conversation(_('Commands: %s') % ChatControl.CHAT_CMDS,
'info')
elif command == 'clear':
self.print_conversation(_('Usage: /%s, clears the text window.') % \
command, 'info')
elif command == 'compact':
self.print_conversation(_('Usage: /%s, hide the chat buttons.') % \
command, 'info')
elif command == 'me':
self.print_conversation(_('Usage: /%(command)s <action>, sends action '
'to the current group chat. Use third person. (e.g. /%(command)s '
'explodes.)'
) % {'command': command}, 'info')
elif command == 'ping':
self.print_conversation(_('Usage: /%s, sends a ping to the contact') %\
command, 'info')
elif command == 'say':
self.print_conversation(_('Usage: /%s, send the message to the contact') %\
command, 'info')
else:
self.print_conversation(_('No help info for /%s') % command, 'info')
def send_message(self, message, keyID='', chatstate=None, xhtml=None):
'''Send a message to contact'''
if message in ('', None, '\n') or self._process_command(message):
if message in ('', None, '\n'):
return None
# Do we need to process command for the message ?
process_command = True
if message.startswith('/say'):
message = message[5:]
process_command = False
# refresh timers
self.reset_kbd_mouse_timeout_vars()
@ -1853,7 +1759,7 @@ class ChatControl(ChatControlBase):
ChatControlBase.send_message(self, message, keyID, type_='chat',
chatstate=chatstate_to_send, composing_xep=composing_xep,
process_command=process_command, xhtml=xhtml, callback=_on_sent,
xhtml=xhtml, callback=_on_sent,
callback_args=[contact, message, encrypted, xhtml])
def check_for_possible_paused_chatstate(self, arg):

View File

@ -182,10 +182,6 @@ class PrivateChatControl(ChatControl):
class GroupchatControl(ChatControlBase):
TYPE_ID = message_control.TYPE_GC
# alphanum sorted
MUC_CMDS = ['ban', 'block', 'chat', 'query', 'clear', 'close', 'compact',
'help', 'invite', 'join', 'kick', 'leave', 'me', 'msg', 'nick',
'part', 'names', 'say', 'topic', 'unblock']
def __init__(self, parent_win, contact, acct, is_continued=False):
ChatControlBase.__init__(self, self.TYPE_ID, parent_win,
@ -281,7 +277,6 @@ class GroupchatControl(ChatControlBase):
self.attention_list = []
self.room_creation = int(time.time()) # Use int to reduce mem usage
self.nick_hits = []
self.cmd_hits = []
self.last_key_tabs = False
self.subject = ''
@ -1510,262 +1505,6 @@ class GroupchatControl(ChatControlBase):
if model.iter_n_children(parent_iter) == 0:
model.remove(parent_iter)
def _process_command(self, message):
if message[0] != '/':
return False
# Handle common commands
if ChatControlBase._process_command(self, message):
return True
message = message[1:]
message_array = message.split(' ', 1)
command = message_array.pop(0).lower()
if message_array == ['']:
message_array = []
if command == 'me':
return False # This is not really a command
if command == 'nick':
# example: /nick foo
if len(message_array) and message_array[0] != self.nick:
nick = message_array[0]
try:
nick = helpers.parse_resource(nick)
except Exception:
# Invalid Nickname
dialogs.ErrorDialog(_('Invalid nickname'),
_('The nickname has not allowed characters.'))
return True
gajim.connections[self.account].join_gc(nick, self.room_jid, None,
change_nick=True)
self.new_nick = nick
self.clear(self.msg_textview)
else:
self.get_command_help(command)
return True
elif command == 'query' or command == 'chat':
# Open a chat window to the specified nick
# example: /query foo
if len(message_array):
nick0 = message_array.pop(0)
if nick0[-1] == ' ':
nick1 = nick0[:-1]
else:
nick1 = nick0
nicks = gajim.contacts.get_nick_list(self.account, self.room_jid)
for nick in (nick0, nick1):
if nick in nicks:
self.on_send_pm(nick=nick)
self.clear(self.msg_textview)
return True
self.print_conversation(_('Nickname not found: %s') % \
nick0, 'info')
else:
self.get_command_help(command)
return True
elif command == 'msg':
# Send a message to a nick. Also opens a private message window.
# example: /msg foo Hey, what's up?
if len(message_array):
message_array = message_array[0].split()
nick = message_array.pop(0)
room_nicks = gajim.contacts.get_nick_list(self.account,
self.room_jid)
if nick in room_nicks:
privmsg = ' '.join(message_array)
self.on_send_pm(nick=nick, msg=privmsg)
self.clear(self.msg_textview)
else:
self.print_conversation(_('Nickname not found: %s') % nick,
'info')
else:
self.get_command_help(command)
return True
elif command == 'topic':
# display or change the room topic
# example: /topic : print topic
# /topic foo : change topic to foo
if len(message_array):
new_topic = message_array.pop(0)
gajim.connections[self.account].send_gc_subject(self.room_jid,
new_topic)
elif self.subject is not '':
self.print_conversation(self.subject, 'info')
else:
self.print_conversation(_('This group chat has no subject'), 'info')
self.clear(self.msg_textview)
return True
elif command == 'invite':
# invite a user to a room for a reason
# example: /invite user@example.com reason
if len(message_array):
message_array = message_array[0].split()
invitee = message_array.pop(0)
reason = ' '.join(message_array)
gajim.connections[self.account].send_invite(self.room_jid, invitee,
reason)
s = _('Invited %(contact_jid)s to %(room_jid)s.') % {
'contact_jid': invitee,
'room_jid': self.room_jid}
self.print_conversation(s, 'info')
self.clear(self.msg_textview)
else:
self.get_command_help(command)
return True
elif command == 'join':
# example: /join room@conference.example.com/nick
if len(message_array):
room_jid = message_array[0]
if room_jid.find('@') < 0:
room_jid = room_jid + '@' + gajim.get_server_from_jid(
self.room_jid)
else:
room_jid = '@' + gajim.get_server_from_jid(self.room_jid)
if room_jid.find('/') >= 0:
room_jid, nick = room_jid.split('/', 1)
else:
nick = ''
# join_gc window is needed in order to provide for password entry.
if 'join_gc' in gajim.interface.instances[self.account]:
gajim.interface.instances[self.account]['join_gc'].\
window.present()
else:
try:
dialogs.JoinGroupchatWindow(account=None, room_jid=room_jid,
nick=nick)
except GajimGeneralException:
pass
self.clear(self.msg_textview)
return True
elif command == 'leave' or command == 'part' or command == 'close':
# Leave the room and close the tab or window
reason = 'offline'
if len(message_array):
reason = message_array.pop(0)
self.parent_win.remove_tab(self, self.parent_win.CLOSE_COMMAND, reason)
self.clear(self.msg_textview)
return True
elif command == 'ban':
if len(message_array):
room_nicks = gajim.contacts.get_nick_list(self.account,
self.room_jid)
nb_match = 0
nick_ban = ''
for nick in room_nicks:
if message_array[0].startswith(nick):
nb_match += 1
nick_ban = nick
test_reason = message_array[0][len(nick) + 1:]
if len(test_reason) == 0:
reason = 'None'
else:
reason = test_reason
banned_jid = None
if nb_match == 1:
gc_contact = gajim.contacts.get_gc_contact(self.account,
self.room_jid, nick_ban)
banned_jid = gc_contact.jid
elif nb_match > 1:
self.print_conversation(_('There is an ambiguity: %d nicks '
'match.\n Please use graphical interface ') % nb_match,
'info')
self.clear(self.msg_textview)
elif message_array[0].split()[0].find('@') > 0:
message_splited = message_array[0].split(' ', 1)
banned_jid = message_splited[0]
if len(message_splited) == 2:
reason = message_splited[1]
else:
reason = 'None'
if banned_jid:
gajim.connections[self.account].gc_set_affiliation(self.room_jid,
banned_jid, 'outcast', reason)
self.clear(self.msg_textview)
else:
self.print_conversation(_('Nickname not found'), 'info')
else:
self.get_command_help(command)
return True
elif command == 'kick':
if len(message_array):
nick_kick = ''
room_nicks = gajim.contacts.get_nick_list(self.account,
self.room_jid)
nb_match = 0
for nick in room_nicks:
if message_array[0].startswith(nick):
nb_match += 1
nick_kick = nick
test_reason = message_array[0][len(nick) + 1:]
if len(test_reason) == 0:
reason = 'None'
else:
reason = test_reason
if nb_match == 1:
gajim.connections[self.account].gc_set_role(self.room_jid,
nick_kick, 'none', reason)
self.clear(self.msg_textview)
elif nb_match > 1:
self.print_conversation(_('There is an ambiguity: %d nicks '
'match.\n Please use graphical interface') % nb_match ,
'info' )
self.clear(self.msg_textview)
else:
# We can't do the difference between nick and reason
# So we don't say the nick
self.print_conversation(_('Nickname not found') , 'info')
else:
self.get_command_help(command)
return True
elif command == 'names':
# print the list of participants
nicklist=''
i=0
for contact in self.iter_contact_rows():
nicklist += '[ %-12.12s ] ' % (contact[C_NICK].decode('utf-8'))
i=i+1
if i == 3:
i=0
self.print_conversation(nicklist, 'info')
nicklist=''
if nicklist:
self.print_conversation(nicklist, 'info')
self.clear(self.msg_textview)
return True
elif command == 'help':
if len(message_array):
subcommand = message_array.pop(0)
self.get_command_help(subcommand)
else:
self.get_command_help(command)
self.clear(self.msg_textview)
return True
elif command == 'say':
gajim.connections[self.account].send_gc_message(self.room_jid,
message[4:])
self.clear(self.msg_textview)
return True
elif command == 'block':
if len(message_array) == 0:
self.get_command_help(command)
return True
nick = message_array[0].strip()
self.on_block(None, nick)
self.clear(self.msg_textview)
return True
elif command == 'unblock':
if len(message_array) == 0:
self.get_command_help(command)
return True
nick = message_array[0].strip()
self.on_unblock(None, nick)
self.clear(self.msg_textview)
return True
return False
def send_message(self, message, xhtml=None):
'''call this function to send our message'''
if not message:
@ -1779,79 +1518,12 @@ class GroupchatControl(ChatControlBase):
if message != '' or message != '\n':
self.save_sent_message(message)
if not self._process_command(message):
# Send the message
gajim.connections[self.account].send_gc_message(self.room_jid,
message, xhtml=xhtml)
self.msg_textview.get_buffer().set_text('')
self.msg_textview.grab_focus()
def get_command_help(self, command):
if command == 'help':
self.print_conversation(_('Commands: %s') % GroupchatControl.MUC_CMDS,
'info')
elif command == 'ban':
s = _('Usage: /%s <nickname|JID> [reason], bans the JID from the group'
' chat. The nickname of an occupant may be substituted, but not if '
'it contains "@". If the JID is currently in the group chat, '
'he/she/it will also be kicked.') % command
self.print_conversation(s, 'info')
elif command == 'chat' or command == 'query':
self.print_conversation(_('Usage: /%s <nickname>, opens a private chat'
' window with the specified occupant.') % command, 'info')
elif command == 'clear':
self.print_conversation(
_('Usage: /%s, clears the text window.') % command, 'info')
elif command == 'close' or command == 'leave' or command == 'part':
self.print_conversation(_('Usage: /%s [reason], closes the current '
'window or tab, displaying reason if specified.') % command, 'info')
elif command == 'compact':
self.print_conversation(_('Usage: /%s, hide the chat buttons.') % \
command, 'info')
elif command == 'invite':
self.print_conversation(_('Usage: /%s <JID> [reason], invites JID to '
'the current group chat, optionally providing a reason.') % command,
'info')
elif command == 'join':
self.print_conversation(_('Usage: /%s <room>@<server>[/nickname], '
'offers to join room@server optionally using specified nickname.') \
% command, 'info')
elif command == 'kick':
self.print_conversation(_('Usage: /%s <nickname> [reason], removes '
'the occupant specified by nickname from the group chat and '
'optionally displays a reason.') % command, 'info')
elif command == 'me':
self.print_conversation(_('Usage: /%(command)s <action>, sends action '
'to the current group chat. Use third person. (e.g. /%(command)s '
'explodes.)') % {'command': command}, 'info')
elif command == 'msg':
s = _('Usage: /%s <nickname> [message], opens a private message window'
' and sends message to the occupant specified by nickname.') % \
command
self.print_conversation(s, 'info')
elif command == 'nick':
s = _('Usage: /%s <nickname>, changes your nickname in current group '
'chat.') % command
self.print_conversation(s, 'info')
elif command == 'names':
s = _('Usage: /%s , display the names of group chat occupants.')\
% command
self.print_conversation(s, 'info')
elif command == 'topic':
self.print_conversation(_('Usage: /%s [topic], displays or updates the'
' current group chat topic.') % command, 'info')
elif command == 'say':
self.print_conversation(_('Usage: /%s <message>, sends a message '
'without looking for other commands.') % command, 'info')
elif command == 'block':
self.print_conversation(_('Usage: /%s <nickname>, prevent <nickname> '
'to send you messages or private messages.') % command, 'info')
elif command == 'unblock':
self.print_conversation(_('Usage: /%s <nickname>, allow <nickname> '
'to send you messages and private messages.') % command, 'info')
else:
self.print_conversation(_('No help info for /%s') % command, 'info')
def get_role(self, nick):
gc_contact = gajim.contacts.get_gc_contact(self.account, self.room_jid,
nick)
@ -2100,41 +1772,6 @@ class GroupchatControl(ChatControlBase):
'utf-8')
splitted_text = text.split()
# topic completion
splitted_text2 = text.split(None, 1)
if text.startswith('/topic '):
if len(splitted_text2) == 2 and \
self.subject.startswith(splitted_text2[1]) and\
len(self.subject) > len(splitted_text2[1]):
message_buffer.insert_at_cursor(
self.subject[len(splitted_text2[1]):])
return True
elif len(splitted_text2) == 1 and text.startswith('/topic '):
message_buffer.delete(start_iter, end_iter)
message_buffer.insert_at_cursor('/topic '+self.subject)
return True
# command completion
if text.startswith('/') and len(splitted_text) == 1:
text = splitted_text[0]
if len(text) == 1: # user wants to cycle all commands
self.cmd_hits = GroupchatControl.MUC_CMDS
else:
# cycle possible commands depending on what the user typed
if self.last_key_tabs and len(self.cmd_hits) and \
self.cmd_hits[0].startswith(text.lstrip('/')):
self.cmd_hits.append(self.cmd_hits[0])
self.cmd_hits.pop(0)
else: # find possible commands
self.cmd_hits = []
for cmd in GroupchatControl.MUC_CMDS:
if cmd.startswith(text.lstrip('/')):
self.cmd_hits.append(cmd)
if len(self.cmd_hits):
message_buffer.delete(start_iter, end_iter)
message_buffer.insert_at_cursor('/' + self.cmd_hits[0] + ' ')
self.last_key_tabs = True
return True
# nick completion
# check if tab is pressed with empty message