| | 22 | evil_characters_re=re.compile(r"[\000-\010\013\014\016-\037]") |
|---|
| | 23 | def remove_evil_characters(s): |
|---|
| | 24 | return evil_characters_re.sub(" ",s) |
|---|
| | 25 | |
|---|
| | 26 | color_re=re.compile(r"\x03\d\d|\x0f") |
|---|
| | 27 | def strip_colors(s): |
|---|
| | 28 | return color_re.sub("",s) |
|---|
| | 29 | |
|---|
| | 30 | numeric_re=re.compile(r"\d\d\d") |
|---|
| | 31 | channel_re=re.compile(r"^[&#+!][^\000 \007 ,:\r\n]{1,49}$") |
|---|
| | 32 | nick_re=re.compile(r"^[a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}][a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}0-9-]{0,8}$") |
|---|
| | 33 | |
|---|
| | 34 | def escape_node_string(s): |
|---|
| | 35 | s=s.replace(",quot,",'"') |
|---|
| | 36 | s=s.replace(",amp,","&") |
|---|
| | 37 | s=s.replace(",apos,","'") |
|---|
| | 38 | s=s.replace(",slash,","/") |
|---|
| | 39 | s=s.replace(",lt,","<") |
|---|
| | 40 | s=s.replace(",gt,",">") |
|---|
| | 41 | s=s.replace(",at,","@") |
|---|
| | 42 | return s |
|---|
| | 43 | |
|---|
| | 44 | def unescape_node_string(s): |
|---|
| | 45 | s=s.replace('"',",quot,") |
|---|
| | 46 | s=s.replace("&",",amp,") |
|---|
| | 47 | s=s.replace("'",",apos,") |
|---|
| | 48 | s=s.replace("/",",slash,") |
|---|
| | 49 | s=s.replace("<",",lt,") |
|---|
| | 50 | s=s.replace(">",",gt,") |
|---|
| | 51 | s=s.replace("@",",at,") |
|---|
| | 52 | return s |
|---|
| | 53 | |
|---|
| | 54 | def node_to_channel(n,encoding): |
|---|
| | 55 | s=n.encode(encoding,"strict") |
|---|
| | 56 | s=escape_node_string(s) |
|---|
| | 57 | if not channel_re.match(s): |
|---|
| | 58 | raise ValueError,"Bad channel name: %r" % (s,) |
|---|
| | 59 | return s |
|---|
| | 60 | |
|---|
| | 61 | def channel_to_node(ch,encoding): |
|---|
| | 62 | s=unescape_node_string(ch) |
|---|
| | 63 | n=unicode(s,encoding,"strict") |
|---|
| | 64 | return n |
|---|
| | 65 | |
|---|
| | 66 | def node_to_nick(n,encoding): |
|---|
| | 67 | s=n.encode(encoding,"strict") |
|---|
| | 68 | s=escape_node_string(s) |
|---|
| | 69 | if not nick_re.match(s): |
|---|
| | 70 | raise ValueError,"Bad nick name: %r" % (s,) |
|---|
| | 71 | return s |
|---|
| | 72 | |
|---|
| | 73 | def nick_to_node(ch,encoding): |
|---|
| | 74 | s=unescape_node_string(ch) |
|---|
| | 75 | n=unicode(s,encoding,"strict") |
|---|
| | 76 | return n |
|---|
| | 77 | |
|---|
| | 78 | irc_translate_table=string.maketrans( |
|---|
| | 79 | string.ascii_uppercase+"[]\\~", |
|---|
| | 80 | string.ascii_lowercase+"{}|^") |
|---|
| | 81 | |
|---|
| | 82 | def normalize(s): |
|---|
| | 83 | return s.translate(irc_translate_table) |
|---|
| | 84 | |
|---|
| | 85 | |
|---|
| 66 | | |
|---|
| 67 | | evil_characters_re=re.compile(r"[\000-\010\013\014\016-\037]") |
|---|
| 68 | | def remove_evil_characters(s): |
|---|
| 69 | | return evil_characters_re.sub(" ",s) |
|---|
| 70 | | |
|---|
| 71 | | color_re=re.compile(r"\x03\d\d|\x0f") |
|---|
| 72 | | def strip_colors(s): |
|---|
| 73 | | return color_re.sub("",s) |
|---|
| 74 | | |
|---|
| 75 | | numeric_re=re.compile(r"\d\d\d") |
|---|
| 76 | | channel_re=re.compile(r"^[&#+!][^\000 \007 ,:\r\n]{1,49}$") |
|---|
| 77 | | nick_re=re.compile(r"^[a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}][a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}0-9-]{0,8}$") |
|---|
| 78 | | |
|---|
| 79 | | def escape_node_string(s): |
|---|
| 80 | | s=s.replace(",quot,",'"') |
|---|
| 81 | | s=s.replace(",amp,","&") |
|---|
| 82 | | s=s.replace(",apos,","'") |
|---|
| 83 | | s=s.replace(",slash,","/") |
|---|
| 84 | | s=s.replace(",lt,","<") |
|---|
| 85 | | s=s.replace(",gt,",">") |
|---|
| 86 | | s=s.replace(",at,","@") |
|---|
| 87 | | return s |
|---|
| 88 | | |
|---|
| 89 | | def unescape_node_string(s): |
|---|
| 90 | | s=s.replace('"',",quot,") |
|---|
| 91 | | s=s.replace("&",",amp,") |
|---|
| 92 | | s=s.replace("'",",apos,") |
|---|
| 93 | | s=s.replace("/",",slash,") |
|---|
| 94 | | s=s.replace("<",",lt,") |
|---|
| 95 | | s=s.replace(">",",gt,") |
|---|
| 96 | | s=s.replace("@",",at,") |
|---|
| 97 | | return s |
|---|
| 98 | | |
|---|
| 99 | | def node_to_channel(n,encoding): |
|---|
| 100 | | s=n.encode(encoding,"strict") |
|---|
| 101 | | s=escape_node_string(s) |
|---|
| 102 | | if not channel_re.match(s): |
|---|
| 103 | | raise ValueError,"Bad channel name: %r" % (s,) |
|---|
| 104 | | return s |
|---|
| 105 | | |
|---|
| 106 | | def channel_to_node(ch,encoding): |
|---|
| 107 | | s=unescape_node_string(ch) |
|---|
| 108 | | n=unicode(s,encoding,"strict") |
|---|
| 109 | | return n |
|---|
| 110 | | |
|---|
| 111 | | def node_to_nick(n,encoding): |
|---|
| 112 | | s=n.encode(encoding,"strict") |
|---|
| 113 | | s=escape_node_string(s) |
|---|
| 114 | | if not nick_re.match(s): |
|---|
| 115 | | raise ValueError,"Bad nick name: %r" % (s,) |
|---|
| 116 | | return s |
|---|
| 117 | | |
|---|
| 118 | | def nick_to_node(ch,encoding): |
|---|
| 119 | | s=unescape_node_string(ch) |
|---|
| 120 | | n=unicode(s,encoding,"strict") |
|---|
| 121 | | return n |
|---|
| 122 | | |
|---|
| 123 | | irc_translate_table=string.maketrans( |
|---|
| 124 | | string.ascii_uppercase+"[]\\~", |
|---|
| 125 | | string.ascii_lowercase+"{}|^") |
|---|
| 126 | | |
|---|
| 127 | | def normalize(s): |
|---|
| 128 | | return s.translate(irc_translate_table) |
|---|
| 902 | | channel=stanza.get_to().node |
|---|
| 903 | | channel=node_to_channel(channel,self.default_encoding) |
|---|
| 904 | | if not channel_re.match(channel): |
|---|
| 905 | | debug("Bad channel name: %r" % (channel,)) |
|---|
| 906 | | return |
|---|
| 907 | | body=stanza.get_body().encode(self.default_encoding,"replace") |
|---|
| | 923 | channel_name=stanza.get_to().node |
|---|
| | 924 | channel_name=node_to_channel(channel_name,self.default_encoding) |
|---|
| | 925 | if not channel_re.match(channel_name): |
|---|
| | 926 | self.debug("Bad channel name: %r" % (channel_name,)) |
|---|
| | 927 | return |
|---|
| | 928 | channel=self.channels.get(normalize(channel_name)) |
|---|
| | 929 | if channel: |
|---|
| | 930 | encoding=channel.encoding |
|---|
| | 931 | else: |
|---|
| | 932 | encoding=self.default_encoding |
|---|
| | 933 | body=stanza.get_body().encode(encoding,"replace") |
|---|