root/trunk/jjigw/common.py

Revision 165, 2.9 kB (checked in by jajcus, 2 years ago)

- addresses updated

Line 
1 #!/usr/bin/python -u
2 #
3 #  Jajcus' Jabber to IRC Gateway
4 #  Copyright (C) 2004  Jacek Konieczny <jajcus@jajcus.net>
5 #
6 #  This program is free software; you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation; either version 2 of the License, or
9 #  (at your option) any later version.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License along
17 #  with this program; if not, write to the Free Software Foundation, Inc.,
18 #  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20
21 import re
22 import string
23 from types import StringType,UnicodeType
24
25 class JJIGWFatalError(RuntimeError):
26     pass
27
28 evil_characters_re=re.compile(r"[\000-\010\013\014\016-\037]")
29 def remove_evil_characters(s):
30     return evil_characters_re.sub(" ",s)
31
32 color_re=re.compile(r"\x03\d\d|\x0f")
33 def strip_colors(s):
34     return color_re.sub("",s)
35
36 numeric_re=re.compile(r"\d\d\d")
37 channel_re=re.compile(r"^[&#+!][^\000 \007 ,:\r\n]+$")
38 nick_re=re.compile(r"^[a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}][a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}0-9-]*$")
39 nick8_re=re.compile(r"^[a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}\x80-\xff][a-zA-Z\x5b-\x60\x7b-\x7d\[\]\\`_^{|}0-9\x80-\xff-]*$")
40
41 def escape_node_string(s):
42     s=s.replace(",quot,",'"')
43     s=s.replace(",amp,","&")
44     s=s.replace(",apos,","'")
45     s=s.replace(",slash,","/")
46     s=s.replace(",lt,","<")
47     s=s.replace(",gt,",">")
48     s=s.replace(",at,","@")
49     return s
50
51 def unescape_node_string(s):
52     s=s.replace('"',",quot,")
53     s=s.replace("&",",amp,")
54     s=s.replace("'",",apos,")
55     s=s.replace("/",",slash,")
56     s=s.replace("<",",lt,")
57     s=s.replace(">",",gt,")
58     s=s.replace("@",",at,")
59     return s
60
61 def node_to_channel(n,encoding):
62     s=n.encode(encoding,"strict")
63     s=escape_node_string(s)
64     if not channel_re.match(s):
65         raise ValueError,"Bad channel name: %r" % (s,)
66     return s
67
68 def channel_to_node(ch,encoding):
69     s=unescape_node_string(ch)
70     n=unicode(s,encoding,"strict")
71     return n
72
73 def node_to_nick(n,encoding,network):
74     s=n.encode(encoding,"strict")
75     s=escape_node_string(s)
76     if not network.valid_nick(s):
77         raise ValueError,"Bad nick name: %r" % (s,)
78     return s
79
80 def nick_to_node(ch,encoding):
81     s=unescape_node_string(ch)
82     n=unicode(s,encoding,"strict")
83     return n
84
85 irc_translate_table=string.maketrans(
86         string.ascii_uppercase+"[]\\~",
87         string.ascii_lowercase+"{}|^")
88
89 def normalize(s):
90     return s.translate(irc_translate_table)
91
92 class ConnectionInfo:
93     def __init__(self,socket,user):
94         self.localip,self.localport=socket.getsockname()
95         self.remoteip,self.remoteport=socket.getpeername()
96         self.user=user
97
98
99 # vi: sts=4 et sw=4
100
Note: See TracBrowser for help on using the browser.