comparison jabberbot/xmppbot.py @ 2694:6500951853e2

Make "get page info" work with data forms.
author Karol Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 17 Aug 2007 23:29:52 +0200
parents da94375a625c
children 724b6b93f2f9
comparison
equal deleted inserted replaced
2693:dde1f1ee1d80 2694:6500951853e2
320 elif action == u'page_deleted': 320 elif action == u'page_deleted':
321 self.handle_deleted_action(cmd_data, jid, contact) 321 self.handle_deleted_action(cmd_data, jid, contact)
322 elif action == u'file_attached': 322 elif action == u'file_attached':
323 self.handle_attached_action(cmd_data, jid, contact) 323 self.handle_attached_action(cmd_data, jid, contact)
324 elif action == u'page_renamed': 324 elif action == u'page_renamed':
325 try: 325 self.handle_renamed_action(cmd_data, jid, contact)
326 self.handle_renamed_action(cmd_data, jid, contact)
327 except Exception, e:
328 import traceback
329 traceback.print_exc(e)
330 elif action == u'user_created': 326 elif action == u'user_created':
331 self.handle_user_created_action(cmd_data, jid, contact) 327 self.handle_user_created_action(cmd_data, jid, contact)
332 else: 328 else:
333 self.send_message(jid, cmd_data, command.msg_type) 329 self.send_message(jid, cmd_data, command.msg_type)
334 330
356 pagelist = u"\n".join(command.data) 352 pagelist = u"\n".join(command.data)
357 353
358 self.send_message(command.jid, {'text': msg % (pagelist, )}) 354 self.send_message(command.jid, {'text': msg % (pagelist, )})
359 355
360 elif isinstance(command, cmd.GetPageInfo): 356 elif isinstance(command, cmd.GetPageInfo):
361 intro = _("""Following detailed information on page "%(pagename)s" \ 357 self.handle_page_info(command)
362 is available:""")
363
364 if command.data['author'].startswith("Self:"):
365 author = command.data['author'][5:]
366 else:
367 author = command.data['author']
368
369 datestr = str(command.data['lastModified'])
370 date = u"%(year)s-%(month)s-%(day)s at %(time)s" % {
371 'year': datestr[:4],
372 'month': datestr[4:6],
373 'day': datestr[6:8],
374 'time': datestr[9:17],
375 }
376
377 msg = _("""Last author: %(author)s
378 Last modification: %(modification)s
379 Current version: %(version)s""") % {
380 'author': author,
381 'modification': date,
382 'version': command.data['version'],
383 }
384
385 self.send_message(command.jid, {'text': intro % {'pagename': command.pagename}})
386 self.send_message(command.jid, {'text': msg})
387 358
388 elif isinstance(command, cmd.GetUserLanguage): 359 elif isinstance(command, cmd.GetUserLanguage):
389 if command.jid in self.contacts: 360 if command.jid in self.contacts:
390 self.contacts[command.jid].language = command.language 361 self.contacts[command.jid].language = command.language
391 362
648 do_nothing = forms.Option("n", action1) 619 do_nothing = forms.Option("n", action1)
649 revert = forms.Option("r", action2) 620 revert = forms.Option("r", action2)
650 view_info = forms.Option("v", action3) 621 view_info = forms.Option("v", action3)
651 622
652 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions) 623 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
653 form.add_field(name="action", field_type="hidden", value="change_notify_action") 624 form.add_field(name='page_name', field_type='hidden', value=msg_data['page_name'])
654 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor")) 625 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
655 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment")) 626 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment"))
656 627
657 # Add lines of text as separate values, as recommended in XEP 628 # Add lines of text as separate values, as recommended in XEP
658 diff_lines = msg_data['diff'].split('\n') 629 diff_lines = msg_data['diff'].split('\n')
722 do_nothing = forms.Option("n", action1) 693 do_nothing = forms.Option("n", action1)
723 revert = forms.Option("r", action2) 694 revert = forms.Option("r", action2)
724 view_info = forms.Option("v", action3) 695 view_info = forms.Option("v", action3)
725 696
726 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions) 697 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
727 form.add_field(name="action", field_type="hidden", value="delete_notify_action")
728 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor")) 698 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
729 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment")) 699 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment"))
730 700
731 full_jid = JID(jid) 701 full_jid = JID(jid)
732 bare_jid = full_jid.bare().as_unicode() 702 bare_jid = full_jid.bare().as_unicode()
791 do_nothing = forms.Option("n", action1) 761 do_nothing = forms.Option("n", action1)
792 revert = forms.Option("r", action2) 762 revert = forms.Option("r", action2)
793 view_info = forms.Option("v", action3) 763 view_info = forms.Option("v", action3)
794 764
795 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions) 765 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
796 form.add_field(name="action", field_type="hidden", value="change_notify_action")
797 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor")) 766 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
798 form.add_field(name='page', field_type='text-single', value=msg_data['page_name'], label=_("Page name")) 767 form.add_field(name='page', field_type='text-single', value=msg_data['page_name'], label=_("Page name"))
799 form.add_field(name='name', field_type='text-single', value=msg_data['attach_name'], label=_("File name")) 768 form.add_field(name='name', field_type='text-single', value=msg_data['attach_name'], label=_("File name"))
800 form.add_field(name='size', field_type='text-single', value=msg_data['attach_size'], label=_("File size")) 769 form.add_field(name='size', field_type='text-single', value=msg_data['attach_size'], label=_("File size"))
801 770
861 do_nothing = forms.Option("n", action1) 830 do_nothing = forms.Option("n", action1)
862 revert = forms.Option("r", action2) 831 revert = forms.Option("r", action2)
863 view_info = forms.Option("v", action3) 832 view_info = forms.Option("v", action3)
864 833
865 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions) 834 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
866 form.add_field(name="action", field_type="hidden", value="change_notify_action")
867 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor")) 835 form.add_field(name='editor', field_type='text-single', value=msg_data['editor'], label=_("Editor"))
868 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment")) 836 form.add_field(name='comment', field_type='text-single', value=msg_data.get('comment', ''), label=_("Comment"))
869 form.add_field(name='old', field_type='text-single', value=msg_data['old_name'], label=_("Old name")) 837 form.add_field(name='old', field_type='text-single', value=msg_data['old_name'], label=_("Old name"))
870 form.add_field(name='new', field_type='text-single', value=msg_data['page_name'], label=_("New name")) 838 form.add_field(name='new', field_type='text-single', value=msg_data['page_name'], label=_("New name"))
871 839
908 } 876 }
909 877
910 data = {'text': message, 'subject': msg_data['subject']} 878 data = {'text': message, 'subject': msg_data['subject']}
911 self.send_message(jid, data, u"message") 879 self.send_message(jid, data, u"message")
912 880
881 def handle_page_info(self, command):
882 """Handles GetPageInfo commands
883
884 @param command: a command instance
885 @type command: jabberbot.commands.GetPageInfo
886
887 """
888 # Process command data first so it can be directly usable
889 if command.data['author'].startswith("Self:"):
890 command.data['author'] = command.data['author'][5:]
891
892 datestr = str(command.data['lastModified'])
893 command.data['lastModified'] = u"%(year)s-%(month)s-%(day)s at %(time)s" % {
894 'year': datestr[:4],
895 'month': datestr[4:6],
896 'day': datestr[6:8],
897 'time': datestr[9:17],
898 }
899
900 if command.presentation == u"text":
901 self.send_pageinfo_text(command)
902 elif command.presentation == u"dataforms":
903 self.send_pageinfo_form(command)
904
905 else:
906 raise ValueError("presentation value '%s' is not supported!" % (command.presentation, ))
907
908 def send_pageinfo_text(self, command):
909 """Sends detailed page info with plain text
910
911 @param command: command with detailed data
912 @type command: jabberbot.command.GetPageInfo
913
914 """
915 _ = self.get_text(command.jid)
916
917 intro = _("""Following detailed information on page "%(pagename)s" \
918 is available:""")
919
920 msg = _("""Last author: %(author)s
921 Last modification: %(modification)s
922 Current version: %(version)s""") % {
923 'author': command.data['author'],
924 'modification': command.data['lastModified'],
925 'version': command.data['version'],
926 }
927
928 self.send_message(command.jid, {'text': intro % {'pagename': command.pagename}})
929 self.send_message(command.jid, {'text': msg})
930
931 def send_pageinfo_form(self, command):
932 """Sends page info using Data Forms
933
934
935 """
936 _ = self.get_text(command.jid)
937 data = command.data
938
939 form_title = _("Detailed page information").encode("utf-8")
940 instructions = _("Submit this form with a specified action to continue.").encode("utf-8")
941 action_label = _("What to do next")
942
943 action1 = _("Do nothing")
944 action2 = _("Get page contents")
945 action3 = _("Get page contents (HTML)")
946
947 do_nothing = forms.Option("n", action1)
948 get_content = forms.Option("c", action2)
949 get_content_html = forms.Option("h", action3)
950
951 form = forms.Form(xmlnode_or_type="form", title=form_title, instructions=instructions)
952 form.add_field(name='pagename', field_type='text-single', value=command.pagename, label=_("Page name"))
953 form.add_field(name="changed", field_type='text-single', value=data['lastModified'], label=_("Last changed"))
954 form.add_field(name='editor', field_type='text-single', value=data['author'], label=_("Last editor"))
955 form.add_field(name='version', field_type='text-single', value=data['version'], label=_("Current version"))
956
957 # full_jid = JID(jid)
958 # bare_jid = full_jid.bare().as_unicode()
959 # resource = full_jid.resource
960
961 # Add URLs as OOB data if it's supported and as separate fields otherwise
962 # if bare_jid in self.contacts and self.contacts[bare_jid].supports(resource, u'jabber:x:oob'):
963 # url_list = msg_data['url_list']
964 # else:
965 # url_list = []
966 #
967 # for number, url in enumerate(msg_data['url_list']):
968 # field_name = "url%d" % (number, )
969 # form.add_field(name=field_name, field_type="text-single", value=url["url"], label=url["description"])
970
971 # Selection of a following action
972 form.add_field(name="options", field_type="list-single", options=[do_nothing, get_content, get_content_html], label=action_label)
973
974 self.send_form(command.jid, form, _("Detailed page information"))
913 975
914 def is_internal(self, command): 976 def is_internal(self, command):
915 """Check if a given command is internal 977 """Check if a given command is internal
916 978
917 @type command: unicode 979 @type command: unicode
974 form = forms.Form(form_node) 1036 form = forms.Form(form_node)
975 1037
976 if form.type != u"submit": 1038 if form.type != u"submit":
977 return 1039 return
978 1040
979 try: 1041 if "action" in form:
980 action = form["action"].value 1042 action = form["action"].value
981 except KeyError: 1043 if action == u"search":
982 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')} 1044 self.handle_search_form(jid, form)
983 self.send_message(jid.as_unicode(), data, u"message") 1045 else:
984 return 1046 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')}
985 1047 self.send_message(jid.as_unicode(), data, u"message")
986 if action == u"search": 1048 elif "options" in form:
987 self.handle_search_form(jid, form) 1049 option = form["options"].value
988 else: 1050
989 data = {'text': _('The form you submitted was invalid!'), 'subject': _('Invalid data')} 1051 # View page info
990 self.send_message(jid.as_unicode(), data, u"message") 1052 if option == "v":
1053 command = cmd.GetPageInfo(jid.as_unicode(), form["page_name"].value, presentation="dataforms")
1054 self.from_commands.put_nowait(command)
991 1055
992 1056
993 def handle_search_form(self, jid, form): 1057 def handle_search_form(self, jid, form):
994 """Handles a search form 1058 """Handles a search form
995 1059